menu.zc

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

ZC
835
字号
//[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
//[c]
import "base/types"
import "base/memory-allocator"
import "text/string"
import "text/string-buffer"
import "collection/vector"
import "graphics/geometry"
import "user/accelerator"
import "user/command"
import "user/box"
import "user/keyboard"

import "private/sys-command"
import "private/sys-menu"

import "glib/glib"
import "glib/glib-object"
import "gdk/gdk"
import "gtk/gtk"
//[cf]
//[of]:structures
//[c]
//[c]	Common ancestor to menu and menu item
//[c]
public struct menu base : local system menu

	public id : int
	public is menu : bool
	public is separator : bool

end
//[c]
//[c]	Menu
//[c]
public struct menu: local menu base

	private is menu bar : bool

	// for popup menu in the main menu
	// (not display for contextual menus)
	caption: string

	children: local vector

end
//[c]
//[c]	Menu item
//[c]
public struct menu item: local menu base

	// the menu containing the item
	parent: menu
	
	// the command
	cmd: command
	
	// watch state changes of the command
	listener: local command listener

	// the caption
	// It is the caption of the command plus the
	// description of the accelerator
	caption: string

end
//[c]
//[c]	Menu separator
//[c]
public struct menu separator: local menu base

	private widget: GtkSeparatorMenuItem

end
//[cf]
//[c]
//[of]:menu
//[of]:instance creation
//[of]:new menu
//[c]
public func new menu

	def menu = allocate memory (sizeof local menu): menu
	id (menu) = 0
	is menu (menu) = true
	is separator (menu) = false
	is menu bar (menu) = true

	def menu bar = gtk_menu_bar_new : GtkMenuBar
	gtk_widget_show (menu bar)
	menu bar (menu) = menu bar
	
	initialize (children (menu), 8)
	return menu

end
//[cf]
//[of]:new popup menu (id, caption)
//[c]
public func new popup menu (id: int, caption: string)

	def menu = allocate memory (sizeof local menu): menu
	id (menu) = id
	is menu (menu) = true
	is separator (menu) = false
	is menu bar (menu) = false
	caption (menu) = caption
	initialize (children (menu), 8)

	def t := temp string buffer
	append caption (caption, t)
	def menu item = gtk_menu_item_new_with_mnemonic (as string (t)) : GtkMenuItem
	release (t)

	def popup menu = gtk_menu_new : GtkMenu
	gtk_widget_show (menu item)
	gtk_widget_show (popup menu)
	gtk_menu_item_set_submenu (menu item, popup menu)
	menu item (menu) = menu item
	popup menu (menu) = popup menu

	return menu

end
//[cf]
//[c]
//[of]:delete (menu)
//[c]
public func delete (menu: menu) : void

	each (menu) ? child
		delete (child)
	end
	release (children (menu))
	free memory (menu)

end
//[cf]
//[cf]
//[of]:initialization macros
//[of]:new (i top menu, recipient, components)
//[c]
//[c]DESCRIPTION
//[c]	Creates a menu from a menu initializer structure.
//[c]	
//[c]ARGUMENTS
//[c]	i is a pointer to the structures describing the menu.
//[c]	recipient is the object that will be passed to all commands defined in this menu.
//[c]	c is an array to store labels
//[c]
//[c]RETURN VALUE
//[c]	Returns a newly created menu object
//[c]
public func new (i: i top menu, recipient: object, c: [] object)

	def menu = new menu
	def items = items (i)
	while not nil (items[])
		def item = items[]
		def type = type (item)
		if type == mit popup
			append menu (menu, new (item : i popup menu, recipient, c))
		else
			append menu (menu, new (item : i label popup, recipient, c))
		end		
		++items
	end

	return menu

end
//[cf]
//[of]:new (i popup menu, recipient, components)
//[c]Creates a popup menu from a popup menu initializer
//[c]
public func new (i: i popup menu, recipient: object, comp: [] object): menu

	def menu = new popup menu (id (i), caption (i))
	def items = items (i)

	while not nil (items[])
		def c = items[]
		def type = type (c)
		if type == mit command
			append menu (menu, new (c:i command, recipient))
		elsif type == mit popup
			append menu (menu, new (c:i popup menu, recipient, comp))
		elsif type == mit label command
			append menu (menu, new (c:i label command, recipient, comp))
		elsif type == mit label popup
			append menu (menu, new (c:i label popup, recipient, comp))
		else
			append separator (menu)
		end
		++ items
	end

	return menu

end
//[cf]
//[c]
//[of]:top menu (items)
//[c]
public equ top menu (items: [] i menu) = 
	const i top menu (items)
//[cf]
//[of]:popup menu (caption, items)
//[c]
public equ popup menu (id: int, caption: string, items: [] i menu) = 
	const i popup menu (mit popup, id, caption, items)
//[cf]
//[of]:command (name, caption, description, run)
//[c]
public equ command (name: string, caption: string, description: string, run: {box} void) = 
	const i command (mit command, name, caption, description, 0, 0, true, false, run)
//[cf]
//[of]:command (name, caption, description, flags, key, run)
//[c]
public equ command (
		name: string,
		caption: string, 
		description: string, 
		flags: int,
		key: int,
		run: {box} void) = 

	const i command (mit command, name, caption, description, flags, key, true, false, run)
//[cf]
//[of]:command (name, caption, description, enabled, checked, run)
//[c]
public equ command (
		name: string,
		caption: string, 
		description: string, 
		enabled: bool,
		checked: bool,
		run: {box} void) = 

	const i command (mit command, name, caption, description, 0, 0, enabled, checked, run)
//[cf]
//[of]:command (name, caption, description, flags, key, enabled, checked, run)
//[c]
public equ command (
		name: string,
		caption: string, 
		description: string, 
		flags: int,
		key: int,
		enabled: bool,
		checked: bool,
		run: {box} void) = 

	const i command (mit command, name, caption, description, flags, key, enabled, checked, run)
//[cf]
//[of]:separator
//[c]
public equ separator = 
	const i menu (mit separator)
//[cf]
//[c]
//[of]:label << popup
//[c]This macros is used to save the pointer of the generated popup
//[c]
public equ @shl (label: int, obj: i popup menu) = 
	const i label popup (mit label popup, label, obj)
//[cf]
//[of]:label << command
//[c]This macros is used to save the pointer to the command generated
//[c]from the initializer object.
//[c]
//[c]	Example:
//[c]	
//[c]	enum
//[c]		open cmd
//[c]		close cmd
//[c]		...
//[c]	end
//[c]	
//[c]	...
//[c]	open cmd << command ("Open...", "Open a new file", nil)
//[c]	...
//[c]	
//[c]	The assignation will take effect when creating actual commands
//[c]
public equ @shl (label: int, obj: i command) = 
	const i label command (mit label command, label, obj)
//[cf]
//[c]
//[of]:private
//[c]
//[of]:i menu
//[c]
struct i menu

	type: 
		enum
			mit separator
			mit command
			mit popup
			mit label command
			mit label popup
		end

end
//[cf]
//[of]:i command
//[c]
struct i command: local i menu

	name: string
	caption: string
	description: string
	flags: int
	key: int
	enabled: bool
	checked: bool
	run: {box} void

end
//[cf]
//[of]:i popup menu
//[c]
struct i popup menu: local i menu

	id : int
	caption : string
	items : [] i menu

end
//[cf]
//[of]:i label command
//[c]
struct i label command : local i menu

	label: int
	cmd: i command

end
//[cf]
//[of]:i label popup
//[c]
struct i label popup : local i menu

	label: int
	popup: i popup menu

end
//[cf]
//[of]:i top menu
//[c]
public struct i top menu

	items: [] i menu

end
//[cf]
//[of]:menu items
//[c]
public typedef menu items = [] i menu
//[cf]
//[c]
//[of]:new (i command, recipient)
//[c]
func new (i: i command, recipient: object)

	return new command (
		name (i),
		caption (i), 
		description (i), 
		flags (i): byte,
		key (i),
		enabled (i), 
		checked (i), 
		run (i),
		recipient)

end
//[cf]
//[of]:new (i label popup, recipient, components)
//[c]
//[c]DESCRIPTION
//[c]	Creates a menu from a label popup initializer structure.
//[c]	
//[c]ARGUMENTS
//[c]	i is a pointer to the structures describing the label.
//[c]	recipient is the object that will be passed to all commands defined in this menu.
//[c]	c is an array to store labels
//[c]
//[c]RETURN VALUE
//[c]	Returns a newly created popup object
//[c]
private func new (i: i label popup, recipient: object, c: [] object)

	def popup = new (popup (i), recipient, c)
	c [label (i)] = popup
	return popup

end
//[cf]

⌨️ 快捷键说明

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