File: itemwindow.tcl

package info (click to toggle)
ical 2.1-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,680 kB
  • ctags: 1,650
  • sloc: cpp: 12,639; tcl: 6,726; makefile: 584; sh: 521; perl: 60; ansic: 27
file content (680 lines) | stat: -rw-r--r-- 18,312 bytes parent folder | download
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# Copyright (c) 1993 by Sanjay Ghemawat
##############################################################################
# ItemWindow
#
#	Displays item contents as canvas item.
#
# Description
# ===========
# An ItemWindow displays item contents and allows editing.  An item
# window can be "selected".  At most one item window is selected in
# the entire application at any point in time.  Item selections
# and unselections result in the firing of triggers.
#
#	trigger fire select
#
# The following user-level hooks are also executed:
#
#	item-select   <item> <date>
#	item-unselect <item> <date>
#
# Implementation Notes
# ====================
#
# A mapping from <toplevel,item,date> to ItemWindow objects is
# maintained.  This map is used to find the item window responsible
# for displaying an occurrence of a specific item.

# effects - Create ItemWindow in canvas.
class ItemWindow {canvas font item date} {
    set slot(canvas) $canvas
    set slot(item) $item
    set slot(date) $date
    set slot(x) -100
    set slot(y) -100
    set slot(clickx) 0
    set slot(clicky) 0
    set slot(icon) {}
    set slot(link) {}
    set slot(width) 1
    set slot(height) 1
    set slot(sel) 0

    $canvas create rectangle -100 -100 -101 -101\
	-fill [pref itemOverflowColor]\
	-stipple [pref itemOverflowStipple]\
	-width 1\
	-tags [list item $self back back.$self click.$self vis.$self]

    $canvas create rectangle -100 -100 -101 -101\
	-fill [pref itemBg]\
	-width 0\
	-tags [list item $self rect.$self click.$self vis.$self]

    $canvas create bitmap -100 -100\
	-anchor nw\
	-foreground [pref itemFg]\
	-background [pref itemBg]\
	-tags [list item $self icon.$self vis.$self]

    $canvas create bitmap -100 -100\
	-anchor ne\
	-foreground [pref itemFg]\
	-background [pref itemBg]\
	-tags [list item $self link.$self vis.$self]

    set slot(text) [$canvas create text -100 -100\
			-anchor nw\
			-fill [pref itemFg]\
			-font $font\
			-width 0\
			-text ""\
			-tags [list item $self text.$self click.$self vis.$self]]

    $canvas bind click.$self <Button-1> [list $self click %x %y]
    $canvas bind click.$self <B1-Motion> [list $self select_to %x %y]
    $canvas bind icon.$self  <Button-1> [list $self toggle_done]
    $canvas bind link.$self  <Button-1> [list $self follow_link]
    $canvas bind click.$self <Double-Button-1> {ical_edit_item}

    global item_map
    set item_map([winfo toplevel $canvas],$item,$date) $self

    $self read
}

# effects - Destroy item
method ItemWindow destructor {} {
    # Clear selection if this is the selected item
    global item_map last_sel
    if ![string compare $last_sel $self] ical_unselect
    unset item_map([winfo toplevel $slot(canvas)],$slot(item),$slot(date))

    $slot(canvas) delete $self
}

# effects - Set item geometry
method ItemWindow geometry {x y w h} {
    set slot(x) $x
    set slot(y) $y
    set slot(width) $w
    set slot(height) $h

    $self place
}

# effects - Raise associated window
method ItemWindow raise {} {
    $slot(canvas) raise $self
}

method ItemWindow save {} {
    set item $slot(item)
    set new [lindex [$slot(canvas) itemconfig text.$self -text] 4]
    set old [$item text]

    if {[string compare $new $old] == 0} {return}

    $item text $new
}

method ItemWindow read {} {
    set slot(icon) {}
    set slot(link) {}
    if [$slot(item) todo]    {set slot(icon) todo_box}
    if [$slot(item) is_done] {set slot(icon) done_box}

    if ![catch {$slot(item) option Link}] {
	set slot(link) right_arrow
    }

    $slot(canvas) itemconfigure text.$self -text [$slot(item) text]
    $self place
}

method ItemWindow editable {} {
    # Check for read-only calendar
    set i $slot(item)
    set r 1
    catch {set r [cal readonly [$i calendar]]}
    if $r {return 0}

    # Check for repeating items
    set result [repeat_check [winfo toplevel $slot(canvas)] $i $slot(date)]
    if ![string compare $result cancel] {return 0}
    return 1
}

method ItemWindow insert {str} {
    if [$self editable] {
	$slot(canvas) insert text.$self insert $str
	$self save
    }
}

method ItemWindow select {} {
    set slot(sel) 1
    focus $slot(canvas)
    $slot(canvas) itemconfig text.$self -fill [pref itemSelectFg]
    $slot(canvas) itemconfig icon.$self -background [pref itemSelectBg]
    $slot(canvas) itemconfig link.$self -background [pref itemSelectBg]
    $slot(canvas) itemconfig rect.$self\
	-fill [pref itemSelectBg]\
	-width [pref itemSelectWidth]

    $slot(canvas) focus text.$self
    $self raise
}

method ItemWindow unselect {} {
    set slot(sel) 0
    focus [winfo toplevel $slot(canvas)]
    $slot(canvas) itemconfig text.$self -fill [pref itemFg]
    $slot(canvas) itemconfig icon.$self -background [pref itemBg]
    $slot(canvas) itemconfig link.$self -background [pref itemBg]
    $slot(canvas) itemconfig rect.$self\
	-fill [pref itemBg]\
	-width 0
    $slot(canvas) focus ""
}

method ItemWindow click {x y} {
    ical_select $slot(item) $slot(date)
    if !$slot(sel) return

    set x [expr int([$slot(canvas) canvasx $x])]
    set y [expr int([$slot(canvas) canvasy $y])]
    $slot(canvas) icursor text.$self @$x,$y
    $slot(canvas) select from text.$self @$x,$y

    set slot(clickx) $x
    set slot(clicky) $y
}

method ItemWindow select_to {x y} {
    set x [expr int([$slot(canvas) canvasx $x])]
    set y [expr int([$slot(canvas) canvasy $y])]

    if {(abs($x - $slot(clickx)) > 5) || (abs($y - $slot(clicky)) > 5)} {
	$slot(canvas) select to text.$self @$x,$y
    }
}

# effects - Return bounding box for specified item
method ItemWindow bbox {} {
    return [$slot(canvas) bbox vis.$self]
}

# effects - Toggle done status of item
method ItemWindow toggle_done {} {
    ical_select $slot(item) $slot(date)
    if !$slot(sel) return
    ical_toggle_done
}

# effects - Follow item link
method ItemWindow follow_link {} {
    ical_select $slot(item) $slot(date)
    if !$slot(sel) return
    ical_follow_link
}

# Internal Operations

# effects - Just position the text and any icon correctly
method ItemWindow place_text_and_icon {} {
    if ![string compare $slot(icon) {}] {
	set iw 0
	$slot(canvas) coords icon.$self -100 -100
    } else {
	set iw 18
	$slot(canvas) coords icon.$self [expr $slot(x)+2] [expr $slot(y)+2]
    }
    $slot(canvas) itemconfig icon.$self -bitmap $slot(icon)

    if ![string compare $slot(link) {}] {
	set lw 0
	$slot(canvas) coords link.$self -100 -100
    } else {
	set lw 18
	set pos [expr $slot(x) + $slot(width)]
	$slot(canvas) coords link.$self [expr $pos - 2] [expr $slot(y)+2]
    }
    $slot(canvas) itemconfig link.$self -bitmap $slot(link)

    set tx [expr $slot(x) + $iw + 2]
    $slot(canvas) coords text.$self $tx [expr $slot(y)+1]
    $slot(canvas) itemconfig text.$self -width [expr $slot(width) - $iw - $lw]
}

# effects - Place window at appropriate position in canvas
method ItemWindow place {} {
    $self place_text_and_icon

    set x1 $slot(x)
    set y1 $slot(y)
    set x2 [expr $x1 + $slot(width)]
    set y2 [expr $y1 + $slot(height)]

    # Auto size item.  Check for text overflow
    set bbox [$slot(canvas) bbox text.$self]
    set yt [lindex $bbox 3]
    if {$yt > $y2} {set y2 $yt}

    $slot(canvas) coords rect.$self $x1 $y1 $x2 $y2
}

method ItemWindow canvas {} {
    return $slot(canvas)
}

method ItemWindow item {} {
    return $slot(item)
}

method ItemWindow date {} {
    return $slot(date)
}

##############################################################################
# ApptItemWindow
#
#	Displays appointment contents as canvas item.
#
# Description
# ===========
# An ApptItemWindow displays appt contents and allows editing.
# The ApptItemWindow is always maintained in a canvas.
#
# An ApptItemWindow is a subclass of ItemWindow that maintains
# the following extra slots --
#
#	move_callback
#		Execute {move_callback $item <y-coord>} when dragging
#		an itemwindow around.  <y-coord> is coordinate for top
#		of window (within parent canvas).
#		When drag is finished, {move_callback $item done} is called.
#
#	resize_callback
#		Execute {resize_callback $item <top> <bot>} when resizing
#		an itemwindow with the mouse.  <top>/<bot> are coordinates
#		for the top and bottom of the window (within parent canvas).
#		When resize is finished, {resize_callback $item done done}
#		is called.

# effects - Create ApptItemWindow in canvas.
subclass ApptItemWindow ItemWindow {canvas font item date m r} {
    # Create resize handles
    foreach v {t b} {
	foreach s {l m r} {
	    set slot(handle:$v$s) [$canvas create rectangle\
				     -100 -100 -101 -101\
				     -fill [pref itemFg]\
				     -width 0\
				     -tags [list $self\
						hand.$self\
						${v}hand.$self\
						${v}hand\
						invis.$self]]
	}
    }

    super constructor $canvas $font $item $date
    $canvas raise hand.$self

    set slot(move_callback) $m
    set slot(resize_callback) $r

    set slot(dragy) 0
    set slot(dragtop) 0
    set slot(dragbot) 0
    set slot(moving) 0
    set slot(resizing) 0
    set slot(minheight) 0

    $canvas bind thand.$self <ButtonPress-1>	[list $self size_top %y]
    $canvas bind thand.$self <B1-Motion>	[list $self size_continue %y]
    $canvas bind thand.$self <ButtonRelease-1>	[list $self size_finish %y]
    $canvas bind bhand.$self <ButtonPress-1>	[list $self size_bot %y]
    $canvas bind bhand.$self <B1-Motion>	[list $self size_continue %y]
    $canvas bind bhand.$self <ButtonRelease-1>	[list $self size_finish %y]

    $canvas bind $self <ButtonPress-2>		[list $self move_start %y]
    $canvas bind $self <B2-Motion>	 	[list $self move_continue %y]
    $canvas bind $self <ButtonRelease-2>	[list $self move_finish %y]

    $canvas bind $self <ButtonPress-3>		[list $self size_start %y]
    $canvas bind $self <B3-Motion>	 	[list $self size_continue %y]
    $canvas bind $self <ButtonRelease-3>	[list $self size_finish %y]
}

method ApptItemWindow save {} {
    set item $slot(item)
    set new [lindex [$slot(canvas) itemconfig text.$self -text] 4]
    set old [$item text]

    if {[string compare $new $old] == 0} {return}

    if ![cal option AllowOverflow] {
	# Prevent text from overflowing appointment
	if {[string length $new] > [string length $old]} {
	    set bbox [$slot(canvas) bbox text.$self]
	    if {[lindex $bbox 3] > ($slot(y) + $slot(height))} {
		# Refuse to enlarge text if it does not fit
		$self read
		return
	    }
	}
    }

    $item text $new
}

method ApptItemWindow unselect {} {
    set slot(moving) 0
    set slot(resizing) 0
    super unselect
}

# Internal Operations

# effects - Place window at appropriate position in canvas
method ApptItemWindow place {} {
    set text [$slot(item) text]
    $slot(canvas) itemconfig text.$self -text $text

    $self place_text_and_icon

    set x1 $slot(x)
    set y1 $slot(y)
    set x2 [expr $x1 + $slot(width)]
    set y2 [expr $y1 + $slot(height)]

    $slot(canvas) coords rect.$self $x1 $y1 $x2 $y2
    $self place_handles $x1 $y1 $x2 $y2

    if $slot(sel) {
	# Auto size item to avoid text overflow
	set bbox [$slot(canvas) bbox text.$self]
	set yt [lindex $bbox 3]
	if {$yt > $y2} {set y2 $yt}
    } else {
	# Truncate text to fit inside boundary
	set i [$slot(canvas) index text.$self @0,$y2]
	set text2 [string range $text 0 [expr $i-1]]
	if [string compare $text $text2] {
	    set text2 [string range $text 0 [expr $i-4]]...
	    $slot(canvas) itemconfigure text.$self -text $text2
	}
    }

    $slot(canvas) coords back.$self $x1 $y1 $x2 $y2
}

method ApptItemWindow place_handles {x1 y1 x2 y2} {
    # Display handles only if selected and writable
    set hide 1
    if {$slot(sel)} {
	catch {set hide [cal readonly [$slot(item) calendar]]}
    }

    if $hide {
	foreach h {tl tm tr bl bm br} {
	    $slot(canvas) coords $slot(handle:$h) -100 -100 -101 -101
	}
    } else {
	$slot(canvas) coords $slot(handle:tl)\
	    [expr $x1-4] [expr $y1-4] [expr $x1+4] [expr $y1+4]
	$slot(canvas) coords $slot(handle:tm)\
	    [expr ($x1+$x2)/2-4] [expr $y1-4] [expr ($x1+$x2)/2+4] [expr $y1+4]
	$slot(canvas) coords $slot(handle:tr)\
	    [expr $x2-4] [expr $y1-4] [expr $x2+4] [expr $y1+4]
	$slot(canvas) coords $slot(handle:bl)\
	    [expr $x1-4] [expr $y2-4] [expr $x1+4] [expr $y2+4]
	$slot(canvas) coords $slot(handle:bm)\
	    [expr ($x1+$x2)/2-4] [expr $y2-4] [expr ($x1+$x2)/2+4] [expr $y2+4]
	$slot(canvas) coords $slot(handle:br)\
	    [expr $x2-4] [expr $y2-4] [expr $x2+4] [expr $y2+4]
    }
}

method ApptItemWindow move_start {y} {
    ical_select $slot(item) $slot(date)
    if [catch {set cal [$slot(item) calendar]}] return
    if [cal readonly $cal] {return}

    set r [repeat_check [winfo toplevel $slot(canvas)] $slot(item) $slot(date)]
    if {$r != "unnecessary"} return

    set slot(moving) 1
    set slot(dragy) [expr $y-$slot(y)]
}

method ApptItemWindow move_continue {y} {
    if !$slot(moving) {return}

    set new_y [expr $y-$slot(dragy)]
    eval $slot(move_callback) $slot(item) $new_y
}

method ApptItemWindow move_finish {y} {
    if !$slot(moving) {return}
    $self move_continue $y
    set slot(moving) 0
    eval $slot(move_callback) $slot(item) done
}

method ApptItemWindow size_top {y} {
    ical_select $slot(item) $slot(date)
    if [catch {set cal [$slot(item) calendar]}] return
    if [cal readonly $cal] {return}

    set r [repeat_check [winfo toplevel $slot(canvas)] $slot(item) $slot(date)]
    if {$r != "unnecessary"} return

    set slot(resizing) 1
    set slot(dragtop) 1
    set slot(dragbot) 0
    set slot(minheight) 1

    if ![cal option AllowOverflow] {
	set bbox [$slot(canvas) bbox text.$self]
	set slot(minheight) [expr [lindex $bbox 3] - [lindex $bbox 1]]
    }
}

method ApptItemWindow size_bot {y} {
    ical_select $slot(item) $slot(date)
    if [catch {set cal [$slot(item) calendar]}] return
    if [cal readonly $cal] {return}

    set r [repeat_check [winfo toplevel $slot(canvas)] $slot(item) $slot(date)]
    if {$r != "unnecessary"} return

    set slot(resizing) 1
    set slot(dragtop) 0
    set slot(dragbot) 1
    set slot(minheight) 1

    if ![cal option AllowOverflow] {
	set bbox [$slot(canvas) bbox text.$self]
	set slot(minheight) [expr [lindex $bbox 3] - [lindex $bbox 1]]
    }
}

method ApptItemWindow size_start {y} {
    ical_select $slot(item) $slot(date)
    if [catch {set cal [$slot(item) calendar]}] return
    if [cal readonly $cal] {return}

    set r [repeat_check [winfo toplevel $slot(canvas)] $slot(item) $slot(date)]
    if {$r != "unnecessary"} return

    set slot(resizing) 1
    set slot(dragtop) 0
    set slot(dragbot) 0
    set slot(minheight) 1

    if ![cal option AllowOverflow] {
	set bbox [$slot(canvas) bbox text.$self]
	set slot(minheight) [expr [lindex $bbox 3] - [lindex $bbox 1]]
    }
}

method ApptItemWindow size_continue {y} {
    if !$slot(resizing) {return}

    set new_y [$slot(canvas) canvasy $y]
    set top $slot(y)
    set bot [expr $slot(y)+$slot(height)]

    if {($new_y < $top) && !$slot(dragbot)} {
	set slot(dragtop) 1
	set slot(dragbot) 0
	eval $slot(resize_callback) $slot(item) $new_y $bot
	return
    }

    if {($new_y > $bot) && !$slot(dragtop)} {
	set slot(dragtop) 0
	set slot(dragbot) 1
	eval $slot(resize_callback) $slot(item) $top $new_y
	return
    }

    if $slot(dragtop) {
	set top $new_y
    }

    if $slot(dragbot) {
	set bot $new_y
    }

    if {($bot - $top) >= $slot(minheight)} {
	eval $slot(resize_callback) $slot(item) $top $bot
    }
}

method ApptItemWindow size_finish {y} {
    if !$slot(resizing) {return}
    $self size_continue $y
    set slot(resizing) 0
    eval $slot(resize_callback) $slot(item) done done
}

#### Selection support ####

set last_sel {}
set last_focus {}
set dv_state(state:remind)	-1
set dv_state(state:hilite)	""
set dv_state(state:todo)	0

proc ical_focus_on {w} {
    global last_focus
    set last_focus [winfo toplevel $w]
}

proc ical_focus {} {
    global last_focus
    return $last_focus
}

proc ical_find_selection {} {
    global last_sel last_focus
    if [catch {set can [$last_sel canvas]}] {error "no selection"}
    if [string compare [winfo toplevel $can] [ical_focus]] {
	# Current focus is not on window with selected item
	error "no selection"
    }

    # Current focus is on window with selected item
    return [$last_sel item]
}

proc ical_select {item date} {
    global item_map last_focus dv_state
    if ![info exists item_map($last_focus,$item,$date)] return
    set sel $item_map($last_focus,$item,$date)

    # Remove any previous selection and then make new selection
    global last_sel
    if [string compare $sel $last_sel] {
	# Remove last selection (if any)
	catch {
	    $last_sel unselect
	    run-hook item-unselect [$last_sel item] [$last_sel date]
	}

	# Create new selection
	set last_sel $sel
	$sel select
	set dv_state(state:remind)	[$item earlywarning]
	set dv_state(state:hilite)	[$item hilite]
	set dv_state(state:todo)	[$item todo]
	trigger fire select
	run-hook item-select $item $date
    }
}

proc ical_unselect {} {
    global last_sel dv_state
    if ![string compare $last_sel {}] return

    # Clear trace of current selection
    set sel $last_sel
    set last_sel {}

    # Now undo the selection
    catch {
	catch {set dv_state(state:remind) -1}
	catch {set dv_state(state:hilite) ""}
	catch {set dv_state(state:todo)   0}

	$sel unselect
	trigger fire select
	run-hook item-unselect [$sel item] [$sel date]
    }
}

#### Editing Support ####

method ItemWindow mvcursor {n} {
    puts stderr "mvcursor $n"
}

method ItemWindow mvline {n} {
    puts stderr "mvline $n"
}

method ItemWindow mvword {n} {
    puts stderr "mvword $n"
}

method ItemWindow mvbob {} {}
method ItemWindow mveob {} {}
method ItemWindow deletekey {} {}

# Proc to run itemwindow key binding after checking for writability
proc itemwindow_mod {c args} {
    global last_sel
    if {[info exists last_sel] && [$last_sel editable]} {
	eval $args
	$last_sel save
    }
}

proc itemwindow_make_bindings {tag} {
    tkCanvasBindings $tag

    # Disable the following bindings to let the general keymap handle them
    bind $tag <Escape> {continue}
    bind $tag <Tab>    {continue}

    # Replace modification check in bindings
    foreach seq [bind $tag] {
	set cmd [bind $tag $seq]
	regsub -all -- tkCanvasMod $cmd itemwindow_mod cmd
	bind $tag $seq $cmd
    }
}