source: tol/tolbase/lib/byswidget/bfieldsdialog/bfieldsdialog.tcl @ 6d7779d

1941-docker-image-to-testdevel
Last change on this file since 6d7779d was 6d7779d, checked in by Jorge Pérez <josp.jorge@…>, 18 years ago

initial import

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@1 be4846ae-f051-483a-831f-b64e7d08527f

  • Property mode set to 100644
File size: 13.8 KB
Line 
1#/////////////////////////////////////////////////////////////////////////////
2# FILE    : bfieldsdialog.tcl
3# PURPOSE : Bayes Fields Dialog. Creates a dialog that has many fields that
4#           could be entries or comboboxes and configurable buttons (accept,
5#           apply , add, info, cancel and exit) and calls an external procedure
6#           with the selected data in the fields
7#/////////////////////////////////////////////////////////////////////////////
8
9package require byscommon
10package require BWidget
11
12#/////////////////////////////////////////////////////////////////////////////
13::snit::widgetadaptor bfieldsdialog {
14#
15# PURPOSE: Beditselector widget class definition
16#
17#/////////////////////////////////////////////////////////////////////////////
18#/////////////////////////////////////////////////////////////////////////////
19# options:
20#  -fields : names of the fields for each entry or combobox
21#  -list  : if a list is provided then a combo box is available to select values
22#           it's in fact a list of lists one for each combo
23#           must be provided at definition time
24#  -width : width of the entries/comboboxes components
25#  -orientation : orientation for the fields, vertical or horizontal
26#  -procedure : this procedure takes two arguments a list and a window path
27#    the list, will contain the fields of an element introduced
28#    the window path is the path of the widget father's
29#    this procedure must do the effective insert command
30#  -initialvalue: initial value to display
31#  -titles: list with titles to show in each one of columns
32#
33# variable:
34#   labels: array of labels
35#   entries: array of entries
36#   combos: array of comboboxes
37#   created: indicates if all components of the widget have been created
38#
39#/////////////////////////////////////////////////////////////////////////////
40
41  option -fields {}
42  option -list {}
43  option -initialvalue ""
44  option -orientation "horizontal"
45  option -width
46  option -procedure ""
47  option -buttons {cancel accept}
48  option -info ""
49  option -titles {}
50 
51  variable labels
52  variable entries
53  variable combos
54
55  variable created
56
57
58  #///////////////////////////////////////////////////////////////////////////
59  constructor { args } {
60  #
61  # PURPOSE: bfieldsdialog constructor
62  #
63  # PARAMETERS:
64  #   args ->
65  #
66  #///////////////////////////////////////////////////////////////////////////
67
68    set list [from args -list {}]
69    set fields [from args -fields {" "}]
70    set titles [from args -titles {}]
71    if {[llength $titles] eq 0} {
72      set titles $fields
73    }
74    if { "" eq $fields } { set fields {" "} }
75    set initialvalue [from args -initialvalue {}]
76
77    # instaling components
78    installhull [Dialog $self -modal local]
79
80    set dialogframe [$self getframe]
81
82    set i 0
83    if { "" eq $list } {
84      foreach col $fields {
85        if {!([lindex $titles $i] eq "")} {
86          install labels($i) using label $dialogframe.l$i -text [lindex $titles $i]
87        }
88        install entries($i) using Entry $dialogframe.e$i \
89          -text [lindex $initialvalue $i]
90        incr i
91      }
92    } else {
93      if {!([lindex $titles $i] eq "")} {
94        install labels($i) using label $dialogframe.l$i -text [lindex $titles $i]
95      }
96      install combos using bcomboboxplus $dialogframe.cb$i \
97                                             -cols $fields \
98                                             -widthlb auto
99      foreach item [lindex $list $i] {
100        $combos insert end $item
101      }
102      $combos selectIndex 2
103      $combos selectIndex 1
104      #When Return is pressed the test in the entry is selected
105      set comboek [$combos getEntryKey]
106      bind $comboek <Return> [list $comboek selection range 0 end]
107    }
108
109    set created 1
110
111    #configuring
112    $self configurelist $args
113    $self configure -list $list
114    $self configure -fields $fields
115    $self configure -initialvalue $initialvalue
116    $self configure -title [mc [$self cget -title]]
117   
118    # adding the buttons
119    set notapply 1
120    foreach button $options(-buttons) {
121      switch -exact $button {
122        info {
123          $self add -image [::Bitmap::get infor] -relief link \
124                -text [mc "Info"] -compound left \
125                -command [list $self _Info] -helptext [mc "Information"]
126        }
127
128        accept {
129          $self add -image [::Bitmap::get accept] -relief link \
130                -text [mc "Accept"] -compound left \
131                -command [list $self _Proc 1] -helptext [mc "Accept element"]
132        }
133   
134        apply {
135          $self add -image [::Bitmap::get apply] -relief link \
136                -text [mc "Apply"] -compound left \
137                -command [list $self _Proc] -helptext [mc "Apply"]
138          set notapply 0
139        }
140        add {
141          $self add -image [::Bitmap::get add] -relief link \
142                -text [mc "Add"] -compound left \
143                -command [list $self _Enter] -helptext [mc "Add element"]
144          set notapply 0
145        }
146
147        addall {
148          $self add -image [::Bitmap::get addall] -relief link \
149                -text [mc "Add all"] -compound left \
150                -command [list $self _Proc 0 1] -helptext [mc "Add all elements"]
151          set notapply 0
152        }
153
154        exit {
155          $self add -image [::Bitmap::get exit] -relief link \
156            -text [mc "Exit"] -compound left \
157            -command [list $self _CloseDialog $self] -helptext [mc "Exit dialog"]
158        }
159       
160        cancel {
161          $self add -image [::Bitmap::get cancel] -relief link \
162              -text [mc "Cancel"] -compound left \
163              -command [list $self _CloseDialog $self] -helptext [mc "Cancel"]
164        }
165      }
166    }
167   
168    if { "" eq $list } {
169      for { set i 0 } { $i < [llength $options(-fields)] } { incr i } {
170        bind $dialogframe.e$i <Key-Return> "$self _Focus; $self _Proc $notapply"
171      }
172    }
173   
174    # packing
175    set i 0
176    if { "" eq $list } {
177      foreach col $fields {
178        if { $options(-orientation) eq "horizontal" } {
179          grid $labels($i)  -row 0 -column [expr $i * 2]    -padx 4
180          grid $entries($i) -row 0 -column [expr $i * 2 +1] -padx 4
181        } else {
182          grid $labels($i)  -row $i -column 0 -pady 4 -sticky w
183          grid $entries($i) -row $i -column 1 -pady 4 -sticky we
184          grid columnconfigure $dialogframe 0 -weight 0
185          grid columnconfigure $dialogframe 1 -weight 1
186        }
187        incr i
188      }
189    } else  {
190      if { $options(-orientation) eq "horizontal" } {
191        grid $labels($i) -row 0 -column [expr $i * 2]     -padx 4
192        grid $combos -row 0 -column [expr $i * 2 + 1] -padx 4
193      } else {
194        grid $labels($i) -row $i -column 0 -sticky w
195        grid $combos -row $i -column 1 -sticky we
196        grid columnconfigure $dialogframe 0 -weight 0
197        grid columnconfigure $dialogframe 1 -weight 1
198      }
199    }
200   
201    #The selected field is proccesed and the window is not closed
202    bind $self <Return> [list $self _Enter]
203    bind $self <Escape> [list $self _CloseDialog $self]
204  }
205 
206  #///////////////////////////////////////////////////////////////////////////
207  #
208  # PURPOSE: Metodos cget y configure
209  #
210  #///////////////////////////////////////////////////////////////////////////
211 
212  onconfigure -width { w } {
213    if { $created } {
214      for { set i 0 } { $i < [llength $options(-fields)] } { incr i  } {
215        if { $options(-list) eq "" } {
216          $entries($i) configure -width $w
217        } else {
218          $combos  configure -width $w
219        }
220      }
221      set options(-width) $w
222    }
223  }
224
225  onconfigure -initialvalue { values } {
226    if { $created } {
227      for { set i 0 } { $i < [llength $options(-fields)] } { incr i } {
228        if { $options(-list) eq "" } {
229          $entries($i) configure -text [lindex $values $i]
230        }
231      }
232      set options(-initialvalue) $values
233    }
234  }
235
236  onconfigure -list { lists } {
237    if { $created } {
238      for { set i 0 } { $i < [llength $options(-fields)] } { incr i } {
239        $combos configure -values [lindex $lists $i]
240      }
241      set options(-list) $lists
242    }
243  }
244 
245  onconfigure -titles { lists } {
246    if { $lists eq {} } {
247      set options(-titles) [from args -fields {" "}]
248    } else {
249      set options(-titles) $lists
250    }
251  }
252
253  #///////////////////////////////////////////////////////////////////////////
254  method _Info { } {
255  #
256  # PURPOSE: shows an information message in a dialog
257  #
258  #///////////////////////////////////////////////////////////////////////////
259    destroy $self.infodialog
260   
261    Dialog $self.infodialog -modal local -title [mc "Filter Information"]
262
263    label $self.infodialog.l -justify left -text $options(-info)
264    pack $self.infodialog.l
265
266     $self.infodialog add -image [::Bitmap::get accept] -relief link \
267               -text [mc "OK"] -compound left \
268               -command [list $self.infodialog  enddialog ""]
269
270    $self.infodialog draw
271  }
272
273
274  #///////////////////////////////////////////////////////////////////////////
275  method _Enter { } {
276  #
277  # PURPOSE: Check content of entrykey and executes _Proc
278  #
279  #///////////////////////////////////////////////////////////////////////////
280    if { $options(-list) eq "" } {
281      $self _Proc
282    } else {
283      #the text in the entry key
284      set text    [$combos key get]
285      #All the possible outputs
286      set indexes [$combos get]
287     
288      foreach index $indexes {
289        if { [lindex [$combos get $index] 0] == $text } {
290          #If the output matches with the written text, is selected and inserted
291          $combos selectIndex $index
292          $self _Proc
293          break
294        }
295      }
296    }
297  }
298
299  #///////////////////////////////////////////////////////////////////////////
300  method _Proc { {close 0} {all 0} } {
301  #
302  # PURPOSE: processes the fields of the widget via an external process
303  # PARAMETERS:
304  #  close -> Sets if the window is closed after execution or not
305  #  all -> Sets if the process will be applied to all elements from combobox
306  #         or not
307  #
308  #///////////////////////////////////////////////////////////////////////////
309    set elements {}
310    if { $options(-list) eq "" } {
311      for { set i 0 } { $i < [llength $options(-fields)] } { incr i } {
312        lappend elements [$entries($i) cget -text]
313        $entries($i) configure -text ""
314      }
315      $options(-procedure) [list $elements] [winfo parent $self]
316    } else  {
317      if { $all } {
318        lappend elements [$combos get [$combos get]]
319        set elements [lindex $elements 0]
320       
321      } else {
322        lappend elements [$combos get [$combos curselection]]
323      }
324      $options(-procedure) $elements [winfo parent $self]
325    }
326
327    if { $close } { $self _CloseDialog $self }
328  }
329
330
331  #///////////////////////////////////////////////////////////////////////////
332  method _CloseDialog { w } {
333  #
334  # PURPOSE: closes de dialog
335  #
336  # PARAMETERS:
337  #  w -> The dialog's window pathname
338  #
339  #///////////////////////////////////////////////////////////////////////////
340    set f [$w getframe]
341    set dialogEntry ""
342    $w enddialog ""
343  }
344
345
346  #///////////////////////////////////////////////////////////////////////////
347  method _Focus { } {
348  #
349  # PURPOSE: focuses the first entry or combobox
350  #
351  #///////////////////////////////////////////////////////////////////////////
352    if { "" eq $options(-list) } {
353      focus $entries(0)
354    } else  {
355      focus $combos
356    }
357  }
358   
359
360  #///////////////////////////////////////////////////////////////////////////
361  method draw {  } {
362  #
363  # PURPOSE: draws the dialog and gives focus to entry/combobox
364  #
365  #///////////////////////////////////////////////////////////////////////////
366    $self _Focus
367    $hull draw
368  }
369
370  delegate method * to hull
371  delegate option * to hull
372}
373
374#TEST CODE
375
376proc __MiProc { elems w } {
377  $w configure -text $elems
378}
379
380proc test_bfieldsdialog {} {
381
382  destroy .top
383  toplevel .top
384  variable horizontal 1
385  variable combos 0
386
387   radiobutton .top.rb1 -text "Horizontal layout" -value 1 -variable horizontal
388   radiobutton .top.rb2 -text "Vertical layout"  -value 0 -variable horizontal
389   checkbutton .top.cb -text "With combos" -command togglecombos
390   .top.cb deselect
391   button .top.b -text "Launch a Fields Dialog" -command "__PreLaunchDialog"
392   LabelEntry .top.le -label "Fields processed..."
393
394
395   grid .top.rb1 -row 0 -column 0 -sticky nsw
396   grid .top.rb2 -row 1 -column 0 -sticky nsw
397   grid .top.cb -row 0 -column 1
398   grid .top.b -row 1 -column 1
399   grid .top.le -row 2 -column 0 -columnspan 2 -sticky "ew"
400   
401   proc togglecombos {} {
402     variable combos
403     set combos [expr ! $combos]
404   }
405
406   proc __PreLaunchDialog {} {
407     variable combos
408     variable horizontal
409     __LaunchDialog $horizontal $combos .top.le
410   }
411
412  proc __LaunchDialog { hor combos father } {
413    set orientation vertical
414    if { $hor } { set orientation horizontal }
415
416    if { $combos } {
417      bfieldsdialog ${father}.d -title "Prueba Dialog with Combos" -width 20 \
418                             -fields { "campo1" "campo2" "campo3" } \
419                             -titles { "Titulo1" "Titulo2" "Titulo3" } \
420                             -initialvalue {uno 2 iii} \
421                             -orientation $orientation -procedure __MiProc \
422                             -buttons {info apply accept cancel} \
423                             -list {{uno} {1 2} {i ii iii}} \
424                             -info \
425"Los valores iniciales suministrados se
426buscan en los combos y son seleccionados"
427    } else {
428      bfieldsdialog ${father}.d -title "Prueba Dialog with Entries" \
429                             -width 20 -fields { "campo1" "campo2" "campo3" } \
430                             -titles { "Titulo1" "Titulo2" "Titulo3" } \
431                             -initialvalue {k kk kkk} -orientation $orientation \
432                             -buttons {add exit} -procedure __MiProc
433    }
434   
435    ${father}.d draw
436    destroy ${father}.d
437  }
438}
439
440package provide bfieldsdialog 1.0
Note: See TracBrowser for help on using the repository browser.