📄 frame.zc
字号:
//[of]:license
//[c]Code Browser - a folding text editor for programmers
//[c]Copyright (C) 2003-07 Marc Kerbiquet
//[c]
//[c]This program is free software; you can redistribute it and/or modify
//[c]it under the terms of the GNU General Public License as published by
//[c]the Free Software Foundation; either version 2 of the License, or
//[c](at your option) any later version.
//[c]
//[c]This program is distributed in the hope that it will be useful,
//[c]but WITHOUT ANY WARRANTY; without even the implied warranty of
//[c]MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//[c]GNU General Public License for more details.
//[c]
//[c]You should have received a copy of the GNU General Public License
//[c]along with this program; if not, write to the Free Software
//[c]Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//[cf]
//[of]:imports
import "base/types"
import "base/memory"
import "base/memory-allocator"
import "collection/vector"
import "text/string"
import "text/string-buffer"
//[c]
import "graphics/geometry"
import "user/box"
import "user/command"
import "user/menu"
import "user/keyboard"
import "user/accelerator"
import "private/sys-menu"
//[c]
import "win32/windows"
//[cf]
//[c]
//[of]:manager
//[of]:description
//[c]Functions to find and communicates with another instance.
//[cf]
//[of]:private
func property name (name: string, return s: string buffer)
initialize (s)
s << "_SWIFT_MANAGER_" << name
end
//[c]
//[c]Returns the hwnd matching the manager name or nil
//[c]
public func manager window (name: string)
def prop name := property name (name)
def data: local search params
name (data) = as string (prop name)
hwnd (data) = nil
EnumWindows (^search window (HWND, LPARAM), data: LPARAM)
release (prop name)
return hwnd (data)
end
//[c]
struct search params
name: string
hwnd: HWND
end
//[c]
func [call="__stdcall"] search window (hwnd: HWND, lParam: LPARAM)
equ data = lParam: search params
def rc = TRUE
def handle = GetPropA (hwnd, name (data))
if not nil (handle)
rc = FALSE
hwnd (data) = hwnd
end
return rc
end
//[cf]
//[c]
//[of]:can send message (manager)
//[c]Returns true if there is a manager with the given name
//[c]
public func can send message (name: string)
return not nil (manager window (name))
end
//[cf]
//[of]:send message (manager, message)
//[c]
public func send message (name: string, message: string)
def hWnd = manager window (name)
if is nil (hWnd)
return
end
if IsIconic (hWnd) == TRUE
ShowWindow (hWnd, SW_RESTORE)
else
SetForegroundWindow (hWnd)
end
def cds: COPYDATASTRUCT
cbData (cds) = size (message)
lpData (cds) = message
SendMessageA (hWnd, WM_COPYDATA, 0, cds:LPARAM)
end
//[cf]
//[cf]
//[of]:frame box
//[of]:type
public struct frame box : local box
private
parent frame: frame box
hicon : HICON
public
running: bool
active: box
menu: menu
title: string
is dialog: bool
end
//[cf]
//[of]:instance creation
//[of]:new frame box (style)
public func new frame box (style: box style)
equ s = sizeof local frame box
def f = allocate memory (s): frame box
initialize (f, style)
return f
end
//[cf]
//[of]:new frame box (parent, style)
public func new frame box (parent: box, style: box style)
equ s = sizeof local frame box
def f = allocate memory (s): frame box
initialize (f, parent, style)
return f
end
//[cf]
//[cf]
//[of]:operations
//[of]:activate
public func activate (m: frame box)
validate size (m)
def cmd = SW_SHOW
ShowWindow (hwnd (m), cmd)
UpdateWindow (hwnd (m))
end
//[cf]
//[of]:process message
//[c]Processes one message
//[c]Returns false to exit a message loop
//[c]
public func process message (m: frame box, wait: bool)
def msg: local LPMSG
if ~ wait && PeekMessageA (msg, nil, 0, 0, PM_NOREMOVE) == 0
return true
end
// Enter main message loop
if GetMessageA (msg, nil, 0, 0) == 0
return false
end
if ~ translate accelerator (m, msg)
TranslateMessage (msg)
DispatchMessageA (msg)
end
validate size (m)
return true
end
//[cf]
//[of]:message loop
public func message loop (m: frame box)
def msg: local LPMSG
// Enter main message loop
while GetMessageA (msg, nil, 0, 0) <> 0
if ~ translate accelerator (m, msg)
TranslateMessage (msg)
DispatchMessageA (msg)
end
validate size (m)
end
end
//[cf]
//[of]:modal loop
public func modal loop (m: frame box)
def p = parent frame (m)
disable (p)
message loop (m)
end
//[cf]
//[of]:do modal loop
public equ do modal loop (m: frame box)
def p = parent frame (m)
disable (p)
while process message (m, false)
yield
end
end
//[cf]
//[of]:close
public func close (m: frame box)
send message (m, WM_CLOSE)
end
//[cf]
//[cf]
//[of]:menu
//[of]:set menu (menu)
//[c]Sets the menu
//[c]
public func set menu (m: frame box, menu: menu)
menu (m) = menu
def hmenu = NULL: HMENU
if not nil (menu)
hmenu = handle (peer (menu))
end
SetMenu (hwnd (m), hmenu)
end
//[cf]
//[of]:append menu (menu, cmd)
//[c]Adds a command a sub-menu
//[c]
//[c]This method must be used when adding a menu item in menu after call to
//[c]configure menu (...). Same as 'append menu (menu, cmd)' on windows platform.
//[c]
public func append menu (m: frame box, popup: menu, cmd: command)
append menu (popup, cmd)
end
//[cf]
//[of]:remove menu (menu, index)
//[c]Removes a menu item from a sub-menu
//[c]
//[c]This method removes a menu item by its index and unregister any shortcut
//[c]associated to it. Same as 'remove menu (menu, index)' on windows platform.
//[c]
public func remove menu (m: frame box, popup: menu, index: dword)
remove menu (popup, index)
end
//[cf]
//[cf]
//[of]:icon
//[of]:set icon (icon)
public func set icon (m: frame box, icon: resource id)
def hicon = LoadIconA (hinstance, icon:LPCSTR)
hicon (m) = hicon
send message (m, WM_SETICON, ICON_BIG: WPARAM, hicon:LPARAM)
end
//[cf]
//[cf]
//[of]:title
//[of]:set title (text)
//[c]
public func set title (m: frame box, text: string)
delete (title (m))
title (m) = new string (text)
set window text (m, text)
end
//[cf]
//[cf]
//[of]:geometry
//[of]:set default size (width, height)
//[c]
public func set default size (
m: frame box,
w: int,
h: int)
def rect: RECT
GetWindowRect (hwnd (m), rect)
// Center the frame
def parent = parent frame (m)
if not nil (parent)
def p: RECT
GetWindowRect (hwnd (parent), p)
left (rect) = left (p) + (width (p) - w) / 2
top (rect) = top (p) + (height (p) - h) / 2
end
def r := rectangle (client rect (m))
left (r) = left (rect)
top (r) = top (rect)
width (r) = max (w, min width (m))
height (r) = max (h, min height (m))
move (m, r)
end
//[cf]
//[cf]
//[of]:manager
//[of]:set manager (manager)
//[c]This frame can receive a message from another process when
//[c]invoking send message with the same name.
//[c]
public func set manager (m: frame box, name: string)
// Just set a property with a name composed with a prefix
// and the manager name and a dummy value (1)
def prop name := property name (name)
SetPropA (hwnd (m), as string (prop name), 1:HANDLE)
release (prop name)
end
//[cf]
//[cf]
//[c]
//[of]:restricted
//[of]:initialize
public func initialize (m: frame box, style: box style)
initialize (m, nil, style)
end
//[cf]
//[of]:initialize (parent)
public func initialize (m: frame box, parent: box, style: box style)
// special handling of parent: the frame must not
// be insterted in the tree of the parent
initialize (super (m), nil)
set style (m, style)
class (m) = frame box class
active (m) = nil
menu (m) = nil
hicon (m) = nil
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -