From: ewt Date: Fri, 17 Oct 1997 20:45:47 +0000 (+0000) Subject: added listbox, textbox, and reflowed text X-Git-Tag: r0-20~32 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6e25d997083d01137d9b7f85f8e977e017dada45;p=thirdparty%2Fnewt.git added listbox, textbox, and reflowed text --- diff --git a/listbox.c b/listbox.c index 589e881..19143cf 100644 --- a/listbox.c +++ b/listbox.c @@ -172,7 +172,7 @@ void newtListboxSetWidth(newtComponent co, int width) { co->width = width; li->curWidth = co->width - li->sbAdjust - 2 * li->bdxAdjust; li->userHasSetWidth = 1; - li->sb->left = co->width + co->left - 1; + if (li->sb) li->sb->left = co->width + co->left - 1; listboxDraw(co); } diff --git a/popcorn.py b/popcorn.py index 1d8d302..1d19807 100755 --- a/popcorn.py +++ b/popcorn.py @@ -4,6 +4,10 @@ from snack import * screen = SnackScreen() +t = Textbox(25, 1, "Some text") +li = Listbox(5, width = 20) +li.append("First") +li.append("Second") b = Button("Button") e = Entry(15, "Entry") l = Label("label") @@ -11,24 +15,33 @@ cb = Checkbox("checkbox") r1 = SingleRadioButton("Radio 1", None, 1) r2 = SingleRadioButton("Radio 2", r1) -g = Grid(2, 3) -g.setField(0, 0, b) -g.setField(1, 0, e, (1, 0, 0, 0)) -g.setField(0, 1, l, (0, 1, 0, 0)) -g.setField(1, 1, cb, (1, 1, 0, 0)) -g.setField(0, 2, r1, (0, 1, 0, 0)) -g.setField(1, 2, r2, (1, 1, 0, 0)) +sg = Grid(2, 3) +sg.setField(0, 0, b, anchorLeft = 1) +sg.setField(1, 0, e, (1, 0, 0, 0), anchorLeft = 1, anchorTop = 1) +sg.setField(0, 1, l, (0, 1, 0, 0), anchorLeft = 1) +sg.setField(1, 1, cb, (1, 1, 0, 0), anchorLeft = 1) +sg.setField(0, 2, r1, (0, 0, 0, 0), anchorLeft = 1) +sg.setField(1, 2, r2, (1, 0, 0, 0), anchorLeft = 1) + +g = Grid(1, 3) + +g.setField(0, 0, t) +g.setField(0, 1, li, (0, 1, 0, 1)) +g.setField(0, 2, sg) + g.place(1, 1) screen.gridWrappedWindow(g, "title") f = Form() +f.add(li) f.add(b) f.add(e) f.add(l) f.add(cb) f.add(r1) f.add(r2) +f.add(t) f.run() @@ -38,5 +51,6 @@ screen.finish() print "val", e.value() 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 diff --git a/snack.py b/snack.py index 552e3d8..59fe694 100644 --- a/snack.py +++ b/snack.py @@ -30,6 +30,24 @@ class SingleRadioButton(Widget): else: self.w = _snack.radiobutton(text, None, isOn) +class Listbox(Widget): + + def append(self, text): + return self.w.listboxAddItem(text) + + def current(self): + return self.w.listboxGetCurrent() + + def __init__(self, height, scroll = 0, returnExit = 0, width = 0): + self.w = _snack.listbox(height, scroll, returnExit) + if (width): + self.w.listboxSetWidth(width) + +class Textbox(Widget): + + def __init__(self, width, height, text, scroll = 0): + self.w = _snack.textbox(width, height, text, scroll) + class Label(Widget): def __init__(self, text): @@ -62,8 +80,31 @@ class Grid: def place(self, x, y): return self.g.place(x, y) - def setField(self, col, row, what, padding = (0, 0, 0, 0)): - return self.g.setfield(col, row, what.w, padding) + def setField(self, col, row, what, padding = (0, 0, 0, 0), + anchorLeft = 0, anchorTop = 0, anchorRight = 0, + anchorBottom = 0, growx = 0, growy = 0): + anchorFlags = 0 + if (anchorLeft): + anchorFlags = _snack.ANCHOR_LEFT + elif (anchorRight): + anchorFlags = _snack.ANCHOR_RIGHT + + if (anchorTop): + anchorFlags = anchorFlags | _snack.ANCHOR_TOP + elif (anchorBottom): + anchorFlags = anchorFlags | _snack.ANCHOR_BOTTOM + + gridFlags = 0 + if (growx): + gridFlags = _snack.GRID_GROWX + if (growy): + gridFlags = gridFlags | _snack.GRID_GROWY + + if (what.__dict__.has_key('g')): + return self.g.setfield(col, row, what.g, padding, anchorFlags, + gridFlags) + else: + return self.g.setfield(col, row, what.w, padding, anchorFlags) def __init__(self, *args): self.g = apply(_snack.grid, args) @@ -90,3 +131,8 @@ class SnackScreen: def refresh(self): return _snack.refresh() + +# returns a tuple of the wrapped text, the actual width, and the actual height +def reflow(text, width, flexDown = 5, flexUp = 5): + return _snack.reflow(text, width, flexDown, flexUp) + diff --git a/snackmodule.c b/snackmodule.c index f497a70..36e8858 100644 --- a/snackmodule.c +++ b/snackmodule.c @@ -24,11 +24,14 @@ static PyObject * gridWrappedWindow(PyObject * s, PyObject * args); static PyObject * finishScreen(PyObject * s, PyObject * args); static PyObject * initScreen(PyObject * s, PyObject * args); static snackWidget * labelWidget(PyObject * s, PyObject * args); +static snackWidget * listboxWidget(PyObject * s, PyObject * args); static PyObject * messageWindow(PyObject * s, PyObject * args); static PyObject * openWindow(PyObject * s, PyObject * args); static PyObject * popWindow(PyObject * s, PyObject * args); static snackWidget * radioButtonWidget(PyObject * s, PyObject * args); static PyObject * refreshScreen(PyObject * s, PyObject * args); +static PyObject * reflowText(PyObject * s, PyObject * args); +static snackWidget * textWidget(PyObject * s, PyObject * args); static PyObject * ternaryWindow(PyObject * s, PyObject * args); static PyMethodDef snackModuleMethods[] = { @@ -43,12 +46,15 @@ static PyMethodDef snackModuleMethods[] = { { "gridwrappedwindow", gridWrappedWindow, METH_VARARGS, NULL }, { "init", initScreen, METH_VARARGS, NULL }, { "label", (PyCFunction) labelWidget, METH_VARARGS, NULL }, + { "listbox", (PyCFunction) listboxWidget, METH_VARARGS, NULL }, { "message", messageWindow, METH_VARARGS, NULL }, { "openwindow", openWindow, METH_VARARGS, NULL }, { "popwindow", popWindow, METH_VARARGS, NULL }, { "radiobutton", (PyCFunction) radioButtonWidget, METH_VARARGS, NULL }, + { "reflow", (PyCFunction) reflowText, METH_VARARGS, NULL }, { "refresh", refreshScreen, METH_VARARGS, NULL }, { "ternary", ternaryWindow, METH_VARARGS, NULL }, + { "textbox", (PyCFunction) textWidget, METH_VARARGS, NULL }, { NULL } } ; @@ -121,13 +127,20 @@ struct snackWidget_s { newtComponent co; char achar; void * apointer; + int anint; } ; static PyObject * widgetGetAttr(PyObject * s, char * name); static PyObject * widgetEntrySetValue(snackWidget * s, PyObject * args); +static PyObject * widgetListboxSetW(snackWidget * s, PyObject * args); +static PyObject * widgetListboxAdd(snackWidget * s, PyObject * args); +static PyObject * widgetListboxGet(snackWidget * s, PyObject * args); static PyMethodDef widgetMethods[] = { { "entrySetValue", (PyCFunction) widgetEntrySetValue, METH_VARARGS, NULL }, + { "listboxAddItem", (PyCFunction) widgetListboxAdd, METH_VARARGS, NULL }, + { "listboxGetCurrent", (PyCFunction) widgetListboxGet, METH_VARARGS, NULL }, + { "listboxSetWidth", (PyCFunction) widgetListboxSetW, METH_VARARGS, NULL }, { NULL } }; @@ -168,6 +181,23 @@ static PyObject * refreshScreen(PyObject * s, PyObject * args) { return Py_None; } +static PyObject * reflowText(PyObject * s, PyObject * args) { + char * text, * new; + int width, minus = 5, plus = 5; + int realWidth, realHeight; + PyObject * tuple; + + if (!PyArg_ParseTuple(args, "si|ii", &text, &width, &minus, &plus)) + return NULL; + + new = newtReflowText(text, width, minus, plus, &realWidth, &realHeight); + + tuple = Py_BuildValue("(sii)", new, realWidth, realHeight); + free(new); + + return tuple; +} + static PyObject * centeredWindow(PyObject * s, PyObject * args) { int width, height; char * title; @@ -283,6 +313,40 @@ static snackWidget * labelWidget(PyObject * s, PyObject * args) { return widget; } +static snackWidget * listboxWidget(PyObject * s, PyObject * args) { + snackWidget * widget; + int height; + int doScroll = 0, returnExit = 0 ; + + if (!PyArg_ParseTuple(args, "i|ii", &height, &doScroll, &returnExit)) + return NULL; + + widget = PyObject_NEW(snackWidget, &snackWidgetType); + widget->co = newtListbox(-1, -1, height, + (doScroll ? 0 : NEWT_FLAG_NOSCROLL) | + (returnExit ? NEWT_FLAG_RETURNEXIT : 0)); + widget->anint = 0; + + return widget; +} + +static snackWidget * textWidget(PyObject * s, PyObject * args) { + char * text; + int width, height; + int scrollBar = 0; + snackWidget * widget; + + if (!PyArg_ParseTuple(args, "iis|i", &width, &height, &text, &scrollBar)) + return NULL; + + widget = PyObject_NEW(snackWidget, &snackWidgetType); + widget->co = newtTextbox(-1, -1, width, height, + scrollBar ? NEWT_FLAG_SCROLL : 0); + newtTextboxSetText(widget->co, text); + + return widget; +} + static snackWidget * radioButtonWidget(PyObject * s, PyObject * args) { snackWidget * widget, * group; char * text; @@ -370,15 +434,26 @@ static PyObject * gridPlace(snackGrid * grid, PyObject * args) { static PyObject * gridSetField(snackGrid * grid, PyObject * args) { snackWidget * w; + snackGrid * g; int x, y; int pLeft = 0, pTop = 0, pRight = 0, pBottom = 0; + int anchorFlags = 0, growFlags = 0; - if (!PyArg_ParseTuple(args, "iiO!|(iiii)", &x, &y, &snackWidgetType, - &w, &pLeft, &pTop, &pRight, &pBottom)) + if (!PyArg_ParseTuple(args, "iiO|(iiii)ii", &x, &y, + &w, &pLeft, &pTop, &pRight, &pBottom, + &anchorFlags, &growFlags)) return NULL; - newtGridSetField(grid->grid, x, y, NEWT_GRID_COMPONENT, - w->co, pLeft, pTop, pRight, pBottom, 0, 0); + if (w->ob_type == &snackWidgetType) { + newtGridSetField(grid->grid, x, y, NEWT_GRID_COMPONENT, + w->co, pLeft, pTop, pRight, pBottom, anchorFlags, + growFlags); + } else { + g = (snackGrid *) w; + newtGridSetField(grid->grid, x, y, NEWT_GRID_SUBGRID, + g->grid, pLeft, pTop, pRight, pBottom, anchorFlags, + growFlags); + } Py_INCREF(Py_None); return Py_None; @@ -442,9 +517,50 @@ static PyObject * widgetEntrySetValue(snackWidget * s, PyObject * args) { return Py_None; } +static PyObject * widgetListboxAdd(snackWidget * s, PyObject * args) { + char * text; + + if (!PyArg_ParseTuple(args, "s", &text)) + return NULL; + + newtListboxAddEntry(s->co, text, (void *) s->anint); + + return PyInt_FromLong(s->anint++); +} + +static PyObject * widgetListboxGet(snackWidget * s, PyObject * args) { + if (!PyArg_ParseTuple(args, "")) + return NULL; + + return PyInt_FromLong((long) newtListboxGetCurrent(s->co)); +} + +static PyObject * widgetListboxSetW(snackWidget * s, PyObject * args) { + int width; + + if (!PyArg_ParseTuple(args, "i", &width)) + return NULL; + + newtListboxSetWidth(s->co, width); + + Py_INCREF(Py_None); + return Py_None; +} + static void emptyDestructor(PyObject * s) { } void init_snack(void) { - Py_InitModule("_snack", snackModuleMethods); + PyObject * d, * m; + + m = Py_InitModule("_snack", snackModuleMethods); + d = PyModule_GetDict(m); + + PyDict_SetItemString(d, "ANCHOR_LEFT", PyInt_FromLong(NEWT_ANCHOR_LEFT)); + PyDict_SetItemString(d, "ANCHOR_TOP", PyInt_FromLong(NEWT_ANCHOR_TOP)); + PyDict_SetItemString(d, "ANCHOR_RIGHT", PyInt_FromLong(NEWT_ANCHOR_RIGHT)); + PyDict_SetItemString(d, "ANCHOR_BOTTOM", + 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)); }