Version 0 of Q&D Dialog Box

Updated 2009-08-26 22:04:56 by LarrySmith
 if 0 {

This utility will enable you to trivially throw up a dialog box which will allow a quick-and-dirty way to edit global variables in your program. It will be most useful for testing or one-off personal programs, rather than production programs, as the generated box is not very elegant.

The code:

 }


 proc dlgdone { result } {
  wm withdraw $::curdlg
  set ::dismiss $result
 }

 proc updvars { args } {
  clear name type var init
  foreach field $::curflds {
    foreach { name type var init } $field break
    set ::$var [ $::curdlg.$var get ]
  }
  dlgdone 1
 }

 proc dialog { dlgname wait title fields } {
  set ::curdlg $dlgname
  set ::curflds $fields 
  if { [ info commands $dlgname ] eq "" } {
    toplevel $dlgname
    wm protocol . WM_DELETE_WINDOW done
    wm title $dlgname $title
    set label 0
    set row 0
    foreach field $fields {
      clear name type var init
      foreach { name type var init } $field break
      switch -exact $type {
        file   {
          gridit [ label $dlgname.[incr label] -text $name] [incr row] 0
          gridit [ entry $dlgname.$var ] $row 1
          grid [ button $dlgname.choose[incr label] -text ... \
            -command [ list getfn $dlgname.$var $name ] ] -row $row -column 2 \
            -columnspan 2 -sticky ew
        }
        text   {
          gridit [ label $dlgname.[incr label] -text $name] [incr row] 0
          gridit [ entry $dlgname.$var ] $row 1
        }
        number {
          gridit [ label $dlgname.[incr label] -text $name ] [incr row] 0
          gridit [ entry $dlgname.$var ] $row 1
          gridit [ button $dlgname.up$var -text "<" \
            -command [ list dec $dlgname.$var ] ] $row 2
          gridit [ button $dlgname.dn$var -text ">" \
            -command [ list inc $dlgname.$var ] ] $row 3
        }
      }
      $dlgname.$var insert 0 $init
    }
    grid [ button $dlgname.okay -text "OK" -width 6 \
      -command updvars ] -row [incr row] -column 1 -sticky w
    grid [ button $dlgname.cancel -text "Cancel" -width 6 \
      -command { dlgdone 0 } ] -row $row -column 1 -sticky e
  }
  wm manage $dlgname
  if $wait { vwait ::dismiss }
  return $::dismiss
 }

 if 0 {

The test code:

  if [dialog .calcbody 1 "Calculator Body" {
    { "Left Side" file leftfn }
    { "Right Side" file rightfn }
    { "Foot" file footfn }
    { "Width" number cw 300 }
    { "Height" number ch 500}
    { "BG Color" text calcbg #464646}
  } ] {
    putimage [ loadimage leftside $::leftfn ] left
    putimage [ loadimage footside $::footfn ] foot
    putimage [ loadimage rightside $::rightfn ] right
    $::calc configure -bg $::calcbg
  } 
 }

It only supports the three data types at the moment. The "wait" flag indicates modality, the program halts and waits for the dialog to be completed and dismissed. A 0 will return right away, leaving the user an opportunity to edit the vars and then accept or cancel out when they are ready.