summaryrefslogtreecommitdiff
path: root/support/xtexshell/fileselbox.tcl
blob: 2e8d5b91354e9812b01970a99d6fb12ced3789b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#******************************************************************************
#***
#*** This file is part of XTeXShell; see file xtexsh for details
#*** Version 0.91 (21.2.94)
#***
#******************************************************************************

proc FileSelBox {Mode {startdir "."}} {

#***
#*** Displays a File Selector Box
#***
#*** Input Value (optional)
#***     Mode = (OPEN / CREATE / OPENCREATE / DIR / CD)
#***     Start directory ("." is default)
#***
#*** Return Values:
#***     "" if operation was canceled
#***     Full File Name if operation was completed successfully
#***

#*** Variables and Definitions
      
        global      fsbfname fsbfmode

        set         fsbfname [expr {[cequal $startdir "."] ? [eval pwd] : $startdir}]

        switch $Mode {
                "OPEN"       { set title "OPEN existing File" }
                "CREATE"     { set title "CREATE a new File" }  
                "OPENCREATE" { set title "OPEN / CREATE a File" }
                "DIR"        { set title "View Directory" }
                "CD"         { set title "Change Directory" }
        }       
        set         fsbfmode $Mode

#*** Generate Window

        global      fsbwin  fsbwin_name  fsbwin_geo
        global      fixbold_font bold_font

        set         fsbwin  [CreateTopWin fsbwin "CREATE"]

        wm title    $fsbwin $title
        wm iconname $fsbwin "File Selector Box"
        wm minsize  $fsbwin 5 5

        entry       $fsbwin.mask   -relief sunken -textvariable fsbfname -font $bold_font -exportselection 0
        frame       $fsbwin.but    -relief raised -borderwidth 1
        scrollbar   $fsbwin.scroll -command "$fsbwin.list yview"
        listbox     $fsbwin.list   -yscroll "$fsbwin.scroll set" -relief flat -geometry 20x20 \
                                   -setgrid yes -font $fixbold_font -exportselection 0

        pack        $fsbwin.mask   -side top     -fill x -padx 3m -pady 1m -fill  x
        pack        $fsbwin.but    -side bottom  -fill x
        pack        $fsbwin.scroll -side right   -fill y
        pack        $fsbwin.list   -side left    -fill both -expand yes

#*** Generate Buttons

        button      $fsbwin.but.ok     -text "OK"      -command {fsbcheck OK 0}
        button      $fsbwin.but.cancel -text "Cancel"  -command {fsbcheck CANCEL 0}
        button      $fsbwin.but.form   -text "Format"  -command {lvarpush fsboptions [lvarpop fsboptions 0] "end+1"; fsbgetlist}
        button      $fsbwin.but.help   -text "Help"    -command { }    
        pack        $fsbwin.but.ok $fsbwin.but.cancel $fsbwin.but.form $fsbwin.but.help -side left -padx 3m -pady 1m -ipadx 0.6m

#*** Set Input focus and Keyboard events

        bind        $fsbwin.mask <Return>               {fsbcheck RETURN 0}
        bind        $fsbwin.list <Control-q>            {fsbcheck CANCEL 0}
        bind        $fsbwin.list <Control-c>            {fsbcheck CANCEL 0}
#       bind        $fsbwin.list <Button-1>             { }
#       bind        $fsbwin.list <Any-ButtonPress>      { }
#       bind        $fsbwin.list <Any-ButtonRelease>    { }
#       bind        $fsbwin.list <Any-Motion>           { } 
        bind        $fsbwin.list <Double-Button-1>      {fsbcheck BUT1 %y}


#*** Read File List

        fsbgetlist 

#*** Wait for OK or cancel 

        tkwait  window $fsbwin
        return  $fsbfname
}

#*******************************************************************************
#*** functions to handle user input and read directories ***********************
#*******************************************************************************

proc fsbcheck {Mode {ypos 0}} {

#*** This function is called after input from user
#*** Action depends on Mode

        global fsbwin fsbfname fsbfmode
        global boldl_font

#*** User requested CANCEL. 

        if {[cequal $Mode "CANCEL"]} {
                set fsbfname ""
                destroy $fsbwin
                return
        }

#*** User pushed Double Button 1                                
                
        if {[cequal $Mode "BUT1"]} {
		if {![file isdirectory $fsbfname]} {
			DisplayInfo "Can't concatenate filename because mask is not a valid directory name" $boldl_font
			return
		} 
 
                set str [$fsbwin.list get [$fsbwin.list nearest $ypos]]                 
                set str [lvarpop str end]

                if {[cequal $str "."]} {        
                        fsbgetlist
                        return
                }       

                if {[cequal $fsbfname  "."]} {   
                        set fsbfname $str
                } else {
			if {[cequal "/" [cindex $fsbfname end]]} {
				set fsbfname "$fsbfname$str"
			} else {
				set fsbfname "$fsbfname/$str"
			}
                }
                set Mode "RETURN"
        }

#*** User requested RETURN.

        if {[cequal $Mode "RETURN"]} {
                if {[file isdirectory $fsbfname]} {
                        fsbgetlist 
                        return
                }
        }

#*** OK, user has selected a filename which is in fsbfname. 

        switch $fsbfmode {
                "OPENCREATE" {
                        destroy $fsbwin
                }
                "OPEN" {
                        if {[file exists $fsbfname]} {
                                destroy $fsbwin
                                return
                        }
                        DisplayInfo "Warning!!!\n\nThe file you selected:\n$fsbfname\nis not a valid file\nPlease choose a new one" "$boldl_font"
                } 
                "CREATE" {              
                        if {[file exists $fsbfname]} {
                                set retval [DisplayQuest "Warning!!!\n\nFile:\n$fsbfname\n already exists!\nOverwrite?"\
                                            "$boldl_font" "Overwrite" "Select New File"]                
                                if {$retval==1} {
                                        destroy $fsbwin
                                        return
                                }
                        } else {
                                destroy $fsbwin
                                return
                        }                               
                }
                "DIR" {
                        set fsbfname ""
                        destroy $fsbwin
                        return
                }
                "CD"  {
                        if {[file isdirectory $fsbfname]} {
                                destroy $fsbwin
                                return
                        }
                        DisplayInfo "Error!!!\n\nThe path you selected:\nis not a valid directory\nPlease chose a new directory" "$boldl_font"
                }
        }
}               

proc fsbgetlist {} {
        
#*** Clear Listbox. Then fill Listbox with directory entries

        global  fsbfname fsboptions fsbwin

        $fsbwin.list delete 0 10000

        set filelist [exec ls [lindex $fsboptions 0] $fsbfname]
        foreach i [split $filelist "\n"] {
              $fsbwin.list insert end $i
        }

	if {![cequal "/" [cindex $fsbfname end]]} {
		set fsbfname "$fsbfname/"
	}

        focus       $fsbwin.mask
}