1# RCS: @(#) $Id: help.tcl,v 1.22 2006/12/04 00:16:02 treectrl Exp $
2
3#
4# Demo: Help contents
5#
6proc DemoHelpContents {} {
7
8    global HelpContents
9
10    set T [DemoList]
11
12    set height [font metrics [$T cget -font] -linespace]
13    if {$height < 18} {
14	set height 18
15    }
16
17    #
18    # Configure the treectrl widget
19    #
20
21    $T configure -showroot no -showbuttons no -showlines no -itemheight $height \
22	-selectmode browse
23
24    InitPics help-*
25
26    #
27    # Create columns
28    #
29
30    $T column create -text "Help Contents" -tags C0
31
32    $T configure -treecolumn C0
33
34    #
35    # Create elements
36    #
37
38    # Define a new item state
39    $T state define mouseover
40
41    $T element create elemImgPage image -image help-page
42    $T element create elemImgBook image -image {help-book-open {open} help-book-closed {}}
43    $T element create elemTxt text -fill [list $::SystemHighlightText {selected focus} blue {mouseover}] \
44	-font [list DemoFontUnderline {mouseover}]
45    $T element create elemRectSel rect -fill [list $::SystemHighlight {selected focus}] -showfocus yes
46
47    #
48    # Create styles using the elements
49    #
50
51    # book
52    set S [$T style create styBook]
53    $T style elements $S {elemRectSel elemImgBook elemTxt}
54    $T style layout $S elemImgBook -padx {0 4} -expand ns
55    $T style layout $S elemTxt -expand ns
56    $T style layout $S elemRectSel -union [list elemTxt] -iexpand ns -ipadx 2
57
58    # page
59    set S [$T style create styPage]
60    $T style elements $S {elemRectSel elemImgPage elemTxt}
61    $T style layout $S elemImgPage -padx {0 4} -expand ns
62    $T style layout $S elemTxt -expand ns
63    $T style layout $S elemRectSel -union [list elemTxt] -iexpand ns -ipadx 2
64
65    #
66    # Create items and assign styles
67    #
68
69    set parentList [list root {} {} {} {} {} {}]
70    set parent root
71    foreach {depth style text} {
72	0 styPage "Welcome to Help"
73	0 styBook "Introducing Windows 98"
74	    1 styBook "How to Use Help"
75		2 styPage "Find a topic"
76		2 styPage "Get more out of help"
77	    1 styBook "Register Your Software"
78		2 styPage "Registering Windows 98 online"
79	    1 styBook "What's New in Windows 98"
80		2 styPage "Innovative, easy-to-use features"
81		2 styPage "Improved reliability"
82		2 styPage "A faster operating system"
83		2 styPage "True Web integration"
84		2 styPage "More entertaining and fun"
85	    1 styBook "If You're New to Windows 98"
86		2 styBook "Tips for Macintosh Users"
87		    3 styPage "Why does the mouse have two buttons?"
88    } {
89	set item [$T item create -open no]
90	$T item style set $item C0 $style
91	$T item element configure $item C0 elemTxt -text $text
92	$T item lastchild [lindex $parentList $depth] $item
93	incr depth
94	set parentList [lreplace $parentList $depth $depth $item]
95    }
96
97    bind DemoHelpContents <Double-ButtonPress-1> {
98	if {[lindex [%W identify %x %y] 0] eq "header"} {
99	    TreeCtrl::DoubleButton1 %W %x %y
100	} else {
101	    HelpButton1 %W %x %y
102	}
103	break
104    }
105    bind DemoHelpContents <ButtonPress-1> {
106	HelpButton1 %W %x %y
107	break
108    }
109    bind DemoHelpContents <Button1-Motion> {
110	# noop
111    }
112    bind DemoHelpContents <Button1-Leave> {
113	# noop
114    }
115    bind DemoHelpContents <Motion> {
116	HelpMotion %W %x %y
117    }
118    bind DemoHelpContents <Leave> {
119	HelpMotion %W %x %y
120    }
121    bind DemoHelpContents <KeyPress-Return> {
122	if {[%W selection count] == 1} {
123	    %W item toggle [%W selection get 0]
124	}
125	break
126    }
127
128    set HelpContents(prev) ""
129    bindtags $T [list $T DemoHelpContents TreeCtrl [winfo toplevel $T] all]
130
131    return
132}
133
134# This is an alternate implementation that does not define a new item state
135# to change the appearance of the item under the cursor.
136proc DemoHelpContents_2 {} {
137
138    global HelpContents
139
140    set T [DemoList]
141
142    set height [font metrics [$T cget -font] -linespace]
143    if {$height < 18} {
144	set height 18
145    }
146
147    #
148    # Configure the treectrl widget
149    #
150
151    $T configure -showroot no -showbuttons no -showlines no -itemheight $height \
152	-selectmode browse
153
154    InitPics help-*
155
156    #
157    # Create columns
158    #
159
160    $T column create -text "Help Contents"
161
162    #
163    # Create elements
164    #
165
166    $T element create elemImgPage image -image help-page
167    $T element create elemImgBook image -image {help-book-open {open} help-book-closed {}}
168    $T element create elemTxt text -fill [list $::SystemHighlightText {selected focus}]
169    $T element create elemRectSel rect -fill [list $::SystemHighlight {selected focus}] -showfocus yes
170    $T element create elemTxtOver text -fill [list $::SystemHighlightText {selected focus} blue {}] \
171	-font "[$T cget -font] underline"
172
173    #
174    # Create styles using the elements
175    #
176
177    # book
178    set S [$T style create styBook]
179    $T style elements $S {elemRectSel elemImgBook elemTxt}
180    $T style layout $S elemImgBook -padx {0 4} -expand ns
181    $T style layout $S elemTxt -expand ns
182    $T style layout $S elemRectSel -union [list elemTxt] -iexpand ns -ipadx 2
183
184    # page
185    set S [$T style create styPage]
186    $T style elements $S {elemRectSel elemImgPage elemTxt}
187    $T style layout $S elemImgPage -padx {0 4} -expand ns
188    $T style layout $S elemTxt -expand ns
189    $T style layout $S elemRectSel -union [list elemTxt] -iexpand ns -ipadx 2
190
191    # book (focus)
192    set S [$T style create styBook.f]
193    $T style elements $S {elemRectSel elemImgBook elemTxtOver}
194    $T style layout $S elemImgBook -padx {0 4} -expand ns
195    $T style layout $S elemTxtOver -expand ns
196    $T style layout $S elemRectSel -union [list elemTxtOver] -iexpand ns -ipadx {1 2}
197
198    # page (focus)
199    set S [$T style create styPage.f]
200    $T style elements $S {elemRectSel elemImgPage elemTxtOver}
201    $T style layout $S elemImgPage -padx {0 4} -expand ns
202    $T style layout $S elemTxtOver -expand ns
203    $T style layout $S elemRectSel -union [list elemTxtOver] -iexpand ns -ipadx {1 2}
204
205    #
206    # Create items and assign styles
207    #
208
209    set parentList [list root {} {} {} {} {} {}]
210    set parent root
211    foreach {depth style text} {
212	0 styPage "Welcome to Help"
213	0 styBook "Introducing Windows 98"
214	    1 styBook "How to Use Help"
215		2 styPage "Find a topic"
216		2 styPage "Get more out of help"
217	    1 styBook "Register Your Software"
218		2 styPage "Registering Windows 98 online"
219	    1 styBook "What's New in Windows 98"
220		2 styPage "Innovative, easy-to-use features"
221		2 styPage "Improved reliability"
222		2 styPage "A faster operating system"
223		2 styPage "True Web integration"
224		2 styPage "More entertaining and fun"
225	    1 styBook "If You're New to Windows 98"
226		2 styBook "Tips for Macintosh Users"
227		    3 styPage "Why does the mouse have two buttons?"
228    } {
229	set item [$T item create -open no]
230	$T item style set $item 0 $style
231	$T item element configure $item 0 elemTxt -text $text
232	$T item lastchild [lindex $parentList $depth] $item
233	incr depth
234	set parentList [lreplace $parentList $depth $depth $item]
235    }
236
237    bind DemoHelpContents <Double-ButtonPress-1> {
238	if {[lindex [%W identify %x %y] 0] eq "header"} {
239	    TreeCtrl::DoubleButton1 %W %x %y
240	} else {
241	    HelpButton1 %W %x %y
242	}
243	break
244    }
245    bind DemoHelpContents <ButtonPress-1> {
246	HelpButton1 %W %x %y
247	break
248    }
249    bind DemoHelpContents <Button1-Motion> {
250	# noop
251    }
252    bind DemoHelpContents <Button1-Leave> {
253	# noop
254    }
255    bind DemoHelpContents <Motion> {
256	HelpMotion_2 %W %x %y
257    }
258    bind DemoHelpContents <Leave> {
259	HelpMotion_2 %W %x %y
260    }
261    bind DemoHelpContents <KeyPress-Return> {
262	if {[%W selection count] == 1} {
263	    %W item toggle [%W selection get 0]
264	}
265	break
266    }
267
268    set HelpContents(prev) ""
269    bindtags $T [list $T DemoHelpContents TreeCtrl [winfo toplevel $T] all]
270
271    return
272}
273
274proc HelpButton1 {w x y} {
275    variable TreeCtrl::Priv
276    focus $w
277    set id [$w identify $x $y]
278    set Priv(buttonMode) ""
279    if {[lindex $id 0] eq "header"} {
280	TreeCtrl::ButtonPress1 $w $x $y
281    } elseif {[lindex $id 0] eq "item"} {
282	set item [lindex $id 1]
283	# didn't click an element
284	if {[llength $id] != 6} return
285	if {[$w selection includes $item]} {
286	    $w item toggle $item
287	    return
288	}
289	if {[$w selection count]} {
290	    set item2 [$w selection get 0]
291	    $w item collapse $item2
292	    foreach item2 [$w item ancestors $item2] {
293		if {[$w item compare $item != $item2]} {
294		    $w item collapse $item2
295		}
296	    }
297	}
298	$w activate $item
299	$w item expand [list $item ancestors]
300	$w item toggle $item
301	$w selection modify $item all
302    }
303    return
304}
305
306proc HelpMotion {w x y} {
307    global HelpContents
308    set id [$w identify $x $y]
309    if {$id eq ""} {
310    } elseif {[lindex $id 0] eq "header"} {
311    } elseif {[lindex $id 0] eq "item"} {
312	set item [lindex $id 1]
313	if {[llength $id] == 6} {
314	    if {$item ne $HelpContents(prev)} {
315		if {$HelpContents(prev) ne ""} {
316		    $w item state set $HelpContents(prev) !mouseover
317		}
318		$w item state set $item mouseover
319		$w configure -cursor hand2
320		set HelpContents(prev) $item
321	    }
322	    return
323	}
324    }
325    if {$HelpContents(prev) ne ""} {
326	$w item state set $HelpContents(prev) !mouseover
327	$w configure -cursor ""
328	set HelpContents(prev) ""
329    }
330    return
331}
332
333# Alternate implementation that does not rely on run-time states
334proc HelpMotion_2 {w x y} {
335    variable TreeCtrl::Priv
336    global HelpContents
337    set id [$w identify $x $y]
338    if {[lindex $id 0] eq "header"} {
339    } elseif {$id ne ""} {
340	set item [lindex $id 1]
341	if {[llength $id] == 6} {
342	    if {$item ne $HelpContents(prev)} {
343		if {$HelpContents(prev) ne ""} {
344		    set style [$w item style set $HelpContents(prev) 0]
345		    set style [string trim $style .f]
346		    $w item style map $HelpContents(prev) 0 $style {elemTxtOver elemTxt}
347		}
348		set style [$w item style set $item 0]
349		$w item style map $item 0 $style.f {elemTxt elemTxtOver}
350		set HelpContents(prev) $item
351	    }
352	    return
353	}
354    }
355    if {$HelpContents(prev) ne ""} {
356	set style [$w item style set $HelpContents(prev) 0]
357	set style [string trim $style .f]
358	$w item style map $HelpContents(prev) 0 $style {elemTxtOver elemTxt}
359	set HelpContents(prev) ""
360    }
361    return
362}
363
364