Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Interface to the ncurses panel library |
| 3 | * |
| 4 | * Original version by Thomas Gellekum |
| 5 | */ |
| 6 | |
| 7 | /* Release Number */ |
| 8 | |
| 9 | static char *PyCursesVersion = "2.1"; |
| 10 | |
| 11 | /* Includes */ |
| 12 | |
| 13 | #include "Python.h" |
| 14 | |
| 15 | #include "py_curses.h" |
| 16 | |
| 17 | #include <panel.h> |
| 18 | |
| 19 | static PyObject *PyCursesError; |
| 20 | |
| 21 | |
| 22 | /* Utility Functions */ |
| 23 | |
| 24 | /* |
| 25 | * Check the return code from a curses function and return None |
| 26 | * or raise an exception as appropriate. |
| 27 | */ |
| 28 | |
| 29 | static PyObject * |
| 30 | PyCursesCheckERR(int code, char *fname) |
| 31 | { |
| 32 | if (code != ERR) { |
| 33 | Py_INCREF(Py_None); |
| 34 | return Py_None; |
| 35 | } else { |
| 36 | if (fname == NULL) { |
| 37 | PyErr_SetString(PyCursesError, catchall_ERR); |
| 38 | } else { |
| 39 | PyErr_Format(PyCursesError, "%s() returned ERR", fname); |
| 40 | } |
| 41 | return NULL; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /***************************************************************************** |
| 46 | The Panel Object |
| 47 | ******************************************************************************/ |
| 48 | |
| 49 | /* Definition of the panel object and panel type */ |
| 50 | |
| 51 | typedef struct { |
| 52 | PyObject_HEAD |
| 53 | PANEL *pan; |
| 54 | PyCursesWindowObject *wo; /* for reference counts */ |
| 55 | } PyCursesPanelObject; |
| 56 | |
| 57 | PyTypeObject PyCursesPanel_Type; |
| 58 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 59 | #define PyCursesPanel_Check(v) (Py_TYPE(v) == &PyCursesPanel_Type) |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 60 | |
| 61 | /* Some helper functions. The problem is that there's always a window |
| 62 | associated with a panel. To ensure that Python's GC doesn't pull |
| 63 | this window from under our feet we need to keep track of references |
| 64 | to the corresponding window object within Python. We can't use |
| 65 | dupwin(oldwin) to keep a copy of the curses WINDOW because the |
| 66 | contents of oldwin is copied only once; code like |
| 67 | |
| 68 | win = newwin(...) |
| 69 | pan = win.panel() |
| 70 | win.addstr(some_string) |
| 71 | pan.window().addstr(other_string) |
| 72 | |
| 73 | will fail. */ |
| 74 | |
| 75 | /* We keep a linked list of PyCursesPanelObjects, lop. A list should |
| 76 | suffice, I don't expect more than a handful or at most a few |
| 77 | dozens of panel objects within a typical program. */ |
| 78 | typedef struct _list_of_panels { |
| 79 | PyCursesPanelObject *po; |
| 80 | struct _list_of_panels *next; |
| 81 | } list_of_panels; |
| 82 | |
| 83 | /* list anchor */ |
| 84 | static list_of_panels *lop; |
| 85 | |
| 86 | /* Insert a new panel object into lop */ |
| 87 | static int |
| 88 | insert_lop(PyCursesPanelObject *po) |
| 89 | { |
| 90 | list_of_panels *new; |
| 91 | |
| 92 | if ((new = (list_of_panels *)malloc(sizeof(list_of_panels))) == NULL) { |
| 93 | PyErr_NoMemory(); |
| 94 | return -1; |
| 95 | } |
| 96 | new->po = po; |
| 97 | new->next = lop; |
| 98 | lop = new; |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /* Remove the panel object from lop */ |
| 103 | static void |
| 104 | remove_lop(PyCursesPanelObject *po) |
| 105 | { |
| 106 | list_of_panels *temp, *n; |
| 107 | |
| 108 | temp = lop; |
| 109 | if (temp->po == po) { |
| 110 | lop = temp->next; |
| 111 | free(temp); |
| 112 | return; |
| 113 | } |
Thomas Wouters | 47f003d | 2006-03-07 13:38:14 +0000 | [diff] [blame] | 114 | while (temp->next == NULL || temp->next->po != po) { |
| 115 | if (temp->next == NULL) { |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 116 | PyErr_SetString(PyExc_RuntimeError, |
| 117 | "remove_lop: can't find Panel Object"); |
Thomas Wouters | 47f003d | 2006-03-07 13:38:14 +0000 | [diff] [blame] | 118 | return; |
| 119 | } |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 120 | temp = temp->next; |
| 121 | } |
| 122 | n = temp->next->next; |
| 123 | free(temp->next); |
| 124 | temp->next = n; |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | /* Return the panel object that corresponds to pan */ |
| 129 | static PyCursesPanelObject * |
| 130 | find_po(PANEL *pan) |
| 131 | { |
| 132 | list_of_panels *temp; |
| 133 | for (temp = lop; temp->po->pan != pan; temp = temp->next) |
| 134 | if (temp->next == NULL) return NULL; /* not found!? */ |
| 135 | return temp->po; |
| 136 | } |
| 137 | |
| 138 | /* Function Prototype Macros - They are ugly but very, very useful. ;-) |
| 139 | |
| 140 | X - function name |
| 141 | TYPE - parameter Type |
| 142 | ERGSTR - format string for construction of the return value |
| 143 | PARSESTR - format string for argument parsing */ |
| 144 | |
| 145 | #define Panel_NoArgNoReturnFunction(X) \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 146 | static PyObject *PyCursesPanel_##X(PyCursesPanelObject *self) \ |
| 147 | { return PyCursesCheckERR(X(self->pan), # X); } |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 148 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 149 | #define Panel_NoArgTrueFalseFunction(X) \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 150 | static PyObject *PyCursesPanel_##X(PyCursesPanelObject *self) \ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 151 | { \ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 152 | if (X (self->pan) == FALSE) { Py_INCREF(Py_False); return Py_False; } \ |
| 153 | else { Py_INCREF(Py_True); return Py_True; } } |
| 154 | |
| 155 | #define Panel_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \ |
Fred Drake | 4e36d58 | 2000-12-23 05:46:23 +0000 | [diff] [blame] | 156 | static PyObject *PyCursesPanel_##X(PyCursesPanelObject *self, PyObject *args) \ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 157 | { \ |
| 158 | TYPE arg1, arg2; \ |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 159 | if (!PyArg_ParseTuple(args, PARSESTR, &arg1, &arg2)) return NULL; \ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 160 | return PyCursesCheckERR(X(self->pan, arg1, arg2), # X); } |
| 161 | |
| 162 | /* ------------- PANEL routines --------------- */ |
| 163 | |
| 164 | Panel_NoArgNoReturnFunction(bottom_panel) |
| 165 | Panel_NoArgNoReturnFunction(hide_panel) |
| 166 | Panel_NoArgNoReturnFunction(show_panel) |
| 167 | Panel_NoArgNoReturnFunction(top_panel) |
| 168 | Panel_NoArgTrueFalseFunction(panel_hidden) |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 169 | Panel_TwoArgNoReturnFunction(move_panel, int, "ii;y,x") |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 170 | |
| 171 | /* Allocation and deallocation of Panel Objects */ |
| 172 | |
| 173 | static PyObject * |
| 174 | PyCursesPanel_New(PANEL *pan, PyCursesWindowObject *wo) |
| 175 | { |
| 176 | PyCursesPanelObject *po; |
| 177 | |
| 178 | po = PyObject_NEW(PyCursesPanelObject, &PyCursesPanel_Type); |
| 179 | if (po == NULL) return NULL; |
| 180 | po->pan = pan; |
| 181 | po->wo = wo; |
| 182 | Py_INCREF(wo); |
| 183 | if (insert_lop(po) < 0) { |
| 184 | PyObject_DEL(po); |
| 185 | return NULL; |
| 186 | } |
| 187 | return (PyObject *)po; |
| 188 | } |
| 189 | |
| 190 | static void |
| 191 | PyCursesPanel_Dealloc(PyCursesPanelObject *po) |
| 192 | { |
| 193 | (void)del_panel(po->pan); |
| 194 | Py_DECREF(po->wo); |
| 195 | remove_lop(po); |
Michael W. Hudson | cf6bfe4 | 2002-01-30 15:47:34 +0000 | [diff] [blame] | 196 | PyObject_DEL(po); |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* panel_above(NULL) returns the bottom panel in the stack. To get |
| 200 | this behaviour we use curses.panel.bottom_panel(). */ |
| 201 | static PyObject * |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 202 | PyCursesPanel_above(PyCursesPanelObject *self) |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 203 | { |
| 204 | PANEL *pan; |
| 205 | PyCursesPanelObject *po; |
| 206 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 207 | pan = panel_above(self->pan); |
| 208 | |
| 209 | if (pan == NULL) { /* valid output, it means the calling panel |
| 210 | is on top of the stack */ |
| 211 | Py_INCREF(Py_None); |
| 212 | return Py_None; |
| 213 | } |
| 214 | po = find_po(pan); |
| 215 | if (po == NULL) { |
| 216 | PyErr_SetString(PyExc_RuntimeError, |
| 217 | "panel_above: can't find Panel Object"); |
| 218 | return NULL; |
| 219 | } |
| 220 | Py_INCREF(po); |
| 221 | return (PyObject *)po; |
| 222 | } |
| 223 | |
| 224 | /* panel_below(NULL) returns the top panel in the stack. To get |
Andrew M. Kuchling | ae89af9 | 2001-01-19 15:35:26 +0000 | [diff] [blame] | 225 | this behaviour we use curses.panel.top_panel(). */ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 226 | static PyObject * |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 227 | PyCursesPanel_below(PyCursesPanelObject *self) |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 228 | { |
| 229 | PANEL *pan; |
| 230 | PyCursesPanelObject *po; |
| 231 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 232 | pan = panel_below(self->pan); |
| 233 | |
| 234 | if (pan == NULL) { /* valid output, it means the calling panel |
| 235 | is on the bottom of the stack */ |
| 236 | Py_INCREF(Py_None); |
| 237 | return Py_None; |
| 238 | } |
| 239 | po = find_po(pan); |
| 240 | if (po == NULL) { |
| 241 | PyErr_SetString(PyExc_RuntimeError, |
| 242 | "panel_below: can't find Panel Object"); |
| 243 | return NULL; |
| 244 | } |
| 245 | Py_INCREF(po); |
| 246 | return (PyObject *)po; |
| 247 | } |
| 248 | |
| 249 | static PyObject * |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 250 | PyCursesPanel_window(PyCursesPanelObject *self) |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 251 | { |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 252 | Py_INCREF(self->wo); |
| 253 | return (PyObject *)self->wo; |
| 254 | } |
| 255 | |
| 256 | static PyObject * |
| 257 | PyCursesPanel_replace_panel(PyCursesPanelObject *self, PyObject *args) |
| 258 | { |
| 259 | PyCursesPanelObject *po; |
| 260 | PyCursesWindowObject *temp; |
| 261 | int rtn; |
| 262 | |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 263 | if (PyTuple_Size(args) != 1) { |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 264 | PyErr_SetString(PyExc_TypeError, "replace requires one argument"); |
| 265 | return NULL; |
| 266 | } |
| 267 | if (!PyArg_ParseTuple(args, "O!;window object", |
| 268 | &PyCursesWindow_Type, &temp)) |
| 269 | return NULL; |
| 270 | |
| 271 | po = find_po(self->pan); |
| 272 | if (po == NULL) { |
| 273 | PyErr_SetString(PyExc_RuntimeError, |
| 274 | "replace_panel: can't find Panel Object"); |
| 275 | return NULL; |
| 276 | } |
| 277 | |
| 278 | rtn = replace_panel(self->pan, temp->win); |
| 279 | if (rtn == ERR) { |
| 280 | PyErr_SetString(PyCursesError, "replace_panel() returned ERR"); |
| 281 | return NULL; |
| 282 | } |
| 283 | Py_DECREF(po->wo); |
| 284 | po->wo = temp; |
| 285 | Py_INCREF(po->wo); |
| 286 | Py_INCREF(Py_None); |
| 287 | return Py_None; |
| 288 | } |
| 289 | |
| 290 | static PyObject * |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 291 | PyCursesPanel_set_panel_userptr(PyCursesPanelObject *self, PyObject *obj) |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 292 | { |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 293 | Py_INCREF(obj); |
Martin v. Löwis | a38d916 | 2001-10-13 08:50:10 +0000 | [diff] [blame] | 294 | return PyCursesCheckERR(set_panel_userptr(self->pan, (void*)obj), |
Fred Drake | 4e36d58 | 2000-12-23 05:46:23 +0000 | [diff] [blame] | 295 | "set_panel_userptr"); |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 298 | static PyObject * |
| 299 | PyCursesPanel_userptr(PyCursesPanelObject *self) |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 300 | { |
| 301 | PyObject *obj; |
| 302 | PyCursesInitialised; |
Fred Drake | 4e36d58 | 2000-12-23 05:46:23 +0000 | [diff] [blame] | 303 | obj = (PyObject *) panel_userptr(self->pan); |
Neal Norwitz | 5e3d862 | 2006-01-09 06:24:35 +0000 | [diff] [blame] | 304 | if (obj == NULL) { |
| 305 | PyErr_SetString(PyCursesError, "no userptr set"); |
| 306 | return NULL; |
| 307 | } |
| 308 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 309 | Py_INCREF(obj); |
| 310 | return obj; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | /* Module interface */ |
| 315 | |
| 316 | static PyMethodDef PyCursesPanel_Methods[] = { |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 317 | {"above", (PyCFunction)PyCursesPanel_above, METH_NOARGS}, |
| 318 | {"below", (PyCFunction)PyCursesPanel_below, METH_NOARGS}, |
| 319 | {"bottom", (PyCFunction)PyCursesPanel_bottom_panel, METH_NOARGS}, |
| 320 | {"hidden", (PyCFunction)PyCursesPanel_panel_hidden, METH_NOARGS}, |
| 321 | {"hide", (PyCFunction)PyCursesPanel_hide_panel, METH_NOARGS}, |
| 322 | {"move", (PyCFunction)PyCursesPanel_move_panel, METH_VARARGS}, |
Neal Norwitz | 01b2694 | 2002-03-31 14:55:17 +0000 | [diff] [blame] | 323 | {"replace", (PyCFunction)PyCursesPanel_replace_panel, METH_VARARGS}, |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 324 | {"set_userptr", (PyCFunction)PyCursesPanel_set_panel_userptr, METH_O}, |
| 325 | {"show", (PyCFunction)PyCursesPanel_show_panel, METH_NOARGS}, |
| 326 | {"top", (PyCFunction)PyCursesPanel_top_panel, METH_NOARGS}, |
| 327 | {"userptr", (PyCFunction)PyCursesPanel_userptr, METH_NOARGS}, |
| 328 | {"window", (PyCFunction)PyCursesPanel_window, METH_NOARGS}, |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 329 | {NULL, NULL} /* sentinel */ |
| 330 | }; |
| 331 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 332 | /* -------------------------------------------------------*/ |
| 333 | |
| 334 | PyTypeObject PyCursesPanel_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 335 | PyVarObject_HEAD_INIT(NULL, 0) |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 336 | "_curses_panel.curses panel", /*tp_name*/ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 337 | sizeof(PyCursesPanelObject), /*tp_basicsize*/ |
| 338 | 0, /*tp_itemsize*/ |
| 339 | /* methods */ |
| 340 | (destructor)PyCursesPanel_Dealloc, /*tp_dealloc*/ |
| 341 | 0, /*tp_print*/ |
Amaury Forgeot d'Arc | 1f900f1 | 2008-07-02 22:38:47 +0000 | [diff] [blame] | 342 | 0, /*tp_getattr*/ |
| 343 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame^] | 344 | 0, /*tp_reserved*/ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 345 | 0, /*tp_repr*/ |
| 346 | 0, /*tp_as_number*/ |
| 347 | 0, /*tp_as_sequence*/ |
| 348 | 0, /*tp_as_mapping*/ |
| 349 | 0, /*tp_hash*/ |
Amaury Forgeot d'Arc | 1f900f1 | 2008-07-02 22:38:47 +0000 | [diff] [blame] | 350 | 0, /*tp_call*/ |
| 351 | 0, /*tp_str*/ |
| 352 | 0, /*tp_getattro*/ |
| 353 | 0, /*tp_setattro*/ |
| 354 | 0, /*tp_as_buffer*/ |
| 355 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 356 | 0, /*tp_doc*/ |
| 357 | 0, /*tp_traverse*/ |
| 358 | 0, /*tp_clear*/ |
| 359 | 0, /*tp_richcompare*/ |
| 360 | 0, /*tp_weaklistoffset*/ |
| 361 | 0, /*tp_iter*/ |
| 362 | 0, /*tp_iternext*/ |
| 363 | PyCursesPanel_Methods, /*tp_methods*/ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 364 | }; |
| 365 | |
| 366 | /* Wrapper for panel_above(NULL). This function returns the bottom |
| 367 | panel of the stack, so it's renamed to bottom_panel(). |
| 368 | panel.above() *requires* a panel object in the first place which |
| 369 | may be undesirable. */ |
| 370 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 371 | PyCurses_bottom_panel(PyObject *self) |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 372 | { |
| 373 | PANEL *pan; |
| 374 | PyCursesPanelObject *po; |
| 375 | |
| 376 | PyCursesInitialised; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 377 | |
| 378 | pan = panel_above(NULL); |
| 379 | |
Andrew M. Kuchling | ae89af9 | 2001-01-19 15:35:26 +0000 | [diff] [blame] | 380 | if (pan == NULL) { /* valid output, it means |
| 381 | there's no panel at all */ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 382 | Py_INCREF(Py_None); |
| 383 | return Py_None; |
| 384 | } |
| 385 | po = find_po(pan); |
| 386 | if (po == NULL) { |
| 387 | PyErr_SetString(PyExc_RuntimeError, |
| 388 | "panel_above: can't find Panel Object"); |
| 389 | return NULL; |
| 390 | } |
| 391 | Py_INCREF(po); |
| 392 | return (PyObject *)po; |
| 393 | } |
| 394 | |
| 395 | static PyObject * |
| 396 | PyCurses_new_panel(PyObject *self, PyObject *args) |
| 397 | { |
| 398 | PyCursesWindowObject *win; |
| 399 | PANEL *pan; |
| 400 | |
Fred Drake | 4e36d58 | 2000-12-23 05:46:23 +0000 | [diff] [blame] | 401 | if (!PyArg_ParseTuple(args, "O!", &PyCursesWindow_Type, &win)) |
| 402 | return NULL; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 403 | pan = new_panel(win->win); |
| 404 | if (pan == NULL) { |
| 405 | PyErr_SetString(PyCursesError, catchall_NULL); |
| 406 | return NULL; |
| 407 | } |
| 408 | return (PyObject *)PyCursesPanel_New(pan, win); |
| 409 | } |
| 410 | |
| 411 | |
| 412 | /* Wrapper for panel_below(NULL). This function returns the top panel |
| 413 | of the stack, so it's renamed to top_panel(). panel.below() |
| 414 | *requires* a panel object in the first place which may be |
| 415 | undesirable. */ |
| 416 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 417 | PyCurses_top_panel(PyObject *self) |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 418 | { |
| 419 | PANEL *pan; |
| 420 | PyCursesPanelObject *po; |
| 421 | |
| 422 | PyCursesInitialised; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 423 | |
| 424 | pan = panel_below(NULL); |
| 425 | |
| 426 | if (pan == NULL) { /* valid output, it means |
| 427 | there's no panel at all */ |
| 428 | Py_INCREF(Py_None); |
| 429 | return Py_None; |
| 430 | } |
| 431 | po = find_po(pan); |
| 432 | if (po == NULL) { |
| 433 | PyErr_SetString(PyExc_RuntimeError, |
| 434 | "panel_below: can't find Panel Object"); |
| 435 | return NULL; |
| 436 | } |
| 437 | Py_INCREF(po); |
| 438 | return (PyObject *)po; |
| 439 | } |
| 440 | |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 441 | static PyObject *PyCurses_update_panels(PyObject *self) |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 442 | { |
| 443 | PyCursesInitialised; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 444 | update_panels(); |
| 445 | Py_INCREF(Py_None); |
| 446 | return Py_None; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | /* List of functions defined in the module */ |
| 451 | |
| 452 | static PyMethodDef PyCurses_methods[] = { |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 453 | {"bottom_panel", (PyCFunction)PyCurses_bottom_panel, METH_NOARGS}, |
| 454 | {"new_panel", (PyCFunction)PyCurses_new_panel, METH_VARARGS}, |
| 455 | {"top_panel", (PyCFunction)PyCurses_top_panel, METH_NOARGS}, |
| 456 | {"update_panels", (PyCFunction)PyCurses_update_panels, METH_NOARGS}, |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 457 | {NULL, NULL} /* sentinel */ |
| 458 | }; |
| 459 | |
| 460 | /* Initialization function for the module */ |
| 461 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 462 | |
| 463 | static struct PyModuleDef _curses_panelmodule = { |
| 464 | PyModuleDef_HEAD_INIT, |
| 465 | "_curses_panel", |
| 466 | NULL, |
| 467 | -1, |
| 468 | PyCurses_methods, |
| 469 | NULL, |
| 470 | NULL, |
| 471 | NULL, |
| 472 | NULL |
| 473 | }; |
| 474 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 475 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 476 | PyInit__curses_panel(void) |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 477 | { |
| 478 | PyObject *m, *d, *v; |
| 479 | |
Fred Drake | 2174f80 | 2001-01-27 18:58:04 +0000 | [diff] [blame] | 480 | /* Initialize object type */ |
Amaury Forgeot d'Arc | 1f900f1 | 2008-07-02 22:38:47 +0000 | [diff] [blame] | 481 | if (PyType_Ready(&PyCursesPanel_Type) < 0) |
| 482 | return NULL; |
Guido van Rossum | a120ffc | 2001-01-22 15:29:14 +0000 | [diff] [blame] | 483 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 484 | import_curses(); |
Fred Drake | 2174f80 | 2001-01-27 18:58:04 +0000 | [diff] [blame] | 485 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 486 | /* Create the module and add the functions */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 487 | m = PyModule_Create(&_curses_panelmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 488 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 489 | return NULL; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 490 | d = PyModule_GetDict(m); |
| 491 | |
| 492 | /* For exception _curses_panel.error */ |
| 493 | PyCursesError = PyErr_NewException("_curses_panel.error", NULL, NULL); |
| 494 | PyDict_SetItemString(d, "error", PyCursesError); |
| 495 | |
| 496 | /* Make the version available */ |
Neal Norwitz | 53cbdaa | 2007-08-23 21:42:55 +0000 | [diff] [blame] | 497 | v = PyUnicode_FromString(PyCursesVersion); |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 498 | PyDict_SetItemString(d, "version", v); |
| 499 | PyDict_SetItemString(d, "__version__", v); |
| 500 | Py_DECREF(v); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 501 | return m; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 502 | } |