2 # $Id: notebook.tcl,v 1.4 2007/02/24 09:15:07 das Exp $
4 # Bindings for TNotebook widget
7 namespace eval ttk
::notebook {
8 variable TLNotebooks
;# See enableTraversal
11 bind TNotebook
<ButtonPress-1
> { ttk
::notebook::Press %W
%x
%y
}
12 bind TNotebook
<Key-Right
> { ttk
::notebook::CycleTab %W
1; break }
13 bind TNotebook
<Key-Left
> { ttk
::notebook::CycleTab %W
-1; break }
14 bind TNotebook
<Control-Key-Tab
> { ttk
::notebook::CycleTab %W
1; break }
15 bind TNotebook
<Control-Shift-Key-Tab
> { ttk
::notebook::CycleTab %W
-1; break }
17 bind TNotebook
<Control-ISO_Left_Tab
> { ttk
::notebook::CycleTab %W
-1; break }
19 bind TNotebook
<Destroy
> { ttk
::notebook::Cleanup %W
}
21 # ActivateTab $nb $tab --
22 # Select the specified tab and set focus.
24 # If $tab was already the current tab, set the focus to the
25 # notebook widget. Otherwise, set the focus to the first
26 # traversable widget in the pane. The behavior is that the
27 # notebook takes focus when the user selects the same tab
28 # a second time. This mirrors Windows tab behavior.
30 proc ttk
::notebook::ActivateTab {w tab
} {
31 if {[$w index
$tab] eq
[$w index current
]} {
35 update ;# needed so focus logic sees correct mapped/unmapped states
36 if {[set f
[ttk
::focusFirst [$w select
]]] ne
""} {
43 # ButtonPress-1 binding for notebook widgets.
44 # Activate the tab under the mouse cursor, if any.
46 proc ttk
::notebook::Press {w x y
} {
47 set index
[$w index
@$x,$y]
54 # Select the next/previous tab in the list.
56 proc ttk
::notebook::CycleTab {w dir
} {
57 if {[$w index end
] != 0} {
58 set current
[$w index current
]
59 set select
[expr {($current + $dir) % [$w index end
]}]
60 while {[$w tab
$select -state] != "normal" && ($select != $current)} {
61 set select
[expr {($select + $dir) % [$w index end
]}]
63 if {$select != $current} {
64 ActivateTab
$w $select
69 # MnemonicTab $nb $key --
70 # Scan all tabs in the specified notebook for one with the
71 # specified mnemonic. If found, returns path name of tab;
72 # otherwise returns ""
74 proc ttk
::notebook::MnemonicTab {nb key
} {
75 set key
[string toupper
$key]
76 foreach tab
[$nb tabs
] {
77 set label [$nb tab
$tab -text]
78 set underline
[$nb tab
$tab -underline]
79 set mnemonic
[string toupper
[string index
$label $underline]]
80 if {$mnemonic ne
"" && $mnemonic eq
$key} {
87 # +++ Toplevel keyboard traversal.
91 # Enable keyboard traversal for a notebook widget
92 # by adding bindings to the containing toplevel window.
94 # TLNotebooks($top) keeps track of the list of all traversal-enabled
95 # notebooks contained in the toplevel
97 proc ttk
::notebook::enableTraversal {nb
} {
100 set top
[winfo toplevel $nb]
102 if {![info exists TLNotebooks
($top)]} {
103 # Augment $top bindings:
105 bind $top <Control-Key-Tab
> {+ttk
::notebook::TLCycleTab %W
1}
106 bind $top <Shift-Control-Key-Tab
> {+ttk
::notebook::TLCycleTab %W
-1}
108 bind $top <Control-Key-ISO_Left_Tab
> {+ttk
::notebook::TLCycleTab %W
-1}
110 if {[tk windowingsystem
] eq
"aqua"} {
111 bind $top <Option-KeyPress
> \
112 +[list ttk
::notebook::MnemonicActivation $top %K
]
114 bind $top <Alt-KeyPress
> \
115 +[list ttk
::notebook::MnemonicActivation $top %K
]
117 bind $top <Destroy
> {+ttk
::notebook::TLCleanup %W
}
120 lappend TLNotebooks
($top) $nb
123 # TLCleanup -- <Destroy> binding for traversal-enabled toplevels
125 proc ttk
::notebook::TLCleanup {w
} {
127 if {$w eq
[winfo toplevel $w]} {
128 unset -nocomplain -please TLNotebooks
($w)
132 # Cleanup -- <Destroy> binding for notebooks
134 proc ttk
::notebook::Cleanup {nb
} {
136 set top
[winfo toplevel $nb]
137 if {[info exists TLNotebooks
($top)]} {
138 set index
[lsearch -exact $TLNotebooks($top) $nb]
139 set TLNotebooks
($top) [lreplace $TLNotebooks($top) $index $index]
143 # EnclosingNotebook $w --
144 # Return the nearest traversal-enabled notebook widget
147 # BUGS: this only works properly for tabs that are direct children
148 # of the notebook widget. This routine should follow the
149 # geometry manager hierarchy, not window ancestry, but that
150 # information is not available in Tk.
152 proc ttk
::notebook::EnclosingNotebook {w
} {
155 set top
[winfo toplevel $w]
156 if {![info exists TLNotebooks
($top)]} { return }
158 while {$w ne
$top && $w ne
""} {
159 if {[lsearch -exact $TLNotebooks($top) $w] >= 0} {
162 set w
[winfo parent
$w]
168 # toplevel binding procedure for Control-Tab / Shift-Control-Tab
169 # Select the next/previous tab in the nearest ancestor notebook.
171 proc ttk
::notebook::TLCycleTab {w dir
} {
172 set nb
[EnclosingNotebook
$w]
179 # MnemonicActivation $nb $key --
180 # Alt-KeyPress binding procedure for mnemonic activation.
181 # Scan all notebooks in specified toplevel for a tab with the
182 # the specified mnemonic. If found, activate it and return TCL_BREAK.
184 proc ttk
::notebook::MnemonicActivation {top key
} {
186 foreach nb
$TLNotebooks($top) {
187 if {[set tab
[MnemonicTab
$nb $key]] ne
""} {
188 ActivateTab
$nb [$nb index
$tab]