]> git.ipfire.org Git - thirdparty/newt.git/blob - snackmodule.c
rev
[thirdparty/newt.git] / snackmodule.c
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <sys/stat.h>
4 #include <sys/time.h>
5 #include <unistd.h>
6
7 #include "Python.h"
8 #include "newt.h"
9
10 typedef struct snackWidget_s snackWidget;
11 typedef struct snackGrid_s snackGrid;
12 typedef struct snackForm_s snackForm;
13
14 struct callbackStruct {
15 PyObject * cb, * data;
16 };
17
18 static struct callbackStruct suspend;
19 static struct callbackStruct helpCallback;
20
21 static void emptyDestructor(PyObject * s);
22
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);
55
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 },
87 { NULL }
88 } ;
89
90 struct snackGrid_s {
91 PyObject_HEAD;
92 newtGrid grid;
93 } ;
94
95 static PyObject * gridGetAttr(PyObject * s, char * name);
96 static PyObject * gridPlace(snackGrid * s, PyObject * args);
97 static PyObject * gridSetField(snackGrid * s, PyObject * args);
98
99 static PyMethodDef gridMethods[] = {
100 { "place", (PyCFunction) gridPlace, METH_VARARGS, NULL },
101 { "setfield", (PyCFunction) gridSetField, METH_VARARGS, NULL },
102 { NULL }
103 };
104
105 static PyTypeObject snackGridType = {
106 PyObject_HEAD_INIT(&PyType_Type)
107 0, /* ob_size */
108 "snackgrid", /* tp_name */
109 sizeof(snackGrid), /* tp_size */
110 0, /* tp_itemsize */
111 emptyDestructor, /* tp_dealloc */
112 0, /* tp_print */
113 gridGetAttr, /* tp_getattr */
114 0, /* tp_setattr */
115 0, /* tp_compare */
116 0, /* tp_repr */
117 0, /* tp_as_number */
118 0, /* tp_as_sequence */
119 0, /* tp_as_mapping */
120 };
121
122 struct snackForm_s {
123 PyObject_HEAD;
124 newtComponent fo;
125 } ;
126
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);
134
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 },
142 { NULL }
143 };
144
145 static PyTypeObject snackFormType = {
146 PyObject_HEAD_INIT(&PyType_Type)
147 0, /* ob_size */
148 "snackform", /* tp_name */
149 sizeof(snackForm), /* tp_size */
150 0, /* tp_itemsize */
151 emptyDestructor, /* tp_dealloc */
152 0, /* tp_print */
153 formGetAttr, /* tp_getattr */
154 0, /* tp_setattr */
155 0, /* tp_compare */
156 0, /* tp_repr */
157 0, /* tp_as_number */
158 0, /* tp_as_sequence */
159 0, /* tp_as_mapping */
160 };
161
162 struct snackWidget_s {
163 PyObject_HEAD;
164 newtComponent co;
165 char achar;
166 void * apointer;
167 int anint;
168 struct callbackStruct scs;
169 } ;
170
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);
192
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 },
220 { NULL }
221 };
222
223 static PyTypeObject snackWidgetType = {
224 PyObject_HEAD_INIT(&PyType_Type)
225 0, /* ob_size */
226 "snackwidget", /* tp_name */
227 sizeof(snackWidget), /* tp_size */
228 0, /* tp_itemsize */
229 widgetDestructor, /* tp_dealloc */
230 0, /* tp_print */
231 widgetGetAttr, /* tp_getattr */
232 0, /* tp_setattr */
233 0, /* tp_compare */
234 0, /* tp_repr */
235 0, /* tp_as_number */
236 0, /* tp_as_sequence */
237 0, /* tp_as_mapping */
238 };
239
240 static snackWidget * snackWidgetNew (void) {
241 snackWidget * widget;
242
243 widget = PyObject_NEW(snackWidget, &snackWidgetType);
244
245 widget->scs.cb = NULL;
246 widget->scs.data = NULL;
247
248 return widget;
249 }
250
251 static PyObject * initScreen(PyObject * s, PyObject * args) {
252 suspend.cb = NULL;
253 suspend.data = NULL;
254
255 newtInit();
256 newtCls();
257
258 Py_INCREF(Py_None);
259 return Py_None;
260 }
261
262 static PyObject * finishScreen(PyObject * s, PyObject * args) {
263 Py_XDECREF (suspend.cb);
264 Py_XDECREF (suspend.data);
265
266 newtFinished();
267 Py_INCREF(Py_None);
268 return Py_None;
269 }
270
271 static PyObject * refreshScreen(PyObject * s, PyObject * args) {
272 newtRefresh();
273 Py_INCREF(Py_None);
274 return Py_None;
275 }
276
277 static PyObject * scaleWidget(PyObject * s, PyObject * args) {
278 snackWidget * widget;
279 int width, fullAmount;
280
281 if (!PyArg_ParseTuple(args, "ii", &width, &fullAmount)) return NULL;
282
283 widget = snackWidgetNew ();
284 widget->co = newtScale(-1, -1, width, fullAmount);
285
286 return (PyObject *) widget;
287 }
288
289 static PyObject * scaleSet(snackWidget * s, PyObject * args) {
290 int amount;
291
292 if (!PyArg_ParseTuple(args, "i", &amount)) return NULL;
293
294 newtScaleSet(s->co, amount);
295
296 Py_INCREF(Py_None);
297 return Py_None;
298 }
299
300 static PyObject * screenSize(PyObject * s, PyObject * args) {
301 int width, height;
302
303 if (!PyArg_ParseTuple(args, ""))
304 return NULL;
305
306 newtGetScreenSize(&width, &height);
307
308 return Py_BuildValue("(ii)", width, height);
309 }
310
311 static void helpCallbackMarshall(newtComponent co, void * data) {
312 PyObject * args, * result;
313
314 args = Py_BuildValue("(O)", data);
315 result = PyEval_CallObject(helpCallback.cb, args);
316 Py_DECREF (args);
317 Py_XDECREF(result);
318
319 return;
320 }
321
322 static void suspendCallbackMarshall(void * data) {
323 struct callbackStruct * scs = data;
324 PyObject * args, * result;
325
326 if (scs->data) {
327 args = Py_BuildValue("(O)", scs->data);
328 result = PyEval_CallObject(scs->cb, args);
329 Py_DECREF (args);
330 } else
331 result = PyEval_CallObject(scs->cb, NULL);
332
333 if (!result) {
334 PyErr_Print();
335 PyErr_Clear();
336 }
337
338 Py_XDECREF(result);
339
340 return;
341 }
342
343 static void callbackMarshall(newtComponent co, void * data) {
344 struct callbackStruct * scs = data;
345 PyObject * args, * result;
346
347 if (scs->data) {
348 args = Py_BuildValue("(O)", scs->data);
349 result = PyEval_CallObject(scs->cb, args);
350 Py_DECREF (args);
351 } else
352 result = PyEval_CallObject(scs->cb, NULL);
353
354 if (!result) {
355 PyErr_Print();
356 PyErr_Clear();
357 }
358
359 Py_XDECREF(result);
360
361 return;
362 }
363
364 static PyObject * setSuspendCallback(PyObject * s, PyObject * args) {
365 if (!PyArg_ParseTuple(args, "O|O", &suspend.cb, &suspend.data))
366 return NULL;
367
368 Py_INCREF (suspend.cb);
369 Py_XINCREF (suspend.data);
370
371 newtSetSuspendCallback(suspendCallbackMarshall, &suspend);
372
373 Py_INCREF(Py_None);
374 return Py_None;
375 }
376
377 static PyObject * setHelpCallback(PyObject * s, PyObject * args) {
378 if (!PyArg_ParseTuple(args, "O", &helpCallback.cb))
379 return NULL;
380
381 /*if (helpCallback.cb) {
382 Py_DECREF (helpCallback.cb);
383 }*/
384
385 Py_INCREF (helpCallback.cb);
386
387 newtSetHelpCallback(helpCallbackMarshall);
388
389 Py_INCREF(Py_None);
390 return Py_None;
391 }
392
393 static PyObject * drawRootText(PyObject * s, PyObject * args) {
394 int left, top;
395 char * text;
396
397 if (!PyArg_ParseTuple(args, "iis", &left, &top, &text))
398 return NULL;
399
400 newtDrawRootText(left, top, text);
401
402 Py_INCREF(Py_None);
403 return Py_None;
404 }
405
406 static PyObject * doSuspend(PyObject * s, PyObject * args) {
407 newtSuspend();
408
409 Py_INCREF(Py_None);
410 return Py_None;
411 }
412
413 static PyObject * doResume(PyObject * s, PyObject * args) {
414 newtResume();
415
416 Py_INCREF(Py_None);
417 return Py_None;
418 }
419
420 static PyObject * popHelpLine(PyObject * s, PyObject * args) {
421 newtPopHelpLine();
422 Py_INCREF(Py_None);
423 return Py_None;
424 }
425
426 static PyObject * pushHelpLine(PyObject * s, PyObject * args) {
427 char * text;
428
429 if (!PyArg_ParseTuple(args, "s", &text))
430 return NULL;
431
432 if (!strcmp(text, "*default*"))
433 newtPushHelpLine(NULL);
434 else
435 newtPushHelpLine(text);
436
437 Py_INCREF(Py_None);
438 return Py_None;
439 }
440
441 static PyObject * reflowText(PyObject * s, PyObject * args) {
442 char * text, * new;
443 int width, minus = 5, plus = 5;
444 int realWidth, realHeight;
445 PyObject * tuple;
446
447 if (!PyArg_ParseTuple(args, "si|ii", &text, &width, &minus, &plus))
448 return NULL;
449
450 new = newtReflowText(text, width, minus, plus, &realWidth, &realHeight);
451
452 tuple = Py_BuildValue("(sii)", new, realWidth, realHeight);
453 free(new);
454
455 return tuple;
456 }
457
458 static PyObject * centeredWindow(PyObject * s, PyObject * args) {
459 int width, height;
460 char * title;
461
462 if (!PyArg_ParseTuple(args, "iis", &width, &height, &title))
463 return NULL;
464
465 newtCenteredWindow(width, height, title);
466
467 Py_INCREF(Py_None);
468 return Py_None;
469 }
470
471 static PyObject * gridWrappedWindow(PyObject * s, PyObject * args) {
472 snackGrid * grid;
473 char * title;
474 int x = -1, y = -1;
475
476 if (!PyArg_ParseTuple(args, "O!s|ii", &snackGridType, &grid, &title,
477 &x, &y))
478 return NULL;
479
480 if (y == -1)
481 newtGridWrappedWindow(grid->grid, title);
482 else
483 newtGridWrappedWindowAt(grid->grid, title, x, y);
484
485 Py_INCREF(Py_None);
486 return Py_None;
487 }
488
489 static PyObject * openWindow(PyObject * s, PyObject * args) {
490 int left, top, width, height;
491 char * title;
492
493 if (!PyArg_ParseTuple(args, "iiiis", &left, &top, &width, &height, &title))
494 return NULL;
495
496 newtOpenWindow(left, top, width, height, title);
497
498 Py_INCREF(Py_None);
499 return Py_None;
500 }
501
502 static PyObject * popWindow(PyObject * s, PyObject * args) {
503 newtPopWindow();
504 Py_INCREF(Py_None);
505 return Py_None;
506 }
507
508 static PyObject * messageWindow(PyObject * s, PyObject * args) {
509 char * title, * text;
510 char * okbutton = "Ok";
511
512 if (!PyArg_ParseTuple(args, "ss|s", &title, &text, &okbutton))
513 return NULL;
514
515 newtWinMessage(title, okbutton, text);
516
517 Py_INCREF(Py_None);
518 return Py_None;
519 }
520
521 static PyObject * choiceWindow(PyObject * s, PyObject * args) {
522 char * title, * text;
523 char * okbutton = "Ok";
524 char * cancelbutton = "Cancel";
525 int rc;
526
527 if (!PyArg_ParseTuple(args, "ss|ss", &title, &text, &okbutton,
528 &cancelbutton))
529 return NULL;
530
531 rc = newtWinChoice(title, okbutton, cancelbutton, text);
532
533 return Py_BuildValue("i", rc);
534 }
535
536 static PyObject * ternaryWindow(PyObject * s, PyObject * args) {
537 char * title, * text, * button1, * button2, * button3;
538 int rc;
539
540 if (!PyArg_ParseTuple(args, "sssss", &title, &text, &button1, &button2,
541 &button3))
542 return NULL;
543
544 rc = newtWinTernary(title, button1, button2, button3, text);
545
546 return Py_BuildValue("i", rc);
547 }
548
549 static snackWidget * buttonWidget(PyObject * s, PyObject * args) {
550 snackWidget * widget;
551 char * label;
552
553 if (!PyArg_ParseTuple(args, "s", &label)) return NULL;
554
555 widget = snackWidgetNew ();
556 widget->co = newtButton(-1, -1, label);
557
558 return widget;
559 }
560
561 static snackWidget * labelWidget(PyObject * s, PyObject * args) {
562 char * label;
563 snackWidget * widget;
564
565 if (!PyArg_ParseTuple(args, "s", &label)) return NULL;
566
567 widget = snackWidgetNew ();
568 widget->co = newtLabel(-1, -1, label);
569
570 return widget;
571 }
572
573 static PyObject * widgetLabelText(snackWidget * s, PyObject * args) {
574 char * label;
575
576 if (!PyArg_ParseTuple(args, "s", &label)) return NULL;
577
578 newtLabelSetText(s->co, label);
579
580 Py_INCREF(Py_None);
581 return Py_None;
582 }
583
584 static PyObject * widgetTextboxText(snackWidget * s, PyObject * args) {
585 char * text;
586
587 if (!PyArg_ParseTuple(args, "s", &text)) return NULL;
588
589 newtTextboxSetText(s->co, text);
590
591 Py_INCREF(Py_None);
592 return Py_None;
593 }
594
595 static snackWidget * listboxWidget(PyObject * s, PyObject * args) {
596 snackWidget * widget;
597 int height;
598 int doScroll = 0, returnExit = 0 ;
599
600 if (!PyArg_ParseTuple(args, "i|ii", &height, &doScroll, &returnExit))
601 return NULL;
602
603 widget = snackWidgetNew ();
604 widget->co = newtListbox(-1, -1, height,
605 (doScroll ? NEWT_FLAG_SCROLL : 0) |
606 (returnExit ? NEWT_FLAG_RETURNEXIT : 0));
607 widget->anint = 1;
608
609 return widget;
610 }
611
612 static snackWidget * textWidget(PyObject * s, PyObject * args) {
613 char * text;
614 int width, height;
615 int scrollBar = 0;
616 int wrap = 0;
617 snackWidget * widget;
618
619 if (!PyArg_ParseTuple(args, "iis|ii", &width, &height, &text, &scrollBar, &wrap))
620 return NULL;
621
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);
627
628 return widget;
629 }
630
631 static snackWidget * radioButtonWidget(PyObject * s, PyObject * args) {
632 snackWidget * widget, * group;
633 char * text;
634 int isOn;
635
636 if (!PyArg_ParseTuple(args, "sOi", &text, &group, &isOn))
637 return NULL;
638
639 widget = snackWidgetNew ();
640
641 if ((PyObject *) group == Py_None)
642 widget->co = newtRadiobutton(-1, -1, text, isOn, NULL);
643 else
644 widget->co = newtRadiobutton(-1, -1, text, isOn, group->co);
645
646 return widget;
647 }
648
649 static snackWidget * checkboxWidget(PyObject * s, PyObject * args) {
650 snackWidget * widget;
651 char * text;
652 int isOn;
653
654 if (!PyArg_ParseTuple(args, "si", &text, &isOn)) return NULL;
655
656 widget = snackWidgetNew ();
657 widget->co = newtCheckbox(-1, -1, text, isOn ? '*' : ' ', NULL,
658 &widget->achar);
659
660 return widget;
661 }
662
663 static PyObject * widgetCheckboxSetFlags(snackWidget * s, PyObject * args) {
664 int flag, sense;
665
666 if (!PyArg_ParseTuple(args, "ii", &flag, &sense)) return NULL;
667
668 newtCheckboxSetFlags(s->co, flag, sense);
669
670 Py_INCREF(Py_None);
671 return Py_None;
672 }
673
674 static PyObject * widgetCheckboxSetValue(snackWidget * s, PyObject * args) {
675 char *value;
676
677 if (!PyArg_ParseTuple(args, "s", &value)) return NULL;
678
679 newtCheckboxSetValue(s->co, *value);
680
681 Py_INCREF(Py_None);
682 return Py_None;
683 }
684
685 static snackWidget * entryWidget(PyObject * s, PyObject * args) {
686 snackWidget * widget;
687 int width;
688 char * initial;
689 int isHidden, isScrolled, returnExit, isPassword;
690
691 if (!PyArg_ParseTuple(args, "isiiii", &width, &initial,
692 &isHidden, &isPassword, &isScrolled, &returnExit)) return NULL;
693
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));
700
701 return widget;
702 }
703
704 static snackForm * formCreate(PyObject * s, PyObject * args) {
705 snackForm * form;
706 PyObject * help = Py_None;
707
708 if (!PyArg_ParseTuple(args, "|O", &help)) return NULL;
709
710 if (help == Py_None)
711 help = NULL;
712
713 form = PyObject_NEW(snackForm, &snackFormType);
714 form->fo = newtForm(NULL, help, 0);
715
716 return form;
717 }
718
719 static snackGrid * gridCreate(PyObject * s, PyObject * args) {
720 int rows, cols;
721 snackGrid * grid;
722
723 if (!PyArg_ParseTuple(args, "ii", &cols, &rows)) return NULL;
724
725 grid = PyObject_NEW(snackGrid, &snackGridType);
726 grid->grid = newtCreateGrid(cols, rows);
727
728 return grid;
729 }
730
731 static PyObject * gridGetAttr(PyObject * s, char * name) {
732 return Py_FindMethod(gridMethods, s, name);
733 }
734
735 static PyObject * gridPlace(snackGrid * grid, PyObject * args) {
736 int x, y;
737
738 if (!PyArg_ParseTuple(args, "ii", &x, &y)) return NULL;
739
740 newtGridPlace(grid->grid, x, y);
741
742 Py_INCREF(Py_None);
743 return Py_None;
744 }
745
746 static PyObject * gridSetField(snackGrid * grid, PyObject * args) {
747 snackWidget * w;
748 snackGrid * g;
749 int x, y;
750 int pLeft = 0, pTop = 0, pRight = 0, pBottom = 0;
751 int anchorFlags = 0, growFlags = 0;
752
753 if (!PyArg_ParseTuple(args, "iiO|(iiii)ii", &x, &y,
754 &w, &pLeft, &pTop, &pRight, &pBottom,
755 &anchorFlags, &growFlags))
756 return NULL;
757
758 if (w->ob_type == &snackWidgetType) {
759 newtGridSetField(grid->grid, x, y, NEWT_GRID_COMPONENT,
760 w->co, pLeft, pTop, pRight, pBottom, anchorFlags,
761 growFlags);
762 } else {
763 g = (snackGrid *) w;
764 newtGridSetField(grid->grid, x, y, NEWT_GRID_SUBGRID,
765 g->grid, pLeft, pTop, pRight, pBottom, anchorFlags,
766 growFlags);
767 }
768
769 Py_INCREF(Py_None);
770 return Py_None;
771 }
772
773 static PyObject * formGetAttr(PyObject * s, char * name) {
774 return Py_FindMethod(formMethods, s, name);
775 }
776
777 static PyObject * formDraw(snackForm * s, PyObject * args) {
778 newtDrawForm(s->fo);
779
780 Py_INCREF(Py_None);
781 return Py_None;
782 }
783
784 static PyObject * formAdd(snackForm * s, PyObject * args) {
785 snackWidget * w;
786 int size = PyTuple_Size(args), i;
787
788 if (!size) {
789 /* this is a hack, I should give an error directly */
790 if (!PyArg_ParseTuple(args, "O!", &snackWidgetType, &w))
791 return NULL;
792 }
793
794 for (i = 0; i < size; i++) {
795 w = (snackWidget *) PyTuple_GET_ITEM(args, i);
796 newtFormAddComponent(s->fo, w->co);
797 }
798
799 Py_INCREF(Py_None);
800 return Py_None;
801 }
802
803 static PyObject * formRun(snackForm * s, PyObject * args) {
804 struct newtExitStruct result;
805
806 newtFormRun(s->fo, &result);
807
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);
812 else
813 return Py_BuildValue("(si)", "widget", result.u.co);
814 }
815
816 static PyObject * formHotKey(snackForm * s, PyObject * args) {
817 int key;
818
819 if (!PyArg_ParseTuple(args, "i", &key))
820 return NULL;
821
822 newtFormAddHotKey(s->fo, key);
823
824 Py_INCREF(Py_None);
825 return Py_None;
826 }
827
828 static PyObject * formSetTimer(snackForm * form, PyObject * args) {
829 int millisecs;
830
831 if (!PyArg_ParseTuple(args, "i", &millisecs))
832 return NULL;
833
834 newtFormSetTimer(form->fo, millisecs);
835
836 Py_INCREF(Py_None);
837 return Py_None;
838 }
839
840 static PyObject * formSetCurrent(snackForm * form, PyObject * args) {
841 snackWidget * w;
842
843 if (!PyArg_ParseTuple(args, "O", &w))
844 return NULL;
845
846 newtFormSetCurrent(form->fo, w->co);
847
848 Py_INCREF(Py_None);
849 return Py_None;
850 }
851
852 static PyObject * widgetGetAttr(PyObject * s, char * name) {
853 snackWidget * w = (snackWidget *) s;
854
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));
863 }
864
865 return Py_FindMethod(widgetMethods, s, name);
866 }
867
868 static void widgetDestructor(PyObject * o) {
869 snackWidget * s = (snackWidget *) o;
870
871 Py_XDECREF (s->scs.cb);
872 Py_XDECREF (s->scs.data);
873
874 PyMem_DEL(o);
875 }
876
877 static PyObject * widgetAddCallback(snackWidget * s, PyObject * args) {
878 s->scs.cb = NULL;
879 s->scs.data = NULL;
880
881 if (!PyArg_ParseTuple(args, "O|O", &s->scs.cb, &s->scs.data))
882 return NULL;
883
884 Py_INCREF (s->scs.cb);
885 Py_XINCREF (s->scs.data);
886
887 newtComponentAddCallback(s->co, callbackMarshall, &s->scs);
888
889 Py_INCREF(Py_None);
890 return Py_None;
891 }
892
893 static PyObject * widgetEntrySetValue(snackWidget * s, PyObject * args) {
894 char * val;
895
896 if (!PyArg_ParseTuple(args, "s", &val))
897 return NULL;
898
899 newtEntrySet(s->co, val, 1);
900
901 Py_INCREF(Py_None);
902 return Py_None;
903 }
904
905 static PyObject * widgetEntrySetFlags(snackWidget * s, PyObject * args) {
906 int flag, sense;
907
908 if (!PyArg_ParseTuple(args, "ii", &flag, &sense)) return NULL;
909
910 newtEntrySetFlags(s->co, flag, sense);
911
912 Py_INCREF(Py_None);
913 return Py_None;
914 }
915
916
917 static PyObject * widgetListboxAdd(snackWidget * s, PyObject * args) {
918 char * text;
919
920 if (!PyArg_ParseTuple(args, "s", &text))
921 return NULL;
922
923 newtListboxAddEntry(s->co, text, (void *) s->anint);
924
925 return PyInt_FromLong(s->anint++);
926 }
927
928 static PyObject * widgetListboxIns(snackWidget * s, PyObject * args) {
929 char * text;
930 int key;
931
932 if (!PyArg_ParseTuple(args, "si", &text, &key))
933 return NULL;
934
935 newtListboxInsertEntry(s->co, text, (void *) s->anint, (void *) key);
936
937 return PyInt_FromLong(s->anint++);
938 }
939
940 static PyObject * widgetListboxDel(snackWidget * s, PyObject * args) {
941 int key;
942
943 if (!PyArg_ParseTuple(args, "i", &key))
944 return NULL;
945
946 newtListboxDeleteEntry(s->co, (void *) key);
947
948 Py_INCREF(Py_None);
949 return Py_None;
950 }
951
952 static PyObject * widgetListboxGet(snackWidget * s, PyObject * args) {
953 if (!PyArg_ParseTuple(args, ""))
954 return NULL;
955
956 return PyInt_FromLong((long) newtListboxGetCurrent(s->co));
957 }
958
959 static PyObject * widgetListboxSet(snackWidget * s, PyObject * args) {
960 int index;
961
962 if (!PyArg_ParseTuple(args, "i", &index))
963 return NULL;
964
965 newtListboxSetCurrentByKey(s->co, (void *) index);
966
967 Py_INCREF(Py_None);
968 return Py_None;
969 }
970
971 static PyObject * widgetListboxSetW(snackWidget * s, PyObject * args) {
972 int width;
973
974 if (!PyArg_ParseTuple(args, "i", &width))
975 return NULL;
976
977 newtListboxSetWidth(s->co, width);
978
979 Py_INCREF(Py_None);
980 return Py_None;
981 }
982
983 static void emptyDestructor(PyObject * s) {
984 }
985
986 static snackWidget * checkboxTreeWidget(PyObject * s, PyObject * args) {
987 int height;
988 int scrollBar = 0;
989 snackWidget * widget;
990
991 if (!PyArg_ParseTuple(args, "i|i", &height, &scrollBar))
992 return NULL;
993
994 widget = snackWidgetNew ();
995 widget->co = newtCheckboxTree(-1, -1, height,
996 scrollBar ? NEWT_FLAG_SCROLL : 0);
997
998 widget->anint = 1;
999
1000 return widget;
1001 }
1002
1003 static PyObject * widgetCheckboxTreeAddItem(snackWidget * s, PyObject * args) {
1004 char * text;
1005 int selected = 0;
1006 PyObject * pathList, * o;
1007 int len;
1008 int * path;
1009 int i;
1010
1011 if (!PyArg_ParseTuple(args, "sOi", &text, &pathList, &selected))
1012 return NULL;
1013
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);
1019 }
1020 path[len] = NEWT_ARG_LAST;
1021
1022 newtCheckboxTreeAddArray(s->co, text, (void *) s->anint,
1023 selected ? NEWT_FLAG_SELECTED : 0, path);
1024
1025 return PyInt_FromLong(s->anint++);
1026 }
1027
1028 static PyObject * widgetCheckboxTreeGetCur(snackWidget * s, PyObject * args) {
1029 if (!PyArg_ParseTuple(args, ""))
1030 return NULL;
1031
1032 return PyInt_FromLong((int) newtCheckboxTreeGetCurrent(s->co));
1033 }
1034
1035 static PyObject * widgetCheckboxTreeSetEntry(snackWidget * s, PyObject * args) {
1036 int data;
1037 char *text;
1038
1039 if (!PyArg_ParseTuple(args, "is", &data, &text)) return NULL;
1040
1041 newtCheckboxTreeSetEntry(s->co, (void *)data, text);
1042
1043 Py_INCREF(Py_None);
1044 return Py_None;
1045 }
1046
1047 static PyObject * widgetCheckboxTreeSetEntryValue(snackWidget * s, PyObject * args) {
1048 int data;
1049 int isOn = 1;
1050
1051 if (!PyArg_ParseTuple(args, "i|i", &data, &isOn)) return NULL;
1052
1053 newtCheckboxTreeSetEntryValue(s->co, (void *)data,
1054 isOn ? NEWT_CHECKBOXTREE_SELECTED :
1055 NEWT_CHECKBOXTREE_UNSELECTED);
1056
1057 Py_INCREF(Py_None);
1058 return Py_None;
1059 }
1060
1061 static PyObject * widgetCheckboxTreeGetEntryValue(snackWidget * s, PyObject * args) {
1062 int data;
1063 int isOn = 0;
1064 int isBranch = 0;
1065 char selection;
1066
1067 if (!PyArg_ParseTuple(args, "i", &data)) return NULL;
1068
1069 selection = newtCheckboxTreeGetEntryValue(s->co, (void *)data);
1070
1071 if (selection == -1) return NULL;
1072
1073 switch (selection) {
1074 case NEWT_CHECKBOXTREE_EXPANDED:
1075 isOn = 1;
1076 case NEWT_CHECKBOXTREE_COLLAPSED:
1077 isBranch = 1;
1078 break;
1079 case NEWT_CHECKBOXTREE_UNSELECTED:
1080 break;
1081 default:
1082 isOn = 1;
1083 break;
1084 }
1085 return Py_BuildValue("(ii)", isBranch, isOn);
1086 }
1087
1088 static PyObject * widgetCheckboxTreeGetSel(snackWidget * s,
1089 PyObject * args) {
1090 void ** selection;
1091 int numselected;
1092 int i;
1093 PyObject * sel;
1094
1095 if (!PyArg_ParseTuple(args, ""))
1096 return NULL;
1097
1098 selection = (void **) newtCheckboxTreeGetSelection(s->co, &numselected);
1099
1100 sel = PyList_New(0);
1101
1102 if (!selection) {
1103 return sel;
1104 }
1105
1106 sel = PyList_New(0);
1107 for (i = 0; i < numselected; i++) {
1108 PyList_Append(sel, PyInt_FromLong((int) selection[i]));
1109 }
1110 free(selection);
1111
1112 return sel;
1113 }
1114
1115 void init_snack(void) {
1116 PyObject * d, * m;
1117
1118 m = Py_InitModule("_snack", snackModuleMethods);
1119 d = PyModule_GetDict(m);
1120
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));
1128
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"));
1132
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));
1145
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));
1150 }