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

📄 container-box.zc

📁 实现树形结构
💻 ZC
字号:
//[of]:description
//[c]Base class for custom controls
//[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"
//[c]
import "graphics/geometry"
import "graphics/graphics"
import "user/box"
import "toolbox/clipboard"
//[c]
import "private/sys-graphics"
import "glib/glib"
import "gdk/gdk"
import "gtk/gtk"
//[cf]
//[c]
//[of]:container
//[of]:type
public struct container box : local box
	gc : GdkGC
	can paint : bool
end
//[cf]
//[c]
//[of]:restricted
//[of]:initialize (parent, clip with children)
//[c]Initializes the container
//[c]
//[c]ARGUMENTS
//[c]	parent
//[c]		The parent box
//[c]	clip with children
//[c]		If set, all draw in the paint method will be clipped with children.
//[c]		With this option it is possible to draw something behind children
//[c]		without flicking.
//[c]		This option has no effect on GTK+.
//[c]
public func initialize (m: container box, parent: box, clip with children: bool)
	initialize (m, parent)
end
//[cf]
//[of]:initialize (parent)
public func initialize (m: container box, parent: box)

	def p = gtk_custom_new : GtkCustom
	widget (m) = p
	gtk_widget_show (p)

	initialize (super (m), parent)
	class (m) = container box class
	gc (m) = nil
	can paint (m) = true

	gtk_widget_add_events(p,
		GDK_BUTTON_PRESS_MASK:int | 
		GDK_BUTTON_RELEASE_MASK:int |
		GDK_POINTER_MOTION_MASK:int |
		GDK_KEY_PRESS_MASK:int |
		GDK_KEY_RELEASE_MASK:int	|
		GDK_FOCUS_CHANGE_MASK:int)

	connect (m, 
		"size-request", 
		^size request (GtkWidget, GtkRequisition, container box))

	connect (m, 
		"expose-event", 
		^expose event (GtkWidget, GdkEventExpose, container box))

	connect (m, 
		"button-press-event", 
		^button press event (GtkWidget, GdkEventButton, box))

	connect (m, 
		"button-release-event", 
		^button release event (GtkWidget, GdkEventButton, box))
		
	connect (m, 
		"motion-notify-event", 
		^motion notify event (GtkWidget, GdkEventMotion, container box))

	connect (m, 
		"scroll-event", 
		^scroll event (GtkWidget, GdkEventScroll, container box))

	connect (m, 
		"realize", 
		^realize event (GtkWidget, container box))

	connect (m, 
		"selection-received", 
		^selection received (GtkWidget, GtkSelectionData, guint, container box))

	connect (m, 
		"selection-get", 
		^selection get (GtkWidget, GtkSelectionData, guint, guint, container box))
		
end
//[cf]
//[of]:initialize
public func initialize (m: container box)

	initialize(m, nil : box)

end
//[cf]
//[c]
//[of]:actual activate
public func actual activate (m: container box)

	// Set the GTK_CAN_FOCUS flag according to the 'accept focus'
	// property.
	// This is an invocation to a virtual method, it must be
	// performed when the control is fully initialized
	// (expecially class function table)
	if accept focus (m)
		gtk_widget_set_flags (widget (m), GTK_CAN_FOCUS:guint)
	else
		gtk_widget_unset_flags (widget (m), GTK_CAN_FOCUS:guint)
	end

	actual activate (super (m))

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

	def gc = gc (m)
	if not nil (gc)
		gdk_gc_unref (gc)
	end

	actual release (super (m))

end
//[cf]
//[cf]
//[of]:private
//[of]:class
private equ class (m: container box) = 

	class (super (m)) : container box class
//[cf]
//[c]
//[of]:size request (widget, requisition, container box)
//[c]GTK+ is asking about the minsize
//[c]
private func size request (w: GtkWidget, r: GtkRequisition, m: container box)

	each child (m) ? c
		def req : local GtkRequisition
		gtk_widget_size_request (widget (c), req)
		
		// some control are pure gtk and do not invoke compute min size:
		// need to fill the min size field
		set min size (c, width (req), height (req))
	end

	// update the min size
	compute min size (m)

	// pass result to gtk
	width (r) = min width (m)
	height (r) = min height (m)

end
//[cf]
//[of]:realize event (widget, event, container box)
//[c]Windows paint event
//[c]
private func realize event (w: GtkWidget, m: container box)

	if can paint (m)
		gdk_window_set_back_pixmap (gtk_widget_get_window (w), nil, FALSE)
	end

end
//[cf]
//[of]:expose event (widget, event, container box)
//[c]Windows paint event
//[c]
private func expose event (
		w: GtkWidget,
		e: GdkEventExpose,
		m: container box)

	if can paint (m)

		// Create the graphics context on the first expose event
		def gc = gc (m)
		if is nil (gc)
			gc = gdk_gc_new (window (e))
			gc (m) = gc
		end
	
		// Create the canvas
		def canvas : local canvas
		initialize (canvas, window (e), gc, w)
	
		// Paint exposed region
		on paint (m, canvas, area (e) : rectangle)
	
		// Release the canvas
		release (canvas)
		
	end

	return FALSE

end
//[cf]
//[of]:motion notify event (widget, event, container box)
//[c]The mouse has moved
//[c]
private func motion notify event (
		w: GtkWidget, 
		ev: GdkEventMotion, 
		m: container box)

	def e: local mouse move event
	type (e) = mouse move event type
	status (e) = get key state (state (ev))
	x (position (e)) = gdk_event_motion_get_x (ev)
	y (position (e)) =gdk_event_motion_get_y (ev)
	return bool to gboolean (on mouse move (m, e))

end
//[cf]
//[of]:scroll event (widget, event, container box)
//[c]Scroll event
//[c]
private func scroll event (
		w: GtkWidget, 
		ev: GdkEventScroll, 
		m: container box)

	def e: local mouse wheel event
	type (e) = mouse wheel event type
	status (e) = get key state (state (ev))
	x (position (e)) = max (0, gdk_event_scroll_get_x (ev))
	y (position (e)) = max (0, gdk_event_scroll_get_y (ev))
	delta (e) = (direction (ev) == GDK_SCROLL_UP -> 1, -1)
	on mouse wheel (m, e)

	return TRUE

end
//[cf]
//[of]:selection received (widget, selection data, time, container box)
//[c]
func selection received (widget: GtkWidget, data: GtkSelectionData, time: guint, m: container box)

	if selection (data) <> GDK_SELECTION_PRIMARY
		return
	end

	def type = type (data)	
	def length = length (data)
	def buffer = data (data)
	
	// failed to receive required data: notify sender with a nil buffer
	if length < 0
		type = target (data)
		buffer = nil
		length = 0
	end

	def e: local receive primary selection event
	format (e) = type
	buffer (e) = buffer
	size (e) = length
	on receive primary selection (m, e)

end
//[cf]
//[of]:selection get (widget, selection data, info, time, container box)
//[c]
func selection get (
		widget: GtkWidget, 
		data: GtkSelectionData, 
		info: guint,
		time: guint, 
		m: container box)

	if selection (data) <> GDK_SELECTION_PRIMARY
		return
	end
	
	def e: local query primary selection event
	format (e) = target (data)
	data (e) = data
	on query primary selection (m, e)

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

	def c = the container box class
	if ~ initialized
		initialized = true
		c . copy (box class)
		c . mem size = sizeof local container box class
		c . release = ^actual release (container box)
		c . activate = ^actual activate (container box)
	end
	return c

end

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

⌨️ 快捷键说明

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