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

📄 accelerator.zc

📁 实现树形结构
💻 ZC
字号:
//[of]:description
//[c]accelerator - define a keyboard short cut
//[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/memory-allocator"
import "base/types"
import "text/string"
import "text/string-buffer"
import "user/keyboard"
//[cf]
//[c]
//[of]:accelerator
//[of]:type
//[c]
public struct accelerator
	flags: byte
	private _pad1: byte // ### bug with alignment in compiler
	private _pad2: word // ### bug with alignment in compiler
	key: int
end
//[cf]
//[c]
//[of]:instance creation
//[of]:new accelerator (accelerator)
public func new accelerator (a: accelerator)
	def m = allocate memory (sizeof local accelerator) : accelerator
	flags (m) = flags (a)
	key (m) = key (a)
	return m
end
//[cf]
//[of]:delete (m)
public func delete (m: accelerator)
	free memory (m)
end
//[cf]
//[cf]
//[of]:output
//[of]:append (string buffer, accelerator)
//[c]Appends a description of the accelerator to the string buffer
//[c]
public func append (s: string buffer, a: accelerator)

	def flags = flags (a)
	def key = key (a)
	
	// no accelerator ?
	if key == 0
		return s
	end
	
	// Append the modifiers
	if (flags & CTRL:byte) <> 0:b
		s << control string << $+
	end
	if (flags & ALT:byte) <> 0:b
		s << alt string << $+
	end
	if (flags & SHIFT:byte) <> 0:b
		s << shift string << $+
	end

	// Is is a regular ascii key code  
	equ c = key: char
	if ($A <= c && c <= $Z) || ($0 <= c && c <= $9)
		s << c
		return s
	end

	// Is is a function key
	if VK F1 <= key && key <= VK F24
		s << $F << (key:int - VK F1 + 1)
		return s
	end

	// Search in the list	
	def p = names
	repeat
		def k = key (p[])
		if k == 0
			break
		end
		if k == key
			s << name (p[])
			return s
		end
		++p
	end
	
	// Not found: display a question mark
	s << "?"
	return s
end
//[cf]
//[of]:string buffer << accelerator
//[c]
public equ @shl (s: string buffer, a: accelerator) = 
	append (s, a)
//[cf]
//[cf]
//[of]:parsing
//[of]:accelerator (s)
//[c]Convert a string to an accelerator
//[c]
//[c]Examples:
//[c]	+
//[c]	A
//[c]	Ctrl+A
//[c]	Shift+A
//[c]	Shift+Ctrl+Numpad 9
//[c]
public func accelerator (s: string, return a: accelerator)

	def flags = 0 : byte
	def key = 0

	def p = s
	repeat
		def start = p
		def limit = p
						
		def c = p++[]
		if is nul (c)
			break
		end

		repeat
			c = p++[]
			if c == nul char
				p -= 1
				limit = p
				break
			elsif c == $+
				limit = p - 1
				break
			end
		end
		
		def size = limit - start
		c = upper (start [])
	
		// is it alpha or digit ?
		if size == 1 && (($A <= c && c <= $Z) || ($0 <= c && c <= $9))
			key = c : int
			
		// is it a F1-F24 key func
		elsif c == $F && is digit (start [1])
			def n = decimal to integer (start + 1)
			if n >= 1 && n <= 24
				key = VK F1 + n - 1
			end
		
		// CTRL ?
		elsif compare no case (start, control string, size) == 0
			flags |= CTRL:byte
		
		// SHIFT ?
		elsif compare no case (start, shift string, size) == 0
			flags |= SHIFT:byte
		
		// ALT ?
		elsif compare no case (start, alt string, size) == 0
			flags |= ALT:byte
		
		// Lookup in table
		else
			def q = names
			repeat
				def k = key (q[])
				def n = name (q[])
				if k == 0
					break
				end
				if compare no case (n, start, size) == 0
					key = k
					break
				end
				q += 1
			end

		end

	end
	
	key (a) = key
	flags (a) = flags

end
//[cf]
//[cf]
//[of]:testing
//[of]:match (m, flags, key)
//[c]
public func match (a: accelerator, flags: byte, key: int)
	return flags == flags (a) && key == key (a)
end
//[cf]
//[cf]
//[of]:private
//[c]
def shift string := "Shift"
def control string := "Ctrl"
def alt string := "Alt"

def names := const [] struct; key: int; name: string; end (
	VK BACK, "Backspace",
	VK TAB, "Tab",
	VK RETURN, "Return",
	VK ESCAPE, "Escape",
	VK SPACE, "Space",
	VK PRIOR, "Page Up",
	VK NEXT, "Page Down",
	VK END, "End",
	VK HOME, "Home",
	VK LEFT, "Left",
	VK UP, "Up",
	VK RIGHT, "Right",
	VK DOWN, "Down",
	VK INSERT, "Insert",
	VK DELETE, "Delete",
	VK CANCEL, "Break",
	VK NUMPAD0, "NumPad 0",
	VK NUMPAD1, "NumPad 1",
	VK NUMPAD2, "NumPad 2",
	VK NUMPAD3, "NumPad 3",
	VK NUMPAD4, "NumPad 4",
	VK NUMPAD5, "NumPad 5",
	VK NUMPAD6, "NumPad 6",
	VK NUMPAD7, "NumPad 7",
	VK NUMPAD8, "NumPad 8",
	VK NUMPAD9, "NumPad 9",
	VK MULTIPLY, "*",
	VK ADD, "+",
	VK SUBTRACT, "-",
	VK DECIMAL, ".",
	VK DIVIDE, "/",
	0, "")
//[cf]
//[cf]

⌨️ 快捷键说明

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