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

📄 notebook.zc

📁 实现树形结构
💻 ZC
字号:
//[of]:description
//[c]Display multiples pages using a notebook
//[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/array-of-strings"
import "text/string"
import "text/string-buffer"
//[c]
import "graphics/geometry"
import "graphics/graphics"
import "user/box"
//[c]
import "glib/glib"
import "glib/glib-object"
import "gtk/gtk"
//[cf]
//[c]
//[of]:notebook
//[of]:type
public struct notebook : local box

	private 
		pages : local vector
		labels : local array of strings
		tab bottom : bool

end
//[cf]
//[of]:instance creation
//[of]:new notebook (parent)
public func new notebook (parent: box)

	equ s = sizeof local notebook
	def e = allocate memory (s): notebook
	initialize (e, parent)
	return e

end
//[cf]
//[cf]
//[of]:pages
//[of]:add (page, label)
//[c]Adds a new page
//[c]
public func add (m: notebook, page: box, label: string)

	add (labels (m), label)
	add (pages (m), page)

	if activated (m) && ~ activated (page)
		activate (page)
		invalidate min size (m)
		set label (m, page, label)
	end

end
//[cf]
//[of]:remove (page)
public func remove (m: notebook, page: box)

	delete (page)

end
//[cf]
//[of]:each page
//[c]Enumerates pages
//[c]
public equ each page (m: notebook)

	each (pages (m)) ? e
		equ page = e : box
		yield (page)
	end

end
//[cf]
//[of]:set label (page, label)
public func set label (m: notebook, page: box, label: string)

	def index = index (m, page)
	if index == -1
		return
	end
	
	set (labels (m), index, label)

	def child = widget (page)
	gtk_notebook_set_tab_label_text (gtk notebook (m), child, to UTF8 (label))

end
//[cf]
//[cf]
//[of]:accessing
//[of]:index
//[c]Returns the index of the current selected page
//[c]
//[c]	Returns -1 if no page is selected
//[c]
private func index (m: notebook)

	// If the notebook is destroyed, return immediately
	// or
	// If the notebook is not activated yet, the pages
	// are not attached. return the first index
	if ~activated (m) || is nil (widget (m))
		if size (pages (m)) > 0
			return 0
		else
			return -1
		end
	end

	return gtk_notebook_get_current_page (gtk notebook (m)) : int

end
//[cf]
//[of]:page
//[c]Changes the current page
//[c]
public func page (m: notebook)

	def index = index (m)
	if index == -1
		return nil
	end
	
	return page (m, index)

end
//[cf]
//[of]:set index (index)
//[c]Changes the page
//[c]
public func set index (m: notebook, index: int)

	// if the notebook is destroyed, return immediately
	if is nil (widget (m))
		return
	end

	if index == -1
		return
	end

	if index  == index (m)
		return
	end

	gtk_notebook_set_current_page (gtk notebook (m), index)

end
//[cf]
//[of]:set page (page)
//[c]Changes the current page
//[c]
public func set page (m: notebook, page: box)

	set index (m, index (m, page))

end
//[cf]
//[cf]
//[of]:tabs
//[of]:set tabs position
public enum notebook tab position
	tab position top
	tab position bottom
end
	
public func set tab position (m: notebook, pos: notebook tab position)
	tab bottom (m) = (pos == tab position bottom)
	gtk_notebook_set_tab_pos (gtk notebook (m), tab bottom (m) -> GTK_POS_BOTTOM, GTK_POS_TOP)
end

//[cf]
//[cf]
//[c]
//[of]:restricted
//[of]:initialize (parent)
public func initialize (m: notebook, parent: box)

	def t = gtk_notebook_new : GtkNotebook
	widget (m) = t
	gtk_notebook_set_scrollable (t, TRUE)
	gtk_widget_show (t)

	// must connect after, otherwise the current page is not up to date
	// when emitting 'on selection changed'
	g_signal_connect_after (
		widget (m), 
		"switch-page", 
		^switched page (GtkNotebook, GtkNotebookPage, guint, notebook):GCallback,
		m)

	initialize (super (m), parent)
	class (m) = notebook class
	tab bottom (m) = false
	initialize (labels (m), 8)
	initialize (pages (m), 8)

end
//[cf]
//[c]
//[of]:actual activate
//[c]
public func actual activate (m: notebook)

	def n = gtk notebook (m)
	def i = 0
	each child (m) ? page
		activate (page)

		def label = gtk_label_new (labels (m)[i++])
		gtk_notebook_append_page (n, widget (page), label)

	end

end
//[cf]
//[of]:actual release
//[c]
public func actual release (m: notebook)

	// Release parent class first to delete children while keeping pages
	// (needed for event handling)
	actual release (super (m))

	release (labels (m))
	release (pages (m))

end
//[cf]
//[c]
//[of]:handle child event (child, event)
//[c]Handles child events
//[c]
public func handle child event (m: notebook, src: box, evt: event)

	def t = type (evt)
	if t == destroy event type
	
		def index = index (m, src)
		
		// is it a page ?
		if index <> -1
			// remove the page from the list
			remove (pages (m), index)
		end

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

end
//[cf]
//[of]:handle focus (event)
//[c]Transfers the focus to the current page
//[c]
public func handle focus (m: notebook, e: event)

	def page = page (m)
	if not nil (page)
		set focus (page (m))
		return true
	end
	
	return handle focus (super (m), e)

end
//[cf]
//[cf]
//[of]:private
//[of]:index (page)
//[c]Returns the index of a page or -1
//[c]
private func index (m: notebook, page: box)

	def index = 0
	each page (m) ? p
		if p == page
			return index
		end
		++ index
	end
	return -1

end
//[cf]
//[of]:page (index)
//[c]Returns the page at given index
//[c]
private func page (m: notebook, index: int)

	def page = nil: box
	if index <> -1
		page = pages (m) [index] : box
	end
	return page

end
//[cf]
//[c]
//[of]:gtk notebook
private equ gtk notebook (m: notebook) =
	
	widget (m) : GtkNotebook
//[cf]
//[of]:switched page (GtkNotebook, index, notebook)
//[c]Standard notify procedure
//[c]
private func switched page (
		w: GtkNotebook, 
		p: GtkNotebookPage,
		index: guint, 
		m: notebook)

	// Hack - transfer focus to the page (avoid having focus on the
	// tab). It should not be performed if the focus is not in a descendant
	// of the notebook.
	set focus (page (m))

	on selection changed (m)

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

	def c = the notebook class
	if ~ initialized
		initialized = true
		c . copy (box class)
		c . release = ^actual release (notebook)
		c . activate = ^actual activate (notebook)
		c . on focus = ^handle focus (notebook, event)
		c . accept focus = ^yes (box)
		c . on child event = ^handle child event (notebook, box, event)
	end
	return c

end

private def initialized = false
private def the notebook class: local notebook class
//[cf]
//[cf]

⌨️ 快捷键说明

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