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

📄 left-list-box.zc

📁 实现树形结构
💻 ZC
字号:
//[of]:description
//[c]left-list-box - a container with two children
//[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"
//[c]
import "graphics/geometry"
import "graphics/graphics"
import "user/box"
import "user/container-box"
import "user/blank-box"
//[cf]
//[c]
//[of]:left list box
//[of]:type
public struct left list box : local container box

	show list : bool
		
	// remember if a children has the focus
	public has focus : bool

	// the last page having the focus
	private last activated child : box

end
//[cf]
//[of]:instance creation
//[of]:new left list box (parent box)
public func new left list box (parent: box)

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

end
//[cf]
//[cf]
//[of]:focus
//[of]:set focus (child)
//[c]Sets the focus on a child
//[c]
public func set focus (m: left list box, child: box)

	if has focus (m)
		set focus (child)
	else
		// well, we don't have the focus yet, but we must
		// remember to activate the first pane 
		last activated child (m) = child
	end

end
//[cf]
//[of]:focused child
//[c]Returns the immediate child that contain the focused widget or nil
//[c]
public func focused child (m: left list box)
	
	def child = last activated child (m)
	if not nil (child)
		while not nil (child)
			def parent = parent (child)
			if parent == m
				return child
			end
			child = parent
		end
	end
	return first child (m)

end
//[cf]
//[cf]
//[of]:visibility
//[of]:show list (flag)
//[c]
public func show list (m: left list box, show list: bool)

	if show list (m) == show list
		return
	end
	show list (m) = show list

	// focus must be updated before visibility otherwise the framework (GTK)
	// may throw the focus in outer space
	if ~ show list && focused child (m) == list (m)
		set focus (m, detail (m))
	end

	set visible (list (m), show list)

	// only if sized
	if ~ is empty (client rect (m))
		compute layout (m)
	end

	invalidate min size (m)
	invalidate (m)
end
//[cf]
//[cf]
//[of]:accessing
//[of]:list
//[c]
public func list (m: left list box)
	return first child (m)
end
//[cf]
//[of]:detail
//[c]
public func detail (m: left list box)
	return next sibling (list (m))
end
//[cf]
//[cf]
//[c]
//[of]:restricted
//[of]:initialize (parent box, show list)
public func initialize (m: left list box, parent: box)

	initialize (super (m), parent)
	class (m) = left list box class
	show list (m) = true
	has focus (m) = false
	last activated child (m) = nil
	
end
//[cf]
//[c]
//[of]:actual adjust
//[c]Adjust
//[c]
//[c]	Min sizes may have changed: the layout must be recomputed
//[c]
public func actual adjust (m: left list box)
	compute layout (m)
end
//[cf]
//[of]:actual compute min size
//[c]Compute min size
//[c]
public func actual compute min size(m: left list box)

	def p1 = first child (m)
	def p2 = next sibling (p1)
	def a = min size (m)

	if show list (m)
		w (a) = min width (p1) + min width (p2) + sep size
		h (a) = max (min height (p1), min height (p2))
	else
		set(a, min size (p2))
	end
		
end
//[cf]
//[c]
//[of]:handle size
//[c]The grid has been resized
//[c]
public func handle size (m: left list box)
	compute layout (m)
end
//[cf]
//[of]:handle paint (canvas, clip)
//[c]
public func handle paint (m: left list box, c: canvas, clip: rectangle)

	if show list (m)

		def a = client rect (m)
		def p1 = first child (m)
		def r : local rectangle
		x (r) = max (min width (p1), w (a) / 3)
		w (r) = sep size
		y (r) = 0
		h (r) = h (a)
	
		def b := brush from system color (color button face)
		fill rectangle (c, r, b)
	
	end

end
//[cf]
//[of]:handle focus (event)
public func handle focus (m: left list box, e: event)

	// transfer the focus to the last activated pane
	def child = last activated child (m)
	if is nil (child)
		child = next sibling (first child (m))
	end
	set focus (child)
	return true

end
//[cf]
//[of]:handle child event (child, event)
public func handle child event (m: left list box, b: box, e: event)

	def t = type (e)
	if t == focus event type
		has focus (m) = true
		def child = find child (m, b)
		if not nil (child)
			last activated child (m) = child
		end
	elsif t == blur event type
		has focus (m) = false
	end

	return handle child event (super (m), b, e)

end
//[cf]
//[cf]
//[of]:private
//[of]:constants
private equ sep size = 4
//[cf]
//[c]
//[of]:compute layout (m)
//[c]Compute the layout: move all children
//[c]
private func compute layout (m: left list box)

	def a = client rect (m)
	def p1 = first child (m)
	def p2 = next sibling (p1)

	if ~ show list (m)

		move (p2, a)
	
	else

		def w = w (a)
		def h = h (a)
		def r1 : local rectangle
		def r2 : local rectangle

		def w1 = max (min width (p1), w / 3)
		def w2 = w - w1 - sep size
		
		x (r1) = 0
		y (r1) = 0
		w (r1) = w1
		h (r1) = h

		x (r2) = w1 + sep size
		y (r2) = 0
		w (r2) = w2
		h (r2) = h
	
		move (p1, r1)
		move (p2, r2)

	end

end
//[cf]
//[cf]
//[cf]
//[of]:left list box class
//[of]:type
public struct left list box class : local container box class
end
//[cf]
//[of]:left list box class
public func left list box class

	def c = the left list box class
	if ~ initialized
		initialized = true
		c . copy (container box class)
		c . adjust = ^actual adjust (left list box)
		c . compute min size = ^actual compute min size (left list box)
		c . accept focus = ^yes (box)
		c . on size = ^handle size (left list box)
		c . on paint = ^handle paint (left list box, canvas, rectangle)
		c . on focus = ^handle focus (left list box, event)
		c . on child event = ^handle child event (left list box, box, event)
	end
	return c

end

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

⌨️ 快捷键说明

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