📄 button.zc
字号:
//[of]:description
//[c]A clickable button
//[c]
//[cf]
//[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-allocator"
import "text/string"
//[c]
import "graphics/geometry"
import "graphics/graphics"
import "user/box"
//[c]
import "win32/windows"
//[cf]
//[c]
//[of]:button
//[of]:type
public struct button : local box
private
default: bool
content: string
public
min button width: int
end
//[cf]
//[of]:instance creation
//[of]:new button (parent, caption)
//[c]
public func new button (parent: box, caption: string)
def m = allocate memory (sizeof local button): button
initialize (m, parent, caption)
return m
end
//[cf]
//[cf]
//[of]:action
//[of]:click
//[c]Click - simulate a button click
//[c]
public equ click (m: button) = on click (m)
//[cf]
//[of]:set default (is default)
//[c]Marks the button as the default button
//[c]
public func set default (m: button, default: bool)
default (m) = default
send message (m, BM_SETSTYLE, get style (m):WPARAM, 1:LPARAM)
end
//[cf]
//[cf]
//[of]:text
//[of]:text
//[c]
public func text (m: button)
return content (m)
end
//[cf]
//[of]:set text (text)
//[c]Changes the caption of the button
//[c]
public func set text (m: button, t: string)
delete (content (m))
content (m) = new string (t)
set window text (m, t)
invalidate min size (m)
invalidate (m)
end
//[cf]
//[of]:min button width
//[c]See struct definition
//[cf]
//[of]:set min button width (w)
//[c]
public func set min button width (m: button, w: int)
min button width (m) = w
end
//[cf]
//[cf]
//[of]:converting
//[of]:as button
//[c]Returns a pointer to a button from a box if the box is a button
//[c]
public equ as button (m: box) = get interface (m, button interface) : button
//[cf]
//[cf]
//[c]
//[of]:restricted
//[of]:initialize (parent, caption)
public func initialize (m: button, parent: box, caption: string)
initialize (super (m), parent)
class (m) = button class
content (m) = new string (caption)
default (m) = false
min button width (m) = 80
create managed window ex (
m,
0,
button class name,
content (m),
get style (m),
m:HMENU,
nil)
set label font (m)
end
//[cf]
//[c]
//[of]:actual get interface (interface id)
public func actual get interface (m: button, id: interface id)
if id == button interface
return m
end
return actual get interface (super (m), id)
end
//[cf]
//[of]:actual compute min size
func actual compute min size (m: button)
def a: local area
def canvas: local canvas
initialize (canvas)
set font (canvas, label font (style (m)))
text extent (canvas, content (m), size (content (m)), a)
release (canvas)
w (a) += 24
h (a) += 10
w (a) = max (min button width (m), w (a))
set (min size (m), a)
end
//[cf]
//[of]:actual release
public func actual release (m: button)
delete (content (m))
actual release (super (m))
end
//[cf]
//[c]
//[cf]
//[of]:private
//[of]:constants
equ button interface = the button class
def button class name := "button"
//[cf]
//[of]:get style (m)
func get style (m: button)
def style = base style (m) | BS_VCENTER | BS_NOTIFY
if default (m)
style |= BS_DEFPUSHBUTTON
else
style |= BS_PUSHBUTTON
end
def edit border = edit border (style (m))
if edit border == border single || edit border == border none
style |= BS_FLAT
end
return style
end
//[cf]
//[of]:actual win command (m, notify code)
func actual win command (m: button, notify code: WORD)
if notify code == BN_CLICKED:WORD
on click (m)
end
end
//[cf]
//[cf]
//[cf]
//[of]:button class
//[of]:type
public struct button class : local box class
// empty
end
//[cf]
//[of]:button class
public func button class
def c = the button class
if ~ initialized
initialized = true
c . copy (box class)
c . release = ^actual release (button)
c . get interface = ^actual get interface (button, interface id)
c . compute min size = ^actual compute min size (button)
c . accept focus = ^yes (box)
c . wm size = ^wm size (box, UINT, WPARAM, LPARAM)
c . command = ^actual win command (button, WORD)
end
return c
end
private def initialized = false
private def the button class: local button class
//[cf]
//[cf]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -