OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [start/] [insight/] [libgui/] [src/] [tkTable.tcl] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
# table.tcl --
2
#
3
# version 1.8, jeff.hobbs@acm.org
4
# This file defines the default bindings for Tk table widgets
5
# and provides procedures that help in implementing those bindings.
6
#
7
 
8
#--------------------------------------------------------------------------
9
# tkPriv elements used in this file:
10
#
11
# afterId -             Token returned by "after" for autoscanning.
12
# tablePrev -           The last element to be selected or deselected
13
#                       during a selection operation.
14
#--------------------------------------------------------------------------
15
 
16
# tkTableClipboardKeysyms --
17
# This procedure is invoked to identify the keys that correspond to
18
# the "copy", "cut", and "paste" functions for the clipboard.
19
#
20
# Arguments:
21
# copy -        Name of the key (keysym name plus modifiers, if any,
22
#               such as "Meta-y") used for the copy operation.
23
# cut -         Name of the key used for the cut operation.
24
# paste -       Name of the key used for the paste operation.
25
 
26
proc tkTableClipboardKeysyms {copy cut paste} {
27
    bind Table <$copy>  {tk_tableCopy %W}
28
    bind Table <$cut>   {tk_tableCut %W}
29
    bind Table <$paste> {tk_tablePaste %W}
30
}
31
 
32
## Interactive row resizing, affected by -resizeborders option
33
##
34
bind Table <3>          {
35
    ## You might want to check for row returned if you want to
36
    ## restrict the resizing of certain rows
37
    %W border mark %x %y
38
}
39
bind Table <B3-Motion>  { %W border dragto %x %y }
40
 
41
## Button events
42
 
43
bind Table <1> {
44
    if {[winfo exists %W]} {
45
        tkTableBeginSelect %W [%W index @%x,%y]
46
        focus %W
47
    }
48
}
49
bind Table <B1-Motion> {
50
    array set tkPriv {x %x y %y}
51
    tkTableMotion %W [%W index @%x,%y]
52
}
53
bind Table <Double-1> {
54
    # empty
55
}
56
bind Table <ButtonRelease-1> {
57
    if {[winfo exists %W]} {
58
        tkCancelRepeat
59
        %W activate @%x,%y
60
    }
61
}
62
 
63
bind Table <Shift-1>    {tkTableBeginExtend %W [%W index @%x,%y]}
64
bind Table <Control-1>  {tkTableBeginToggle %W [%W index @%x,%y]}
65
bind Table <B1-Enter>   {tkCancelRepeat}
66
bind Table <B1-Leave>   {
67
    array set tkPriv {x %x y %y}
68
    tkTableAutoScan %W
69
}
70
bind Table <2> {
71
    %W scan mark %x %y
72
    array set tkPriv {x %x y %y}
73
    set tkPriv(mouseMoved) 0
74
}
75
bind Table <B2-Motion> {
76
    if {(%x != $tkPriv(x)) || (%y != $tkPriv(y))} { set tkPriv(mouseMoved) 1 }
77
    if $tkPriv(mouseMoved) { %W scan dragto %x %y }
78
}
79
bind Table <ButtonRelease-2> {
80
    if {!$tkPriv(mouseMoved)} { tk_tablePaste %W [%W index @%x,%y] }
81
}
82
 
83
## Key events
84
 
85
if {[string comp {} [info command event]]} {
86
    tkTableClipboardKeysyms <Copy> <Cut> <Paste>
87
} else {
88
    tkTableClipboardKeysyms Control-c Control-x Control-v
89
}
90
 
91
bind Table <Any-Tab> {
92
    # empty to allow Tk focus movement
93
}
94
# This forces a cell commit if an active cell exists
95
# Remove this if you don't want cell commit to occur
96
# on every FocusOut
97
bind Table <FocusOut> {
98
    catch {%W activate active}
99
}
100
bind Table <Shift-Up>           {tkTableExtendSelect %W -1  0}
101
bind Table <Shift-Down>         {tkTableExtendSelect %W  1  0}
102
bind Table <Shift-Left>         {tkTableExtendSelect %W  0 -1}
103
bind Table <Shift-Right>        {tkTableExtendSelect %W  0  1}
104
bind Table <Prior>              {%W yview scroll -1 pages; %W activate @0,0}
105
bind Table <Next>               {%W yview scroll  1 pages; %W activate @0,0}
106
bind Table <Control-Prior>      {%W xview scroll -1 pages}
107
bind Table <Control-Next>       {%W xview scroll  1 pages}
108
bind Table <Home>               {%W see origin}
109
bind Table <End>                {%W see end}
110
bind Table <Control-Home> {
111
    %W selection clear all
112
    %W activate origin
113
    %W selection set active
114
    %W see active
115
}
116
bind Table <Control-End> {
117
    %W selection clear all
118
    %W activate end
119
    %W selection set active
120
    %W see active
121
}
122
bind Table <Shift-Control-Home> {tkTableDataExtend %W origin}
123
bind Table <Shift-Control-End>  {tkTableDataExtend %W end}
124
bind Table <Select>             {tkTableBeginSelect %W [%W index active]}
125
bind Table <Shift-Select>       {tkTableBeginExtend %W [%W index active]}
126
bind Table <Control-slash>      {tkTableSelectAll %W}
127
bind Table <Control-backslash> {
128
    if {[string match browse [%W cget -selectmode]]} {%W selection clear all}
129
}
130
bind Table <Up>                 {tkTableMoveCell %W -1  0}
131
bind Table <Down>               {tkTableMoveCell %W  1  0}
132
bind Table <Left>               {tkTableMoveCell %W  0 -1}
133
bind Table <Right>              {tkTableMoveCell %W  0  1}
134
bind Table <Any-KeyPress> {
135
    if {[string compare {} %A]} { %W insert active insert %A }
136
}
137
bind Table <BackSpace> {
138
    set tkPriv(junk) [%W icursor]
139
    if {[string compare {} $tkPriv(junk)] && $tkPriv(junk)} {
140
        %W delete active [expr {$tkPriv(junk)-1}]
141
    }
142
}
143
bind Table <Delete>             {%W delete active insert}
144
bind Table <Escape>             {%W reread}
145
 
146
#bind Table <Return>            {tkTableMoveCell %W 1 0}
147
bind Table <Return> {
148
    %W insert active insert "\n"
149
}
150
 
151
bind Table <Control-Left>       {%W icursor [expr {[%W icursor]-1}]}
152
bind Table <Control-Right>      {%W icursor [expr {[%W icursor]+1}]}
153
bind Table <Control-e>          {%W icursor end}
154
bind Table <Control-a>          {%W icursor 0}
155
bind Table <Control-k>          {%W delete active insert end}
156
bind Table <Control-equal>      {tkTableChangeWidth %W active  1}
157
bind Table <Control-minus>      {tkTableChangeWidth %W active -1}
158
 
159
# tkTableBeginSelect --
160
#
161
# This procedure is typically invoked on button-1 presses. It begins
162
# the process of making a selection in the table. Its exact behavior
163
# depends on the selection mode currently in effect for the table;
164
# see the Motif documentation for details.
165
#
166
# Arguments:
167
# w     - The table widget.
168
# el    - The element for the selection operation (typically the
169
#       one under the pointer).  Must be in row,col form.
170
 
171
proc tkTableBeginSelect {w el} {
172
    global tkPriv
173
    if {[scan $el %d,%d r c] != 2} return
174
    switch [$w cget -selectmode] {
175
        multiple {
176
            if {[$w tag includes title $el]} {
177
                ## in the title area
178
                if {$r < [$w cget -titlerows]+[$w cget -roworigin]} {
179
                    ## We're in a column header
180
                    if {$c < [$w cget -titlecols]+[$w cget -colorigin]} {
181
                        ## We're in the topleft title area
182
                        set inc topleft
183
                        set el2 end
184
                    } else {
185
                        set inc [$w index topleft row],$c
186
                        set el2 [$w index end row],$c
187
                    }
188
                } else {
189
                    ## We're in a row header
190
                    set inc $r,[$w index topleft col]
191
                    set el2 $r,[$w index end col]
192
                }
193
            } else {
194
                set inc $el
195
                set el2 $el
196
            }
197
            if [$w selection includes $inc] {
198
                $w selection clear $el $el2
199
            } else {
200
                $w selection set $el $el2
201
            }
202
        }
203
        extended {
204
            $w selection clear all
205
            if {[$w tag includes title $el]} {
206
                if {$r < [$w cget -titlerows]+[$w cget -roworigin]} {
207
                    ## We're in a column header
208
                    if {$c < [$w cget -titlecols]+[$w cget -colorigin]} {
209
                        $w selection set origin end
210
                    } else {
211
                        $w selection set $el [$w index end row],$c
212
                    }
213
                } else {
214
                    ## We're in a row header
215
                    $w selection set $el $r,[$w index end col]
216
                }
217
            } else {
218
                $w selection set $el
219
            }
220
            $w selection anchor $el
221
            set tkPriv(tablePrev) $el
222
        }
223
        default {
224
            if {![$w tag includes title $el]} {
225
                $w selection clear all
226
                $w selection set $el
227
                set tkPriv(tablePrev) $el
228
            }
229
            $w selection anchor $el
230
        }
231
    }
232
}
233
 
234
# tkTableMotion --
235
#
236
# This procedure is called to process mouse motion events while
237
# button 1 is down. It may move or extend the selection, depending
238
# on the table's selection mode.
239
#
240
# Arguments:
241
# w     - The table widget.
242
# el    - The element under the pointer (must be in row,col form).
243
 
244
proc tkTableMotion {w el} {
245
    global tkPriv
246
    if {![info exists tkPriv(tablePrev)]} {
247
        set tkPriv(tablePrev) $el
248
        return
249
    }
250
    if {[string match $tkPriv(tablePrev) $el]} return
251
    switch [$w cget -selectmode] {
252
        browse {
253
            $w selection clear all
254
            $w selection set $el
255
            set tkPriv(tablePrev) $el
256
        }
257
        extended {
258
            scan $tkPriv(tablePrev) %d,%d r c
259
            scan $el %d,%d elr elc
260
            if {[$w tag includes title $el]} {
261
                if {$r < [$w cget -titlerows]+[$w cget -roworigin]} {
262
                    ## We're in a column header
263
                    if {$c < [$w cget -titlecols]+[$w cget -colorigin]} {
264
                        ## We're in the topleft title area
265
                        $w selection clear anchor end
266
                    } else {
267
                        $w selection clear anchor [$w index end row],$c
268
                    }
269
                    $w selection set anchor [$w index end row],$elc
270
                } else {
271
                    ## We're in a row header
272
                    $w selection clear anchor $r,[$w index end col]
273
                    $w selection set anchor $elr,[$w index end col]
274
                }
275
            } else {
276
                $w selection clear anchor $tkPriv(tablePrev)
277
                $w selection set anchor $el
278
            }
279
            set tkPriv(tablePrev) $el
280
        }
281
    }
282
}
283
 
284
# tkTableBeginExtend --
285
#
286
# This procedure is typically invoked on shift-button-1 presses. It
287
# begins the process of extending a selection in the table. Its
288
# exact behavior depends on the selection mode currently in effect
289
# for the table; see the Motif documentation for details.
290
#
291
# Arguments:
292
# w - The table widget.
293
# el - The element for the selection operation (typically the
294
# one under the pointer). Must be in numerical form.
295
 
296
proc tkTableBeginExtend {w el} {
297
    if {[string match extended [$w cget -selectmode]] &&
298
        [$w selection includes anchor]} {
299
        tkTableMotion $w $el
300
    }
301
}
302
 
303
# tkTableBeginToggle --
304
#
305
# This procedure is typically invoked on control-button-1 presses. It
306
# begins the process of toggling a selection in the table. Its
307
# exact behavior depends on the selection mode currently in effect
308
# for the table; see the Motif documentation for details.
309
#
310
# Arguments:
311
# w - The table widget.
312
# el - The element for the selection operation (typically the
313
# one under the pointer). Must be in numerical form.
314
 
315
proc tkTableBeginToggle {w el} {
316
    global tkPriv
317
    if {[string match extended [$w cget -selectmode]]} {
318
        set tkPriv(tablePrev) $el
319
        $w selection anchor $el
320
        if [$w selection includes $el] {
321
            $w selection clear $el
322
        } else {
323
            $w selection set $el
324
        }
325
    }
326
}
327
 
328
# tkTableAutoScan --
329
# This procedure is invoked when the mouse leaves an entry window
330
# with button 1 down. It scrolls the window up, down, left, or
331
# right, depending on where the mouse left the window, and reschedules
332
# itself as an "after" command so that the window continues to scroll until
333
# the mouse moves back into the window or the mouse button is released.
334
#
335
# Arguments:
336
# w - The entry window.
337
 
338
proc tkTableAutoScan {w} {
339
    global tkPriv
340
    if {![winfo exists $w]} return
341
    set x $tkPriv(x)
342
    set y $tkPriv(y)
343
    if {$y >= [winfo height $w]} {
344
        $w yview scroll 1 units
345
    } elseif {$y < 0} {
346
        $w yview scroll -1 units
347
    } elseif {$x >= [winfo width $w]} {
348
        $w xview scroll 1 units
349
    } elseif {$x < 0} {
350
        $w xview scroll -1 units
351
    } else {
352
        return
353
    }
354
    tkTableMotion $w [$w index @$x,$y]
355
    set tkPriv(afterId) [after 50 tkTableAutoScan $w]
356
}
357
 
358
# tkTableMoveCell --
359
#
360
# Moves the location cursor (active element) by the specified number
361
# of cells and changes the selection if we're in browse or extended
362
# selection mode.
363
#
364
# Arguments:
365
# w - The table widget.
366
# x - +1 to move down one cell, -1 to move up one cell.
367
# y - +1 to move right one cell, -1 to move left one cell.
368
 
369
proc tkTableMoveCell {w x y} {
370
    global tkPriv
371
    if {[catch {$w index active row} r]} return
372
    set c [$w index active col]
373
    $w activate [incr r $x],[incr c $y]
374
    $w see active
375
    switch [$w cget -selectmode] {
376
        browse {
377
            $w selection clear all
378
            $w selection set active
379
        }
380
        extended {
381
            $w selection clear all
382
            $w selection set active
383
            $w selection anchor active
384
            set tkPriv(tablePrev) [$w index active]
385
        }
386
    }
387
}
388
 
389
# tkTableExtendSelect --
390
#
391
# Does nothing unless we're in extended selection mode; in this
392
# case it moves the location cursor (active element) by the specified
393
# number of cells, and extends the selection to that point.
394
#
395
# Arguments:
396
# w - The table widget.
397
# x - +1 to move down one cell, -1 to move up one cell.
398
# y - +1 to move right one cell, -1 to move left one cell.
399
 
400
proc tkTableExtendSelect {w x y} {
401
    if {[string compare extended [$w cget -selectmode]] ||
402
        [catch {$w index active row} r]} return
403
    set c [$w index active col]
404
    $w activate [incr r $x],[incr c $y]
405
    $w see active
406
    tkTableMotion $w [$w index active]
407
}
408
 
409
# tkTableDataExtend
410
#
411
# This procedure is called for key-presses such as Shift-KEndData.
412
# If the selection mode isnt multiple or extend then it does nothing.
413
# Otherwise it moves the active element to el and, if we're in
414
# extended mode, extends the selection to that point.
415
#
416
# Arguments:
417
# w - The table widget.
418
# el - An integer cell number.
419
 
420
proc tkTableDataExtend {w el} {
421
    set mode [$w cget -selectmode]
422
    if {[string match extended $mode]} {
423
        $w activate $el
424
        $w see $el
425
        if [$w selection includes anchor] {tkTableMotion $w $el}
426
    } elseif {[string match multiple $mode]} {
427
        $w activate $el
428
        $w see $el
429
    }
430
}
431
 
432
# tkTableSelectAll
433
#
434
# This procedure is invoked to handle the "select all" operation.
435
# For single and browse mode, it just selects the active element.
436
# Otherwise it selects everything in the widget.
437
#
438
# Arguments:
439
# w - The table widget.
440
 
441
proc tkTableSelectAll {w} {
442
    if {[regexp {^(single|browse)$} [$w cget -selectmode]]} {
443
        $w selection clear all
444
        $w selection set active
445
        tkTableHandleType $w [$w index active]
446
    } else {
447
        $w selection set origin end
448
    }
449
}
450
 
451
# tkTableChangeWidth --
452
# Adjust the widget of the specified cell by $a.
453
#
454
# Arguments:
455
# w - The table widget.
456
# i - cell index
457
# a - amount to adjust by
458
 
459
proc tkTableChangeWidth {w i a} {
460
    set tmp [$w index $i col]
461
    if {[set width [$w width $tmp]] >= 0} {
462
        $w width $tmp [incr width $a]
463
    } else {
464
        $w width $tmp [incr width -$a]
465
    }
466
}
467
 
468
# tk_tableCopy --
469
# This procedure copies the selection from a table widget into the
470
# clipboard.
471
#
472
# Arguments:
473
# w -           Name of a table widget.
474
 
475
proc tk_tableCopy w {
476
    if {[selection own -displayof $w] == "$w"} {
477
        clipboard clear -displayof $w
478
        catch {clipboard append -displayof $w [selection get -displayof $w]}
479
    }
480
}
481
 
482
# tk_tableCut --
483
# This procedure copies the selection from a table widget into the
484
# clipboard, then deletes the selection (if it exists in the given
485
# widget).
486
#
487
# Arguments:
488
# w -           Name of a table widget.
489
 
490
proc tk_tableCut w {
491
    if {[selection own -displayof $w] == "$w"} {
492
        clipboard clear -displayof $w
493
        catch {
494
            clipboard append -displayof $w [selection get -displayof $w]
495
            $w cursel set {}
496
            $w selection clear all
497
        }
498
    }
499
}
500
 
501
# tk_tablePaste --
502
# This procedure pastes the contents of the clipboard to the specified
503
# cell (active by default) in a table widget.
504
#
505
# Arguments:
506
# w -           Name of a table widget.
507
# cell -        Cell to start pasting in.
508
 
509
proc tk_tablePaste {w {cell {}}} {
510
    if {[string compare {} $cell]} {
511
        if {[catch {selection get -displayof $w} data]} return
512
    } else {
513
        if {[catch {selection get -displayof $w -selection CLIPBOARD} data]} {
514
            return
515
        }
516
        set cell active
517
    }
518
    tk_tablePasteHandler $w [$w index $cell] $data
519
    if {[$w cget -state] == "normal"} {focus $w}
520
}
521
 
522
# tk_tablePasteHandler --
523
# This procedure handles how data is pasted into the table widget.
524
# This handles data in the default table selection form.
525
# NOTE: this allows pasting into all cells, even those with -state disabled
526
#
527
# Arguments:
528
# w -           Name of a table widget.
529
# cell -        Cell to start pasting in.
530
 
531
proc tk_tablePasteHandler {w cell data} {
532
    set rows    [expr {[$w cget -rows]-[$w cget -roworigin]}]
533
    set cols    [expr {[$w cget -cols]-[$w cget -colorigin]}]
534
    set r       [$w index $cell row]
535
    set c       [$w index $cell col]
536
    set rsep    [$w cget -rowseparator]
537
    set csep    [$w cget -colseparator]
538
    ## Assume separate rows are split by row separator if specified
539
    ## If you were to want multi-character row separators, you would need:
540
    # regsub -all $rsep $data <newline> data
541
    # set data [join $data <newline>]
542
    if {[string comp {} $rsep]} { set data [split $data $rsep] }
543
    set row     $r
544
    foreach line $data {
545
        if {$row > $rows} break
546
        set col $c
547
        ## Assume separate cols are split by col separator if specified
548
        ## Unless a -separator was specified
549
        if {[string comp {} $csep]} { set line [split $line $csep] }
550
        ## If you were to want multi-character col separators, you would need:
551
        # regsub -all $csep $line <newline> line
552
        # set line [join $line <newline>]
553
        foreach item $line {
554
            if {$col > $cols} break
555
            $w set $row,$col $item
556
            incr col
557
        }
558
        incr row
559
    }
560
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.