From 10ca74fc833f96c6ddbd52487c1005f17b4ffb05 Mon Sep 17 00:00:00 2001 From: mfranz Date: Sun, 17 Jun 2007 15:27:33 +0000 Subject: [PATCH] - make dir entry an input field - make title & button text separately settable - sequential numbers for clones rather than random numbers --- Nasal/gui.nas | 33 ++++++++++------- gui/dialogs/file-select.xml | 71 +++++++++++++++++++++++++------------ 2 files changed, 70 insertions(+), 34 deletions(-) diff --git a/Nasal/gui.nas b/Nasal/gui.nas index 85a4a5d09..c5f938c77 100644 --- a/Nasal/gui.nas +++ b/Nasal/gui.nas @@ -239,30 +239,39 @@ Dialog = { ## # FileSelector class (derived from Dialog class). # -# SYNOPSIS: FileSelector.new( [, [, [, [, ]]]) +# SYNOPSIS: FileSelector.new( [, [, <button> [, <dir> [, <file> [, <dotfiles>]]]]) # # callback ... callback function that gets return value as cmdarg().getValue() -# oper ... string that describes purpose (put on the "OK" button) +# title ... dialog title +# button ... button text ("OK" by default, but should say "Save", "Load", etc.)) # dir ... starting dir ($FG_ROOT if unset) # file ... pre-selected default file name -# hidden ... flag that decids whether UNIX dotfiles should be shown (1) or not (0) +# dotfiles ... flag that decids whether UNIX dotfiles should be shown (1) or not (0) # # EXAMPLE: # # var report = func { print("file ", cmdarg().getValue(), " selected") } -# var selector = gui.FileSelector.new(report, "Save Flight", "/tmp", "flight.sav"); +# var selector = gui.FileSelector.new(report, "Save Flight", "Save", "/tmp", "flight.sav"); # selector.open(); # see the Dialog class for other methods # var FileSelector = { - new : func(callback, operation = "OK", dir = "", file = "", show_hidden = 0) { - var name = "file-select-" ~ int(1e9 * rand()); - var data = props.globals.getNode("/sim/gui/dialogs/" ~ name, 1); + new : func(callback, title = "File Selection", button = "OK", + dir = "", file = "", dotfiles = 0) { + var name = "file-select-"; + var data = props.globals.getNode("/sim/gui/dialogs/", 1); + var i = nil; + for (i = 1; 1; i += 1) + if (data.getNode(name ~ i, 0) == nil) + break; + data = data.getNode(name ~= i, 1); var m = Dialog.new(data.getNode("dialog", 1), "gui/dialogs/file-select.xml", name); m.parents = [FileSelector, Dialog]; - data.getNode("operation", 1).setValue(operation); - data.getNode("directory", 1).setValue(dir); - data.getNode("selection", 1).setValue(file); - data.getNode("show-hidden", 1).setBoolValue(show_hidden); + m.data = data; + m.data.getNode("title", 1).setValue(title); + m.data.getNode("button", 1).setValue(button); + m.data.getNode("directory", 1).setValue(dir); + m.data.getNode("selection", 1).setValue(file); + m.data.getNode("dotfiles", 1).setBoolValue(dotfiles); m.cblistener = setlistener(data.getNode("path", 1), callback); return m; }, @@ -270,7 +279,7 @@ var FileSelector = { me.close(); delete(me.instance, me.name); removelistener(me.cblistener); - me.prop.getParent().removeChild(me.prop.getName(), me.prop.getIndex()); + props.globals.getNode("/sim/gui/dialogs", 1).removeChildren(me.name); }, }; diff --git a/gui/dialogs/file-select.xml b/gui/dialogs/file-select.xml index 590b99aa7..093f59e05 100644 --- a/gui/dialogs/file-select.xml +++ b/gui/dialogs/file-select.xml @@ -14,11 +14,10 @@ <group> <layout>hbox</layout> - <empty><whatever/></empty> + <empty><stretch>1</stretch></empty> <text> - <property>/sim/gui/dialogs/file-select/title</property> - <live>1</live> + <label>Select File</label> </text> <empty><stretch>1</stretch></empty> @@ -42,6 +41,21 @@ <hrule/> + <input> + <name>dir-input</name> + <pref-width>442</pref-width> + <property>/sim/gui/dialogs/file-select/directory</property> + <live>1</live> + <binding> + <command>dialog-apply</command> + <object-name>dir-input</object-name> + </binding> + <binding> + <command>nasal</command> + <script>dir_input()</script> + </binding> + </input> + <list> <name>list</name> <halign>fill</halign> @@ -61,17 +75,17 @@ <layout>hbox</layout> <input> - <name>input</name> + <name>file-input</name> <pref-width>230</pref-width> <property>/sim/gui/dialogs/file-select/selection</property> <live>1</live> <binding> <command>dialog-apply</command> - <object-name>input</object-name> + <object-name>file-input</object-name> </binding> <binding> <command>nasal</command> - <script>input()</script> + <script>file_input()</script> </binding> </input> @@ -99,19 +113,19 @@ var list = self.getNode("list"); var dlgname = self.getNode("name").getValue(); - self.getNode("group/text/property").setValue("/sim/gui/dialogs/" ~ dlgname ~ "/title"); + self.getNode("input/property").setValue("/sim/gui/dialogs/" ~ dlgname ~ "/directory"); self.getNode("list/property").setValue("/sim/gui/dialogs/" ~ dlgname ~ "/selection"); self.getNode("group[1]/input/property").setValue("/sim/gui/dialogs/" ~ dlgname ~ "/selection"); var dlg = props.globals.getNode("/sim/gui/dialogs/" ~ dlgname, 1); var selection = dlg.getNode("selection", 1); - var operation = dlg.getNode("operation", 1); var title = dlg.getNode("title", 1); + var button = dlg.getNode("button", 1); var dir = dlg.getNode("directory", 1); var file = dlg.getNode("file", 1); var path = dlg.getNode("path", 1); - var show_hidden = dlg.getNode("show-hidden", 1); - show_hidden.setBoolValue(show_hidden.getValue()); + var dotfiles = dlg.getNode("dotfiles", 1); + dotfiles.setBoolValue(dotfiles.getValue()); var kbdctrl = props.globals.getNode("/devices/status/keyboard/ctrl", 1); var kbdshift = props.globals.getNode("/devices/status/keyboard/shift", 1); @@ -128,7 +142,7 @@ var files = []; var dirs = []; - var hide = !show_hidden.getValue(); + var hide = !dotfiles.getValue(); foreach (var e; entries) { if (e == ".") { append(dirs, e); @@ -158,7 +172,7 @@ list.getChild("value", i, 1).setValue(entries[i]); dir.setValue(d); - title.setValue(squeeze(d, 45)); + gui.dialog_apply("dir-input", "list"); return 1; } @@ -169,7 +183,7 @@ if (e == ".") { new = current.dir; if (kbdctrl.getValue()) - show_hidden.setBoolValue(!show_hidden.getValue()); + dotfiles.setBoolValue(!dotfiles.getValue()); } elsif (e == "..") { if (kbdctrl.getValue()) new = getprop("/sim/fg-root"); @@ -181,28 +195,36 @@ new = current.dir ~ "/" ~ e; } else { current.file = e; - gui.dialog_update(dlgname, "input"); + gui.dialog_update(dlgname, "file-input"); } if (new != nil) { var p = io.fixpath(new); if (update(p)) current.dir = p; selection.setValue(""); - gui.dialog_update(dlgname, "list", "input"); + gui.dialog_update(dlgname, "list", "file-input"); ## apply? } } - var input = func { + var file_input = func { current.file = selection.getValue(); } + var dir_input = func { + print("dir input"); + var p = io.fixpath(dir.getValue()); + if (update(p)) + current.dir = p; + gui.dialog_update(dlgname, "list"); + } + var close = func { - var err = []; - call(func { gui.Dialog.instance[dlgname].close() }, nil, err); + call(func { gui.Dialog.instance[dlgname].close() }, nil, var err = []); } var ok = func { - input(); + dir_input(); + file_input(); var p = io.fixpath(current.dir ~ "/" ~ current.file); var stat = io.stat(p); if (stat == nil) @@ -212,16 +234,21 @@ close(); } - var op = operation.getValue(); + var op = button.getValue(); if (op == nil or op == "") op = "OK"; self.getNode("group[1]/button/legend").setValue(op); + var t = title.getValue(); + if (t == nil or t == "") + t = "Select File"; + self.getNode("group[0]/text/label").setValue(t); + current.dir = (var d = dir.getValue()) != nil and d != "" ? d : getprop("/sim/fg-root"); current.file = (var d = file.getValue()) != nil and d != "" ? d : ""; - gui.dialog_update(dlgname, "input"); + gui.dialog_update(dlgname, "file-input"); ## dir-input ? update(io.fixpath(current.dir)); - title.setValue(current.dir); + dir.setValue(current.dir); </open> </nasal> </PropertyList>