📄 notebook.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 "win32/windows"
import "win32/commctrl"
//[cf]
//[c]
//[of]:notebook
//[of]:type
public struct notebook : local box
private
pages : local vector
labels : local array of strings
a child has focus : bool
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)
invalidate min size (m)
add (labels (m), label)
add (pages (m), page)
def index = size (pages (m))
def item: TCITEMA
mask (item) = TCIF_TEXT
pszText (item) = label
iImage (item) = -1
send message (m, TCM_INSERTITEMA, index : WPARAM, item : LPARAM)
end
//[cf]
//[of]:remove (page)
//[c]Removes a page
//[c]
public func remove (m: notebook, page: box)
delete (page)
def index = index (m, page)
// remove the page from the list
remove (pages (m), index)
// remove the tab
send message (m, TCM_DELETEITEM, index : WPARAM)
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)
//[c]Changes the label of a page
//[c]
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)
if not nil (hwnd (m))
def item: TCITEMA
mask (item) = TCIF_TEXT
pszText (item) = label
iImage (item) = -1
send message (m, TCM_SETITEMA, index : WPARAM, item : LPARAM)
end
end
//[cf]
//[cf]
//[of]:current page
//[of]:page
//[c]Returns 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]:index
//[c]Returns the index of the current page
//[c]
private func index (m: notebook)
if size (pages (m)) == 0
return -1
end
return send message (m, TCM_GETCURSEL) : int
end
//[cf]
//[of]:set page (page)
//[c]Changes the current page
//[c]
public func set page (m: notebook, page: box)
def current page = page (m)
if current page == page
return
end
def index = index (m, page)
if index == -1
return
end
update page (m, page)
send message (m, TCM_SETCURSEL, index: WPARAM)
on selection changed (m)
end
//[cf]
//[of]:set index (index)
//[c]Changes the current page
//[c]
public func set index (m: notebook, index: int)
set page (m, page (m, index))
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)
set style (m, get style (m):int)
end
//[cf]
//[cf]
//[c]
//[of]:restricted
//[of]:initialize (parent)
//[c]
public func initialize (m: notebook, parent: box)
initialize (super (m), parent)
class (m) = notebook class
a child has focus (m) = false
tab bottom (m) = false
initialize (labels (m), 8)
initialize (pages (m), 8)
create managed window ex (
m,
get ex style (m),
WC_TABCONTROLA,
nil,
get style (m),
m : HMENU,
nil)
set label font (m)
end
//[cf]
//[c]
//[of]:actual release
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]
//[of]:actual adjust
public func actual adjust (m: notebook)
compute layout (m)
end
//[cf]
//[of]:actual compute min size
public func actual compute min size (m: notebook)
def min width = 0
def min height = 0
each child (m) ? page
min width = max (min width , min width (page))
min height = max (min height, min height (page))
end
def rect: local LPRECT
left (rect) = 0
right (rect) = min width
top (rect) = 0
bottom (rect) = min height
send message (m, TCM_ADJUSTRECT, TRUE: WPARAM, rect: LPARAM)
def outer: local rectangle
rectangle from RECT (outer, rect)
set min width (m, w (outer))
set min height (m, h (outer))
end
//[cf]
//[c]
//[of]:handle size
public func handle size (m: notebook)
compute layout (m)
end
//[cf]
//[of]:handle focus (event)
//[c]The notebook receives the focus: transfer the focus to the current page
//[c]
public func handle focus (m: notebook, e: event)
set focus (page (m))
return true
end
//[cf]
//[of]:handle child event (child, event)
public func handle child event (m: notebook, child: box, e: event)
def t = type (e)
if t == focus event type
a child has focus (m) = true
elsif t == focus event type
a child has focus (m) = false
end
return handle child event (super (m), child, 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 or nil if index is -1
//[c]
//[c]The index must not exceed the number of pages.
//[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]:get ex style
//[c]
private func get ex style (m: notebook)
return 0:d
end
//[cf]
//[of]:get style
func get style (m: notebook)
def s = base style (m) | WS_CLIPCHILDREN
if tab bottom (m)
s |= TCS_BOTTOM
end
return s
end
//[cf]
//[of]:update page (page)
//[c]
private func update page (m: notebook, page: box)
def has focus = a child has focus (m)
if not nil (page)
show (page)
if has focus
set focus (page)
end
end
each child (m) ? child
if child <> page
hide (child)
end
end
end
//[cf]
//[of]:notify proc (pnmh)
//[c]Standard notify procedure
//[c]
public func notify proc (m: notebook, pnmh: LPNMHDR)
if code (pnmh) == TCN_SELCHANGE
update page (m, page (m))
on selection changed (m)
end
return 0
end
//[cf]
//[of]:compute layout
//[c]
private func compute layout (m: notebook)
def r = client rect (m)
def rect: local LPRECT
left (rect) = x (r)
right (rect) = w (r) - x (r)
top (rect) = y (r)
bottom (rect) = h (r) - y (r)
send message (m, TCM_ADJUSTRECT, FALSE : WPARAM, rect : LPARAM)
def inner : local rectangle
rectangle from RECT (inner, rect)
each child (m) ? child
move (child, inner)
end
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 . adjust = ^actual adjust (notebook)
c . compute min size = ^actual compute min size (notebook)
c . accept focus = ^yes (box)
c . on size = ^handle size (notebook)
c . on focus = ^handle focus (notebook, event)
c . on child event = ^handle child event (notebook, box, event)
c . wm size = ^wm size (box, UINT, WPARAM, LPARAM)
c . wm command = ^wm command (box, UINT, WPARAM, LPARAM)
c . wm notify = ^wm notify (box, UINT, WPARAM, LPARAM)
c . notify = ^notify proc (notebook, LPNMHDR)
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 + -