]> git.ipfire.org Git - thirdparty/newt.git/commitdiff
1) forms return things now
authorewt <ewt>
Mon, 27 Oct 1997 19:42:05 +0000 (19:42 +0000)
committerewt <ewt>
Mon, 27 Oct 1997 19:42:05 +0000 (19:42 +0000)
2) added hotkeys

popcorn.py
snack.py
snackmodule.c

index 7549e444951bb63f84404e92e08a85a95602c84c..fa755a9f815e982501dc10d8b1d6c252ad74859b 100755 (executable)
@@ -5,7 +5,7 @@ from snack import *
 screen = SnackScreen()
 
 t = Textbox(25, 1, "Some text")
-li = Listbox(5, width = 20)
+li = Listbox(5, width = 20, returnExit = 1)
 li.append("First")
 li.append("Second")
 b = Button("Button")
@@ -43,7 +43,9 @@ f.add(r1)
 f.add(r2)
 f.add(t)
 
-f.run()
+f.addHotKey("F1")
+
+res = f.run()
 
 screen.popWindow()
 
@@ -54,3 +56,4 @@ print "check", cb.value()
 print "r1", r1.selected()
 print "listbox", li.current()
 # returns a tuple of the wrapped text, the actual width, and the actual height
+print res
index aafa19e413f600a181f43c6497299954e812ca22..a7f6649297b2f2250a49b6f5104a3c6adaf9d9c6 100644 (file)
--- a/snack.py
+++ b/snack.py
@@ -22,7 +22,7 @@ class Checkbox(Widget):
 class SingleRadioButton(Widget):
 
     def selected(self):
-       return self.w.radiobuttonkey == self.w.radioValue;
+       return self.w.key == self.w.radioValue;
     
     def __init__(self, text, group, isOn = 0):
        if group:
@@ -66,14 +66,23 @@ class Entry(Widget):
 
 class Form:
 
+    def addHotKey(self, keyname):
+       self.f.addhotkey(hotkeys[keyname])
+
     def add(self, widget):
-       return self.g.add(widget.w)
+       self.trans[widget.w.key] = widget
+       return self.f.add(widget.w)
 
     def run(self):
-       return self.g.run()
+       (what, which) = self.f.run()
+       if (what == _snack.FORM_EXIT_WIDGET):
+           return self.trans[which]
+
+       return hotkeys[which]
 
     def __init__(self):
-       self.g = _snack.form()
+       self.trans = {}
+       self.f = _snack.form()
 
 class Grid:
 
@@ -170,7 +179,7 @@ def ButtonBar(Grid):
        Grid.__init__(self, len(buttonlist), 1)
        for (title, value) in buttonlist:
            b = Button(title)
-           self.list.append(b, value))
+           self.list.append(b, value)
            self.setField(b, self.item, 0, (0, 1, 0, 1))
            self.item = self.item + 1
 
@@ -200,3 +209,11 @@ def GridForm(Grid):
        self.form.run()
        self.screen.popWindow()
 
+hotkeys = { "F1" : _snack.KEY_F1, "F2" : _snack.KEY_F2, "F3" : _snack.KEY_F3, 
+            "F4" : _snack.KEY_F4, "F5" : _snack.KEY_F5, "F6" : _snack.KEY_F6, 
+            "F7" : _snack.KEY_F7, "F8" : _snack.KEY_F8, "F9" : _snack.KEY_F9, 
+            "F10" : _snack.KEY_F10, "F11" : _snack.KEY_F11, 
+            "F12" : _snack.KEY_F12 }
+
+for n in hotkeys.keys():
+    hotkeys[hotkeys[n]] = n
index 36e8858fdb0e6820521bb80229c9605f605ad580..980a6e6ea6240902fa1a3c65e18d022520bcfb96 100644 (file)
@@ -98,10 +98,12 @@ struct snackForm_s {
 static PyObject * formGetAttr(PyObject * s, char * name);
 static PyObject * formAdd(snackForm * s, PyObject * args);
 static PyObject * formRun(snackForm * s, PyObject * args);
+static PyObject * formHotKey(snackForm * s, PyObject * args);
 
 static PyMethodDef formMethods[] = {
     { "add", (PyCFunction) formAdd, METH_VARARGS, NULL },
     { "run", (PyCFunction) formRun, METH_VARARGS, NULL },
+    { "addhotkey", (PyCFunction) formHotKey, METH_VARARGS, NULL },
     { NULL }
 };
 
@@ -483,7 +485,23 @@ static PyObject * formAdd(snackForm * s, PyObject * args) {
 }
 
 static PyObject * formRun(snackForm * s, PyObject * args) {
-    newtRunForm(s->fo);
+    struct newtExitStruct result;
+
+    newtFormRun(s->fo, &result);
+
+    if (result.reason == NEWT_EXIT_HOTKEY)
+       return Py_BuildValue("(si)", "hotkey", result.u.key);
+    else
+       return Py_BuildValue("(si)", "widget", result.u.co);
+}
+
+static PyObject * formHotKey(snackForm * s, PyObject * args) {
+    int key;
+
+    if (!PyArg_ParseTuple(args, "i", &key))
+       return NULL;
+
+    newtFormAddHotKey(s->fo, key);
 
     Py_INCREF(Py_None);
     return Py_None;
@@ -492,7 +510,7 @@ static PyObject * formRun(snackForm * s, PyObject * args) {
 static PyObject * widgetGetAttr(PyObject * s, char * name) {
     snackWidget * w = (snackWidget *) s;
 
-    if (!strcmp(name, "radiobuttonkey")) {
+    if (!strcmp(name, "key")) {
        return Py_BuildValue("i", w->co);
     } else if (!strcmp(name, "entryValue")) {
        return Py_BuildValue("s", w->apointer);
@@ -563,4 +581,20 @@ void init_snack(void) {
                         PyInt_FromLong(NEWT_ANCHOR_BOTTOM));
     PyDict_SetItemString(d, "GRID_GROWX", PyInt_FromLong(NEWT_GRID_FLAG_GROWX));
     PyDict_SetItemString(d, "GRID_GROWY", PyInt_FromLong(NEWT_GRID_FLAG_GROWY));
+
+    PyDict_SetItemString(d, "FORM_EXIT_HOTKEY", PyString_FromString("hotkey"));
+    PyDict_SetItemString(d, "FORM_EXIT_WIDGET", PyString_FromString("widget"));
+
+    PyDict_SetItemString(d, "KEY_F1", PyInt_FromLong(NEWT_KEY_F1));
+    PyDict_SetItemString(d, "KEY_F2", PyInt_FromLong(NEWT_KEY_F2));
+    PyDict_SetItemString(d, "KEY_F3", PyInt_FromLong(NEWT_KEY_F3));
+    PyDict_SetItemString(d, "KEY_F4", PyInt_FromLong(NEWT_KEY_F4));
+    PyDict_SetItemString(d, "KEY_F5", PyInt_FromLong(NEWT_KEY_F5));
+    PyDict_SetItemString(d, "KEY_F6", PyInt_FromLong(NEWT_KEY_F6));
+    PyDict_SetItemString(d, "KEY_F7", PyInt_FromLong(NEWT_KEY_F7));
+    PyDict_SetItemString(d, "KEY_F8", PyInt_FromLong(NEWT_KEY_F8));
+    PyDict_SetItemString(d, "KEY_F9", PyInt_FromLong(NEWT_KEY_F9));
+    PyDict_SetItemString(d, "KEY_F10", PyInt_FromLong(NEWT_KEY_F10));
+    PyDict_SetItemString(d, "KEY_F11", PyInt_FromLong(NEWT_KEY_F11));
+    PyDict_SetItemString(d, "KEY_F12", PyInt_FromLong(NEWT_KEY_F12));
 }