⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 label-box.zc

📁 实现树形结构
💻 ZC
字号:
//[of]:description
//[c]Display a simple label
//[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"
import "user/container-box"
//[cf]
//[c]
//[of]:label box
//[of]:type
public struct label box : local container box
	private text : string
	private font : font
	private alignment : alignment
	private can be truncated : bool
end
//[cf]
//[of]:instance creation
//[of]:new label box (parent, caption)
public func new label box (parent: box, caption: string)

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

end
//[cf]
//[cf]
//[of]:accessing
//[of]:set text (caption)
public func set text (m: label box, caption: string)

	if text (m) : [] byte == caption : [] byte
		return
	end

	delete (text (m))
	text (m) = new string (caption)
	
	// the min size must be updated
	invalidate min size (m)
	
	// the displayed content must also be updated
	invalidate (m)

end
//[cf]
//[of]:set font (font)
//[c]
public func set font (m: label box, font: font)

	font (m) = font
	
	// the min size must be updated
	invalidate min size (m)
	
	// the displayed content must also be updated
	invalidate (m)

end
//[cf]
//[of]:set alignment (alignment)
//[c]Changes the alignment
//[c]
public func set alignment (m: label box, alignment: alignment)

	alignment (m) = alignment
	
	// the displayed content must also be updated
	invalidate (m)

end
//[cf]
//[of]:set can be truncated (bool)
//[c]Change the trucation flag
//[c]
//[c]	If this flag is set, the label can be truncated, i.e. the min size
//[c]	does not depend on the content.
//[c]
public func set can be truncated (m: label box, t: bool)

	can be truncated (m) = t
	
	// the min size must be updated
	invalidate min size (m)
	
	// the displayed content must also be updated
	invalidate (m)

end
//[cf]
//[of]:set mnemonic box (box)
//[c]Sets the box to activate when the mnemonic is activated
//[c]
//[c]Mnemonic are not handled by windows, only by GTK, so this implementation
//[c]does nothing and the application must handle mnemonics manually.
//[c]
public func set mnemonic box (m: label box, box: box)
	// empty
end
//[cf]
//[cf]
//[c]
//[of]:restricted
//[of]:initialize (parent, caption)
public func initialize (m: label box, parent: box, caption: string)

	initialize (super (m), parent)
	class (m) = label box class
	text (m) = new string (caption)
	font (m) = label font (style (m))
	alignment (m) = label alignment (style (m))
	can be truncated (m) = false

end
//[cf]
//[c]
//[of]:handle size
//[c]Must repaint all when size changed
//[c]
public func handle size (m: label box)
	invalidate (m)
end
//[cf]
//[of]:handle paint (canvas, clip)
//[c]
public func handle paint (m: label box, c: canvas, clip: rectangle)

	def style = style (m)

	fill rectangle (c, client rect (m), label background (style))
	set font (c, font (m))
	
	if is enabled (m)
		draw text (c, text (m), client rect (m), alignment (m))
	else
		def r := rectangle (client rect (m))
		++ x (r)
		++ y (r)
		set text color (c, button highlight color (style))
		draw text (c, text (m), r, alignment (m))
		set text color (c, button shadow color (style))
		draw text(c, text (m), client rect (m), alignment (m))
	end
		
end
//[cf]
//[c]
//[of]:actual compute min size
public func actual compute min size (m: label box)

	def canvas: local canvas
	initialize (canvas)
	set font (canvas, font (m))
	def min size := text extent (canvas, text (m), size (text (m)))
	if can be truncated (m)
		w (min size) = 1
	end
	set (min size (m), min size)
	release (canvas)

end
//[cf]
//[of]:actual release
public func actual release (m: label box)

	delete (text (m))
	actual release (super (m))

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

	def c = the label box class
	if ~ initialized
		initialized = true
		c . copy (container box class)
		c . release = ^actual release (label box)	
		c . on size = ^handle size (label box)
		c . compute min size = ^actual compute min size(label box)
		c . on paint = ^handle paint(label box, canvas, rectangle)
	end
	return c

end

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

⌨️ 快捷键说明

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