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