]>
Commit | Line | Data |
---|---|---|
d638bf45 | 1 | |
2 | #include "config.h" | |
3 | ||
4 | #ifdef HAVE_ALLOCA_H | |
5 | #include <alloca.h> | |
6 | #endif | |
7 | ||
80d03bd8 | 8 | #include <errno.h> |
9 | #include <fcntl.h> | |
10 | #include <sys/stat.h> | |
11 | #include <sys/time.h> | |
12 | #include <unistd.h> | |
13 | ||
14 | #include "Python.h" | |
15 | #include "newt.h" | |
ce11acc8 | 16 | #include "newt_pr.h" |
80d03bd8 | 17 | |
18 | typedef struct snackWidget_s snackWidget; | |
19 | typedef struct snackGrid_s snackGrid; | |
20 | typedef struct snackForm_s snackForm; | |
21 | ||
5d678658 | 22 | struct callbackStruct { |
06cf519e | 23 | PyObject * cb, * data; |
24 | }; | |
25 | ||
d7e202d6 | 26 | /* Integer to pointer, 64-bit-sane */ |
27 | #define I2P(x) ((void *)(long)(x)) | |
28 | ||
1dfa0cfa | 29 | static struct callbackStruct suspend; |
72b71fa6 | 30 | static struct callbackStruct helpCallback; |
1dfa0cfa | 31 | |
80d03bd8 | 32 | static void emptyDestructor(PyObject * s); |
33 | ||
34 | static snackWidget * buttonWidget(PyObject * s, PyObject * args); | |
f954a19b | 35 | static snackWidget * compactbuttonWidget(PyObject * s, PyObject * args); |
80d03bd8 | 36 | static PyObject * centeredWindow(PyObject * s, PyObject * args); |
37 | static snackWidget * checkboxWidget(PyObject * s, PyObject * args); | |
38 | static PyObject * choiceWindow(PyObject * s, PyObject * args); | |
39 | static snackWidget * entryWidget(PyObject * s, PyObject * args); | |
75fb0452 | 40 | static PyObject * drawRootText(PyObject * s, PyObject * args); |
f1aef30b | 41 | static PyObject * doResume(PyObject * s, PyObject * args); |
42 | static PyObject * doSuspend(PyObject * s, PyObject * args); | |
43 | static PyObject * doSuspend(PyObject * s, PyObject * args); | |
80d03bd8 | 44 | static snackForm * formCreate(PyObject * s, PyObject * args); |
45 | static snackGrid * gridCreate(PyObject * s, PyObject * args); | |
46 | static PyObject * gridWrappedWindow(PyObject * s, PyObject * args); | |
47 | static PyObject * finishScreen(PyObject * s, PyObject * args); | |
48 | static PyObject * initScreen(PyObject * s, PyObject * args); | |
49 | static snackWidget * labelWidget(PyObject * s, PyObject * args); | |
6e25d997 | 50 | static snackWidget * listboxWidget(PyObject * s, PyObject * args); |
80d03bd8 | 51 | static PyObject * messageWindow(PyObject * s, PyObject * args); |
52 | static PyObject * openWindow(PyObject * s, PyObject * args); | |
75fb0452 | 53 | static PyObject * popHelpLine(PyObject * s, PyObject * args); |
80d03bd8 | 54 | static PyObject * popWindow(PyObject * s, PyObject * args); |
75fb0452 | 55 | static PyObject * pushHelpLine(PyObject * s, PyObject * args); |
80d03bd8 | 56 | static snackWidget * radioButtonWidget(PyObject * s, PyObject * args); |
57 | static PyObject * refreshScreen(PyObject * s, PyObject * args); | |
06cf519e | 58 | static PyObject * scaleWidget(PyObject * s, PyObject * args); |
59 | static PyObject * scaleSet(snackWidget * s, PyObject * args); | |
9f0ab2ac | 60 | static PyObject * screenSize(PyObject * s, PyObject * args); |
06cf519e | 61 | static PyObject * setSuspendCallback(PyObject * s, PyObject * args); |
72b71fa6 | 62 | static PyObject * setHelpCallback(PyObject * s, PyObject * args); |
6e25d997 | 63 | static PyObject * reflowText(PyObject * s, PyObject * args); |
64 | static snackWidget * textWidget(PyObject * s, PyObject * args); | |
80d03bd8 | 65 | static PyObject * ternaryWindow(PyObject * s, PyObject * args); |
615bc463 | 66 | static snackWidget * checkboxTreeWidget(PyObject * s, PyObject * args, PyObject * kwargs); |
ce11acc8 | 67 | static PyObject * pywstrlen(PyObject * s, PyObject * args); |
80d03bd8 | 68 | |
69 | static PyMethodDef snackModuleMethods[] = { | |
70 | { "button", (PyCFunction) buttonWidget, METH_VARARGS, NULL }, | |
f954a19b | 71 | { "compactbutton", (PyCFunction) compactbuttonWidget, METH_VARARGS, NULL }, |
80d03bd8 | 72 | { "checkbox", (PyCFunction) checkboxWidget, METH_VARARGS, NULL }, |
73 | { "choice", choiceWindow, METH_VARARGS, NULL }, | |
74 | { "centeredwindow", centeredWindow, METH_VARARGS, NULL }, | |
75fb0452 | 75 | { "drawroottext", drawRootText, METH_VARARGS, NULL }, |
80d03bd8 | 76 | { "entry", (PyCFunction) entryWidget, METH_VARARGS, NULL }, |
77 | { "finish", finishScreen, METH_VARARGS, NULL }, | |
78 | { "form", (PyCFunction) formCreate, METH_VARARGS, NULL }, | |
79 | { "grid", (PyCFunction) gridCreate, METH_VARARGS, NULL }, | |
80 | { "gridwrappedwindow", gridWrappedWindow, METH_VARARGS, NULL }, | |
72b71fa6 | 81 | { "helpcallback", setHelpCallback, METH_VARARGS, NULL }, |
80d03bd8 | 82 | { "init", initScreen, METH_VARARGS, NULL }, |
83 | { "label", (PyCFunction) labelWidget, METH_VARARGS, NULL }, | |
6e25d997 | 84 | { "listbox", (PyCFunction) listboxWidget, METH_VARARGS, NULL }, |
80d03bd8 | 85 | { "message", messageWindow, METH_VARARGS, NULL }, |
86 | { "openwindow", openWindow, METH_VARARGS, NULL }, | |
75fb0452 | 87 | { "pophelpline", popHelpLine, METH_VARARGS, NULL }, |
80d03bd8 | 88 | { "popwindow", popWindow, METH_VARARGS, NULL }, |
75fb0452 | 89 | { "pushhelpline", pushHelpLine, METH_VARARGS, NULL }, |
80d03bd8 | 90 | { "radiobutton", (PyCFunction) radioButtonWidget, METH_VARARGS, NULL }, |
6e25d997 | 91 | { "reflow", (PyCFunction) reflowText, METH_VARARGS, NULL }, |
80d03bd8 | 92 | { "refresh", refreshScreen, METH_VARARGS, NULL }, |
f1aef30b | 93 | { "resume", doResume, METH_VARARGS, NULL }, |
06cf519e | 94 | { "scale", scaleWidget, METH_VARARGS, NULL }, |
9f0ab2ac | 95 | { "size", screenSize, METH_VARARGS, NULL }, |
f1aef30b | 96 | { "suspend", doSuspend, METH_VARARGS, NULL }, |
06cf519e | 97 | { "suspendcallback", setSuspendCallback, METH_VARARGS, NULL }, |
80d03bd8 | 98 | { "ternary", ternaryWindow, METH_VARARGS, NULL }, |
6e25d997 | 99 | { "textbox", (PyCFunction) textWidget, METH_VARARGS, NULL }, |
615bc463 | 100 | { "checkboxtree", (PyCFunction) checkboxTreeWidget, METH_VARARGS | METH_KEYWORDS, NULL }, |
ce11acc8 | 101 | { "wstrlen", (PyCFunction) pywstrlen, METH_VARARGS | METH_KEYWORDS, NULL }, |
80d03bd8 | 102 | { NULL } |
103 | } ; | |
104 | ||
105 | struct snackGrid_s { | |
cf4cf78f | 106 | PyObject_HEAD |
80d03bd8 | 107 | newtGrid grid; |
108 | } ; | |
109 | ||
110 | static PyObject * gridGetAttr(PyObject * s, char * name); | |
111 | static PyObject * gridPlace(snackGrid * s, PyObject * args); | |
112 | static PyObject * gridSetField(snackGrid * s, PyObject * args); | |
113 | ||
114 | static PyMethodDef gridMethods[] = { | |
115 | { "place", (PyCFunction) gridPlace, METH_VARARGS, NULL }, | |
116 | { "setfield", (PyCFunction) gridSetField, METH_VARARGS, NULL }, | |
117 | { NULL } | |
118 | }; | |
119 | ||
120 | static PyTypeObject snackGridType = { | |
121 | PyObject_HEAD_INIT(&PyType_Type) | |
122 | 0, /* ob_size */ | |
123 | "snackgrid", /* tp_name */ | |
124 | sizeof(snackGrid), /* tp_size */ | |
125 | 0, /* tp_itemsize */ | |
6f481af2 | 126 | emptyDestructor, /* tp_dealloc */ |
80d03bd8 | 127 | 0, /* tp_print */ |
6f481af2 | 128 | gridGetAttr, /* tp_getattr */ |
80d03bd8 | 129 | 0, /* tp_setattr */ |
130 | 0, /* tp_compare */ | |
131 | 0, /* tp_repr */ | |
132 | 0, /* tp_as_number */ | |
133 | 0, /* tp_as_sequence */ | |
6f481af2 | 134 | 0, /* tp_as_mapping */ |
80d03bd8 | 135 | }; |
136 | ||
137 | struct snackForm_s { | |
cf4cf78f | 138 | PyObject_HEAD |
80d03bd8 | 139 | newtComponent fo; |
140 | } ; | |
141 | ||
142 | static PyObject * formGetAttr(PyObject * s, char * name); | |
143 | static PyObject * formAdd(snackForm * s, PyObject * args); | |
bd38d47e | 144 | static PyObject * formDraw(snackForm * s, PyObject * args); |
80d03bd8 | 145 | static PyObject * formRun(snackForm * s, PyObject * args); |
ea59ae12 | 146 | static PyObject * formHotKey(snackForm * s, PyObject * args); |
5d678658 | 147 | static PyObject * formSetCurrent(snackForm * form, PyObject * args); |
73390860 | 148 | static PyObject * formSetTimer(snackForm * form, PyObject * args); |
d7e202d6 | 149 | static PyObject * formWatchFD(snackForm * form, PyObject * args); |
80d03bd8 | 150 | |
151 | static PyMethodDef formMethods[] = { | |
152 | { "add", (PyCFunction) formAdd, METH_VARARGS, NULL }, | |
bd38d47e | 153 | { "draw", (PyCFunction) formDraw, METH_VARARGS, NULL }, |
80d03bd8 | 154 | { "run", (PyCFunction) formRun, METH_VARARGS, NULL }, |
ea59ae12 | 155 | { "addhotkey", (PyCFunction) formHotKey, METH_VARARGS, NULL }, |
5d678658 | 156 | { "setcurrent", (PyCFunction) formSetCurrent, METH_VARARGS, NULL }, |
73390860 | 157 | { "settimer", (PyCFunction) formSetTimer, METH_VARARGS, NULL }, |
d7e202d6 | 158 | { "watchfd", (PyCFunction) formWatchFD, METH_VARARGS, NULL }, |
80d03bd8 | 159 | { NULL } |
160 | }; | |
161 | ||
162 | static PyTypeObject snackFormType = { | |
163 | PyObject_HEAD_INIT(&PyType_Type) | |
164 | 0, /* ob_size */ | |
165 | "snackform", /* tp_name */ | |
166 | sizeof(snackForm), /* tp_size */ | |
167 | 0, /* tp_itemsize */ | |
6f481af2 | 168 | emptyDestructor, /* tp_dealloc */ |
80d03bd8 | 169 | 0, /* tp_print */ |
6f481af2 | 170 | formGetAttr, /* tp_getattr */ |
80d03bd8 | 171 | 0, /* tp_setattr */ |
172 | 0, /* tp_compare */ | |
173 | 0, /* tp_repr */ | |
174 | 0, /* tp_as_number */ | |
175 | 0, /* tp_as_sequence */ | |
6f481af2 | 176 | 0, /* tp_as_mapping */ |
80d03bd8 | 177 | }; |
178 | ||
179 | struct snackWidget_s { | |
cf4cf78f | 180 | PyObject_HEAD |
80d03bd8 | 181 | newtComponent co; |
182 | char achar; | |
183 | void * apointer; | |
6e25d997 | 184 | int anint; |
5d678658 | 185 | struct callbackStruct scs; |
80d03bd8 | 186 | } ; |
187 | ||
6a703ca8 | 188 | static PyObject * widgetAddCallback(snackWidget * s, PyObject * args); |
80d03bd8 | 189 | static PyObject * widgetGetAttr(PyObject * s, char * name); |
1dfa0cfa | 190 | static void widgetDestructor(PyObject * s); |
80d03bd8 | 191 | static PyObject * widgetEntrySetValue(snackWidget * s, PyObject * args); |
522d3500 | 192 | static PyObject * widgetLabelText(snackWidget * s, PyObject * args); |
6e25d997 | 193 | static PyObject * widgetListboxSetW(snackWidget * s, PyObject * args); |
194 | static PyObject * widgetListboxAdd(snackWidget * s, PyObject * args); | |
a507b3ec | 195 | static PyObject * widgetListboxIns(snackWidget * s, PyObject * args); |
196 | static PyObject * widgetListboxDel(snackWidget * s, PyObject * args); | |
6e25d997 | 197 | static PyObject * widgetListboxGet(snackWidget * s, PyObject * args); |
99438c19 | 198 | static PyObject * widgetListboxSet(snackWidget * s, PyObject * args); |
50c9056c | 199 | static PyObject * widgetListboxClear(snackWidget * s, PyObject * args); |
36f926a6 | 200 | static PyObject * widgetTextboxText(snackWidget * s, PyObject * args); |
75d40644 | 201 | static PyObject * widgetCheckboxTreeAddItem(snackWidget * s, PyObject * args); |
5d678658 | 202 | static PyObject * widgetCheckboxTreeGetSel(snackWidget * s, PyObject * args); |
2b6641b1 | 203 | static PyObject * widgetCheckboxTreeGetCur(snackWidget * s, PyObject * args); |
c895490d | 204 | static PyObject * widgetCheckboxTreeSetEntry(snackWidget * s, PyObject * args); |
d8c97d41 | 205 | static PyObject * widgetCheckboxTreeSetWidth(snackWidget * s, PyObject * args); |
5160fc22 | 206 | static PyObject * widgetCheckboxTreeSetCurrent(snackWidget * s, PyObject * args); |
c895490d | 207 | static PyObject * widgetCheckboxTreeSetEntryValue(snackWidget * s, PyObject * args); |
208 | static PyObject * widgetCheckboxTreeGetEntryValue(snackWidget * s, PyObject * args); | |
5d678658 | 209 | static PyObject * widgetEntrySetFlags(snackWidget * s, PyObject * args); |
c573c71b | 210 | static PyObject * widgetCheckboxSetFlags(snackWidget * s, PyObject * args); |
870ff9eb | 211 | static PyObject * widgetCheckboxSetValue(snackWidget * s, PyObject * args); |
80d03bd8 | 212 | |
213 | static PyMethodDef widgetMethods[] = { | |
6a703ca8 | 214 | { "setCallback", (PyCFunction) widgetAddCallback, METH_VARARGS, NULL }, |
522d3500 | 215 | { "labelText", (PyCFunction) widgetLabelText, METH_VARARGS, NULL }, |
36f926a6 | 216 | { "textboxText", (PyCFunction) widgetTextboxText, METH_VARARGS, NULL }, |
80d03bd8 | 217 | { "entrySetValue", (PyCFunction) widgetEntrySetValue, METH_VARARGS, NULL }, |
6e25d997 | 218 | { "listboxAddItem", (PyCFunction) widgetListboxAdd, METH_VARARGS, NULL }, |
a507b3ec | 219 | { "listboxInsertItem", (PyCFunction) widgetListboxIns, METH_VARARGS, NULL }, |
6e25d997 | 220 | { "listboxGetCurrent", (PyCFunction) widgetListboxGet, METH_VARARGS, NULL }, |
99438c19 | 221 | { "listboxSetCurrent", (PyCFunction) widgetListboxSet, METH_VARARGS, NULL }, |
6e25d997 | 222 | { "listboxSetWidth", (PyCFunction) widgetListboxSetW, METH_VARARGS, NULL }, |
a507b3ec | 223 | { "listboxDeleteItem", (PyCFunction) widgetListboxDel, METH_VARARGS, NULL }, |
50c9056c | 224 | { "listboxClear", (PyCFunction) widgetListboxClear, METH_VARARGS, NULL }, |
06cf519e | 225 | { "scaleSet", (PyCFunction) scaleSet, METH_VARARGS, NULL }, |
75d40644 | 226 | { "checkboxtreeAddItem", (PyCFunction) widgetCheckboxTreeAddItem, |
f06c5a99 | 227 | METH_VARARGS, NULL }, |
2b6641b1 | 228 | { "checkboxtreeGetCurrent", (PyCFunction) widgetCheckboxTreeGetCur, |
229 | METH_VARARGS, NULL }, | |
c895490d | 230 | { "checkboxtreeGetEntryValue", (PyCFunction) widgetCheckboxTreeGetEntryValue, |
231 | METH_VARARGS, NULL }, | |
232 | { "checkboxtreeSetEntry", (PyCFunction) widgetCheckboxTreeSetEntry, | |
233 | METH_VARARGS, NULL }, | |
d8c97d41 | 234 | { "checkboxtreeSetWidth", (PyCFunction) widgetCheckboxTreeSetWidth, METH_VARARGS, NULL }, |
5160fc22 | 235 | { "checkboxtreeSetCurrent", (PyCFunction) widgetCheckboxTreeSetCurrent, |
236 | METH_VARARGS, NULL }, | |
c895490d | 237 | { "checkboxtreeSetEntryValue", (PyCFunction) widgetCheckboxTreeSetEntryValue, |
238 | METH_VARARGS, NULL }, | |
f06c5a99 | 239 | { "checkboxtreeGetSelection", (PyCFunction) widgetCheckboxTreeGetSel, |
615bc463 | 240 | METH_VARARGS, NULL }, |
5d678658 | 241 | { "entrySetFlags", (PyCFunction) widgetEntrySetFlags, METH_VARARGS, NULL }, |
c573c71b | 242 | { "checkboxSetFlags", (PyCFunction) widgetCheckboxSetFlags, METH_VARARGS, NULL }, |
870ff9eb | 243 | { "checkboxSetValue", (PyCFunction) widgetCheckboxSetValue, METH_VARARGS, NULL }, |
80d03bd8 | 244 | { NULL } |
245 | }; | |
246 | ||
247 | static PyTypeObject snackWidgetType = { | |
248 | PyObject_HEAD_INIT(&PyType_Type) | |
249 | 0, /* ob_size */ | |
250 | "snackwidget", /* tp_name */ | |
251 | sizeof(snackWidget), /* tp_size */ | |
252 | 0, /* tp_itemsize */ | |
6f481af2 | 253 | widgetDestructor, /* tp_dealloc */ |
80d03bd8 | 254 | 0, /* tp_print */ |
6f481af2 | 255 | widgetGetAttr, /* tp_getattr */ |
80d03bd8 | 256 | 0, /* tp_setattr */ |
257 | 0, /* tp_compare */ | |
258 | 0, /* tp_repr */ | |
259 | 0, /* tp_as_number */ | |
260 | 0, /* tp_as_sequence */ | |
6f481af2 | 261 | 0, /* tp_as_mapping */ |
80d03bd8 | 262 | }; |
263 | ||
1dfa0cfa | 264 | static snackWidget * snackWidgetNew (void) { |
265 | snackWidget * widget; | |
266 | ||
267 | widget = PyObject_NEW(snackWidget, &snackWidgetType); | |
268 | ||
269 | widget->scs.cb = NULL; | |
270 | widget->scs.data = NULL; | |
271 | ||
272 | return widget; | |
273 | } | |
274 | ||
80d03bd8 | 275 | static PyObject * initScreen(PyObject * s, PyObject * args) { |
1dfa0cfa | 276 | suspend.cb = NULL; |
277 | suspend.data = NULL; | |
278 | ||
80d03bd8 | 279 | newtInit(); |
280 | newtCls(); | |
281 | ||
282 | Py_INCREF(Py_None); | |
283 | return Py_None; | |
284 | } | |
285 | ||
286 | static PyObject * finishScreen(PyObject * s, PyObject * args) { | |
dd66cfb7 | 287 | Py_XDECREF (suspend.cb); |
288 | Py_XDECREF (suspend.data); | |
289 | ||
80d03bd8 | 290 | newtFinished(); |
291 | Py_INCREF(Py_None); | |
292 | return Py_None; | |
293 | } | |
294 | ||
295 | static PyObject * refreshScreen(PyObject * s, PyObject * args) { | |
296 | newtRefresh(); | |
297 | Py_INCREF(Py_None); | |
298 | return Py_None; | |
299 | } | |
300 | ||
06cf519e | 301 | static PyObject * scaleWidget(PyObject * s, PyObject * args) { |
302 | snackWidget * widget; | |
303 | int width, fullAmount; | |
304 | ||
305 | if (!PyArg_ParseTuple(args, "ii", &width, &fullAmount)) return NULL; | |
306 | ||
1dfa0cfa | 307 | widget = snackWidgetNew (); |
06cf519e | 308 | widget->co = newtScale(-1, -1, width, fullAmount); |
309 | ||
310 | return (PyObject *) widget; | |
311 | } | |
312 | ||
313 | static PyObject * scaleSet(snackWidget * s, PyObject * args) { | |
314 | int amount; | |
315 | ||
316 | if (!PyArg_ParseTuple(args, "i", &amount)) return NULL; | |
317 | ||
318 | newtScaleSet(s->co, amount); | |
319 | ||
320 | Py_INCREF(Py_None); | |
321 | return Py_None; | |
322 | } | |
323 | ||
9f0ab2ac | 324 | static PyObject * screenSize(PyObject * s, PyObject * args) { |
325 | int width, height; | |
326 | ||
327 | if (!PyArg_ParseTuple(args, "")) | |
6f481af2 | 328 | return NULL; |
9f0ab2ac | 329 | |
330 | newtGetScreenSize(&width, &height); | |
331 | ||
332 | return Py_BuildValue("(ii)", width, height); | |
333 | } | |
334 | ||
72b71fa6 | 335 | static void helpCallbackMarshall(newtComponent co, void * data) { |
336 | PyObject * args, * result; | |
337 | ||
338 | args = Py_BuildValue("(O)", data); | |
339 | result = PyEval_CallObject(helpCallback.cb, args); | |
340 | Py_DECREF (args); | |
341 | Py_XDECREF(result); | |
342 | ||
343 | return; | |
344 | } | |
345 | ||
5d678658 | 346 | static void suspendCallbackMarshall(void * data) { |
347 | struct callbackStruct * scs = data; | |
348 | PyObject * args, * result; | |
349 | ||
1dfa0cfa | 350 | if (scs->data) { |
6f481af2 | 351 | args = Py_BuildValue("(O)", scs->data); |
352 | result = PyEval_CallObject(scs->cb, args); | |
353 | Py_DECREF (args); | |
1dfa0cfa | 354 | } else |
6f481af2 | 355 | result = PyEval_CallObject(scs->cb, NULL); |
1dfa0cfa | 356 | |
357 | if (!result) { | |
6f481af2 | 358 | PyErr_Print(); |
359 | PyErr_Clear(); | |
1dfa0cfa | 360 | } |
5d678658 | 361 | |
5d678658 | 362 | Py_XDECREF(result); |
363 | ||
364 | return; | |
365 | } | |
366 | ||
367 | static void callbackMarshall(newtComponent co, void * data) { | |
368 | struct callbackStruct * scs = data; | |
06cf519e | 369 | PyObject * args, * result; |
370 | ||
1dfa0cfa | 371 | if (scs->data) { |
6f481af2 | 372 | args = Py_BuildValue("(O)", scs->data); |
373 | result = PyEval_CallObject(scs->cb, args); | |
374 | Py_DECREF (args); | |
1dfa0cfa | 375 | } else |
6f481af2 | 376 | result = PyEval_CallObject(scs->cb, NULL); |
1ea8f607 | 377 | |
5d678658 | 378 | if (!result) { |
6f481af2 | 379 | PyErr_Print(); |
380 | PyErr_Clear(); | |
5d678658 | 381 | } |
382 | ||
1ea8f607 | 383 | Py_XDECREF(result); |
06cf519e | 384 | |
1ea8f607 | 385 | return; |
06cf519e | 386 | } |
387 | ||
388 | static PyObject * setSuspendCallback(PyObject * s, PyObject * args) { | |
1dfa0cfa | 389 | if (!PyArg_ParseTuple(args, "O|O", &suspend.cb, &suspend.data)) |
6f481af2 | 390 | return NULL; |
06cf519e | 391 | |
1dfa0cfa | 392 | Py_INCREF (suspend.cb); |
393 | Py_XINCREF (suspend.data); | |
394 | ||
395 | newtSetSuspendCallback(suspendCallbackMarshall, &suspend); | |
06cf519e | 396 | |
397 | Py_INCREF(Py_None); | |
398 | return Py_None; | |
399 | } | |
400 | ||
72b71fa6 | 401 | static PyObject * setHelpCallback(PyObject * s, PyObject * args) { |
402 | if (!PyArg_ParseTuple(args, "O", &helpCallback.cb)) | |
6f481af2 | 403 | return NULL; |
72b71fa6 | 404 | |
405 | /*if (helpCallback.cb) { | |
6f481af2 | 406 | Py_DECREF (helpCallback.cb); |
72b71fa6 | 407 | }*/ |
408 | ||
409 | Py_INCREF (helpCallback.cb); | |
410 | ||
411 | newtSetHelpCallback(helpCallbackMarshall); | |
412 | ||
413 | Py_INCREF(Py_None); | |
414 | return Py_None; | |
415 | } | |
416 | ||
75fb0452 | 417 | static PyObject * drawRootText(PyObject * s, PyObject * args) { |
418 | int left, top; | |
419 | char * text; | |
420 | ||
421 | if (!PyArg_ParseTuple(args, "iis", &left, &top, &text)) | |
6f481af2 | 422 | return NULL; |
75fb0452 | 423 | |
ec65dcfe | 424 | newtDrawRootText(left, top, text); |
75fb0452 | 425 | |
426 | Py_INCREF(Py_None); | |
427 | return Py_None; | |
428 | } | |
429 | ||
f1aef30b | 430 | static PyObject * doSuspend(PyObject * s, PyObject * args) { |
431 | newtSuspend(); | |
432 | ||
433 | Py_INCREF(Py_None); | |
434 | return Py_None; | |
435 | } | |
436 | ||
437 | static PyObject * doResume(PyObject * s, PyObject * args) { | |
438 | newtResume(); | |
439 | ||
440 | Py_INCREF(Py_None); | |
441 | return Py_None; | |
442 | } | |
443 | ||
75fb0452 | 444 | static PyObject * popHelpLine(PyObject * s, PyObject * args) { |
445 | newtPopHelpLine(); | |
446 | Py_INCREF(Py_None); | |
447 | return Py_None; | |
448 | } | |
449 | ||
450 | static PyObject * pushHelpLine(PyObject * s, PyObject * args) { | |
451 | char * text; | |
452 | ||
453 | if (!PyArg_ParseTuple(args, "s", &text)) | |
6f481af2 | 454 | return NULL; |
75fb0452 | 455 | |
456 | if (!strcmp(text, "*default*")) | |
6f481af2 | 457 | newtPushHelpLine(NULL); |
75fb0452 | 458 | else |
6f481af2 | 459 | newtPushHelpLine(text); |
75fb0452 | 460 | |
461 | Py_INCREF(Py_None); | |
462 | return Py_None; | |
463 | } | |
464 | ||
6e25d997 | 465 | static PyObject * reflowText(PyObject * s, PyObject * args) { |
466 | char * text, * new; | |
467 | int width, minus = 5, plus = 5; | |
468 | int realWidth, realHeight; | |
469 | PyObject * tuple; | |
470 | ||
471 | if (!PyArg_ParseTuple(args, "si|ii", &text, &width, &minus, &plus)) | |
6f481af2 | 472 | return NULL; |
6e25d997 | 473 | |
474 | new = newtReflowText(text, width, minus, plus, &realWidth, &realHeight); | |
475 | ||
476 | tuple = Py_BuildValue("(sii)", new, realWidth, realHeight); | |
477 | free(new); | |
478 | ||
479 | return tuple; | |
480 | } | |
481 | ||
80d03bd8 | 482 | static PyObject * centeredWindow(PyObject * s, PyObject * args) { |
483 | int width, height; | |
484 | char * title; | |
485 | ||
486 | if (!PyArg_ParseTuple(args, "iis", &width, &height, &title)) | |
6f481af2 | 487 | return NULL; |
80d03bd8 | 488 | |
489 | newtCenteredWindow(width, height, title); | |
490 | ||
491 | Py_INCREF(Py_None); | |
492 | return Py_None; | |
493 | } | |
494 | ||
495 | static PyObject * gridWrappedWindow(PyObject * s, PyObject * args) { | |
496 | snackGrid * grid; | |
497 | char * title; | |
2b6641b1 | 498 | int x = -1, y = -1; |
80d03bd8 | 499 | |
2b6641b1 | 500 | if (!PyArg_ParseTuple(args, "O!s|ii", &snackGridType, &grid, &title, |
6f481af2 | 501 | &x, &y)) |
502 | return NULL; | |
80d03bd8 | 503 | |
2b6641b1 | 504 | if (y == -1) |
6f481af2 | 505 | newtGridWrappedWindow(grid->grid, title); |
2b6641b1 | 506 | else |
6f481af2 | 507 | newtGridWrappedWindowAt(grid->grid, title, x, y); |
80d03bd8 | 508 | |
509 | Py_INCREF(Py_None); | |
510 | return Py_None; | |
511 | } | |
512 | ||
513 | static PyObject * openWindow(PyObject * s, PyObject * args) { | |
514 | int left, top, width, height; | |
515 | char * title; | |
516 | ||
517 | if (!PyArg_ParseTuple(args, "iiiis", &left, &top, &width, &height, &title)) | |
6f481af2 | 518 | return NULL; |
80d03bd8 | 519 | |
520 | newtOpenWindow(left, top, width, height, title); | |
521 | ||
522 | Py_INCREF(Py_None); | |
523 | return Py_None; | |
524 | } | |
525 | ||
526 | static PyObject * popWindow(PyObject * s, PyObject * args) { | |
527 | newtPopWindow(); | |
528 | Py_INCREF(Py_None); | |
529 | return Py_None; | |
530 | } | |
531 | ||
532 | static PyObject * messageWindow(PyObject * s, PyObject * args) { | |
533 | char * title, * text; | |
534 | char * okbutton = "Ok"; | |
535 | ||
536 | if (!PyArg_ParseTuple(args, "ss|s", &title, &text, &okbutton)) | |
6f481af2 | 537 | return NULL; |
80d03bd8 | 538 | |
539 | newtWinMessage(title, okbutton, text); | |
540 | ||
541 | Py_INCREF(Py_None); | |
542 | return Py_None; | |
543 | } | |
544 | ||
545 | static PyObject * choiceWindow(PyObject * s, PyObject * args) { | |
546 | char * title, * text; | |
547 | char * okbutton = "Ok"; | |
548 | char * cancelbutton = "Cancel"; | |
549 | int rc; | |
550 | ||
551 | if (!PyArg_ParseTuple(args, "ss|ss", &title, &text, &okbutton, | |
6f481af2 | 552 | &cancelbutton)) |
553 | return NULL; | |
80d03bd8 | 554 | |
555 | rc = newtWinChoice(title, okbutton, cancelbutton, text); | |
556 | ||
93a3a267 | 557 | return Py_BuildValue("i", rc); |
80d03bd8 | 558 | } |
559 | ||
560 | static PyObject * ternaryWindow(PyObject * s, PyObject * args) { | |
561 | char * title, * text, * button1, * button2, * button3; | |
562 | int rc; | |
563 | ||
564 | if (!PyArg_ParseTuple(args, "sssss", &title, &text, &button1, &button2, | |
6f481af2 | 565 | &button3)) |
566 | return NULL; | |
80d03bd8 | 567 | |
568 | rc = newtWinTernary(title, button1, button2, button3, text); | |
569 | ||
570 | return Py_BuildValue("i", rc); | |
571 | } | |
572 | ||
573 | static snackWidget * buttonWidget(PyObject * s, PyObject * args) { | |
574 | snackWidget * widget; | |
575 | char * label; | |
576 | ||
577 | if (!PyArg_ParseTuple(args, "s", &label)) return NULL; | |
578 | ||
1dfa0cfa | 579 | widget = snackWidgetNew (); |
80d03bd8 | 580 | widget->co = newtButton(-1, -1, label); |
581 | ||
582 | return widget; | |
583 | } | |
584 | ||
f954a19b | 585 | static snackWidget * compactbuttonWidget(PyObject * s, PyObject * args) { |
586 | snackWidget * widget; | |
587 | char * label; | |
588 | ||
589 | if (!PyArg_ParseTuple(args, "s", &label)) return NULL; | |
590 | ||
591 | widget = snackWidgetNew (); | |
592 | widget->co = newtCompactButton(-1, -1, label); | |
593 | ||
594 | return widget; | |
595 | } | |
596 | ||
80d03bd8 | 597 | static snackWidget * labelWidget(PyObject * s, PyObject * args) { |
598 | char * label; | |
599 | snackWidget * widget; | |
600 | ||
601 | if (!PyArg_ParseTuple(args, "s", &label)) return NULL; | |
602 | ||
1dfa0cfa | 603 | widget = snackWidgetNew (); |
80d03bd8 | 604 | widget->co = newtLabel(-1, -1, label); |
605 | ||
606 | return widget; | |
607 | } | |
608 | ||
522d3500 | 609 | static PyObject * widgetLabelText(snackWidget * s, PyObject * args) { |
610 | char * label; | |
611 | ||
612 | if (!PyArg_ParseTuple(args, "s", &label)) return NULL; | |
613 | ||
614 | newtLabelSetText(s->co, label); | |
615 | ||
616 | Py_INCREF(Py_None); | |
617 | return Py_None; | |
618 | } | |
619 | ||
36f926a6 | 620 | static PyObject * widgetTextboxText(snackWidget * s, PyObject * args) { |
621 | char * text; | |
622 | ||
623 | if (!PyArg_ParseTuple(args, "s", &text)) return NULL; | |
624 | ||
625 | newtTextboxSetText(s->co, text); | |
626 | ||
627 | Py_INCREF(Py_None); | |
628 | return Py_None; | |
629 | } | |
630 | ||
6e25d997 | 631 | static snackWidget * listboxWidget(PyObject * s, PyObject * args) { |
632 | snackWidget * widget; | |
633 | int height; | |
b5c4016a | 634 | int doScroll = 0, returnExit = 0, showCursor = 0 ; |
6e25d997 | 635 | |
b5c4016a | 636 | if (!PyArg_ParseTuple(args, "i|iii", &height, &doScroll, &returnExit, &showCursor)) |
6f481af2 | 637 | return NULL; |
6e25d997 | 638 | |
1dfa0cfa | 639 | widget = snackWidgetNew (); |
6e25d997 | 640 | widget->co = newtListbox(-1, -1, height, |
6f481af2 | 641 | (doScroll ? NEWT_FLAG_SCROLL : 0) | |
642 | (returnExit ? NEWT_FLAG_RETURNEXIT : 0) | | |
643 | (showCursor ? NEWT_FLAG_SHOWCURSOR : 0) | |
644 | ); | |
a507b3ec | 645 | widget->anint = 1; |
6e25d997 | 646 | |
647 | return widget; | |
648 | } | |
649 | ||
650 | static snackWidget * textWidget(PyObject * s, PyObject * args) { | |
651 | char * text; | |
652 | int width, height; | |
653 | int scrollBar = 0; | |
36f926a6 | 654 | int wrap = 0; |
6e25d997 | 655 | snackWidget * widget; |
656 | ||
36f926a6 | 657 | if (!PyArg_ParseTuple(args, "iis|ii", &width, &height, &text, &scrollBar, &wrap)) |
6f481af2 | 658 | return NULL; |
6e25d997 | 659 | |
1dfa0cfa | 660 | widget = snackWidgetNew (); |
6e25d997 | 661 | widget->co = newtTextbox(-1, -1, width, height, |
6f481af2 | 662 | (scrollBar ? NEWT_FLAG_SCROLL : 0) | |
663 | (wrap ? NEWT_FLAG_WRAP : 0)); | |
6e25d997 | 664 | newtTextboxSetText(widget->co, text); |
665 | ||
666 | return widget; | |
667 | } | |
668 | ||
80d03bd8 | 669 | static snackWidget * radioButtonWidget(PyObject * s, PyObject * args) { |
670 | snackWidget * widget, * group; | |
671 | char * text; | |
672 | int isOn; | |
673 | ||
674 | if (!PyArg_ParseTuple(args, "sOi", &text, &group, &isOn)) | |
6f481af2 | 675 | return NULL; |
80d03bd8 | 676 | |
1dfa0cfa | 677 | widget = snackWidgetNew (); |
80d03bd8 | 678 | |
679 | if ((PyObject *) group == Py_None) | |
6f481af2 | 680 | widget->co = newtRadiobutton(-1, -1, text, isOn, NULL); |
80d03bd8 | 681 | else |
6f481af2 | 682 | widget->co = newtRadiobutton(-1, -1, text, isOn, group->co); |
80d03bd8 | 683 | |
684 | return widget; | |
685 | } | |
686 | ||
687 | static snackWidget * checkboxWidget(PyObject * s, PyObject * args) { | |
688 | snackWidget * widget; | |
689 | char * text; | |
690 | int isOn; | |
691 | ||
692 | if (!PyArg_ParseTuple(args, "si", &text, &isOn)) return NULL; | |
693 | ||
1dfa0cfa | 694 | widget = snackWidgetNew (); |
80d03bd8 | 695 | widget->co = newtCheckbox(-1, -1, text, isOn ? '*' : ' ', NULL, |
6f481af2 | 696 | &widget->achar); |
80d03bd8 | 697 | |
698 | return widget; | |
699 | } | |
700 | ||
c573c71b | 701 | static PyObject * widgetCheckboxSetFlags(snackWidget * s, PyObject * args) { |
702 | int flag, sense; | |
703 | ||
704 | if (!PyArg_ParseTuple(args, "ii", &flag, &sense)) return NULL; | |
705 | ||
706 | newtCheckboxSetFlags(s->co, flag, sense); | |
707 | ||
708 | Py_INCREF(Py_None); | |
709 | return Py_None; | |
710 | } | |
711 | ||
870ff9eb | 712 | static PyObject * widgetCheckboxSetValue(snackWidget * s, PyObject * args) { |
713 | char *value; | |
714 | ||
715 | if (!PyArg_ParseTuple(args, "s", &value)) return NULL; | |
716 | ||
717 | newtCheckboxSetValue(s->co, *value); | |
718 | ||
719 | Py_INCREF(Py_None); | |
720 | return Py_None; | |
721 | } | |
c573c71b | 722 | |
80d03bd8 | 723 | static snackWidget * entryWidget(PyObject * s, PyObject * args) { |
724 | snackWidget * widget; | |
725 | int width; | |
726 | char * initial; | |
20fb366a | 727 | int isHidden, isScrolled, returnExit, isPassword; |
80d03bd8 | 728 | |
20fb366a | 729 | if (!PyArg_ParseTuple(args, "isiiii", &width, &initial, |
6f481af2 | 730 | &isHidden, &isPassword, &isScrolled, &returnExit)) return NULL; |
80d03bd8 | 731 | |
1dfa0cfa | 732 | widget = snackWidgetNew (); |
d78245ab | 733 | widget->co = newtEntry(-1, -1, initial, width, |
734 | (const char **) &widget->apointer, | |
6f481af2 | 735 | (isHidden ? NEWT_FLAG_HIDDEN : 0) | |
736 | (isPassword ? NEWT_FLAG_PASSWORD : 0) | | |
737 | (returnExit ? NEWT_FLAG_RETURNEXIT : 0) | | |
738 | (isScrolled ? NEWT_FLAG_SCROLL : 0)); | |
80d03bd8 | 739 | |
740 | return widget; | |
741 | } | |
742 | ||
743 | static snackForm * formCreate(PyObject * s, PyObject * args) { | |
744 | snackForm * form; | |
72b71fa6 | 745 | PyObject * help = Py_None; |
746 | ||
747 | if (!PyArg_ParseTuple(args, "|O", &help)) return NULL; | |
748 | ||
749 | if (help == Py_None) | |
6f481af2 | 750 | help = NULL; |
80d03bd8 | 751 | |
752 | form = PyObject_NEW(snackForm, &snackFormType); | |
72b71fa6 | 753 | form->fo = newtForm(NULL, help, 0); |
80d03bd8 | 754 | |
755 | return form; | |
756 | } | |
757 | ||
758 | static snackGrid * gridCreate(PyObject * s, PyObject * args) { | |
759 | int rows, cols; | |
760 | snackGrid * grid; | |
761 | ||
762 | if (!PyArg_ParseTuple(args, "ii", &cols, &rows)) return NULL; | |
763 | ||
764 | grid = PyObject_NEW(snackGrid, &snackGridType); | |
765 | grid->grid = newtCreateGrid(cols, rows); | |
766 | ||
767 | return grid; | |
768 | } | |
769 | ||
770 | static PyObject * gridGetAttr(PyObject * s, char * name) { | |
771 | return Py_FindMethod(gridMethods, s, name); | |
772 | } | |
773 | ||
774 | static PyObject * gridPlace(snackGrid * grid, PyObject * args) { | |
775 | int x, y; | |
776 | ||
777 | if (!PyArg_ParseTuple(args, "ii", &x, &y)) return NULL; | |
778 | ||
779 | newtGridPlace(grid->grid, x, y); | |
780 | ||
781 | Py_INCREF(Py_None); | |
782 | return Py_None; | |
783 | } | |
784 | ||
785 | static PyObject * gridSetField(snackGrid * grid, PyObject * args) { | |
786 | snackWidget * w; | |
6e25d997 | 787 | snackGrid * g; |
80d03bd8 | 788 | int x, y; |
789 | int pLeft = 0, pTop = 0, pRight = 0, pBottom = 0; | |
6e25d997 | 790 | int anchorFlags = 0, growFlags = 0; |
80d03bd8 | 791 | |
6e25d997 | 792 | if (!PyArg_ParseTuple(args, "iiO|(iiii)ii", &x, &y, |
6f481af2 | 793 | &w, &pLeft, &pTop, &pRight, &pBottom, |
794 | &anchorFlags, &growFlags)) | |
795 | return NULL; | |
80d03bd8 | 796 | |
6e25d997 | 797 | if (w->ob_type == &snackWidgetType) { |
6f481af2 | 798 | newtGridSetField(grid->grid, x, y, NEWT_GRID_COMPONENT, |
799 | w->co, pLeft, pTop, pRight, pBottom, anchorFlags, | |
800 | growFlags); | |
6e25d997 | 801 | } else { |
6f481af2 | 802 | g = (snackGrid *) w; |
803 | newtGridSetField(grid->grid, x, y, NEWT_GRID_SUBGRID, | |
804 | g->grid, pLeft, pTop, pRight, pBottom, anchorFlags, | |
805 | growFlags); | |
6e25d997 | 806 | } |
80d03bd8 | 807 | |
808 | Py_INCREF(Py_None); | |
809 | return Py_None; | |
810 | } | |
811 | ||
812 | static PyObject * formGetAttr(PyObject * s, char * name) { | |
813 | return Py_FindMethod(formMethods, s, name); | |
814 | } | |
815 | ||
bd38d47e | 816 | static PyObject * formDraw(snackForm * s, PyObject * args) { |
817 | newtDrawForm(s->fo); | |
818 | ||
819 | Py_INCREF(Py_None); | |
820 | return Py_None; | |
821 | } | |
822 | ||
80d03bd8 | 823 | static PyObject * formAdd(snackForm * s, PyObject * args) { |
824 | snackWidget * w; | |
825 | int size = PyTuple_Size(args), i; | |
826 | ||
827 | if (!size) { | |
6f481af2 | 828 | /* this is a hack, I should give an error directly */ |
829 | if (!PyArg_ParseTuple(args, "O!", &snackWidgetType, &w)) | |
830 | return NULL; | |
80d03bd8 | 831 | } |
832 | ||
833 | for (i = 0; i < size; i++) { | |
6f481af2 | 834 | w = (snackWidget *) PyTuple_GET_ITEM(args, i); |
835 | newtFormAddComponent(s->fo, w->co); | |
80d03bd8 | 836 | } |
837 | ||
838 | Py_INCREF(Py_None); | |
839 | return Py_None; | |
840 | } | |
841 | ||
842 | static PyObject * formRun(snackForm * s, PyObject * args) { | |
ea59ae12 | 843 | struct newtExitStruct result; |
844 | ||
845 | newtFormRun(s->fo, &result); | |
846 | ||
847 | if (result.reason == NEWT_EXIT_HOTKEY) | |
6f481af2 | 848 | return Py_BuildValue("(si)", "hotkey", result.u.key); |
73390860 | 849 | else if (result.reason == NEWT_EXIT_TIMER) |
6f481af2 | 850 | return Py_BuildValue("(si)", "timer", 0); |
d7e202d6 | 851 | else if (result.reason == NEWT_EXIT_FDREADY) |
6f481af2 | 852 | return Py_BuildValue("(si)", "fdready", result.u.watch); |
ea59ae12 | 853 | else |
6f481af2 | 854 | return Py_BuildValue("(si)", "widget", result.u.co); |
ea59ae12 | 855 | } |
856 | ||
857 | static PyObject * formHotKey(snackForm * s, PyObject * args) { | |
858 | int key; | |
859 | ||
860 | if (!PyArg_ParseTuple(args, "i", &key)) | |
6f481af2 | 861 | return NULL; |
ea59ae12 | 862 | |
863 | newtFormAddHotKey(s->fo, key); | |
80d03bd8 | 864 | |
865 | Py_INCREF(Py_None); | |
866 | return Py_None; | |
867 | } | |
868 | ||
73390860 | 869 | static PyObject * formSetTimer(snackForm * form, PyObject * args) { |
870 | int millisecs; | |
871 | ||
872 | if (!PyArg_ParseTuple(args, "i", &millisecs)) | |
6f481af2 | 873 | return NULL; |
73390860 | 874 | |
875 | newtFormSetTimer(form->fo, millisecs); | |
876 | ||
877 | Py_INCREF(Py_None); | |
878 | return Py_None; | |
879 | } | |
880 | ||
d7e202d6 | 881 | static PyObject * formWatchFD(snackForm * form, PyObject * args) { |
882 | int fd, fdflags; | |
883 | ||
884 | if (!PyArg_ParseTuple(args, "ii", &fd, &fdflags)) | |
6f481af2 | 885 | return NULL; |
d7e202d6 | 886 | |
887 | newtFormWatchFd(form->fo, fd, fdflags); | |
888 | ||
889 | Py_INCREF(Py_None); | |
890 | return Py_None; | |
891 | } | |
892 | ||
5d678658 | 893 | static PyObject * formSetCurrent(snackForm * form, PyObject * args) { |
894 | snackWidget * w; | |
895 | ||
896 | if (!PyArg_ParseTuple(args, "O", &w)) | |
6f481af2 | 897 | return NULL; |
5d678658 | 898 | |
899 | newtFormSetCurrent(form->fo, w->co); | |
900 | ||
901 | Py_INCREF(Py_None); | |
902 | return Py_None; | |
903 | } | |
904 | ||
80d03bd8 | 905 | static PyObject * widgetGetAttr(PyObject * s, char * name) { |
906 | snackWidget * w = (snackWidget *) s; | |
907 | ||
ea59ae12 | 908 | if (!strcmp(name, "key")) { |
6f481af2 | 909 | return Py_BuildValue("i", w->co); |
80d03bd8 | 910 | } else if (!strcmp(name, "entryValue")) { |
6f481af2 | 911 | return Py_BuildValue("s", w->apointer); |
80d03bd8 | 912 | } else if (!strcmp(name, "checkboxValue")) { |
6f481af2 | 913 | return Py_BuildValue("i", w->achar == ' ' ? 0 : 1); |
80d03bd8 | 914 | } else if (!strcmp(name, "radioValue")) { |
6f481af2 | 915 | return Py_BuildValue("i", newtRadioGetCurrent(w->co)); |
80d03bd8 | 916 | } |
917 | ||
918 | return Py_FindMethod(widgetMethods, s, name); | |
919 | } | |
920 | ||
1dfa0cfa | 921 | static void widgetDestructor(PyObject * o) { |
922 | snackWidget * s = (snackWidget *) o; | |
923 | ||
924 | Py_XDECREF (s->scs.cb); | |
925 | Py_XDECREF (s->scs.data); | |
926 | ||
927 | PyMem_DEL(o); | |
928 | } | |
929 | ||
6a703ca8 | 930 | static PyObject * widgetAddCallback(snackWidget * s, PyObject * args) { |
1dfa0cfa | 931 | s->scs.cb = NULL; |
932 | s->scs.data = NULL; | |
933 | ||
5d678658 | 934 | if (!PyArg_ParseTuple(args, "O|O", &s->scs.cb, &s->scs.data)) |
6f481af2 | 935 | return NULL; |
6a703ca8 | 936 | |
1dfa0cfa | 937 | Py_INCREF (s->scs.cb); |
938 | Py_XINCREF (s->scs.data); | |
939 | ||
5d678658 | 940 | newtComponentAddCallback(s->co, callbackMarshall, &s->scs); |
6a703ca8 | 941 | |
942 | Py_INCREF(Py_None); | |
943 | return Py_None; | |
944 | } | |
945 | ||
80d03bd8 | 946 | static PyObject * widgetEntrySetValue(snackWidget * s, PyObject * args) { |
947 | char * val; | |
948 | ||
949 | if (!PyArg_ParseTuple(args, "s", &val)) | |
6f481af2 | 950 | return NULL; |
80d03bd8 | 951 | |
952 | newtEntrySet(s->co, val, 1); | |
953 | ||
954 | Py_INCREF(Py_None); | |
955 | return Py_None; | |
956 | } | |
957 | ||
5d678658 | 958 | static PyObject * widgetEntrySetFlags(snackWidget * s, PyObject * args) { |
959 | int flag, sense; | |
960 | ||
961 | if (!PyArg_ParseTuple(args, "ii", &flag, &sense)) return NULL; | |
962 | ||
963 | newtEntrySetFlags(s->co, flag, sense); | |
964 | ||
965 | Py_INCREF(Py_None); | |
966 | return Py_None; | |
967 | } | |
968 | ||
969 | ||
6e25d997 | 970 | static PyObject * widgetListboxAdd(snackWidget * s, PyObject * args) { |
971 | char * text; | |
972 | ||
973 | if (!PyArg_ParseTuple(args, "s", &text)) | |
6f481af2 | 974 | return NULL; |
6e25d997 | 975 | |
d7e202d6 | 976 | newtListboxAddEntry(s->co, text, I2P(s->anint)); |
6e25d997 | 977 | |
978 | return PyInt_FromLong(s->anint++); | |
979 | } | |
980 | ||
a507b3ec | 981 | static PyObject * widgetListboxIns(snackWidget * s, PyObject * args) { |
982 | char * text; | |
983 | int key; | |
984 | ||
985 | if (!PyArg_ParseTuple(args, "si", &text, &key)) | |
6f481af2 | 986 | return NULL; |
a507b3ec | 987 | |
d7e202d6 | 988 | newtListboxInsertEntry(s->co, text, I2P(s->anint), I2P(key)); |
a507b3ec | 989 | |
990 | return PyInt_FromLong(s->anint++); | |
991 | } | |
992 | ||
993 | static PyObject * widgetListboxDel(snackWidget * s, PyObject * args) { | |
994 | int key; | |
995 | ||
996 | if (!PyArg_ParseTuple(args, "i", &key)) | |
6f481af2 | 997 | return NULL; |
a507b3ec | 998 | |
d7e202d6 | 999 | newtListboxDeleteEntry(s->co, I2P(key)); |
a507b3ec | 1000 | |
1001 | Py_INCREF(Py_None); | |
1002 | return Py_None; | |
1003 | } | |
1004 | ||
6e25d997 | 1005 | static PyObject * widgetListboxGet(snackWidget * s, PyObject * args) { |
1006 | if (!PyArg_ParseTuple(args, "")) | |
6f481af2 | 1007 | return NULL; |
6e25d997 | 1008 | |
1009 | return PyInt_FromLong((long) newtListboxGetCurrent(s->co)); | |
1010 | } | |
1011 | ||
99438c19 | 1012 | static PyObject * widgetListboxSet(snackWidget * s, PyObject * args) { |
1013 | int index; | |
1014 | ||
1015 | if (!PyArg_ParseTuple(args, "i", &index)) | |
6f481af2 | 1016 | return NULL; |
99438c19 | 1017 | |
d7e202d6 | 1018 | newtListboxSetCurrentByKey(s->co, I2P(index)); |
99438c19 | 1019 | |
1020 | Py_INCREF(Py_None); | |
1021 | return Py_None; | |
1022 | } | |
1023 | ||
6e25d997 | 1024 | static PyObject * widgetListboxSetW(snackWidget * s, PyObject * args) { |
1025 | int width; | |
1026 | ||
1027 | if (!PyArg_ParseTuple(args, "i", &width)) | |
6f481af2 | 1028 | return NULL; |
6e25d997 | 1029 | |
1030 | newtListboxSetWidth(s->co, width); | |
1031 | ||
1032 | Py_INCREF(Py_None); | |
1033 | return Py_None; | |
1034 | } | |
1035 | ||
50c9056c | 1036 | static PyObject * widgetListboxClear(snackWidget * s, PyObject * args) { |
1037 | if (!PyArg_ParseTuple(args, "")) | |
1038 | return NULL; | |
1039 | ||
1040 | newtListboxClear(s->co); | |
1041 | ||
1042 | Py_INCREF(Py_None); | |
1043 | return Py_None; | |
1044 | } | |
1045 | ||
80d03bd8 | 1046 | static void emptyDestructor(PyObject * s) { |
1047 | } | |
1048 | ||
615bc463 | 1049 | static snackWidget * checkboxTreeWidget(PyObject * s, PyObject * args, PyObject * kwargs) { |
f06c5a99 | 1050 | int height; |
1051 | int scrollBar = 0; | |
615bc463 | 1052 | int hide_checkbox = 0; |
1053 | int unselectable = 0; | |
1054 | int flags; | |
f06c5a99 | 1055 | snackWidget * widget; |
615bc463 | 1056 | const char *kw[] = {"height", "scrollbar", "hide_checkbox", "unselectable", NULL}; |
f06c5a99 | 1057 | |
615bc463 | 1058 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|iii", (char **) kw, |
6f481af2 | 1059 | &height, &scrollBar, &hide_checkbox, &unselectable)) |
1060 | return NULL; | |
f06c5a99 | 1061 | |
615bc463 | 1062 | flags = (scrollBar ? NEWT_FLAG_SCROLL : 0) | |
6f481af2 | 1063 | (hide_checkbox ? NEWT_CHECKBOXTREE_HIDE_BOX : 0) | |
1064 | (unselectable ? NEWT_CHECKBOXTREE_UNSELECTABLE : 0); | |
615bc463 | 1065 | |
1dfa0cfa | 1066 | widget = snackWidgetNew (); |
615bc463 | 1067 | widget->co = newtCheckboxTree(-1, -1, height, flags); |
f06c5a99 | 1068 | |
1069 | widget->anint = 1; | |
1070 | ||
1071 | return widget; | |
1072 | } | |
1073 | ||
75d40644 | 1074 | static PyObject * widgetCheckboxTreeAddItem(snackWidget * s, PyObject * args) { |
f06c5a99 | 1075 | char * text; |
f06c5a99 | 1076 | int selected = 0; |
75d40644 | 1077 | PyObject * pathList, * o; |
1078 | int len; | |
1079 | int * path; | |
1080 | int i; | |
1081 | ||
1082 | if (!PyArg_ParseTuple(args, "sOi", &text, &pathList, &selected)) | |
6f481af2 | 1083 | return NULL; |
f06c5a99 | 1084 | |
75d40644 | 1085 | len = PyTuple_Size(pathList); |
1086 | path = alloca(sizeof(*path) * (len + 1)); | |
1087 | for (i = 0; i < len; i++) { | |
1088 | o = PyTuple_GetItem(pathList, i); | |
6f481af2 | 1089 | path[i] = PyInt_AsLong(o); |
75d40644 | 1090 | } |
1091 | path[len] = NEWT_ARG_LAST; | |
1092 | ||
d7e202d6 | 1093 | newtCheckboxTreeAddArray(s->co, text, I2P(s->anint), |
6f481af2 | 1094 | selected ? NEWT_FLAG_SELECTED : 0, path); |
f06c5a99 | 1095 | |
1096 | return PyInt_FromLong(s->anint++); | |
1097 | } | |
1098 | ||
2b6641b1 | 1099 | static PyObject * widgetCheckboxTreeGetCur(snackWidget * s, PyObject * args) { |
1100 | if (!PyArg_ParseTuple(args, "")) | |
6f481af2 | 1101 | return NULL; |
2b6641b1 | 1102 | |
d7e202d6 | 1103 | return PyInt_FromLong((long)newtCheckboxTreeGetCurrent(s->co)); |
2b6641b1 | 1104 | } |
1105 | ||
c895490d | 1106 | static PyObject * widgetCheckboxTreeSetEntry(snackWidget * s, PyObject * args) { |
1107 | int data; | |
1108 | char *text; | |
1109 | ||
1110 | if (!PyArg_ParseTuple(args, "is", &data, &text)) return NULL; | |
1111 | ||
d7e202d6 | 1112 | newtCheckboxTreeSetEntry(s->co, I2P(data), text); |
c895490d | 1113 | |
1114 | Py_INCREF(Py_None); | |
d8c97d41 | 1115 | return Py_None; |
1116 | } | |
1117 | ||
1118 | static PyObject * widgetCheckboxTreeSetWidth(snackWidget * s, PyObject * args) { | |
1119 | int width; | |
1120 | ||
1121 | if (!PyArg_ParseTuple(args, "i", &width)) | |
6f481af2 | 1122 | return NULL; |
d8c97d41 | 1123 | |
1124 | newtCheckboxTreeSetWidth(s->co, width); | |
1125 | ||
1126 | Py_INCREF(Py_None); | |
c895490d | 1127 | return Py_None; |
1128 | } | |
1129 | ||
5160fc22 | 1130 | static PyObject * widgetCheckboxTreeSetCurrent(snackWidget * s, PyObject * args) { |
1131 | int data; | |
1132 | ||
1133 | if (!PyArg_ParseTuple(args, "i", &data)) return NULL; | |
1134 | ||
d7e202d6 | 1135 | newtCheckboxTreeSetCurrent(s->co, I2P(data)); |
5160fc22 | 1136 | |
1137 | Py_INCREF(Py_None); | |
1138 | return Py_None; | |
1139 | } | |
1140 | ||
c895490d | 1141 | static PyObject * widgetCheckboxTreeSetEntryValue(snackWidget * s, PyObject * args) { |
1142 | int data; | |
1143 | int isOn = 1; | |
1144 | ||
1145 | if (!PyArg_ParseTuple(args, "i|i", &data, &isOn)) return NULL; | |
1146 | ||
d7e202d6 | 1147 | newtCheckboxTreeSetEntryValue(s->co, I2P(data), |
6f481af2 | 1148 | isOn ? NEWT_CHECKBOXTREE_SELECTED : |
1149 | NEWT_CHECKBOXTREE_UNSELECTED); | |
c895490d | 1150 | |
1151 | Py_INCREF(Py_None); | |
1152 | return Py_None; | |
1153 | } | |
1154 | ||
1155 | static PyObject * widgetCheckboxTreeGetEntryValue(snackWidget * s, PyObject * args) { | |
1156 | int data; | |
1157 | int isOn = 0; | |
1158 | int isBranch = 0; | |
1159 | char selection; | |
1160 | ||
1161 | if (!PyArg_ParseTuple(args, "i", &data)) return NULL; | |
1162 | ||
d7e202d6 | 1163 | selection = newtCheckboxTreeGetEntryValue(s->co, I2P(data)); |
c895490d | 1164 | |
1165 | if (selection == -1) return NULL; | |
1166 | ||
1167 | switch (selection) { | |
1168 | case NEWT_CHECKBOXTREE_EXPANDED: | |
6f481af2 | 1169 | isOn = 1; |
c895490d | 1170 | case NEWT_CHECKBOXTREE_COLLAPSED: |
6f481af2 | 1171 | isBranch = 1; |
1172 | break; | |
c895490d | 1173 | case NEWT_CHECKBOXTREE_UNSELECTED: |
6f481af2 | 1174 | break; |
c895490d | 1175 | default: |
6f481af2 | 1176 | isOn = 1; |
1177 | break; | |
c895490d | 1178 | } |
1179 | return Py_BuildValue("(ii)", isBranch, isOn); | |
1180 | } | |
1181 | ||
5a6729cf | 1182 | static PyObject * widgetCheckboxTreeGetSel(snackWidget * s, |
6f481af2 | 1183 | PyObject * args) { |
f06c5a99 | 1184 | void ** selection; |
1185 | int numselected; | |
5a6729cf | 1186 | int i; |
1187 | PyObject * sel; | |
f06c5a99 | 1188 | |
2b6641b1 | 1189 | if (!PyArg_ParseTuple(args, "")) |
6f481af2 | 1190 | return NULL; |
2b6641b1 | 1191 | |
72b71fa6 | 1192 | selection = (void **) newtCheckboxTreeGetSelection(s->co, &numselected); |
f06c5a99 | 1193 | |
788ec656 | 1194 | sel = PyList_New(0); |
1195 | ||
5a6729cf | 1196 | if (!selection) { |
6f481af2 | 1197 | return sel; |
f06c5a99 | 1198 | } |
1199 | ||
5a6729cf | 1200 | sel = PyList_New(0); |
1201 | for (i = 0; i < numselected; i++) { | |
6f481af2 | 1202 | PyList_Append(sel, PyInt_FromLong((long) selection[i])); |
5a6729cf | 1203 | } |
1204 | free(selection); | |
f06c5a99 | 1205 | |
5a6729cf | 1206 | return sel; |
f06c5a99 | 1207 | } |
1208 | ||
ce11acc8 | 1209 | static PyObject * pywstrlen(PyObject * s, PyObject * args) |
1210 | { | |
1211 | char *str; | |
1212 | int len = -1; | |
1213 | ||
1214 | if (!PyArg_ParseTuple(args, "s|i", &str, &len)) return NULL; | |
1215 | ||
1216 | return PyInt_FromLong(wstrlen(str, len)); | |
1217 | } | |
1218 | ||
80d03bd8 | 1219 | void init_snack(void) { |
6e25d997 | 1220 | PyObject * d, * m; |
1221 | ||
1222 | m = Py_InitModule("_snack", snackModuleMethods); | |
1223 | d = PyModule_GetDict(m); | |
1224 | ||
1225 | PyDict_SetItemString(d, "ANCHOR_LEFT", PyInt_FromLong(NEWT_ANCHOR_LEFT)); | |
1226 | PyDict_SetItemString(d, "ANCHOR_TOP", PyInt_FromLong(NEWT_ANCHOR_TOP)); | |
1227 | PyDict_SetItemString(d, "ANCHOR_RIGHT", PyInt_FromLong(NEWT_ANCHOR_RIGHT)); | |
1228 | PyDict_SetItemString(d, "ANCHOR_BOTTOM", | |
6f481af2 | 1229 | PyInt_FromLong(NEWT_ANCHOR_BOTTOM)); |
6e25d997 | 1230 | PyDict_SetItemString(d, "GRID_GROWX", PyInt_FromLong(NEWT_GRID_FLAG_GROWX)); |
1231 | PyDict_SetItemString(d, "GRID_GROWY", PyInt_FromLong(NEWT_GRID_FLAG_GROWY)); | |
ea59ae12 | 1232 | |
d7e202d6 | 1233 | PyDict_SetItemString(d, "FD_READ", PyInt_FromLong(NEWT_FD_READ)); |
1234 | PyDict_SetItemString(d, "FD_WRITE", PyInt_FromLong(NEWT_FD_WRITE)); | |
1235 | PyDict_SetItemString(d, "FD_EXCEPT", PyInt_FromLong(NEWT_FD_EXCEPT)); | |
1236 | ||
ea59ae12 | 1237 | PyDict_SetItemString(d, "FORM_EXIT_HOTKEY", PyString_FromString("hotkey")); |
1238 | PyDict_SetItemString(d, "FORM_EXIT_WIDGET", PyString_FromString("widget")); | |
73390860 | 1239 | PyDict_SetItemString(d, "FORM_EXIT_TIMER", PyString_FromString("timer")); |
d7e202d6 | 1240 | PyDict_SetItemString(d, "FORM_EXIT_FDREADY", PyString_FromString("fdready")); |
ea59ae12 | 1241 | |
1242 | PyDict_SetItemString(d, "KEY_F1", PyInt_FromLong(NEWT_KEY_F1)); | |
1243 | PyDict_SetItemString(d, "KEY_F2", PyInt_FromLong(NEWT_KEY_F2)); | |
1244 | PyDict_SetItemString(d, "KEY_F3", PyInt_FromLong(NEWT_KEY_F3)); | |
1245 | PyDict_SetItemString(d, "KEY_F4", PyInt_FromLong(NEWT_KEY_F4)); | |
1246 | PyDict_SetItemString(d, "KEY_F5", PyInt_FromLong(NEWT_KEY_F5)); | |
1247 | PyDict_SetItemString(d, "KEY_F6", PyInt_FromLong(NEWT_KEY_F6)); | |
1248 | PyDict_SetItemString(d, "KEY_F7", PyInt_FromLong(NEWT_KEY_F7)); | |
1249 | PyDict_SetItemString(d, "KEY_F8", PyInt_FromLong(NEWT_KEY_F8)); | |
1250 | PyDict_SetItemString(d, "KEY_F9", PyInt_FromLong(NEWT_KEY_F9)); | |
1251 | PyDict_SetItemString(d, "KEY_F10", PyInt_FromLong(NEWT_KEY_F10)); | |
1252 | PyDict_SetItemString(d, "KEY_F11", PyInt_FromLong(NEWT_KEY_F11)); | |
1253 | PyDict_SetItemString(d, "KEY_F12", PyInt_FromLong(NEWT_KEY_F12)); | |
5d678658 | 1254 | |
1255 | PyDict_SetItemString(d, "FLAG_DISABLED", PyInt_FromLong(NEWT_FLAG_DISABLED)); | |
1256 | PyDict_SetItemString(d, "FLAGS_SET", PyInt_FromLong(NEWT_FLAGS_SET)); | |
1257 | PyDict_SetItemString(d, "FLAGS_RESET", PyInt_FromLong(NEWT_FLAGS_RESET)); | |
1258 | PyDict_SetItemString(d, "FLAGS_TOGGLE", PyInt_FromLong(NEWT_FLAGS_TOGGLE)); | |
80d03bd8 | 1259 | } |