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

📄 scrollbar.zc

📁 实现树形结构
💻 ZC
字号:
//[of]:description
//[c]The scrollbar component
//[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/types"
import "base/memory-allocator"
//[c]
import "graphics/geometry"
import "user/box"
//[c]
import "glib/glib"
import "gtk/gtk"
import "gtk/gtkrange"
import "gtk/gtkscrollbar"
import "gtk/gtkvscrollbar"
import "gtk/gtkhscrollbar"
//[cf]
//[of]:constants
public enum scrollbar type
	sb horizontal
	sb vertical
end
//[cf]
//[c]
//[of]:scroll event
public struct scroll event: local event
	pos: int
end
//[c]
public def scroll event type : local event type
//[cf]
//[of]:scrollbar
//[of]:type
public struct scrollbar : local box

	private 
		mask: dword
		min: int
		max: int
		page: int
		pos: int

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

	equ s = sizeof local scrollbar
	def m = allocate memory (s): scrollbar
	initialize (m, parent, type)
	return m

end
//[cf]
//[cf]
//[of]:accessing
//[of]:set scroll info (min, max, page, pos)
//[c]
public func set scroll info (
		m: scrollbar,
		min: int,
		max: int,
		page: int,
		pos: int)

	def mask = mask (m)

	if min (m) <> min
		min (m) = min
		mask |= sif range
	end

	if max (m) <> max
		max (m) = max
		mask |= sif range
	end
		
	if page (m) <> page
		page (m) = page
		mask |= sif page
	end
	
	if pos (m) <> pos
		pos (m) = pos
		mask |= sif pos
	end

	mask (m) = mask

	update scroll (m)
		
end
//[cf]
//[cf]
//[c]
//[of]:restricted
//[of]:initialize (parent)
public func initialize (m: scrollbar, parent: box, type: scrollbar type)

	def p : GtkScrollbar
	if type == sb vertical
		p = gtk_vscrollbar_new (nil) : GtkScrollbar
	else
		p = gtk_hscrollbar_new (nil) : GtkScrollbar
	end
	gtk_range_set_update_policy (p, GTK_UPDATE_CONTINUOUS)
	widget (m) = p
	gtk_widget_show (p)

	connect (m, 
		"value-changed", 
		^value changed (GtkScrollbar, scrollbar))

	initialize (super (m), parent)
	class (m) = scrollbar class

	min (m) = 0
	max (m) = 1
	page (m) = 1
	pos (m) = 0
	mask (m) = sif range | sif page | sif pos
	update scroll (m)

end
//[cf]
//[c]
//[cf]
//[of]:private
//[of]:constants
//[c]
//[c]Flags to determine what to refresh
equ sif range = 1
equ sif page = 2
equ sif pos = 4
//[cf]
//[of]:on scroll (pos)
//[c]Emits the scroll event
//[c]
private func on scroll (m: scrollbar, pos: int)

	def e: local scroll event
	type (e) = scroll event type
	pos (e) = pos
	//on scroll (class (m)) {m, e}
	notify parent (m, e)

end
//[cf]
//[of]:update scroll
func update scroll (m: scrollbar)

	if page (m) >= max (m) - min (m) + 1
		disable (m)
		def adjust = gtk_adjustment_new_int (0, 0, 1, 1, 1, 1): GtkAdjustment
		gtk_range_set_adjustment (scrollbar (m), adjust)
	else
		enable (m)

		def mask = mask (m)
		if mask == 0
			return
		end

		def pos = pos (m)
		pos = min (pos, max (m) - page (m) + 1)

		// optim: avoid to change adjustment if only pos has changed
		if mask == sif pos
			gtk_range_set_value_int (scrollbar (m), pos)
		elsif mask <> 0
			def adjust = gtk_adjustment_new_int (
				pos,
				min (m),
				max (m) + 1,
				1,
				page (m),
				page (m)) : GtkAdjustment
			gtk_range_set_adjustment (scrollbar (m), adjust)
		end
		mask (m) = 0
	end

end
//[cf]
//[c]
//[of]:value changed (gtk scrollbar, scrollbar)
//[c]
private func value changed (w: GtkScrollbar, m: scrollbar)

	def pos = gtk_range_get_value_int (scrollbar (m))

	if pos (m) <> pos

		pos (m) = pos
		on scroll (m, pos)

	end

end
//[cf]
//[of]:scrollbar
private equ scrollbar (m: scrollbar) = 

	widget (m) : GtkScrollbar
//[cf]
//[of]:class
private equ class (m: scrollbar) = 

	class (super (m)) : scrollbar class
//[cf]
//[cf]
//[cf]
//[of]:scrollbar class
//[of]:type
public struct scrollbar class : local box class
	// empty
end
//[c]
//[cf]
//[of]:scrollbar class
public func scrollbar class

	def c = the scrollbar class
	if ~ initialized
		initialized = true
		c . copy (box class)
		c . accept focus = ^no (box)
	end
	return c

end

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

⌨️ 快捷键说明

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