]>
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 | ||
06cf519e | 14 | struct suspendCallbackStruct { |
15 | PyObject * cb, * data; | |
16 | }; | |
17 | ||
80d03bd8 | 18 | static void emptyDestructor(PyObject * s); |
19 | ||
20 | static snackWidget * buttonWidget(PyObject * s, PyObject * args); | |
21 | static PyObject * centeredWindow(PyObject * s, PyObject * args); | |
22 | static snackWidget * checkboxWidget(PyObject * s, PyObject * args); | |
23 | static PyObject * choiceWindow(PyObject * s, PyObject * args); | |
24 | static snackWidget * entryWidget(PyObject * s, PyObject * args); | |
75fb0452 | 25 | static PyObject * drawRootText(PyObject * s, PyObject * args); |
80d03bd8 | 26 | static snackForm * formCreate(PyObject * s, PyObject * args); |
27 | static snackGrid * gridCreate(PyObject * s, PyObject * args); | |
28 | static PyObject * gridWrappedWindow(PyObject * s, PyObject * args); | |
29 | static PyObject * finishScreen(PyObject * s, PyObject * args); | |
30 | static PyObject * initScreen(PyObject * s, PyObject * args); | |
31 | static snackWidget * labelWidget(PyObject * s, PyObject * args); | |
6e25d997 | 32 | static snackWidget * listboxWidget(PyObject * s, PyObject * args); |
80d03bd8 | 33 | static PyObject * messageWindow(PyObject * s, PyObject * args); |
34 | static PyObject * openWindow(PyObject * s, PyObject * args); | |
75fb0452 | 35 | static PyObject * popHelpLine(PyObject * s, PyObject * args); |
80d03bd8 | 36 | static PyObject * popWindow(PyObject * s, PyObject * args); |
75fb0452 | 37 | static PyObject * pushHelpLine(PyObject * s, PyObject * args); |
80d03bd8 | 38 | static snackWidget * radioButtonWidget(PyObject * s, PyObject * args); |
39 | static PyObject * refreshScreen(PyObject * s, PyObject * args); | |
06cf519e | 40 | static PyObject * scaleWidget(PyObject * s, PyObject * args); |
41 | static PyObject * scaleSet(snackWidget * s, PyObject * args); | |
9f0ab2ac | 42 | static PyObject * screenSize(PyObject * s, PyObject * args); |
06cf519e | 43 | static PyObject * setSuspendCallback(PyObject * s, PyObject * args); |
6e25d997 | 44 | static PyObject * reflowText(PyObject * s, PyObject * args); |
45 | static snackWidget * textWidget(PyObject * s, PyObject * args); | |
80d03bd8 | 46 | static PyObject * ternaryWindow(PyObject * s, PyObject * args); |
f06c5a99 | 47 | static snackWidget * checkboxTreeWidget(PyObject * s, PyObject * args); |
80d03bd8 | 48 | |
49 | static PyMethodDef snackModuleMethods[] = { | |
50 | { "button", (PyCFunction) buttonWidget, METH_VARARGS, NULL }, | |
51 | { "checkbox", (PyCFunction) checkboxWidget, METH_VARARGS, NULL }, | |
52 | { "choice", choiceWindow, METH_VARARGS, NULL }, | |
53 | { "centeredwindow", centeredWindow, METH_VARARGS, NULL }, | |
75fb0452 | 54 | { "drawroottext", drawRootText, METH_VARARGS, NULL }, |
80d03bd8 | 55 | { "entry", (PyCFunction) entryWidget, METH_VARARGS, NULL }, |
56 | { "finish", finishScreen, METH_VARARGS, NULL }, | |
57 | { "form", (PyCFunction) formCreate, METH_VARARGS, NULL }, | |
58 | { "grid", (PyCFunction) gridCreate, METH_VARARGS, NULL }, | |
59 | { "gridwrappedwindow", gridWrappedWindow, METH_VARARGS, NULL }, | |
60 | { "init", initScreen, METH_VARARGS, NULL }, | |
61 | { "label", (PyCFunction) labelWidget, METH_VARARGS, NULL }, | |
6e25d997 | 62 | { "listbox", (PyCFunction) listboxWidget, METH_VARARGS, NULL }, |
80d03bd8 | 63 | { "message", messageWindow, METH_VARARGS, NULL }, |
64 | { "openwindow", openWindow, METH_VARARGS, NULL }, | |
75fb0452 | 65 | { "pophelpline", popHelpLine, METH_VARARGS, NULL }, |
80d03bd8 | 66 | { "popwindow", popWindow, METH_VARARGS, NULL }, |
75fb0452 | 67 | { "pushhelpline", pushHelpLine, METH_VARARGS, NULL }, |
80d03bd8 | 68 | { "radiobutton", (PyCFunction) radioButtonWidget, METH_VARARGS, NULL }, |
6e25d997 | 69 | { "reflow", (PyCFunction) reflowText, METH_VARARGS, NULL }, |
80d03bd8 | 70 | { "refresh", refreshScreen, METH_VARARGS, NULL }, |
06cf519e | 71 | { "scale", scaleWidget, METH_VARARGS, NULL }, |
9f0ab2ac | 72 | { "size", screenSize, METH_VARARGS, NULL }, |
06cf519e | 73 | { "suspendcallback", setSuspendCallback, METH_VARARGS, NULL }, |
80d03bd8 | 74 | { "ternary", ternaryWindow, METH_VARARGS, NULL }, |
6e25d997 | 75 | { "textbox", (PyCFunction) textWidget, METH_VARARGS, NULL }, |
f06c5a99 | 76 | { "checkboxtree", (PyCFunction) checkboxTreeWidget, METH_VARARGS, NULL }, |
80d03bd8 | 77 | { NULL } |
78 | } ; | |
79 | ||
80 | struct snackGrid_s { | |
81 | PyObject_HEAD; | |
82 | newtGrid grid; | |
83 | } ; | |
84 | ||
85 | static PyObject * gridGetAttr(PyObject * s, char * name); | |
86 | static PyObject * gridPlace(snackGrid * s, PyObject * args); | |
87 | static PyObject * gridSetField(snackGrid * s, PyObject * args); | |
88 | ||
89 | static PyMethodDef gridMethods[] = { | |
90 | { "place", (PyCFunction) gridPlace, METH_VARARGS, NULL }, | |
91 | { "setfield", (PyCFunction) gridSetField, METH_VARARGS, NULL }, | |
92 | { NULL } | |
93 | }; | |
94 | ||
95 | static PyTypeObject snackGridType = { | |
96 | PyObject_HEAD_INIT(&PyType_Type) | |
97 | 0, /* ob_size */ | |
98 | "snackgrid", /* tp_name */ | |
99 | sizeof(snackGrid), /* tp_size */ | |
100 | 0, /* tp_itemsize */ | |
101 | emptyDestructor, /* tp_dealloc */ | |
102 | 0, /* tp_print */ | |
103 | gridGetAttr, /* tp_getattr */ | |
104 | 0, /* tp_setattr */ | |
105 | 0, /* tp_compare */ | |
106 | 0, /* tp_repr */ | |
107 | 0, /* tp_as_number */ | |
108 | 0, /* tp_as_sequence */ | |
109 | 0, /* tp_as_mapping */ | |
110 | }; | |
111 | ||
112 | struct snackForm_s { | |
113 | PyObject_HEAD; | |
114 | newtComponent fo; | |
115 | } ; | |
116 | ||
117 | static PyObject * formGetAttr(PyObject * s, char * name); | |
118 | static PyObject * formAdd(snackForm * s, PyObject * args); | |
bd38d47e | 119 | static PyObject * formDraw(snackForm * s, PyObject * args); |
80d03bd8 | 120 | static PyObject * formRun(snackForm * s, PyObject * args); |
ea59ae12 | 121 | static PyObject * formHotKey(snackForm * s, PyObject * args); |
80d03bd8 | 122 | |
123 | static PyMethodDef formMethods[] = { | |
124 | { "add", (PyCFunction) formAdd, METH_VARARGS, NULL }, | |
bd38d47e | 125 | { "draw", (PyCFunction) formDraw, METH_VARARGS, NULL }, |
80d03bd8 | 126 | { "run", (PyCFunction) formRun, METH_VARARGS, NULL }, |
ea59ae12 | 127 | { "addhotkey", (PyCFunction) formHotKey, METH_VARARGS, NULL }, |
80d03bd8 | 128 | { NULL } |
129 | }; | |
130 | ||
131 | static PyTypeObject snackFormType = { | |
132 | PyObject_HEAD_INIT(&PyType_Type) | |
133 | 0, /* ob_size */ | |
134 | "snackform", /* tp_name */ | |
135 | sizeof(snackForm), /* tp_size */ | |
136 | 0, /* tp_itemsize */ | |
137 | emptyDestructor, /* tp_dealloc */ | |
138 | 0, /* tp_print */ | |
139 | formGetAttr, /* tp_getattr */ | |
140 | 0, /* tp_setattr */ | |
141 | 0, /* tp_compare */ | |
142 | 0, /* tp_repr */ | |
143 | 0, /* tp_as_number */ | |
144 | 0, /* tp_as_sequence */ | |
145 | 0, /* tp_as_mapping */ | |
146 | }; | |
147 | ||
148 | struct snackWidget_s { | |
149 | PyObject_HEAD; | |
150 | newtComponent co; | |
151 | char achar; | |
152 | void * apointer; | |
6e25d997 | 153 | int anint; |
80d03bd8 | 154 | } ; |
155 | ||
6a703ca8 | 156 | static PyObject * widgetAddCallback(snackWidget * s, PyObject * args); |
80d03bd8 | 157 | static PyObject * widgetGetAttr(PyObject * s, char * name); |
158 | static PyObject * widgetEntrySetValue(snackWidget * s, PyObject * args); | |
522d3500 | 159 | static PyObject * widgetLabelText(snackWidget * s, PyObject * args); |
6e25d997 | 160 | static PyObject * widgetListboxSetW(snackWidget * s, PyObject * args); |
161 | static PyObject * widgetListboxAdd(snackWidget * s, PyObject * args); | |
a507b3ec | 162 | static PyObject * widgetListboxIns(snackWidget * s, PyObject * args); |
163 | static PyObject * widgetListboxDel(snackWidget * s, PyObject * args); | |
6e25d997 | 164 | static PyObject * widgetListboxGet(snackWidget * s, PyObject * args); |
36f926a6 | 165 | static PyObject * widgetTextboxText(snackWidget * s, PyObject * args); |
75d40644 | 166 | static PyObject * widgetCheckboxTreeAddItem(snackWidget * s, PyObject * args); |
5a6729cf | 167 | static PyObject * widgetCheckboxTreeGetSel(snackWidget * s, |
168 | PyObject * args); | |
80d03bd8 | 169 | |
170 | static PyMethodDef widgetMethods[] = { | |
6a703ca8 | 171 | { "setCallback", (PyCFunction) widgetAddCallback, METH_VARARGS, NULL }, |
522d3500 | 172 | { "labelText", (PyCFunction) widgetLabelText, METH_VARARGS, NULL }, |
36f926a6 | 173 | { "textboxText", (PyCFunction) widgetTextboxText, METH_VARARGS, NULL }, |
80d03bd8 | 174 | { "entrySetValue", (PyCFunction) widgetEntrySetValue, METH_VARARGS, NULL }, |
6e25d997 | 175 | { "listboxAddItem", (PyCFunction) widgetListboxAdd, METH_VARARGS, NULL }, |
a507b3ec | 176 | { "listboxInsertItem", (PyCFunction) widgetListboxIns, METH_VARARGS, NULL }, |
6e25d997 | 177 | { "listboxGetCurrent", (PyCFunction) widgetListboxGet, METH_VARARGS, NULL }, |
178 | { "listboxSetWidth", (PyCFunction) widgetListboxSetW, METH_VARARGS, NULL }, | |
a507b3ec | 179 | { "listboxDeleteItem", (PyCFunction) widgetListboxDel, METH_VARARGS, NULL }, |
06cf519e | 180 | { "scaleSet", (PyCFunction) scaleSet, METH_VARARGS, NULL }, |
75d40644 | 181 | { "checkboxtreeAddItem", (PyCFunction) widgetCheckboxTreeAddItem, |
f06c5a99 | 182 | METH_VARARGS, NULL }, |
183 | { "checkboxtreeGetSelection", (PyCFunction) widgetCheckboxTreeGetSel, | |
184 | METH_VARARGS, NULL }, | |
80d03bd8 | 185 | { NULL } |
186 | }; | |
187 | ||
188 | static PyTypeObject snackWidgetType = { | |
189 | PyObject_HEAD_INIT(&PyType_Type) | |
190 | 0, /* ob_size */ | |
191 | "snackwidget", /* tp_name */ | |
192 | sizeof(snackWidget), /* tp_size */ | |
193 | 0, /* tp_itemsize */ | |
194 | emptyDestructor, /* tp_dealloc */ | |
195 | 0, /* tp_print */ | |
196 | widgetGetAttr, /* tp_getattr */ | |
197 | 0, /* tp_setattr */ | |
198 | 0, /* tp_compare */ | |
199 | 0, /* tp_repr */ | |
200 | 0, /* tp_as_number */ | |
201 | 0, /* tp_as_sequence */ | |
202 | 0, /* tp_as_mapping */ | |
203 | }; | |
204 | ||
6a703ca8 | 205 | static void callbackEntry(newtComponent co, void * arg) { |
206 | PyEval_CallObject(arg, arg); | |
207 | } | |
208 | ||
80d03bd8 | 209 | static PyObject * initScreen(PyObject * s, PyObject * args) { |
210 | newtInit(); | |
211 | newtCls(); | |
212 | ||
213 | Py_INCREF(Py_None); | |
214 | return Py_None; | |
215 | } | |
216 | ||
217 | static PyObject * finishScreen(PyObject * s, PyObject * args) { | |
218 | newtFinished(); | |
219 | Py_INCREF(Py_None); | |
220 | return Py_None; | |
221 | } | |
222 | ||
223 | static PyObject * refreshScreen(PyObject * s, PyObject * args) { | |
224 | newtRefresh(); | |
225 | Py_INCREF(Py_None); | |
226 | return Py_None; | |
227 | } | |
228 | ||
06cf519e | 229 | static PyObject * scaleWidget(PyObject * s, PyObject * args) { |
230 | snackWidget * widget; | |
231 | int width, fullAmount; | |
232 | ||
233 | if (!PyArg_ParseTuple(args, "ii", &width, &fullAmount)) return NULL; | |
234 | ||
235 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
236 | widget->co = newtScale(-1, -1, width, fullAmount); | |
237 | ||
238 | return (PyObject *) widget; | |
239 | } | |
240 | ||
241 | static PyObject * scaleSet(snackWidget * s, PyObject * args) { | |
242 | int amount; | |
243 | ||
244 | if (!PyArg_ParseTuple(args, "i", &amount)) return NULL; | |
245 | ||
246 | newtScaleSet(s->co, amount); | |
247 | ||
248 | Py_INCREF(Py_None); | |
249 | return Py_None; | |
250 | } | |
251 | ||
9f0ab2ac | 252 | static PyObject * screenSize(PyObject * s, PyObject * args) { |
253 | int width, height; | |
254 | ||
255 | if (!PyArg_ParseTuple(args, "")) | |
256 | return NULL; | |
257 | ||
258 | newtGetScreenSize(&width, &height); | |
259 | ||
260 | return Py_BuildValue("(ii)", width, height); | |
261 | } | |
262 | ||
06cf519e | 263 | static void suspendCallback(void * data) { |
264 | struct suspendCallbackStruct * scs = data; | |
265 | PyObject * args, * result; | |
266 | ||
1ea8f607 | 267 | args = Py_BuildValue("(O)", scs->data); |
06cf519e | 268 | result = PyEval_CallObject(scs->cb, args); |
1ea8f607 | 269 | |
06cf519e | 270 | Py_DECREF(args); |
1ea8f607 | 271 | Py_XDECREF(result); |
06cf519e | 272 | |
1ea8f607 | 273 | return; |
06cf519e | 274 | } |
275 | ||
276 | static PyObject * setSuspendCallback(PyObject * s, PyObject * args) { | |
277 | static struct suspendCallbackStruct scs; | |
278 | ||
279 | if (!PyArg_ParseTuple(args, "OO", &scs.cb, &scs.data)) | |
280 | return NULL; | |
281 | ||
282 | newtSetSuspendCallback(suspendCallback, &scs); | |
283 | ||
284 | Py_INCREF(Py_None); | |
285 | return Py_None; | |
286 | } | |
287 | ||
75fb0452 | 288 | static PyObject * drawRootText(PyObject * s, PyObject * args) { |
289 | int left, top; | |
290 | char * text; | |
291 | ||
292 | if (!PyArg_ParseTuple(args, "iis", &left, &top, &text)) | |
293 | return NULL; | |
294 | ||
ec65dcfe | 295 | newtDrawRootText(left, top, text); |
75fb0452 | 296 | |
297 | Py_INCREF(Py_None); | |
298 | return Py_None; | |
299 | } | |
300 | ||
301 | static PyObject * popHelpLine(PyObject * s, PyObject * args) { | |
302 | newtPopHelpLine(); | |
303 | Py_INCREF(Py_None); | |
304 | return Py_None; | |
305 | } | |
306 | ||
307 | static PyObject * pushHelpLine(PyObject * s, PyObject * args) { | |
308 | char * text; | |
309 | ||
310 | if (!PyArg_ParseTuple(args, "s", &text)) | |
311 | return NULL; | |
312 | ||
313 | if (!strcmp(text, "*default*")) | |
314 | newtPushHelpLine(NULL); | |
315 | else | |
316 | newtPushHelpLine(text); | |
317 | ||
318 | Py_INCREF(Py_None); | |
319 | return Py_None; | |
320 | } | |
321 | ||
6e25d997 | 322 | static PyObject * reflowText(PyObject * s, PyObject * args) { |
323 | char * text, * new; | |
324 | int width, minus = 5, plus = 5; | |
325 | int realWidth, realHeight; | |
326 | PyObject * tuple; | |
327 | ||
328 | if (!PyArg_ParseTuple(args, "si|ii", &text, &width, &minus, &plus)) | |
329 | return NULL; | |
330 | ||
331 | new = newtReflowText(text, width, minus, plus, &realWidth, &realHeight); | |
332 | ||
333 | tuple = Py_BuildValue("(sii)", new, realWidth, realHeight); | |
334 | free(new); | |
335 | ||
336 | return tuple; | |
337 | } | |
338 | ||
80d03bd8 | 339 | static PyObject * centeredWindow(PyObject * s, PyObject * args) { |
340 | int width, height; | |
341 | char * title; | |
342 | ||
343 | if (!PyArg_ParseTuple(args, "iis", &width, &height, &title)) | |
344 | return NULL; | |
345 | ||
346 | newtCenteredWindow(width, height, title); | |
347 | ||
348 | Py_INCREF(Py_None); | |
349 | return Py_None; | |
350 | } | |
351 | ||
352 | static PyObject * gridWrappedWindow(PyObject * s, PyObject * args) { | |
353 | snackGrid * grid; | |
354 | char * title; | |
355 | ||
356 | if (!PyArg_ParseTuple(args, "O!s", &snackGridType, &grid, &title)) | |
357 | return NULL; | |
358 | ||
359 | newtGridWrappedWindow(grid->grid, title); | |
360 | ||
361 | Py_INCREF(Py_None); | |
362 | return Py_None; | |
363 | } | |
364 | ||
365 | static PyObject * openWindow(PyObject * s, PyObject * args) { | |
366 | int left, top, width, height; | |
367 | char * title; | |
368 | ||
369 | if (!PyArg_ParseTuple(args, "iiiis", &left, &top, &width, &height, &title)) | |
370 | return NULL; | |
371 | ||
372 | newtOpenWindow(left, top, width, height, title); | |
373 | ||
374 | Py_INCREF(Py_None); | |
375 | return Py_None; | |
376 | } | |
377 | ||
378 | static PyObject * popWindow(PyObject * s, PyObject * args) { | |
379 | newtPopWindow(); | |
380 | Py_INCREF(Py_None); | |
381 | return Py_None; | |
382 | } | |
383 | ||
384 | static PyObject * messageWindow(PyObject * s, PyObject * args) { | |
385 | char * title, * text; | |
386 | char * okbutton = "Ok"; | |
387 | ||
388 | if (!PyArg_ParseTuple(args, "ss|s", &title, &text, &okbutton)) | |
389 | return NULL; | |
390 | ||
391 | newtWinMessage(title, okbutton, text); | |
392 | ||
393 | Py_INCREF(Py_None); | |
394 | return Py_None; | |
395 | } | |
396 | ||
397 | static PyObject * choiceWindow(PyObject * s, PyObject * args) { | |
398 | char * title, * text; | |
399 | char * okbutton = "Ok"; | |
400 | char * cancelbutton = "Cancel"; | |
401 | int rc; | |
402 | ||
403 | if (!PyArg_ParseTuple(args, "ss|ss", &title, &text, &okbutton, | |
404 | &cancelbutton)) | |
405 | return NULL; | |
406 | ||
407 | rc = newtWinChoice(title, okbutton, cancelbutton, text); | |
408 | ||
93a3a267 | 409 | return Py_BuildValue("i", rc); |
80d03bd8 | 410 | } |
411 | ||
412 | static PyObject * ternaryWindow(PyObject * s, PyObject * args) { | |
413 | char * title, * text, * button1, * button2, * button3; | |
414 | int rc; | |
415 | ||
416 | if (!PyArg_ParseTuple(args, "sssss", &title, &text, &button1, &button2, | |
417 | &button3)) | |
418 | return NULL; | |
419 | ||
420 | rc = newtWinTernary(title, button1, button2, button3, text); | |
421 | ||
422 | return Py_BuildValue("i", rc); | |
423 | } | |
424 | ||
425 | static snackWidget * buttonWidget(PyObject * s, PyObject * args) { | |
426 | snackWidget * widget; | |
427 | char * label; | |
428 | ||
429 | if (!PyArg_ParseTuple(args, "s", &label)) return NULL; | |
430 | ||
431 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
432 | widget->co = newtButton(-1, -1, label); | |
433 | ||
434 | return widget; | |
435 | } | |
436 | ||
437 | static snackWidget * labelWidget(PyObject * s, PyObject * args) { | |
438 | char * label; | |
439 | snackWidget * widget; | |
440 | ||
441 | if (!PyArg_ParseTuple(args, "s", &label)) return NULL; | |
442 | ||
443 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
444 | widget->co = newtLabel(-1, -1, label); | |
445 | ||
446 | return widget; | |
447 | } | |
448 | ||
522d3500 | 449 | static PyObject * widgetLabelText(snackWidget * s, PyObject * args) { |
450 | char * label; | |
451 | ||
452 | if (!PyArg_ParseTuple(args, "s", &label)) return NULL; | |
453 | ||
454 | newtLabelSetText(s->co, label); | |
455 | ||
456 | Py_INCREF(Py_None); | |
457 | return Py_None; | |
458 | } | |
459 | ||
36f926a6 | 460 | static PyObject * widgetTextboxText(snackWidget * s, PyObject * args) { |
461 | char * text; | |
462 | ||
463 | if (!PyArg_ParseTuple(args, "s", &text)) return NULL; | |
464 | ||
465 | newtTextboxSetText(s->co, text); | |
466 | ||
467 | Py_INCREF(Py_None); | |
468 | return Py_None; | |
469 | } | |
470 | ||
6e25d997 | 471 | static snackWidget * listboxWidget(PyObject * s, PyObject * args) { |
472 | snackWidget * widget; | |
473 | int height; | |
474 | int doScroll = 0, returnExit = 0 ; | |
475 | ||
476 | if (!PyArg_ParseTuple(args, "i|ii", &height, &doScroll, &returnExit)) | |
477 | return NULL; | |
478 | ||
479 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
480 | widget->co = newtListbox(-1, -1, height, | |
49a91bd2 | 481 | (doScroll ? NEWT_FLAG_SCROLL : 0) | |
6e25d997 | 482 | (returnExit ? NEWT_FLAG_RETURNEXIT : 0)); |
a507b3ec | 483 | widget->anint = 1; |
6e25d997 | 484 | |
485 | return widget; | |
486 | } | |
487 | ||
488 | static snackWidget * textWidget(PyObject * s, PyObject * args) { | |
489 | char * text; | |
490 | int width, height; | |
491 | int scrollBar = 0; | |
36f926a6 | 492 | int wrap = 0; |
6e25d997 | 493 | snackWidget * widget; |
494 | ||
36f926a6 | 495 | if (!PyArg_ParseTuple(args, "iis|ii", &width, &height, &text, &scrollBar, &wrap)) |
6e25d997 | 496 | return NULL; |
497 | ||
498 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
499 | widget->co = newtTextbox(-1, -1, width, height, | |
36f926a6 | 500 | (scrollBar ? NEWT_FLAG_SCROLL : 0) | |
501 | (wrap ? NEWT_FLAG_WRAP : 0)); | |
6e25d997 | 502 | newtTextboxSetText(widget->co, text); |
503 | ||
504 | return widget; | |
505 | } | |
506 | ||
80d03bd8 | 507 | static snackWidget * radioButtonWidget(PyObject * s, PyObject * args) { |
508 | snackWidget * widget, * group; | |
509 | char * text; | |
510 | int isOn; | |
511 | ||
512 | if (!PyArg_ParseTuple(args, "sOi", &text, &group, &isOn)) | |
513 | return NULL; | |
514 | ||
515 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
516 | ||
517 | if ((PyObject *) group == Py_None) | |
518 | widget->co = newtRadiobutton(-1, -1, text, isOn, NULL); | |
519 | else | |
520 | widget->co = newtRadiobutton(-1, -1, text, isOn, group->co); | |
521 | ||
522 | return widget; | |
523 | } | |
524 | ||
525 | static snackWidget * checkboxWidget(PyObject * s, PyObject * args) { | |
526 | snackWidget * widget; | |
527 | char * text; | |
528 | int isOn; | |
529 | ||
530 | if (!PyArg_ParseTuple(args, "si", &text, &isOn)) return NULL; | |
531 | ||
532 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
533 | widget->co = newtCheckbox(-1, -1, text, isOn ? '*' : ' ', NULL, | |
534 | &widget->achar); | |
535 | ||
536 | return widget; | |
537 | } | |
538 | ||
539 | static snackWidget * entryWidget(PyObject * s, PyObject * args) { | |
540 | snackWidget * widget; | |
541 | int width; | |
542 | char * initial; | |
5a8da880 | 543 | int isHidden, isScrolled, returnExit; |
80d03bd8 | 544 | |
b85af896 | 545 | if (!PyArg_ParseTuple(args, "isiii", &width, &initial, |
5a8da880 | 546 | &isHidden, &isScrolled, &returnExit)) return NULL; |
80d03bd8 | 547 | |
548 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
549 | widget->co = newtEntry(-1, -1, initial, width, (char **) &widget->apointer, | |
550 | (isHidden ? NEWT_FLAG_HIDDEN : 0) | | |
5a8da880 | 551 | (returnExit ? NEWT_FLAG_RETURNEXIT : 0) | |
49a91bd2 | 552 | (isScrolled ? NEWT_FLAG_SCROLL : 0)); |
80d03bd8 | 553 | |
554 | return widget; | |
555 | } | |
556 | ||
557 | static snackForm * formCreate(PyObject * s, PyObject * args) { | |
558 | snackForm * form; | |
559 | ||
560 | form = PyObject_NEW(snackForm, &snackFormType); | |
561 | form->fo = newtForm(NULL, NULL, 0); | |
562 | ||
563 | return form; | |
564 | } | |
565 | ||
566 | static snackGrid * gridCreate(PyObject * s, PyObject * args) { | |
567 | int rows, cols; | |
568 | snackGrid * grid; | |
569 | ||
570 | if (!PyArg_ParseTuple(args, "ii", &cols, &rows)) return NULL; | |
571 | ||
572 | grid = PyObject_NEW(snackGrid, &snackGridType); | |
573 | grid->grid = newtCreateGrid(cols, rows); | |
574 | ||
575 | return grid; | |
576 | } | |
577 | ||
578 | static PyObject * gridGetAttr(PyObject * s, char * name) { | |
579 | return Py_FindMethod(gridMethods, s, name); | |
580 | } | |
581 | ||
582 | static PyObject * gridPlace(snackGrid * grid, PyObject * args) { | |
583 | int x, y; | |
584 | ||
585 | if (!PyArg_ParseTuple(args, "ii", &x, &y)) return NULL; | |
586 | ||
587 | newtGridPlace(grid->grid, x, y); | |
588 | ||
589 | Py_INCREF(Py_None); | |
590 | return Py_None; | |
591 | } | |
592 | ||
593 | static PyObject * gridSetField(snackGrid * grid, PyObject * args) { | |
594 | snackWidget * w; | |
6e25d997 | 595 | snackGrid * g; |
80d03bd8 | 596 | int x, y; |
597 | int pLeft = 0, pTop = 0, pRight = 0, pBottom = 0; | |
6e25d997 | 598 | int anchorFlags = 0, growFlags = 0; |
80d03bd8 | 599 | |
6e25d997 | 600 | if (!PyArg_ParseTuple(args, "iiO|(iiii)ii", &x, &y, |
601 | &w, &pLeft, &pTop, &pRight, &pBottom, | |
602 | &anchorFlags, &growFlags)) | |
80d03bd8 | 603 | return NULL; |
604 | ||
6e25d997 | 605 | if (w->ob_type == &snackWidgetType) { |
606 | newtGridSetField(grid->grid, x, y, NEWT_GRID_COMPONENT, | |
607 | w->co, pLeft, pTop, pRight, pBottom, anchorFlags, | |
608 | growFlags); | |
609 | } else { | |
610 | g = (snackGrid *) w; | |
611 | newtGridSetField(grid->grid, x, y, NEWT_GRID_SUBGRID, | |
612 | g->grid, pLeft, pTop, pRight, pBottom, anchorFlags, | |
613 | growFlags); | |
614 | } | |
80d03bd8 | 615 | |
616 | Py_INCREF(Py_None); | |
617 | return Py_None; | |
618 | } | |
619 | ||
620 | static PyObject * formGetAttr(PyObject * s, char * name) { | |
621 | return Py_FindMethod(formMethods, s, name); | |
622 | } | |
623 | ||
bd38d47e | 624 | static PyObject * formDraw(snackForm * s, PyObject * args) { |
625 | newtDrawForm(s->fo); | |
626 | ||
627 | Py_INCREF(Py_None); | |
628 | return Py_None; | |
629 | } | |
630 | ||
80d03bd8 | 631 | static PyObject * formAdd(snackForm * s, PyObject * args) { |
632 | snackWidget * w; | |
633 | int size = PyTuple_Size(args), i; | |
634 | ||
635 | if (!size) { | |
636 | /* this is a hack, I should give an error directly */ | |
637 | if (!PyArg_ParseTuple(args, "O!", &snackWidgetType, &w)) | |
638 | return NULL; | |
639 | } | |
640 | ||
641 | for (i = 0; i < size; i++) { | |
642 | w = (snackWidget *) PyTuple_GET_ITEM(args, i); | |
643 | newtFormAddComponent(s->fo, w->co); | |
644 | } | |
645 | ||
646 | Py_INCREF(Py_None); | |
647 | return Py_None; | |
648 | } | |
649 | ||
650 | static PyObject * formRun(snackForm * s, PyObject * args) { | |
ea59ae12 | 651 | struct newtExitStruct result; |
652 | ||
653 | newtFormRun(s->fo, &result); | |
654 | ||
655 | if (result.reason == NEWT_EXIT_HOTKEY) | |
656 | return Py_BuildValue("(si)", "hotkey", result.u.key); | |
657 | else | |
658 | return Py_BuildValue("(si)", "widget", result.u.co); | |
659 | } | |
660 | ||
661 | static PyObject * formHotKey(snackForm * s, PyObject * args) { | |
662 | int key; | |
663 | ||
664 | if (!PyArg_ParseTuple(args, "i", &key)) | |
665 | return NULL; | |
666 | ||
667 | newtFormAddHotKey(s->fo, key); | |
80d03bd8 | 668 | |
669 | Py_INCREF(Py_None); | |
670 | return Py_None; | |
671 | } | |
672 | ||
673 | static PyObject * widgetGetAttr(PyObject * s, char * name) { | |
674 | snackWidget * w = (snackWidget *) s; | |
675 | ||
ea59ae12 | 676 | if (!strcmp(name, "key")) { |
80d03bd8 | 677 | return Py_BuildValue("i", w->co); |
678 | } else if (!strcmp(name, "entryValue")) { | |
679 | return Py_BuildValue("s", w->apointer); | |
680 | } else if (!strcmp(name, "checkboxValue")) { | |
681 | return Py_BuildValue("i", w->achar == ' ' ? 0 : 1); | |
682 | } else if (!strcmp(name, "radioValue")) { | |
683 | return Py_BuildValue("i", newtRadioGetCurrent(w->co)); | |
684 | } | |
685 | ||
686 | return Py_FindMethod(widgetMethods, s, name); | |
687 | } | |
688 | ||
6a703ca8 | 689 | static PyObject * widgetAddCallback(snackWidget * s, PyObject * args) { |
690 | PyObject * object; | |
691 | ||
692 | if (!PyArg_ParseTuple(args, "O", &object)) | |
693 | return NULL; | |
694 | ||
695 | newtComponentAddCallback(s->co, callbackEntry, object); | |
696 | ||
697 | Py_INCREF(Py_None); | |
698 | return Py_None; | |
699 | } | |
700 | ||
80d03bd8 | 701 | static PyObject * widgetEntrySetValue(snackWidget * s, PyObject * args) { |
702 | char * val; | |
703 | ||
704 | if (!PyArg_ParseTuple(args, "s", &val)) | |
705 | return NULL; | |
706 | ||
707 | newtEntrySet(s->co, val, 1); | |
708 | ||
709 | Py_INCREF(Py_None); | |
710 | return Py_None; | |
711 | } | |
712 | ||
6e25d997 | 713 | static PyObject * widgetListboxAdd(snackWidget * s, PyObject * args) { |
714 | char * text; | |
715 | ||
716 | if (!PyArg_ParseTuple(args, "s", &text)) | |
717 | return NULL; | |
718 | ||
719 | newtListboxAddEntry(s->co, text, (void *) s->anint); | |
720 | ||
721 | return PyInt_FromLong(s->anint++); | |
722 | } | |
723 | ||
a507b3ec | 724 | static PyObject * widgetListboxIns(snackWidget * s, PyObject * args) { |
725 | char * text; | |
726 | int key; | |
727 | ||
728 | if (!PyArg_ParseTuple(args, "si", &text, &key)) | |
729 | return NULL; | |
730 | ||
731 | newtListboxInsertEntry(s->co, text, (void *) s->anint, (void *) key); | |
732 | ||
733 | return PyInt_FromLong(s->anint++); | |
734 | } | |
735 | ||
736 | static PyObject * widgetListboxDel(snackWidget * s, PyObject * args) { | |
737 | int key; | |
738 | ||
739 | if (!PyArg_ParseTuple(args, "i", &key)) | |
740 | return NULL; | |
741 | ||
742 | newtListboxDeleteEntry(s->co, (void *) key); | |
743 | ||
744 | Py_INCREF(Py_None); | |
745 | return Py_None; | |
746 | } | |
747 | ||
6e25d997 | 748 | static PyObject * widgetListboxGet(snackWidget * s, PyObject * args) { |
749 | if (!PyArg_ParseTuple(args, "")) | |
750 | return NULL; | |
751 | ||
752 | return PyInt_FromLong((long) newtListboxGetCurrent(s->co)); | |
753 | } | |
754 | ||
755 | static PyObject * widgetListboxSetW(snackWidget * s, PyObject * args) { | |
756 | int width; | |
757 | ||
758 | if (!PyArg_ParseTuple(args, "i", &width)) | |
759 | return NULL; | |
760 | ||
761 | newtListboxSetWidth(s->co, width); | |
762 | ||
763 | Py_INCREF(Py_None); | |
764 | return Py_None; | |
765 | } | |
766 | ||
80d03bd8 | 767 | static void emptyDestructor(PyObject * s) { |
768 | } | |
769 | ||
f06c5a99 | 770 | static snackWidget * checkboxTreeWidget(PyObject * s, PyObject * args) { |
771 | int height; | |
772 | int scrollBar = 0; | |
773 | snackWidget * widget; | |
774 | ||
775 | if (!PyArg_ParseTuple(args, "i|i", &height, &scrollBar)) | |
776 | return NULL; | |
777 | ||
778 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
779 | widget->co = newtCheckboxTree(-1, -1, height, | |
780 | scrollBar ? NEWT_FLAG_SCROLL : 0); | |
781 | ||
782 | widget->anint = 1; | |
783 | ||
784 | return widget; | |
785 | } | |
786 | ||
75d40644 | 787 | static PyObject * widgetCheckboxTreeAddItem(snackWidget * s, PyObject * args) { |
f06c5a99 | 788 | char * text; |
f06c5a99 | 789 | int selected = 0; |
75d40644 | 790 | PyObject * pathList, * o; |
791 | int len; | |
792 | int * path; | |
793 | int i; | |
794 | ||
795 | if (!PyArg_ParseTuple(args, "sOi", &text, &pathList, &selected)) | |
f06c5a99 | 796 | return NULL; |
797 | ||
75d40644 | 798 | len = PyTuple_Size(pathList); |
799 | path = alloca(sizeof(*path) * (len + 1)); | |
800 | for (i = 0; i < len; i++) { | |
801 | o = PyTuple_GetItem(pathList, i); | |
802 | path[i] = PyInt_AsLong(o); | |
75d40644 | 803 | } |
804 | path[len] = NEWT_ARG_LAST; | |
805 | ||
5a6729cf | 806 | newtCheckboxTreeAddArray(s->co, text, (void *) s->anint, |
75d40644 | 807 | selected ? NEWT_FLAG_SELECTED : 0, path); |
f06c5a99 | 808 | |
809 | return PyInt_FromLong(s->anint++); | |
810 | } | |
811 | ||
5a6729cf | 812 | static PyObject * widgetCheckboxTreeGetSel(snackWidget * s, |
f06c5a99 | 813 | PyObject * args) { |
814 | void ** selection; | |
815 | int numselected; | |
5a6729cf | 816 | int i; |
817 | PyObject * sel; | |
f06c5a99 | 818 | |
819 | selection = newtCheckboxTreeGetSelection(s->co, &numselected); | |
820 | ||
5a6729cf | 821 | if (!selection) { |
822 | Py_INCREF(Py_None); | |
823 | return Py_None; | |
f06c5a99 | 824 | } |
825 | ||
5a6729cf | 826 | sel = PyList_New(0); |
827 | for (i = 0; i < numselected; i++) { | |
828 | PyList_Append(sel, PyInt_FromLong((int) selection[i])); | |
5a6729cf | 829 | } |
830 | free(selection); | |
f06c5a99 | 831 | |
5a6729cf | 832 | return sel; |
f06c5a99 | 833 | } |
834 | ||
80d03bd8 | 835 | void init_snack(void) { |
6e25d997 | 836 | PyObject * d, * m; |
837 | ||
838 | m = Py_InitModule("_snack", snackModuleMethods); | |
839 | d = PyModule_GetDict(m); | |
840 | ||
841 | PyDict_SetItemString(d, "ANCHOR_LEFT", PyInt_FromLong(NEWT_ANCHOR_LEFT)); | |
842 | PyDict_SetItemString(d, "ANCHOR_TOP", PyInt_FromLong(NEWT_ANCHOR_TOP)); | |
843 | PyDict_SetItemString(d, "ANCHOR_RIGHT", PyInt_FromLong(NEWT_ANCHOR_RIGHT)); | |
844 | PyDict_SetItemString(d, "ANCHOR_BOTTOM", | |
845 | PyInt_FromLong(NEWT_ANCHOR_BOTTOM)); | |
846 | PyDict_SetItemString(d, "GRID_GROWX", PyInt_FromLong(NEWT_GRID_FLAG_GROWX)); | |
847 | PyDict_SetItemString(d, "GRID_GROWY", PyInt_FromLong(NEWT_GRID_FLAG_GROWY)); | |
ea59ae12 | 848 | |
849 | PyDict_SetItemString(d, "FORM_EXIT_HOTKEY", PyString_FromString("hotkey")); | |
850 | PyDict_SetItemString(d, "FORM_EXIT_WIDGET", PyString_FromString("widget")); | |
851 | ||
852 | PyDict_SetItemString(d, "KEY_F1", PyInt_FromLong(NEWT_KEY_F1)); | |
853 | PyDict_SetItemString(d, "KEY_F2", PyInt_FromLong(NEWT_KEY_F2)); | |
854 | PyDict_SetItemString(d, "KEY_F3", PyInt_FromLong(NEWT_KEY_F3)); | |
855 | PyDict_SetItemString(d, "KEY_F4", PyInt_FromLong(NEWT_KEY_F4)); | |
856 | PyDict_SetItemString(d, "KEY_F5", PyInt_FromLong(NEWT_KEY_F5)); | |
857 | PyDict_SetItemString(d, "KEY_F6", PyInt_FromLong(NEWT_KEY_F6)); | |
858 | PyDict_SetItemString(d, "KEY_F7", PyInt_FromLong(NEWT_KEY_F7)); | |
859 | PyDict_SetItemString(d, "KEY_F8", PyInt_FromLong(NEWT_KEY_F8)); | |
860 | PyDict_SetItemString(d, "KEY_F9", PyInt_FromLong(NEWT_KEY_F9)); | |
861 | PyDict_SetItemString(d, "KEY_F10", PyInt_FromLong(NEWT_KEY_F10)); | |
862 | PyDict_SetItemString(d, "KEY_F11", PyInt_FromLong(NEWT_KEY_F11)); | |
863 | PyDict_SetItemString(d, "KEY_F12", PyInt_FromLong(NEWT_KEY_F12)); | |
80d03bd8 | 864 | } |