box.zc

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

ZC
2,624
字号
	client rect: local rectangle
	min size: local area

	being destroyed: bool
	private valid min size: bool
	private valid size: bool

end
//[cf]
//[of]:instance creation
//[of]:Notes
//[c]This is an abstract class, there is no constructor for this class.
//[cf]
//[of]:delete
//[c]
public func delete (m: box)
	release (class (m)) {m}
	free memory (m : mem)
end
//[cf]
//[c]
//[cf]
//[of]:hierarchy
//[of]:each child
//[c]
public equ each child (b: box)
	def box = first child (b)
	while not nil (box)
		yield (box)
		box = next sibling (box)
	end
end
//[cf]
//[of]:each ancestor with self
//[c]
public equ each ancestor with self (b: box)
	def box = b
	while not nil (box)
		yield (box)
		box = parent (box)
	end
end
//[cf]
//[of]:each descendant
//[c]Iterates on all the tree
//[c]the initial box must be the root
//[c]
public equ each descendant (box: box)
	def b = box
	while not nil (b)
		yield (b)
		b = next in tree (b, false)
	end
end
//[cf]
//[c]
//[of]:next in tree (cycle)
//[c]
public func next in tree (m: box, cycle: bool)

	def n = first child (m)
	if not nil (n)
		return n
	end

	def c = m
	repeat
		n = next sibling (c)
		if not nil (n); return n; end
		
		n = parent (c)
		if is nil (n)
			return cycle -> c, nil
		end
		
		c = n
	end

	// never executed - make compiler happy
	return nil

end
//[cf]
//[of]:prev in tree
//[c]
public func prev in tree (m: box)

	def c = m
	def p = parent (m)
	if not nil (p)
		c = prev sibling (c)
		if is nil (c)
			return p
		end
	end
	
	repeat
		def n = last child (c)
		if is nil (n)
			return c
		end
		c = n
	end

	// never executed - make compiler happy
	return nil

end
//[cf]
//[of]:find child (box)
//[c]Find the child containing box
//[c]
public func find child (m: box, box: box)

	while not nil (box)
		def parent = parent (box)
		if parent == m
			return box
		end
		box = parent
	end

	return nil

end
//[cf]
//[cf]
//[of]:geometry
//[of]:move (rectangle, repaint)
//[c]Move the box
//[c]
//[c]The position of a box should be changed only by its parent from
//[c]the on size or adjust event.
//[c]
public func move (m: box, r: rectangle, repaint: bool)
	move (class (m)) {m, r, repaint}
end
//[cf]
//[of]:move (rectangle)
//[c]Move the box
//[c]
//[c]The position of a box should be changed only by its parent from
//[c]the on size or adjust event.
//[c]
public func move (m: box, r: rectangle)
	move (m, r, true)
end
//[cf]
//[of]:validate size
//[c]Validates the size
//[c]
//[c]	This method should be invoked only from the toplevel window
//[c]	before re-entering into the message loop wait event.
//[c]	
//[c]	Note: with GTK+, the validation of size is automatically handled
//[c]	so this method is useless for this platform.
//[c]
public func validate size (m: box)

	validate min size (m)
	adjust content (m)

end
//[c]
//[c]SUB-FUNCTIONS
//[of]:	validate min size
//[c]Validates the min size of the box
//[c]
//[c]Min size of all children are validated, then the min size of the
//[c]box is computed by invoking compute min size.
//[c]
private func validate min size (m: box) : void

	// Returns immediately if the min size is valid
	if valid min size (m)
		return
	end
	
	// Validate the children's min size
	each child (m) ? c
		validate min size (c)
	end

  	// Compute the min size according to the minsize of children
  	compute min size (m)
  	
  	// Now the minsize is valid
  	valid min size (m) = true
  	
	// but content is invalid
	valid size (m) = false
	
end
//[cf]
//[of]:	adjust content
private func adjust content (m: box) : void

	if valid size (m)
		return
	end

	adjust (m)
	
	// adjust the children's content
	each child (m) ? c
		adjust content (c)
	end
	
	// content is now valid
	valid size (m) = true

end
//[c]
//[of]:adjust
//[c]
//[c]Adjust is invoked after the min size of the control or
//[c]a child has just been fixed.
//[c]
//[c]A top-level box must check if it has to resize itself since it can be too small.
//[c]A child box is not responsible of its size but it must compute the
//[c]layout of its children.
//[c]
//[c]This method is invoked during the validation of sizes (validate size ()).
//[c]
//[c]Remark: this method is not invoked is the control has just been resized
//[c]by the parent.
//[c]
private func adjust (m: box)
	adjust (class (m)) {m}
end
//[cf]
//[cf]
//[cf]
//[c]
//[of]:invalidate min size
//[c]Invalidates the minsize
//[c]
public func invalidate min size (m: box): void

	// nothing to do if already invalidated
	if ~ valid min size (m)
		return
	end
	
	// now the min size is invalid
	valid min size (m) = false
	
	// the minsize of the parent is also invalid
	if not nil (parent (m))
		invalidate min size (parent (m))
	end

end
//[cf]
//[of]:min width
//[c]
public equ min width (m: box) = w (min size (m))
//[cf]
//[of]:min height
//[c]
public equ min height (m: box) = h (min size (m))
//[cf]
//[of]:set min width (w)
//[c]
public equ set min width (m: box, w: int) =
	w (min size (m)) = w
//[cf]
//[of]:set min height (h)
//[c]
public equ set min height (m: box, h: int) = 
	h (min size (m)) = h
//[cf]
//[of]:set min size (w, h)
//[c]Changes the min size
//[c]
public func set min size (m: box, w:int, h: int)
	set (min size (m), w, h)
end
//[cf]
//[cf]
//[of]:display
//[of]:invalidate
//[c]
public func invalidate (m: box)
	InvalidateRect(hwnd (m), nil, 0)
end
//[cf]
//[of]:invalidate (rectangle)
//[c]
public func invalidate (m: box, r: rectangle)
	def rect: local LPRECT
	RECT from rectangle (rect, r)
	InvalidateRect (hwnd (m), rect, 0)
end
//[cf]
//[of]:canvas
//[c]Creates a canvas for this box
//[c]
public func canvas (m: box, return c: canvas)
	initialize (c)
end
//[cf]
//[cf]
//[of]:cursor
//[of]:set cursor (cursor)
//[c]
public func set cursor (m: box, c: cursor)

	def previous = cursor (m) : cursor
	if c <> previous
		cursor (m) = c
		if contains (client rect (m), cursor position (m)) && not nil (c)
			SetCursor (hCursor (c))
		end
	end

	return previous

end
//[cf]
//[of]:cursor position
//[c]
public func cursor position (m: box, return pt: point)

	def p: POINT
	GetCursorPos (p)
	ScreenToClient (hwnd (m), p)
	x (pt) = x (p)
	y (pt) = y (p)

end
//[cf]
//[of]:start capture
//[c]
public func start capture (m: box)
	SetCapture (hwnd (m))
end
//[cf]
//[of]:stop capture
//[c]
public func stop capture (m: box)
	ReleaseCapture
end
//[cf]
//[cf]
//[of]:focus
//[of]:set focus
//[c]
public func set focus (m: box) : void

	if can get focus (m)
		// if the control seems to accept focus, call the windows
		// API to transfer actually the focus to the control
		SetFocus (hwnd (m))
	else
		// the control does not accept the focus, transfer the
		// focus to the next valid control.
		transfer focus (m, false)
	end

end
//[cf]
//[of]:transfer focus (reverse)
//[c]Transfers the focus to the first valid control after self.
//[c]It can be a descendant of self.
//[c]
public func transfer focus (m: box, reverse: bool)

	def first = m
	def c = first
	repeat
		if reverse
			c = prev in tree (c)
		else
			c = next in tree (c, true)
		end
		
		// completed the loop, no box accept focus
		if c==first; return; end
		
		if can get focus (c)
			set focus (c)
			return
		end

	end

end
//[cf]
//[of]:accept focus
//[c]
public func accept focus (m: box)
	return accept focus (class (m)) {m}
end
//[cf]
//[of]:can get focus
//[c]Returns true if the control can get the focus
//[c]
public func can get focus (m: box)

	if ~ accept focus (m)
		return false
	end

	each ancestor with self (m) ? c
		if not visible (c) || not enabled (c) || being destroyed (c)
			return false
		end
	end

	return true

end
//[cf]
//[cf]
//[of]:selection
//[of]:cut
//[c]
public func cut (m: box)
	return cut (class (m)) {m}
end
//[cf]
//[of]:copy
//[c]
public func copy (m: box)
	return copy (class (m)) {m}
end
//[cf]
//[of]:paste
//[c]
public func paste (m: box)
	return paste (class (m)) {m}
end
//[cf]
//[of]:clear
//[c]
public func clear (m: box)
	return clear (class (m)) {m}
end
//[cf]
//[of]:select all
//[c]
public func select all (m: box)
	return select all (class (m)) {m}
end
//[cf]
//[c]
//[of]:can cut
//[c]
public func can cut (m: box)
	return can cut (class (m)) {m}
end
//[cf]
//[of]:can copy
//[c]
public func can copy (m: box)
	return can copy (class (m)) {m}
end
//[cf]
//[of]:can paste
//[c]
public func can paste (m: box)
	return can paste (class (m)) {m}
end
//[cf]
//[of]:can clear
//[c]
public func can clear (m: box)
	return can clear (class (m)) {m}
end
//[cf]
//[of]:can select all
//[c]
public func can select all (m: box)
	return can select all (class (m)) {m}
end
//[cf]
//[c]
//[of]:take primary selection
//[c]The control says that it owns the primary selection
//[c]
public func take primary selection (m: box)
	take primary selection (class (m)) {m}
end
//[cf]
//[of]:add primary selection format (clipboard format)
//[c]
public func add primary selection format (m: box, cf: clipboard format)
	add primary selection format (class (m)) {m, cf}
end
//[cf]
//[of]:query primary selection (clipboard format)
//[c]
public func query primary selection (m: box, cf: clipboard format)
	query primary selection (class (m)) {m, cf}
end
//[cf]
//[of]:send primary selection (event, buffer, size)
//[c]This method is called from a query primary selection event
//[c]
public func send primary selection (
		m: box, 
		e: query primary selection event, 
		buffer: [] byte,
		size: size)

	send primary selection (class (m)) {m, e, buffer, size}
end
//[cf]
//[cf]
//[of]:visibility
//[of]:set visible (bool)
//[c]
public func set visible (m: box, vi: bool)

	// exit if unchanged
	if visible (m) == vi
		return
	end
	
	def show : int
	if vi 
		show = SW_SHOW 
	else 
		show = SW_HIDE
	end
	ShowWindow (hwnd (m), show)
	
	visible (m) = vi
	
end
//[cf]
//[of]:show
//[c]
public func show (m: box)
	set visible(m, true)
end
//[cf]
//[of]:hide
//[c]
public func hide (m: box)
	set visible(m, false)
end
//[cf]
//[c]
//[of]:is visible
//[c]
public func is visible (m: box)
	return visible (m)
end
//[cf]
//[of]:not visible
//[c]
public equ not visible (b: box) = ~ is visible (b)
//[cf]
//[cf]
//[of]:enabling
//[of]:set enable (bool)
//[c]
public func set enable (m: box, en: bool)
	set enable (class (m)) {m, en}
end
//[cf]
//[of]:enable
//[c]
public func enable (m: box)
	set enable(m, true)
end
//[cf]
//[of]:disable
//[c]
public func disable (m: box)
	set enable(m, false)
end
//[cf]
//[c]
//[of]:is enabled
//[c]
public func is enabled (m: box)
	return enabled (m)
end
//[cf]
//[of]:not enabled
//[c]
public equ not enabled (b: box) = ~ is enabled (b)
//[cf]
//[cf]
//[of]:read only
//[of]:set read only (bool)
//[c]
public func set read only (m: box, ro: bool)
	set read only (class (m)) {m, ro}
end
//[cf]
//[c]
//[of]:is read only
//[c]
public func is read only (m: box)
	return read only (m)
end
//[cf]
//[of]:not read only
//[c]
public equ not read only (b: box) = ~ is read only (b)
//[cf]
//[cf]
//[of]:drag and drop
//[of]:set drag accept files (bool)
//[c]
public func set drag accept files (m: box, f: bool)
	DragAcceptFiles (hwnd (m), BOOL (f))
	accept drop files (m) = f
end
//[cf]
//[c]
//[cf]
//[of]:style
//[of]:set style (style)
//[c]
public func set style (m: box, style: box style)
	style (m) = style
end
//[cf]
//[c]
//[cf]
//[of]:converting
//[of]:get interface (id)
//[c]
public func get interface (m: box, id: interface id)
	return get interface (class (m)) {m, id}
end
//[cf]
//[c]
//[cf]
//[c]
//[of]:generic functions
//[c]Useful methods for default implementation
//[c]of some methods
//[c]
//[of]:do nothing
//[c]Never does anything
//[c]
public func do nothing (m: box)
	// Do nothing
end
//[cf]
//[of]:do nothing (bool)
//[c]Never does anything
//[c]
public func do nothing (m: box, b: bool)
	// do nothing
end
//[cf]
//[of]:do nothing (clipboard format)
//[c]Never does anything
//[c]

⌨️ 快捷键说明

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