]>
Commit | Line | Data |
---|---|---|
80d03bd8 | 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 | static void emptyDestructor(PyObject * s); | |
15 | ||
16 | static snackWidget * buttonWidget(PyObject * s, PyObject * args); | |
17 | static PyObject * centeredWindow(PyObject * s, PyObject * args); | |
18 | static snackWidget * checkboxWidget(PyObject * s, PyObject * args); | |
19 | static PyObject * choiceWindow(PyObject * s, PyObject * args); | |
20 | static snackWidget * entryWidget(PyObject * s, PyObject * args); | |
75fb0452 | 21 | static PyObject * drawRootText(PyObject * s, PyObject * args); |
80d03bd8 | 22 | static snackForm * formCreate(PyObject * s, PyObject * args); |
23 | static snackGrid * gridCreate(PyObject * s, PyObject * args); | |
24 | static PyObject * gridWrappedWindow(PyObject * s, PyObject * args); | |
25 | static PyObject * finishScreen(PyObject * s, PyObject * args); | |
26 | static PyObject * initScreen(PyObject * s, PyObject * args); | |
27 | static snackWidget * labelWidget(PyObject * s, PyObject * args); | |
6e25d997 | 28 | static snackWidget * listboxWidget(PyObject * s, PyObject * args); |
80d03bd8 | 29 | static PyObject * messageWindow(PyObject * s, PyObject * args); |
30 | static PyObject * openWindow(PyObject * s, PyObject * args); | |
75fb0452 | 31 | static PyObject * popHelpLine(PyObject * s, PyObject * args); |
80d03bd8 | 32 | static PyObject * popWindow(PyObject * s, PyObject * args); |
75fb0452 | 33 | static PyObject * pushHelpLine(PyObject * s, PyObject * args); |
80d03bd8 | 34 | static snackWidget * radioButtonWidget(PyObject * s, PyObject * args); |
35 | static PyObject * refreshScreen(PyObject * s, PyObject * args); | |
6e25d997 | 36 | static PyObject * reflowText(PyObject * s, PyObject * args); |
37 | static snackWidget * textWidget(PyObject * s, PyObject * args); | |
80d03bd8 | 38 | static PyObject * ternaryWindow(PyObject * s, PyObject * args); |
39 | ||
40 | static PyMethodDef snackModuleMethods[] = { | |
41 | { "button", (PyCFunction) buttonWidget, METH_VARARGS, NULL }, | |
42 | { "checkbox", (PyCFunction) checkboxWidget, METH_VARARGS, NULL }, | |
43 | { "choice", choiceWindow, METH_VARARGS, NULL }, | |
44 | { "centeredwindow", centeredWindow, METH_VARARGS, NULL }, | |
75fb0452 | 45 | { "drawroottext", drawRootText, METH_VARARGS, NULL }, |
80d03bd8 | 46 | { "entry", (PyCFunction) entryWidget, METH_VARARGS, NULL }, |
47 | { "finish", finishScreen, METH_VARARGS, NULL }, | |
48 | { "form", (PyCFunction) formCreate, METH_VARARGS, NULL }, | |
49 | { "grid", (PyCFunction) gridCreate, METH_VARARGS, NULL }, | |
50 | { "gridwrappedwindow", gridWrappedWindow, METH_VARARGS, NULL }, | |
51 | { "init", initScreen, METH_VARARGS, NULL }, | |
52 | { "label", (PyCFunction) labelWidget, METH_VARARGS, NULL }, | |
6e25d997 | 53 | { "listbox", (PyCFunction) listboxWidget, METH_VARARGS, NULL }, |
80d03bd8 | 54 | { "message", messageWindow, METH_VARARGS, NULL }, |
55 | { "openwindow", openWindow, METH_VARARGS, NULL }, | |
75fb0452 | 56 | { "pophelpline", popHelpLine, METH_VARARGS, NULL }, |
80d03bd8 | 57 | { "popwindow", popWindow, METH_VARARGS, NULL }, |
75fb0452 | 58 | { "pushhelpline", pushHelpLine, METH_VARARGS, NULL }, |
80d03bd8 | 59 | { "radiobutton", (PyCFunction) radioButtonWidget, METH_VARARGS, NULL }, |
6e25d997 | 60 | { "reflow", (PyCFunction) reflowText, METH_VARARGS, NULL }, |
80d03bd8 | 61 | { "refresh", refreshScreen, METH_VARARGS, NULL }, |
62 | { "ternary", ternaryWindow, METH_VARARGS, NULL }, | |
6e25d997 | 63 | { "textbox", (PyCFunction) textWidget, METH_VARARGS, NULL }, |
80d03bd8 | 64 | { NULL } |
65 | } ; | |
66 | ||
67 | struct snackGrid_s { | |
68 | PyObject_HEAD; | |
69 | newtGrid grid; | |
70 | } ; | |
71 | ||
72 | static PyObject * gridGetAttr(PyObject * s, char * name); | |
73 | static PyObject * gridPlace(snackGrid * s, PyObject * args); | |
74 | static PyObject * gridSetField(snackGrid * s, PyObject * args); | |
75 | ||
76 | static PyMethodDef gridMethods[] = { | |
77 | { "place", (PyCFunction) gridPlace, METH_VARARGS, NULL }, | |
78 | { "setfield", (PyCFunction) gridSetField, METH_VARARGS, NULL }, | |
79 | { NULL } | |
80 | }; | |
81 | ||
82 | static PyTypeObject snackGridType = { | |
83 | PyObject_HEAD_INIT(&PyType_Type) | |
84 | 0, /* ob_size */ | |
85 | "snackgrid", /* tp_name */ | |
86 | sizeof(snackGrid), /* tp_size */ | |
87 | 0, /* tp_itemsize */ | |
88 | emptyDestructor, /* tp_dealloc */ | |
89 | 0, /* tp_print */ | |
90 | gridGetAttr, /* tp_getattr */ | |
91 | 0, /* tp_setattr */ | |
92 | 0, /* tp_compare */ | |
93 | 0, /* tp_repr */ | |
94 | 0, /* tp_as_number */ | |
95 | 0, /* tp_as_sequence */ | |
96 | 0, /* tp_as_mapping */ | |
97 | }; | |
98 | ||
99 | struct snackForm_s { | |
100 | PyObject_HEAD; | |
101 | newtComponent fo; | |
102 | } ; | |
103 | ||
104 | static PyObject * formGetAttr(PyObject * s, char * name); | |
105 | static PyObject * formAdd(snackForm * s, PyObject * args); | |
106 | static PyObject * formRun(snackForm * s, PyObject * args); | |
ea59ae12 | 107 | static PyObject * formHotKey(snackForm * s, PyObject * args); |
80d03bd8 | 108 | |
109 | static PyMethodDef formMethods[] = { | |
110 | { "add", (PyCFunction) formAdd, METH_VARARGS, NULL }, | |
111 | { "run", (PyCFunction) formRun, METH_VARARGS, NULL }, | |
ea59ae12 | 112 | { "addhotkey", (PyCFunction) formHotKey, METH_VARARGS, NULL }, |
80d03bd8 | 113 | { NULL } |
114 | }; | |
115 | ||
116 | static PyTypeObject snackFormType = { | |
117 | PyObject_HEAD_INIT(&PyType_Type) | |
118 | 0, /* ob_size */ | |
119 | "snackform", /* tp_name */ | |
120 | sizeof(snackForm), /* tp_size */ | |
121 | 0, /* tp_itemsize */ | |
122 | emptyDestructor, /* tp_dealloc */ | |
123 | 0, /* tp_print */ | |
124 | formGetAttr, /* tp_getattr */ | |
125 | 0, /* tp_setattr */ | |
126 | 0, /* tp_compare */ | |
127 | 0, /* tp_repr */ | |
128 | 0, /* tp_as_number */ | |
129 | 0, /* tp_as_sequence */ | |
130 | 0, /* tp_as_mapping */ | |
131 | }; | |
132 | ||
133 | struct snackWidget_s { | |
134 | PyObject_HEAD; | |
135 | newtComponent co; | |
136 | char achar; | |
137 | void * apointer; | |
6e25d997 | 138 | int anint; |
80d03bd8 | 139 | } ; |
140 | ||
141 | static PyObject * widgetGetAttr(PyObject * s, char * name); | |
142 | static PyObject * widgetEntrySetValue(snackWidget * s, PyObject * args); | |
6e25d997 | 143 | static PyObject * widgetListboxSetW(snackWidget * s, PyObject * args); |
144 | static PyObject * widgetListboxAdd(snackWidget * s, PyObject * args); | |
a507b3ec | 145 | static PyObject * widgetListboxIns(snackWidget * s, PyObject * args); |
146 | static PyObject * widgetListboxDel(snackWidget * s, PyObject * args); | |
6e25d997 | 147 | static PyObject * widgetListboxGet(snackWidget * s, PyObject * args); |
80d03bd8 | 148 | |
149 | static PyMethodDef widgetMethods[] = { | |
150 | { "entrySetValue", (PyCFunction) widgetEntrySetValue, METH_VARARGS, NULL }, | |
6e25d997 | 151 | { "listboxAddItem", (PyCFunction) widgetListboxAdd, METH_VARARGS, NULL }, |
a507b3ec | 152 | { "listboxInsertItem", (PyCFunction) widgetListboxIns, METH_VARARGS, NULL }, |
6e25d997 | 153 | { "listboxGetCurrent", (PyCFunction) widgetListboxGet, METH_VARARGS, NULL }, |
154 | { "listboxSetWidth", (PyCFunction) widgetListboxSetW, METH_VARARGS, NULL }, | |
a507b3ec | 155 | { "listboxDeleteItem", (PyCFunction) widgetListboxDel, METH_VARARGS, NULL }, |
80d03bd8 | 156 | { NULL } |
157 | }; | |
158 | ||
159 | static PyTypeObject snackWidgetType = { | |
160 | PyObject_HEAD_INIT(&PyType_Type) | |
161 | 0, /* ob_size */ | |
162 | "snackwidget", /* tp_name */ | |
163 | sizeof(snackWidget), /* tp_size */ | |
164 | 0, /* tp_itemsize */ | |
165 | emptyDestructor, /* tp_dealloc */ | |
166 | 0, /* tp_print */ | |
167 | widgetGetAttr, /* tp_getattr */ | |
168 | 0, /* tp_setattr */ | |
169 | 0, /* tp_compare */ | |
170 | 0, /* tp_repr */ | |
171 | 0, /* tp_as_number */ | |
172 | 0, /* tp_as_sequence */ | |
173 | 0, /* tp_as_mapping */ | |
174 | }; | |
175 | ||
176 | static PyObject * initScreen(PyObject * s, PyObject * args) { | |
177 | newtInit(); | |
178 | newtCls(); | |
179 | ||
180 | Py_INCREF(Py_None); | |
181 | return Py_None; | |
182 | } | |
183 | ||
184 | static PyObject * finishScreen(PyObject * s, PyObject * args) { | |
185 | newtFinished(); | |
186 | Py_INCREF(Py_None); | |
187 | return Py_None; | |
188 | } | |
189 | ||
190 | static PyObject * refreshScreen(PyObject * s, PyObject * args) { | |
191 | newtRefresh(); | |
192 | Py_INCREF(Py_None); | |
193 | return Py_None; | |
194 | } | |
195 | ||
75fb0452 | 196 | static PyObject * drawRootText(PyObject * s, PyObject * args) { |
197 | int left, top; | |
198 | char * text; | |
199 | ||
200 | if (!PyArg_ParseTuple(args, "iis", &left, &top, &text)) | |
201 | return NULL; | |
202 | ||
ec65dcfe | 203 | newtDrawRootText(left, top, text); |
75fb0452 | 204 | |
205 | Py_INCREF(Py_None); | |
206 | return Py_None; | |
207 | } | |
208 | ||
209 | static PyObject * popHelpLine(PyObject * s, PyObject * args) { | |
210 | newtPopHelpLine(); | |
211 | Py_INCREF(Py_None); | |
212 | return Py_None; | |
213 | } | |
214 | ||
215 | static PyObject * pushHelpLine(PyObject * s, PyObject * args) { | |
216 | char * text; | |
217 | ||
218 | if (!PyArg_ParseTuple(args, "s", &text)) | |
219 | return NULL; | |
220 | ||
221 | if (!strcmp(text, "*default*")) | |
222 | newtPushHelpLine(NULL); | |
223 | else | |
224 | newtPushHelpLine(text); | |
225 | ||
226 | Py_INCREF(Py_None); | |
227 | return Py_None; | |
228 | } | |
229 | ||
6e25d997 | 230 | static PyObject * reflowText(PyObject * s, PyObject * args) { |
231 | char * text, * new; | |
232 | int width, minus = 5, plus = 5; | |
233 | int realWidth, realHeight; | |
234 | PyObject * tuple; | |
235 | ||
236 | if (!PyArg_ParseTuple(args, "si|ii", &text, &width, &minus, &plus)) | |
237 | return NULL; | |
238 | ||
239 | new = newtReflowText(text, width, minus, plus, &realWidth, &realHeight); | |
240 | ||
241 | tuple = Py_BuildValue("(sii)", new, realWidth, realHeight); | |
242 | free(new); | |
243 | ||
244 | return tuple; | |
245 | } | |
246 | ||
80d03bd8 | 247 | static PyObject * centeredWindow(PyObject * s, PyObject * args) { |
248 | int width, height; | |
249 | char * title; | |
250 | ||
251 | if (!PyArg_ParseTuple(args, "iis", &width, &height, &title)) | |
252 | return NULL; | |
253 | ||
254 | newtCenteredWindow(width, height, title); | |
255 | ||
256 | Py_INCREF(Py_None); | |
257 | return Py_None; | |
258 | } | |
259 | ||
260 | static PyObject * gridWrappedWindow(PyObject * s, PyObject * args) { | |
261 | snackGrid * grid; | |
262 | char * title; | |
263 | ||
264 | if (!PyArg_ParseTuple(args, "O!s", &snackGridType, &grid, &title)) | |
265 | return NULL; | |
266 | ||
267 | newtGridWrappedWindow(grid->grid, title); | |
268 | ||
269 | Py_INCREF(Py_None); | |
270 | return Py_None; | |
271 | } | |
272 | ||
273 | static PyObject * openWindow(PyObject * s, PyObject * args) { | |
274 | int left, top, width, height; | |
275 | char * title; | |
276 | ||
277 | if (!PyArg_ParseTuple(args, "iiiis", &left, &top, &width, &height, &title)) | |
278 | return NULL; | |
279 | ||
280 | newtOpenWindow(left, top, width, height, title); | |
281 | ||
282 | Py_INCREF(Py_None); | |
283 | return Py_None; | |
284 | } | |
285 | ||
286 | static PyObject * popWindow(PyObject * s, PyObject * args) { | |
287 | newtPopWindow(); | |
288 | Py_INCREF(Py_None); | |
289 | return Py_None; | |
290 | } | |
291 | ||
292 | static PyObject * messageWindow(PyObject * s, PyObject * args) { | |
293 | char * title, * text; | |
294 | char * okbutton = "Ok"; | |
295 | ||
296 | if (!PyArg_ParseTuple(args, "ss|s", &title, &text, &okbutton)) | |
297 | return NULL; | |
298 | ||
299 | newtWinMessage(title, okbutton, text); | |
300 | ||
301 | Py_INCREF(Py_None); | |
302 | return Py_None; | |
303 | } | |
304 | ||
305 | static PyObject * choiceWindow(PyObject * s, PyObject * args) { | |
306 | char * title, * text; | |
307 | char * okbutton = "Ok"; | |
308 | char * cancelbutton = "Cancel"; | |
309 | int rc; | |
310 | ||
311 | if (!PyArg_ParseTuple(args, "ss|ss", &title, &text, &okbutton, | |
312 | &cancelbutton)) | |
313 | return NULL; | |
314 | ||
315 | rc = newtWinChoice(title, okbutton, cancelbutton, text); | |
316 | ||
317 | switch (rc) { | |
318 | case 0: return Py_BuildValue("i", 1); | |
319 | case 1: return Py_BuildValue("i", 2); | |
320 | } | |
321 | ||
322 | return Py_BuildValue("i", 0); | |
323 | } | |
324 | ||
325 | static PyObject * ternaryWindow(PyObject * s, PyObject * args) { | |
326 | char * title, * text, * button1, * button2, * button3; | |
327 | int rc; | |
328 | ||
329 | if (!PyArg_ParseTuple(args, "sssss", &title, &text, &button1, &button2, | |
330 | &button3)) | |
331 | return NULL; | |
332 | ||
333 | rc = newtWinTernary(title, button1, button2, button3, text); | |
334 | ||
335 | return Py_BuildValue("i", rc); | |
336 | } | |
337 | ||
338 | static snackWidget * buttonWidget(PyObject * s, PyObject * args) { | |
339 | snackWidget * widget; | |
340 | char * label; | |
341 | ||
342 | if (!PyArg_ParseTuple(args, "s", &label)) return NULL; | |
343 | ||
344 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
345 | widget->co = newtButton(-1, -1, label); | |
346 | ||
347 | return widget; | |
348 | } | |
349 | ||
350 | static snackWidget * labelWidget(PyObject * s, PyObject * args) { | |
351 | char * label; | |
352 | snackWidget * widget; | |
353 | ||
354 | if (!PyArg_ParseTuple(args, "s", &label)) return NULL; | |
355 | ||
356 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
357 | widget->co = newtLabel(-1, -1, label); | |
358 | ||
359 | return widget; | |
360 | } | |
361 | ||
6e25d997 | 362 | static snackWidget * listboxWidget(PyObject * s, PyObject * args) { |
363 | snackWidget * widget; | |
364 | int height; | |
365 | int doScroll = 0, returnExit = 0 ; | |
366 | ||
367 | if (!PyArg_ParseTuple(args, "i|ii", &height, &doScroll, &returnExit)) | |
368 | return NULL; | |
369 | ||
370 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
371 | widget->co = newtListbox(-1, -1, height, | |
372 | (doScroll ? 0 : NEWT_FLAG_NOSCROLL) | | |
373 | (returnExit ? NEWT_FLAG_RETURNEXIT : 0)); | |
a507b3ec | 374 | widget->anint = 1; |
6e25d997 | 375 | |
376 | return widget; | |
377 | } | |
378 | ||
379 | static snackWidget * textWidget(PyObject * s, PyObject * args) { | |
380 | char * text; | |
381 | int width, height; | |
382 | int scrollBar = 0; | |
383 | snackWidget * widget; | |
384 | ||
385 | if (!PyArg_ParseTuple(args, "iis|i", &width, &height, &text, &scrollBar)) | |
386 | return NULL; | |
387 | ||
388 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
389 | widget->co = newtTextbox(-1, -1, width, height, | |
390 | scrollBar ? NEWT_FLAG_SCROLL : 0); | |
391 | newtTextboxSetText(widget->co, text); | |
392 | ||
393 | return widget; | |
394 | } | |
395 | ||
80d03bd8 | 396 | static snackWidget * radioButtonWidget(PyObject * s, PyObject * args) { |
397 | snackWidget * widget, * group; | |
398 | char * text; | |
399 | int isOn; | |
400 | ||
401 | if (!PyArg_ParseTuple(args, "sOi", &text, &group, &isOn)) | |
402 | return NULL; | |
403 | ||
404 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
405 | ||
406 | if ((PyObject *) group == Py_None) | |
407 | widget->co = newtRadiobutton(-1, -1, text, isOn, NULL); | |
408 | else | |
409 | widget->co = newtRadiobutton(-1, -1, text, isOn, group->co); | |
410 | ||
411 | return widget; | |
412 | } | |
413 | ||
414 | static snackWidget * checkboxWidget(PyObject * s, PyObject * args) { | |
415 | snackWidget * widget; | |
416 | char * text; | |
417 | int isOn; | |
418 | ||
419 | if (!PyArg_ParseTuple(args, "si", &text, &isOn)) return NULL; | |
420 | ||
421 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
422 | widget->co = newtCheckbox(-1, -1, text, isOn ? '*' : ' ', NULL, | |
423 | &widget->achar); | |
424 | ||
425 | return widget; | |
426 | } | |
427 | ||
428 | static snackWidget * entryWidget(PyObject * s, PyObject * args) { | |
429 | snackWidget * widget; | |
430 | int width; | |
431 | char * initial; | |
432 | int isHidden, isScrolled; | |
433 | ||
434 | if (!PyArg_ParseTuple(args, "isii", &width, &initial, | |
435 | &isHidden, &isScrolled)) return NULL; | |
436 | ||
437 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
438 | widget->co = newtEntry(-1, -1, initial, width, (char **) &widget->apointer, | |
439 | (isHidden ? NEWT_FLAG_HIDDEN : 0) | | |
440 | (!isScrolled ? NEWT_FLAG_NOSCROLL : 0)); | |
441 | ||
442 | return widget; | |
443 | } | |
444 | ||
445 | static snackForm * formCreate(PyObject * s, PyObject * args) { | |
446 | snackForm * form; | |
447 | ||
448 | form = PyObject_NEW(snackForm, &snackFormType); | |
449 | form->fo = newtForm(NULL, NULL, 0); | |
450 | ||
451 | return form; | |
452 | } | |
453 | ||
454 | static snackGrid * gridCreate(PyObject * s, PyObject * args) { | |
455 | int rows, cols; | |
456 | snackGrid * grid; | |
457 | ||
458 | if (!PyArg_ParseTuple(args, "ii", &cols, &rows)) return NULL; | |
459 | ||
460 | grid = PyObject_NEW(snackGrid, &snackGridType); | |
461 | grid->grid = newtCreateGrid(cols, rows); | |
462 | ||
463 | return grid; | |
464 | } | |
465 | ||
466 | static PyObject * gridGetAttr(PyObject * s, char * name) { | |
467 | return Py_FindMethod(gridMethods, s, name); | |
468 | } | |
469 | ||
470 | static PyObject * gridPlace(snackGrid * grid, PyObject * args) { | |
471 | int x, y; | |
472 | ||
473 | if (!PyArg_ParseTuple(args, "ii", &x, &y)) return NULL; | |
474 | ||
475 | newtGridPlace(grid->grid, x, y); | |
476 | ||
477 | Py_INCREF(Py_None); | |
478 | return Py_None; | |
479 | } | |
480 | ||
481 | static PyObject * gridSetField(snackGrid * grid, PyObject * args) { | |
482 | snackWidget * w; | |
6e25d997 | 483 | snackGrid * g; |
80d03bd8 | 484 | int x, y; |
485 | int pLeft = 0, pTop = 0, pRight = 0, pBottom = 0; | |
6e25d997 | 486 | int anchorFlags = 0, growFlags = 0; |
80d03bd8 | 487 | |
6e25d997 | 488 | if (!PyArg_ParseTuple(args, "iiO|(iiii)ii", &x, &y, |
489 | &w, &pLeft, &pTop, &pRight, &pBottom, | |
490 | &anchorFlags, &growFlags)) | |
80d03bd8 | 491 | return NULL; |
492 | ||
6e25d997 | 493 | if (w->ob_type == &snackWidgetType) { |
494 | newtGridSetField(grid->grid, x, y, NEWT_GRID_COMPONENT, | |
495 | w->co, pLeft, pTop, pRight, pBottom, anchorFlags, | |
496 | growFlags); | |
497 | } else { | |
498 | g = (snackGrid *) w; | |
499 | newtGridSetField(grid->grid, x, y, NEWT_GRID_SUBGRID, | |
500 | g->grid, pLeft, pTop, pRight, pBottom, anchorFlags, | |
501 | growFlags); | |
502 | } | |
80d03bd8 | 503 | |
504 | Py_INCREF(Py_None); | |
505 | return Py_None; | |
506 | } | |
507 | ||
508 | static PyObject * formGetAttr(PyObject * s, char * name) { | |
509 | return Py_FindMethod(formMethods, s, name); | |
510 | } | |
511 | ||
512 | static PyObject * formAdd(snackForm * s, PyObject * args) { | |
513 | snackWidget * w; | |
514 | int size = PyTuple_Size(args), i; | |
515 | ||
516 | if (!size) { | |
517 | /* this is a hack, I should give an error directly */ | |
518 | if (!PyArg_ParseTuple(args, "O!", &snackWidgetType, &w)) | |
519 | return NULL; | |
520 | } | |
521 | ||
522 | for (i = 0; i < size; i++) { | |
523 | w = (snackWidget *) PyTuple_GET_ITEM(args, i); | |
524 | newtFormAddComponent(s->fo, w->co); | |
525 | } | |
526 | ||
527 | Py_INCREF(Py_None); | |
528 | return Py_None; | |
529 | } | |
530 | ||
531 | static PyObject * formRun(snackForm * s, PyObject * args) { | |
ea59ae12 | 532 | struct newtExitStruct result; |
533 | ||
534 | newtFormRun(s->fo, &result); | |
535 | ||
536 | if (result.reason == NEWT_EXIT_HOTKEY) | |
537 | return Py_BuildValue("(si)", "hotkey", result.u.key); | |
538 | else | |
539 | return Py_BuildValue("(si)", "widget", result.u.co); | |
540 | } | |
541 | ||
542 | static PyObject * formHotKey(snackForm * s, PyObject * args) { | |
543 | int key; | |
544 | ||
545 | if (!PyArg_ParseTuple(args, "i", &key)) | |
546 | return NULL; | |
547 | ||
548 | newtFormAddHotKey(s->fo, key); | |
80d03bd8 | 549 | |
550 | Py_INCREF(Py_None); | |
551 | return Py_None; | |
552 | } | |
553 | ||
554 | static PyObject * widgetGetAttr(PyObject * s, char * name) { | |
555 | snackWidget * w = (snackWidget *) s; | |
556 | ||
ea59ae12 | 557 | if (!strcmp(name, "key")) { |
80d03bd8 | 558 | return Py_BuildValue("i", w->co); |
559 | } else if (!strcmp(name, "entryValue")) { | |
560 | return Py_BuildValue("s", w->apointer); | |
561 | } else if (!strcmp(name, "checkboxValue")) { | |
562 | return Py_BuildValue("i", w->achar == ' ' ? 0 : 1); | |
563 | } else if (!strcmp(name, "radioValue")) { | |
564 | return Py_BuildValue("i", newtRadioGetCurrent(w->co)); | |
565 | } | |
566 | ||
567 | return Py_FindMethod(widgetMethods, s, name); | |
568 | } | |
569 | ||
570 | static PyObject * widgetEntrySetValue(snackWidget * s, PyObject * args) { | |
571 | char * val; | |
572 | ||
573 | if (!PyArg_ParseTuple(args, "s", &val)) | |
574 | return NULL; | |
575 | ||
576 | newtEntrySet(s->co, val, 1); | |
577 | ||
578 | Py_INCREF(Py_None); | |
579 | return Py_None; | |
580 | } | |
581 | ||
6e25d997 | 582 | static PyObject * widgetListboxAdd(snackWidget * s, PyObject * args) { |
583 | char * text; | |
584 | ||
585 | if (!PyArg_ParseTuple(args, "s", &text)) | |
586 | return NULL; | |
587 | ||
588 | newtListboxAddEntry(s->co, text, (void *) s->anint); | |
589 | ||
590 | return PyInt_FromLong(s->anint++); | |
591 | } | |
592 | ||
a507b3ec | 593 | static PyObject * widgetListboxIns(snackWidget * s, PyObject * args) { |
594 | char * text; | |
595 | int key; | |
596 | ||
597 | if (!PyArg_ParseTuple(args, "si", &text, &key)) | |
598 | return NULL; | |
599 | ||
600 | newtListboxInsertEntry(s->co, text, (void *) s->anint, (void *) key); | |
601 | ||
602 | return PyInt_FromLong(s->anint++); | |
603 | } | |
604 | ||
605 | static PyObject * widgetListboxDel(snackWidget * s, PyObject * args) { | |
606 | int key; | |
607 | ||
608 | if (!PyArg_ParseTuple(args, "i", &key)) | |
609 | return NULL; | |
610 | ||
611 | newtListboxDeleteEntry(s->co, (void *) key); | |
612 | ||
613 | Py_INCREF(Py_None); | |
614 | return Py_None; | |
615 | } | |
616 | ||
6e25d997 | 617 | static PyObject * widgetListboxGet(snackWidget * s, PyObject * args) { |
618 | if (!PyArg_ParseTuple(args, "")) | |
619 | return NULL; | |
620 | ||
621 | return PyInt_FromLong((long) newtListboxGetCurrent(s->co)); | |
622 | } | |
623 | ||
624 | static PyObject * widgetListboxSetW(snackWidget * s, PyObject * args) { | |
625 | int width; | |
626 | ||
627 | if (!PyArg_ParseTuple(args, "i", &width)) | |
628 | return NULL; | |
629 | ||
630 | newtListboxSetWidth(s->co, width); | |
631 | ||
632 | Py_INCREF(Py_None); | |
633 | return Py_None; | |
634 | } | |
635 | ||
80d03bd8 | 636 | static void emptyDestructor(PyObject * s) { |
637 | } | |
638 | ||
639 | void init_snack(void) { | |
6e25d997 | 640 | PyObject * d, * m; |
641 | ||
642 | m = Py_InitModule("_snack", snackModuleMethods); | |
643 | d = PyModule_GetDict(m); | |
644 | ||
645 | PyDict_SetItemString(d, "ANCHOR_LEFT", PyInt_FromLong(NEWT_ANCHOR_LEFT)); | |
646 | PyDict_SetItemString(d, "ANCHOR_TOP", PyInt_FromLong(NEWT_ANCHOR_TOP)); | |
647 | PyDict_SetItemString(d, "ANCHOR_RIGHT", PyInt_FromLong(NEWT_ANCHOR_RIGHT)); | |
648 | PyDict_SetItemString(d, "ANCHOR_BOTTOM", | |
649 | PyInt_FromLong(NEWT_ANCHOR_BOTTOM)); | |
650 | PyDict_SetItemString(d, "GRID_GROWX", PyInt_FromLong(NEWT_GRID_FLAG_GROWX)); | |
651 | PyDict_SetItemString(d, "GRID_GROWY", PyInt_FromLong(NEWT_GRID_FLAG_GROWY)); | |
ea59ae12 | 652 | |
653 | PyDict_SetItemString(d, "FORM_EXIT_HOTKEY", PyString_FromString("hotkey")); | |
654 | PyDict_SetItemString(d, "FORM_EXIT_WIDGET", PyString_FromString("widget")); | |
655 | ||
656 | PyDict_SetItemString(d, "KEY_F1", PyInt_FromLong(NEWT_KEY_F1)); | |
657 | PyDict_SetItemString(d, "KEY_F2", PyInt_FromLong(NEWT_KEY_F2)); | |
658 | PyDict_SetItemString(d, "KEY_F3", PyInt_FromLong(NEWT_KEY_F3)); | |
659 | PyDict_SetItemString(d, "KEY_F4", PyInt_FromLong(NEWT_KEY_F4)); | |
660 | PyDict_SetItemString(d, "KEY_F5", PyInt_FromLong(NEWT_KEY_F5)); | |
661 | PyDict_SetItemString(d, "KEY_F6", PyInt_FromLong(NEWT_KEY_F6)); | |
662 | PyDict_SetItemString(d, "KEY_F7", PyInt_FromLong(NEWT_KEY_F7)); | |
663 | PyDict_SetItemString(d, "KEY_F8", PyInt_FromLong(NEWT_KEY_F8)); | |
664 | PyDict_SetItemString(d, "KEY_F9", PyInt_FromLong(NEWT_KEY_F9)); | |
665 | PyDict_SetItemString(d, "KEY_F10", PyInt_FromLong(NEWT_KEY_F10)); | |
666 | PyDict_SetItemString(d, "KEY_F11", PyInt_FromLong(NEWT_KEY_F11)); | |
667 | PyDict_SetItemString(d, "KEY_F12", PyInt_FromLong(NEWT_KEY_F12)); | |
80d03bd8 | 668 | } |