15
#include "structmember.h"
20
#if PY_MAJOR_VERSION >= 3
21
#define PyInt_FromLong PyLong_FromLong
22
#define PyInt_AsLong PyLong_AsLong
23
#define PyString_FromString PyUnicode_FromString
24
#define MOD_ERROR_VAL NULL
25
#define MOD_SUCCESS_VAL(val) val
26
#define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
29
#define MOD_SUCCESS_VAL(val)
30
#define MOD_INIT(name) void init##name(void)
33
typedef struct snackWidget_s snackWidget;
34
typedef struct snackGrid_s snackGrid;
35
typedef struct snackForm_s snackForm;
37
struct callbackStruct {
38
PyObject * cb, * data;
41
/* Integer to pointer, 64-bit-sane */
42
#define I2P(x) ((void *)(long)(x))
44
static struct callbackStruct suspend;
45
static struct callbackStruct helpCallback;
47
static void emptyDestructor(PyObject * s);
49
static snackWidget * buttonWidget(PyObject * s, PyObject * args);
50
static snackWidget * compactbuttonWidget(PyObject * s, PyObject * args);
51
static PyObject * centeredWindow(PyObject * s, PyObject * args);
52
static snackWidget * checkboxWidget(PyObject * s, PyObject * args);
53
static PyObject * choiceWindow(PyObject * s, PyObject * args);
54
static snackWidget * entryWidget(PyObject * s, PyObject * args);
55
static PyObject * drawRootText(PyObject * s, PyObject * args);
56
static PyObject * doResume(PyObject * s, PyObject * args);
57
static PyObject * doSuspend(PyObject * s, PyObject * args);
58
static PyObject * doSuspend(PyObject * s, PyObject * args);
59
static snackForm * formCreate(PyObject * s, PyObject * args);
60
static snackGrid * gridCreate(PyObject * s, PyObject * args);
61
static PyObject * gridWrappedWindow(PyObject * s, PyObject * args);
62
static PyObject * finishScreen(PyObject * s, PyObject * args);
63
static PyObject * initScreen(PyObject * s, PyObject * args);
64
static snackWidget * labelWidget(PyObject * s, PyObject * args);
65
static snackWidget * listboxWidget(PyObject * s, PyObject * args);
66
static PyObject * messageWindow(PyObject * s, PyObject * args);
67
static PyObject * openWindow(PyObject * s, PyObject * args);
68
static PyObject * popHelpLine(PyObject * s, PyObject * args);
69
static PyObject * popWindow(PyObject * s, PyObject * args);
70
static PyObject * popWindowNoRefresh(PyObject * s, PyObject * args);
71
static PyObject * pushHelpLine(PyObject * s, PyObject * args);
72
static snackWidget * radioButtonWidget(PyObject * s, PyObject * args);
73
static PyObject * refreshScreen(PyObject * s, PyObject * args);
74
static PyObject * setColor(PyObject * s, PyObject * args);
75
static PyObject * scaleWidget(PyObject * s, PyObject * args);
76
static PyObject * scaleSet(snackWidget * s, PyObject * args);
77
static PyObject * screenSize(PyObject * s, PyObject * args);
78
static PyObject * setSuspendCallback(PyObject * s, PyObject * args);
79
static PyObject * setHelpCallback(PyObject * s, PyObject * args);
80
static PyObject * reflowText(PyObject * s, PyObject * args);
81
static snackWidget * textWidget(PyObject * s, PyObject * args);
82
static PyObject * ternaryWindow(PyObject * s, PyObject * args);
83
static snackWidget * checkboxTreeWidget(PyObject * s, PyObject * args, PyObject * kwargs);
84
static PyObject * pywstrlen(PyObject * s, PyObject * args);
85
static PyObject * widget_get_checkboxValue(PyObject *self, void *closure);
86
static PyObject * widget_get_radioValue(PyObject *self, void *closure);
88
static PyMethodDef snackModuleMethods[] = {
89
{ "button", (PyCFunction) buttonWidget, METH_VARARGS, NULL },
90
{ "compactbutton", (PyCFunction) compactbuttonWidget, METH_VARARGS, NULL },
91
{ "checkbox", (PyCFunction) checkboxWidget, METH_VARARGS, NULL },
92
{ "choice", choiceWindow, METH_VARARGS, NULL },
93
{ "centeredwindow", centeredWindow, METH_VARARGS, NULL },
94
{ "drawroottext", drawRootText, METH_VARARGS, NULL },
95
{ "entry", (PyCFunction) entryWidget, METH_VARARGS, NULL },
96
{ "finish", finishScreen, METH_VARARGS, NULL },
97
{ "form", (PyCFunction) formCreate, METH_VARARGS, NULL },
98
{ "grid", (PyCFunction) gridCreate, METH_VARARGS, NULL },
99
{ "gridwrappedwindow", gridWrappedWindow, METH_VARARGS, NULL },
100
{ "helpcallback", setHelpCallback, METH_VARARGS, NULL },
101
{ "init", initScreen, METH_VARARGS, NULL },
102
{ "label", (PyCFunction) labelWidget, METH_VARARGS, NULL },
103
{ "listbox", (PyCFunction) listboxWidget, METH_VARARGS, NULL },
104
{ "message", messageWindow, METH_VARARGS, NULL },
105
{ "openwindow", openWindow, METH_VARARGS, NULL },
106
{ "pophelpline", popHelpLine, METH_VARARGS, NULL },
107
{ "popwindow", popWindow, METH_VARARGS, NULL },
108
{ "popwindownorefresh", popWindowNoRefresh, METH_VARARGS, NULL },
109
{ "pushhelpline", pushHelpLine, METH_VARARGS, NULL },
110
{ "radiobutton", (PyCFunction) radioButtonWidget, METH_VARARGS, NULL },
111
{ "reflow", (PyCFunction) reflowText, METH_VARARGS, NULL },
112
{ "refresh", refreshScreen, METH_VARARGS, NULL },
113
{ "setcolor", setColor, METH_VARARGS, NULL },
114
{ "resume", doResume, METH_VARARGS, NULL },
115
{ "scale", scaleWidget, METH_VARARGS, NULL },
116
{ "size", screenSize, METH_VARARGS, NULL },
117
{ "suspend", doSuspend, METH_VARARGS, NULL },
118
{ "suspendcallback", setSuspendCallback, METH_VARARGS, NULL },
119
{ "ternary", ternaryWindow, METH_VARARGS, NULL },
120
{ "textbox", (PyCFunction) textWidget, METH_VARARGS, NULL },
121
{ "checkboxtree", (PyCFunction) checkboxTreeWidget, METH_VARARGS | METH_KEYWORDS, NULL },
122
{ "wstrlen", (PyCFunction) pywstrlen, METH_VARARGS | METH_KEYWORDS, NULL },
126
#if PY_MAJOR_VERSION >= 3
127
static struct PyModuleDef moduledef = {
128
PyModuleDef_HEAD_INIT,
129
"_snack", /* m_name */
132
snackModuleMethods, /* m_methods */
134
NULL, /* m_traverse */
140
static struct PyGetSetDef widget_getset[] = {
141
{ "checkboxValue", widget_get_checkboxValue, 0, NULL, NULL },
142
{ "radioValue", widget_get_radioValue, 0, NULL, NULL },
151
static PyObject * gridPlace(snackGrid * s, PyObject * args);
152
static PyObject * gridSetField(snackGrid * s, PyObject * args);
154
static PyMethodDef gridMethods[] = {
155
{ "place", (PyCFunction) gridPlace, METH_VARARGS, NULL },
156
{ "setfield", (PyCFunction) gridSetField, METH_VARARGS, NULL },
160
static PyTypeObject snackGridType = {
161
PyVarObject_HEAD_INIT(&PyType_Type, 0)
162
"snackgrid", /* tp_name */
163
sizeof(snackGrid), /* tp_size */
165
emptyDestructor, /* tp_dealloc */
171
0, /* tp_as_number */
172
0, /* tp_as_sequence */
173
0, /* tp_as_mapping */
177
PyObject_GenericGetAttr, /* tp_getattro */
179
0, /* tp_as_buffer */
180
Py_TPFLAGS_DEFAULT, /* tp_flags */
184
0, /* tp_richcompare */
185
0, /* tp_weaklistoffset */
188
gridMethods /* tp_methods */
196
static PyObject * formAdd(snackForm * s, PyObject * args);
197
static PyObject * formDraw(snackForm * s, PyObject * args);
198
static PyObject * formRun(snackForm * s, PyObject * args);
199
static PyObject * formHotKey(snackForm * s, PyObject * args);
200
static PyObject * formSetCurrent(snackForm * form, PyObject * args);
201
static PyObject * formSetTimer(snackForm * form, PyObject * args);
202
static PyObject * formWatchFD(snackForm * form, PyObject * args);
204
static PyMethodDef formMethods[] = {
205
{ "add", (PyCFunction) formAdd, METH_VARARGS, NULL },
206
{ "draw", (PyCFunction) formDraw, METH_VARARGS, NULL },
207
{ "run", (PyCFunction) formRun, METH_VARARGS, NULL },
208
{ "addhotkey", (PyCFunction) formHotKey, METH_VARARGS, NULL },
209
{ "setcurrent", (PyCFunction) formSetCurrent, METH_VARARGS, NULL },
210
{ "settimer", (PyCFunction) formSetTimer, METH_VARARGS, NULL },
211
{ "watchfd", (PyCFunction) formWatchFD, METH_VARARGS, NULL },
215
static PyTypeObject snackFormType = {
216
PyVarObject_HEAD_INIT(&PyType_Type, 0)
217
"snackform", /* tp_name */
218
sizeof(snackForm), /* tp_size */
220
emptyDestructor, /* tp_dealloc */
226
0, /* tp_as_number */
227
0, /* tp_as_sequence */
228
0, /* tp_as_mapping */
232
PyObject_GenericGetAttr, /* tp_getattro */
234
0, /* tp_as_buffer */
235
Py_TPFLAGS_DEFAULT, /* tp_flags */
239
0, /* tp_richcompare */
240
0, /* tp_weaklistoffset */
243
formMethods /* tp_methods */
246
struct snackWidget_s {
252
struct callbackStruct scs;
255
static PyObject * widgetAddCallback(snackWidget * s, PyObject * args);
256
static void widgetDestructor(PyObject * s);
257
static PyObject * widgetEntrySetValue(snackWidget * s, PyObject * args);
258
static PyObject * widgetLabelText(snackWidget * s, PyObject * args);
259
static PyObject * widgetLabelSetColors(snackWidget * s, PyObject * args);
260
static PyObject * widgetListboxSetW(snackWidget * s, PyObject * args);
261
static PyObject * widgetListboxAdd(snackWidget * s, PyObject * args);
262
static PyObject * widgetListboxIns(snackWidget * s, PyObject * args);
263
static PyObject * widgetListboxDel(snackWidget * s, PyObject * args);
264
static PyObject * widgetListboxGet(snackWidget * s, PyObject * args);
265
static PyObject * widgetListboxGetSel(snackWidget * s, PyObject * args);
266
static PyObject * widgetListboxSet(snackWidget * s, PyObject * args);
267
static PyObject * widgetListboxClear(snackWidget * s, PyObject * args);
268
static PyObject * widgetTextboxText(snackWidget * s, PyObject * args);
269
static PyObject * widgetTextboxHeight(snackWidget * s, PyObject * args);
270
static PyObject * widgetCheckboxTreeAddItem(snackWidget * s, PyObject * args);
271
static PyObject * widgetCheckboxTreeGetSel(snackWidget * s, PyObject * args);
272
static PyObject * widgetCheckboxTreeGetCur(snackWidget * s, PyObject * args);
273
static PyObject * widgetCheckboxTreeSetEntry(snackWidget * s, PyObject * args);
274
static PyObject * widgetCheckboxTreeSetWidth(snackWidget * s, PyObject * args);
275
static PyObject * widgetCheckboxTreeSetCurrent(snackWidget * s, PyObject * args);
276
static PyObject * widgetCheckboxTreeSetEntryValue(snackWidget * s, PyObject * args);
277
static PyObject * widgetCheckboxTreeGetEntryValue(snackWidget * s, PyObject * args);
278
static PyObject * widgetEntrySetFlags(snackWidget * s, PyObject * args);
279
static PyObject * widgetCheckboxSetFlags(snackWidget * s, PyObject * args);
280
static PyObject * widgetCheckboxSetValue(snackWidget * s, PyObject * args);
282
static PyMethodDef widgetMethods[] = {
283
{ "setCallback", (PyCFunction) widgetAddCallback, METH_VARARGS, NULL },
284
{ "labelSetColors", (PyCFunction) widgetLabelSetColors, METH_VARARGS, NULL },
285
{ "labelText", (PyCFunction) widgetLabelText, METH_VARARGS, NULL },
286
{ "textboxText", (PyCFunction) widgetTextboxText, METH_VARARGS, NULL },
287
{ "textboxHeight", (PyCFunction) widgetTextboxHeight, METH_VARARGS, NULL },
288
{ "entrySetValue", (PyCFunction) widgetEntrySetValue, METH_VARARGS, NULL },
289
{ "listboxAddItem", (PyCFunction) widgetListboxAdd, METH_VARARGS, NULL },
290
{ "listboxInsertItem", (PyCFunction) widgetListboxIns, METH_VARARGS, NULL },
291
{ "listboxGetCurrent", (PyCFunction) widgetListboxGet, METH_VARARGS, NULL },
292
{ "listboxGetSelection", (PyCFunction) widgetListboxGetSel, METH_VARARGS, NULL },
293
{ "listboxSetCurrent", (PyCFunction) widgetListboxSet, METH_VARARGS, NULL },
294
{ "listboxSetWidth", (PyCFunction) widgetListboxSetW, METH_VARARGS, NULL },
295
{ "listboxDeleteItem", (PyCFunction) widgetListboxDel, METH_VARARGS, NULL },
296
{ "listboxClear", (PyCFunction) widgetListboxClear, METH_VARARGS, NULL },
297
{ "scaleSet", (PyCFunction) scaleSet, METH_VARARGS, NULL },
298
{ "checkboxtreeAddItem", (PyCFunction) widgetCheckboxTreeAddItem,
299
METH_VARARGS, NULL },
300
{ "checkboxtreeGetCurrent", (PyCFunction) widgetCheckboxTreeGetCur,
301
METH_VARARGS, NULL },
302
{ "checkboxtreeGetEntryValue", (PyCFunction) widgetCheckboxTreeGetEntryValue,
303
METH_VARARGS, NULL },
304
{ "checkboxtreeSetEntry", (PyCFunction) widgetCheckboxTreeSetEntry,
305
METH_VARARGS, NULL },
306
{ "checkboxtreeSetWidth", (PyCFunction) widgetCheckboxTreeSetWidth, METH_VARARGS, NULL },
307
{ "checkboxtreeSetCurrent", (PyCFunction) widgetCheckboxTreeSetCurrent,
308
METH_VARARGS, NULL },
309
{ "checkboxtreeSetEntryValue", (PyCFunction) widgetCheckboxTreeSetEntryValue,
310
METH_VARARGS, NULL },
311
{ "checkboxtreeGetSelection", (PyCFunction) widgetCheckboxTreeGetSel,
312
METH_VARARGS, NULL },
313
{ "entrySetFlags", (PyCFunction) widgetEntrySetFlags, METH_VARARGS, NULL },
314
{ "checkboxSetFlags", (PyCFunction) widgetCheckboxSetFlags, METH_VARARGS, NULL },
315
{ "checkboxSetValue", (PyCFunction) widgetCheckboxSetValue, METH_VARARGS, NULL },
319
static PyMemberDef widget_members[] = {
320
{ "key" , T_INT, offsetof(snackWidget, co), 0, NULL },
321
{ "entryValue", T_STRING, offsetof(snackWidget, apointer), 0, NULL },
325
static PyTypeObject snackWidgetType = {
326
PyVarObject_HEAD_INIT(&PyType_Type, 0)
327
"snackwidget", /* tp_name */
328
sizeof(snackWidget), /* tp_size */
330
widgetDestructor, /* tp_dealloc */
336
0, /* tp_as_number */
337
0, /* tp_as_sequence */
338
0, /* tp_as_mapping */
342
PyObject_GenericGetAttr, /* tp_getattro */
344
0, /* tp_as_buffer */
345
Py_TPFLAGS_DEFAULT, /* tp_flags */
349
0, /* tp_richcompare */
350
0, /* tp_weaklistoffset */
353
widgetMethods, /* tp_methods */
354
widget_members, /* tp_members */
355
widget_getset /* tp_getset */
358
static snackWidget * snackWidgetNew (void) {
359
snackWidget * widget;
361
widget = PyObject_New(snackWidget, &snackWidgetType);
363
widget->scs.cb = NULL;
364
widget->scs.data = NULL;
369
static PyObject * initScreen(PyObject * s, PyObject * args) {
370
static int init_newt = 1;
384
static PyObject * finishScreen(PyObject * s, PyObject * args) {
385
Py_XDECREF (suspend.cb);
386
Py_XDECREF (suspend.data);
393
static PyObject * refreshScreen(PyObject * s, PyObject * args) {
399
static PyObject * setColor(PyObject * s, PyObject * args) {
402
if (!PyArg_ParseTuple(args, "iss", &colorset, &fg, &bg))
405
newtSetColor(colorset, fg, bg);
411
static PyObject * scaleWidget(PyObject * s, PyObject * args) {
412
snackWidget * widget;
413
int width, fullAmount;
415
if (!PyArg_ParseTuple(args, "ii", &width, &fullAmount)) return NULL;
417
widget = snackWidgetNew ();
418
widget->co = newtScale(-1, -1, width, fullAmount);
420
return (PyObject *) widget;
423
static PyObject * scaleSet(snackWidget * s, PyObject * args) {
426
if (!PyArg_ParseTuple(args, "i", &amount)) return NULL;
428
newtScaleSet(s->co, amount);
434
static PyObject * screenSize(PyObject * s, PyObject * args) {
437
if (!PyArg_ParseTuple(args, ""))
440
newtGetScreenSize(&width, &height);
442
return Py_BuildValue("(ii)", width, height);
445
static void helpCallbackMarshall(newtComponent co, void * data) {
446
PyObject * args, * result;
448
PyGILState_STATE _state = PyGILState_Ensure();
450
args = Py_BuildValue("(O)", data);
451
result = PyEval_CallObject(helpCallback.cb, args);
455
PyGILState_Release(_state);
460
static void suspendCallbackMarshall(void * data) {
461
struct callbackStruct * scs = data;
462
PyObject * args, * result;
464
PyGILState_STATE _state = PyGILState_Ensure();
467
args = Py_BuildValue("(O)", scs->data);
468
result = PyEval_CallObject(scs->cb, args);
471
result = PyEval_CallObject(scs->cb, NULL);
480
PyGILState_Release(_state);
485
static void callbackMarshall(newtComponent co, void * data) {
486
struct callbackStruct * scs = data;
487
PyObject * args, * result;
489
PyGILState_STATE _state = PyGILState_Ensure();
492
args = Py_BuildValue("(O)", scs->data);
493
result = PyEval_CallObject(scs->cb, args);
496
result = PyEval_CallObject(scs->cb, NULL);
505
PyGILState_Release(_state);
510
static PyObject * setSuspendCallback(PyObject * s, PyObject * args) {
511
if (!PyArg_ParseTuple(args, "O|O", &suspend.cb, &suspend.data))
514
Py_INCREF (suspend.cb);
515
Py_XINCREF (suspend.data);
517
newtSetSuspendCallback(suspendCallbackMarshall, &suspend);
523
static PyObject * setHelpCallback(PyObject * s, PyObject * args) {
524
if (!PyArg_ParseTuple(args, "O", &helpCallback.cb))
527
/*if (helpCallback.cb) {
528
Py_DECREF (helpCallback.cb);
531
Py_INCREF (helpCallback.cb);
533
newtSetHelpCallback(helpCallbackMarshall);
539
static PyObject * drawRootText(PyObject * s, PyObject * args) {
543
if (!PyArg_ParseTuple(args, "iis", &left, &top, &text))
546
newtDrawRootText(left, top, text);
552
static PyObject * doSuspend(PyObject * s, PyObject * args) {
559
static PyObject * doResume(PyObject * s, PyObject * args) {
566
static PyObject * popHelpLine(PyObject * s, PyObject * args) {
572
static PyObject * pushHelpLine(PyObject * s, PyObject * args) {
575
if (!PyArg_ParseTuple(args, "s", &text))
578
if (!strcmp(text, "*default*"))
579
newtPushHelpLine(NULL);
581
newtPushHelpLine(text);
587
static PyObject * reflowText(PyObject * s, PyObject * args) {
589
int width, minus = 5, plus = 5;
590
int realWidth, realHeight;
593
if (!PyArg_ParseTuple(args, "si|ii", &text, &width, &minus, &plus))
596
new = newtReflowText(text, width, minus, plus, &realWidth, &realHeight);
598
tuple = Py_BuildValue("(sii)", new, realWidth, realHeight);
604
static PyObject * centeredWindow(PyObject * s, PyObject * args) {
608
if (!PyArg_ParseTuple(args, "iis", &width, &height, &title))
611
newtCenteredWindow(width, height, title);
617
static PyObject * gridWrappedWindow(PyObject * s, PyObject * args) {
622
if (!PyArg_ParseTuple(args, "O!s|ii", &snackGridType, &grid, &title,
627
newtGridWrappedWindow(grid->grid, title);
629
newtGridWrappedWindowAt(grid->grid, title, x, y);
635
static PyObject * openWindow(PyObject * s, PyObject * args) {
636
int left, top, width, height;
639
if (!PyArg_ParseTuple(args, "iiiis", &left, &top, &width, &height, &title))
642
newtOpenWindow(left, top, width, height, title);
648
static PyObject * popWindow(PyObject * s, PyObject * args) {
654
static PyObject * popWindowNoRefresh(PyObject * s, PyObject * args) {
655
newtPopWindowNoRefresh();
660
static PyObject * messageWindow(PyObject * s, PyObject * args) {
661
char * title, * text;
662
char * okbutton = "Ok";
664
if (!PyArg_ParseTuple(args, "ss|s", &title, &text, &okbutton))
667
Py_BEGIN_ALLOW_THREADS
668
newtWinMessage(title, okbutton, text);
675
static PyObject * choiceWindow(PyObject * s, PyObject * args) {
676
char * title, * text;
677
char * okbutton = "Ok";
678
char * cancelbutton = "Cancel";
681
if (!PyArg_ParseTuple(args, "ss|ss", &title, &text, &okbutton,
685
Py_BEGIN_ALLOW_THREADS
686
rc = newtWinChoice(title, okbutton, cancelbutton, text);
689
return Py_BuildValue("i", rc);
692
static PyObject * ternaryWindow(PyObject * s, PyObject * args) {
693
char * title, * text, * button1, * button2, * button3;
696
if (!PyArg_ParseTuple(args, "sssss", &title, &text, &button1, &button2,
700
Py_BEGIN_ALLOW_THREADS
701
rc = newtWinTernary(title, button1, button2, button3, text);
704
return Py_BuildValue("i", rc);
707
static snackWidget * buttonWidget(PyObject * s, PyObject * args) {
708
snackWidget * widget;
711
if (!PyArg_ParseTuple(args, "s", &label)) return NULL;
713
widget = snackWidgetNew ();
714
widget->co = newtButton(-1, -1, label);
719
static snackWidget * compactbuttonWidget(PyObject * s, PyObject * args) {
720
snackWidget * widget;
723
if (!PyArg_ParseTuple(args, "s", &label)) return NULL;
725
widget = snackWidgetNew ();
726
widget->co = newtCompactButton(-1, -1, label);
731
static snackWidget * labelWidget(PyObject * s, PyObject * args) {
733
snackWidget * widget;
735
if (!PyArg_ParseTuple(args, "s", &label)) return NULL;
737
widget = snackWidgetNew ();
738
widget->co = newtLabel(-1, -1, label);
743
static PyObject * widgetLabelText(snackWidget * s, PyObject * args) {
746
if (!PyArg_ParseTuple(args, "s", &label)) return NULL;
748
newtLabelSetText(s->co, label);
754
static PyObject * widgetLabelSetColors(snackWidget * s, PyObject * args) {
757
if (!PyArg_ParseTuple(args, "i", &colorset)) return NULL;
759
newtLabelSetColors(s->co, colorset);
765
static PyObject * widgetTextboxText(snackWidget * s, PyObject * args) {
768
if (!PyArg_ParseTuple(args, "s", &text)) return NULL;
770
newtTextboxSetText(s->co, text);
776
static PyObject * widgetTextboxHeight(snackWidget * s, PyObject * args) {
779
if (!PyArg_ParseTuple(args, "i", &height)) return NULL;
781
newtTextboxSetHeight(s->co, height);
787
static snackWidget * listboxWidget(PyObject * s, PyObject * args) {
788
snackWidget * widget;
790
int doScroll = 0, returnExit = 0, showCursor = 0, multiple = 0, border = 0;
792
if (!PyArg_ParseTuple(args, "i|iiiii", &height, &doScroll, &returnExit,
793
&showCursor, &multiple, &border))
796
widget = snackWidgetNew ();
797
widget->co = newtListbox(-1, -1, height,
798
(doScroll ? NEWT_FLAG_SCROLL : 0) |
799
(returnExit ? NEWT_FLAG_RETURNEXIT : 0) |
800
(showCursor ? NEWT_FLAG_SHOWCURSOR : 0) |
801
(multiple ? NEWT_FLAG_MULTIPLE : 0) |
802
(border ? NEWT_FLAG_BORDER : 0)
809
static snackWidget * textWidget(PyObject * s, PyObject * args) {
814
snackWidget * widget;
816
if (!PyArg_ParseTuple(args, "iis|ii", &width, &height, &text, &scrollBar, &wrap))
819
widget = snackWidgetNew ();
820
widget->co = newtTextbox(-1, -1, width, height,
821
(scrollBar ? NEWT_FLAG_SCROLL : 0) |
822
(wrap ? NEWT_FLAG_WRAP : 0));
823
newtTextboxSetText(widget->co, text);
828
static snackWidget * radioButtonWidget(PyObject * s, PyObject * args) {
829
snackWidget * widget, * group;
833
if (!PyArg_ParseTuple(args, "sOi", &text, &group, &isOn))
836
widget = snackWidgetNew ();
838
if ((PyObject *) group == Py_None)
839
widget->co = newtRadiobutton(-1, -1, text, isOn, NULL);
841
widget->co = newtRadiobutton(-1, -1, text, isOn, group->co);
846
static snackWidget * checkboxWidget(PyObject * s, PyObject * args) {
847
snackWidget * widget;
851
if (!PyArg_ParseTuple(args, "si", &text, &isOn)) return NULL;
853
widget = snackWidgetNew ();
854
widget->co = newtCheckbox(-1, -1, text, isOn ? '*' : ' ', NULL,
860
static PyObject * widgetCheckboxSetFlags(snackWidget * s, PyObject * args) {
863
if (!PyArg_ParseTuple(args, "ii", &flag, &sense)) return NULL;
865
newtCheckboxSetFlags(s->co, flag, sense);
871
static PyObject * widgetCheckboxSetValue(snackWidget * s, PyObject * args) {
874
if (!PyArg_ParseTuple(args, "s", &value)) return NULL;
876
newtCheckboxSetValue(s->co, *value);
882
static snackWidget * entryWidget(PyObject * s, PyObject * args) {
883
snackWidget * widget;
886
int isHidden, isScrolled, returnExit, isPassword;
888
if (!PyArg_ParseTuple(args, "isiiii", &width, &initial,
889
&isHidden, &isPassword, &isScrolled, &returnExit)) return NULL;
891
widget = snackWidgetNew ();
892
widget->co = newtEntry(-1, -1, initial, width,
893
(const char **) &widget->apointer,
894
(isHidden ? NEWT_FLAG_HIDDEN : 0) |
895
(isPassword ? NEWT_FLAG_PASSWORD : 0) |
896
(returnExit ? NEWT_FLAG_RETURNEXIT : 0) |
897
(isScrolled ? NEWT_FLAG_SCROLL : 0));
902
static snackForm * formCreate(PyObject * s, PyObject * args) {
904
PyObject * help = Py_None;
906
if (!PyArg_ParseTuple(args, "|O", &help)) return NULL;
911
form = PyObject_New(snackForm, &snackFormType);
912
form->fo = newtForm(NULL, help, 0);
917
static snackGrid * gridCreate(PyObject * s, PyObject * args) {
921
if (!PyArg_ParseTuple(args, "ii", &cols, &rows)) return NULL;
923
grid = PyObject_New(snackGrid, &snackGridType);
924
grid->grid = newtCreateGrid(cols, rows);
929
static PyObject * gridPlace(snackGrid * grid, PyObject * args) {
932
if (!PyArg_ParseTuple(args, "ii", &x, &y)) return NULL;
934
newtGridPlace(grid->grid, x, y);
940
static PyObject * gridSetField(snackGrid * grid, PyObject * args) {
944
int pLeft = 0, pTop = 0, pRight = 0, pBottom = 0;
945
int anchorFlags = 0, growFlags = 0;
947
if (!PyArg_ParseTuple(args, "iiO|(iiii)ii", &x, &y,
948
&w, &pLeft, &pTop, &pRight, &pBottom,
949
&anchorFlags, &growFlags))
952
if (Py_TYPE(w) == &snackWidgetType) {
953
newtGridSetField(grid->grid, x, y, NEWT_GRID_COMPONENT,
954
w->co, pLeft, pTop, pRight, pBottom, anchorFlags,
958
newtGridSetField(grid->grid, x, y, NEWT_GRID_SUBGRID,
959
g->grid, pLeft, pTop, pRight, pBottom, anchorFlags,
967
static PyObject * formDraw(snackForm * s, PyObject * args) {
974
static PyObject * formAdd(snackForm * s, PyObject * args) {
976
int size = PyTuple_Size(args), i;
979
/* this is a hack, I should give an error directly */
980
if (!PyArg_ParseTuple(args, "O!", &snackWidgetType, &w))
984
for (i = 0; i < size; i++) {
985
w = (snackWidget *) PyTuple_GET_ITEM(args, i);
986
newtFormAddComponent(s->fo, w->co);
993
static PyObject * formRun(snackForm * s, PyObject * args) {
994
struct newtExitStruct result;
996
Py_BEGIN_ALLOW_THREADS
997
newtFormRun(s->fo, &result);
1000
if (result.reason == NEWT_EXIT_HOTKEY)
1001
return Py_BuildValue("(si)", "hotkey", result.u.key);
1002
else if (result.reason == NEWT_EXIT_TIMER)
1003
return Py_BuildValue("(si)", "timer", 0);
1004
else if (result.reason == NEWT_EXIT_FDREADY)
1005
return Py_BuildValue("(si)", "fdready", result.u.watch);
1007
return Py_BuildValue("(si)", "widget", result.u.co);
1010
static PyObject * formHotKey(snackForm * s, PyObject * args) {
1013
if (!PyArg_ParseTuple(args, "i", &key))
1016
newtFormAddHotKey(s->fo, key);
1022
static PyObject * formSetTimer(snackForm * form, PyObject * args) {
1025
if (!PyArg_ParseTuple(args, "i", &millisecs))
1028
newtFormSetTimer(form->fo, millisecs);
1034
static PyObject * formWatchFD(snackForm * form, PyObject * args) {
1037
if (!PyArg_ParseTuple(args, "ii", &fd, &fdflags))
1040
newtFormWatchFd(form->fo, fd, fdflags);
1046
static PyObject * formSetCurrent(snackForm * form, PyObject * args) {
1049
if (!PyArg_ParseTuple(args, "O", &w))
1052
newtFormSetCurrent(form->fo, w->co);
1058
static PyObject * widget_get_checkboxValue(PyObject *self, void *closure)
1060
snackWidget *w = (snackWidget *)self;
1062
return Py_BuildValue("i", w->achar == ' ' ? 0 : 1);
1065
static PyObject * widget_get_radioValue(PyObject *self, void *closure)
1067
snackWidget *w = (snackWidget *)self;
1069
return Py_BuildValue("i", newtRadioGetCurrent(w->co));
1072
static void widgetDestructor(PyObject * o) {
1073
snackWidget * s = (snackWidget *) o;
1075
Py_XDECREF (s->scs.cb);
1076
Py_XDECREF (s->scs.data);
1081
static PyObject * widgetAddCallback(snackWidget * s, PyObject * args) {
1085
if (!PyArg_ParseTuple(args, "O|O", &s->scs.cb, &s->scs.data))
1088
Py_INCREF (s->scs.cb);
1089
Py_XINCREF (s->scs.data);
1091
newtComponentAddCallback(s->co, callbackMarshall, &s->scs);
1097
static PyObject * widgetEntrySetValue(snackWidget * s, PyObject * args) {
1099
int cursorAtEnd = 1;
1101
if (!PyArg_ParseTuple(args, "s|i", &val, &cursorAtEnd))
1104
newtEntrySet(s->co, val, cursorAtEnd);
1110
static PyObject * widgetEntrySetFlags(snackWidget * s, PyObject * args) {
1113
if (!PyArg_ParseTuple(args, "ii", &flag, &sense)) return NULL;
1115
newtEntrySetFlags(s->co, flag, sense);
1122
static PyObject * widgetListboxAdd(snackWidget * s, PyObject * args) {
1125
if (!PyArg_ParseTuple(args, "s", &text))
1128
newtListboxAddEntry(s->co, text, I2P(s->anint));
1130
return PyInt_FromLong(s->anint++);
1133
static PyObject * widgetListboxIns(snackWidget * s, PyObject * args) {
1137
if (!PyArg_ParseTuple(args, "si", &text, &key))
1140
newtListboxInsertEntry(s->co, text, I2P(s->anint), I2P(key));
1142
return PyInt_FromLong(s->anint++);
1145
static PyObject * widgetListboxDel(snackWidget * s, PyObject * args) {
1148
if (!PyArg_ParseTuple(args, "i", &key))
1151
newtListboxDeleteEntry(s->co, I2P(key));
1157
static PyObject * widgetListboxGet(snackWidget * s, PyObject * args) {
1158
if (!PyArg_ParseTuple(args, ""))
1161
return PyInt_FromLong((long) newtListboxGetCurrent(s->co));
1164
static PyObject * widgetListboxGetSel(snackWidget * s, PyObject * args) {
1170
if (!PyArg_ParseTuple(args, ""))
1173
selection = (void **) newtListboxGetSelection(s->co, &numselected);
1175
sel = PyList_New(0);
1181
sel = PyList_New(0);
1182
for (i = 0; i < numselected; i++) {
1183
PyList_Append(sel, PyInt_FromLong((long) selection[i]));
1190
static PyObject * widgetListboxSet(snackWidget * s, PyObject * args) {
1193
if (!PyArg_ParseTuple(args, "i", &index))
1196
newtListboxSetCurrentByKey(s->co, I2P(index));
1202
static PyObject * widgetListboxSetW(snackWidget * s, PyObject * args) {
1205
if (!PyArg_ParseTuple(args, "i", &width))
1208
newtListboxSetWidth(s->co, width);
1214
static PyObject * widgetListboxClear(snackWidget * s, PyObject * args) {
1215
if (!PyArg_ParseTuple(args, ""))
1218
newtListboxClear(s->co);
1224
static void emptyDestructor(PyObject * s) {
1227
static snackWidget * checkboxTreeWidget(PyObject * s, PyObject * args, PyObject * kwargs) {
1230
int hide_checkbox = 0;
1231
int unselectable = 0;
1233
snackWidget * widget;
1234
const char *kw[] = {"height", "scrollbar", "hide_checkbox", "unselectable", NULL};
1236
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|iii", (char **) kw,
1237
&height, &scrollBar, &hide_checkbox, &unselectable))
1240
flags = (scrollBar ? NEWT_FLAG_SCROLL : 0) |
1241
(hide_checkbox ? NEWT_CHECKBOXTREE_HIDE_BOX : 0) |
1242
(unselectable ? NEWT_CHECKBOXTREE_UNSELECTABLE : 0);
1244
widget = snackWidgetNew ();
1245
widget->co = newtCheckboxTree(-1, -1, height, flags);
1252
static PyObject * widgetCheckboxTreeAddItem(snackWidget * s, PyObject * args) {
1255
PyObject * pathList, * o;
1260
if (!PyArg_ParseTuple(args, "sOi", &text, &pathList, &selected))
1263
len = PyTuple_Size(pathList);
1264
path = alloca(sizeof(*path) * (len + 1));
1265
for (i = 0; i < len; i++) {
1266
o = PyTuple_GetItem(pathList, i);
1267
path[i] = PyInt_AsLong(o);
1269
path[len] = NEWT_ARG_LAST;
1271
newtCheckboxTreeAddArray(s->co, text, I2P(s->anint),
1272
selected ? NEWT_FLAG_SELECTED : 0, path);
1274
return PyInt_FromLong(s->anint++);
1277
static PyObject * widgetCheckboxTreeGetCur(snackWidget * s, PyObject * args) {
1278
if (!PyArg_ParseTuple(args, ""))
1281
return PyInt_FromLong((long)newtCheckboxTreeGetCurrent(s->co));
1284
static PyObject * widgetCheckboxTreeSetEntry(snackWidget * s, PyObject * args) {
1288
if (!PyArg_ParseTuple(args, "is", &data, &text)) return NULL;
1290
newtCheckboxTreeSetEntry(s->co, I2P(data), text);
1296
static PyObject * widgetCheckboxTreeSetWidth(snackWidget * s, PyObject * args) {
1299
if (!PyArg_ParseTuple(args, "i", &width))
1302
newtCheckboxTreeSetWidth(s->co, width);
1308
static PyObject * widgetCheckboxTreeSetCurrent(snackWidget * s, PyObject * args) {
1311
if (!PyArg_ParseTuple(args, "i", &data)) return NULL;
1313
newtCheckboxTreeSetCurrent(s->co, I2P(data));
1319
static PyObject * widgetCheckboxTreeSetEntryValue(snackWidget * s, PyObject * args) {
1323
if (!PyArg_ParseTuple(args, "i|i", &data, &isOn)) return NULL;
1325
newtCheckboxTreeSetEntryValue(s->co, I2P(data),
1326
isOn ? NEWT_CHECKBOXTREE_SELECTED :
1327
NEWT_CHECKBOXTREE_UNSELECTED);
1333
static PyObject * widgetCheckboxTreeGetEntryValue(snackWidget * s, PyObject * args) {
1339
if (!PyArg_ParseTuple(args, "i", &data)) return NULL;
1341
selection = newtCheckboxTreeGetEntryValue(s->co, I2P(data));
1343
if (selection == -1) return NULL;
1345
switch (selection) {
1346
case NEWT_CHECKBOXTREE_EXPANDED:
1348
case NEWT_CHECKBOXTREE_COLLAPSED:
1351
case NEWT_CHECKBOXTREE_UNSELECTED:
1357
return Py_BuildValue("(ii)", isBranch, isOn);
1360
static PyObject * widgetCheckboxTreeGetSel(snackWidget * s,
1367
if (!PyArg_ParseTuple(args, ""))
1370
selection = (void **) newtCheckboxTreeGetSelection(s->co, &numselected);
1372
sel = PyList_New(0);
1378
sel = PyList_New(0);
1379
for (i = 0; i < numselected; i++) {
1380
PyList_Append(sel, PyInt_FromLong((long) selection[i]));
1387
static PyObject * pywstrlen(PyObject * s, PyObject * args)
1392
if (!PyArg_ParseTuple(args, "s|i", &str, &len)) return NULL;
1394
return PyInt_FromLong(wstrlen(str, len));
1401
#if PY_MAJOR_VERSION >= 3
1402
m = PyModule_Create(&moduledef);
1404
m = Py_InitModule("_snack", snackModuleMethods);
1408
return MOD_ERROR_VAL;
1410
d = PyModule_GetDict(m);
1412
PyDict_SetItemString(d, "ANCHOR_LEFT", PyInt_FromLong(NEWT_ANCHOR_LEFT));
1413
PyDict_SetItemString(d, "ANCHOR_TOP", PyInt_FromLong(NEWT_ANCHOR_TOP));
1414
PyDict_SetItemString(d, "ANCHOR_RIGHT", PyInt_FromLong(NEWT_ANCHOR_RIGHT));
1415
PyDict_SetItemString(d, "ANCHOR_BOTTOM",
1416
PyInt_FromLong(NEWT_ANCHOR_BOTTOM));
1417
PyDict_SetItemString(d, "GRID_GROWX", PyInt_FromLong(NEWT_GRID_FLAG_GROWX));
1418
PyDict_SetItemString(d, "GRID_GROWY", PyInt_FromLong(NEWT_GRID_FLAG_GROWY));
1420
PyDict_SetItemString(d, "FD_READ", PyInt_FromLong(NEWT_FD_READ));
1421
PyDict_SetItemString(d, "FD_WRITE", PyInt_FromLong(NEWT_FD_WRITE));
1422
PyDict_SetItemString(d, "FD_EXCEPT", PyInt_FromLong(NEWT_FD_EXCEPT));
1424
PyDict_SetItemString(d, "FORM_EXIT_HOTKEY", PyString_FromString("hotkey"));
1425
PyDict_SetItemString(d, "FORM_EXIT_WIDGET", PyString_FromString("widget"));
1426
PyDict_SetItemString(d, "FORM_EXIT_TIMER", PyString_FromString("timer"));
1427
PyDict_SetItemString(d, "FORM_EXIT_FDREADY", PyString_FromString("fdready"));
1429
PyDict_SetItemString(d, "KEY_TAB", PyInt_FromLong(NEWT_KEY_TAB));
1430
PyDict_SetItemString(d, "KEY_ENTER", PyInt_FromLong(NEWT_KEY_ENTER));
1431
PyDict_SetItemString(d, "KEY_SUSPEND", PyInt_FromLong(NEWT_KEY_SUSPEND));
1432
PyDict_SetItemString(d, "KEY_UP", PyInt_FromLong(NEWT_KEY_UP));
1433
PyDict_SetItemString(d, "KEY_DOWN", PyInt_FromLong(NEWT_KEY_DOWN));
1434
PyDict_SetItemString(d, "KEY_LEFT", PyInt_FromLong(NEWT_KEY_LEFT));
1435
PyDict_SetItemString(d, "KEY_RIGHT", PyInt_FromLong(NEWT_KEY_RIGHT));
1436
PyDict_SetItemString(d, "KEY_BACKSPACE", PyInt_FromLong(NEWT_KEY_BKSPC));
1437
PyDict_SetItemString(d, "KEY_DELETE", PyInt_FromLong(NEWT_KEY_DELETE));
1438
PyDict_SetItemString(d, "KEY_HOME", PyInt_FromLong(NEWT_KEY_HOME));
1439
PyDict_SetItemString(d, "KEY_END", PyInt_FromLong(NEWT_KEY_END));
1440
PyDict_SetItemString(d, "KEY_UNTAB", PyInt_FromLong(NEWT_KEY_UNTAB));
1441
PyDict_SetItemString(d, "KEY_PAGEUP", PyInt_FromLong(NEWT_KEY_PGUP));
1442
PyDict_SetItemString(d, "KEY_PAGEGDOWN", PyInt_FromLong(NEWT_KEY_PGDN));
1443
PyDict_SetItemString(d, "KEY_INSERT", PyInt_FromLong(NEWT_KEY_INSERT));
1444
PyDict_SetItemString(d, "KEY_F1", PyInt_FromLong(NEWT_KEY_F1));
1445
PyDict_SetItemString(d, "KEY_F2", PyInt_FromLong(NEWT_KEY_F2));
1446
PyDict_SetItemString(d, "KEY_F3", PyInt_FromLong(NEWT_KEY_F3));
1447
PyDict_SetItemString(d, "KEY_F4", PyInt_FromLong(NEWT_KEY_F4));
1448
PyDict_SetItemString(d, "KEY_F5", PyInt_FromLong(NEWT_KEY_F5));
1449
PyDict_SetItemString(d, "KEY_F6", PyInt_FromLong(NEWT_KEY_F6));
1450
PyDict_SetItemString(d, "KEY_F7", PyInt_FromLong(NEWT_KEY_F7));
1451
PyDict_SetItemString(d, "KEY_F8", PyInt_FromLong(NEWT_KEY_F8));
1452
PyDict_SetItemString(d, "KEY_F9", PyInt_FromLong(NEWT_KEY_F9));
1453
PyDict_SetItemString(d, "KEY_F10", PyInt_FromLong(NEWT_KEY_F10));
1454
PyDict_SetItemString(d, "KEY_F11", PyInt_FromLong(NEWT_KEY_F11));
1455
PyDict_SetItemString(d, "KEY_F12", PyInt_FromLong(NEWT_KEY_F12));
1456
PyDict_SetItemString(d, "KEY_ESC", PyInt_FromLong(NEWT_KEY_ESCAPE));
1458
PyDict_SetItemString(d, "FLAG_DISABLED", PyInt_FromLong(NEWT_FLAG_DISABLED));
1459
PyDict_SetItemString(d, "FLAGS_SET", PyInt_FromLong(NEWT_FLAGS_SET));
1460
PyDict_SetItemString(d, "FLAGS_RESET", PyInt_FromLong(NEWT_FLAGS_RESET));
1461
PyDict_SetItemString(d, "FLAGS_TOGGLE", PyInt_FromLong(NEWT_FLAGS_TOGGLE));
1463
PyDict_SetItemString(d, "COLORSET_ROOT", PyInt_FromLong(NEWT_COLORSET_ROOT));
1464
PyDict_SetItemString(d, "COLORSET_BORDER", PyInt_FromLong(NEWT_COLORSET_BORDER));
1465
PyDict_SetItemString(d, "COLORSET_WINDOW", PyInt_FromLong(NEWT_COLORSET_WINDOW));
1466
PyDict_SetItemString(d, "COLORSET_SHADOW", PyInt_FromLong(NEWT_COLORSET_SHADOW));
1467
PyDict_SetItemString(d, "COLORSET_TITLE", PyInt_FromLong(NEWT_COLORSET_TITLE));
1468
PyDict_SetItemString(d, "COLORSET_BUTTON", PyInt_FromLong(NEWT_COLORSET_BUTTON));
1469
PyDict_SetItemString(d, "COLORSET_ACTBUTTON", PyInt_FromLong(NEWT_COLORSET_ACTBUTTON));
1470
PyDict_SetItemString(d, "COLORSET_CHECKBOX", PyInt_FromLong(NEWT_COLORSET_CHECKBOX));
1471
PyDict_SetItemString(d, "COLORSET_ACTCHECKBOX", PyInt_FromLong(NEWT_COLORSET_ACTCHECKBOX));
1472
PyDict_SetItemString(d, "COLORSET_ENTRY", PyInt_FromLong(NEWT_COLORSET_ENTRY));
1473
PyDict_SetItemString(d, "COLORSET_LABEL", PyInt_FromLong(NEWT_COLORSET_LABEL));
1474
PyDict_SetItemString(d, "COLORSET_LISTBOX", PyInt_FromLong(NEWT_COLORSET_LISTBOX));
1475
PyDict_SetItemString(d, "COLORSET_ACTLISTBOX", PyInt_FromLong(NEWT_COLORSET_ACTLISTBOX));
1476
PyDict_SetItemString(d, "COLORSET_TEXTBOX", PyInt_FromLong(NEWT_COLORSET_TEXTBOX));
1477
PyDict_SetItemString(d, "COLORSET_ACTTEXTBOX", PyInt_FromLong(NEWT_COLORSET_ACTTEXTBOX));
1478
PyDict_SetItemString(d, "COLORSET_HELPLINE", PyInt_FromLong(NEWT_COLORSET_HELPLINE));
1479
PyDict_SetItemString(d, "COLORSET_ROOTTEXT", PyInt_FromLong(NEWT_COLORSET_ROOTTEXT));
1480
PyDict_SetItemString(d, "COLORSET_EMPTYSCALE", PyInt_FromLong(NEWT_COLORSET_EMPTYSCALE));
1481
PyDict_SetItemString(d, "COLORSET_FULLSCALE", PyInt_FromLong(NEWT_COLORSET_FULLSCALE));
1482
PyDict_SetItemString(d, "COLORSET_DISENTRY", PyInt_FromLong(NEWT_COLORSET_DISENTRY));
1483
PyDict_SetItemString(d, "COLORSET_COMPACTBUTTON", PyInt_FromLong(NEWT_COLORSET_COMPACTBUTTON));
1484
PyDict_SetItemString(d, "COLORSET_ACTSELLISTBOX", PyInt_FromLong(NEWT_COLORSET_ACTSELLISTBOX));
1485
PyDict_SetItemString(d, "COLORSET_SELLISTBOX", PyInt_FromLong(NEWT_COLORSET_SELLISTBOX));
1487
return MOD_SUCCESS_VAL(m);