tree-box.zc

来自「实现树形结构」· ZC 代码 · 共 1,017 行 · 第 1/2 页

ZC
1,017
字号
	user_data (iter) = item
	
	def gtk tree model = gtk_tree_view_get_model (tree view (m))
	def path = get path (gtk tree model, item)
	gtk_tree_model_row_has_child_toggled (gtk tree model, path, iter)
	gtk_tree_path_free (path)

end
//[cf]
//[of]:data changed
func data changed (m: tree box, item: tree item)

	def e: local tree item changed event
	type (e) = tree item changed event type
	tree item (e) = item
	notify parent (m, e)

end
//[cf]
//[of]:children replaced
func children replaced (m: tree box, e: tree replace event)
	
	// find first item to remove
	def gtk tree model = gtk_tree_view_get_model (tree view (m))
	def path = get path (gtk tree model, parent (e))
	gtk_tree_path_append_index(path, index (e):gint)
	
	// removed items	
	def n = removed (e)
	while n > 0
		gtk_tree_model_row_deleted (gtk tree model, path)
		n -= 1
	end

	// inserted items
	n = inserted (e)
	def indices = gtk_tree_path_get_indices (path)
	def last index = gtk_tree_path_get_depth (path) - 1
	def iter: local GtkTreeIter
	custom tree get iter (gtk tree model, iter, path)	
	while n > 0
		gtk_tree_model_row_inserted (gtk tree model, path, iter)
		gtk_tree_model_row_has_child_toggled (gtk tree model, path, iter)
		indices [last index] += 1
		user_data (iter) = next sibling (user_data (iter) : tree item)
		n -= 1
	end

	gtk_tree_path_free (path)
	
	// update item if item added or removed
	if inserted (e) <> 0 || removed (e) <> 0
		has children changed (m, parent (e))
	end
	
end
//[cf]
//[c]
//[c]Signals from tree view and tree selection
//[of]:changed (tree selection, tree box)
//[c]The selection has changed
//[c]
private func changed (w: GtkTreeSelection, m: tree box)

	if ~ synchronizing selection (m)
		def iter : local GtkTreeIter
		def sel = gtk selection (m)
		if gtk_tree_selection_get_selected (sel, nil, iter) == TRUE
			selected item (m) = user_data (iter) : tree item
		else
			selected item (m) = nil
		end
		
		on selection changed (m)
	end

end
//[cf]
//[of]:popup menu (widget, tree box)
//[c]
private func popup menu (widget: GtkWidget, m: tree box)

	def pt: local point
	x (pt) = 0
	y (pt) = 0

	return popup menu (m, pt)

end
//[cf]
//[of]:test row expand (widget, iter, path, tree box)
//[c]Test if the row can be expanded
//[c]
func test row expand (w: GtkTreeView, iter: GtkTreeIter, path: GtkTreePath, m: tree box)

	def item = user_data (iter) : tree item
	if is nil (item)
		return TRUE
	end

	open (item)
	if ~ has children (item)
		def gtk tree model = gtk_tree_view_get_model (w)
		gtk_tree_model_row_has_child_toggled (gtk tree model, path, iter)
		return TRUE
	end
	
	return FALSE
end
//[cf]
//[c]
//[c]Helpers
//[of]:gtk selection
//[c]
private equ gtk selection (m: tree box) = 
	gtk_tree_view_get_selection (tree view (m))
//[cf]
//[of]:class
//[c]
private equ class (m: tree box) = 
	class (super (m)) : tree box class
//[cf]
//[cf]
//[cf]
//[of]:tree box class
//[of]:type
public struct tree box class : local box class
	// empty
end
//[c]
//[cf]
//[of]:tree box class
public func tree box class

	def c = the tree box class
	if ~ initialized
		initialized = true
		c . copy (box class)
		c . release = ^actual release (tree box)
		c . accept focus = ^yes (box)
		c . set focus = ^actual set focus (tree box)
		c . on mouse down = ^handle mouse down (tree box, mouse button event)
		c . on popup = ^handle popup (tree box, popup event)
	end
	return c

end

private def initialized = false
private def the tree box class: local tree box class
//[cf]
//[cf]
//[of]:custom tree model
//[of]:definitions

struct custom tree model : local GObject
	tree model: tree model
	image list: image list
end

struct custom tree model class : local GObjectClass
	// empty
end
//[cf]
//[c]
//[of]:new custom tree model
func new custom tree model (tree: tree model, image list: image list)

	def new custom tree = g_object_new_0 (custom tree model get type) : custom tree model
	tree model (new custom tree) = tree
	image list (new custom tree) = image list
	return new custom tree

end
//[cf]
//[c]
//[of]:custom tree model get type
private def custom tree model type = nil : GType
private def custom tree info: local GTypeInfo
private def custom tree model info: local GInterfaceInfo
private def parent class: GObjectClass

func custom tree model get type : GType
	
	if custom tree model type <> 0:GType
		return custom tree model type
	end

	def info = custom tree info
	class_size (info) = sizeof local custom tree model class : word
	base_init (info) = nil
	base_finalize (info) = nil
	class_init (info) = ^custom tree class init (gpointer, gpointer)
	class_finalize (info) = nil
	class_data (info) = nil
	instance_size (info) = sizeof local custom tree model : word
	n_preallocs (info) = 0:word
	instance_init (info) = ^custom tree init (custom tree model, gpointer)
	value_table (info) = nil
	
	custom tree model type = g_type_register_static (G_TYPE_OBJECT, "CustomTree", info, 0:GTypeFlags)
	
	interface_init (custom tree model info) = ^custom tree model init (GtkTreeModelIface, gpointer)
	interface_finalize (custom tree model info) = nil
	interface_data(custom tree model info) = nil
	
	g_type_add_interface_static (custom tree model type, gtk_tree_model_get_type, custom tree model info)
	
	return custom tree model type

end
//[cf]
//[of]:custom tree model init
func custom tree model init (iface: GtkTreeModelIface, data: gpointer)

	get_flags (iface) = ^custom tree get flags (GtkTreeModel)
	get_n_columns (iface) = ^custom tree get n columns (GtkTreeModel)
	get_column_type (iface) = ^custom tree get column type (GtkTreeModel, gint)
	get_iter (iface) = ^custom tree get iter (GtkTreeModel, GtkTreeIter, GtkTreePath)
	get_path (iface) = ^custom tree get path (GtkTreeModel, GtkTreeIter)
	get_value (iface) = ^custom tree get value (GtkTreeModel, GtkTreeIter, gint, GValue)
	iter_next (iface) = ^custom tree iter next (GtkTreeModel, GtkTreeIter)
	iter_children (iface) = ^custom tree iter children (GtkTreeModel, GtkTreeIter, GtkTreeIter)
	iter_has_child (iface) = ^custom tree iter has child (GtkTreeModel, GtkTreeIter)
	iter_n_children (iface) = ^custom tree iter n children (GtkTreeModel, GtkTreeIter)
	iter_nth_child (iface) = ^custom tree iter nth child (GtkTreeModel, GtkTreeIter, GtkTreeIter, gint)
	iter_parent (iface) = ^custom tree iter parent (GtkTreeModel, GtkTreeIter, GtkTreeIter)

end
//[cf]
//[of]:custom tree class init
func custom tree class init (class: gpointer, data: gpointer)
	
	parent class = g_type_class_peek_parent (class) : GObjectClass
	equ object class = class : GObjectClass
	
	finalize (object class) = ^custom tree finalize (GObject)
end
//[cf]
//[of]:custom tree init
func custom tree init (object: custom tree model, class: gpointer)
end
//[cf]
//[of]:custom tree finalize
func custom tree finalize (object: GObject)
	finalize (parent class) {object}
end
//[cf]
//[c]
//[of]:custom tree get flags
func custom tree get flags (tree model: GtkTreeModel)
	return GTK_TREE_MODEL_ITERS_PERSIST
end
//[cf]
//[of]:custom tree get n columns
func custom tree get n columns (tree model: GtkTreeModel)
	return 2
end
//[cf]
//[of]:custom tree get column type
func custom tree get column type (tree model: GtkTreeModel, index: gint)
	return (index == 0) -> 
		GDK_TYPE_PIXBUF, 
		G_TYPE_STRING
end
//[cf]
//[of]:custom tree get iter
func custom tree get iter (tree model: GtkTreeModel, iter: GtkTreeIter, path: GtkTreePath)

	def item = get item (tree model, path)
	set iter from item (tree model, iter, item)
	return TRUE

end
//[cf]
//[of]:custom tree get path
func custom tree get path (tree model: GtkTreeModel, iter: GtkTreeIter)

	def item = get item from iter (tree model, iter)
	return get path (tree model, item)
	
end
//[cf]
//[of]:custom tree get value
func custom tree get value (tree model: GtkTreeModel, iter: GtkTreeIter, column: gint, value: GValue)

	def tree = custom tree (tree model)
	def item = get item from iter (tree model, iter)

	if column == 0
		g_value_init (value, GDK_TYPE_PIXBUF)
		g_value_set_object (value, images (image list (tree)) [image (item)])
	else	
		def t := temp string buffer
		append text (item, t)
		g_value_init (value, G_TYPE_STRING)
		g_value_set_string (value, to UTF8 (as string (t)))
		release (t)
	end

end
//[cf]
//[of]:custom tree iter next
func custom tree iter next (tree model: GtkTreeModel, iter: GtkTreeIter)

	def tree = custom tree (tree model)
	def item = get item from iter (tree model, iter)

	if is nil (item)
		return FALSE
	end

	def next sibling = next sibling (item)
	if is nil (next sibling)
		return FALSE
	end
	
	set iter from item (tree model, iter, next sibling)
	return TRUE

end
//[cf]
//[of]:custom tree iter children
//[c]Returns the first child of the parent node
//[c]
func custom tree iter children (tree model: GtkTreeModel, iter: GtkTreeIter, parent: GtkTreeIter)

	def tree = custom tree (tree model)

	if is nil (parent)
		set iter from item (tree model, iter, root (tree model (tree)))
		return TRUE
	else
		def parent item = user_data (parent) : tree item
		open (parent item)
		def child = first child (parent item)
		if is nil (child)
			return FALSE
		end
		set iter from item (tree model, iter, child)
		return TRUE
	end

end
//[cf]
//[of]:custom tree iter has child
func custom tree iter has child (tree model: GtkTreeModel, iter: GtkTreeIter)

	def tree = custom tree (tree model)
	def item = get item from iter (tree model, iter)

	if is nil (item)
		return FALSE
	end

	return has children (item) -> TRUE, FALSE

end
//[cf]
//[of]:custom tree iter n children
func custom tree iter n children (tree model: GtkTreeModel, iter: GtkTreeIter)

	def tree = custom tree (tree model)
	
	if is nil (iter)
		// return the number of toplevel items
		return 1
	else
		def parent item = get item from iter (tree model, iter)
		open (parent item)
		def n = 0
		def child = first child (parent item)
		while not nil (child)
			child = next sibling (child)
			n += 1
		end
		return n
	end
	
end
//[cf]
//[of]:custom tree iter nth child
func custom tree iter nth child (
		tree model: GtkTreeModel,
		iter: GtkTreeIter,
		parent: GtkTreeIter,
		n: gint)

	def tree = custom tree (tree model)

	if is nil (parent)
		// return the root
		set iter from item (tree model, iter, root (tree model (tree)))
		return TRUE
	else
		def parent item = user_data (parent) : tree item
		open (parent item)
		def i = n
		def child = first child (parent item)
		while i>0 && not nil (child)
			child = next sibling (child)
			i -= 1
		end
		if is nil (child)
			return FALSE
		end
		
		set iter from item (tree model, iter, child)
		return TRUE
	end	
	
end
//[cf]
//[of]:custom tree iter parent
func custom tree iter parent (
		tree model: GtkTreeModel, 
		iter: GtkTreeIter,
              child: GtkTreeIter)

	def tree = custom tree (tree model)
	def item = user_data (child) : tree item

	if is nil (item)
		return FALSE
	end
	
	def parent = parent (item)
	if is nil (parent)
		return FALSE
	end

	set iter from item (tree model, iter, parent)
	return TRUE
end
//[cf]
//[c]
//[of]:get path
func get path (tree model: GtkTreeModel, item: tree item)

	def tree = custom tree (tree model)
	def path = gtk_tree_path_new
	while not nil (parent (item))
		def parent = parent (item)
		def child = first child (parent)
		def index = 0
		while child <> item
			child = next sibling (child)
			index += 1
		end
		gtk_tree_path_prepend_index(path, index)
		item = parent
	end

	gtk_tree_path_prepend_index(path, 0)
	
	return path

end
//[cf]
//[of]:get item
//[c]
func get item (tree model: GtkTreeModel, path: GtkTreePath)

	def tree = custom tree (tree model)
	
	def indices = gtk_tree_path_get_indices (path)
	def depth   = gtk_tree_path_get_depth (path)
	
	def item = root (tree model (tree))
	def i = 1
	while i < depth
		def n = indices[i]
		open (item)
		item = first child (item)
		while n > 0
			item = next sibling (item)
			n -= 1
		end
		i += 1
	end

	return item

end
//[cf]
//[of]:set iter from item
//[c]
func set iter from item (tree model: GtkTreeModel, iter: GtkTreeIter, item: tree item)
	stamp (iter) = 0
	user_data (iter) = item
end
//[cf]
//[of]:get item from iter
//[c]
equ get item from iter (tree model: GtkTreeModel, iter: GtkTreeIter) =
	user_data (iter) : tree item
//[cf]
//[c]
//[of]:custom tree
equ custom tree (instance: GTypeInstance) = 
	g_type_check_instance_cast (instance, custom tree model get type): custom tree model
//[cf]
//[of]:gtk tree model
equ gtk tree model (instance: GTypeInstance) = 
	g_type_check_instance_cast (instance, gtk_tree_model_get_type): GtkTreeModel
//[cf]
//[cf]

⌨️ 快捷键说明

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