File: CanvasCmd.tcl

package info (click to toggle)
coccinella 0.96.20-9
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 13,184 kB
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 62
file content (489 lines) | stat: -rw-r--r-- 13,036 bytes parent folder | download | duplicates (4)
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
# CanvasCmd.tcl ---
#  
#       Implementations of some whiteboard canvas actions.
#      
#  Copyright (c) 2000-2003  Mats Bengtsson
#  
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#   
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#  
# $Id: CanvasCmd.tcl,v 1.17 2008-03-25 08:52:31 matben Exp $

package provide CanvasCmd 1.0


namespace eval ::CanvasCmd:: {
    
}

# CanvasCmd::SelectAll --
#
#       Selects all items in the canvas.
#   
# Arguments:
#       wcan        canvas widget
#       
# Results:
#       none

proc ::CanvasCmd::SelectAll {wcan} {
    
    $wcan delete tbbox
    set ids [$wcan find all]
    foreach id $ids {
	$wcan dtag $id selected
	::CanvasDraw::MarkBbox $wcan 1 $id
    }
}

# CanvasCmd::DeselectAll --
#
#       Deselects all items in the canvas.
#   
# Arguments:
#       wcan        canvas widget
#       
# Results:
#       none

proc ::CanvasCmd::DeselectAll {wcan} {
	
    $wcan delete withtag tbbox
    $wcan dtag all selected
}

proc ::CanvasCmd::MoveSelected {wcan detail} {
    
    set selected [$wcan find withtag selected]
    if {![llength $selected]} {
	return
    }
    set dx 0
    set dy 0
    set step 10
    
    switch -- $detail {
	up {
	    set dy -$step
	}
	down {
	    set dy $step
	}
	right {
	    set dx $step
	}
	left {
	    set dx -$step
	}
    }
    
    set mdx [expr {-1*$dx}]
    set mdy [expr {-1*$dy}]
    set cmdList {}
    set cmdUndoList {}
    
    foreach id $selected {
	set utag [::CanvasUtils::GetUtag $wcan $id]
	
	# Let images use coords instead since more robust if transported.
	switch -- [$wcan type $id] {
	    image {
		
		# Find new coords.
		lassign [$wcan coords $id] x0 y0
		set x [expr {$x0 + $dx}]
		set y [expr {$y0 + $dy}]
		lappend cmdList [list coords $utag $x $y]
		lappend cmdUndoList [list coords $utag $x0 $y0]
	    }
	    default {
		lappend cmdList [list move $utag $dx $dy]
		lappend cmdUndoList [list move $utag $mdx $mdy]
	    }
	}
    }
    set w [winfo toplevel $wcan]
    set redo [list ::CanvasUtils::CommandList $w $cmdList]
    set undo [list ::CanvasUtils::CommandList $w $cmdUndoList]
    eval $redo
    undo::add [::WB::GetUndoToken $wcan] $undo $redo   

    ::CanvasFile::SetUnsaved $wcan
    
    DeselectAll $wcan
    foreach id $selected {
	::CanvasDraw::SelectItem $wcan $id
    }
}

# CanvasCmd::RaiseOrLowerItems --
#
#       Raise or lower the stacking order of the selected canvas item.
#       
# Arguments:
#       wcan        canvas widget
#       what        "raise" or "lower"
#       
# Results:
#       none.

proc ::CanvasCmd::RaiseOrLowerItems {wcan {what raise}} {
        
    set w [winfo toplevel $wcan]
    upvar ::WB::${w}::state state

    set cmdList {}
    set undoList {}
    
    # The items are returned in stacking order, with the lowest item first.
    # If lower we must start with the topmost, and then go downwards!
    set selected [$wcan find withtag selected]
    if {[string equal $what "lower"]} {
	set selected [lrevert $selected]
    }
    foreach id $selected {
	set utag [::CanvasUtils::GetUtag $wcan $id]
	lappend cmdList [list $what $utag all]
	lappend undoList [::CanvasUtils::GetStackingCmd $wcan $utag]
    }
    if {$state(canGridOn) && [string equal $what "lower"]} {
	lappend cmdList [list lower grid all]
    }
    set redo [list ::CanvasUtils::CommandList $w $cmdList]
    set undo [list ::CanvasUtils::CommandList $w $undoList]
    eval $redo
    undo::add [::WB::GetUndoToken $wcan] $undo $redo

    ::CanvasFile::SetUnsaved $wcan
}

# CanvasCmd::SetCanvasBgColor --
#
#       Sets background color of canvas.
#       
# Arguments:
#       w           toplevel widget path
#       
# Results:
#       color dialog shown.

proc ::CanvasCmd::SetCanvasBgColor {w} {
    global  prefs
    upvar ::WB::${w}::state state
	
    set wcan [::WB::GetCanvasFromWtop $w]
    set prevCol $state(bgColCan)
    set col [tk_chooseColor -initialcolor $state(bgColCan)]
    if {$col ne ""} {
	
	# The change should be triggered automatically through the trace.
	set state(bgColCan) $col
	set cmd [list configure -bg $col -highlightbackground $col]
	set undocmd [list configure -bg $prevCol -highlightbackground $prevCol]
	set redo [list ::CanvasUtils::Command $w $cmd]
	set undo [list ::CanvasUtils::Command $w $undocmd]
	eval $redo
	undo::add [::WB::GetUndoToken $wcan] $undo $redo
    }
}

# CanvasCmd::DoCanvasGrid --
#
#       Make a grid in the canvas; uses state(canGridOn) to toggle grid.
#       
# Arguments:
#       
# Results:
#       grid shown/hidden.

proc ::CanvasCmd::DoCanvasGrid {w} {
    global  prefs this
    
    upvar ::WB::${w}::state state

    set wcan [::WB::GetCanvasFromWtop $w]
    set length 2000
    set gridDist $prefs(gridDist)
    if {$state(canGridOn) == 0} {
	$wcan delete grid
	return
    }
    if {$this(platform) eq "windows"} {
	for {set x $gridDist} {$x <= $length} {set x [expr {$x + $gridDist}]} {
	    $wcan create line $x 0 $x $length  \
	      -width 1 -fill gray50 -tags {notactive grid}
	}
	for {set y $gridDist} {$y <= $length} {set y [expr {$y + $gridDist}]} {
	    $wcan create line 0 $y $length $y  \
	      -width 1 -fill gray50 -tags {notactive grid}
	}
    } else {
	for {set x $gridDist} {$x <= $length} {set x [expr {$x + $gridDist}]} {
	    $wcan create line $x 0 $x $length  \
	      -width 1 -fill gray50 -tags {notactive grid} -stipple gray50
	}
	for {set y $gridDist} {$y <= $length} {set y [expr {$y + $gridDist}]} {
	    $wcan create line 0 $y $length $y  \
	      -width 1 -fill gray50 -tags {notactive grid} -stipple gray50
	}
    }
    $wcan lower grid
}

# CanvasCmd::ResizeItem --
#
#       Scales each selected item in canvas 'w' by a factor 'factor'. 
#       Not all item types are rescaled. 
#       
# Arguments:
#       wcan        canvas widget
#       factor      a numerical factor to scale with.
#       
# Results:
#       item resized, propagated to clients.

proc ::CanvasCmd::ResizeItem {wcan factor} {
    global  prefs
	
    set w [winfo toplevel $wcan]
    set ids [$wcan find withtag selected]
    if {$ids eq {}} {
	return
    }
    if {$prefs(scaleCommonCG)} {
	set bbox [eval $wcan bbox $ids]
	set cgx [expr {([lindex $bbox 0] + [lindex $bbox 2])/2.0}]
	set cgy [expr {([lindex $bbox 1] + [lindex $bbox 3])/2.0}]
    }
    set cmdList {}
    set undoList {}
    set invfactor [expr {1.0/$factor}]
    foreach id $ids {
	set utag [::CanvasUtils::GetUtag $wcan $id]
	if {$utag eq ""} {
	    continue
	}	

	# Sort out the nonrescalable ones.
	set type [$wcan type $id]
	if {[string equal $type "text"] ||   \
	  [string equal $type "image"] ||    \
	  [string equal $type "window"]} {
	    continue
	}
	if {!$prefs(scaleCommonCG)} {
	    foreach {left top right bottom} [$wcan bbox $id] break
	    set cgx [expr {($left + $right)/2.0}]
	    set cgy [expr {($top + $bottom)/2.0}]
	}
	lappend cmdList [list scale $utag $cgx $cgy $factor $factor]
	lappend undoList [list scale $utag $cgx $cgy $invfactor $invfactor]
    }    
    set redo [list ::CanvasUtils::CommandList $w $cmdList]
    set undo [list ::CanvasUtils::CommandList $w $undoList]
    eval $redo
    undo::add [::WB::GetUndoToken $wcan] $undo $redo

    ::CanvasFile::SetUnsaved $wcan

    # New markers.
    foreach id $ids {
	::CanvasDraw::DeleteSelection $wcan $id
	::CanvasDraw::MarkBbox $wcan 1 $id
    }
}

# CanvasCmd::FlipItem --
#
#

proc ::CanvasCmd::FlipItem {wcan direction} {

    set w [winfo toplevel $wcan]
    set id [$wcan find withtag selected]
    if {[llength $id] != 1} {
	return
    }
    set theType [$wcan type $id]
    if {![string equal $theType "line"] &&  \
      ![string equal $theType "polygon"]} {
	return
    }
    set utag [::CanvasUtils::GetUtag $wcan $id]
    foreach {left top right bottom} [$wcan bbox $id] break
    set xmid [expr {($left + $right)/2}]
    set ymid [expr {($top + $bottom)/2}]
    set flipco {}
    set coords [$wcan coords $id]
    if {[string equal $direction "horizontal"]} {
	foreach {x y} $coords {
	    lappend flipco [expr {2*$xmid - $x}] $y
	}	
    } elseif {[string equal $direction "vertical"]} {
	foreach {x y} $coords {
	    lappend flipco $x [expr {2*$ymid - $y}]
	}	
    }
    set cmd [concat coords $utag $flipco]
    set undocmd [concat coords $utag $coords]
    set redo [list ::CanvasUtils::Command $w $cmd]
    set undo [list ::CanvasUtils::Command $w $undocmd]
    eval $redo
    undo::add [::WB::GetUndoToken $wcan] $undo $redo

    ::CanvasFile::SetUnsaved $wcan

    # New markers.
    ::CanvasDraw::DeleteSelection $wcan $id
    ::CanvasDraw::MarkBbox $wcan 1 $id
}

proc ::CanvasCmd::Undo {wcan} {
    
    # Make the text stuff in sync.
    ::CanvasText::EvalBufferedText $wcan
    
    # The actual undo command.
    undo::undo [::WB::GetUndoToken $wcan]
    
    ::CanvasFile::SetUnsaved $wcan
    ::CanvasDraw::SyncMarks $wcan
}

# CanvasCmd::Redo --
#
#       The redo command.

proc ::CanvasCmd::Redo {wcan} {
    undo::redo [::WB::GetUndoToken $wcan]
    ::CanvasFile::SetUnsaved $wcan
}

# CanvasCmd::DoEraseAll --
#
#       Erases all items in canvas except for grids. Deselects all items.
#       
# Arguments:
#       w           canvas widget
#       where    "all": erase this canvas and all others.
#                "remote": erase only client canvases.
#                "local": erase only own canvas.
#       
# Results:
#       all items deleted, propagated to all clients.

proc ::CanvasCmd::DoEraseAll {wcan {where all}} {
    DeselectAll $wcan
    ::CanvasDraw::DeleteIds $wcan [$wcan find all] $where
}

proc ::CanvasCmd::EraseAll {wcan} {	
    foreach id [$wcan find all] {
	$wcan delete $id
    }
}

# CanvasCmd::DoPutCanvasDlg --
#
#       Erases all client canvases, transfers this canvas to all clients.
#       
# Arguments:
#       w           toplevel widget path
#       
# Results:
#       all items deleted, propagated to all clients.

proc ::CanvasCmd::DoPutCanvasDlg {w} {
	
    set wcan [::WB::GetCanvasFromWtop $w]
    set ans [::UI::MessageBox -message  \
      "Warning! Syncing this canvas first erases all client canvases." \
      -icon warning -type okcancel -default ok]
    if {$ans ne "ok"} {
	return
    }
    
    # Erase all other client canvases.
    DoEraseAll $wcan remote

    # Put this canvas to all others.
    ::CanvasCmd::DoPutCanvas $wcan all
}
    
# CanvasCmd::DoPutCanvas --
#   
#       Synchronizes, or puts, this canvas to all others. 
#       It uses a temporary file. Images don't work automatically.
#       If 'toIPnum' then put canvas 'w' only to that ip number.
#       
# Arguments:
#       wcan     the canvas.
#       toIPnum  if ip number given, then put canvas 'w' only to that ip number.
#                else put to all clients.
#       
# Results:
#       .

proc ::CanvasCmd::DoPutCanvas {wcan {toIPnum all}} {
    global  this

    Debug 2 "::CanvasCmd::DoPutCanvas wcan=$wcan, toIPnum=$toIPnum"

    set tmpFile ".tmp[clock seconds].can"
    set absFilePath [file join $this(tmpPath) $tmpFile]

    # Save canvas to temporary file.
    if {[catch {open $absFilePath w} fileId]} {
	::UI::MessageBox -message [mc "Cannot write to %s: %s" $tmpFile $fileId] \
	  -icon error -title [mc "Error"] -type ok
    }
    fconfigure $fileId -encoding utf-8
    ::CanvasFile::CanvasToChannel $wcan $fileId $absFilePath
    catch {close $fileId}

    if {[catch {open $absFilePath r} fileId]} {
	::UI::MessageBox -message [mc "Cannot read %s: %s" $tmpFile $fileId] \
	  -icon error -title [mc "Error"] -type ok
    }
    fconfigure $fileId -encoding utf-8
    
    # Distribute to all other client canvases.
    if {$toIPnum eq "all"} {
	::CanvasFile::FileToCanvas $wcan $fileId $absFilePath -where remote
    } else {
	::CanvasFile::FileToCanvas $wcan $fileId $absFilePath -where $toIPnum
    }
    catch {close $fileId}

    # Finally delete the temporary file.
    file delete $absFilePath
}


# CanvasCmd::DoSendCanvas --
#
#       Puts the complete canvas to remote client(s).
#       Does not use a temporary file. Does not add anything to undo stack.
#       Needed because jabber should get everything in single batch.
#       Lets remote client get binary entities (images ...) via http.

proc ::CanvasCmd::DoSendCanvas {w} {
    
    set wcan [::WB::GetCanvasFromWtop $w]
    set cmdList [::CanvasUtils::GetCompleteCanvas $wcan]
        
    # Just invoke the send message hook.
    ::WB::SendMessageList $w $cmdList -force 1
}

#-------------------------------------------------------------------------------