
package provide stead 2.0


## xtk_listPrompt ?options? - demande une valeur parmi une liste
# 
# Version initiale de Jay Sekoka (jstools)
# 
# options are:
#   -text (default "Enter a value:"
#   -default (default "")
#   -cancelvalue (default "")
#   -file (default 0)
#   -title (default "Prompt")
#   -values (default {})
# if $file, then the Tab key will do filename completion
# 
proc xtk_listPrompt { args } {

    # La globale d'état (du nom de cette proc)
    set procName "::te:listPrompt"
    upvar #0 $procName data
    Parsarg_fullSpec data {
        text           {"Enter a value:"}
        default        ""
        cancelvalue    ""
        file           0
        title          Prompt
        values         ""
    }
    
    global EDITPREFS

    # On donne à la fenetre le nom de cette procédure
    set data(toplevel) .$procName
    set w $data(toplevel)

    # 
    # creation du macro-widget
    # 
    if {[winfo exists $w]} {
        destroy $w
    } 
    
    toplevel $w
    if {[string length $data(-default)] < 40} {
        set width 60
    } else {
        set width [expr [string length $data(-default)] + 1]
    }
    
    message $w.msg -width 300 -anchor w -text $data(-text)
    # creation de la listbox
    frame $w.list
    
    listbox $w.list.values \
                -width 20 -height 12 \
                -yscrollcommand "$w.list.scroll set" \
                       -relief sunken \
                       -borderwidth 2 \
                       -exportselection false
    
    
    scrollbar $w.list.scroll \
           -orient vertical \
           -command "$w.list.values yview" \
           -relief sunken -borderwidth 2
    
    entry $w.field \
           -relief sunken \
           -width $width \
           -font $EDITPREFS(textfont) 
    
    
    set buttonsSpec {
            {ok OK          {xtk_listPrompt_setResult "ok"}      }
            {cancel Cancel  {xtk_listPrompt_setResult "cancel"}  }
    } 
    
    te:buttonbar $w.b \
            -default ok \
            -buttons $buttonsSpec

    pack append $w.list    $w.list.values     left  \
                           $w.list.scroll    {left filly}
    pack append $w \
        $w.msg      {top fill expand padx 10} \
        $w.list     {top fill expand padx 10} \
        $w.field    {top padx 10 pady 10} \
        $w.b        {bottom fillx} \
                        [xtk_rule $w -width 200] {bottom fillx}

    $w.field delete 0 end
    $w.field insert end $data(-default)


    foreach value $data(-values)  {
        $w.list.values insert end $value
    }
       
    bind $w.list.values <Double-1> {
        xtk_listPrompt_setResult sel
    }
    
    bind $w.list.values <ButtonRelease-1> [subst -nocommands {
        $w.field delete 0 end
        $w.field insert end \
               [$w.list.values get [$w.list.values curselection]]
    }]
    bind $w.field <Return> {
        xtk_listPrompt_setResult sel
    }

    if {$data(-file)} {
        bind $w.field <Tab> {xtk_listPrompt_completeFileName %W}
    }
    te:default_button   $w.b.ok     $w.field
    te:cancel_button    $w.b.cancel $w.field

    # 
    # Le macro-widget est construit : on le montre
    # 
    
    xtk_placeWindow $w
    wm title $w $data(-title)
    xtk_setFocusGrab $w $w.field
    
    tkwait window $data(toplevel)
    
    # xtk_restoreFocusGrab $w $w.field withdraw
    xtk_restoreFocusGrab $w $w.field destroy

    return $data(result)
}

## xtk_listPrompt_setResult -- proc interne (appui sur OK ou cancel)
# 
# 
proc xtk_listPrompt_setResult { what } {
    # La globale d'état (du nom de cette proc)
    set procName "::te:listPrompt"
    upvar #0 $procName data
    upvar 0 data(toplevel) w
    # set w $data(toplevel) 
    

    switch -exact -- $what {
      ok {
          set data(result) [$data(toplevel).field get]
      }
      cancel {
          set data(result) $data(-cancelvalue)
      }
      sel {
          set data(result) [$w.list.values get [$w.list.values curselection]]
      }
      default {
          error "Cas non \"$what\" prévu : devrait etre ok|cancel|sel"
      }
    }
    destroy $data(toplevel)
}
## xtk_listPrompt_completeFileName -- complete le nom de fichier dans l'entry
# 
# 
proc xtk_listPrompt_completeFileName { w } {
    set filePrefix [$w get]
    $w delete 0 end
    $w insert end [te:expand_filename $filePrefix]
}
# ./
