Debian Codegraph @

gui/dialogs/file-select.xml

raw symbols SCIP
1 <?xml version="1.0"?>
2
3 <!--
4 Ctrl-click on "." toggles display of hidden Unix filex (dotfiles)
5 Ctrl-click on ".." enters $FG_ROOT
6 Shift-click on ".." enters $FG_HOME
7 Alt-click on ".." enters current working directory
8 -->
9
10 <PropertyList>
11 <name>file-select</name>
12 <dialog-name>file-select</dialog-name>
13 <layout>vbox</layout>
14 <resizable>true</resizable>
15 <pref-width>450</pref-width>
16
17 <group>
18 <layout>hbox</layout>
19
20 <text>
21 <label>Select File</label>
22 </text>
23
24 <empty><stretch>1</stretch></empty>
25
26 <button>
27 <legend>X</legend>
28 <key>Esc</key>
29 <pref-width>20</pref-width>
30 <pref-height>20</pref-height>
31 <binding>
32 <command>nasal</command>
33 <script>close()</script>
34 </binding>
35 <binding>
36 <command>dialog-close</command>
37 </binding>
38 </button>
39 </group>
40
41 <hrule/>
42
43 <input>
44 <name>dir-input</name>
45 <pref-width>442</pref-width>
46 <halign>fill</halign>
47 <property>/sim/gui/dialogs/file-select/directory</property>
48 <live>1</live>
49 <binding>
50 <command>dialog-apply</command>
51 <object-name>dir-input</object-name>
52 </binding>
53 <binding>
54 <command>nasal</command>
55 <script>dir_input()</script>
56 </binding>
57 </input>
58
59 <list>
60 <name>list</name>
61 <halign>fill</halign>
62 <valign>fill</valign>
63 <stretch>true</stretch>
64 <pref-height>300</pref-height>
65 <property>/sim/gui/dialogs/file-select/selection</property>
66 <binding>
67 <command>dialog-apply</command>
68 <object-name>list</object-name>
69 </binding>
70 <binding>
71 <command>nasal</command>
72 <script>select()</script>
73 </binding>
74 </list>
75
76 <group>
77 <layout>hbox</layout>
78
79 <input>
80 <name>file-input</name>
81 <pref-width>230</pref-width>
82 <halign>fill</halign>
83 <stretch>true</stretch>
84 <property>/sim/gui/dialogs/file-select/selection</property>
85 <live>1</live>
86 <binding>
87 <command>dialog-apply</command>
88 <object-name>file-input</object-name>
89 </binding>
90 <binding>
91 <command>nasal</command>
92 <script>file_input()</script>
93 </binding>
94 <visible>
95 <equals> <!-- yes, odd condition. easier ways somehow fail,
96 since we're changing the values with Nasal below... -->
97 <value>1</value>
98 <value>1</value>
99 </equals>
100 </visible>
101 </input>
102
103 <button>
104 <legend>OK</legend>
105 <live>1</live>
106 <pref-width>200</pref-width>
107 <default>true</default>
108 <binding>
109 <command>dialog-apply</command>
110 </binding>
111 <binding>
112 <command>nasal</command>
113 <script>ok()</script>
114 </binding>
115 <binding>
116 <command>dialog-close</command>
117 </binding>
118 </button>
119 </group>
120
121 <nasal>
122 <open>
123 var self = cmdarg();
124 var list = self.getNode("list");
125
126 # cloning
127 var dlgname = self.getNode("name").getValue();
128 self.getNode("input/property").setValue("/sim/gui/dialogs/" ~ dlgname ~ "/directory");
129 self.getNode("list/property").setValue("/sim/gui/dialogs/" ~ dlgname ~ "/selection");
130 self.getNode("group[1]/input/property").setValue("/sim/gui/dialogs/" ~ dlgname ~ "/selection");
131
132 var dlg = props.globals.getNode("/sim/gui/dialogs/" ~ dlgname, 1);
133 var selection = dlg.getNode("selection", 1);
134 var title = dlg.getNode("title", 1);
135 var button = dlg.getNode("button", 1);
136 var dir = dlg.getNode("directory", 1);
137 var file = dlg.getNode("file", 1);
138 var path = dlg.getNode("path", 1);
139 var dotfiles = dlg.getNode("dotfiles", 1);
140 dotfiles.setBoolValue(dotfiles.getValue());
141 # disable files to show a directory dialog only
142 self.show_files = dlg.getNode("show-files",1).getValue();
143 self.getNode("group[1]/input/visible/equals/value").setValue(self.show_files);
144
145 var kbdctrl = props.globals.getNode("/devices/status/keyboard/ctrl", 1);
146 var kbdshift = props.globals.getNode("/devices/status/keyboard/shift", 1);
147 var kbdalt = props.globals.getNode("/devices/status/keyboard/alt", 1);
148 var current = { dir : "", file : "" };
149 var pattern = [];
150 foreach (var p; dlg.getChildren("pattern"))
151 append(pattern, p.getValue());
152
153 var matches = func(s) {
154 foreach (var p; pattern)
155 if (string.match(s, p))
156 return 1;
157 return 0;
158 }
159
160 var update = func(d) {
161 var entries = directory(d);
162 var retval = 1;
163 if (entries == nil) { # dir doesn't exist or no permissions
164 entries = ["..", "Not found or access denied", "Ctrl-click .. for FG_ROOT", "Shift-click .. for FG_HOME","To allow more directories, pass them to --allow-nasal-read"];
165 retval = 0;
166 } else {
167 var files = [];
168 var dirs = [];
169 var hide = !dotfiles.getValue();
170 foreach (var e; entries) {
171 if (e == ".") {
172 append(dirs, e);
173 continue;
174 }
175 if (e == "..") {
176 if (d != "/")
177 append(dirs, e);
178 continue;
179 }
180 if (hide and e[0] == `.`)
181 continue;
182
183 var stat = io.stat(d ~ "/" ~ e);
184 if (stat == nil) # dead link
185 continue;
186
187 if (stat[11] == "dir")
188 append(dirs, e ~ "/");
189 elsif (self.show_files and (!size(pattern) or matches(e)))
190 append(files, e);
191 }
192 var entries = sort(dirs, cmp) ~ sort(files, cmp);
193 }
194
195 list.removeChildren("value");
196 forindex (var i; entries)
197 list.getChild("value", i, 1).setValue(entries[i]);
198
199 dir.setValue(d);
200 gui.dialog_update(dlgname, "dir-input", "list");
201 return retval;
202 }
203
204 var select = func {
205 var e = selection.getValue();
206 current.file = "";
207 var new = nil;
208 if (e == ".") {
209 new = current.dir;
210 if (kbdctrl.getValue())
211 dotfiles.setBoolValue(!dotfiles.getValue());
212 } elsif (e == "..") {
213 if (kbdctrl.getValue())
214 new = getprop("/sim/fg-root");
215 elsif (kbdshift.getValue())
216 new = getprop("/sim/fg-home");
217 elsif (kbdalt.getValue())
218 new = getprop("/sim/fg-current");
219 else
220 new = current.dir ~ "/..";
221 } elsif (e[size(e) - 1] == `/`) {
222 new = current.dir ~ "/" ~ e;
223 } else {
224 current.file = e;
225 gui.dialog_update(dlgname, "file-input");
226 }
227 if (new != nil) {
228 var p = string.normpath(new);
229 if (update(p))
230 current.dir = p;
231 selection.setValue("");
232 }
233 }
234
235 var file_input = func {
236 current.file = selection.getValue();
237 }
238
239 var dir_input = func {
240 var p = string.normpath(dir.getValue());
241 if (update(p))
242 current.dir = p;
243 gui.dialog_update(dlgname, "list");
244 }
245
246 var close = func {
247 call(func { gui.Dialog.instance[dlgname].close() }, nil, var err = []);
248 }
249
250 var ok = func {
251 dir_input();
252 if (self.show_files)
253 file_input();
254 else
255 current.file = "";
256 var p = string.normpath(current.dir ~ "/" ~ current.file);
257 var stat = io.stat(p);
258 path.setValue(stat != nil and stat[11] == "dir" ? p ~ "/" : p);
259 file.setValue(current.file);
260 close();
261 }
262
263 var op = button.getValue();
264 if (op == nil or op == "")
265 op = "OK";
266 self.getNode("group[1]/button/legend").setValue(op);
267
268 var t = title.getValue();
269 if (t == nil or t == "")
270 {
271 if (self.show_files)
272 t = "Select Directory";
273 else
274 t = "Select File";
275 }
276 self.getNode("group[0]/text/label").setValue(t);
277
278 current.dir = (var d = dir.getValue()) != nil and d != "" ? d : getprop("/sim/fg-current");
279 current.file = (var d = file.getValue()) != nil and d != "" ? d : "";
280 gui.dialog_update(dlgname, "file-input"); ## dir-input ?
281 update(string.normpath(current.dir));
282 dir.setValue(current.dir);
283 </open>
284 </nasal>
285 </PropertyList>