box.zc

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

ZC
2,624
字号
public func do nothing (m: box, o: clipboard format)
	// Do nothing
end
//[cf]
//[of]:yes
//[c]Always answers 'yes'
//[c]
public func yes (m: box)
	return true
end
//[cf]
//[of]:no
//[c]Always answers 'no'
//[c]
public func no (m: box)
	return false
end
//[cf]
//[c]
//[of]:min size of union
public func min size of union (m: box)

	def w = 0
	def h = 0
	each child (m) ? c
		w = max (w, min width (c))
		h = max (h, min height (c))
	end
	set min size (m, w, h)

end
//[cf]
//[of]:adjust to parent
//[c]Moves all the children to the size of the parent
//[c]
public func adjust to parent (m: box)

	each child (m) ? c
		move (c, client rect (m))
	end

end
//[cf]
//[of]:focus to first visible
public func focus to first visible (m: box, e: event)

	// transfer the focus to the first visible child
	each child (m) ? child
		if is visible (child) && ~ being destroyed (child)
			set focus (child)
			return true
		end
	end

	return handle focus (m, e)

end
//[cf]
//[of]:handle single child size
//[c]The size of the box has changed
//[c]
public func handle single child size (m: box)

	if not nil (first child (m))
		move (first child (m), client rect (m))
	end

end
//[cf]
//[of]:actual compute signle child min size
public func actual compute signle child min size (m: box)

	def w = 100
	def h = 100
	
	def first child = first child (m)
	if not nil (first child)
		w = min width (first child)
		h = min height (first child)
	end

	set min size(m, w, h)

end
//[cf]
//[cf]
//[of]:restricted
//[of]:initialize (parent)
//[c]Initializes and inserts a newly created box
//[c]
public func initialize (m: box, parent: box)

	parent (m) = nil
	first child (m) = nil
	last child (m) = nil
	next sibling (m) = nil
	prev sibling (m) = nil

	style (m) = nil
	being destroyed (m) = false
	valid min size (m) = false
	valid size (m) = false
	
	empty (client rect (m))
	empty (min size (m))

	if not nil (parent)
		insert child (parent, m)
		
		// the style is inherited from the parent
		style (m) = style (parent)
	end

	hwnd (m) = nil
	cursor (m) = nil
	visible (m) = true
	enabled (m) = true
	read only (m) = false
	accept drop files (m) = false
	last key event processed (m) = false

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

	// 1. Mark this box as being destroyed.
	// It prevents to reassign the focus for each child being
	// destroyed: can get focus (box) check this flags for all its ancestors.
	being destroyed (m) = true

	// 2. Deletes all children
	// do not use iterator here since the object
	// is deleted before getting its next sibling
	def c = first child (m)
	while not nil (c)
		def n = next sibling (c)
		delete (c)
		c = n
	end

	// 3. Destroy the window
	DestroyWindow (hwnd (m))

	def parent = parent (m)
	if is nil (parent)
		return
	end

	// 4. Detach from parent
	remove child (parent, m)
	
	// 5. Notify parent
	// the parent has been resetted, need to call on child event
	// explicitely
	def e := event (destroy event type)
	on child event (parent, m, e)

end
//[cf]
//[of]:actual get interface (id)
//[c]
public func actual get interface (m: box, id: interface id)
	return nil
end
//[cf]
//[of]:actual cut
//[c]Default implementation for the cut command
//[c]
//[c]	Just performs a copy, then a clear if the copy method
//[c]	returned true.
//[c]
public func actual cut (m: box)
	return copy(m) && clear(m)
end
//[cf]
//[of]:actual can cut
//[c]
public func actual can cut (m: box)
	return can copy (m) && can clear (m)
end
//[cf]
//[cf]
//[of]:default event handling
//[of]:handle size
//[c]
public equ handle size (m: box) = do nothing (m)
public equ default size function = ^do nothing (box)
//[cf]
//[of]:handle paint (canvas, clip rectangle)
//[c]
public func handle paint (m: box, c: canvas, clip: rectangle)
	// sub-class responsibility
end
//[cf]
//[of]:handle drop files (event)
//[c]The control recieves files
//[c]
public func handle drop files (m: box, e: drop files event)
	default window proc (m, msg (e), wParam (e), lParam (e))
end
//[cf]
//[of]:handle query primary selection (event)
public func handle query primary selection (m: box, e: query primary selection event)
	// empty
end
//[cf]
//[of]:handle receive primary selection (event)
public func handle receive primary selection (m: box, e: receive primary selection event)
	// empty
end
//[cf]
//[c]
//[of]:handle focus (event)
//[c]The control has gained the focus
//[c]
public equ handle focus (m: box, e: event) = notify parent (m, e)
private equ default focus function = ^notify parent (box, event)
//[cf]
//[of]:handle blur (event)
//[c]The control has lost the focus
//[c]
public equ handle blur (m: box, e: event) = notify parent (m, e)
private equ default blur function = ^notify parent (box, event)
//[cf]
//[of]:handle key down (event)
public equ handle key down (m: box, e: event) = notify parent (m, e)
private equ default key down function = ^notify parent (box, event)
//[cf]
//[of]:handle key up (event)
public equ handle key up (m: box, e: event) = notify parent (m, e)
private equ default key up function = ^notify parent (box, event)
//[cf]
//[of]:handle char (event)
public equ handle char (m: box, e: event) = notify parent (m, e)
private equ default char function = ^notify parent (box, event)
//[cf]
//[of]:handle mouse down (event)
//[c]The control receives a mouse button event 
//[c]
public equ handle mouse down (m: box, e: event) = notify parent (m, e)
private equ default mouse down function = ^notify parent (box, event)
//[cf]
//[of]:handle mouse up (event)
//[c]The control receives a mouse button event 
//[c]
public equ handle mouse up (m: box, e: event) = notify parent (m, e)
private equ default mouse up function = ^notify parent (box, event)
//[cf]
//[of]:handle mouse move (event)
//[c]The control receives a mouse move event 
//[c]
public equ handle mouse move (m: box, e: event) = notify parent (m, e)
private equ default mouse move function = ^notify parent (box, event)
//[cf]
//[of]:handle mouse wheel (event)
//[c]The control receives a mouse wheel event 
//[c]
public equ handle mouse wheel (m: box, e: event) = notify parent (m, e)
private equ default mouse wheel function = ^notify parent (box, event)
//[cf]
//[of]:handle mouse cancel
//[c]The control receives a mouse cancel event 
//[c]
public equ handle mouse cancel (m: box, e: event) = notify parent (m, e)
private equ default mouse cancel function = ^notify parent (box, event)
//[cf]
//[of]:handle mouse double click
//[c]The control receives a mouse button event 
//[c]
public equ handle mouse double click (m: box, e: event) = notify parent (m, e)
private equ default mouse double click function = ^notify parent (box, event)
//[cf]
//[of]:handle popup (event)
//[c]The control receives a mouse wheel event 
//[c]
public equ handle popup (m: box, e: event) = notify parent (m, e)
private equ default popup function = ^notify parent (box, event)
//[cf]
//[of]:handle enter menu (event)
//[c]Notifies the focused widget that the window menu has been activated
//[c]
public equ handle enter menu (m: box, e: event) = notify parent (m, e)
private equ default enter menu function = ^notify parent (box, event)
//[cf]
//[of]:handle leave menu (event)
//[c]Notifies the focused widget that the window menu has been deactivated
//[c]
public equ handle leave menu (m: box, e: event) = notify parent (m, e)
private equ default leave menu function = ^notify parent (box, event)
//[cf]
//[c]
//[of]:handle value changed (event)
//[c]Notifies the parent that the value of the widget has changed
//[c]
public equ handle value changed (m: box, e: event) = notify parent (m, e)
private equ default value changed function = ^notify parent (box, event)
//[cf]
//[of]:handle index changed (event)
//[c]Notifies the parent that the index of the widget has changed
//[c]
public equ handle index changed (m: box, e: event) = notify parent (m, e)
private equ default index changed function = ^notify parent (box, event)
//[cf]
//[of]:handle selection changed (event)
//[c]Notifies the parent that the selection of the widget has changed
//[c]
public equ handle selection changed (m: box, e: event) = notify parent (m, e)
private equ default selection changed function = ^notify parent (box, event)
//[cf]
//[of]:handle click (event)
//[c]Notifies the parent that the widget has been clicked
//[c]
public equ handle click (m: box, e: event) = notify parent (m, e)
private equ default click function = ^notify parent (box, event)
//[cf]
//[c]
//[of]:handle child event (child, event)
//[c]A custom event has been emitted by a child
//[c]
//[c]The default implementation forwards the event to the parent.
//[c]
public func handle child event (m: box, child: box, e: event)

	def parent = parent (m)
	if is nil (parent)
		return false
	end
	
	return on child event (parent, child, e)

end
//[cf]
//[cf]
//[of]:send events
//[of]:compute min size
public func compute min size (m: box)
	compute min size (class (m)) {m}
end
//[cf]
//[c]
//[of]:on size
private func on size (m: box)
	on size (class (m)) {m}
end
//[cf]
//[of]:on paint (canvas, clip rectangle)
public func on paint (m: box, c: canvas, clip: rectangle)
	on paint (class (m)) {m, c, clip}
end
//[cf]
//[of]:on drop files (drop files event)
private func on drop files (m: box, e: drop files event)
	on drop files (class (m)) {m, e}
end
//[cf]
//[of]:on receive primary selection (event)
public func on receive primary selection (m: box, e: receive primary selection event)
	on receive primary selection (class (m)) {m, e}
end
//[cf]
//[of]:on query primary selection (event)
public func on query primary selection (m: box, e: query primary selection event)
	on query primary selection (class (m)) {m, e}
end
//[cf]
//[c]
//[of]:on focus
public func on focus (m: box)
	def e := event (focus event type)
	return on focus (class (m)) {m, e}
end
//[cf]
//[of]:on blur
public func on blur (m: box)
	def e := event (blur event type)
	return on blur (class (m)) {m, e}
end
//[cf]
//[of]:on key down (key event)
public func on key down (m: box, e: key event)
	return on key down (class (m)) {m, e}
end
//[cf]
//[of]:on key up (key event)
public func on key up (m: box, e: key event)
	return on key up (class (m)) {m, e}
end
//[cf]
//[of]:on char (char event)
public func on char (m: box, e: char event)
	return on char (class (m)) {m, e}
end
//[cf]
//[of]:on mouse down (mouse button event)
private func on mouse down (m: box, e: mouse button event)
	return on mouse down (class (m)) {m, e}
end
//[cf]
//[of]:on mouse up (mouse button event)
private func on mouse up (m: box, e: mouse button event)
	return on mouse up (class (m)) {m, e}
end
//[cf]
//[of]:on mouse move (mouse move event)
private func on mouse move (m: box, e: mouse move event)
	return on mouse move (class (m)) {m, e}
end
//[cf]
//[of]:on mouse wheel (mouse wheel event)
private func on mouse wheel (m: box, e: mouse wheel event)
	return on mouse wheel (class (m)) {m, e}
end
//[cf]
//[of]:on mouse cancel (event)
private func on mouse cancel (m: box)
	def e := event (mouse cancel event type)
	return on mouse cancel (class (m)) {m, e}
end
//[cf]
//[of]:on mouse double click (mouse button event)
public func on mouse double click (m: box, e: mouse button event)
	return on mouse double click (class (m)) {m, e}
end
//[cf]
//[of]:on popup (popup event)
public func on popup (m: box, e: popup event)
	return on popup (class (m)) {m, e}
end
//[cf]
//[of]:on enter menu
private func on enter menu (m: box)
	def e := event (enter menu event type)
	return on enter menu (class (m)) {m, e}
end
//[cf]
//[of]:on leave menu
private func on leave menu (m: box)
	def e := event (leave menu event type)
	return on leave menu (class (m)) {m, e}
end
//[cf]
//[c]
//[of]:on value changed
public func on value changed (m: box)
	def e := event (value changed event type)
	return on value changed (class (m)) {m, e}
end
//[cf]
//[of]:on index changed
public func on index changed (m: box)
	def e := event (index changed event type)
	return on index changed (class (m)) {m, e}
end
//[cf]
//[of]:on selection changed
public func on selection changed (m: box)
	def e := event (selection changed event type)
	return on selection changed (class (m)) {m, e}
end
//[cf]
//[of]:on click
public func on click (m: box)
	def e := event (click event type)
	return on click (class (m)) {m, e}
end
//[cf]
//[c]
//[of]:on child event (child, event)
public func on child event (m: box, child: box, e: event)
	return on child event (class (m)) {m, child, e}
end
//[cf]
//[of]:notify parent (event)
//[c]Sends the event to the parent with self as the source of the event
//[c]
public func notify parent (m: box, e: event)

	def parent = parent (m)
	if is nil (parent)
		return false
	end
	
	return on child event (parent, m, e)

end
//[cf]
//[cf]
//[cf]
//[cf]
//[of]:box class
//[of]:type
public struct box class : local sys box class
	mem size : size

	// Initialize-Release
	release: {box} void
	get interface: {box, interface id} interface

	// Geometry
	compute min size: {box} void
	adjust: {box} void
	
	// Focus
	accept focus: {box} bool
	
	// Selection
	cut: {box} bool
	copy: {box} bool
	paste: {box} bool
	clear: {box} bool
	select all: {box} bool
	can cut: {box} bool
	can copy: {box} bool
	can paste: {box} bool
	can clear: {box} bool
	can select all: {box} bool
	take primary selection: {box} void
	add primary selection format: {box, clipboard format} void
	query primary selection: {box, clipboard format} void
	send primary selection: {box, query primary selection event, [] byte, size} void

	// Events
	on size: {box} void
	on paint: {box, canvas, rectangle} void
	on drop files : {box, drop files event} void
	on receive primary selection: {box, receive primary selection event} void
	on query primary selection: {box, query primary selection event} void

	on focus: {box, event} bool
	on blur: {box, event} bool
	on key down: {box, event} bool
	on key up: {box, event} bool
	on char: {box, event} bool
	on mouse down: {box, event} bool
	on mouse up: {box, event} bool
	on mouse move: {box, event} bool
	on mouse wheel: {box, event} bool
	on mouse cancel: {box, event} bool
	on mouse double click: {box, event} bool
	on popup: {box, event} bool
	on enter menu: {box, event} bool
	on leave menu: {box, event} bool
	
	on value changed: {box, event} bool
	on selection changed: {box, event} bool
	on index changed: {box, event} bool
	on click: {box, event} bool

	on child event: {box, box, event} bool
end
//[cf]
//[of]:box class
//[c]Initializes the box class
//[c]
public func box class
	def c = the box class
	if ~ initialized
		initialized = true

		equ notify parent = ^notify parent (box, event)
		equ do nothing = ^do nothing (box)
		equ yes = ^yes (box)
		equ no = ^no (box)

		c . mem size = sizeof local box class
		c . release = ^actual release (box)
		c . get interface = ^actual get interface (box, interface id)
		c . adjust = do nothing

		c . compute min size = do nothing
		c . accept focus = no
		c . cut = ^actual cut (box)
		c . copy = no
		c . paste = no
		c . clear = no
		c . select all = no
		c . can cut = ^actual can cut (box)
		c . can copy = no
		c . can paste = no
		c . can clear = no
		c . can select all = no
		c . take primary selection = do nothing
		c . add primary selection format = ^do nothing (box, clipboard format)
		c . query primary selection = ^do nothing (box, clipboard format)
		
		c . on size = default size function
		c . on paint = ^handle paint (box, canvas, rectangle)
		c . on drop files = ^handle drop files (box, drop files event)
		c . on query primary selection = ^handle query primary selection (box, query primary selection event)
		c . on receive primary selection = ^handle receive primary selection (box, receive primary selection event)

		c . on focus = default focus function
		c . on blur = default blur function
		c . on key down = default key down function
		c . on key up = default key up function
		c . on char = default char function
		c . on mouse down = default mouse down function
		c . on mouse up = default mouse up function
		c . on mouse move = default mouse move function
		c . on mouse wheel = default mouse wheel function
		c . on mouse cancel = default mouse cancel function
		c . on mouse double click = default mouse double click function
		c . on popup  = default popup function
		c . on enter menu = default enter menu function
		c . on leave menu = default leave menu function

		c . on value changed = default value changed function
		c . on selection changed = default selection changed function
		c . on index changed = default index changed function
		c . on click = default click function

		c . on child event = ^handle child event (box, box, event)

		// win32 events
		c . window proc = ^window proc (box, UINT, WPARAM, LPARAM)
		c . wm create = ^actual create (box, UINT, WPARAM, LPARAM)
		c . wm destroy = ^default window proc (box, UINT, WPARAM, LPARAM)
		c . wm paint = ^default window proc (box, UINT, WPARAM, LPARAM)
		c . wm close = ^default window proc (box, UINT, WPARAM, LPARAM)
		c . wm size = ^default window proc (box, UINT, WPARAM, LPARAM)
		c . wm command = ^default window proc (box, UINT, WPARAM, LPARAM)
		c . wm notify = ^default window proc (box, UINT, WPARAM, LPARAM)
		c . wm hscroll = ^wm hscroll (box, UINT, WPARAM, LPARAM)
		c . wm vscroll = ^wm vscroll (box, UINT, WPARAM, LPARAM)
		c . wm key down = ^wm key down (box, UINT, WPARAM, LPARAM)
		c . wm key up = ^wm key up (box, UINT, WPARAM, LPARAM)
		c . wm char = ^wm char (box, UINT, WPARAM, LPARAM)
		c . wm set cursor = ^wm set cursor (box, UINT, WPARAM, LPARAM)
		c . wm activate = ^default window proc (box, UINT, WPARAM, LPARAM)
		c . wm copy data = ^default window proc (box, UINT, WPARAM, LPARAM)
		c . command = ^actual command (box, WORD)
		c . notify = ^actual notify (box, LPNMHDR)
		c . hscroll = ^ignore scroll (box, int, int)
		c . vscroll = ^ignore scroll (box, int, int)
		c . move = ^do move (box, rectangle, bool)
		c . set read only = ^do nothing (box, bool)
		c . set enable = ^do set enable (box, bool)
	end
	return c
end

private def initialized = false
private def the box class: local box class
//[cf]
//[of]:copy (src class)
//[c]
public func copy (dst: box class, src: box class)
	copy (dst:mem, src:mem, mem size (src))
end
//[cf]
//[cf]

⌨️ 快捷键说明

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