frame.zc

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

ZC
743
字号
	parent frame (m) = parent: frame box

	connect (m, "destroy", ^destroy (GtkWidget, frame box))

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

	connect (m, 
		"selection-get", 
		^selection get (GtkWidget, GtkSelectionData, guint, guint, frame box))

end
//[c]
func realize vbox (w: GtkWidget, o: object)
	gdk_window_set_back_pixmap (gtk_widget_get_window (w), nil, FALSE)
end
//[cf]
//[of]:initialize (style)
//[c]
public func initialize (m: frame box, style: box style)

	initialize (m, nil, style)

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

	def menu = menu (m)
	if not nil (menu)
		gtk_box_pack_start (
			vbox (m),
			menu bar (menu),
			FALSE,
			FALSE,
			0)
	end
	
	// activate all children
	each child (m) ? c
		gtk_box_pack_start (
			vbox (m), 
			widget (c),
			TRUE,
			TRUE,
			0)
		activate (c)
	end

	gtk_widget_show (widget (m))

	update icon (m)

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

	if not nil (menu (m))
		delete (menu (m))
	end

	actual release (super (m))

end
//[cf]
//[c]
//[of]:handle activation (event)
public func handle activation (m: frame box, b: bool)
	// empty
end
//[cf]
//[of]:handle close
//[c]Default handling of the close event: 
//[c]	
//[c]	destroy window by returning FALSE, i.e. "do not block event handling"
//[c]
public func handle close (m: frame box)

	return false

end
//[cf]
//[of]:handle message (string)
public func handle message (m: frame box, s: string)
	// empty
end
//[cf]
//[of]:handle child event (child, event)
public func handle child event (m: frame box, src: box, evt: event)

	def t = type (evt)
	if t == focus event type
	
		active (m) = src

	elsif t == destroy event type
	
		if src == active (m)
			active (m) = nil

			// reassign if possible
			if running (m)
				transfer focus (m, false)
			end

		end
	
	end

	return handle child event (super (m), src, evt)

end
//[cf]
//[cf]
//[of]:private
//[of]:class
private equ class (m: frame box) = 
	
	class (super (m)) : frame box class
//[cf]
//[of]:window
private equ window (m: frame box) = 
	
	widget (m) : GtkWindow
//[cf]
//[c]
//[of]:on close (event)
//[c]Close Event
//[c]
//[c]close is a request from an external agent to close the frame box.
//[c]
//[c]To actually close the window, the handler must call the super
//[c]handler (the frame box default handler perform the close action).
//[c]
private equ on close (m: frame box) =

	on close (class (m)) {m}
//[cf]
//[of]:on activation (event)
private func on activation (m: frame box, b: bool)
	on activation (class (m)) {m, b}
end
//[cf]
//[of]:on message (string)
public func on message (m: frame box, s: string)
	on message (class (m)) {m, s}
end
//[cf]
//[of]:query close
//[c]Query to close the window
//[c]
//[c]	Returns true if it is ok to close the window
//[c]
private func query close (m: frame box)
	return ~ on close (m)
end
//[cf]
//[of]:update icon
private func update icon (m: frame box)

	def w = gtk_widget_get_window (widget (m))

	def icon id = icon id (m)
	def parent = parent frame (m)
	while icon id == 0 && not nil (parent)
		icon id = icon id (parent)
		parent = parent frame (parent)
	end
	
	if not nil (w) && icon id <> 0
		def index = icon id - 101
		def mask : [1] GdkBitmap
		def icon = gdk_pixmap_create_from_xpm_d (w, mask, nil, gtk_resources [index] : [][] gchar)
		gdk_window_set_icon (w, nil, icon, mask[])
	end

end
//[cf]
//[of]:register shortcut (menu item)
//[c]Registers a shortcut
//[c]
private func register shortcut (m: frame box, item: menu item)

	accel group (item) = accel group (m)

	def cmd = cmd (item)
	def accelerator = accelerator (cmd)
	def key = key (accelerator)
	def flags = flags (accelerator) : dword
	if key <> 0
		gtk_widget_add_accelerator (
			menu item (item),
			"activate",
			accel group (m),
			key (accelerator): guint,
			mask (flags),
			GTK_ACCEL_VISIBLE)
	end

end
//[cf]
//[of]:unregister shortcut (menu item)
//[c]Registers a shortcut
//[c]
private func unregister shortcut (m: frame box, item: menu item)

	def cmd = cmd (item)
	def accelerator = accelerator (cmd)
	def key = key (accelerator)
	def flags = flags (accelerator) : dword
	if key <> 0
		gtk_widget_remove_accelerator (
			menu item (item),
			accel group (m),
			key: guint,
			mask (flags))
	end

end
//[cf]
//[c]
//[of]:mask (flags)
//[c]Registers a shortcut
//[c]
private func mask (flags: dword)

	def mask = 0
	if (flags & SHIFT) <> 0
		mask |= GDK_SHIFT_MASK : int
	end
	if (flags & CTRL) <> 0
		mask |= GDK_CONTROL_MASK : int
	end
	if (flags & ALT) <> 0
		mask |= GDK_MOD1_MASK : int
	end
	return mask : GdkModifierType

end
//[cf]
//[of]:focus in event (window, event, frame box)
private func focus in event (w: GtkWindow, e: GdkEventFocus, m: frame box)

	// The window is being activated,
	// another application may have changed the clipboard, a flag
	// is set but the request can not be done here. It will be done
	// when a child widget gets the focus.
	clipboard need update = true

	// custom code can be executed now
	on activation (m, true)

	return FALSE

end
//[cf]
//[of]:focus out event (window, event, frame box)
private func focus out event (w: GtkWindow, e: GdkEventFocus, m: frame box)

	// custom code can be executed now
	on activation (m, false)

	return FALSE

end
//[cf]
//[of]:delete event (widget, event, frame box)
private func delete event (widget: GtkWidget, event: GdkEvent, m: frame box)

	return query close (m) -> FALSE, TRUE

end
//[cf]
//[of]:destroy (widget, box)
private func destroy (widget: GtkWidget, m: frame box)

	if gtk_main_level == 0
		running (m) = false
	else
		gtk_main_quit
	end

	return FALSE

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

	def length = length (data)
	def buffer = data (data)

	if  selection (data) == swift message atom && length >= 0

		// Do not use gtk_window_present() since it tends
		// to only make it blinking in the GNOME window bar.
		//gtk_window_present (widget:GtkWindow)
		def window = gtk_widget_get_window (widget (m):GtkWindow)
		gdk_window_show (window)
		gdk_window_focus (window, gtk_get_current_event_time)

		def message = new string (buffer: string, length)
		on message (m, message)
		delete (message)
	end
	
end
//[cf]
//[of]:selection get (widget, selection data, info, time, frame box)
//[c]
func selection get (
		widget: GtkWidget, 
		data: GtkSelectionData, 
		info: guint,
		time: guint, 
		m: frame box)

	if target (data) == swift command atom
	
		// Received a signal from another process that is trying to send me
		// a message. Query the message
		gtk_selection_convert (
			widget, 
			swift message atom,
			GDK_TARGET_STRING,
			GDK_CURRENT_TIME)

	end

end
//[cf]
//[cf]
//[cf]
//[of]:frame box class
//[of]:type
public struct frame box class : local box class
	on close : {box} bool
	on activation : {box, bool} void
	on message: {box, string} void
end
//[cf]
//[of]:frame box class
public func frame box class

	def c = the frame box class
	if ~ initialized
		initialized = true
		c . copy (box class)
		c . mem size = sizeof local frame box class
		c . activate = ^actual activate (frame box)
		c . release = ^actual release (frame box)
		c . on child event = ^handle child event (frame box, box, event)
		c . on close = ^handle close (frame box)
		c . on message = ^handle message (frame box, string)
		c . on activation = ^handle activation (frame box, bool)
	end
	return c

end

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

⌨️ 快捷键说明

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