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

Subversion Repositories or1k_old

[/] [or1k_old/] [tags/] [start/] [insight/] [itcl/] [iwidgets3.0.0/] [generic/] [disjointlistbox.itk] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
#
2
# ::iwidgets::Disjointlistbox
3
# ----------------------------------------------------------------------
4
# Implements a widget which maintains a disjoint relationship between
5
# the items displayed by two listboxes.  The disjointlistbox is composed
6
# of 2 Scrolledlistboxes,  2 Pushbuttons, and 2 labels.
7
#
8
# The disjoint behavior of this widget exists between the two Listboxes,
9
# That is, a given instance of a ::iwidgets::Disjointlistbox will never
10
# exist which has Listbox widgets with items in common.
11
#
12
# Users may transfer items between the two Listbox widgets using the
13
# the two Pushbuttons.
14
#
15
# The options include the ability to configure the "items" displayed by
16
# either of the two Listboxes and to control the placement of the insertion
17
# and removal buttons.
18
#
19
# The following depicts the allowable "-buttonplacement" option values
20
# and their associated layout:
21
#
22
#   "-buttonplacement" => center
23
#
24
#   --------------------------
25
#   |listbox|        |listbox|
26
#   |       |________|       |
27
#   | (LHS) | button | (RHS) |
28
#   |       |========|       |
29
#   |       | button |       |
30
#   |_______|--------|_______|
31
#   | count |        | count |
32
#   --------------------------
33
#
34
#   "-buttonplacement" => bottom
35
#
36
#   ---------------------
37
#   | listbox | listbox |
38
#   |  (LHS)  |  (RHS)  |
39
#   |_________|_________|
40
#   | button  | button  |
41
#   |---------|---------|
42
#   | count   | count   |
43
#   ---------------------
44
#
45
# ----------------------------------------------------------------------
46
#  AUTHOR: John A. Tucker               EMAIL: jatucker@spd.dsccc.com
47
#
48
# ======================================================================
49
 
50
#
51
# Default resources.
52
#
53
 
54
set tk_strictMotif 1
55
 
56
option add *Disjointlistbox.lhsLabelText        Available       widgetDefault
57
option add *Disjointlistbox.rhsLabelText        Current         widgetDefault
58
option add *Disjointlistbox.lhsButtonLabel      {Insert >>}     widgetDefault
59
option add *Disjointlistbox.rhsButtonLabel      {<< Remove}     widgetDefault
60
option add *Disjointlistbox.vscrollMode         static          widgetDefault
61
option add *Disjointlistbox.hscrollMode         static          widgetDefault
62
option add *Disjointlistbox.selectMode          multiple        widgetDefault
63
option add *Disjointlistbox.labelPos            nw              widgetDefault
64
option add *Disjointlistbox.buttonPlacement     bottom          widgetDefault
65
 
66
 
67
#
68
# Usual options.
69
#
70
itk::usual Disjointlistbox {
71
  keep -background -textbackground -cursor \
72
       -foreground -textfont -labelfont
73
}
74
 
75
 
76
# ----------------------------------------------------------------------
77
# ::iwidgets::Disjointlistbox
78
# ----------------------------------------------------------------------
79
class ::iwidgets::Disjointlistbox {
80
 
81
  inherit itk::Widget
82
 
83
  #
84
  # options
85
  #
86
  itk_option define -buttonplacement buttonPlacement ButtonPlacement bottom
87
  itk_option define -lhsbuttonlabel lhsButtonLabel LabelText {Insert >>}
88
  itk_option define -rhsbuttonlabel rhsButtonLabel LabelText {<< Remove}
89
 
90
  constructor {args} {}
91
 
92
  #
93
  # PUBLIC
94
  #
95
  public {
96
    method clear {}
97
    method getlhs {{first 0} {last end}}
98
    method getrhs {{first 0} {last end}}
99
    method lhs {args}
100
    method insertlhs {items}
101
    method insertrhs {items}
102
    method setlhs {items}
103
    method setrhs {items}
104
    method rhs {args}
105
  }
106
 
107
  #
108
  # PROTECTED
109
  #
110
  protected {
111
    method insert {theListbox items}
112
    method listboxClick {clickSide otherSide}
113
    method listboxDblClick {clickSide otherSide}
114
    method remove {theListbox items}
115
    method showCount {}
116
    method transfer {}
117
 
118
    variable sourceListbox {}
119
    variable destinationListbox {}
120
  }
121
}
122
 
123
#
124
# Provide a lowercased access method for the ::iwidgets::Disjointlistbox class.
125
#
126
proc ::iwidgets::disjointlistbox {pathName args} {
127
    uplevel ::iwidgets::Disjointlistbox $pathName $args
128
}
129
 
130
# ------------------------------------------------------------------
131
#
132
# Method: Constructor
133
#
134
# Purpose:
135
#
136
body ::iwidgets::Disjointlistbox::constructor {args} {
137
  #
138
  # Create the left-most Listbox
139
  #
140
  itk_component add lhs {
141
    iwidgets::Scrolledlistbox $itk_interior.lhs \
142
      -selectioncommand [code $this listboxClick lhs rhs] \
143
      -dblclickcommand [code $this listboxDblClick lhs rhs]
144
  } {
145
    usual
146
    keep -selectmode -vscrollmode -hscrollmode
147
    rename -labeltext -lhslabeltext lhsLabelText LabelText
148
  }
149
 
150
  #
151
  # Create the right-most Listbox
152
  #
153
  itk_component add rhs {
154
    iwidgets::Scrolledlistbox $itk_interior.rhs \
155
      -selectioncommand [code $this listboxClick rhs lhs] \
156
      -dblclickcommand [code $this listboxDblClick rhs lhs]
157
  } {
158
    usual
159
    keep -selectmode -vscrollmode -hscrollmode
160
    rename -labeltext -rhslabeltext rhsLabelText LabelText
161
  }
162
 
163
  #
164
  # Create the left-most item count Label
165
  #
166
  itk_component add lhsCount {
167
    label $itk_interior.lhscount
168
  } {
169
    usual
170
    rename -font -labelfont labelFont Font
171
  }
172
 
173
  #
174
  # Create the right-most item count Label
175
  #
176
  itk_component add rhsCount {
177
    label $itk_interior.rhscount
178
  } {
179
    usual
180
    rename -font -labelfont labelFont Font
181
  }
182
 
183
  set sourceListbox $itk_component(lhs)
184
  set destinationListbox $itk_component(rhs)
185
 
186
  #
187
  # Bind the "showCount" method to the Map event of one of the labels
188
  # to keep the diplayed item count current.
189
  #
190
  bind $itk_component(lhsCount)  [code $this showCount]
191
 
192
  grid $itk_component(lhs) -row 0 -column 0 -sticky nsew
193
  grid $itk_component(rhs) -row 0 -column 2 -sticky nsew
194
 
195
  grid rowconfigure    $itk_interior 0 -weight 1
196
  grid columnconfigure $itk_interior 0 -weight 1
197
  grid columnconfigure $itk_interior 2 -weight 1
198
 
199
  eval itk_initialize $args
200
}
201
 
202
# ------------------------------------------------------------------
203
# Method:  listboxClick
204
#
205
# Purpose: Evaluate a single click make in the specified Listbox.
206
#
207
body ::iwidgets::Disjointlistbox::listboxClick {clickSide otherSide} {
208
    set button "button"
209
    $itk_component($clickSide$button) configure -state active
210
    $itk_component($otherSide$button) configure -state disabled
211
    set sourceListbox      $itk_component($clickSide)
212
    set destinationListbox $itk_component($otherSide)
213
}
214
 
215
# ------------------------------------------------------------------
216
# Method:  listboxDblClick
217
#
218
# Purpose: Evaluate a double click in the specified Listbox.
219
#
220
body ::iwidgets::Disjointlistbox::listboxDblClick {clickSide otherSide} {
221
  listboxClick $clickSide $otherSide
222
  transfer
223
}
224
 
225
# ------------------------------------------------------------------
226
# Method:  transfer
227
#
228
# Purpose: Transfer source Listbox items to destination Listbox
229
#
230
body ::iwidgets::Disjointlistbox::transfer {} {
231
 
232
  if {[$sourceListbox selecteditemcount] == 0} {
233
    return
234
  }
235
  set selectedindices [lsort -integer -decreasing [$sourceListbox curselection]]
236
  set selecteditems [$sourceListbox getcurselection]
237
 
238
  foreach index $selectedindices {
239
    $sourceListbox delete $index
240
  }
241
 
242
  foreach item $selecteditems {
243
    $destinationListbox insert end $item
244
  }
245
  $destinationListbox sort increasing
246
 
247
  showCount
248
}
249
 
250
# ------------------------------------------------------------------
251
# Method: getlhs
252
#
253
# Purpose: Retrieve the items of the left Listbox widget
254
#
255
body ::iwidgets::Disjointlistbox::getlhs {{first 0} {last end}} {
256
  return [lhs get $first $last]
257
}
258
 
259
# ------------------------------------------------------------------
260
# Method: getrhs
261
#
262
# Purpose: Retrieve the items of the right Listbox widget
263
#
264
body ::iwidgets::Disjointlistbox::getrhs {{first 0} {last end}} {
265
  return [rhs get $first $last]
266
}
267
 
268
# ------------------------------------------------------------------
269
# Method: insertrhs
270
#
271
# Purpose: Insert items into the right Listbox widget
272
#
273
body ::iwidgets::Disjointlistbox::insertrhs {items} {
274
  remove $itk_component(lhs) $items
275
  insert $itk_component(rhs) $items
276
}
277
 
278
# ------------------------------------------------------------------
279
# Method: insertlhs
280
#
281
# Purpose: Insert items into the left Listbox widget
282
#
283
body ::iwidgets::Disjointlistbox::insertlhs {items} {
284
  remove $itk_component(rhs) $items
285
  insert $itk_component(lhs) $items
286
}
287
 
288
# ------------------------------------------------------------------
289
# Method:  clear
290
#
291
# Purpose: Remove the items from the Listbox widgets and set the item count
292
#          Labels text to 0
293
#
294
body ::iwidgets::Disjointlistbox::clear {} {
295
  lhs clear
296
  rhs clear
297
  showCount
298
}
299
 
300
# ------------------------------------------------------------------
301
# Method: insert
302
#
303
# Purpose: Insert the input items into the input Listbox widget while
304
#          maintaining the disjoint property between them.
305
#
306
body ::iwidgets::Disjointlistbox::insert {theListbox items} {
307
 
308
  set curritems [$theListbox get 0 end]
309
 
310
  foreach item $items {
311
    #
312
    # if the item is not already present in the Listbox then insert it
313
    #
314
    if {[lsearch -exact $curritems $item] == -1} {
315
      $theListbox insert end $item
316
    }
317
  }
318
  $theListbox sort increasing
319
  showCount
320
}
321
 
322
# ------------------------------------------------------------------
323
# Method: remove
324
#
325
# Purpose: Remove the input items from the input Listbox widget while
326
#          maintaining the disjoint property between them.
327
#
328
body ::iwidgets::Disjointlistbox::remove {theListbox items} {
329
 
330
  set indexes {}
331
  set curritems [$theListbox get 0 end]
332
 
333
  foreach item $items {
334
    #
335
    # if the item is in the listbox then add its index to the index list
336
    #
337
    if {[set index [lsearch -exact $curritems $item]] != -1} {
338
      lappend indexes $index
339
    }
340
  }
341
 
342
  foreach index [lsort -integer -decreasing $indexes] {
343
    $theListbox delete $index
344
  }
345
  showCount
346
}
347
 
348
# ------------------------------------------------------------------
349
# Method: showCount
350
#
351
# Purpose: Set the text of the item count Labels.
352
#
353
body ::iwidgets::Disjointlistbox::showCount {} {
354
  $itk_component(lhsCount) config -text "item count: [lhs size]"
355
  $itk_component(rhsCount) config -text "item count: [rhs size]"
356
}
357
 
358
# ------------------------------------------------------------------
359
# METHOD: setlhs
360
#
361
# Set the items of the left-most Listbox with the input list
362
# option.  Remove all (if any) items from the right-most Listbox
363
# which exist in the input list option to maintain the disjoint
364
# property between the two
365
#
366
body ::iwidgets::Disjointlistbox::setlhs {items} {
367
  lhs clear
368
  insertlhs $items
369
}
370
 
371
# ------------------------------------------------------------------
372
# METHOD: setrhs
373
#
374
# Set the items of the right-most Listbox with the input list
375
# option.  Remove all (if any) items from the left-most Listbox
376
# which exist in the input list option to maintain the disjoint
377
# property between the two
378
#
379
body ::iwidgets::Disjointlistbox::setrhs {items} {
380
  rhs clear
381
  insertrhs $items
382
}
383
 
384
# ------------------------------------------------------------------
385
# Method:  lhs
386
#
387
# Purpose: Evaluates the specified arguments against the lhs Listbox
388
#
389
body ::iwidgets::Disjointlistbox::lhs {args} {
390
  return [eval $itk_component(lhs) $args]
391
}
392
 
393
# ------------------------------------------------------------------
394
# Method:  rhs
395
#
396
# Purpose: Evaluates the specified arguments against the rhs Listbox
397
#
398
body ::iwidgets::Disjointlistbox::rhs {args} {
399
  return [eval $itk_component(rhs) $args]
400
}
401
 
402
# ------------------------------------------------------------------
403
# OPTION: buttonplacement
404
#
405
# Configure the placement of the buttons to be either between or below
406
# the two list boxes.
407
#
408
configbody ::iwidgets::Disjointlistbox::buttonplacement {
409
  if {$itk_option(-buttonplacement) != ""} {
410
 
411
    if { [lsearch [component] lhsbutton] != -1 } {
412
      eval destroy $itk_component(rhsbutton) $itk_component(lhsbutton)
413
    }
414
 
415
    if { [lsearch [component] bbox] != -1 } {
416
      destroy $itk_component(bbox)
417
    }
418
 
419
    set where $itk_option(-buttonplacement)
420
 
421
    switch $where {
422
 
423
      center {
424
        #
425
        # Create the button box frame
426
        #
427
        itk_component add bbox {
428
          frame $itk_interior.bbox
429
        }
430
 
431
        itk_component add lhsbutton {
432
          button $itk_component(bbox).lhsbutton -command [code $this transfer]
433
        } {
434
          usual
435
          rename -text -lhsbuttonlabel lhsButtonLabel LabelText
436
          rename -font -labelfont labelFont Font
437
        }
438
 
439
        itk_component add rhsbutton {
440
          button $itk_component(bbox).rhsbutton -command [code $this transfer]
441
        } {
442
          usual
443
          rename -text -rhsbuttonlabel rhsButtonLabel LabelText
444
          rename -font -labelfont labelFont Font
445
        }
446
 
447
        grid configure $itk_component(lhsCount) -row 1 -column 0 -sticky ew
448
        grid configure $itk_component(rhsCount) -row 1 -column 2 -sticky ew
449
 
450
        grid configure $itk_component(bbox) \
451
              -in $itk_interior -row 0 -column 1 -columnspan 1 -sticky nsew
452
 
453
        grid configure $itk_component(rhsbutton) \
454
              -in $itk_component(bbox) -row 0 -column 0 -sticky ew
455
        grid configure $itk_component(lhsbutton) \
456
              -in $itk_component(bbox) -row 1 -column 0 -sticky ew
457
      }
458
 
459
      bottom {
460
 
461
        itk_component add lhsbutton {
462
          button $itk_interior.lhsbutton -command [code $this transfer]
463
        } {
464
          usual
465
          rename -text -lhsbuttonlabel lhsButtonLabel LabelText
466
          rename -font -labelfont labelFont Font
467
        }
468
 
469
        itk_component add rhsbutton {
470
          button $itk_interior.rhsbutton -command [code $this transfer]
471
        } {
472
          usual
473
          rename -text -rhsbuttonlabel rhsButtonLabel LabelText
474
          rename -font -labelfont labelFont Font
475
        }
476
 
477
        grid $itk_component(lhsCount)  -row 2 -column 0 -sticky ew
478
        grid $itk_component(rhsCount)  -row 2 -column 2 -sticky ew
479
        grid $itk_component(lhsbutton) -row 1 -column 0 -sticky ew
480
        grid $itk_component(rhsbutton) -row 1 -column 2 -sticky ew
481
      }
482
 
483
      default {
484
        error "bad buttonplacement option\"$where\": should be center or bottom"
485
      }
486
    }
487
  }
488
}
489
 

powered by: WebSVN 2.1.0

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