- make dir entry an input field
- make title & button text separately settable - sequential numbers for clones rather than random numbers
This commit is contained in:
parent
e6ca353144
commit
10ca74fc83
2 changed files with 70 additions and 34 deletions
|
@ -239,30 +239,39 @@ Dialog = {
|
||||||
##
|
##
|
||||||
# FileSelector class (derived from Dialog class).
|
# FileSelector class (derived from Dialog class).
|
||||||
#
|
#
|
||||||
# SYNOPSIS: FileSelector.new(<callback> [, <oper> [, <dir> [, <file> [, <hidden>]]])
|
# SYNOPSIS: FileSelector.new(<callback> [, <title> [, <button> [, <dir> [, <file> [, <dotfiles>]]]])
|
||||||
#
|
#
|
||||||
# callback ... callback function that gets return value as cmdarg().getValue()
|
# 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)
|
# dir ... starting dir ($FG_ROOT if unset)
|
||||||
# file ... pre-selected default file name
|
# 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:
|
# EXAMPLE:
|
||||||
#
|
#
|
||||||
# var report = func { print("file ", cmdarg().getValue(), " selected") }
|
# 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
|
# selector.open(); # see the Dialog class for other methods
|
||||||
#
|
#
|
||||||
var FileSelector = {
|
var FileSelector = {
|
||||||
new : func(callback, operation = "OK", dir = "", file = "", show_hidden = 0) {
|
new : func(callback, title = "File Selection", button = "OK",
|
||||||
var name = "file-select-" ~ int(1e9 * rand());
|
dir = "", file = "", dotfiles = 0) {
|
||||||
var data = props.globals.getNode("/sim/gui/dialogs/" ~ name, 1);
|
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);
|
var m = Dialog.new(data.getNode("dialog", 1), "gui/dialogs/file-select.xml", name);
|
||||||
m.parents = [FileSelector, Dialog];
|
m.parents = [FileSelector, Dialog];
|
||||||
data.getNode("operation", 1).setValue(operation);
|
m.data = data;
|
||||||
data.getNode("directory", 1).setValue(dir);
|
m.data.getNode("title", 1).setValue(title);
|
||||||
data.getNode("selection", 1).setValue(file);
|
m.data.getNode("button", 1).setValue(button);
|
||||||
data.getNode("show-hidden", 1).setBoolValue(show_hidden);
|
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);
|
m.cblistener = setlistener(data.getNode("path", 1), callback);
|
||||||
return m;
|
return m;
|
||||||
},
|
},
|
||||||
|
@ -270,7 +279,7 @@ var FileSelector = {
|
||||||
me.close();
|
me.close();
|
||||||
delete(me.instance, me.name);
|
delete(me.instance, me.name);
|
||||||
removelistener(me.cblistener);
|
removelistener(me.cblistener);
|
||||||
me.prop.getParent().removeChild(me.prop.getName(), me.prop.getIndex());
|
props.globals.getNode("/sim/gui/dialogs", 1).removeChildren(me.name);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -14,11 +14,10 @@
|
||||||
|
|
||||||
<group>
|
<group>
|
||||||
<layout>hbox</layout>
|
<layout>hbox</layout>
|
||||||
<empty><whatever/></empty>
|
<empty><stretch>1</stretch></empty>
|
||||||
|
|
||||||
<text>
|
<text>
|
||||||
<property>/sim/gui/dialogs/file-select/title</property>
|
<label>Select File</label>
|
||||||
<live>1</live>
|
|
||||||
</text>
|
</text>
|
||||||
|
|
||||||
<empty><stretch>1</stretch></empty>
|
<empty><stretch>1</stretch></empty>
|
||||||
|
@ -42,6 +41,21 @@
|
||||||
|
|
||||||
<hrule/>
|
<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>
|
<list>
|
||||||
<name>list</name>
|
<name>list</name>
|
||||||
<halign>fill</halign>
|
<halign>fill</halign>
|
||||||
|
@ -61,17 +75,17 @@
|
||||||
<layout>hbox</layout>
|
<layout>hbox</layout>
|
||||||
|
|
||||||
<input>
|
<input>
|
||||||
<name>input</name>
|
<name>file-input</name>
|
||||||
<pref-width>230</pref-width>
|
<pref-width>230</pref-width>
|
||||||
<property>/sim/gui/dialogs/file-select/selection</property>
|
<property>/sim/gui/dialogs/file-select/selection</property>
|
||||||
<live>1</live>
|
<live>1</live>
|
||||||
<binding>
|
<binding>
|
||||||
<command>dialog-apply</command>
|
<command>dialog-apply</command>
|
||||||
<object-name>input</object-name>
|
<object-name>file-input</object-name>
|
||||||
</binding>
|
</binding>
|
||||||
<binding>
|
<binding>
|
||||||
<command>nasal</command>
|
<command>nasal</command>
|
||||||
<script>input()</script>
|
<script>file_input()</script>
|
||||||
</binding>
|
</binding>
|
||||||
</input>
|
</input>
|
||||||
|
|
||||||
|
@ -99,19 +113,19 @@
|
||||||
var list = self.getNode("list");
|
var list = self.getNode("list");
|
||||||
|
|
||||||
var dlgname = self.getNode("name").getValue();
|
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("list/property").setValue("/sim/gui/dialogs/" ~ dlgname ~ "/selection");
|
||||||
self.getNode("group[1]/input/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 dlg = props.globals.getNode("/sim/gui/dialogs/" ~ dlgname, 1);
|
||||||
var selection = dlg.getNode("selection", 1);
|
var selection = dlg.getNode("selection", 1);
|
||||||
var operation = dlg.getNode("operation", 1);
|
|
||||||
var title = dlg.getNode("title", 1);
|
var title = dlg.getNode("title", 1);
|
||||||
|
var button = dlg.getNode("button", 1);
|
||||||
var dir = dlg.getNode("directory", 1);
|
var dir = dlg.getNode("directory", 1);
|
||||||
var file = dlg.getNode("file", 1);
|
var file = dlg.getNode("file", 1);
|
||||||
var path = dlg.getNode("path", 1);
|
var path = dlg.getNode("path", 1);
|
||||||
var show_hidden = dlg.getNode("show-hidden", 1);
|
var dotfiles = dlg.getNode("dotfiles", 1);
|
||||||
show_hidden.setBoolValue(show_hidden.getValue());
|
dotfiles.setBoolValue(dotfiles.getValue());
|
||||||
|
|
||||||
var kbdctrl = props.globals.getNode("/devices/status/keyboard/ctrl", 1);
|
var kbdctrl = props.globals.getNode("/devices/status/keyboard/ctrl", 1);
|
||||||
var kbdshift = props.globals.getNode("/devices/status/keyboard/shift", 1);
|
var kbdshift = props.globals.getNode("/devices/status/keyboard/shift", 1);
|
||||||
|
@ -128,7 +142,7 @@
|
||||||
|
|
||||||
var files = [];
|
var files = [];
|
||||||
var dirs = [];
|
var dirs = [];
|
||||||
var hide = !show_hidden.getValue();
|
var hide = !dotfiles.getValue();
|
||||||
foreach (var e; entries) {
|
foreach (var e; entries) {
|
||||||
if (e == ".") {
|
if (e == ".") {
|
||||||
append(dirs, e);
|
append(dirs, e);
|
||||||
|
@ -158,7 +172,7 @@
|
||||||
list.getChild("value", i, 1).setValue(entries[i]);
|
list.getChild("value", i, 1).setValue(entries[i]);
|
||||||
|
|
||||||
dir.setValue(d);
|
dir.setValue(d);
|
||||||
title.setValue(squeeze(d, 45));
|
gui.dialog_apply("dir-input", "list");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,7 +183,7 @@
|
||||||
if (e == ".") {
|
if (e == ".") {
|
||||||
new = current.dir;
|
new = current.dir;
|
||||||
if (kbdctrl.getValue())
|
if (kbdctrl.getValue())
|
||||||
show_hidden.setBoolValue(!show_hidden.getValue());
|
dotfiles.setBoolValue(!dotfiles.getValue());
|
||||||
} elsif (e == "..") {
|
} elsif (e == "..") {
|
||||||
if (kbdctrl.getValue())
|
if (kbdctrl.getValue())
|
||||||
new = getprop("/sim/fg-root");
|
new = getprop("/sim/fg-root");
|
||||||
|
@ -181,28 +195,36 @@
|
||||||
new = current.dir ~ "/" ~ e;
|
new = current.dir ~ "/" ~ e;
|
||||||
} else {
|
} else {
|
||||||
current.file = e;
|
current.file = e;
|
||||||
gui.dialog_update(dlgname, "input");
|
gui.dialog_update(dlgname, "file-input");
|
||||||
}
|
}
|
||||||
if (new != nil) {
|
if (new != nil) {
|
||||||
var p = io.fixpath(new);
|
var p = io.fixpath(new);
|
||||||
if (update(p))
|
if (update(p))
|
||||||
current.dir = p;
|
current.dir = p;
|
||||||
selection.setValue("");
|
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();
|
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 close = func {
|
||||||
var err = [];
|
call(func { gui.Dialog.instance[dlgname].close() }, nil, var err = []);
|
||||||
call(func { gui.Dialog.instance[dlgname].close() }, nil, err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var ok = func {
|
var ok = func {
|
||||||
input();
|
dir_input();
|
||||||
|
file_input();
|
||||||
var p = io.fixpath(current.dir ~ "/" ~ current.file);
|
var p = io.fixpath(current.dir ~ "/" ~ current.file);
|
||||||
var stat = io.stat(p);
|
var stat = io.stat(p);
|
||||||
if (stat == nil)
|
if (stat == nil)
|
||||||
|
@ -212,16 +234,21 @@
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
var op = operation.getValue();
|
var op = button.getValue();
|
||||||
if (op == nil or op == "")
|
if (op == nil or op == "")
|
||||||
op = "OK";
|
op = "OK";
|
||||||
self.getNode("group[1]/button/legend").setValue(op);
|
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.dir = (var d = dir.getValue()) != nil and d != "" ? d : getprop("/sim/fg-root");
|
||||||
current.file = (var d = file.getValue()) != nil and d != "" ? d : "";
|
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));
|
update(io.fixpath(current.dir));
|
||||||
title.setValue(current.dir);
|
dir.setValue(current.dir);
|
||||||
</open>
|
</open>
|
||||||
</nasal>
|
</nasal>
|
||||||
</PropertyList>
|
</PropertyList>
|
||||||
|
|
Loading…
Add table
Reference in a new issue