From: ewt Date: Mon, 27 Oct 1997 19:42:05 +0000 (+0000) Subject: 1) forms return things now X-Git-Tag: r0-20~28 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ea59ae12f772e069ca814584b10561cdd6896675;p=thirdparty%2Fnewt.git 1) forms return things now 2) added hotkeys --- diff --git a/popcorn.py b/popcorn.py index 7549e44..fa755a9 100755 --- a/popcorn.py +++ b/popcorn.py @@ -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 diff --git a/snack.py b/snack.py index aafa19e..a7f6649 100644 --- 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 diff --git a/snackmodule.c b/snackmodule.c index 36e8858..980a6e6 100644 --- a/snackmodule.c +++ b/snackmodule.c @@ -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)); }