~ubuntu-branches/ubuntu/saucy/newt/saucy-proposed

« back to all changes in this revision

Viewing changes to snackmodule.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2012-11-08 14:50:00 UTC
  • mfrom: (2.1.20 sid)
  • Revision ID: package-import@ubuntu.com-20121108145000-0yfrol7obct0jufq
Tags: 0.52.14-11ubuntu1
* Merge with Debian; remaining changes:
  - Port python module to python3.
  - Fix installation for multiple python3 versions.
  - Move libnewt to /lib and whiptail to /bin so they can be used by
    friendly-recovery on systems that have a separate /usr.
  - debian/libnewt0.52.install, debian/libnewt0.52.postinst,
    debian/palette => debian/palette.original:
    - move palette from /usr to /etc such that they can be edited by an
      admin.
* Configure --with-colorsfile=/etc/newt/palette.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include "config.h"
 
3
 
 
4
#ifdef HAVE_ALLOCA_H
 
5
#include <alloca.h>
 
6
#endif
 
7
 
 
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 "structmember.h"
 
16
#include "nls.h"
 
17
#include "newt.h"
 
18
#include "newt_pr.h"
 
19
 
 
20
#if PY_MAJOR_VERSION >= 3
 
21
  #define PyInt_FromLong PyLong_FromLong
 
22
  #define PyInt_AsLong PyLong_AsLong
 
23
  #define PyString_FromString PyUnicode_FromString
 
24
  #define MOD_ERROR_VAL NULL
 
25
  #define MOD_SUCCESS_VAL(val) val
 
26
  #define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
 
27
#else
 
28
  #define MOD_ERROR_VAL
 
29
  #define MOD_SUCCESS_VAL(val)
 
30
  #define MOD_INIT(name) void init##name(void)
 
31
#endif
 
32
 
 
33
typedef struct snackWidget_s snackWidget;
 
34
typedef struct snackGrid_s snackGrid;
 
35
typedef struct snackForm_s snackForm;
 
36
 
 
37
struct callbackStruct {
 
38
    PyObject * cb, * data;
 
39
};
 
40
 
 
41
/* Integer to pointer, 64-bit-sane */
 
42
#define I2P(x) ((void *)(long)(x))
 
43
 
 
44
static struct callbackStruct suspend;
 
45
static struct callbackStruct helpCallback;
 
46
 
 
47
static void emptyDestructor(PyObject * s);
 
48
 
 
49
static snackWidget * buttonWidget(PyObject * s, PyObject * args);
 
50
static snackWidget * compactbuttonWidget(PyObject * s, PyObject * args);
 
51
static PyObject * centeredWindow(PyObject * s, PyObject * args);
 
52
static snackWidget * checkboxWidget(PyObject * s, PyObject * args);
 
53
static PyObject * choiceWindow(PyObject * s, PyObject * args);
 
54
static snackWidget * entryWidget(PyObject * s, PyObject * args);
 
55
static PyObject * drawRootText(PyObject * s, PyObject * args);
 
56
static PyObject * doResume(PyObject * s, PyObject * args);
 
57
static PyObject * doSuspend(PyObject * s, PyObject * args);
 
58
static PyObject * doSuspend(PyObject * s, PyObject * args);
 
59
static snackForm * formCreate(PyObject * s, PyObject * args);
 
60
static snackGrid * gridCreate(PyObject * s, PyObject * args);
 
61
static PyObject * gridWrappedWindow(PyObject * s, PyObject * args);
 
62
static PyObject * finishScreen(PyObject * s, PyObject * args);
 
63
static PyObject * initScreen(PyObject * s, PyObject * args);
 
64
static snackWidget * labelWidget(PyObject * s, PyObject * args);
 
65
static snackWidget * listboxWidget(PyObject * s, PyObject * args);
 
66
static PyObject * messageWindow(PyObject * s, PyObject * args);
 
67
static PyObject * openWindow(PyObject * s, PyObject * args);
 
68
static PyObject * popHelpLine(PyObject * s, PyObject * args);
 
69
static PyObject * popWindow(PyObject * s, PyObject * args);
 
70
static PyObject * popWindowNoRefresh(PyObject * s, PyObject * args);
 
71
static PyObject * pushHelpLine(PyObject * s, PyObject * args);
 
72
static snackWidget * radioButtonWidget(PyObject * s, PyObject * args);
 
73
static PyObject * refreshScreen(PyObject * s, PyObject * args);
 
74
static PyObject * setColor(PyObject * s, PyObject * args);
 
75
static PyObject * scaleWidget(PyObject * s, PyObject * args);
 
76
static PyObject * scaleSet(snackWidget * s, PyObject * args);
 
77
static PyObject * screenSize(PyObject * s, PyObject * args);
 
78
static PyObject * setSuspendCallback(PyObject * s, PyObject * args);
 
79
static PyObject * setHelpCallback(PyObject * s, PyObject * args);
 
80
static PyObject * reflowText(PyObject * s, PyObject * args);
 
81
static snackWidget * textWidget(PyObject * s, PyObject * args);
 
82
static PyObject * ternaryWindow(PyObject * s, PyObject * args);
 
83
static snackWidget * checkboxTreeWidget(PyObject * s, PyObject * args, PyObject * kwargs);
 
84
static PyObject * pywstrlen(PyObject * s, PyObject * args);
 
85
static PyObject * widget_get_checkboxValue(PyObject *self, void *closure);
 
86
static PyObject * widget_get_radioValue(PyObject *self, void *closure);
 
87
 
 
88
static PyMethodDef snackModuleMethods[] = {
 
89
    { "button", (PyCFunction) buttonWidget, METH_VARARGS, NULL },
 
90
    { "compactbutton", (PyCFunction) compactbuttonWidget, METH_VARARGS, NULL },
 
91
    { "checkbox", (PyCFunction) checkboxWidget, METH_VARARGS, NULL },
 
92
    { "choice", choiceWindow, METH_VARARGS, NULL },
 
93
    { "centeredwindow", centeredWindow, METH_VARARGS, NULL },
 
94
    { "drawroottext", drawRootText, METH_VARARGS, NULL },
 
95
    { "entry", (PyCFunction) entryWidget, METH_VARARGS, NULL },
 
96
    { "finish", finishScreen, METH_VARARGS, NULL },
 
97
    { "form", (PyCFunction) formCreate, METH_VARARGS, NULL },
 
98
    { "grid", (PyCFunction) gridCreate, METH_VARARGS, NULL },
 
99
    { "gridwrappedwindow", gridWrappedWindow, METH_VARARGS, NULL },
 
100
    { "helpcallback", setHelpCallback, METH_VARARGS, NULL },
 
101
    { "init", initScreen, METH_VARARGS, NULL },
 
102
    { "label", (PyCFunction) labelWidget, METH_VARARGS, NULL },
 
103
    { "listbox", (PyCFunction) listboxWidget, METH_VARARGS, NULL },
 
104
    { "message", messageWindow, METH_VARARGS, NULL },
 
105
    { "openwindow", openWindow, METH_VARARGS, NULL },
 
106
    { "pophelpline", popHelpLine, METH_VARARGS, NULL },
 
107
    { "popwindow", popWindow, METH_VARARGS, NULL },
 
108
    { "popwindownorefresh", popWindowNoRefresh, METH_VARARGS, NULL },
 
109
    { "pushhelpline", pushHelpLine, METH_VARARGS, NULL },
 
110
    { "radiobutton", (PyCFunction) radioButtonWidget, METH_VARARGS, NULL },
 
111
    { "reflow", (PyCFunction) reflowText, METH_VARARGS, NULL },
 
112
    { "refresh", refreshScreen, METH_VARARGS, NULL },
 
113
    { "setcolor", setColor, METH_VARARGS, NULL },
 
114
    { "resume", doResume, METH_VARARGS, NULL },
 
115
    { "scale", scaleWidget, METH_VARARGS, NULL },
 
116
    { "size", screenSize, METH_VARARGS, NULL },
 
117
    { "suspend", doSuspend, METH_VARARGS, NULL },
 
118
    { "suspendcallback", setSuspendCallback, METH_VARARGS, NULL },
 
119
    { "ternary", ternaryWindow, METH_VARARGS, NULL },
 
120
    { "textbox", (PyCFunction) textWidget, METH_VARARGS, NULL },
 
121
    { "checkboxtree", (PyCFunction) checkboxTreeWidget, METH_VARARGS | METH_KEYWORDS, NULL },
 
122
    { "wstrlen", (PyCFunction) pywstrlen, METH_VARARGS | METH_KEYWORDS, NULL },
 
123
    { NULL }
 
124
} ;
 
125
 
 
126
#if PY_MAJOR_VERSION >= 3
 
127
static struct PyModuleDef moduledef = {
 
128
    PyModuleDef_HEAD_INIT,
 
129
        "_snack",            /* m_name */
 
130
        NULL,                /* m_doc */
 
131
        -1,                  /* m_size */
 
132
        snackModuleMethods,  /* m_methods */
 
133
        NULL,                /* m_reload */
 
134
        NULL,                /* m_traverse */
 
135
        NULL,                /* m_clear */
 
136
        NULL,                /* m_free */
 
137
    };
 
138
#endif
 
139
 
 
140
static struct PyGetSetDef widget_getset[] = {
 
141
        { "checkboxValue", widget_get_checkboxValue, 0, NULL, NULL },
 
142
        { "radioValue", widget_get_radioValue, 0, NULL, NULL },
 
143
        { NULL }
 
144
};
 
145
 
 
146
struct snackGrid_s {
 
147
    PyObject_HEAD
 
148
    newtGrid grid;
 
149
} ;
 
150
 
 
151
static PyObject * gridPlace(snackGrid * s, PyObject * args);
 
152
static PyObject * gridSetField(snackGrid * s, PyObject * args);
 
153
 
 
154
static PyMethodDef gridMethods[] = {
 
155
    { "place", (PyCFunction) gridPlace, METH_VARARGS, NULL },
 
156
    { "setfield", (PyCFunction) gridSetField, METH_VARARGS, NULL },
 
157
    { NULL }
 
158
};
 
159
 
 
160
static PyTypeObject snackGridType = {
 
161
        PyVarObject_HEAD_INIT(&PyType_Type, 0)
 
162
        "snackgrid",                    /* tp_name */
 
163
        sizeof(snackGrid),              /* tp_size */
 
164
        0,                              /* tp_itemsize */
 
165
        emptyDestructor,                        /* tp_dealloc */
 
166
        0,                              /* tp_print */
 
167
        0,                              /* 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
        0,                              /* tp_hash */
 
175
        0,                              /* tp_call */
 
176
        0,                              /* tp_str */
 
177
        PyObject_GenericGetAttr,        /* tp_getattro */
 
178
        0,                              /* tp_setattro */
 
179
        0,                              /* tp_as_buffer */
 
180
        Py_TPFLAGS_DEFAULT,             /* tp_flags */
 
181
        0,                              /* tp_doc */
 
182
        0,                              /* tp_traverse */
 
183
        0,                              /* tp_clear */
 
184
        0,                              /* tp_richcompare */
 
185
        0,                              /* tp_weaklistoffset */
 
186
        0,                              /* tp_iter */
 
187
        0,                              /* tp_iternext */
 
188
        gridMethods                     /* tp_methods */
 
189
};
 
190
 
 
191
struct snackForm_s {
 
192
    PyObject_HEAD
 
193
    newtComponent fo;
 
194
} ;
 
195
 
 
196
static PyObject * formAdd(snackForm * s, PyObject * args);
 
197
static PyObject * formDraw(snackForm * s, PyObject * args);
 
198
static PyObject * formRun(snackForm * s, PyObject * args);
 
199
static PyObject * formHotKey(snackForm * s, PyObject * args);
 
200
static PyObject * formSetCurrent(snackForm * form, PyObject * args);
 
201
static PyObject * formSetTimer(snackForm * form, PyObject * args);
 
202
static PyObject * formWatchFD(snackForm * form, PyObject * args);
 
203
 
 
204
static PyMethodDef formMethods[] = {
 
205
    { "add", (PyCFunction) formAdd, METH_VARARGS, NULL },
 
206
    { "draw", (PyCFunction) formDraw, METH_VARARGS, NULL },
 
207
    { "run", (PyCFunction) formRun, METH_VARARGS, NULL },
 
208
    { "addhotkey", (PyCFunction) formHotKey, METH_VARARGS, NULL },
 
209
    { "setcurrent", (PyCFunction) formSetCurrent, METH_VARARGS, NULL },
 
210
    { "settimer", (PyCFunction) formSetTimer, METH_VARARGS, NULL },
 
211
    { "watchfd", (PyCFunction) formWatchFD, METH_VARARGS, NULL },
 
212
    { NULL }
 
213
};
 
214
 
 
215
static PyTypeObject snackFormType = {
 
216
        PyVarObject_HEAD_INIT(&PyType_Type, 0)
 
217
        "snackform",                    /* tp_name */
 
218
        sizeof(snackForm),              /* tp_size */
 
219
        0,                              /* tp_itemsize */
 
220
        emptyDestructor,                /* tp_dealloc */
 
221
        0,                              /* tp_print */
 
222
        0,                              /* tp_getattr */
 
223
        0,                              /* tp_setattr */
 
224
        0,                              /* tp_compare */
 
225
        0,                              /* tp_repr */
 
226
        0,                              /* tp_as_number */
 
227
        0,                              /* tp_as_sequence */
 
228
        0,                              /* tp_as_mapping */
 
229
        0,                              /* tp_hash */
 
230
        0,                              /* tp_call */
 
231
        0,                              /* tp_str */
 
232
        PyObject_GenericGetAttr,        /* tp_getattro */
 
233
        0,                              /* tp_setattro */
 
234
        0,                              /* tp_as_buffer */
 
235
        Py_TPFLAGS_DEFAULT,             /* tp_flags */
 
236
        0,                              /* tp_doc */
 
237
        0,                              /* tp_traverse */
 
238
        0,                              /* tp_clear */
 
239
        0,                              /* tp_richcompare */
 
240
        0,                              /* tp_weaklistoffset */
 
241
        0,                              /* tp_iter */
 
242
        0,                              /* tp_iternext */
 
243
        formMethods                     /* tp_methods */
 
244
};
 
245
 
 
246
struct snackWidget_s {
 
247
    PyObject_HEAD
 
248
    newtComponent co;
 
249
    char achar;
 
250
    void * apointer;
 
251
    int anint;
 
252
    struct callbackStruct scs;
 
253
} ;
 
254
 
 
255
static PyObject * widgetAddCallback(snackWidget * s, PyObject * args);
 
256
static void widgetDestructor(PyObject * s);
 
257
static PyObject * widgetEntrySetValue(snackWidget * s, PyObject * args);
 
258
static PyObject * widgetLabelText(snackWidget * s, PyObject * args);
 
259
static PyObject * widgetLabelSetColors(snackWidget * s, PyObject * args);
 
260
static PyObject * widgetListboxSetW(snackWidget * s, PyObject * args);
 
261
static PyObject * widgetListboxAdd(snackWidget * s, PyObject * args);
 
262
static PyObject * widgetListboxIns(snackWidget * s, PyObject * args);
 
263
static PyObject * widgetListboxDel(snackWidget * s, PyObject * args);
 
264
static PyObject * widgetListboxGet(snackWidget * s, PyObject * args);
 
265
static PyObject * widgetListboxGetSel(snackWidget * s, PyObject * args);
 
266
static PyObject * widgetListboxSet(snackWidget * s, PyObject * args);
 
267
static PyObject * widgetListboxClear(snackWidget * s, PyObject * args);
 
268
static PyObject * widgetTextboxText(snackWidget * s, PyObject * args);
 
269
static PyObject * widgetTextboxHeight(snackWidget * s, PyObject * args);
 
270
static PyObject * widgetCheckboxTreeAddItem(snackWidget * s, PyObject * args);
 
271
static PyObject * widgetCheckboxTreeGetSel(snackWidget * s, PyObject * args);
 
272
static PyObject * widgetCheckboxTreeGetCur(snackWidget * s, PyObject * args);
 
273
static PyObject * widgetCheckboxTreeSetEntry(snackWidget * s, PyObject * args);
 
274
static PyObject * widgetCheckboxTreeSetWidth(snackWidget * s, PyObject * args);
 
275
static PyObject * widgetCheckboxTreeSetCurrent(snackWidget * s, PyObject * args);
 
276
static PyObject * widgetCheckboxTreeSetEntryValue(snackWidget * s, PyObject * args);
 
277
static PyObject * widgetCheckboxTreeGetEntryValue(snackWidget * s, PyObject * args);
 
278
static PyObject * widgetEntrySetFlags(snackWidget * s, PyObject * args);
 
279
static PyObject * widgetCheckboxSetFlags(snackWidget * s, PyObject * args);
 
280
static PyObject * widgetCheckboxSetValue(snackWidget * s, PyObject * args);
 
281
 
 
282
static PyMethodDef widgetMethods[] = {
 
283
    { "setCallback", (PyCFunction) widgetAddCallback, METH_VARARGS, NULL },
 
284
    { "labelSetColors", (PyCFunction) widgetLabelSetColors, METH_VARARGS, NULL },
 
285
    { "labelText", (PyCFunction) widgetLabelText, METH_VARARGS, NULL },
 
286
    { "textboxText", (PyCFunction) widgetTextboxText, METH_VARARGS, NULL },
 
287
    { "textboxHeight", (PyCFunction) widgetTextboxHeight, METH_VARARGS, NULL },
 
288
    { "entrySetValue", (PyCFunction) widgetEntrySetValue, METH_VARARGS, NULL },
 
289
    { "listboxAddItem", (PyCFunction) widgetListboxAdd, METH_VARARGS, NULL },
 
290
    { "listboxInsertItem", (PyCFunction) widgetListboxIns, METH_VARARGS, NULL },
 
291
    { "listboxGetCurrent", (PyCFunction) widgetListboxGet, METH_VARARGS, NULL },
 
292
    { "listboxGetSelection", (PyCFunction) widgetListboxGetSel, METH_VARARGS, NULL },
 
293
    { "listboxSetCurrent", (PyCFunction) widgetListboxSet, METH_VARARGS, NULL },
 
294
    { "listboxSetWidth", (PyCFunction) widgetListboxSetW, METH_VARARGS, NULL },
 
295
    { "listboxDeleteItem", (PyCFunction) widgetListboxDel, METH_VARARGS, NULL },
 
296
    { "listboxClear", (PyCFunction) widgetListboxClear, METH_VARARGS, NULL },
 
297
    { "scaleSet", (PyCFunction) scaleSet, METH_VARARGS, NULL },
 
298
    { "checkboxtreeAddItem", (PyCFunction) widgetCheckboxTreeAddItem,
 
299
      METH_VARARGS, NULL },
 
300
    { "checkboxtreeGetCurrent", (PyCFunction) widgetCheckboxTreeGetCur,
 
301
      METH_VARARGS, NULL },
 
302
    { "checkboxtreeGetEntryValue", (PyCFunction) widgetCheckboxTreeGetEntryValue,
 
303
      METH_VARARGS, NULL },
 
304
    { "checkboxtreeSetEntry", (PyCFunction) widgetCheckboxTreeSetEntry,
 
305
      METH_VARARGS, NULL },
 
306
    { "checkboxtreeSetWidth", (PyCFunction) widgetCheckboxTreeSetWidth, METH_VARARGS, NULL },
 
307
    { "checkboxtreeSetCurrent", (PyCFunction) widgetCheckboxTreeSetCurrent,
 
308
      METH_VARARGS, NULL },
 
309
    { "checkboxtreeSetEntryValue", (PyCFunction) widgetCheckboxTreeSetEntryValue,
 
310
      METH_VARARGS, NULL },
 
311
    { "checkboxtreeGetSelection", (PyCFunction) widgetCheckboxTreeGetSel,
 
312
      METH_VARARGS, NULL },  
 
313
    { "entrySetFlags", (PyCFunction) widgetEntrySetFlags, METH_VARARGS, NULL },
 
314
    { "checkboxSetFlags", (PyCFunction) widgetCheckboxSetFlags, METH_VARARGS, NULL },
 
315
    { "checkboxSetValue", (PyCFunction) widgetCheckboxSetValue, METH_VARARGS, NULL },
 
316
    { NULL }
 
317
};
 
318
 
 
319
static PyMemberDef widget_members[] = {
 
320
        { "key" , T_INT, offsetof(snackWidget, co), 0, NULL },
 
321
        { "entryValue", T_STRING, offsetof(snackWidget, apointer), 0, NULL },
 
322
        { NULL }
 
323
};
 
324
 
 
325
static PyTypeObject snackWidgetType = {
 
326
        PyVarObject_HEAD_INIT(&PyType_Type, 0)
 
327
        "snackwidget",                  /* tp_name */
 
328
        sizeof(snackWidget),            /* tp_size */
 
329
        0,                              /* tp_itemsize */
 
330
        widgetDestructor,               /* tp_dealloc */
 
331
        0,                              /* tp_print */
 
332
        0,                              /* tp_getattr */
 
333
        0,                              /* tp_setattr */
 
334
        0,                              /* tp_compare */
 
335
        0,                              /* tp_repr */
 
336
        0,                              /* tp_as_number */
 
337
        0,                              /* tp_as_sequence */
 
338
        0,                              /* tp_as_mapping */
 
339
        0,                              /* tp_hash */
 
340
        0,                              /* tp_call */
 
341
        0,                              /* tp_str */
 
342
        PyObject_GenericGetAttr,        /* tp_getattro */
 
343
        0,                              /* tp_setattro */
 
344
        0,                              /* tp_as_buffer */
 
345
        Py_TPFLAGS_DEFAULT,             /* tp_flags */
 
346
        0,                              /* tp_doc */
 
347
        0,                              /* tp_traverse */
 
348
        0,                              /* tp_clear */
 
349
        0,                              /* tp_richcompare */
 
350
        0,                              /* tp_weaklistoffset */
 
351
        0,                              /* tp_iter */
 
352
        0,                              /* tp_iternext */
 
353
        widgetMethods,                  /* tp_methods */
 
354
        widget_members,                 /* tp_members */
 
355
        widget_getset                   /* tp_getset */
 
356
};
 
357
 
 
358
static snackWidget * snackWidgetNew (void) {
 
359
    snackWidget * widget;
 
360
     
 
361
    widget = PyObject_New(snackWidget, &snackWidgetType);
 
362
 
 
363
    widget->scs.cb = NULL;
 
364
    widget->scs.data = NULL;
 
365
 
 
366
    return widget;
 
367
}
 
368
 
 
369
static PyObject * initScreen(PyObject * s, PyObject * args) {
 
370
    static int init_newt = 1;
 
371
    suspend.cb = NULL;
 
372
    suspend.data = NULL;
 
373
    
 
374
    if (init_newt) { 
 
375
        newtInit();
 
376
        init_newt = 0;
 
377
    }
 
378
    newtCls();
 
379
 
 
380
    Py_INCREF(Py_None);
 
381
    return Py_None;
 
382
}
 
383
 
 
384
static PyObject * finishScreen(PyObject * s, PyObject * args) {
 
385
    Py_XDECREF (suspend.cb);
 
386
    Py_XDECREF (suspend.data);
 
387
    
 
388
    newtFinished();
 
389
    Py_INCREF(Py_None);
 
390
    return Py_None;
 
391
}
 
392
 
 
393
static PyObject * refreshScreen(PyObject * s, PyObject * args) {
 
394
    newtRefresh();
 
395
    Py_INCREF(Py_None);
 
396
    return Py_None;
 
397
}
 
398
 
 
399
static PyObject * setColor(PyObject * s, PyObject * args) {
 
400
    char * fg, * bg;
 
401
    int colorset;
 
402
    if (!PyArg_ParseTuple(args, "iss", &colorset, &fg, &bg))
 
403
        return NULL;
 
404
 
 
405
    newtSetColor(colorset, fg, bg);
 
406
 
 
407
    Py_INCREF(Py_None);
 
408
    return Py_None;
 
409
}
 
410
 
 
411
static PyObject * scaleWidget(PyObject * s, PyObject * args) {
 
412
    snackWidget * widget;
 
413
    int width, fullAmount;
 
414
 
 
415
    if (!PyArg_ParseTuple(args, "ii", &width, &fullAmount)) return NULL;
 
416
 
 
417
    widget = snackWidgetNew ();
 
418
    widget->co = newtScale(-1, -1, width, fullAmount);
 
419
 
 
420
    return (PyObject *) widget;
 
421
}
 
422
 
 
423
static PyObject * scaleSet(snackWidget * s, PyObject * args) {
 
424
    int amount;
 
425
 
 
426
    if (!PyArg_ParseTuple(args, "i", &amount)) return NULL;
 
427
 
 
428
    newtScaleSet(s->co, amount);
 
429
 
 
430
    Py_INCREF(Py_None);
 
431
    return Py_None;
 
432
}
 
433
 
 
434
static PyObject * screenSize(PyObject * s, PyObject * args) {
 
435
    int width, height;
 
436
 
 
437
    if (!PyArg_ParseTuple(args, ""))
 
438
        return NULL;
 
439
 
 
440
    newtGetScreenSize(&width, &height);
 
441
 
 
442
    return Py_BuildValue("(ii)", width, height);
 
443
}
 
444
 
 
445
static void helpCallbackMarshall(newtComponent co, void * data) {
 
446
    PyObject * args, * result;
 
447
 
 
448
    PyGILState_STATE _state = PyGILState_Ensure();
 
449
 
 
450
    args = Py_BuildValue("(O)", data);
 
451
    result = PyEval_CallObject(helpCallback.cb, args);
 
452
    Py_DECREF (args);
 
453
    Py_XDECREF(result);
 
454
 
 
455
    PyGILState_Release(_state);
 
456
 
 
457
    return;
 
458
}
 
459
 
 
460
static void suspendCallbackMarshall(void * data) {
 
461
    struct callbackStruct * scs = data;
 
462
    PyObject * args, * result;
 
463
 
 
464
    PyGILState_STATE _state = PyGILState_Ensure();
 
465
 
 
466
    if (scs->data) {
 
467
        args = Py_BuildValue("(O)", scs->data);
 
468
        result = PyEval_CallObject(scs->cb, args);
 
469
        Py_DECREF (args);
 
470
    } else
 
471
        result = PyEval_CallObject(scs->cb, NULL);
 
472
    
 
473
    if (!result) {
 
474
        PyErr_Print();
 
475
        PyErr_Clear();
 
476
    }
 
477
 
 
478
    Py_XDECREF(result);
 
479
 
 
480
    PyGILState_Release(_state);
 
481
 
 
482
    return;
 
483
}
 
484
 
 
485
static void callbackMarshall(newtComponent co, void * data) {
 
486
    struct callbackStruct * scs = data;
 
487
    PyObject * args, * result;
 
488
 
 
489
    PyGILState_STATE _state = PyGILState_Ensure();
 
490
 
 
491
    if (scs->data) {
 
492
        args = Py_BuildValue("(O)", scs->data);
 
493
        result = PyEval_CallObject(scs->cb, args);
 
494
        Py_DECREF (args);
 
495
    } else
 
496
        result = PyEval_CallObject(scs->cb, NULL);
 
497
 
 
498
    if (!result) {
 
499
        PyErr_Print();
 
500
        PyErr_Clear();
 
501
    }
 
502
    
 
503
    Py_XDECREF(result);
 
504
 
 
505
    PyGILState_Release(_state);
 
506
 
 
507
    return;
 
508
}
 
509
 
 
510
static PyObject * setSuspendCallback(PyObject * s, PyObject * args) {
 
511
    if (!PyArg_ParseTuple(args, "O|O", &suspend.cb, &suspend.data))
 
512
        return NULL;
 
513
 
 
514
    Py_INCREF (suspend.cb);
 
515
    Py_XINCREF (suspend.data);    
 
516
    
 
517
    newtSetSuspendCallback(suspendCallbackMarshall, &suspend);
 
518
 
 
519
    Py_INCREF(Py_None);
 
520
    return Py_None;
 
521
}
 
522
 
 
523
static PyObject * setHelpCallback(PyObject * s, PyObject * args) {
 
524
    if (!PyArg_ParseTuple(args, "O", &helpCallback.cb))
 
525
        return NULL;
 
526
 
 
527
    /*if (helpCallback.cb) {
 
528
        Py_DECREF (helpCallback.cb);
 
529
    }*/
 
530
 
 
531
    Py_INCREF (helpCallback.cb);
 
532
 
 
533
    newtSetHelpCallback(helpCallbackMarshall);
 
534
 
 
535
    Py_INCREF(Py_None);
 
536
    return Py_None;
 
537
}
 
538
 
 
539
static PyObject * drawRootText(PyObject * s, PyObject * args) {
 
540
    int left, top;
 
541
    char * text;
 
542
 
 
543
    if (!PyArg_ParseTuple(args, "iis", &left, &top, &text))
 
544
        return NULL;
 
545
 
 
546
    newtDrawRootText(left, top, text);
 
547
 
 
548
    Py_INCREF(Py_None);
 
549
    return Py_None;
 
550
}
 
551
 
 
552
static PyObject * doSuspend(PyObject * s, PyObject * args) {
 
553
    newtSuspend();
 
554
 
 
555
    Py_INCREF(Py_None);
 
556
    return Py_None;
 
557
}
 
558
 
 
559
static PyObject * doResume(PyObject * s, PyObject * args) {
 
560
    newtResume();
 
561
 
 
562
    Py_INCREF(Py_None);
 
563
    return Py_None;
 
564
}
 
565
 
 
566
static PyObject * popHelpLine(PyObject * s, PyObject * args) {
 
567
    newtPopHelpLine();
 
568
    Py_INCREF(Py_None);
 
569
    return Py_None;
 
570
}
 
571
 
 
572
static PyObject * pushHelpLine(PyObject * s, PyObject * args) {
 
573
    char * text;
 
574
 
 
575
    if (!PyArg_ParseTuple(args, "s", &text))
 
576
        return NULL;
 
577
 
 
578
    if (!strcmp(text, "*default*"))
 
579
        newtPushHelpLine(NULL);
 
580
    else
 
581
        newtPushHelpLine(text);
 
582
 
 
583
    Py_INCREF(Py_None);
 
584
    return Py_None;
 
585
}
 
586
 
 
587
static PyObject * reflowText(PyObject * s, PyObject * args) {
 
588
    char * text, * new;
 
589
    int width, minus = 5, plus = 5;
 
590
    int realWidth, realHeight;
 
591
    PyObject * tuple;
 
592
 
 
593
    if (!PyArg_ParseTuple(args, "si|ii", &text, &width, &minus, &plus))
 
594
        return NULL;
 
595
 
 
596
    new = newtReflowText(text, width, minus, plus, &realWidth, &realHeight);
 
597
 
 
598
    tuple = Py_BuildValue("(sii)", new, realWidth, realHeight);
 
599
    free(new);
 
600
 
 
601
    return tuple;
 
602
}
 
603
 
 
604
static PyObject * centeredWindow(PyObject * s, PyObject * args) {
 
605
    int width, height;
 
606
    char * title;
 
607
 
 
608
    if (!PyArg_ParseTuple(args, "iis", &width, &height, &title))
 
609
        return NULL;
 
610
 
 
611
    newtCenteredWindow(width, height, title);
 
612
 
 
613
    Py_INCREF(Py_None);
 
614
    return Py_None;
 
615
}
 
616
 
 
617
static PyObject * gridWrappedWindow(PyObject * s, PyObject * args) {
 
618
    snackGrid * grid;
 
619
    char * title;
 
620
    int x = -1, y = -1;
 
621
 
 
622
    if (!PyArg_ParseTuple(args, "O!s|ii", &snackGridType, &grid, &title, 
 
623
                          &x, &y))
 
624
        return NULL;
 
625
 
 
626
    if (y == -1)
 
627
        newtGridWrappedWindow(grid->grid, title);
 
628
    else
 
629
        newtGridWrappedWindowAt(grid->grid, title, x, y);
 
630
 
 
631
    Py_INCREF(Py_None);
 
632
    return Py_None;
 
633
}
 
634
 
 
635
static PyObject * openWindow(PyObject * s, PyObject * args) {
 
636
    int left, top, width, height;
 
637
    char * title;
 
638
 
 
639
    if (!PyArg_ParseTuple(args, "iiiis", &left, &top, &width, &height, &title))
 
640
        return NULL;
 
641
 
 
642
    newtOpenWindow(left, top, width, height, title);
 
643
 
 
644
    Py_INCREF(Py_None);
 
645
    return Py_None;
 
646
}
 
647
 
 
648
static PyObject * popWindow(PyObject * s, PyObject * args) {
 
649
    newtPopWindow();
 
650
    Py_INCREF(Py_None);
 
651
    return Py_None;
 
652
}
 
653
 
 
654
static PyObject * popWindowNoRefresh(PyObject * s, PyObject * args) {
 
655
    newtPopWindowNoRefresh();
 
656
    Py_INCREF(Py_None);
 
657
    return Py_None;
 
658
}
 
659
 
 
660
static PyObject * messageWindow(PyObject * s, PyObject * args) {
 
661
    char * title, * text;
 
662
    char * okbutton = "Ok";
 
663
 
 
664
    if (!PyArg_ParseTuple(args, "ss|s", &title, &text, &okbutton)) 
 
665
        return NULL;
 
666
 
 
667
    Py_BEGIN_ALLOW_THREADS
 
668
    newtWinMessage(title, okbutton, text);
 
669
    Py_END_ALLOW_THREADS
 
670
 
 
671
    Py_INCREF(Py_None);
 
672
    return Py_None;
 
673
}
 
674
 
 
675
static PyObject * choiceWindow(PyObject * s, PyObject * args) {
 
676
    char * title, * text;
 
677
    char * okbutton = "Ok";
 
678
    char * cancelbutton = "Cancel";
 
679
    int rc;
 
680
 
 
681
    if (!PyArg_ParseTuple(args, "ss|ss", &title, &text, &okbutton, 
 
682
                          &cancelbutton)) 
 
683
        return NULL;
 
684
 
 
685
    Py_BEGIN_ALLOW_THREADS
 
686
    rc = newtWinChoice(title, okbutton, cancelbutton, text);
 
687
    Py_END_ALLOW_THREADS
 
688
 
 
689
    return Py_BuildValue("i", rc);
 
690
}
 
691
 
 
692
static PyObject * ternaryWindow(PyObject * s, PyObject * args) {
 
693
    char * title, * text, * button1, * button2, * button3;
 
694
    int rc;
 
695
 
 
696
    if (!PyArg_ParseTuple(args, "sssss", &title, &text, &button1, &button2, 
 
697
                          &button3)) 
 
698
        return NULL;
 
699
 
 
700
    Py_BEGIN_ALLOW_THREADS
 
701
    rc = newtWinTernary(title, button1, button2, button3, text);
 
702
    Py_END_ALLOW_THREADS
 
703
 
 
704
    return Py_BuildValue("i", rc);
 
705
}
 
706
 
 
707
static snackWidget * buttonWidget(PyObject * s, PyObject * args) {
 
708
    snackWidget * widget;
 
709
    char * label;
 
710
 
 
711
    if (!PyArg_ParseTuple(args, "s", &label)) return NULL;
 
712
 
 
713
    widget = snackWidgetNew ();
 
714
    widget->co = newtButton(-1, -1, label);
 
715
 
 
716
    return widget;
 
717
}
 
718
 
 
719
static snackWidget * compactbuttonWidget(PyObject * s, PyObject * args) {
 
720
    snackWidget * widget;
 
721
    char * label;
 
722
 
 
723
    if (!PyArg_ParseTuple(args, "s", &label)) return NULL;
 
724
 
 
725
    widget = snackWidgetNew ();
 
726
    widget->co = newtCompactButton(-1, -1, label);
 
727
 
 
728
    return widget;
 
729
}
 
730
 
 
731
static snackWidget * labelWidget(PyObject * s, PyObject * args) {
 
732
    char * label;
 
733
    snackWidget * widget;
 
734
 
 
735
    if (!PyArg_ParseTuple(args, "s", &label)) return NULL;
 
736
 
 
737
    widget = snackWidgetNew ();
 
738
    widget->co = newtLabel(-1, -1, label);
 
739
 
 
740
    return widget;
 
741
}
 
742
 
 
743
static PyObject * widgetLabelText(snackWidget * s, PyObject * args) {
 
744
    char * label;
 
745
 
 
746
    if (!PyArg_ParseTuple(args, "s", &label)) return NULL;
 
747
 
 
748
    newtLabelSetText(s->co, label);
 
749
 
 
750
    Py_INCREF(Py_None);
 
751
    return Py_None;
 
752
}
 
753
 
 
754
static PyObject * widgetLabelSetColors(snackWidget * s, PyObject * args) {
 
755
    int colorset;
 
756
 
 
757
    if (!PyArg_ParseTuple(args, "i", &colorset)) return NULL;
 
758
 
 
759
    newtLabelSetColors(s->co, colorset);
 
760
 
 
761
    Py_INCREF(Py_None);
 
762
    return Py_None;
 
763
}
 
764
 
 
765
static PyObject * widgetTextboxText(snackWidget * s, PyObject * args) {
 
766
    char * text;
 
767
 
 
768
    if (!PyArg_ParseTuple(args, "s", &text)) return NULL;
 
769
 
 
770
    newtTextboxSetText(s->co, text);
 
771
 
 
772
    Py_INCREF(Py_None);
 
773
    return Py_None;
 
774
}
 
775
 
 
776
static PyObject * widgetTextboxHeight(snackWidget * s, PyObject * args) {
 
777
    int height;
 
778
 
 
779
    if (!PyArg_ParseTuple(args, "i", &height)) return NULL;
 
780
 
 
781
    newtTextboxSetHeight(s->co, height);
 
782
 
 
783
    Py_INCREF(Py_None);
 
784
    return Py_None;
 
785
}
 
786
 
 
787
static snackWidget * listboxWidget(PyObject * s, PyObject * args) {
 
788
    snackWidget * widget;
 
789
    int height;
 
790
    int doScroll = 0, returnExit = 0, showCursor = 0, multiple = 0, border = 0;
 
791
 
 
792
    if (!PyArg_ParseTuple(args, "i|iiiii", &height, &doScroll, &returnExit, 
 
793
                                           &showCursor, &multiple, &border))
 
794
        return NULL;
 
795
 
 
796
    widget = snackWidgetNew ();
 
797
    widget->co = newtListbox(-1, -1, height,
 
798
                             (doScroll ? NEWT_FLAG_SCROLL : 0) |
 
799
                             (returnExit ? NEWT_FLAG_RETURNEXIT : 0) |
 
800
                             (showCursor ? NEWT_FLAG_SHOWCURSOR : 0) |
 
801
                             (multiple ? NEWT_FLAG_MULTIPLE : 0) |
 
802
                             (border ? NEWT_FLAG_BORDER : 0)
 
803
                             );
 
804
    widget->anint = 1;
 
805
    
 
806
    return widget;
 
807
}
 
808
 
 
809
static snackWidget * textWidget(PyObject * s, PyObject * args) {
 
810
    char * text;
 
811
    int width, height;
 
812
    int scrollBar = 0;
 
813
    int wrap = 0;
 
814
    snackWidget * widget;
 
815
    
 
816
    if (!PyArg_ParseTuple(args, "iis|ii", &width, &height, &text, &scrollBar, &wrap))
 
817
        return NULL;
 
818
 
 
819
    widget = snackWidgetNew ();
 
820
    widget->co = newtTextbox(-1, -1, width, height,
 
821
                                (scrollBar ? NEWT_FLAG_SCROLL : 0) |
 
822
                                (wrap ? NEWT_FLAG_WRAP : 0));
 
823
    newtTextboxSetText(widget->co, text);
 
824
    
 
825
    return widget;
 
826
}
 
827
 
 
828
static snackWidget * radioButtonWidget(PyObject * s, PyObject * args) {
 
829
    snackWidget * widget, * group;
 
830
    char * text;
 
831
    int isOn;
 
832
 
 
833
    if (!PyArg_ParseTuple(args, "sOi", &text, &group, &isOn)) 
 
834
                return NULL;
 
835
 
 
836
    widget = snackWidgetNew ();
 
837
 
 
838
    if ((PyObject *) group == Py_None)
 
839
        widget->co = newtRadiobutton(-1, -1, text, isOn, NULL);
 
840
    else
 
841
        widget->co = newtRadiobutton(-1, -1, text, isOn, group->co);
 
842
 
 
843
    return widget;
 
844
}
 
845
 
 
846
static snackWidget * checkboxWidget(PyObject * s, PyObject * args) {
 
847
    snackWidget * widget;
 
848
    char * text;
 
849
    int isOn;
 
850
 
 
851
    if (!PyArg_ParseTuple(args, "si", &text, &isOn)) return NULL;
 
852
 
 
853
    widget = snackWidgetNew ();
 
854
    widget->co = newtCheckbox(-1, -1, text, isOn ? '*' : ' ', NULL, 
 
855
                                &widget->achar);
 
856
 
 
857
    return widget;
 
858
}
 
859
 
 
860
static PyObject * widgetCheckboxSetFlags(snackWidget * s, PyObject * args) {
 
861
    int flag, sense;
 
862
 
 
863
    if (!PyArg_ParseTuple(args, "ii", &flag, &sense)) return NULL;
 
864
 
 
865
    newtCheckboxSetFlags(s->co, flag, sense);
 
866
    
 
867
    Py_INCREF(Py_None);
 
868
    return Py_None;
 
869
}
 
870
 
 
871
static PyObject * widgetCheckboxSetValue(snackWidget * s, PyObject * args) {
 
872
    char *value;
 
873
 
 
874
    if (!PyArg_ParseTuple(args, "s", &value)) return NULL;
 
875
 
 
876
    newtCheckboxSetValue(s->co, *value);
 
877
    
 
878
    Py_INCREF(Py_None);
 
879
    return Py_None;
 
880
}
 
881
 
 
882
static snackWidget * entryWidget(PyObject * s, PyObject * args) {
 
883
    snackWidget * widget;
 
884
    int width;
 
885
    char * initial;
 
886
    int isHidden, isScrolled, returnExit, isPassword;
 
887
 
 
888
    if (!PyArg_ParseTuple(args, "isiiii", &width, &initial,
 
889
                          &isHidden, &isPassword, &isScrolled, &returnExit)) return NULL;
 
890
 
 
891
    widget = snackWidgetNew ();
 
892
    widget->co = newtEntry(-1, -1, initial, width,
 
893
                           (const char **) &widget->apointer, 
 
894
                           (isHidden ? NEWT_FLAG_HIDDEN : 0) |
 
895
                           (isPassword ? NEWT_FLAG_PASSWORD : 0) |
 
896
                           (returnExit ? NEWT_FLAG_RETURNEXIT : 0) |
 
897
                           (isScrolled ? NEWT_FLAG_SCROLL : 0));
 
898
 
 
899
    return widget;
 
900
}
 
901
 
 
902
static snackForm * formCreate(PyObject * s, PyObject * args) {
 
903
    snackForm * form;
 
904
    PyObject * help = Py_None;
 
905
 
 
906
    if (!PyArg_ParseTuple(args, "|O", &help)) return NULL;
 
907
 
 
908
    if (help == Py_None)
 
909
        help = NULL;
 
910
 
 
911
    form = PyObject_New(snackForm, &snackFormType);
 
912
    form->fo = newtForm(NULL, help, 0);
 
913
 
 
914
    return form;
 
915
}
 
916
 
 
917
static snackGrid * gridCreate(PyObject * s, PyObject * args) {
 
918
    int rows, cols;
 
919
    snackGrid * grid;
 
920
 
 
921
    if (!PyArg_ParseTuple(args, "ii", &cols, &rows)) return NULL;
 
922
 
 
923
    grid = PyObject_New(snackGrid, &snackGridType);
 
924
    grid->grid = newtCreateGrid(cols, rows);
 
925
 
 
926
    return grid;
 
927
}
 
928
 
 
929
static PyObject * gridPlace(snackGrid * grid, PyObject * args) {
 
930
    int x, y;
 
931
 
 
932
    if (!PyArg_ParseTuple(args, "ii", &x, &y)) return NULL;
 
933
 
 
934
    newtGridPlace(grid->grid, x, y);
 
935
 
 
936
    Py_INCREF(Py_None);
 
937
    return Py_None;
 
938
}
 
939
 
 
940
static PyObject * gridSetField(snackGrid * grid, PyObject * args) {
 
941
    snackWidget * w;
 
942
    snackGrid * g;
 
943
    int x, y;
 
944
    int pLeft = 0, pTop = 0, pRight = 0, pBottom = 0;
 
945
    int anchorFlags = 0, growFlags = 0;
 
946
 
 
947
    if (!PyArg_ParseTuple(args, "iiO|(iiii)ii", &x, &y, 
 
948
                                &w, &pLeft, &pTop, &pRight, &pBottom,
 
949
                                &anchorFlags, &growFlags)) 
 
950
        return NULL;
 
951
 
 
952
    if (Py_TYPE(w) == &snackWidgetType) {
 
953
        newtGridSetField(grid->grid, x, y, NEWT_GRID_COMPONENT,
 
954
                         w->co, pLeft, pTop, pRight, pBottom, anchorFlags, 
 
955
                         growFlags);
 
956
    } else {
 
957
        g = (snackGrid *) w;
 
958
        newtGridSetField(grid->grid, x, y, NEWT_GRID_SUBGRID,
 
959
                         g->grid, pLeft, pTop, pRight, pBottom, anchorFlags, 
 
960
                         growFlags);
 
961
    }
 
962
 
 
963
    Py_INCREF(Py_None);
 
964
    return Py_None;
 
965
}
 
966
 
 
967
static PyObject * formDraw(snackForm * s, PyObject * args) {
 
968
    newtDrawForm(s->fo);
 
969
 
 
970
    Py_INCREF(Py_None);
 
971
    return Py_None;
 
972
}
 
973
 
 
974
static PyObject * formAdd(snackForm * s, PyObject * args) {
 
975
    snackWidget * w;
 
976
    int size = PyTuple_Size(args), i;
 
977
    
 
978
    if (!size) {
 
979
        /* this is a hack, I should give an error directly */
 
980
        if (!PyArg_ParseTuple(args, "O!", &snackWidgetType, &w)) 
 
981
            return NULL;
 
982
    }
 
983
 
 
984
    for (i = 0; i < size; i++) {
 
985
        w = (snackWidget *) PyTuple_GET_ITEM(args, i);
 
986
        newtFormAddComponent(s->fo, w->co);
 
987
    }
 
988
 
 
989
    Py_INCREF(Py_None);
 
990
    return Py_None;
 
991
}
 
992
 
 
993
static PyObject * formRun(snackForm * s, PyObject * args) {
 
994
    struct newtExitStruct result;
 
995
 
 
996
    Py_BEGIN_ALLOW_THREADS
 
997
    newtFormRun(s->fo, &result);
 
998
    Py_END_ALLOW_THREADS
 
999
 
 
1000
    if (result.reason == NEWT_EXIT_HOTKEY)
 
1001
        return Py_BuildValue("(si)", "hotkey", result.u.key);
 
1002
    else if (result.reason == NEWT_EXIT_TIMER)
 
1003
        return Py_BuildValue("(si)", "timer", 0);
 
1004
    else if (result.reason == NEWT_EXIT_FDREADY)
 
1005
        return Py_BuildValue("(si)", "fdready", result.u.watch);
 
1006
    else
 
1007
        return Py_BuildValue("(si)", "widget", result.u.co);
 
1008
}
 
1009
 
 
1010
static PyObject * formHotKey(snackForm * s, PyObject * args) {
 
1011
    int key;
 
1012
 
 
1013
    if (!PyArg_ParseTuple(args, "i", &key))
 
1014
        return NULL;
 
1015
 
 
1016
    newtFormAddHotKey(s->fo, key);
 
1017
 
 
1018
    Py_INCREF(Py_None);
 
1019
    return Py_None;
 
1020
}
 
1021
 
 
1022
static PyObject * formSetTimer(snackForm * form, PyObject * args) {
 
1023
    int millisecs;
 
1024
 
 
1025
    if (!PyArg_ParseTuple(args, "i", &millisecs))
 
1026
        return NULL;
 
1027
 
 
1028
    newtFormSetTimer(form->fo, millisecs);
 
1029
 
 
1030
    Py_INCREF(Py_None);
 
1031
    return Py_None;
 
1032
}
 
1033
 
 
1034
static PyObject * formWatchFD(snackForm * form, PyObject * args) {
 
1035
    int fd, fdflags;
 
1036
 
 
1037
    if (!PyArg_ParseTuple(args, "ii", &fd, &fdflags))
 
1038
        return NULL;
 
1039
 
 
1040
    newtFormWatchFd(form->fo, fd, fdflags);
 
1041
 
 
1042
    Py_INCREF(Py_None);
 
1043
    return Py_None;
 
1044
}
 
1045
 
 
1046
static PyObject * formSetCurrent(snackForm * form, PyObject * args) {
 
1047
    snackWidget * w;
 
1048
 
 
1049
    if (!PyArg_ParseTuple(args, "O", &w))
 
1050
        return NULL;
 
1051
 
 
1052
    newtFormSetCurrent(form->fo, w->co);
 
1053
 
 
1054
    Py_INCREF(Py_None);
 
1055
    return Py_None;
 
1056
}
 
1057
 
 
1058
static PyObject * widget_get_checkboxValue(PyObject *self, void *closure)
 
1059
{
 
1060
        snackWidget *w = (snackWidget *)self;
 
1061
 
 
1062
        return Py_BuildValue("i", w->achar == ' ' ? 0 : 1);
 
1063
}
 
1064
 
 
1065
static PyObject * widget_get_radioValue(PyObject *self, void *closure)
 
1066
{
 
1067
        snackWidget *w = (snackWidget *)self;
 
1068
 
 
1069
        return Py_BuildValue("i", newtRadioGetCurrent(w->co));
 
1070
}
 
1071
 
 
1072
static void widgetDestructor(PyObject * o) {
 
1073
    snackWidget * s = (snackWidget *) o;
 
1074
    
 
1075
    Py_XDECREF (s->scs.cb);
 
1076
    Py_XDECREF (s->scs.data);
 
1077
    
 
1078
    PyObject_Free(o);
 
1079
}
 
1080
 
 
1081
static PyObject * widgetAddCallback(snackWidget * s, PyObject * args) {
 
1082
    s->scs.cb = NULL;
 
1083
    s->scs.data = NULL;
 
1084
    
 
1085
    if (!PyArg_ParseTuple(args, "O|O", &s->scs.cb, &s->scs.data))
 
1086
        return NULL;
 
1087
 
 
1088
    Py_INCREF (s->scs.cb);
 
1089
    Py_XINCREF (s->scs.data);
 
1090
    
 
1091
    newtComponentAddCallback(s->co, callbackMarshall, &s->scs);
 
1092
 
 
1093
    Py_INCREF(Py_None);
 
1094
    return Py_None;
 
1095
}
 
1096
 
 
1097
static PyObject * widgetEntrySetValue(snackWidget * s, PyObject * args) {
 
1098
    char * val;
 
1099
    int cursorAtEnd = 1;
 
1100
 
 
1101
    if (!PyArg_ParseTuple(args, "s|i", &val, &cursorAtEnd))
 
1102
        return NULL;
 
1103
 
 
1104
    newtEntrySet(s->co, val, cursorAtEnd);
 
1105
 
 
1106
    Py_INCREF(Py_None);
 
1107
    return Py_None;
 
1108
}
 
1109
 
 
1110
static PyObject * widgetEntrySetFlags(snackWidget * s, PyObject * args) {
 
1111
    int flag, sense;
 
1112
 
 
1113
    if (!PyArg_ParseTuple(args, "ii", &flag, &sense)) return NULL;
 
1114
 
 
1115
    newtEntrySetFlags(s->co, flag, sense);
 
1116
    
 
1117
    Py_INCREF(Py_None);
 
1118
    return Py_None;
 
1119
}
 
1120
 
 
1121
 
 
1122
static PyObject * widgetListboxAdd(snackWidget * s, PyObject * args) {
 
1123
    char * text;
 
1124
    
 
1125
    if (!PyArg_ParseTuple(args, "s", &text))
 
1126
        return NULL;
 
1127
 
 
1128
    newtListboxAddEntry(s->co, text, I2P(s->anint));
 
1129
 
 
1130
    return PyInt_FromLong(s->anint++);
 
1131
}
 
1132
 
 
1133
static PyObject * widgetListboxIns(snackWidget * s, PyObject * args) {
 
1134
    char * text;
 
1135
    int key;
 
1136
    
 
1137
    if (!PyArg_ParseTuple(args, "si", &text, &key))
 
1138
        return NULL;
 
1139
 
 
1140
    newtListboxInsertEntry(s->co, text, I2P(s->anint), I2P(key));
 
1141
 
 
1142
    return PyInt_FromLong(s->anint++);
 
1143
}
 
1144
 
 
1145
static PyObject * widgetListboxDel(snackWidget * s, PyObject * args) {
 
1146
    int key;
 
1147
    
 
1148
    if (!PyArg_ParseTuple(args, "i", &key))
 
1149
        return NULL;
 
1150
 
 
1151
    newtListboxDeleteEntry(s->co, I2P(key));
 
1152
 
 
1153
    Py_INCREF(Py_None);
 
1154
    return Py_None;
 
1155
}
 
1156
 
 
1157
static PyObject * widgetListboxGet(snackWidget * s, PyObject * args) {
 
1158
    if (!PyArg_ParseTuple(args, ""))
 
1159
        return NULL;
 
1160
 
 
1161
    return PyInt_FromLong((long) newtListboxGetCurrent(s->co));
 
1162
}
 
1163
 
 
1164
static PyObject * widgetListboxGetSel(snackWidget * s, PyObject * args) {
 
1165
    void ** selection;
 
1166
    int numselected;
 
1167
    int i;
 
1168
    PyObject * sel;
 
1169
 
 
1170
    if (!PyArg_ParseTuple(args, ""))
 
1171
        return NULL;
 
1172
 
 
1173
    selection = (void **) newtListboxGetSelection(s->co, &numselected);
 
1174
 
 
1175
    sel = PyList_New(0);
 
1176
 
 
1177
    if (!selection) {
 
1178
        return sel;
 
1179
    }
 
1180
 
 
1181
    sel = PyList_New(0);
 
1182
    for (i = 0; i < numselected; i++) {
 
1183
        PyList_Append(sel, PyInt_FromLong((long) selection[i]));
 
1184
    }
 
1185
    free(selection);
 
1186
 
 
1187
    return sel;
 
1188
}
 
1189
 
 
1190
static PyObject * widgetListboxSet(snackWidget * s, PyObject * args) {
 
1191
    int index;
 
1192
    
 
1193
    if (!PyArg_ParseTuple(args, "i", &index))
 
1194
        return NULL;
 
1195
 
 
1196
    newtListboxSetCurrentByKey(s->co, I2P(index));
 
1197
 
 
1198
    Py_INCREF(Py_None);
 
1199
    return Py_None;
 
1200
}
 
1201
 
 
1202
static PyObject * widgetListboxSetW(snackWidget * s, PyObject * args) {
 
1203
    int width;
 
1204
 
 
1205
    if (!PyArg_ParseTuple(args, "i", &width))
 
1206
        return NULL;
 
1207
 
 
1208
    newtListboxSetWidth(s->co, width);
 
1209
 
 
1210
    Py_INCREF(Py_None);
 
1211
    return Py_None;
 
1212
}
 
1213
 
 
1214
static PyObject * widgetListboxClear(snackWidget * s, PyObject * args) {
 
1215
  if (!PyArg_ParseTuple(args, ""))
 
1216
    return NULL;
 
1217
 
 
1218
  newtListboxClear(s->co);
 
1219
 
 
1220
  Py_INCREF(Py_None);
 
1221
  return Py_None;
 
1222
}
 
1223
 
 
1224
static void emptyDestructor(PyObject * s) {
 
1225
}
 
1226
 
 
1227
static snackWidget * checkboxTreeWidget(PyObject * s, PyObject * args, PyObject * kwargs) {
 
1228
    int height;
 
1229
    int scrollBar = 0;
 
1230
    int hide_checkbox = 0;
 
1231
    int unselectable = 0;
 
1232
    int flags;
 
1233
    snackWidget * widget;
 
1234
    const char *kw[] = {"height", "scrollbar", "hide_checkbox", "unselectable", NULL};
 
1235
    
 
1236
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|iii", (char **) kw,
 
1237
                &height, &scrollBar, &hide_checkbox, &unselectable))
 
1238
        return NULL;
 
1239
 
 
1240
    flags = (scrollBar ? NEWT_FLAG_SCROLL : 0) |
 
1241
        (hide_checkbox ? NEWT_CHECKBOXTREE_HIDE_BOX : 0) |    
 
1242
        (unselectable ? NEWT_CHECKBOXTREE_UNSELECTABLE : 0);
 
1243
 
 
1244
    widget = snackWidgetNew ();
 
1245
    widget->co = newtCheckboxTree(-1, -1, height, flags);
 
1246
 
 
1247
    widget->anint = 1;
 
1248
 
 
1249
    return widget;
 
1250
}
 
1251
 
 
1252
static PyObject * widgetCheckboxTreeAddItem(snackWidget * s, PyObject * args) {
 
1253
    char * text;
 
1254
    int selected = 0;
 
1255
    PyObject * pathList, * o;
 
1256
    int len;
 
1257
    int * path;
 
1258
    int i;
 
1259
 
 
1260
    if (!PyArg_ParseTuple(args, "sOi", &text, &pathList, &selected))
 
1261
        return NULL;
 
1262
 
 
1263
    len = PyTuple_Size(pathList);
 
1264
    path = alloca(sizeof(*path) * (len + 1));
 
1265
    for (i = 0; i < len; i++) {
 
1266
        o = PyTuple_GetItem(pathList, i);
 
1267
        path[i] = PyInt_AsLong(o);
 
1268
    }
 
1269
    path[len] = NEWT_ARG_LAST;
 
1270
 
 
1271
    newtCheckboxTreeAddArray(s->co, text, I2P(s->anint),
 
1272
                             selected ? NEWT_FLAG_SELECTED : 0, path);
 
1273
 
 
1274
    return PyInt_FromLong(s->anint++);
 
1275
}
 
1276
 
 
1277
static PyObject * widgetCheckboxTreeGetCur(snackWidget * s, PyObject * args) {
 
1278
    if (!PyArg_ParseTuple(args, ""))
 
1279
        return NULL;
 
1280
 
 
1281
    return PyInt_FromLong((long)newtCheckboxTreeGetCurrent(s->co));
 
1282
}
 
1283
 
 
1284
static PyObject * widgetCheckboxTreeSetEntry(snackWidget * s, PyObject * args) {
 
1285
    int data;
 
1286
    char *text;
 
1287
 
 
1288
    if (!PyArg_ParseTuple(args, "is", &data, &text)) return NULL;
 
1289
 
 
1290
    newtCheckboxTreeSetEntry(s->co, I2P(data), text);
 
1291
 
 
1292
    Py_INCREF(Py_None);
 
1293
    return Py_None;
 
1294
}
 
1295
 
 
1296
static PyObject * widgetCheckboxTreeSetWidth(snackWidget * s, PyObject * args) {
 
1297
    int width;
 
1298
 
 
1299
    if (!PyArg_ParseTuple(args, "i", &width))
 
1300
        return NULL;
 
1301
 
 
1302
    newtCheckboxTreeSetWidth(s->co, width);
 
1303
 
 
1304
    Py_INCREF(Py_None);
 
1305
    return Py_None;
 
1306
}
 
1307
 
 
1308
static PyObject * widgetCheckboxTreeSetCurrent(snackWidget * s, PyObject * args) {
 
1309
    int data;
 
1310
 
 
1311
    if (!PyArg_ParseTuple(args, "i", &data)) return NULL;
 
1312
 
 
1313
    newtCheckboxTreeSetCurrent(s->co, I2P(data));
 
1314
 
 
1315
    Py_INCREF(Py_None);
 
1316
    return Py_None;
 
1317
}
 
1318
 
 
1319
static PyObject * widgetCheckboxTreeSetEntryValue(snackWidget * s, PyObject * args) {
 
1320
    int data;
 
1321
    int isOn = 1;
 
1322
 
 
1323
    if (!PyArg_ParseTuple(args, "i|i", &data, &isOn)) return NULL;
 
1324
 
 
1325
    newtCheckboxTreeSetEntryValue(s->co, I2P(data),
 
1326
                                  isOn ? NEWT_CHECKBOXTREE_SELECTED :
 
1327
                                         NEWT_CHECKBOXTREE_UNSELECTED);
 
1328
 
 
1329
    Py_INCREF(Py_None);
 
1330
    return Py_None;
 
1331
}
 
1332
 
 
1333
static PyObject * widgetCheckboxTreeGetEntryValue(snackWidget * s, PyObject * args) {
 
1334
    int data;
 
1335
    int isOn = 0;
 
1336
    int isBranch = 0;
 
1337
    char selection;
 
1338
 
 
1339
    if (!PyArg_ParseTuple(args, "i", &data)) return NULL;
 
1340
 
 
1341
    selection = newtCheckboxTreeGetEntryValue(s->co, I2P(data));
 
1342
 
 
1343
    if (selection == -1) return NULL;
 
1344
 
 
1345
    switch (selection) {
 
1346
    case NEWT_CHECKBOXTREE_EXPANDED:
 
1347
        isOn = 1;
 
1348
    case NEWT_CHECKBOXTREE_COLLAPSED:
 
1349
        isBranch = 1;
 
1350
        break;
 
1351
    case NEWT_CHECKBOXTREE_UNSELECTED:
 
1352
        break;
 
1353
    default:
 
1354
        isOn = 1;
 
1355
        break;
 
1356
    }    
 
1357
    return Py_BuildValue("(ii)", isBranch, isOn);
 
1358
}
 
1359
 
 
1360
static PyObject * widgetCheckboxTreeGetSel(snackWidget * s,
 
1361
                                              PyObject * args) {
 
1362
    void ** selection;
 
1363
    int numselected;
 
1364
    int i;
 
1365
    PyObject * sel;
 
1366
 
 
1367
    if (!PyArg_ParseTuple(args, ""))
 
1368
        return NULL;
 
1369
 
 
1370
    selection = (void **) newtCheckboxTreeGetSelection(s->co, &numselected);
 
1371
 
 
1372
    sel = PyList_New(0);
 
1373
    
 
1374
    if (!selection) {
 
1375
        return sel;
 
1376
    }
 
1377
 
 
1378
    sel = PyList_New(0);
 
1379
    for (i = 0; i < numselected; i++) {
 
1380
        PyList_Append(sel, PyInt_FromLong((long) selection[i]));
 
1381
    }
 
1382
    free(selection);
 
1383
 
 
1384
    return sel;
 
1385
}
 
1386
 
 
1387
static PyObject * pywstrlen(PyObject * s, PyObject * args)
 
1388
{
 
1389
    char *str;
 
1390
    int len = -1;
 
1391
    
 
1392
    if (!PyArg_ParseTuple(args, "s|i", &str, &len)) return NULL;
 
1393
 
 
1394
    return PyInt_FromLong(wstrlen(str, len));
 
1395
}
 
1396
 
 
1397
MOD_INIT(_snack)
 
1398
{
 
1399
    PyObject * d, * m;
 
1400
 
 
1401
#if PY_MAJOR_VERSION >= 3
 
1402
    m = PyModule_Create(&moduledef);
 
1403
#else
 
1404
    m = Py_InitModule("_snack", snackModuleMethods);
 
1405
#endif
 
1406
 
 
1407
    if (! m)
 
1408
            return MOD_ERROR_VAL;
 
1409
 
 
1410
    d = PyModule_GetDict(m);
 
1411
 
 
1412
    PyDict_SetItemString(d, "ANCHOR_LEFT", PyInt_FromLong(NEWT_ANCHOR_LEFT));
 
1413
    PyDict_SetItemString(d, "ANCHOR_TOP", PyInt_FromLong(NEWT_ANCHOR_TOP));
 
1414
    PyDict_SetItemString(d, "ANCHOR_RIGHT", PyInt_FromLong(NEWT_ANCHOR_RIGHT));
 
1415
    PyDict_SetItemString(d, "ANCHOR_BOTTOM", 
 
1416
                         PyInt_FromLong(NEWT_ANCHOR_BOTTOM));
 
1417
    PyDict_SetItemString(d, "GRID_GROWX", PyInt_FromLong(NEWT_GRID_FLAG_GROWX));
 
1418
    PyDict_SetItemString(d, "GRID_GROWY", PyInt_FromLong(NEWT_GRID_FLAG_GROWY));
 
1419
 
 
1420
    PyDict_SetItemString(d, "FD_READ", PyInt_FromLong(NEWT_FD_READ));
 
1421
    PyDict_SetItemString(d, "FD_WRITE", PyInt_FromLong(NEWT_FD_WRITE));
 
1422
    PyDict_SetItemString(d, "FD_EXCEPT", PyInt_FromLong(NEWT_FD_EXCEPT));
 
1423
 
 
1424
    PyDict_SetItemString(d, "FORM_EXIT_HOTKEY", PyString_FromString("hotkey"));
 
1425
    PyDict_SetItemString(d, "FORM_EXIT_WIDGET", PyString_FromString("widget"));
 
1426
    PyDict_SetItemString(d, "FORM_EXIT_TIMER", PyString_FromString("timer"));
 
1427
    PyDict_SetItemString(d, "FORM_EXIT_FDREADY", PyString_FromString("fdready"));
 
1428
 
 
1429
    PyDict_SetItemString(d, "KEY_TAB", PyInt_FromLong(NEWT_KEY_TAB));
 
1430
    PyDict_SetItemString(d, "KEY_ENTER", PyInt_FromLong(NEWT_KEY_ENTER));
 
1431
    PyDict_SetItemString(d, "KEY_SUSPEND", PyInt_FromLong(NEWT_KEY_SUSPEND));
 
1432
    PyDict_SetItemString(d, "KEY_UP", PyInt_FromLong(NEWT_KEY_UP));
 
1433
    PyDict_SetItemString(d, "KEY_DOWN", PyInt_FromLong(NEWT_KEY_DOWN));
 
1434
    PyDict_SetItemString(d, "KEY_LEFT", PyInt_FromLong(NEWT_KEY_LEFT));
 
1435
    PyDict_SetItemString(d, "KEY_RIGHT", PyInt_FromLong(NEWT_KEY_RIGHT));
 
1436
    PyDict_SetItemString(d, "KEY_BACKSPACE", PyInt_FromLong(NEWT_KEY_BKSPC));
 
1437
    PyDict_SetItemString(d, "KEY_DELETE", PyInt_FromLong(NEWT_KEY_DELETE));
 
1438
    PyDict_SetItemString(d, "KEY_HOME", PyInt_FromLong(NEWT_KEY_HOME));
 
1439
    PyDict_SetItemString(d, "KEY_END", PyInt_FromLong(NEWT_KEY_END));
 
1440
    PyDict_SetItemString(d, "KEY_UNTAB", PyInt_FromLong(NEWT_KEY_UNTAB));
 
1441
    PyDict_SetItemString(d, "KEY_PAGEUP", PyInt_FromLong(NEWT_KEY_PGUP));
 
1442
    PyDict_SetItemString(d, "KEY_PAGEGDOWN", PyInt_FromLong(NEWT_KEY_PGDN));
 
1443
    PyDict_SetItemString(d, "KEY_INSERT", PyInt_FromLong(NEWT_KEY_INSERT));
 
1444
    PyDict_SetItemString(d, "KEY_F1", PyInt_FromLong(NEWT_KEY_F1));
 
1445
    PyDict_SetItemString(d, "KEY_F2", PyInt_FromLong(NEWT_KEY_F2));
 
1446
    PyDict_SetItemString(d, "KEY_F3", PyInt_FromLong(NEWT_KEY_F3));
 
1447
    PyDict_SetItemString(d, "KEY_F4", PyInt_FromLong(NEWT_KEY_F4));
 
1448
    PyDict_SetItemString(d, "KEY_F5", PyInt_FromLong(NEWT_KEY_F5));
 
1449
    PyDict_SetItemString(d, "KEY_F6", PyInt_FromLong(NEWT_KEY_F6));
 
1450
    PyDict_SetItemString(d, "KEY_F7", PyInt_FromLong(NEWT_KEY_F7));
 
1451
    PyDict_SetItemString(d, "KEY_F8", PyInt_FromLong(NEWT_KEY_F8));
 
1452
    PyDict_SetItemString(d, "KEY_F9", PyInt_FromLong(NEWT_KEY_F9));
 
1453
    PyDict_SetItemString(d, "KEY_F10", PyInt_FromLong(NEWT_KEY_F10));
 
1454
    PyDict_SetItemString(d, "KEY_F11", PyInt_FromLong(NEWT_KEY_F11));
 
1455
    PyDict_SetItemString(d, "KEY_F12", PyInt_FromLong(NEWT_KEY_F12));
 
1456
    PyDict_SetItemString(d, "KEY_ESC", PyInt_FromLong(NEWT_KEY_ESCAPE));
 
1457
 
 
1458
    PyDict_SetItemString(d, "FLAG_DISABLED", PyInt_FromLong(NEWT_FLAG_DISABLED));
 
1459
    PyDict_SetItemString(d, "FLAGS_SET", PyInt_FromLong(NEWT_FLAGS_SET));
 
1460
    PyDict_SetItemString(d, "FLAGS_RESET", PyInt_FromLong(NEWT_FLAGS_RESET));
 
1461
    PyDict_SetItemString(d, "FLAGS_TOGGLE", PyInt_FromLong(NEWT_FLAGS_TOGGLE));
 
1462
 
 
1463
    PyDict_SetItemString(d, "COLORSET_ROOT", PyInt_FromLong(NEWT_COLORSET_ROOT));
 
1464
    PyDict_SetItemString(d, "COLORSET_BORDER", PyInt_FromLong(NEWT_COLORSET_BORDER));
 
1465
    PyDict_SetItemString(d, "COLORSET_WINDOW", PyInt_FromLong(NEWT_COLORSET_WINDOW));
 
1466
    PyDict_SetItemString(d, "COLORSET_SHADOW", PyInt_FromLong(NEWT_COLORSET_SHADOW));
 
1467
    PyDict_SetItemString(d, "COLORSET_TITLE", PyInt_FromLong(NEWT_COLORSET_TITLE));
 
1468
    PyDict_SetItemString(d, "COLORSET_BUTTON", PyInt_FromLong(NEWT_COLORSET_BUTTON));
 
1469
    PyDict_SetItemString(d, "COLORSET_ACTBUTTON", PyInt_FromLong(NEWT_COLORSET_ACTBUTTON));
 
1470
    PyDict_SetItemString(d, "COLORSET_CHECKBOX", PyInt_FromLong(NEWT_COLORSET_CHECKBOX));
 
1471
    PyDict_SetItemString(d, "COLORSET_ACTCHECKBOX", PyInt_FromLong(NEWT_COLORSET_ACTCHECKBOX));
 
1472
    PyDict_SetItemString(d, "COLORSET_ENTRY", PyInt_FromLong(NEWT_COLORSET_ENTRY));
 
1473
    PyDict_SetItemString(d, "COLORSET_LABEL", PyInt_FromLong(NEWT_COLORSET_LABEL));
 
1474
    PyDict_SetItemString(d, "COLORSET_LISTBOX", PyInt_FromLong(NEWT_COLORSET_LISTBOX));
 
1475
    PyDict_SetItemString(d, "COLORSET_ACTLISTBOX", PyInt_FromLong(NEWT_COLORSET_ACTLISTBOX));
 
1476
    PyDict_SetItemString(d, "COLORSET_TEXTBOX", PyInt_FromLong(NEWT_COLORSET_TEXTBOX));
 
1477
    PyDict_SetItemString(d, "COLORSET_ACTTEXTBOX", PyInt_FromLong(NEWT_COLORSET_ACTTEXTBOX));
 
1478
    PyDict_SetItemString(d, "COLORSET_HELPLINE", PyInt_FromLong(NEWT_COLORSET_HELPLINE));
 
1479
    PyDict_SetItemString(d, "COLORSET_ROOTTEXT", PyInt_FromLong(NEWT_COLORSET_ROOTTEXT));
 
1480
    PyDict_SetItemString(d, "COLORSET_EMPTYSCALE", PyInt_FromLong(NEWT_COLORSET_EMPTYSCALE));
 
1481
    PyDict_SetItemString(d, "COLORSET_FULLSCALE", PyInt_FromLong(NEWT_COLORSET_FULLSCALE));
 
1482
    PyDict_SetItemString(d, "COLORSET_DISENTRY", PyInt_FromLong(NEWT_COLORSET_DISENTRY));
 
1483
    PyDict_SetItemString(d, "COLORSET_COMPACTBUTTON", PyInt_FromLong(NEWT_COLORSET_COMPACTBUTTON));
 
1484
    PyDict_SetItemString(d, "COLORSET_ACTSELLISTBOX", PyInt_FromLong(NEWT_COLORSET_ACTSELLISTBOX));
 
1485
    PyDict_SetItemString(d, "COLORSET_SELLISTBOX", PyInt_FromLong(NEWT_COLORSET_SELLISTBOX));
 
1486
 
 
1487
    return MOD_SUCCESS_VAL(m);
 
1488
}