summaryrefslogtreecommitdiffstats
path: root/demos/help.tcl
blob: e131f1512c24d07d1018241821a0ff2ebb6399ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#
# Demo: Help contents
#
proc DemoHelpContents {} {

	set T .f2.f1.t

	set height [font metrics [$T cget -font] -linespace]
	if {$height < 18} {
		set height 18
	}

	#
	# Configure the treectrl widget
	#

	$T configure -showroot no -showbuttons no -showlines no -itemheight $height \
		-selectmode browse

	InitPics help-*

	#
	# Create columns
	#

	$T column create -text "Help Contents"

	# Define a new item state
	$T state define mouseover

	#
	# Create elements
	#

	$T element create e1 image -image help-page
	$T element create e2 image -image {help-book-open {open} help-book-closed {}}
	$T element create e3 text -fill [list $::SystemHighlightText {selected focus} blue {mouseover}] \
		-font [list "[$T cget -font] underline" {mouseover}]
	$T element create e4 rect -fill [list $::SystemHighlight {selected focus}] -showfocus yes

	#
	# Create styles using the elements
	#

	# book
	set S [$T style create s1]
	$T style elements $S {e4 e1 e3}
	$T style layout $S e1 -padx {0 4} -expand ns
	$T style layout $S e3 -expand ns
	$T style layout $S e4 -union [list e3] -iexpand ns -ipadx 2

	# page
	set S [$T style create s2]
	$T style elements $S {e4 e2 e3}
	$T style layout $S e2 -padx {0 4} -expand ns
	$T style layout $S e3 -expand ns
	$T style layout $S e4 -union [list e3] -iexpand ns -ipadx 2

	#
	# Create items and assign styles
	#

	set parentList [list root {} {} {} {} {} {}]
	set parent root
	foreach {depth style text} {
		0 s1 "Welcome to Help"
		0 s2 "Introducing Windows 98"
			1 s2 "How to Use Help"
				2 s1 "Find a topic"
				2 s1 "Get more out of help"
			1 s2 "Register Your Software"
				2 s1 "Registering Windows 98 online"
			1 s2 "What's New in Windows 98"
				2 s1 "Innovative, easy-to-use features"
				2 s1 "Improved reliability"
				2 s1 "A faster operating system"
				2 s1 "True Web integration"
				2 s1 "More entertaining and fun"
			1 s2 "If You're New to Windows 98"
				2 s2 "Tips for Macintosh Users"
					3 s1 "Why does the mouse have two buttons?"
	} {
		set item [$T item create]
		$T item style set $item 0 $style
		$T item element configure $item 0 e3 -text $text
		$T item collapse $item
		$T item lastchild [lindex $parentList $depth] $item
		incr depth
		set parentList [lreplace $parentList $depth $depth $item]
	}

	bind TreeCtrlHelp <Double-ButtonPress-1> {
		if {[lindex [%W identify %x %y] 0] eq "header"} {
			TreeCtrl::DoubleButton1 %W %x %y
		} else {
			TreeCtrl::HelpButton1 %W %x %y
		}
		break
	}
	bind TreeCtrlHelp <ButtonPress-1> {
		TreeCtrl::HelpButton1 %W %x %y
		break
	}
	bind TreeCtrlHelp <Button1-Motion> {
		# noop
	}
	bind TreeCtrlHelp <Button1-Leave> {
		# noop
	}
	bind TreeCtrlHelp <Motion> {
		TreeCtrl::HelpMotion %W %x %y
	}
	bind TreeCtrlHelp <Leave> {
		TreeCtrl::HelpMotion %W %x %y
	}
	bind TreeCtrlHelp <KeyPress-Return> {
		if {[llength [%W selection get]] == 1} {
			%W item toggle [lindex [%W selection get] 0]
		}
		break
	}

	set ::TreeCtrl::Priv(help,prev) ""
	bindtags $T [list $T TreeCtrlHelp TreeCtrl [winfo toplevel $T] all]

	return
}

# This is an alternate implementation that does not define a new item state
# to change the appearance of the item under the cursor.
proc DemoHelpContents_2 {} {

	set T .f2.f1.t

	set height [font metrics [$T cget -font] -linespace]
	if {$height < 18} {
		set height 18
	}

	#
	# Configure the treectrl widget
	#

	$T configure -showroot no -showbuttons no -showlines no -itemheight $height \
		-selectmode browse

	InitPics help-*

	#
	# Create columns
	#

	$T column create -text "Help Contents"

	#
	# Create elements
	#

	$T element create e1 image -image help-page
	$T element create e2 image -image {help-book-open {open} help-book-closed {}}
	$T element create e3 text -fill [list $::SystemHighlightText {selected focus}]
	$T element create e4 rect -fill [list $::SystemHighlight {selected focus}] -showfocus yes
	$T element create e5 text -fill [list $::SystemHighlightText {selected focus} blue {}] \
		-font "[$T cget -font] underline"

	#
	# Create styles using the elements
	#

	# book
	set S [$T style create s1]
	$T style elements $S {e4 e1 e3}
	$T style layout $S e1 -padx {0 4} -expand ns
	$T style layout $S e3 -expand ns
	$T style layout $S e4 -union [list e3] -iexpand ns -ipadx 2

	# page
	set S [$T style create s2]
	$T style elements $S {e4 e2 e3}
	$T style layout $S e2 -padx {0 4} -expand ns
	$T style layout $S e3 -expand ns
	$T style layout $S e4 -union [list e3] -iexpand ns -ipadx 2

	# book (focus)
	set S [$T style create s1.f]
	$T style elements $S {e4 e1 e5}
	$T style layout $S e1 -padx {0 4} -expand ns
	$T style layout $S e5 -expand ns
	$T style layout $S e4 -union [list e5] -iexpand ns -ipadx {1 2}

	# page (focus)
	set S [$T style create s2.f]
	$T style elements $S {e4 e2 e5}
	$T style layout $S e2 -padx {0 4} -expand ns
	$T style layout $S e5 -expand ns
	$T style layout $S e4 -union [list e5] -iexpand ns -ipadx {1 2}

	#
	# Create items and assign styles
	#

	set parentList [list root {} {} {} {} {} {}]
	set parent root
	foreach {depth style text} {
		0 s1 "Welcome to Help"
		0 s2 "Introducing Windows 98"
			1 s2 "How to Use Help"
				2 s1 "Find a topic"
				2 s1 "Get more out of help"
			1 s2 "Register Your Software"
				2 s1 "Registering Windows 98 online"
			1 s2 "What's New in Windows 98"
				2 s1 "Innovative, easy-to-use features"
				2 s1 "Improved reliability"
				2 s1 "A faster operating system"
				2 s1 "True Web integration"
				2 s1 "More entertaining and fun"
			1 s2 "If You're New to Windows 98"
				2 s2 "Tips for Macintosh Users"
					3 s1 "Why does the mouse have two buttons?"
	} {
		set item [$T item create]
		$T item style set $item 0 $style
		$T item element configure $item 0 e3 -text $text
		$T item collapse $item
		$T item lastchild [lindex $parentList $depth] $item
		incr depth
		set parentList [lreplace $parentList $depth $depth $item]
	}

	bind TreeCtrlHelp <Double-ButtonPress-1> {
		if {[lindex [%W identify %x %y] 0] eq "header"} {
			TreeCtrl::DoubleButton1 %W %x %y
		} else {
			TreeCtrl::HelpButton1 %W %x %y
		}
		break
	}
	bind TreeCtrlHelp <ButtonPress-1> {
		TreeCtrl::HelpButton1 %W %x %y
		break
	}
	bind TreeCtrlHelp <Button1-Motion> {
		# noop
	}
	bind TreeCtrlHelp <Button1-Leave> {
		# noop
	}
	bind TreeCtrlHelp <Motion> {
		TreeCtrl::HelpMotion_2 %W %x %y
	}
	bind TreeCtrlHelp <Leave> {
		TreeCtrl::HelpMotion_2 %W %x %y
	}
	bind TreeCtrlHelp <KeyPress-Return> {
		if {[llength [%W selection get]] == 1} {
			%W item toggle [lindex [%W selection get] 0]
		}
		break
	}

	set ::TreeCtrl::Priv(help,prev) ""
	bindtags $T [list $T TreeCtrlHelp TreeCtrl [winfo toplevel $T] all]

	return
}

proc TreeCtrl::HelpButton1 {w x y} {
	variable Priv
	focus $w
	set id [$w identify $x $y]
	set Priv(buttonMode) ""
	if {[lindex $id 0] eq "header"} {
		ButtonPress1 $w $x $y
	} elseif {[lindex $id 0] eq "item"} {
		set item [lindex $id 1]
		# didn't click an element
		if {[llength $id] != 6} return
		if {[$w selection includes $item]} {
			$w item toggle $item
			return
		}
		if {[llength [$w selection get]]} {
			set item2 [lindex [$w selection get] 0]
			$w item collapse $item2
			foreach item2 [$w item ancestors $item2] {
				if {[$w compare $item != $item2]} {
					$w item collapse $item2
				}
			}
		}
		$w activate $item
		foreach item2 [$w item ancestors $item] {
			$w item expand $item2
		}
		$w item toggle $item
		$w selection modify $item all
	}
	return
}

proc TreeCtrl::HelpMotion {w x y} {
	variable Priv
	set id [$w identify $x $y]
	if {$id eq ""} {
	} elseif {[lindex $id 0] eq "header"} {
	} elseif {[lindex $id 0] eq "item"} {
		set item [lindex $id 1]
		if {[llength $id] == 6} {
			if {$item ne $Priv(help,prev)} {
				if {$Priv(help,prev) ne ""} {
					$w item state set $Priv(help,prev) !mouseover
				}
				$w item state set $item mouseover
				set Priv(help,prev) $item
			}
			return
		}
	}
	if {$Priv(help,prev) ne ""} {
		$w item state set $Priv(help,prev) !mouseover
		set Priv(help,prev) ""
	}
	return
}

# Alternate implementation that does not rely on run-time states
proc TreeCtrl::HelpMotion_2 {w x y} {
	variable Priv
	set id [$w identify $x $y]
	if {[lindex $id 0] eq "header"} {
	} elseif {$id ne ""} {
		set item [lindex $id 1]
		if {[llength $id] == 6} {
			if {$item ne $Priv(help,prev)} {
				if {$Priv(help,prev) ne ""} {
					set style [$w item style set $Priv(help,prev) 0]
					set style [string trim $style .f]
					$w item style map $Priv(help,prev) 0 $style {e5 e3}
				}
				set style [$w item style set $item 0]
				$w item style map $item 0 $style.f {e3 e5}
				set Priv(help,prev) $item
			}
			return
		}
	}
	if {$Priv(help,prev) ne ""} {
		set style [$w item style set $Priv(help,prev) 0]
		set style [string trim $style .f]
		$w item style map $Priv(help,prev) 0 $style {e5 e3}
		set Priv(help,prev) ""
	}
	return
}