check-box.zc

来自「实现树形结构」· ZC 代码 · 共 236 行

ZC
236
字号
//[of]:description
//[c]Edit a boolean value
//[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]:check box
//[of]:type
public struct check box : local box

	public value: bool
	private content: string

end
//[cf]
//[of]:instance creation
//[of]:new check box (parent, caption)
public func new check box (parent: box, caption: string)

	equ s = sizeof local check box
	def m = allocate memory (s): check box
	initialize (m, parent, caption)
	return m

end
//[cf]
//[cf]
//[of]:text
//[of]:text
//[c]Returns the caption
//[c]
public func text (m: check box)
	return content (m)
end
//[cf]
//[of]:set text (text)
//[c]Changes the caption
//[c]
public func set text (m: check box, t: string)

	delete (content (m))
	content (m) = new string (t)
	set window text (m, t)
	invalidate min size (m)
	invalidate (m)

end
//[cf]
//[cf]
//[of]:value
//[of]:click
//[c]Simulates a click
//[c]
public func click (m: check box)
	set checked (m, ~ is checked (m))
end
//[cf]
//[of]:set checked (checked)
//[c]Activates or deactivates the check-box
//[c]
public func set checked (m: check box, checked: bool)

	if value (m) == checked
		return
	end

	value (m) = checked

	def wParam = (checked -> BST_CHECKED, BST_UNCHECKED) : WPARAM
	send message (m, BM_SETCHECK, wParam)
	on value changed (m)
	
end
//[cf]
//[of]:is checked
//[c]Returns the current state of the check box
//[c]
public func is checked (m: check box)
	return value (m)
end
//[cf]
//[cf]
//[c]
//[of]:restricted
//[of]:initialize (parent, caption)
//[c]
public func initialize (m: check box, parent: box, caption: string)

	initialize (super (m), parent)
	class (m) = check box class
	value (m) = false
	content (m) = new string (caption)

	create managed window ex (
		m,
		0,
		"button",
		content (m),
		get style (m),
		m:HMENU,
		nil)

	set label font (m)

end
//[cf]
//[c]
//[of]:actual compute min size
public func actual compute min size (m: check box)

	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) += checkbox size + checkbox space
	h (a) = max (checkbox size, h (a))
	set (min size (m), a)

end
//[cf]
//[of]:actual set read only (bool)
public func actual set read only (m: check box, ro: bool)

	// exit if unchanged
	if read only (m) == ro
		return
	end
	
	read only (m) = ro
	
end
//[cf]
//[of]:actual release
public func actual release (m: check box)
	delete (content (m))
	actual release (super (m))
end
//[cf]
//[cf]
//[of]:private
//[of]:constants
//[c]Constant values guessed from windows.
//[c]It seems there is no way to get them from the Windows API.
//[c]
equ checkbox size = 16
equ checkbox space =  4
//[cf]
//[of]:get style
private func get style (m: check box)

	def style = base style (m) | BS_CHECKBOX | BS_VCENTER | BS_NOTIFY

	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 (notify code)
//[c]Command procedure
//[c]
private func actual win command (m: check box, notify code: WORD)

	if notify code == BN_CLICKED:WORD
		if ~ is read only (m)
			set checked (m, ~ value (m))
		end
	end

end
//[cf]
//[cf]
//[cf]
//[of]:check box class
//[of]:type
public struct check box class : local box class
	// empty
end
//[cf]
//[of]:check box class
public func check box class

	def c = the check box class
	if ~ initialized
		initialized = true
		c . copy (box class)
		c . release = ^actual release (check box)
		c . compute min size = ^actual compute min size (check box)
		c . set read only = ^actual set read only (check box, bool)
		c . accept focus = ^yes (box)
		c . wm size = ^wm size (box, UINT, WPARAM, LPARAM)
		c . command = ^actual win command (check box, WORD)
	end
	return c

end

private def initialized = false
private def the check box class: local check box class
//[cf]
//[cf]

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?