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

📄 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"
import "text/string"
//[c]
import "graphics/geometry"
import "graphics/graphics"
import "user/box"
//[c]
import "win32/windows"
//[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 
		type: scrollbar type
		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)
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, type)
//[c]
public func initialize (m: scrollbar, parent: box, type: scrollbar type)

	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
	type(m) = type
	
	create managed window ex (
		m,
		0,
		scrollbar class name,
		nil,
		get style (m),
		m : HMENU,
		nil)

end
//[cf]
//[c]
//[of]:actual compute min size
//[c]
func actual compute min size (m: scrollbar)

	def a: local area
	
	if type (m) == sb vertical
		def w = GetSystemMetrics (SM_CXVSCROLL)
		w (a) = w
		h (a) = 10
	else
		def h = GetSystemMetrics (SM_CYHSCROLL)
		w (a) = 10
		h (a) = h
	end
	
	set (min size (m), a)

end
//[cf]
//[cf]
//[of]:private
//[of]:constants
def scrollbar class name := "scrollbar"
//[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
	notify parent (m, e)

end
//[cf]
//[of]:scroll (code, pos)
private func scroll (m: scrollbar, code: int, final pos: int)

	def pos = pos (m)
	switch code
	case SB_THUMBTRACK
		def si: SCROLLINFO
		cbSize (si) = sizeof SCROLLINFO
		fMask (si) = SIF_TRACKPOS
		GetScrollInfo (hwnd (m), SB_CTL, si)
		pos = nTrackPos (si)
	case SB_LINEUP
		pos = max (min (m), pos - 1)
	case SB_LINEDOWN
		pos = min (max (m), pos + 1)
	case SB_PAGEUP
		pos = max (min (m), pos - page (m))
	case SB_PAGEDOWN
		pos = min (max (m) - page (m) + 1, pos + page (m))
	end
	
	if pos (m) <> pos

		pos (m) = pos
		
		def info: SCROLLINFO
		cbSize (info) = sizeof SCROLLINFO
		fMask (info) = SIF_POS
		nPos (info) = pos
		SetScrollInfo (hwnd (m), SB_CTL, info, TRUE)
		
		on scroll (m, pos)

	end
	
	return true
	
end
//[cf]
//[of]:get style
private func get style (m: scrollbar)

	return base style (m) | ((type (m) == sb vertical) -> SBS_VERT, SBS_HORZ)

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

	def hwnd = hwnd (m)
	if not nil (hwnd)

		def info: SCROLLINFO
		cbSize (info) = sizeof SCROLLINFO

		if page (m) >= max (m) - min (m) + 1
			disable (m)
		else
			enable (m)

			def mask = mask (m)
			if mask <> 0
				fMask (info) = mask
				nMin (info) = min (m)
				nMax (info) = max (m)
				nPage (info) = page (m)
				nPos (info) = pos (m)
				SetScrollInfo (hwnd, SB_CTL, info, TRUE)
				mask (m) = 0
			end
		end

	end

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

	def c = the scrollbar class
	if ~ initialized
		initialized = true
		c . copy (box class)
		c . compute min size = ^actual compute min size (scrollbar)
		c . accept focus = ^no (box)
		c . wm size = ^wm size (box, UINT, WPARAM, LPARAM)
		c . hscroll = ^scroll (scrollbar, int, int)
		c . vscroll = ^scroll (scrollbar, int, int)
	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 + -