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

📄 edit-box.zc

📁 实现树形结构
💻 ZC
字号:
//[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/string-buffer"
//[c]
import "graphics/geometry"
import "graphics/graphics"
import "user/box"
import "toolbox/clipboard"
//[c]
import "win32/windows"
//[cf]
//[c]
//[of]:edit box
//[of]:type
public struct edit box : local box

	private 
		starting position: DWORD
		ending position: DWORD
		content: string

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

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

end
//[cf]
//[cf]
//[of]:text
//[of]:text
//[c]Returns the content of the edit box
//[c]
public func text (m: edit box)

	// update the member attribute from the control
	def stream := temp string buffer
	def size = GetWindowTextLengthA (hwnd (m))
	def p = buffer (stream, size)
	GetWindowTextA (hwnd (m), p, size+1)
	set content (m, as string (stream))
	release (stream)
		
	return content (m)

end
//[cf]
//[of]:set text (text)
//[c]Changes the content of the edit box
//[c]
public func set text (m: edit box, text: string)

	set content (m, text)
	set window text (m, text)
	reset selection save (m)
	on value changed (m)

end
//[cf]
//[cf]
//[of]:selection
//[of]:select (starting pos, ending pos)
//[c]Selects a part of the text
//[c]
public func select (m: edit box, starting pos: int, ending pos: int)
	send message (m, EM_SETSEL, starting pos, ending pos)
end
//[cf]
//[of]:is selection empty
//[c]Tests if the current selection is empty
//[c]
public func is selection empty (m: edit box)
	def l = send message (m, EM_GETSEL, 0, 0): DWORD
	return HIWORD (l) == LOWORD (l)
end
//[cf]
//[cf]
//[c]
//[of]:restricted
//[of]:initialize (parent)
public func initialize (m: edit box, parent: box)

	initialize (super (m), parent)
	class (m) = edit box class
	starting position (m) = 0
	ending position (m) = 0
	content (m) = empty string

	create managed window ex (
		m,
		get ex style (m),
		"edit",
		nil,
		get style (m),
		m:HMENU,
		nil)

	set edit font (m)

end
//[cf]
//[c]
//[of]:actual cut
public func actual cut (m: edit box)

	default window proc (m, WM_CUT, 0, 0)
	return true

end
//[cf]
//[of]:actual copy
public func actual copy (m: edit box)
	return default window proc (m, WM_COPY, 0, 0) > 0
end
//[cf]
//[of]:actual paste
public func actual paste (m: edit box)
	default window proc (m, WM_PASTE, 0, 0)
	return true
end
//[cf]
//[of]:actual clear
public func actual clear (m: edit box)
	default window proc (m, WM_CLEAR, 0, 0)
	return true
end
//[cf]
//[of]:actual select all
public func actual select all (m: edit box)
	select (m, 0, -1)
	return true
end
//[cf]
//[of]:actual can copy
public func actual can copy (m: edit box)
	return ~ is selection empty (m)
end
//[cf]
//[of]:actual can paste
public func actual can paste (m: edit box)

	if is read only (m)
		return false
	end
	
	def can paste = false
	each clipboard format ? fmt
		if fmt == CF_TEXT
			can paste = true
			break
		end
	end
	return can paste

end
//[cf]
//[of]:actual can clear
func actual can clear (m: edit box)
	return ~ is read only (m) && ~ is selection empty (m)
end
//[cf]
//[of]:actual release
public func actual release (m: edit box)
	delete (content (m))
	actual release (super (m))
end
//[cf]
//[of]:actual compute min size
public func actual compute min size (m: edit box)

	def a: local area
	def canvas: local canvas
	initialize (canvas)
	set font (canvas, edit font (style (m)))
	text extent (canvas, "X", 1, a)
	release (canvas)

	def rect : local LPRECT
	left (rect) = 0
	top (rect) = 0
	right (rect) = w (a)
	bottom (rect) = h (a)
	AdjustWindowRectEx (rect, get style (m), 0, get ex style (m))
	
	def w = 120
	def h = bottom (rect) - top (rect)
	if edit border (style (m)) <> border none
		h += 2
	end
	set min size(m, w, h)

end
//[cf]
//[of]:actual set read only (ro)
public func actual set read only (m: edit box, ro: bool)

	// exit if unchanged
	if read only (m) == ro
		return
	end
	
	send message (m, EM_SETREADONLY, ro:BOOL:WPARAM, 0)
	read only (m) = ro

end
//[cf]
//[c]
//[of]:handle focus
public func handle focus (m: edit box, e: event)

	select all (m)
	return handle focus (super (m), e)

end
//[cf]
//[cf]
//[of]:private
//[of]:wm key down
private func wm key down (m: edit box, msg: UINT, wParam: WPARAM, lParam: LPARAM)

	wm key down (super (m), msg, wParam, lParam)
	check selection changed (m)
	return 0

end
//[cf]
//[of]:wm char
private func wm char (m: edit box, msg: UINT, wParam: WPARAM, lParam: LPARAM)

	wm char (super (m), msg, wParam, lParam)
	check selection changed (m)
	return 0

end
//[cf]
//[of]:actual win command (code)
private func actual win command (m: edit box, notify code: WORD)

	if notify code == EN_CHANGE:WORD
		on value changed (m)
	end

end
//[cf]
//[c]
//[of]:set content (text)
private func set content (m: edit box, text: string)

	delete (content (m))
	content (m) = new string (text)

end
//[cf]
//[of]:reset selection save
//[c]Resets the selection save
//[c]
private func reset selection save (m: edit box)

	def starting position: [1] DWORD	
	def ending position: [1] DWORD
	
	send message (
		m, 
		EM_GETSEL, 
		starting position : WPARAM, 
		ending position : LPARAM)

	starting position (m) = starting position []
	ending position (m) = ending position []

end
//[cf]
//[of]:check selection changed
//[c]Checks if the selection has changed.
//[c]Compare the current selection to the last saved.
//[c]
private func check selection changed (m: edit box)

	def starting position: [1] DWORD	
	def ending position: [1] DWORD
	
	send message (
		m, 
		EM_GETSEL, 
		starting position: WPARAM, 
		ending position: LPARAM)
		
	def changed = 
		starting position [] <> starting position (m) ||
		ending position [] <> ending position (m)
		
	// reset before sending event
	starting position (m) = starting position []
	ending position (m) = ending position []

	if changed
		on selection changed (m)
	end

end
//[cf]
//[of]:get style
private func get style (m: edit box)

	def style = base style (m) | ES_AUTOHSCROLL
	
	if is read only (m)
		style |= ES_READONLY
	end
	
	if edit border (style (m)) == border single
		style |= WS_BORDER
	end
	
	return style

end
//[cf]
//[of]:get ex style
private func get ex style (m: edit box)

	if edit border (style (m)) == border sunken
		return WS_EX_CLIENTEDGE
	else
		return 0:d
	end
	
end
//[cf]
//[cf]
//[cf]
//[of]:edit box class
//[of]:type
public struct edit box class : local box class
	// empty
end
//[cf]
//[of]:edit box class
public func edit box class

	def c = the edit box class
	if ~ initialized
		initialized = true
		c . copy (box class)
		c . release = ^actual release (edit box)
		c . compute min size = ^actual compute min size (edit box)
		c . on focus = ^handle focus (edit box, event)
		c . cut = ^actual cut (edit box)
		c . copy = ^actual copy (edit box)
		c . paste = ^actual paste (edit box)
		c . clear = ^actual clear (edit box)
		c . select all = ^actual select all (edit box)
		c . can copy = ^actual can copy (edit box)
		c . can paste = ^actual can paste (edit box)
		c . can clear = ^actual can clear (edit box)
		c . can select all = ^yes (box)
		c . set read only = ^actual set read only (edit box, bool)
		c . accept focus = ^yes (box)
		c . wm size = ^wm size (box, UINT, WPARAM, LPARAM)
		c . wm key down = ^wm key down (edit box, UINT, WPARAM, LPARAM)
		c . wm char = ^wm char (edit box, UINT, WPARAM, LPARAM)
		c . command = ^actual win command (edit box, WORD)
	end
	return c

end

def initialized = false
def the edit box class: local edit box class
//[cf]
//[cf]

⌨️ 快捷键说明

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