summaryrefslogtreecommitdiffstats
path: root/demos/random.tcl
blob: 8b78a559c79c309c29b52f3685b8fba8cd88bbb7 (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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
set RandomN 500

#
# Demo: random N items
#
proc DemoRandom {} {

	set T .f2.f1.t

	InitPics folder-* small-*

	set height [font metrics [$T cget -font] -linespace]
	if {$height < 18} {
		set height 18
	}
	$T configure -itemheight $height -selectmode extended \
		-showroot yes -showrootbutton yes -showbuttons yes -showlines yes \
		-scrollmargin 16 -xscrolldelay "500 50" -yscrolldelay "500 50"

	$T column create -expand yes -text Item -itembackground {#e0e8f0 {}} -tag item
	$T column create -text Parent -justify center -itembackground {gray90 {}} -tag parent
	$T column create -text Depth -justify center -itembackground {linen {}} -tag depth

	$T element create e1 image -image {folder-open {open} folder-closed {}}
	$T element create e2 image -image small-file
	$T element create e3 text \
		-fill [list $::SystemHighlightText {selected focus}]
	$T element create e4 text -fill blue
	$T element create e6 text
	$T element create e5 rect -showfocus yes \
		-fill [list $::SystemHighlight {selected focus} gray {selected !focus}]

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

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

	$T style create s3
	$T style elements s3 {e6}
	$T style layout s3 e6 -padx 6 -expand ns

	set ::TreeCtrl::Priv(sensitive,$T) {
		{item s1 e5 e1 e3}
		{item s2 e5 e2 e3}
	}
	set ::TreeCtrl::Priv(dragimage,$T) {
		{item s1 e1 e3}
		{item s2 e2 e3}
	}

	set clicks [clock clicks]
	for {set i 1} {$i < $::RandomN} {incr i} {
		$T item create
		while 1 {
			set j [expr {int(rand() * $i)}]
			if {[$T depth $j] < 5} break
		}
		if {rand() * 2 > 1} {
			$T collapse $i
		}
		if {rand() * 2 > 1} {
			$T item lastchild $j $i
		} else {
			$T item firstchild $j $i
		}
	}
	puts "created $::RandomN-1 items in [expr [clock clicks] - $clicks] clicks"
	set clicks [clock clicks]
	for {set i 0} {$i < $::RandomN} {incr i} {
		set numChildren [$T item numchildren $i]
		if {$numChildren}  {
			$T item configure $i -button yes
			$T item style set $i 0 s1 1 s3 2 s3
			$T item complex $i \
				[list [list e3 -text "Item $i"] [list e4 -text "($numChildren)"]] \
				[list [list e6 -text "[$T item parent $i]"]] \
				[list [list e6 -text "[$T depth $i]"]]
		} else {
			$T item style set $i 1 s3 2 s3 0 s2
			$T item complex $i \
				[list [list e3 -text "Item $i"]] \
				[list [list e6 -text "[$T item parent $i]"]] \
				[list [list e6 -text "[$T depth $i]"]]
		}
	}
	puts "configured $::RandomN items in [expr [clock clicks] - $clicks] clicks"

	bind TreeCtrlRandom <Double-ButtonPress-1> {
		TreeCtrl::DoubleButton1 %W %x %y
		break
	}
	bind TreeCtrlRandom <Control-ButtonPress-1> {
		set TreeCtrl::Priv(selectMode) toggle
		TreeCtrl::RandomButton1 %W %x %y
		break
	}
	bind TreeCtrlRandom <Shift-ButtonPress-1> {
		set TreeCtrl::Priv(selectMode) add
		TreeCtrl::RandomButton1 %W %x %y
		break
	}
	bind TreeCtrlRandom <ButtonPress-1> {
		set TreeCtrl::Priv(selectMode) set
		TreeCtrl::RandomButton1 %W %x %y
		break
	}
	bind TreeCtrlRandom <Button1-Motion> {
		TreeCtrl::RandomMotion1 %W %x %y
		break
	}
	bind TreeCtrlRandom <Button1-Leave> {
		TreeCtrl::RandomLeave1 %W %x %y
		break
	}
	bind TreeCtrlRandom <ButtonRelease-1> {
		TreeCtrl::RandomRelease1 %W %x %y
		break
	}

	bindtags $T [list $T TreeCtrlRandom TreeCtrl [winfo toplevel $T] all]

	return
}

proc TreeCtrl::RandomButton1 {T x y} {
	variable Priv
	focus $T
	set id [$T identify $x $y]
	puts $id
	set Priv(buttonMode) ""

	# Click outside any item
	if {$id eq ""} {
		$T selection clear

	# Click in header
	} elseif {[lindex $id 0] eq "header"} {
		ButtonPress1 $T $x $y

	# Click in item
	} else {
		foreach {where item arg1 arg2 arg3 arg4} $id {}
		switch $arg1 {
			button {
				$T toggle $item
			}
			line {
				$T toggle $arg2
			}
			column {
				set ok 0
				# Clicked an element
				if {[llength $id] == 6} {
					set column [lindex $id 3]
					set E [lindex $id 5]
					foreach list $Priv(sensitive,$T) {
						set C [lindex $list 0]
						set S [lindex $list 1]
						set eList [lrange $list 2 end]
						if {$column != [$T column index $C]} continue
						if {[$T item style set $item $C] ne $S} continue
						if {[lsearch -exact $eList $E] == -1} continue
						set ok 1
						break
					}
				}
				if {!$ok} {
					$T selection clear
					return
				}

				set Priv(drag,motion) 0
				set Priv(drag,x) [$T canvasx $x]
				set Priv(drag,y) [$T canvasy $y]
				set Priv(drop) ""

				if {$Priv(selectMode) eq "add"} {
					BeginExtend $T $item
				} elseif {$Priv(selectMode) eq "toggle"} {
					BeginToggle $T $item
				} elseif {![$T selection includes $item]} {
					BeginSelect $T $item
				}
				$T activate $item

				if {[$T selection includes $item]} {
					set Priv(buttonMode) drag
				}
			}
		}
	}
	return
}
proc TreeCtrl::RandomMotion1 {T x y} {
	variable Priv
	switch $Priv(buttonMode) {
		"resize" -
		"header" {
			Motion1 $T $x $y
		}
		"drag" {
			RandomAutoScanCheck $T $x $y
			RandomMotion $T $x $y
		}
	}
	return
}
proc TreeCtrl::RandomMotion {T x y} {
	variable Priv
	switch $Priv(buttonMode) {
		"resize" -
		"header" {
			Motion1 $T $x $y
		}
		"drag" {
			# Detect initial mouse movement
			if {!$Priv(drag,motion)} {
				set Priv(selection) [$T selection get]
				set Priv(drop) ""
				$T dragimage clear
				# For each selected item, add 2nd and 3rd elements of
				# column "item" to the dragimage
				foreach I $Priv(selection) {
					foreach list $Priv(dragimage,$T) {
						set C [lindex $list 0]
						set S [lindex $list 1]
						if {[$T item style set $I $C] eq $S} {
							eval $T dragimage add $I $C [lrange $list 2 end]
						}
					}
				}
				set Priv(drag,motion) 1
			}

			# Find the item under the cursor
			set cursor X_cursor
			set drop ""
			set id [$T identify $x $y]
			set ok 0
			if {($id ne "") && ([lindex $id 0] eq "item") && ([llength $id] == 6)} {
				set item [lindex $id 1]
				set column [lindex $id 3]
				set E [lindex $id 5]
				foreach list $Priv(sensitive,$T) {
					set C [lindex $list 0]
					set S [lindex $list 1]
					set eList [lrange $list 2 end]
					if {$column != [$T column index $C]} continue
					if {[$T item style set $item $C] ne $S} continue
					if {[lsearch -exact $eList $E] == -1} continue
					set ok 1
					break
				}
				if {[lsearch -exact $Priv(sensitive,$T) $E] != -1} {
					set ok 1
				}
			}
			if {$ok} {
				# If the item is not in the pre-drag selection
				# (i.e. not being dragged) see if we can drop on it
				if {[lsearch -exact $Priv(selection) $item] == -1} {
					set drop $item
					# We can drop if dragged item isn't an ancestor
					foreach item2 $Priv(selection) {
						if {[$T item isancestor $item2 $item]} {
							set drop ""
							break
						}
					}
					if {$drop ne ""} {
						scan [$T item bbox $drop] "%d %d %d %d" x1 y1 x2 y2
						if {$y < $y1 + 3} {
							set cursor top_side
							set Priv(drop,pos) prevsibling
						} elseif {$y >= $y2 - 3} {
							set cursor bottom_side
							set Priv(drop,pos) nextsibling
						} else {
							set cursor ""
							set Priv(drop,pos) lastchild
						}
					}
				}
			}

			if {[$T cget -cursor] ne $cursor} {
				$T configure -cursor $cursor
			}

			# Select the item under the cursor (if any) and deselect
			# the previous drop-item (if any)
			$T selection modify $drop $Priv(drop)
			set Priv(drop) $drop

			# Show the dragimage in its new position
			set x [expr {[$T canvasx $x] - $Priv(drag,x)}]
			set y [expr {[$T canvasy $y] - $Priv(drag,y)}]
			$T dragimage offset $x $y
			$T dragimage configure -visible yes
		}
	}
	return
}
proc TreeCtrl::RandomLeave1 {T x y} {
	variable Priv
	# This is called when I do ButtonPress-1 on Unix for some reason,
	# and buttonMode is undefined.
	if {![info exists Priv(buttonMode)]} return
	switch $Priv(buttonMode) {
		"header" {
			Leave1 $T $x $y
		}
	}
	return
}
proc TreeCtrl::RandomRelease1 {T x y} {
	variable Priv
	switch $Priv(buttonMode) {
		"resize" -
		"header" {
			Release1 $T $x $y
		}
		"drag" {
			AutoScanCancel $T
			$T dragimage configure -visible no
			$T selection modify {} $Priv(drop)
			$T configure -cursor ""
			if {$Priv(drop) ne ""} {
				RandomDrop $T $Priv(drop) $Priv(selection) $Priv(drop,pos)
			}
		}
	}
	set Priv(buttonMode) ""
	return
}

proc RandomDrop {T target source pos} {
	set parentList {}
	switch -- $pos {
		lastchild { set parent $target }
		prevsibling { set parent [$T item parent $target] }
		nextsibling { set parent [$T item parent $target] }
	}
	foreach item $source {

		# Ignore any item whose ancestor is also selected
		set ignore 0
		foreach ancestor [$T item ancestors $item] {
			if {[lsearch -exact $source $ancestor] != -1} {
				set ignore 1
				break
			}
		}
		if {$ignore} continue

		# Update the old parent of this moved item later
		if {[lsearch -exact $parentList $item] == -1} {
			lappend parentList [$T item parent $item]
		}

		# Add to target
		$T item $pos $target $item

		# Update text: parent
		$T item element configure $item parent e6 -text $parent

		# Update text: depth
		$T item element configure $item depth e6 -text [$T depth $item]

		# Recursively update text: depth
		set itemList [$T item firstchild $item]
		while {[llength $itemList]} {
			# Pop
			set item [lindex $itemList end]
			set itemList [lrange $itemList 0 end-1]

			$T item element configure $item depth e6 -text [$T depth $item]

			set item2 [$T item nextsibling $item]
			if {$item2 ne ""} {
				# Push
				lappend itemList $item2
			}
			set item2 [$T item firstchild $item]
			if {$item2 ne ""} {
				# Push
				lappend itemList $item2
			}
		}
	}

	# Update items that lost some children
	foreach item $parentList {
		set numChildren [$T item numchildren $item]
		if {$numChildren == 0} {
			$T item configure $item -button no
			$T item style map $item item s2 {e3 e3}
		} else {
			$T item element configure $item item e4 -text "($numChildren)"
		}
	}

	# Update the target that gained some children
	if {[$T item style set $parent 0] ne "s1"} {
		$T item configure $parent -button yes
		$T item style map $parent item s1 {e3 e3}
	}
	set numChildren [$T item numchildren $parent]
	$T item element configure $parent item e4 -text "($numChildren)"
	return
}

# Same as TreeCtrl::AutoScanCheck, but calls RandomMotion and
# RandomAutoScanCheckAux
proc TreeCtrl::RandomAutoScanCheck {T x y} {
	variable Priv
	scan [$T contentbox] "%d %d %d %d" x1 y1 x2 y2
	set margin [winfo pixels $T [$T cget -scrollmargin]]
	if {($x < $x1 + $margin) || ($x >= $x2 - $margin) ||
		($y < $y1 + $margin) || ($y >= $y2 - $margin)} {
		if {![info exists Priv(autoscan,afterId,$T)]} {
			if {$y >= $y2 - $margin} {
				$T yview scroll 1 units
				set delay [$T cget -yscrolldelay]
			} elseif {$y < $y1 + $margin} {
				$T yview scroll -1 units
				set delay [$T cget -yscrolldelay]
			} elseif {$x >= $x2 - $margin} {
				$T xview scroll 1 units
				set delay [$T cget -xscrolldelay]
			} elseif {$x < $x1 + $margin} {
				$T xview scroll -1 units
				set delay [$T cget -xscrolldelay]
			}
			set count [scan $delay "%d %d" d1 d2]
			if {[info exists Priv(autoscan,scanning,$T)]} {
				if {$count == 2} {
					set delay $d2
				}
			} else {
				if {$count == 2} {
					set delay $d1
				}
				set Priv(autoscan,scanning,$T) 1
			}
			switch $Priv(buttonMode) {
				"drag" -
				"marquee" {
					RandomMotion $T $x $y
				}
			}
			set Priv(autoscan,afterId,$T) [after $delay [list TreeCtrl::RandomAutoScanCheckAux $T]]
		}
		return
	}
	AutoScanCancel $T
	return
}

proc TreeCtrl::RandomAutoScanCheckAux {T} {
	variable Priv
	unset Priv(autoscan,afterId,$T)
	set x [winfo pointerx $T]
	set y [winfo pointery $T]
	set x [expr {$x - [winfo rootx $T]}]
	set y [expr {$y - [winfo rooty $T]}]
	RandomAutoScanCheck $T $x $y
	return
}

#
# Demo: random N items, button images
#
proc DemoRandom2 {} {

	set T .f2.f1.t

	DemoRandom

	InitPics mac-*

	$T configure -openbuttonimage mac-collapse -closedbuttonimage mac-expand \
		-showlines no

	return
}