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