10 typedef struct snackWidget_s snackWidget
;
11 typedef struct snackGrid_s snackGrid
;
12 typedef struct snackForm_s snackForm
;
14 struct callbackStruct
{
15 PyObject
* cb
, * data
;
18 static struct callbackStruct suspend
;
19 static struct callbackStruct helpCallback
;
21 static void emptyDestructor(PyObject
* s
);
23 static snackWidget
* buttonWidget(PyObject
* s
, PyObject
* args
);
24 static PyObject
* centeredWindow(PyObject
* s
, PyObject
* args
);
25 static snackWidget
* checkboxWidget(PyObject
* s
, PyObject
* args
);
26 static PyObject
* choiceWindow(PyObject
* s
, PyObject
* args
);
27 static snackWidget
* entryWidget(PyObject
* s
, PyObject
* args
);
28 static PyObject
* drawRootText(PyObject
* s
, PyObject
* args
);
29 static PyObject
* doResume(PyObject
* s
, PyObject
* args
);
30 static PyObject
* doSuspend(PyObject
* s
, PyObject
* args
);
31 static PyObject
* doSuspend(PyObject
* s
, PyObject
* args
);
32 static snackForm
* formCreate(PyObject
* s
, PyObject
* args
);
33 static snackGrid
* gridCreate(PyObject
* s
, PyObject
* args
);
34 static PyObject
* gridWrappedWindow(PyObject
* s
, PyObject
* args
);
35 static PyObject
* finishScreen(PyObject
* s
, PyObject
* args
);
36 static PyObject
* initScreen(PyObject
* s
, PyObject
* args
);
37 static snackWidget
* labelWidget(PyObject
* s
, PyObject
* args
);
38 static snackWidget
* listboxWidget(PyObject
* s
, PyObject
* args
);
39 static PyObject
* messageWindow(PyObject
* s
, PyObject
* args
);
40 static PyObject
* openWindow(PyObject
* s
, PyObject
* args
);
41 static PyObject
* popHelpLine(PyObject
* s
, PyObject
* args
);
42 static PyObject
* popWindow(PyObject
* s
, PyObject
* args
);
43 static PyObject
* pushHelpLine(PyObject
* s
, PyObject
* args
);
44 static snackWidget
* radioButtonWidget(PyObject
* s
, PyObject
* args
);
45 static PyObject
* refreshScreen(PyObject
* s
, PyObject
* args
);
46 static PyObject
* scaleWidget(PyObject
* s
, PyObject
* args
);
47 static PyObject
* scaleSet(snackWidget
* s
, PyObject
* args
);
48 static PyObject
* screenSize(PyObject
* s
, PyObject
* args
);
49 static PyObject
* setSuspendCallback(PyObject
* s
, PyObject
* args
);
50 static PyObject
* setHelpCallback(PyObject
* s
, PyObject
* args
);
51 static PyObject
* reflowText(PyObject
* s
, PyObject
* args
);
52 static snackWidget
* textWidget(PyObject
* s
, PyObject
* args
);
53 static PyObject
* ternaryWindow(PyObject
* s
, PyObject
* args
);
54 static snackWidget
* checkboxTreeWidget(PyObject
* s
, PyObject
* args
);
56 static PyMethodDef snackModuleMethods
[] = {
57 { "button", (PyCFunction
) buttonWidget
, METH_VARARGS
, NULL
},
58 { "checkbox", (PyCFunction
) checkboxWidget
, METH_VARARGS
, NULL
},
59 { "choice", choiceWindow
, METH_VARARGS
, NULL
},
60 { "centeredwindow", centeredWindow
, METH_VARARGS
, NULL
},
61 { "drawroottext", drawRootText
, METH_VARARGS
, NULL
},
62 { "entry", (PyCFunction
) entryWidget
, METH_VARARGS
, NULL
},
63 { "finish", finishScreen
, METH_VARARGS
, NULL
},
64 { "form", (PyCFunction
) formCreate
, METH_VARARGS
, NULL
},
65 { "grid", (PyCFunction
) gridCreate
, METH_VARARGS
, NULL
},
66 { "gridwrappedwindow", gridWrappedWindow
, METH_VARARGS
, NULL
},
67 { "helpcallback", setHelpCallback
, METH_VARARGS
, NULL
},
68 { "init", initScreen
, METH_VARARGS
, NULL
},
69 { "label", (PyCFunction
) labelWidget
, METH_VARARGS
, NULL
},
70 { "listbox", (PyCFunction
) listboxWidget
, METH_VARARGS
, NULL
},
71 { "message", messageWindow
, METH_VARARGS
, NULL
},
72 { "openwindow", openWindow
, METH_VARARGS
, NULL
},
73 { "pophelpline", popHelpLine
, METH_VARARGS
, NULL
},
74 { "popwindow", popWindow
, METH_VARARGS
, NULL
},
75 { "pushhelpline", pushHelpLine
, METH_VARARGS
, NULL
},
76 { "radiobutton", (PyCFunction
) radioButtonWidget
, METH_VARARGS
, NULL
},
77 { "reflow", (PyCFunction
) reflowText
, METH_VARARGS
, NULL
},
78 { "refresh", refreshScreen
, METH_VARARGS
, NULL
},
79 { "resume", doResume
, METH_VARARGS
, NULL
},
80 { "scale", scaleWidget
, METH_VARARGS
, NULL
},
81 { "size", screenSize
, METH_VARARGS
, NULL
},
82 { "suspend", doSuspend
, METH_VARARGS
, NULL
},
83 { "suspendcallback", setSuspendCallback
, METH_VARARGS
, NULL
},
84 { "ternary", ternaryWindow
, METH_VARARGS
, NULL
},
85 { "textbox", (PyCFunction
) textWidget
, METH_VARARGS
, NULL
},
86 { "checkboxtree", (PyCFunction
) checkboxTreeWidget
, METH_VARARGS
, NULL
},
95 static PyObject
* gridGetAttr(PyObject
* s
, char * name
);
96 static PyObject
* gridPlace(snackGrid
* s
, PyObject
* args
);
97 static PyObject
* gridSetField(snackGrid
* s
, PyObject
* args
);
99 static PyMethodDef gridMethods
[] = {
100 { "place", (PyCFunction
) gridPlace
, METH_VARARGS
, NULL
},
101 { "setfield", (PyCFunction
) gridSetField
, METH_VARARGS
, NULL
},
105 static PyTypeObject snackGridType
= {
106 PyObject_HEAD_INIT(&PyType_Type
)
108 "snackgrid", /* tp_name */
109 sizeof(snackGrid
), /* tp_size */
111 emptyDestructor
, /* tp_dealloc */
113 gridGetAttr
, /* tp_getattr */
117 0, /* tp_as_number */
118 0, /* tp_as_sequence */
119 0, /* tp_as_mapping */
127 static PyObject
* formGetAttr(PyObject
* s
, char * name
);
128 static PyObject
* formAdd(snackForm
* s
, PyObject
* args
);
129 static PyObject
* formDraw(snackForm
* s
, PyObject
* args
);
130 static PyObject
* formRun(snackForm
* s
, PyObject
* args
);
131 static PyObject
* formHotKey(snackForm
* s
, PyObject
* args
);
132 static PyObject
* formSetCurrent(snackForm
* form
, PyObject
* args
);
133 static PyObject
* formSetTimer(snackForm
* form
, PyObject
* args
);
135 static PyMethodDef formMethods
[] = {
136 { "add", (PyCFunction
) formAdd
, METH_VARARGS
, NULL
},
137 { "draw", (PyCFunction
) formDraw
, METH_VARARGS
, NULL
},
138 { "run", (PyCFunction
) formRun
, METH_VARARGS
, NULL
},
139 { "addhotkey", (PyCFunction
) formHotKey
, METH_VARARGS
, NULL
},
140 { "setcurrent", (PyCFunction
) formSetCurrent
, METH_VARARGS
, NULL
},
141 { "settimer", (PyCFunction
) formSetTimer
, METH_VARARGS
, NULL
},
145 static PyTypeObject snackFormType
= {
146 PyObject_HEAD_INIT(&PyType_Type
)
148 "snackform", /* tp_name */
149 sizeof(snackForm
), /* tp_size */
151 emptyDestructor
, /* tp_dealloc */
153 formGetAttr
, /* tp_getattr */
157 0, /* tp_as_number */
158 0, /* tp_as_sequence */
159 0, /* tp_as_mapping */
162 struct snackWidget_s
{
168 struct callbackStruct scs
;
171 static PyObject
* widgetAddCallback(snackWidget
* s
, PyObject
* args
);
172 static PyObject
* widgetGetAttr(PyObject
* s
, char * name
);
173 static void widgetDestructor(PyObject
* s
);
174 static PyObject
* widgetEntrySetValue(snackWidget
* s
, PyObject
* args
);
175 static PyObject
* widgetLabelText(snackWidget
* s
, PyObject
* args
);
176 static PyObject
* widgetListboxSetW(snackWidget
* s
, PyObject
* args
);
177 static PyObject
* widgetListboxAdd(snackWidget
* s
, PyObject
* args
);
178 static PyObject
* widgetListboxIns(snackWidget
* s
, PyObject
* args
);
179 static PyObject
* widgetListboxDel(snackWidget
* s
, PyObject
* args
);
180 static PyObject
* widgetListboxGet(snackWidget
* s
, PyObject
* args
);
181 static PyObject
* widgetListboxSet(snackWidget
* s
, PyObject
* args
);
182 static PyObject
* widgetTextboxText(snackWidget
* s
, PyObject
* args
);
183 static PyObject
* widgetCheckboxTreeAddItem(snackWidget
* s
, PyObject
* args
);
184 static PyObject
* widgetCheckboxTreeGetSel(snackWidget
* s
, PyObject
* args
);
185 static PyObject
* widgetCheckboxTreeGetCur(snackWidget
* s
, PyObject
* args
);
186 static PyObject
* widgetCheckboxTreeSetEntry(snackWidget
* s
, PyObject
* args
);
187 static PyObject
* widgetCheckboxTreeSetEntryValue(snackWidget
* s
, PyObject
* args
);
188 static PyObject
* widgetCheckboxTreeGetEntryValue(snackWidget
* s
, PyObject
* args
);
189 static PyObject
* widgetEntrySetFlags(snackWidget
* s
, PyObject
* args
);
190 static PyObject
* widgetCheckboxSetFlags(snackWidget
* s
, PyObject
* args
);
191 static PyObject
* widgetCheckboxSetValue(snackWidget
* s
, PyObject
* args
);
193 static PyMethodDef widgetMethods
[] = {
194 { "setCallback", (PyCFunction
) widgetAddCallback
, METH_VARARGS
, NULL
},
195 { "labelText", (PyCFunction
) widgetLabelText
, METH_VARARGS
, NULL
},
196 { "textboxText", (PyCFunction
) widgetTextboxText
, METH_VARARGS
, NULL
},
197 { "entrySetValue", (PyCFunction
) widgetEntrySetValue
, METH_VARARGS
, NULL
},
198 { "listboxAddItem", (PyCFunction
) widgetListboxAdd
, METH_VARARGS
, NULL
},
199 { "listboxInsertItem", (PyCFunction
) widgetListboxIns
, METH_VARARGS
, NULL
},
200 { "listboxGetCurrent", (PyCFunction
) widgetListboxGet
, METH_VARARGS
, NULL
},
201 { "listboxSetCurrent", (PyCFunction
) widgetListboxSet
, METH_VARARGS
, NULL
},
202 { "listboxSetWidth", (PyCFunction
) widgetListboxSetW
, METH_VARARGS
, NULL
},
203 { "listboxDeleteItem", (PyCFunction
) widgetListboxDel
, METH_VARARGS
, NULL
},
204 { "scaleSet", (PyCFunction
) scaleSet
, METH_VARARGS
, NULL
},
205 { "checkboxtreeAddItem", (PyCFunction
) widgetCheckboxTreeAddItem
,
206 METH_VARARGS
, NULL
},
207 { "checkboxtreeGetCurrent", (PyCFunction
) widgetCheckboxTreeGetCur
,
208 METH_VARARGS
, NULL
},
209 { "checkboxtreeGetEntryValue", (PyCFunction
) widgetCheckboxTreeGetEntryValue
,
210 METH_VARARGS
, NULL
},
211 { "checkboxtreeSetEntry", (PyCFunction
) widgetCheckboxTreeSetEntry
,
212 METH_VARARGS
, NULL
},
213 { "checkboxtreeSetEntryValue", (PyCFunction
) widgetCheckboxTreeSetEntryValue
,
214 METH_VARARGS
, NULL
},
215 { "checkboxtreeGetSelection", (PyCFunction
) widgetCheckboxTreeGetSel
,
216 METH_VARARGS
, NULL
},
217 { "entrySetFlags", (PyCFunction
) widgetEntrySetFlags
, METH_VARARGS
, NULL
},
218 { "checkboxSetFlags", (PyCFunction
) widgetCheckboxSetFlags
, METH_VARARGS
, NULL
},
219 { "checkboxSetValue", (PyCFunction
) widgetCheckboxSetValue
, METH_VARARGS
, NULL
},
223 static PyTypeObject snackWidgetType
= {
224 PyObject_HEAD_INIT(&PyType_Type
)
226 "snackwidget", /* tp_name */
227 sizeof(snackWidget
), /* tp_size */
229 widgetDestructor
, /* tp_dealloc */
231 widgetGetAttr
, /* tp_getattr */
235 0, /* tp_as_number */
236 0, /* tp_as_sequence */
237 0, /* tp_as_mapping */
240 static snackWidget
* snackWidgetNew (void) {
241 snackWidget
* widget
;
243 widget
= PyObject_NEW(snackWidget
, &snackWidgetType
);
245 widget
->scs
.cb
= NULL
;
246 widget
->scs
.data
= NULL
;
251 static PyObject
* initScreen(PyObject
* s
, PyObject
* args
) {
262 static PyObject
* finishScreen(PyObject
* s
, PyObject
* args
) {
263 Py_XDECREF (suspend
.cb
);
264 Py_XDECREF (suspend
.data
);
271 static PyObject
* refreshScreen(PyObject
* s
, PyObject
* args
) {
277 static PyObject
* scaleWidget(PyObject
* s
, PyObject
* args
) {
278 snackWidget
* widget
;
279 int width
, fullAmount
;
281 if (!PyArg_ParseTuple(args
, "ii", &width
, &fullAmount
)) return NULL
;
283 widget
= snackWidgetNew ();
284 widget
->co
= newtScale(-1, -1, width
, fullAmount
);
286 return (PyObject
*) widget
;
289 static PyObject
* scaleSet(snackWidget
* s
, PyObject
* args
) {
292 if (!PyArg_ParseTuple(args
, "i", &amount
)) return NULL
;
294 newtScaleSet(s
->co
, amount
);
300 static PyObject
* screenSize(PyObject
* s
, PyObject
* args
) {
303 if (!PyArg_ParseTuple(args
, ""))
306 newtGetScreenSize(&width
, &height
);
308 return Py_BuildValue("(ii)", width
, height
);
311 static void helpCallbackMarshall(newtComponent co
, void * data
) {
312 PyObject
* args
, * result
;
314 args
= Py_BuildValue("(O)", data
);
315 result
= PyEval_CallObject(helpCallback
.cb
, args
);
322 static void suspendCallbackMarshall(void * data
) {
323 struct callbackStruct
* scs
= data
;
324 PyObject
* args
, * result
;
327 args
= Py_BuildValue("(O)", scs
->data
);
328 result
= PyEval_CallObject(scs
->cb
, args
);
331 result
= PyEval_CallObject(scs
->cb
, NULL
);
343 static void callbackMarshall(newtComponent co
, void * data
) {
344 struct callbackStruct
* scs
= data
;
345 PyObject
* args
, * result
;
348 args
= Py_BuildValue("(O)", scs
->data
);
349 result
= PyEval_CallObject(scs
->cb
, args
);
352 result
= PyEval_CallObject(scs
->cb
, NULL
);
364 static PyObject
* setSuspendCallback(PyObject
* s
, PyObject
* args
) {
365 if (!PyArg_ParseTuple(args
, "O|O", &suspend
.cb
, &suspend
.data
))
368 Py_INCREF (suspend
.cb
);
369 Py_XINCREF (suspend
.data
);
371 newtSetSuspendCallback(suspendCallbackMarshall
, &suspend
);
377 static PyObject
* setHelpCallback(PyObject
* s
, PyObject
* args
) {
378 if (!PyArg_ParseTuple(args
, "O", &helpCallback
.cb
))
381 /*if (helpCallback.cb) {
382 Py_DECREF (helpCallback.cb);
385 Py_INCREF (helpCallback
.cb
);
387 newtSetHelpCallback(helpCallbackMarshall
);
393 static PyObject
* drawRootText(PyObject
* s
, PyObject
* args
) {
397 if (!PyArg_ParseTuple(args
, "iis", &left
, &top
, &text
))
400 newtDrawRootText(left
, top
, text
);
406 static PyObject
* doSuspend(PyObject
* s
, PyObject
* args
) {
413 static PyObject
* doResume(PyObject
* s
, PyObject
* args
) {
420 static PyObject
* popHelpLine(PyObject
* s
, PyObject
* args
) {
426 static PyObject
* pushHelpLine(PyObject
* s
, PyObject
* args
) {
429 if (!PyArg_ParseTuple(args
, "s", &text
))
432 if (!strcmp(text
, "*default*"))
433 newtPushHelpLine(NULL
);
435 newtPushHelpLine(text
);
441 static PyObject
* reflowText(PyObject
* s
, PyObject
* args
) {
443 int width
, minus
= 5, plus
= 5;
444 int realWidth
, realHeight
;
447 if (!PyArg_ParseTuple(args
, "si|ii", &text
, &width
, &minus
, &plus
))
450 new = newtReflowText(text
, width
, minus
, plus
, &realWidth
, &realHeight
);
452 tuple
= Py_BuildValue("(sii)", new, realWidth
, realHeight
);
458 static PyObject
* centeredWindow(PyObject
* s
, PyObject
* args
) {
462 if (!PyArg_ParseTuple(args
, "iis", &width
, &height
, &title
))
465 newtCenteredWindow(width
, height
, title
);
471 static PyObject
* gridWrappedWindow(PyObject
* s
, PyObject
* args
) {
476 if (!PyArg_ParseTuple(args
, "O!s|ii", &snackGridType
, &grid
, &title
,
481 newtGridWrappedWindow(grid
->grid
, title
);
483 newtGridWrappedWindowAt(grid
->grid
, title
, x
, y
);
489 static PyObject
* openWindow(PyObject
* s
, PyObject
* args
) {
490 int left
, top
, width
, height
;
493 if (!PyArg_ParseTuple(args
, "iiiis", &left
, &top
, &width
, &height
, &title
))
496 newtOpenWindow(left
, top
, width
, height
, title
);
502 static PyObject
* popWindow(PyObject
* s
, PyObject
* args
) {
508 static PyObject
* messageWindow(PyObject
* s
, PyObject
* args
) {
509 char * title
, * text
;
510 char * okbutton
= "Ok";
512 if (!PyArg_ParseTuple(args
, "ss|s", &title
, &text
, &okbutton
))
515 newtWinMessage(title
, okbutton
, text
);
521 static PyObject
* choiceWindow(PyObject
* s
, PyObject
* args
) {
522 char * title
, * text
;
523 char * okbutton
= "Ok";
524 char * cancelbutton
= "Cancel";
527 if (!PyArg_ParseTuple(args
, "ss|ss", &title
, &text
, &okbutton
,
531 rc
= newtWinChoice(title
, okbutton
, cancelbutton
, text
);
533 return Py_BuildValue("i", rc
);
536 static PyObject
* ternaryWindow(PyObject
* s
, PyObject
* args
) {
537 char * title
, * text
, * button1
, * button2
, * button3
;
540 if (!PyArg_ParseTuple(args
, "sssss", &title
, &text
, &button1
, &button2
,
544 rc
= newtWinTernary(title
, button1
, button2
, button3
, text
);
546 return Py_BuildValue("i", rc
);
549 static snackWidget
* buttonWidget(PyObject
* s
, PyObject
* args
) {
550 snackWidget
* widget
;
553 if (!PyArg_ParseTuple(args
, "s", &label
)) return NULL
;
555 widget
= snackWidgetNew ();
556 widget
->co
= newtButton(-1, -1, label
);
561 static snackWidget
* labelWidget(PyObject
* s
, PyObject
* args
) {
563 snackWidget
* widget
;
565 if (!PyArg_ParseTuple(args
, "s", &label
)) return NULL
;
567 widget
= snackWidgetNew ();
568 widget
->co
= newtLabel(-1, -1, label
);
573 static PyObject
* widgetLabelText(snackWidget
* s
, PyObject
* args
) {
576 if (!PyArg_ParseTuple(args
, "s", &label
)) return NULL
;
578 newtLabelSetText(s
->co
, label
);
584 static PyObject
* widgetTextboxText(snackWidget
* s
, PyObject
* args
) {
587 if (!PyArg_ParseTuple(args
, "s", &text
)) return NULL
;
589 newtTextboxSetText(s
->co
, text
);
595 static snackWidget
* listboxWidget(PyObject
* s
, PyObject
* args
) {
596 snackWidget
* widget
;
598 int doScroll
= 0, returnExit
= 0 ;
600 if (!PyArg_ParseTuple(args
, "i|ii", &height
, &doScroll
, &returnExit
))
603 widget
= snackWidgetNew ();
604 widget
->co
= newtListbox(-1, -1, height
,
605 (doScroll
? NEWT_FLAG_SCROLL
: 0) |
606 (returnExit
? NEWT_FLAG_RETURNEXIT
: 0));
612 static snackWidget
* textWidget(PyObject
* s
, PyObject
* args
) {
617 snackWidget
* widget
;
619 if (!PyArg_ParseTuple(args
, "iis|ii", &width
, &height
, &text
, &scrollBar
, &wrap
))
622 widget
= snackWidgetNew ();
623 widget
->co
= newtTextbox(-1, -1, width
, height
,
624 (scrollBar
? NEWT_FLAG_SCROLL
: 0) |
625 (wrap
? NEWT_FLAG_WRAP
: 0));
626 newtTextboxSetText(widget
->co
, text
);
631 static snackWidget
* radioButtonWidget(PyObject
* s
, PyObject
* args
) {
632 snackWidget
* widget
, * group
;
636 if (!PyArg_ParseTuple(args
, "sOi", &text
, &group
, &isOn
))
639 widget
= snackWidgetNew ();
641 if ((PyObject
*) group
== Py_None
)
642 widget
->co
= newtRadiobutton(-1, -1, text
, isOn
, NULL
);
644 widget
->co
= newtRadiobutton(-1, -1, text
, isOn
, group
->co
);
649 static snackWidget
* checkboxWidget(PyObject
* s
, PyObject
* args
) {
650 snackWidget
* widget
;
654 if (!PyArg_ParseTuple(args
, "si", &text
, &isOn
)) return NULL
;
656 widget
= snackWidgetNew ();
657 widget
->co
= newtCheckbox(-1, -1, text
, isOn
? '*' : ' ', NULL
,
663 static PyObject
* widgetCheckboxSetFlags(snackWidget
* s
, PyObject
* args
) {
666 if (!PyArg_ParseTuple(args
, "ii", &flag
, &sense
)) return NULL
;
668 newtCheckboxSetFlags(s
->co
, flag
, sense
);
674 static PyObject
* widgetCheckboxSetValue(snackWidget
* s
, PyObject
* args
) {
677 if (!PyArg_ParseTuple(args
, "s", &value
)) return NULL
;
679 newtCheckboxSetValue(s
->co
, *value
);
685 static snackWidget
* entryWidget(PyObject
* s
, PyObject
* args
) {
686 snackWidget
* widget
;
689 int isHidden
, isScrolled
, returnExit
, isPassword
;
691 if (!PyArg_ParseTuple(args
, "isiiii", &width
, &initial
,
692 &isHidden
, &isPassword
, &isScrolled
, &returnExit
)) return NULL
;
694 widget
= snackWidgetNew ();
695 widget
->co
= newtEntry(-1, -1, initial
, width
, (char **) &widget
->apointer
,
696 (isHidden
? NEWT_FLAG_HIDDEN
: 0) |
697 (isPassword
? NEWT_FLAG_PASSWORD
: 0) |
698 (returnExit
? NEWT_FLAG_RETURNEXIT
: 0) |
699 (isScrolled
? NEWT_FLAG_SCROLL
: 0));
704 static snackForm
* formCreate(PyObject
* s
, PyObject
* args
) {
706 PyObject
* help
= Py_None
;
708 if (!PyArg_ParseTuple(args
, "|O", &help
)) return NULL
;
713 form
= PyObject_NEW(snackForm
, &snackFormType
);
714 form
->fo
= newtForm(NULL
, help
, 0);
719 static snackGrid
* gridCreate(PyObject
* s
, PyObject
* args
) {
723 if (!PyArg_ParseTuple(args
, "ii", &cols
, &rows
)) return NULL
;
725 grid
= PyObject_NEW(snackGrid
, &snackGridType
);
726 grid
->grid
= newtCreateGrid(cols
, rows
);
731 static PyObject
* gridGetAttr(PyObject
* s
, char * name
) {
732 return Py_FindMethod(gridMethods
, s
, name
);
735 static PyObject
* gridPlace(snackGrid
* grid
, PyObject
* args
) {
738 if (!PyArg_ParseTuple(args
, "ii", &x
, &y
)) return NULL
;
740 newtGridPlace(grid
->grid
, x
, y
);
746 static PyObject
* gridSetField(snackGrid
* grid
, PyObject
* args
) {
750 int pLeft
= 0, pTop
= 0, pRight
= 0, pBottom
= 0;
751 int anchorFlags
= 0, growFlags
= 0;
753 if (!PyArg_ParseTuple(args
, "iiO|(iiii)ii", &x
, &y
,
754 &w
, &pLeft
, &pTop
, &pRight
, &pBottom
,
755 &anchorFlags
, &growFlags
))
758 if (w
->ob_type
== &snackWidgetType
) {
759 newtGridSetField(grid
->grid
, x
, y
, NEWT_GRID_COMPONENT
,
760 w
->co
, pLeft
, pTop
, pRight
, pBottom
, anchorFlags
,
764 newtGridSetField(grid
->grid
, x
, y
, NEWT_GRID_SUBGRID
,
765 g
->grid
, pLeft
, pTop
, pRight
, pBottom
, anchorFlags
,
773 static PyObject
* formGetAttr(PyObject
* s
, char * name
) {
774 return Py_FindMethod(formMethods
, s
, name
);
777 static PyObject
* formDraw(snackForm
* s
, PyObject
* args
) {
784 static PyObject
* formAdd(snackForm
* s
, PyObject
* args
) {
786 int size
= PyTuple_Size(args
), i
;
789 /* this is a hack, I should give an error directly */
790 if (!PyArg_ParseTuple(args
, "O!", &snackWidgetType
, &w
))
794 for (i
= 0; i
< size
; i
++) {
795 w
= (snackWidget
*) PyTuple_GET_ITEM(args
, i
);
796 newtFormAddComponent(s
->fo
, w
->co
);
803 static PyObject
* formRun(snackForm
* s
, PyObject
* args
) {
804 struct newtExitStruct result
;
806 newtFormRun(s
->fo
, &result
);
808 if (result
.reason
== NEWT_EXIT_HOTKEY
)
809 return Py_BuildValue("(si)", "hotkey", result
.u
.key
);
810 else if (result
.reason
== NEWT_EXIT_TIMER
)
811 return Py_BuildValue("(si)", "timer", 0);
813 return Py_BuildValue("(si)", "widget", result
.u
.co
);
816 static PyObject
* formHotKey(snackForm
* s
, PyObject
* args
) {
819 if (!PyArg_ParseTuple(args
, "i", &key
))
822 newtFormAddHotKey(s
->fo
, key
);
828 static PyObject
* formSetTimer(snackForm
* form
, PyObject
* args
) {
831 if (!PyArg_ParseTuple(args
, "i", &millisecs
))
834 newtFormSetTimer(form
->fo
, millisecs
);
840 static PyObject
* formSetCurrent(snackForm
* form
, PyObject
* args
) {
843 if (!PyArg_ParseTuple(args
, "O", &w
))
846 newtFormSetCurrent(form
->fo
, w
->co
);
852 static PyObject
* widgetGetAttr(PyObject
* s
, char * name
) {
853 snackWidget
* w
= (snackWidget
*) s
;
855 if (!strcmp(name
, "key")) {
856 return Py_BuildValue("i", w
->co
);
857 } else if (!strcmp(name
, "entryValue")) {
858 return Py_BuildValue("s", w
->apointer
);
859 } else if (!strcmp(name
, "checkboxValue")) {
860 return Py_BuildValue("i", w
->achar
== ' ' ? 0 : 1);
861 } else if (!strcmp(name
, "radioValue")) {
862 return Py_BuildValue("i", newtRadioGetCurrent(w
->co
));
865 return Py_FindMethod(widgetMethods
, s
, name
);
868 static void widgetDestructor(PyObject
* o
) {
869 snackWidget
* s
= (snackWidget
*) o
;
871 Py_XDECREF (s
->scs
.cb
);
872 Py_XDECREF (s
->scs
.data
);
877 static PyObject
* widgetAddCallback(snackWidget
* s
, PyObject
* args
) {
881 if (!PyArg_ParseTuple(args
, "O|O", &s
->scs
.cb
, &s
->scs
.data
))
884 Py_INCREF (s
->scs
.cb
);
885 Py_XINCREF (s
->scs
.data
);
887 newtComponentAddCallback(s
->co
, callbackMarshall
, &s
->scs
);
893 static PyObject
* widgetEntrySetValue(snackWidget
* s
, PyObject
* args
) {
896 if (!PyArg_ParseTuple(args
, "s", &val
))
899 newtEntrySet(s
->co
, val
, 1);
905 static PyObject
* widgetEntrySetFlags(snackWidget
* s
, PyObject
* args
) {
908 if (!PyArg_ParseTuple(args
, "ii", &flag
, &sense
)) return NULL
;
910 newtEntrySetFlags(s
->co
, flag
, sense
);
917 static PyObject
* widgetListboxAdd(snackWidget
* s
, PyObject
* args
) {
920 if (!PyArg_ParseTuple(args
, "s", &text
))
923 newtListboxAddEntry(s
->co
, text
, (void *) s
->anint
);
925 return PyInt_FromLong(s
->anint
++);
928 static PyObject
* widgetListboxIns(snackWidget
* s
, PyObject
* args
) {
932 if (!PyArg_ParseTuple(args
, "si", &text
, &key
))
935 newtListboxInsertEntry(s
->co
, text
, (void *) s
->anint
, (void *) key
);
937 return PyInt_FromLong(s
->anint
++);
940 static PyObject
* widgetListboxDel(snackWidget
* s
, PyObject
* args
) {
943 if (!PyArg_ParseTuple(args
, "i", &key
))
946 newtListboxDeleteEntry(s
->co
, (void *) key
);
952 static PyObject
* widgetListboxGet(snackWidget
* s
, PyObject
* args
) {
953 if (!PyArg_ParseTuple(args
, ""))
956 return PyInt_FromLong((long) newtListboxGetCurrent(s
->co
));
959 static PyObject
* widgetListboxSet(snackWidget
* s
, PyObject
* args
) {
962 if (!PyArg_ParseTuple(args
, "i", &index
))
965 newtListboxSetCurrentByKey(s
->co
, (void *) index
);
971 static PyObject
* widgetListboxSetW(snackWidget
* s
, PyObject
* args
) {
974 if (!PyArg_ParseTuple(args
, "i", &width
))
977 newtListboxSetWidth(s
->co
, width
);
983 static void emptyDestructor(PyObject
* s
) {
986 static snackWidget
* checkboxTreeWidget(PyObject
* s
, PyObject
* args
) {
989 snackWidget
* widget
;
991 if (!PyArg_ParseTuple(args
, "i|i", &height
, &scrollBar
))
994 widget
= snackWidgetNew ();
995 widget
->co
= newtCheckboxTree(-1, -1, height
,
996 scrollBar
? NEWT_FLAG_SCROLL
: 0);
1003 static PyObject
* widgetCheckboxTreeAddItem(snackWidget
* s
, PyObject
* args
) {
1006 PyObject
* pathList
, * o
;
1011 if (!PyArg_ParseTuple(args
, "sOi", &text
, &pathList
, &selected
))
1014 len
= PyTuple_Size(pathList
);
1015 path
= alloca(sizeof(*path
) * (len
+ 1));
1016 for (i
= 0; i
< len
; i
++) {
1017 o
= PyTuple_GetItem(pathList
, i
);
1018 path
[i
] = PyInt_AsLong(o
);
1020 path
[len
] = NEWT_ARG_LAST
;
1022 newtCheckboxTreeAddArray(s
->co
, text
, (void *) s
->anint
,
1023 selected
? NEWT_FLAG_SELECTED
: 0, path
);
1025 return PyInt_FromLong(s
->anint
++);
1028 static PyObject
* widgetCheckboxTreeGetCur(snackWidget
* s
, PyObject
* args
) {
1029 if (!PyArg_ParseTuple(args
, ""))
1032 return PyInt_FromLong((int) newtCheckboxTreeGetCurrent(s
->co
));
1035 static PyObject
* widgetCheckboxTreeSetEntry(snackWidget
* s
, PyObject
* args
) {
1039 if (!PyArg_ParseTuple(args
, "is", &data
, &text
)) return NULL
;
1041 newtCheckboxTreeSetEntry(s
->co
, (void *)data
, text
);
1047 static PyObject
* widgetCheckboxTreeSetEntryValue(snackWidget
* s
, PyObject
* args
) {
1051 if (!PyArg_ParseTuple(args
, "i|i", &data
, &isOn
)) return NULL
;
1053 newtCheckboxTreeSetEntryValue(s
->co
, (void *)data
,
1054 isOn
? NEWT_CHECKBOXTREE_SELECTED
:
1055 NEWT_CHECKBOXTREE_UNSELECTED
);
1061 static PyObject
* widgetCheckboxTreeGetEntryValue(snackWidget
* s
, PyObject
* args
) {
1067 if (!PyArg_ParseTuple(args
, "i", &data
)) return NULL
;
1069 selection
= newtCheckboxTreeGetEntryValue(s
->co
, (void *)data
);
1071 if (selection
== -1) return NULL
;
1073 switch (selection
) {
1074 case NEWT_CHECKBOXTREE_EXPANDED
:
1076 case NEWT_CHECKBOXTREE_COLLAPSED
:
1079 case NEWT_CHECKBOXTREE_UNSELECTED
:
1085 return Py_BuildValue("(ii)", isBranch
, isOn
);
1088 static PyObject
* widgetCheckboxTreeGetSel(snackWidget
* s
,
1095 if (!PyArg_ParseTuple(args
, ""))
1098 selection
= (void **) newtCheckboxTreeGetSelection(s
->co
, &numselected
);
1100 sel
= PyList_New(0);
1106 sel
= PyList_New(0);
1107 for (i
= 0; i
< numselected
; i
++) {
1108 PyList_Append(sel
, PyInt_FromLong((int) selection
[i
]));
1115 void init_snack(void) {
1118 m
= Py_InitModule("_snack", snackModuleMethods
);
1119 d
= PyModule_GetDict(m
);
1121 PyDict_SetItemString(d
, "ANCHOR_LEFT", PyInt_FromLong(NEWT_ANCHOR_LEFT
));
1122 PyDict_SetItemString(d
, "ANCHOR_TOP", PyInt_FromLong(NEWT_ANCHOR_TOP
));
1123 PyDict_SetItemString(d
, "ANCHOR_RIGHT", PyInt_FromLong(NEWT_ANCHOR_RIGHT
));
1124 PyDict_SetItemString(d
, "ANCHOR_BOTTOM",
1125 PyInt_FromLong(NEWT_ANCHOR_BOTTOM
));
1126 PyDict_SetItemString(d
, "GRID_GROWX", PyInt_FromLong(NEWT_GRID_FLAG_GROWX
));
1127 PyDict_SetItemString(d
, "GRID_GROWY", PyInt_FromLong(NEWT_GRID_FLAG_GROWY
));
1129 PyDict_SetItemString(d
, "FORM_EXIT_HOTKEY", PyString_FromString("hotkey"));
1130 PyDict_SetItemString(d
, "FORM_EXIT_WIDGET", PyString_FromString("widget"));
1131 PyDict_SetItemString(d
, "FORM_EXIT_TIMER", PyString_FromString("timer"));
1133 PyDict_SetItemString(d
, "KEY_F1", PyInt_FromLong(NEWT_KEY_F1
));
1134 PyDict_SetItemString(d
, "KEY_F2", PyInt_FromLong(NEWT_KEY_F2
));
1135 PyDict_SetItemString(d
, "KEY_F3", PyInt_FromLong(NEWT_KEY_F3
));
1136 PyDict_SetItemString(d
, "KEY_F4", PyInt_FromLong(NEWT_KEY_F4
));
1137 PyDict_SetItemString(d
, "KEY_F5", PyInt_FromLong(NEWT_KEY_F5
));
1138 PyDict_SetItemString(d
, "KEY_F6", PyInt_FromLong(NEWT_KEY_F6
));
1139 PyDict_SetItemString(d
, "KEY_F7", PyInt_FromLong(NEWT_KEY_F7
));
1140 PyDict_SetItemString(d
, "KEY_F8", PyInt_FromLong(NEWT_KEY_F8
));
1141 PyDict_SetItemString(d
, "KEY_F9", PyInt_FromLong(NEWT_KEY_F9
));
1142 PyDict_SetItemString(d
, "KEY_F10", PyInt_FromLong(NEWT_KEY_F10
));
1143 PyDict_SetItemString(d
, "KEY_F11", PyInt_FromLong(NEWT_KEY_F11
));
1144 PyDict_SetItemString(d
, "KEY_F12", PyInt_FromLong(NEWT_KEY_F12
));
1146 PyDict_SetItemString(d
, "FLAG_DISABLED", PyInt_FromLong(NEWT_FLAG_DISABLED
));
1147 PyDict_SetItemString(d
, "FLAGS_SET", PyInt_FromLong(NEWT_FLAGS_SET
));
1148 PyDict_SetItemString(d
, "FLAGS_RESET", PyInt_FromLong(NEWT_FLAGS_RESET
));
1149 PyDict_SetItemString(d
, "FLAGS_TOGGLE", PyInt_FromLong(NEWT_FLAGS_TOGGLE
));