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

📄 command.zc

📁 实现树形结构
💻 ZC
字号:
//[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 "collection/vector"
import "collection/linked-collection"

import "user/accelerator"
import "private/sys-command"
//[cf]
//[c]
//[of]:command event type
//[c]
public enum command event type
	command enabling changed
	command check changed
	command caption changed
	command accelerator changed
end
//[c]
//[cf]
//[of]:command event
//[of]:type
public struct command event
	type: command event type
	accelerator: local accelerator
end
//[cf]
//[cf]
//[of]:command listener
//[of]:type
public struct command listener
	private recipient: object
	private notify: {object, command, command event} void
end	
//[cf]
//[c]
//[of]:initialize - release
//[of]:initialize (recipient, notify code)
//[c]
public func initialize (
		listener: command listener,
		recipient: object,
		notify: {object, command, command event} void)
		
	recipient (listener) = recipient
	notify (listener) = notify

end
//[cf]
//[cf]
//[cf]
//[of]:command
//[of]:type
public struct command: local system command

	name: string
	caption: string
	description: string
	
	// icon
	accelerator: local accelerator

	private enabled: bool
	private checked: bool

	private code: {object} void
	private recpt: object
	
	// listeners
	private listeners: local vector
		
end
//[cf]
//[c]
//[of]:class initialization
//[of]:initialize commands
//[c]
public func initialize commands
	initialize (global commands)
end
//[cf]
//[of]:release commands
//[c]
public func release commands

	def c = first (global commands)
	while not nil (c)
		def next = next (c)
		delete (c: command)
		c = next
	end

end
//[cf]
//[cf]
//[of]:class enumerating
//[of]:each global command
public equ each global command
	each (global commands) ? c
		yield (c: command)
	end
end
//[cf]
//[cf]
//[c]
//[of]:instance creation
//[of]:new command (caption, description, flags, key, enabled, checked, code, recipient)
//[c]
public func new command (
		name: string,
		caption: string, 
		description: string,
		flags: byte,
		key: int,
		enabled: bool,
		checked: bool,
		code: {object} void,
		recipient: object)

	def cmd = allocate memory (sizeof local command) : command

	initialize (super (cmd))	
	initialize (listeners (cmd), 2)
	name (cmd) = name
	caption (cmd) = caption
	description (cmd) = description
	flags (accelerator (cmd)) = flags
	key (accelerator (cmd)) = key
	enabled (cmd) = enabled
	checked (cmd) = checked
	code (cmd) = code
	recpt (cmd) = recipient

	add (global commands, cmd)

	return cmd

end
//[cf]
//[of]:delete
//[c]
public func delete (cmd: command)
	remove (global commands, cmd)
	release (listeners (cmd))
	release (super (cmd))
	free memory (cmd)
end
//[cf]
//[c]
//[cf]
//[of]:actions
//[of]:run
//[c]Performs action
//[c]
public equ run (cmd: command) =
	if is enabled (cmd)
		code (cmd:command) {recpt (cmd)}
	end
//[cf]
//[cf]
//[of]:adding - removing
//[of]:add listener (listener)
//[c]
public func add listener (cmd: command, listener: command listener)
	add (listeners (cmd), listener)
end
//[cf]
//[of]:remove listener (listener)
//[c]
public func remove listener (cmd: command, listener: command listener)
	remove (listeners (cmd), listener)
end
//[cf]
//[c]
//[cf]
//[of]:accessing
//[of]:enable
//[c]
public func enable (cmd: command)
	set enable (cmd, true)
end
//[cf]
//[of]:disable
//[c]
public func disable (cmd: command)
	set enable (cmd, false)
end
//[cf]
//[of]:set enable (bool)
//[c]
public func set enable (cmd: command, enabled: bool)

	if enabled (cmd) <> enabled
		def e : local command event
		type (e) = command enabling changed
		enabled (cmd) = enabled
		notify listeners (cmd, e)
	end

end
//[cf]
//[c]
//[of]:check
//[c]
public func check (cmd: command)
	set checked (cmd, true)
end
//[cf]
//[of]:uncheck
//[c]
public func uncheck (cmd: command)
	set checked (cmd, false)
end
//[cf]
//[of]:set checked (bool)
//[c]
public func set checked (cmd: command, checked: bool)

	if checked (cmd) <> checked
		def e : local command event
		type (e) = command check changed
		checked (cmd) = checked
		notify listeners (cmd, e)
	end

end
//[cf]
//[c]
//[of]:set caption (caption)
//[c]
public func set caption (cmd: command, caption: string)

	if caption (cmd) <> caption
		def e : local command event
		type (e) = command caption changed
		caption (cmd) = caption
		notify listeners (cmd, e)
	end

end
//[cf]
//[of]:set accelerator (accelerator)
//[c]
public func set accelerator (cmd: command, a: accelerator)

	equ b = accelerator (cmd)
	def flags = 0 : byte
	def key = 0
	if not nil (a)
		flags = flags (a)
		key = key (a)
	end

	if flags (b) <> flags || key (b) <> key
		def e : local command event
		type (e) = command accelerator changed
		flags (accelerator (e)) = flags (b)
		key (accelerator (e)) = key (b)
		flags (b) = flags
		key (b) = key
		notify listeners (cmd, e)
	end

end
//[cf]
//[cf]
//[of]:testing
//[of]:is enabled
//[c]
public equ is enabled (cmd: command) = enabled (cmd)
//[cf]
//[of]:is checked
//[c]
public equ is checked (cmd: command) = checked (cmd)
//[cf]
//[cf]
//[of]:private
//[of]:globals
def global commands: local linked collection
//[cf]
//[of]:notify listeners (event)
//[c]
func notify listeners (cmd: command, event: command event)

	each (listeners (cmd)) ? e
		equ listener = e: command listener
		notify (listener) {recipient (listener), cmd, event}
	end

end
//[cf]
//[cf]
//[cf]

⌨️ 快捷键说明

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