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

📄 dialog-box.zc

📁 实现树形结构
💻 ZC
字号:
//[of]:description
//[c]Dialog box
//[c]
//[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"
import "base/memory-allocator"
import "text/string"
//[c]
import "user/box"
import "user/keyboard"
import "user/frame"
import "user/button"
import "user/grid-box"
import "user/box-initializers"
//[cf]
//[c]
//[of]:constants
//[c]
public def str ok := "Ok"
public def str cancel := "Cancel"
//[c]
public equ default inner margin = 3

public enum ok cancel component
	ok button
	cancel button
	ok cancel max
end

public def ok cancel buttons := grid box (
	0,
	5,
	const [] int (1, 0, 0, -1),
	const [] int (0, -1),
	const grid cells (
		grid cell (ok button << button (str ok), 1, 0),
		grid cell (cancel button << button (str cancel), 2, 0),
		nil) )

public def ok cancel buttons 2 := grid box (
	0,
	5,
	const [] int (1, 1, -1),
	const [] int (0, 1, -1),
	const grid cells (
		grid cell (ok button << button (str ok), 0, 0),
		grid cell (cancel button << button (str cancel), 1, 0),
		grid cell (blank box (1), 0, 1, 2, 1),
		nil) )
//[c]
public equ dialog box resources (c: i box, title: string, w: int, h : int, db: int, cb: int) =
	const dialog box resources (c, title, w, h , db, cb)
//[c]
//[cf]
//[of]:dialog box
//[of]:type
public struct dialog box : local frame box

	// returned code
	public result: int

	private default button: button
	private cancel button: button
	private original default button: button

end
//[cf]
//[of]:instance creation
//[of]:new dialog box
public func new dialog box

	equ s = sizeof local dialog box
	def f = allocate memory (s): dialog box
	initialize (f)
	return f

end
//[cf]
//[of]:new dialog box (parent)
public func new dialog box (parent: box)

	equ s = sizeof local dialog box
	def f = allocate memory (s): dialog box
	initialize (f, parent)
	return f

end
//[cf]
//[cf]
//[of]:opening - closing
//[of]:show modal
//[c]Shows the dialog box and enters the modal loop
//[c]
public func show modal (m: dialog box)

	activate (m)
	modal loop (m)
	return result (m)

end
//[cf]
//[of]:close (result)
//[c]Closes and sets returned value
//[c]
public func close (m: dialog box, result: int)
	result (m) = result
	close (m)
end
//[cf]
//[cf]
//[of]:accessing
//[of]:set default button (button)
//[c]Sets the default button (permanent)
//[c]
public func set default button (m: dialog box, b: button)
	original default button (m) = b
	set current default button (m, b)
end
//[cf]
//[of]:set cancel button (button)
//[c]Configures the cancel button
//[c]
public func set cancel button (m: dialog box, b: button)
	cancel button (m) = b
end
//[cf]
//[c]
//[cf]
//[c]
//[of]:restricted
//[of]:initialize
public func initialize (m: dialog box)
	initialize (m, nil, nil)
end
//[cf]
//[of]:initialize (parent, syle, class, resources, components)
public func initialize (
		m: dialog box, 
		parent: box, 
		style: box style,
		class: box class,
		default result: int,
		resources: dialog box resources,
		components: [] object)

	initialize (m, parent, style)
	class (m) = class
	result (m) = default result
	set title (m, title (resources))
	set default size (m, 
		default width (resources), 
		default height (resources))

	// initialize with resources
	new (content (resources), m, components)

	def default = default button (resources)
	if default <> -1
		set default button (m, components [default] : button)
	end

	def cancel = cancel button (resources)
	if cancel <> -1
		set cancel button (m, components [cancel] : button)
	end
	
end
//[cf]
//[of]:initialize (parent, style)
public func initialize (m: dialog box, parent: box, style: box style)

	initialize (super (m), parent, style)
	class (m) = dialog box class
	is dialog (m) = true

	original default button (m) = nil
	default button (m) = nil
	cancel button (m) = nil

end
//[cf]
//[c]
//[of]:handle child event (child, event)
//[c]A child has gained the focus
//[c]
//[c]If the widget that has acquired the focus is a button, the default button 
//[c]must be temporarily reassigned.
//[c]
public func handle child event (m: dialog box, child: box, evt: event)

	def type = type (evt)
	if type == focus event type && child <> default button (m)
		def button = as button (child)
		if is nil (button)
			button = original default button (m)
		end
		
		set current default button (m, button)
	
	elsif type == key down event type
		equ e = evt: key event
		
		def button = default button (m)
		if not nil (button) && key (e) == VK RETURN
			click (button)
			return true
		end
		
		button = cancel button (m)
		if not nil (button) && key (e) == VK ESCAPE
			click (button)
			return true
		end
	
	end

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

end
//[cf]
//[cf]
//[of]:private
//[of]:set current default button (button)
//[c]Sets the current default button (temporary)
//[c]
private func set current default button (m: dialog box, b: button)
	
	if b <> default button (m)
		if not nil (default button (m))
			set default (default button (m), false)
		end
		default button (m) = b
		if not nil (b)
			set default (b, true)
		end
	end

end
//[cf]
//[cf]
//[cf]
//[of]:dialog box class
//[of]:type
public struct dialog box class : local frame box class
	// empty
end
//[cf]
//[of]:dialog box class
public func dialog box class

	def c = the dialog box class
	if ~ initialized
		initialized = true
		c . copy (frame box class)
		c . on child event = ^handle child event (dialog box, box, event)
	end
	return c

end

private def initialized = false
private def the dialog box class: local dialog box class
//[cf]
//[cf]
//[of]:dialog box resource
//[of]:type
struct dialog box resources

	content : i box
	title : string
	default width : int
	default height : int
	default button : int
	cancel button : int

end
//[cf]
//[cf]

⌨️ 快捷键说明

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