From: ewt Date: Thu, 16 Oct 1997 22:40:34 +0000 (+0000) Subject: began work on snacks X-Git-Tag: r0-20~34 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=80d03bd8fd43c98f67d4c8180bbc54604aad68f6;p=thirdparty%2Fnewt.git began work on snacks fixed buttons to initialize properly w/ a window isn't already present --- diff --git a/Makefile b/Makefile index c96e589..8e7c3a0 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ else TARGET=depend $(PROGS) endif -all: $(TARGET) +all: $(TARGET) _snackmodule.so test: $(TESTOBJS) $(LIBNEWT) gcc -g -o test $(TESTOBJS) $(LIBNEWT) $(LIBS) @@ -48,11 +48,17 @@ test: $(TESTOBJS) $(LIBNEWT) testgrid: testgrid.o $(LIBNEWT) gcc -g -o testgrid testgrid.o $(LIBNEWT) $(LIBS) +_snackmodule.so: snackmodule.o $(LIBNEWTSH) + gcc --shared -o _snackmodule.so snackmodule.o -L . $(LIBNEWTSH) + +snackmodule.o: snackmodule.c + gcc -I/usr/include/python1.4 -fPIC $(CFLAGS) -c snackmodule.c + whiptail: $(NDIALOGOBJS) $(LIBNEWTSH) - gcc -g -o whiptail $(NDIALOGOBJS) $(LIBNEWTSH) $(LIBS) -lpopt + gcc -g -o whiptail $(NDIALOGOBJS) -L . $(LIBNEWTSH) $(LIBS) -lpopt whiptcl.so: $(WHIPTCLOBJS) $(LIBNEWTSH) - gcc -shared -o whiptcl.so $(WHIPTCLOBJS) $(LIBNEWTSH) -ltcl -lslang -lpopt -lm + gcc -shared -o whiptcl.so $(WHIPTCLOBJS) -L . $(LIBNEWTSH) -ltcl -lslang -lpopt -lm $(LIBNEWT): $(LIBNEWT)($(LIBOBJS)) diff --git a/button.c b/button.c index 0846cf0..a06bae4 100644 --- a/button.c +++ b/button.c @@ -52,7 +52,7 @@ static newtComponent createButton(int left, int row, const char * text, int comp co->takesFocus = 1; newtGotorc(co->top, co->left); - bu->bgColor = (SLsmg_char_at() >> 8) & 0xFF; + bu->bgColor = -1; return co; } @@ -87,6 +87,9 @@ static void buttonDraw(newtComponent co) { static void buttonDrawIt(newtComponent co, int active, int pushed) { struct button * bu = co->data; + if (bu->bgColor == -1) + bu->bgColor = (SLsmg_char_at() >> 8) & 0xFF; + SLsmg_set_color(NEWT_COLORSET_BUTTON); if (bu->compact) { diff --git a/popcorn.py b/popcorn.py new file mode 100755 index 0000000..1d8d302 --- /dev/null +++ b/popcorn.py @@ -0,0 +1,42 @@ +#!/usr/bin/python + +from snack import * + +screen = SnackScreen() + +b = Button("Button") +e = Entry(15, "Entry") +l = Label("label") +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)) +g.place(1, 1) + +screen.gridWrappedWindow(g, "title") + +f = Form() +f.add(b) +f.add(e) +f.add(l) +f.add(cb) +f.add(r1) +f.add(r2) + +f.run() + +screen.popWindow() + +screen.finish() + +print "val", e.value() +print "check", cb.value() + +print "r1", r1.selected() diff --git a/snack.py b/snack.py new file mode 100644 index 0000000..552e3d8 --- /dev/null +++ b/snack.py @@ -0,0 +1,92 @@ +import _snack + +class Widget: + pass + +class Button(Widget): + + def __init__(self, text): + self.w = _snack.button(text) + +class Checkbox(Widget): + + def value(self): + return self.w.checkboxValue + + def selected(self): + return self.w.checkboxValue != 0 + + def __init__(self, text, isOn = 0): + self.w = _snack.checkbox(text, isOn) + +class SingleRadioButton(Widget): + + def selected(self): + return self.w.radiobuttonkey == self.w.radioValue; + + def __init__(self, text, group, isOn = 0): + if group: + self.w = _snack.radiobutton(text, group.w, isOn) + else: + self.w = _snack.radiobutton(text, None, isOn) + +class Label(Widget): + + def __init__(self, text): + self.w = _snack.label(text) + +class Entry(Widget): + + def value(self): + return self.w.entryValue + + def set(self, text): + return self.w.entrySetValue(text) + + def __init__(self, width, text = "", hidden = 0, scroll = 1): + self.w = _snack.entry(width, text, hidden, scroll) + +class Form: + + def add(self, widget): + return self.g.add(widget.w) + + def run(self): + return self.g.run() + + def __init__(self): + self.g = _snack.form() + +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 __init__(self, *args): + self.g = apply(_snack.grid, args) + +class SnackScreen: + + def __init__(self): + _snack.init() + + def finish(self): + return _snack.finish() + + def openWindow(self, left, top, width, height, title): + return _snack.openwindow(left, top, width, height, title) + + def centeredWindow(self, width, height, title): + return _snack.centeredwindow(width, height, title) + + def gridWrappedWindow(self, grid, title): + return _snack.gridwrappedwindow(grid.g, title) + + def popWindow(self): + return _snack.popwindow() + + def refresh(self): + return _snack.refresh() diff --git a/snackmodule.c b/snackmodule.c new file mode 100644 index 0000000..f497a70 --- /dev/null +++ b/snackmodule.c @@ -0,0 +1,450 @@ +#include +#include +#include +#include +#include + +#include "Python.h" +#include "newt.h" + +typedef struct snackWidget_s snackWidget; +typedef struct snackGrid_s snackGrid; +typedef struct snackForm_s snackForm; + +static void emptyDestructor(PyObject * s); + +static snackWidget * buttonWidget(PyObject * s, PyObject * args); +static PyObject * centeredWindow(PyObject * s, PyObject * args); +static snackWidget * checkboxWidget(PyObject * s, PyObject * args); +static PyObject * choiceWindow(PyObject * s, PyObject * args); +static snackWidget * entryWidget(PyObject * s, PyObject * args); +static snackForm * formCreate(PyObject * s, PyObject * args); +static snackGrid * gridCreate(PyObject * s, PyObject * args); +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 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 * ternaryWindow(PyObject * s, PyObject * args); + +static PyMethodDef snackModuleMethods[] = { + { "button", (PyCFunction) buttonWidget, METH_VARARGS, NULL }, + { "checkbox", (PyCFunction) checkboxWidget, METH_VARARGS, NULL }, + { "choice", choiceWindow, METH_VARARGS, NULL }, + { "centeredwindow", centeredWindow, METH_VARARGS, NULL }, + { "entry", (PyCFunction) entryWidget, METH_VARARGS, NULL }, + { "finish", finishScreen, METH_VARARGS, NULL }, + { "form", (PyCFunction) formCreate, METH_VARARGS, NULL }, + { "grid", (PyCFunction) gridCreate, METH_VARARGS, NULL }, + { "gridwrappedwindow", gridWrappedWindow, METH_VARARGS, NULL }, + { "init", initScreen, METH_VARARGS, NULL }, + { "label", (PyCFunction) labelWidget, METH_VARARGS, NULL }, + { "message", messageWindow, METH_VARARGS, NULL }, + { "openwindow", openWindow, METH_VARARGS, NULL }, + { "popwindow", popWindow, METH_VARARGS, NULL }, + { "radiobutton", (PyCFunction) radioButtonWidget, METH_VARARGS, NULL }, + { "refresh", refreshScreen, METH_VARARGS, NULL }, + { "ternary", ternaryWindow, METH_VARARGS, NULL }, + { NULL } +} ; + +struct snackGrid_s { + PyObject_HEAD; + newtGrid grid; +} ; + +static PyObject * gridGetAttr(PyObject * s, char * name); +static PyObject * gridPlace(snackGrid * s, PyObject * args); +static PyObject * gridSetField(snackGrid * s, PyObject * args); + +static PyMethodDef gridMethods[] = { + { "place", (PyCFunction) gridPlace, METH_VARARGS, NULL }, + { "setfield", (PyCFunction) gridSetField, METH_VARARGS, NULL }, + { NULL } +}; + +static PyTypeObject snackGridType = { + PyObject_HEAD_INIT(&PyType_Type) + 0, /* ob_size */ + "snackgrid", /* tp_name */ + sizeof(snackGrid), /* tp_size */ + 0, /* tp_itemsize */ + emptyDestructor, /* tp_dealloc */ + 0, /* tp_print */ + gridGetAttr, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_compare */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ +}; + +struct snackForm_s { + PyObject_HEAD; + newtComponent fo; +} ; + +static PyObject * formGetAttr(PyObject * s, char * name); +static PyObject * formAdd(snackForm * s, PyObject * args); +static PyObject * formRun(snackForm * s, PyObject * args); + +static PyMethodDef formMethods[] = { + { "add", (PyCFunction) formAdd, METH_VARARGS, NULL }, + { "run", (PyCFunction) formRun, METH_VARARGS, NULL }, + { NULL } +}; + +static PyTypeObject snackFormType = { + PyObject_HEAD_INIT(&PyType_Type) + 0, /* ob_size */ + "snackform", /* tp_name */ + sizeof(snackForm), /* tp_size */ + 0, /* tp_itemsize */ + emptyDestructor, /* tp_dealloc */ + 0, /* tp_print */ + formGetAttr, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_compare */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ +}; + +struct snackWidget_s { + PyObject_HEAD; + newtComponent co; + char achar; + void * apointer; +} ; + +static PyObject * widgetGetAttr(PyObject * s, char * name); +static PyObject * widgetEntrySetValue(snackWidget * s, PyObject * args); + +static PyMethodDef widgetMethods[] = { + { "entrySetValue", (PyCFunction) widgetEntrySetValue, METH_VARARGS, NULL }, + { NULL } +}; + +static PyTypeObject snackWidgetType = { + PyObject_HEAD_INIT(&PyType_Type) + 0, /* ob_size */ + "snackwidget", /* tp_name */ + sizeof(snackWidget), /* tp_size */ + 0, /* tp_itemsize */ + emptyDestructor, /* tp_dealloc */ + 0, /* tp_print */ + widgetGetAttr, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_compare */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ +}; + +static PyObject * initScreen(PyObject * s, PyObject * args) { + newtInit(); + newtCls(); + + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * finishScreen(PyObject * s, PyObject * args) { + newtFinished(); + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * refreshScreen(PyObject * s, PyObject * args) { + newtRefresh(); + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * centeredWindow(PyObject * s, PyObject * args) { + int width, height; + char * title; + + if (!PyArg_ParseTuple(args, "iis", &width, &height, &title)) + return NULL; + + newtCenteredWindow(width, height, title); + + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * gridWrappedWindow(PyObject * s, PyObject * args) { + snackGrid * grid; + char * title; + + if (!PyArg_ParseTuple(args, "O!s", &snackGridType, &grid, &title)) + return NULL; + + newtGridWrappedWindow(grid->grid, title); + + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * openWindow(PyObject * s, PyObject * args) { + int left, top, width, height; + char * title; + + if (!PyArg_ParseTuple(args, "iiiis", &left, &top, &width, &height, &title)) + return NULL; + + newtOpenWindow(left, top, width, height, title); + + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * popWindow(PyObject * s, PyObject * args) { + newtPopWindow(); + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * messageWindow(PyObject * s, PyObject * args) { + char * title, * text; + char * okbutton = "Ok"; + + if (!PyArg_ParseTuple(args, "ss|s", &title, &text, &okbutton)) + return NULL; + + newtWinMessage(title, okbutton, text); + + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * choiceWindow(PyObject * s, PyObject * args) { + char * title, * text; + char * okbutton = "Ok"; + char * cancelbutton = "Cancel"; + int rc; + + if (!PyArg_ParseTuple(args, "ss|ss", &title, &text, &okbutton, + &cancelbutton)) + return NULL; + + rc = newtWinChoice(title, okbutton, cancelbutton, text); + + switch (rc) { + case 0: return Py_BuildValue("i", 1); + case 1: return Py_BuildValue("i", 2); + } + + return Py_BuildValue("i", 0); +} + +static PyObject * ternaryWindow(PyObject * s, PyObject * args) { + char * title, * text, * button1, * button2, * button3; + int rc; + + if (!PyArg_ParseTuple(args, "sssss", &title, &text, &button1, &button2, + &button3)) + return NULL; + + rc = newtWinTernary(title, button1, button2, button3, text); + + return Py_BuildValue("i", rc); +} + +static snackWidget * buttonWidget(PyObject * s, PyObject * args) { + snackWidget * widget; + char * label; + + if (!PyArg_ParseTuple(args, "s", &label)) return NULL; + + widget = PyObject_NEW(snackWidget, &snackWidgetType); + widget->co = newtButton(-1, -1, label); + + return widget; +} + +static snackWidget * labelWidget(PyObject * s, PyObject * args) { + char * label; + snackWidget * widget; + + if (!PyArg_ParseTuple(args, "s", &label)) return NULL; + + widget = PyObject_NEW(snackWidget, &snackWidgetType); + widget->co = newtLabel(-1, -1, label); + + return widget; +} + +static snackWidget * radioButtonWidget(PyObject * s, PyObject * args) { + snackWidget * widget, * group; + char * text; + int isOn; + + if (!PyArg_ParseTuple(args, "sOi", &text, &group, &isOn)) + return NULL; + + widget = PyObject_NEW(snackWidget, &snackWidgetType); + + if ((PyObject *) group == Py_None) + widget->co = newtRadiobutton(-1, -1, text, isOn, NULL); + else + widget->co = newtRadiobutton(-1, -1, text, isOn, group->co); + + return widget; +} + +static snackWidget * checkboxWidget(PyObject * s, PyObject * args) { + snackWidget * widget; + char * text; + int isOn; + + if (!PyArg_ParseTuple(args, "si", &text, &isOn)) return NULL; + + widget = PyObject_NEW(snackWidget, &snackWidgetType); + widget->co = newtCheckbox(-1, -1, text, isOn ? '*' : ' ', NULL, + &widget->achar); + + return widget; +} + +static snackWidget * entryWidget(PyObject * s, PyObject * args) { + snackWidget * widget; + int width; + char * initial; + int isHidden, isScrolled; + + if (!PyArg_ParseTuple(args, "isii", &width, &initial, + &isHidden, &isScrolled)) return NULL; + + widget = PyObject_NEW(snackWidget, &snackWidgetType); + widget->co = newtEntry(-1, -1, initial, width, (char **) &widget->apointer, + (isHidden ? NEWT_FLAG_HIDDEN : 0) | + (!isScrolled ? NEWT_FLAG_NOSCROLL : 0)); + + return widget; +} + +static snackForm * formCreate(PyObject * s, PyObject * args) { + snackForm * form; + + form = PyObject_NEW(snackForm, &snackFormType); + form->fo = newtForm(NULL, NULL, 0); + + return form; +} + +static snackGrid * gridCreate(PyObject * s, PyObject * args) { + int rows, cols; + snackGrid * grid; + + if (!PyArg_ParseTuple(args, "ii", &cols, &rows)) return NULL; + + grid = PyObject_NEW(snackGrid, &snackGridType); + grid->grid = newtCreateGrid(cols, rows); + + return grid; +} + +static PyObject * gridGetAttr(PyObject * s, char * name) { + return Py_FindMethod(gridMethods, s, name); +} + +static PyObject * gridPlace(snackGrid * grid, PyObject * args) { + int x, y; + + if (!PyArg_ParseTuple(args, "ii", &x, &y)) return NULL; + + newtGridPlace(grid->grid, x, y); + + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * gridSetField(snackGrid * grid, PyObject * args) { + snackWidget * w; + int x, y; + int pLeft = 0, pTop = 0, pRight = 0, pBottom = 0; + + if (!PyArg_ParseTuple(args, "iiO!|(iiii)", &x, &y, &snackWidgetType, + &w, &pLeft, &pTop, &pRight, &pBottom)) + return NULL; + + newtGridSetField(grid->grid, x, y, NEWT_GRID_COMPONENT, + w->co, pLeft, pTop, pRight, pBottom, 0, 0); + + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * formGetAttr(PyObject * s, char * name) { + return Py_FindMethod(formMethods, s, name); +} + +static PyObject * formAdd(snackForm * s, PyObject * args) { + snackWidget * w; + int size = PyTuple_Size(args), i; + + if (!size) { + /* this is a hack, I should give an error directly */ + if (!PyArg_ParseTuple(args, "O!", &snackWidgetType, &w)) + return NULL; + } + + for (i = 0; i < size; i++) { + w = (snackWidget *) PyTuple_GET_ITEM(args, i); + newtFormAddComponent(s->fo, w->co); + } + + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * formRun(snackForm * s, PyObject * args) { + newtRunForm(s->fo); + + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * widgetGetAttr(PyObject * s, char * name) { + snackWidget * w = (snackWidget *) s; + + if (!strcmp(name, "radiobuttonkey")) { + return Py_BuildValue("i", w->co); + } else if (!strcmp(name, "entryValue")) { + return Py_BuildValue("s", w->apointer); + } else if (!strcmp(name, "checkboxValue")) { + return Py_BuildValue("i", w->achar == ' ' ? 0 : 1); + } else if (!strcmp(name, "radioValue")) { + return Py_BuildValue("i", newtRadioGetCurrent(w->co)); + } + + return Py_FindMethod(widgetMethods, s, name); +} + +static PyObject * widgetEntrySetValue(snackWidget * s, PyObject * args) { + char * val; + + if (!PyArg_ParseTuple(args, "s", &val)) + return NULL; + + newtEntrySet(s->co, val, 1); + + Py_INCREF(Py_None); + return Py_None; +} + +static void emptyDestructor(PyObject * s) { +} + +void init_snack(void) { + Py_InitModule("_snack", snackModuleMethods); +}