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

📄 multi-view.zc

📁 实现树形结构
💻 ZC
字号:
//[of]:description
//[c]A Multiview is a notebook that handle cycling
//[c]of most recents tabs using CTRL+TAB
//[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"
import "text/vector-of-strings"
//[c]
import "user/box"
import "user/notebook"
import "user/keyboard"
//[cf]
//[c]
//[of]:multi view
//[of]:type
public struct multi view: local notebook

	private index: int
	
	// The list of pages ordered by 'recently viewed'
	// - the first is the current one
	// - the next is the one that was display just before
	// - ...
	private recent pages: local vector
	
	// this flag is set to ignore the selection changed emitted by 
	// the notebook (>0 <=> ignore)
	private ignore selection changed: int

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

	equ s = sizeof local multi view
	def b = allocate memory (s): multi view
	initialize (b, parent)
	return b

end
//[cf]
//[cf]
//[of]:current page
//[of]:current page
//[c]Returns the current notebook page
//[c]
public func current page (m: multi view)
	return page (m)
end
//[cf]
//[c]
//[of]:next
//[c]Cycles to next window
//[c]
public func next (m: multi view)

	def index = index (m) 
	++ index
	if index == size (recent pages (m)) : int
		index = 0
	end
	set index (m, index, false)

end
//[cf]
//[of]:previous
//[c]Cycles to previous window
//[c]
public func previous (m: multi view)

	def index = index (m)
	-- index
	if index == -1
		index = size (recent pages (m)) : int - 1
	end
	set index (m, index, false)

end
//[cf]
//[of]:set index (index, bring to top)
//[c]Changes the page
//[c]
public func set index (m: multi view, index: int, bring to top: bool)

	if index (m) == index
		return
	end

	ignore selection changed (m) ++
	set page (super (m), page (m, index))
	ignore selection changed (m) --
	
	index (m) = index

	if bring to top
		bring current to top (m)
	end

end
//[cf]
//[of]:set current page (page, bring to top)
//[c]Changes the current page
//[c]
public func set current page (m: multi view, page: box, bring to top: bool)

	set index (m, index (m, page), bring to top)

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

	add (recent pages (m), page)
	add (super (m), page, label)

end
//[cf]
//[of]:remove (page)
//[c]Removes a page
//[c]
public func remove (m: multi view, page: box)

	ignore selection changed (m) += 1

	def index = index (m, page)

	// remove the page from the list
	remove (recent pages (m), index)

	// Fix the current index
	if index (m) > index
	
		// If the page being destroyed has an index lesser
		// than the current index, the current index must
		// be decrease accoring to the shift.
		-- index (m)
	
	elsif index (m) == index
	
		// The current page is being destroyed
		
		// No more current page: the index is cleared
		index (m) = -1

		// Go to previous page
		if index >= size (recent pages (m)) : int
			index = 0
		end

		// Set the current page: the first 
		// (it is the previous visible)
		set index (m, index, true)
	end

	remove (super (m), page)

	ignore selection changed (m) -= 1
	

end
//[cf]
//[of]:bring current to top
//[c]Brings the current window to the top of the cycling list
//[c]
public func bring current to top (m: multi view)

	def index = index (m)
	if index > 0
		def page = recent pages (m) [index]
		remove (recent pages (m), index)
		index (m) = 0
		add first (recent pages (m), page)
	end

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

	each (recent pages (m)) ? v
		equ view = v : box
		yield (view)
	end

end
//[cf]
//[of]:number of views
//[c]Returns the number of views
//[c]
public func number of views (m: multi view)
	return size (recent pages (m))
end
//[cf]
//[cf]
//[c]
//[of]:restricted
//[of]:initialize (parent)
//[c]
public func initialize (m: multi view, parent: box)

	initialize (super (m), parent)
	class (m) = multi view class
	index (m) = -1
	ignore selection changed (m) = 0
	initialize (recent pages (m), 8)

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

	release (recent pages (m))
	actual release (super (m))

end
//[cf]
//[c]
//[of]:handle child event (child, event)
//[c]Handles the list events
//[c]
public func handle child event (m: multi view, child: box, evt: event)

	def type = type (evt)

	if type == key down event type
		equ e = evt: key event
		if key (e) == VK TAB && status (e) == CTRL && index (m) <> -1
			next (m)
			return true
		end

	elsif type == key up event type
		equ e = evt: key event
		if key (e) == VK CONTROL
			def index = index (m) 
			if index > 0
				def page = recent pages (m) [index]
				remove (recent pages (m), index)
				index (m) = 0
				add first (recent pages (m), page)
			end
		end
	
	end
	
	return handle child event (super (m), child, evt)

end
//[cf]
//[of]:handle key up (key event)
//[c]Handles the list events
//[c]
public func handle key up (m: multi view, e: key event)

	if key (e) == VK CONTROL
		def index = index (m) 
		if index > 0
			def page = recent pages (m) [index]
			remove (recent pages (m), index)
			index (m) = 0
			add first (recent pages (m), page)
		end

	end

	return handle key up (super (m), e)

end
//[cf]
//[of]:handle selection changed
//[c]
public func handle selection changed (m: multi view, e: event)

	if ignore selection changed (m) == 0
		index (m) = index (m, current page (m))
		bring current to top (m)
	end

	return handle selection changed (super (m), e)

end
//[cf]
//[cf]
//[of]:private
//[of]:class
private equ class (m: multi view) =

	class (super (m)) : multi view class
//[cf]
//[of]:page (index)
//[c]Returns the page at given index
//[c]
private func page (m: multi view, index: int)

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

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

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

end
//[cf]
//[cf]
//[cf]
//[of]:multi view class
//[of]:type
public struct multi view class : local notebook class
end
//[cf]
//[of]:multiview class
public func multi view class

	def c = the multi view class
	if ~ initialized
		initialized = true
		c . copy (notebook class)
		c . mem size = sizeof local multi view class
		c . release = ^actual release (multi view)
		c . on child event = ^handle child event (multi view, box, event)
		c . on selection changed = ^handle selection changed (multi view, event)
	end
	
	return c

end

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

⌨️ 快捷键说明

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