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 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 24 | static inline _curses_panelstate* |
| 25 | get_curses_panelstate(PyObject *module) |
| 26 | { |
| 27 | void *state = PyModule_GetState(module); |
| 28 | assert(state != NULL); |
| 29 | return (_curses_panelstate *)state; |
| 30 | } |
Martin v. Löwis | c838ec1 | 2012-06-14 16:00:24 +0200 | [diff] [blame] | 31 | |
Martin v. Löwis | c838ec1 | 2012-06-14 16:00:24 +0200 | [diff] [blame] | 32 | static int |
| 33 | _curses_panel_clear(PyObject *m) |
| 34 | { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 35 | Py_CLEAR(get_curses_panelstate(m)->PyCursesError); |
Martin v. Löwis | c838ec1 | 2012-06-14 16:00:24 +0200 | [diff] [blame] | 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | static int |
| 40 | _curses_panel_traverse(PyObject *m, visitproc visit, void *arg) |
| 41 | { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 42 | Py_VISIT(get_curses_panelstate(m)->PyCursesError); |
Martin v. Löwis | c838ec1 | 2012-06-14 16:00:24 +0200 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | static void |
| 47 | _curses_panel_free(void *m) |
| 48 | { |
| 49 | _curses_panel_clear((PyObject *) m); |
| 50 | } |
| 51 | |
| 52 | static struct PyModuleDef _curses_panelmodule; |
| 53 | |
| 54 | #define _curses_panelstate_global \ |
| 55 | ((_curses_panelstate *) PyModule_GetState(PyState_FindModule(&_curses_panelmodule))) |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 56 | |
| 57 | /* Utility Functions */ |
| 58 | |
| 59 | /* |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | * Check the return code from a curses function and return None |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 61 | * or raise an exception as appropriate. |
| 62 | */ |
| 63 | |
| 64 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 65 | PyCursesCheckERR(int code, const char *fname) |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 66 | { |
| 67 | if (code != ERR) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 68 | Py_RETURN_NONE; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 69 | } else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 70 | if (fname == NULL) { |
Martin v. Löwis | c838ec1 | 2012-06-14 16:00:24 +0200 | [diff] [blame] | 71 | PyErr_SetString(_curses_panelstate_global->PyCursesError, catchall_ERR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | } else { |
Martin v. Löwis | c838ec1 | 2012-06-14 16:00:24 +0200 | [diff] [blame] | 73 | PyErr_Format(_curses_panelstate_global->PyCursesError, "%s() returned ERR", fname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | } |
| 75 | return NULL; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | /***************************************************************************** |
| 80 | The Panel Object |
| 81 | ******************************************************************************/ |
| 82 | |
| 83 | /* Definition of the panel object and panel type */ |
| 84 | |
| 85 | typedef struct { |
| 86 | PyObject_HEAD |
| 87 | PANEL *pan; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | PyCursesWindowObject *wo; /* for reference counts */ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 89 | } PyCursesPanelObject; |
| 90 | |
Martin v. Löwis | bc07cb8 | 2012-06-14 16:01:23 +0200 | [diff] [blame] | 91 | #define PyCursesPanel_Check(v) \ |
Dong-hee Na | 1b55b65 | 2020-02-17 19:09:15 +0900 | [diff] [blame] | 92 | Py_IS_TYPE(v, _curses_panelstate_global->PyCursesPanel_Type) |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 93 | |
| 94 | /* Some helper functions. The problem is that there's always a window |
| 95 | associated with a panel. To ensure that Python's GC doesn't pull |
| 96 | this window from under our feet we need to keep track of references |
| 97 | to the corresponding window object within Python. We can't use |
| 98 | dupwin(oldwin) to keep a copy of the curses WINDOW because the |
| 99 | contents of oldwin is copied only once; code like |
| 100 | |
| 101 | win = newwin(...) |
| 102 | pan = win.panel() |
| 103 | win.addstr(some_string) |
| 104 | pan.window().addstr(other_string) |
| 105 | |
| 106 | will fail. */ |
| 107 | |
| 108 | /* We keep a linked list of PyCursesPanelObjects, lop. A list should |
| 109 | suffice, I don't expect more than a handful or at most a few |
| 110 | dozens of panel objects within a typical program. */ |
| 111 | typedef struct _list_of_panels { |
| 112 | PyCursesPanelObject *po; |
| 113 | struct _list_of_panels *next; |
| 114 | } list_of_panels; |
| 115 | |
| 116 | /* list anchor */ |
| 117 | static list_of_panels *lop; |
| 118 | |
| 119 | /* Insert a new panel object into lop */ |
| 120 | static int |
| 121 | insert_lop(PyCursesPanelObject *po) |
| 122 | { |
| 123 | list_of_panels *new; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 125 | if ((new = (list_of_panels *)PyMem_Malloc(sizeof(list_of_panels))) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 126 | PyErr_NoMemory(); |
| 127 | return -1; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 128 | } |
| 129 | new->po = po; |
| 130 | new->next = lop; |
| 131 | lop = new; |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /* Remove the panel object from lop */ |
| 136 | static void |
| 137 | remove_lop(PyCursesPanelObject *po) |
| 138 | { |
| 139 | list_of_panels *temp, *n; |
| 140 | |
| 141 | temp = lop; |
| 142 | if (temp->po == po) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | lop = temp->next; |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 144 | PyMem_Free(temp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | return; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 146 | } |
Thomas Wouters | 47f003d | 2006-03-07 13:38:14 +0000 | [diff] [blame] | 147 | while (temp->next == NULL || temp->next->po != po) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | if (temp->next == NULL) { |
| 149 | PyErr_SetString(PyExc_RuntimeError, |
| 150 | "remove_lop: can't find Panel Object"); |
| 151 | return; |
| 152 | } |
| 153 | temp = temp->next; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 154 | } |
| 155 | n = temp->next->next; |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 156 | PyMem_Free(temp->next); |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 157 | temp->next = n; |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | /* Return the panel object that corresponds to pan */ |
| 162 | static PyCursesPanelObject * |
| 163 | find_po(PANEL *pan) |
| 164 | { |
| 165 | list_of_panels *temp; |
| 166 | for (temp = lop; temp->po->pan != pan; temp = temp->next) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | if (temp->next == NULL) return NULL; /* not found!? */ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 168 | return temp->po; |
| 169 | } |
| 170 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 171 | /*[clinic input] |
| 172 | module _curses_panel |
| 173 | class _curses_panel.panel "PyCursesPanelObject *" "&PyCursesPanel_Type" |
| 174 | [clinic start generated code]*/ |
| 175 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=2f4ef263ca850a31]*/ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 176 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 177 | #include "clinic/_curses_panel.c.h" |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 178 | |
| 179 | /* ------------- PANEL routines --------------- */ |
| 180 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 181 | /*[clinic input] |
| 182 | _curses_panel.panel.bottom |
| 183 | |
| 184 | Push the panel to the bottom of the stack. |
| 185 | [clinic start generated code]*/ |
| 186 | |
| 187 | static PyObject * |
| 188 | _curses_panel_panel_bottom_impl(PyCursesPanelObject *self) |
| 189 | /*[clinic end generated code: output=7aa7d14d7e1d1ce6 input=b6c920c071b61e2e]*/ |
| 190 | { |
| 191 | return PyCursesCheckERR(bottom_panel(self->pan), "bottom"); |
| 192 | } |
| 193 | |
| 194 | /*[clinic input] |
| 195 | _curses_panel.panel.hide |
| 196 | |
| 197 | Hide the panel. |
| 198 | |
| 199 | This does not delete the object, it just makes the window on screen invisible. |
| 200 | [clinic start generated code]*/ |
| 201 | |
| 202 | static PyObject * |
| 203 | _curses_panel_panel_hide_impl(PyCursesPanelObject *self) |
| 204 | /*[clinic end generated code: output=a7bbbd523e1eab49 input=f6ab884e99386118]*/ |
| 205 | { |
| 206 | return PyCursesCheckERR(hide_panel(self->pan), "hide"); |
| 207 | } |
| 208 | |
| 209 | /*[clinic input] |
| 210 | _curses_panel.panel.show |
| 211 | |
| 212 | Display the panel (which might have been hidden). |
| 213 | [clinic start generated code]*/ |
| 214 | |
| 215 | static PyObject * |
| 216 | _curses_panel_panel_show_impl(PyCursesPanelObject *self) |
| 217 | /*[clinic end generated code: output=6b4553ab45c97769 input=57b167bbefaa3755]*/ |
| 218 | { |
| 219 | return PyCursesCheckERR(show_panel(self->pan), "show"); |
| 220 | } |
| 221 | |
| 222 | /*[clinic input] |
| 223 | _curses_panel.panel.top |
| 224 | |
| 225 | Push panel to the top of the stack. |
| 226 | [clinic start generated code]*/ |
| 227 | |
| 228 | static PyObject * |
| 229 | _curses_panel_panel_top_impl(PyCursesPanelObject *self) |
| 230 | /*[clinic end generated code: output=0f5f2f8cdd2d1777 input=be33975ec3ca0e9a]*/ |
| 231 | { |
| 232 | return PyCursesCheckERR(top_panel(self->pan), "top"); |
| 233 | } |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 234 | |
| 235 | /* Allocation and deallocation of Panel Objects */ |
| 236 | |
| 237 | static PyObject * |
| 238 | PyCursesPanel_New(PANEL *pan, PyCursesWindowObject *wo) |
| 239 | { |
| 240 | PyCursesPanelObject *po; |
| 241 | |
Victor Stinner | 9205520 | 2020-04-08 00:38:15 +0200 | [diff] [blame^] | 242 | po = PyObject_New(PyCursesPanelObject, |
Martin v. Löwis | bc07cb8 | 2012-06-14 16:01:23 +0200 | [diff] [blame] | 243 | (PyTypeObject *)(_curses_panelstate_global)->PyCursesPanel_Type); |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 244 | if (po == NULL) return NULL; |
| 245 | po->pan = pan; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 246 | if (insert_lop(po) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | po->wo = NULL; |
| 248 | Py_DECREF(po); |
| 249 | return NULL; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 250 | } |
Victor Stinner | a761227 | 2010-03-03 21:56:53 +0000 | [diff] [blame] | 251 | po->wo = wo; |
| 252 | Py_INCREF(wo); |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 253 | return (PyObject *)po; |
| 254 | } |
| 255 | |
| 256 | static void |
| 257 | PyCursesPanel_Dealloc(PyCursesPanelObject *po) |
| 258 | { |
Eddie Elizondo | 364f0b0 | 2019-03-27 07:52:18 -0400 | [diff] [blame] | 259 | PyObject *tp, *obj; |
| 260 | |
| 261 | tp = (PyObject *) Py_TYPE(po); |
| 262 | obj = (PyObject *) panel_userptr(po->pan); |
Serhiy Storchaka | df40b62 | 2016-05-09 00:11:59 +0300 | [diff] [blame] | 263 | if (obj) { |
| 264 | (void)set_panel_userptr(po->pan, NULL); |
| 265 | Py_DECREF(obj); |
| 266 | } |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 267 | (void)del_panel(po->pan); |
Victor Stinner | a761227 | 2010-03-03 21:56:53 +0000 | [diff] [blame] | 268 | if (po->wo != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | Py_DECREF(po->wo); |
| 270 | remove_lop(po); |
Victor Stinner | a761227 | 2010-03-03 21:56:53 +0000 | [diff] [blame] | 271 | } |
Michael W. Hudson | cf6bfe4 | 2002-01-30 15:47:34 +0000 | [diff] [blame] | 272 | PyObject_DEL(po); |
Eddie Elizondo | 364f0b0 | 2019-03-27 07:52:18 -0400 | [diff] [blame] | 273 | Py_DECREF(tp); |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* panel_above(NULL) returns the bottom panel in the stack. To get |
| 277 | this behaviour we use curses.panel.bottom_panel(). */ |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 278 | /*[clinic input] |
| 279 | _curses_panel.panel.above |
| 280 | |
| 281 | Return the panel above the current panel. |
| 282 | [clinic start generated code]*/ |
| 283 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 284 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 285 | _curses_panel_panel_above_impl(PyCursesPanelObject *self) |
| 286 | /*[clinic end generated code: output=70ac06d25fd3b4da input=c059994022976788]*/ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 287 | { |
| 288 | PANEL *pan; |
| 289 | PyCursesPanelObject *po; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 291 | pan = panel_above(self->pan); |
| 292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 | if (pan == NULL) { /* valid output, it means the calling panel |
| 294 | is on top of the stack */ |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 295 | Py_RETURN_NONE; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 296 | } |
| 297 | po = find_po(pan); |
| 298 | if (po == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | PyErr_SetString(PyExc_RuntimeError, |
| 300 | "panel_above: can't find Panel Object"); |
| 301 | return NULL; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 302 | } |
| 303 | Py_INCREF(po); |
| 304 | return (PyObject *)po; |
| 305 | } |
| 306 | |
| 307 | /* 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] | 308 | this behaviour we use curses.panel.top_panel(). */ |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 309 | /*[clinic input] |
| 310 | _curses_panel.panel.below |
| 311 | |
| 312 | Return the panel below the current panel. |
| 313 | [clinic start generated code]*/ |
| 314 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 315 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 316 | _curses_panel_panel_below_impl(PyCursesPanelObject *self) |
| 317 | /*[clinic end generated code: output=282861122e06e3de input=cc08f61936d297c6]*/ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 318 | { |
| 319 | PANEL *pan; |
| 320 | PyCursesPanelObject *po; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 322 | pan = panel_below(self->pan); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | |
| 324 | if (pan == NULL) { /* valid output, it means the calling panel |
| 325 | is on the bottom of the stack */ |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 326 | Py_RETURN_NONE; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 327 | } |
| 328 | po = find_po(pan); |
| 329 | if (po == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 330 | PyErr_SetString(PyExc_RuntimeError, |
| 331 | "panel_below: can't find Panel Object"); |
| 332 | return NULL; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 333 | } |
| 334 | Py_INCREF(po); |
| 335 | return (PyObject *)po; |
| 336 | } |
| 337 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 338 | /*[clinic input] |
| 339 | _curses_panel.panel.hidden |
| 340 | |
| 341 | Return True if the panel is hidden (not visible), False otherwise. |
| 342 | [clinic start generated code]*/ |
| 343 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 344 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 345 | _curses_panel_panel_hidden_impl(PyCursesPanelObject *self) |
| 346 | /*[clinic end generated code: output=66eebd1ab4501a71 input=453d4b4fce25e21a]*/ |
| 347 | { |
| 348 | if (panel_hidden(self->pan)) |
| 349 | Py_RETURN_TRUE; |
| 350 | else |
| 351 | Py_RETURN_FALSE; |
| 352 | } |
| 353 | |
| 354 | /*[clinic input] |
| 355 | _curses_panel.panel.move |
| 356 | |
| 357 | y: int |
| 358 | x: int |
| 359 | / |
| 360 | |
| 361 | Move the panel to the screen coordinates (y, x). |
| 362 | [clinic start generated code]*/ |
| 363 | |
| 364 | static PyObject * |
| 365 | _curses_panel_panel_move_impl(PyCursesPanelObject *self, int y, int x) |
| 366 | /*[clinic end generated code: output=d867535a89777415 input=e0b36b78acc03fba]*/ |
| 367 | { |
| 368 | return PyCursesCheckERR(move_panel(self->pan, y, x), "move_panel"); |
| 369 | } |
| 370 | |
| 371 | /*[clinic input] |
| 372 | _curses_panel.panel.window |
| 373 | |
| 374 | Return the window object associated with the panel. |
| 375 | [clinic start generated code]*/ |
| 376 | |
| 377 | static PyObject * |
| 378 | _curses_panel_panel_window_impl(PyCursesPanelObject *self) |
| 379 | /*[clinic end generated code: output=5f05940d4106b4cb input=6067353d2c307901]*/ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 380 | { |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 381 | Py_INCREF(self->wo); |
| 382 | return (PyObject *)self->wo; |
| 383 | } |
| 384 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 385 | /*[clinic input] |
| 386 | _curses_panel.panel.replace |
| 387 | |
| 388 | win: object(type="PyCursesWindowObject *", subclass_of="&PyCursesWindow_Type") |
| 389 | / |
| 390 | |
| 391 | Change the window associated with the panel to the window win. |
| 392 | [clinic start generated code]*/ |
| 393 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 394 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 395 | _curses_panel_panel_replace_impl(PyCursesPanelObject *self, |
| 396 | PyCursesWindowObject *win) |
| 397 | /*[clinic end generated code: output=2253a95f7b287255 input=4b1c4283987d9dfa]*/ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 398 | { |
| 399 | PyCursesPanelObject *po; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 400 | int rtn; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 401 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 402 | po = find_po(self->pan); |
| 403 | if (po == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | PyErr_SetString(PyExc_RuntimeError, |
| 405 | "replace_panel: can't find Panel Object"); |
| 406 | return NULL; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 409 | rtn = replace_panel(self->pan, win->win); |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 410 | if (rtn == ERR) { |
Martin v. Löwis | c838ec1 | 2012-06-14 16:00:24 +0200 | [diff] [blame] | 411 | PyErr_SetString(_curses_panelstate_global->PyCursesError, "replace_panel() returned ERR"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | return NULL; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 413 | } |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 414 | Py_INCREF(win); |
| 415 | Py_SETREF(po->wo, win); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 416 | Py_RETURN_NONE; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 419 | /*[clinic input] |
| 420 | _curses_panel.panel.set_userptr |
| 421 | |
| 422 | obj: object |
| 423 | / |
| 424 | |
animalize | 463572c | 2019-02-25 07:18:48 +0800 | [diff] [blame] | 425 | Set the panel's user pointer to obj. |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 426 | [clinic start generated code]*/ |
| 427 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 428 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 429 | _curses_panel_panel_set_userptr(PyCursesPanelObject *self, PyObject *obj) |
animalize | 463572c | 2019-02-25 07:18:48 +0800 | [diff] [blame] | 430 | /*[clinic end generated code: output=6fb145b3af88cf4a input=d2c6a9dbefabbf39]*/ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 431 | { |
Andrew Kuchling | 53e5ea7 | 2013-06-15 14:04:04 -0400 | [diff] [blame] | 432 | PyObject *oldobj; |
Andrew Kuchling | 9290dd1 | 2013-06-22 14:50:56 -0400 | [diff] [blame] | 433 | int rc; |
Andrew Kuchling | 53e5ea7 | 2013-06-15 14:04:04 -0400 | [diff] [blame] | 434 | PyCursesInitialised; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 435 | Py_INCREF(obj); |
Andrew Kuchling | 9290dd1 | 2013-06-22 14:50:56 -0400 | [diff] [blame] | 436 | oldobj = (PyObject *) panel_userptr(self->pan); |
| 437 | rc = set_panel_userptr(self->pan, (void*)obj); |
| 438 | if (rc == ERR) { |
| 439 | /* In case of an ncurses error, decref the new object again */ |
| 440 | Py_DECREF(obj); |
| 441 | } |
| 442 | Py_XDECREF(oldobj); |
| 443 | return PyCursesCheckERR(rc, "set_panel_userptr"); |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 446 | /*[clinic input] |
| 447 | _curses_panel.panel.userptr |
| 448 | |
| 449 | Return the user pointer for the panel. |
| 450 | [clinic start generated code]*/ |
| 451 | |
Martin v. Löwis | c0e1671 | 2002-01-17 23:08:27 +0000 | [diff] [blame] | 452 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 453 | _curses_panel_panel_userptr_impl(PyCursesPanelObject *self) |
| 454 | /*[clinic end generated code: output=e849c307b5dc9237 input=f78b7a47aef0fd50]*/ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 455 | { |
| 456 | PyObject *obj; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 457 | PyCursesInitialised; |
Fred Drake | 4e36d58 | 2000-12-23 05:46:23 +0000 | [diff] [blame] | 458 | obj = (PyObject *) panel_userptr(self->pan); |
Neal Norwitz | 5e3d862 | 2006-01-09 06:24:35 +0000 | [diff] [blame] | 459 | if (obj == NULL) { |
Martin v. Löwis | c838ec1 | 2012-06-14 16:00:24 +0200 | [diff] [blame] | 460 | PyErr_SetString(_curses_panelstate_global->PyCursesError, "no userptr set"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | return NULL; |
Neal Norwitz | 5e3d862 | 2006-01-09 06:24:35 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 464 | Py_INCREF(obj); |
| 465 | return obj; |
| 466 | } |
| 467 | |
| 468 | |
| 469 | /* Module interface */ |
| 470 | |
| 471 | static PyMethodDef PyCursesPanel_Methods[] = { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 472 | _CURSES_PANEL_PANEL_ABOVE_METHODDEF |
| 473 | _CURSES_PANEL_PANEL_BELOW_METHODDEF |
| 474 | _CURSES_PANEL_PANEL_BOTTOM_METHODDEF |
| 475 | _CURSES_PANEL_PANEL_HIDDEN_METHODDEF |
| 476 | _CURSES_PANEL_PANEL_HIDE_METHODDEF |
| 477 | _CURSES_PANEL_PANEL_MOVE_METHODDEF |
| 478 | _CURSES_PANEL_PANEL_REPLACE_METHODDEF |
| 479 | _CURSES_PANEL_PANEL_SET_USERPTR_METHODDEF |
| 480 | _CURSES_PANEL_PANEL_SHOW_METHODDEF |
| 481 | _CURSES_PANEL_PANEL_TOP_METHODDEF |
| 482 | _CURSES_PANEL_PANEL_USERPTR_METHODDEF |
| 483 | _CURSES_PANEL_PANEL_WINDOW_METHODDEF |
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 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 487 | /* -------------------------------------------------------*/ |
| 488 | |
Martin v. Löwis | bc07cb8 | 2012-06-14 16:01:23 +0200 | [diff] [blame] | 489 | static PyType_Slot PyCursesPanel_Type_slots[] = { |
| 490 | {Py_tp_dealloc, PyCursesPanel_Dealloc}, |
| 491 | {Py_tp_methods, PyCursesPanel_Methods}, |
| 492 | {0, 0}, |
| 493 | }; |
| 494 | |
| 495 | static PyType_Spec PyCursesPanel_Type_spec = { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 496 | "_curses_panel.panel", |
Martin v. Löwis | bc07cb8 | 2012-06-14 16:01:23 +0200 | [diff] [blame] | 497 | sizeof(PyCursesPanelObject), |
| 498 | 0, |
| 499 | Py_TPFLAGS_DEFAULT, |
| 500 | PyCursesPanel_Type_slots |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 501 | }; |
| 502 | |
| 503 | /* Wrapper for panel_above(NULL). This function returns the bottom |
| 504 | panel of the stack, so it's renamed to bottom_panel(). |
| 505 | panel.above() *requires* a panel object in the first place which |
| 506 | may be undesirable. */ |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 507 | /*[clinic input] |
| 508 | _curses_panel.bottom_panel |
| 509 | |
| 510 | Return the bottom panel in the panel stack. |
| 511 | [clinic start generated code]*/ |
| 512 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 513 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 514 | _curses_panel_bottom_panel_impl(PyObject *module) |
| 515 | /*[clinic end generated code: output=3aba9f985f4c2bd0 input=634c2a8078b3d7e4]*/ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 516 | { |
| 517 | PANEL *pan; |
| 518 | PyCursesPanelObject *po; |
| 519 | |
| 520 | PyCursesInitialised; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 521 | |
| 522 | pan = panel_above(NULL); |
| 523 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 524 | if (pan == NULL) { /* valid output, it means |
| 525 | there's no panel at all */ |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 526 | Py_RETURN_NONE; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 527 | } |
| 528 | po = find_po(pan); |
| 529 | if (po == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 530 | PyErr_SetString(PyExc_RuntimeError, |
| 531 | "panel_above: can't find Panel Object"); |
| 532 | return NULL; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 533 | } |
| 534 | Py_INCREF(po); |
| 535 | return (PyObject *)po; |
| 536 | } |
| 537 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 538 | /*[clinic input] |
| 539 | _curses_panel.new_panel |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 540 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 541 | win: object(type="PyCursesWindowObject *", subclass_of="&PyCursesWindow_Type") |
| 542 | / |
| 543 | |
| 544 | Return a panel object, associating it with the given window win. |
| 545 | [clinic start generated code]*/ |
| 546 | |
| 547 | static PyObject * |
| 548 | _curses_panel_new_panel_impl(PyObject *module, PyCursesWindowObject *win) |
| 549 | /*[clinic end generated code: output=45e948e0176a9bd2 input=74d4754e0ebe4800]*/ |
| 550 | { |
| 551 | PANEL *pan = new_panel(win->win); |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 552 | if (pan == NULL) { |
Martin v. Löwis | c838ec1 | 2012-06-14 16:00:24 +0200 | [diff] [blame] | 553 | PyErr_SetString(_curses_panelstate_global->PyCursesError, catchall_NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | return NULL; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 555 | } |
| 556 | return (PyObject *)PyCursesPanel_New(pan, win); |
| 557 | } |
| 558 | |
| 559 | |
| 560 | /* Wrapper for panel_below(NULL). This function returns the top panel |
| 561 | of the stack, so it's renamed to top_panel(). panel.below() |
| 562 | *requires* a panel object in the first place which may be |
| 563 | undesirable. */ |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 564 | /*[clinic input] |
| 565 | _curses_panel.top_panel |
| 566 | |
| 567 | Return the top panel in the panel stack. |
| 568 | [clinic start generated code]*/ |
| 569 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 570 | static PyObject * |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 571 | _curses_panel_top_panel_impl(PyObject *module) |
| 572 | /*[clinic end generated code: output=86704988bea8508e input=e62d6278dba39e79]*/ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 573 | { |
| 574 | PANEL *pan; |
| 575 | PyCursesPanelObject *po; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 577 | PyCursesInitialised; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 578 | |
| 579 | pan = panel_below(NULL); |
| 580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | if (pan == NULL) { /* valid output, it means |
| 582 | there's no panel at all */ |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 583 | Py_RETURN_NONE; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 584 | } |
| 585 | po = find_po(pan); |
| 586 | if (po == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 587 | PyErr_SetString(PyExc_RuntimeError, |
| 588 | "panel_below: can't find Panel Object"); |
| 589 | return NULL; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 590 | } |
| 591 | Py_INCREF(po); |
| 592 | return (PyObject *)po; |
| 593 | } |
| 594 | |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 595 | /*[clinic input] |
| 596 | _curses_panel.update_panels |
| 597 | |
| 598 | Updates the virtual screen after changes in the panel stack. |
| 599 | |
animalize | 463572c | 2019-02-25 07:18:48 +0800 | [diff] [blame] | 600 | This does not call curses.doupdate(), so you'll have to do this yourself. |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 601 | [clinic start generated code]*/ |
| 602 | |
| 603 | static PyObject * |
| 604 | _curses_panel_update_panels_impl(PyObject *module) |
animalize | 463572c | 2019-02-25 07:18:48 +0800 | [diff] [blame] | 605 | /*[clinic end generated code: output=2f3b4c2e03d90ded input=5299624c9a708621]*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | { |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 607 | PyCursesInitialised; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 608 | update_panels(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 609 | Py_RETURN_NONE; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | |
| 613 | /* List of functions defined in the module */ |
| 614 | |
| 615 | static PyMethodDef PyCurses_methods[] = { |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 616 | _CURSES_PANEL_BOTTOM_PANEL_METHODDEF |
| 617 | _CURSES_PANEL_NEW_PANEL_METHODDEF |
| 618 | _CURSES_PANEL_TOP_PANEL_METHODDEF |
| 619 | _CURSES_PANEL_UPDATE_PANELS_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | {NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 621 | }; |
| 622 | |
| 623 | /* Initialization function for the module */ |
| 624 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 625 | |
| 626 | static struct PyModuleDef _curses_panelmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 627 | PyModuleDef_HEAD_INIT, |
| 628 | "_curses_panel", |
| 629 | NULL, |
Martin v. Löwis | c838ec1 | 2012-06-14 16:00:24 +0200 | [diff] [blame] | 630 | sizeof(_curses_panelstate), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | PyCurses_methods, |
| 632 | NULL, |
Martin v. Löwis | c838ec1 | 2012-06-14 16:00:24 +0200 | [diff] [blame] | 633 | _curses_panel_traverse, |
| 634 | _curses_panel_clear, |
| 635 | _curses_panel_free |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 636 | }; |
| 637 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 638 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 639 | PyInit__curses_panel(void) |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 640 | { |
| 641 | PyObject *m, *d, *v; |
| 642 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 643 | /* Create the module and add the functions */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 644 | m = PyModule_Create(&_curses_panelmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 645 | if (m == NULL) |
Martin v. Löwis | bc07cb8 | 2012-06-14 16:01:23 +0200 | [diff] [blame] | 646 | goto fail; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 647 | d = PyModule_GetDict(m); |
| 648 | |
Martin v. Löwis | bc07cb8 | 2012-06-14 16:01:23 +0200 | [diff] [blame] | 649 | /* Initialize object type */ |
Serhiy Storchaka | e3f1b09 | 2016-05-08 20:46:22 +0300 | [diff] [blame] | 650 | v = PyType_FromSpec(&PyCursesPanel_Type_spec); |
| 651 | if (v == NULL) |
Martin v. Löwis | bc07cb8 | 2012-06-14 16:01:23 +0200 | [diff] [blame] | 652 | goto fail; |
Serhiy Storchaka | e3f1b09 | 2016-05-08 20:46:22 +0300 | [diff] [blame] | 653 | ((PyTypeObject *)v)->tp_new = NULL; |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 654 | get_curses_panelstate(m)->PyCursesPanel_Type = v; |
Martin v. Löwis | bc07cb8 | 2012-06-14 16:01:23 +0200 | [diff] [blame] | 655 | |
| 656 | import_curses(); |
Victor Stinner | 569f364 | 2013-07-18 02:31:21 +0200 | [diff] [blame] | 657 | if (PyErr_Occurred()) |
| 658 | goto fail; |
Martin v. Löwis | bc07cb8 | 2012-06-14 16:01:23 +0200 | [diff] [blame] | 659 | |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 660 | /* For exception _curses_panel.error */ |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 661 | get_curses_panelstate(m)->PyCursesError = PyErr_NewException("_curses_panel.error", NULL, NULL); |
| 662 | PyDict_SetItemString(d, "error", get_curses_panelstate(m)->PyCursesError); |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 663 | |
| 664 | /* Make the version available */ |
Neal Norwitz | 53cbdaa | 2007-08-23 21:42:55 +0000 | [diff] [blame] | 665 | v = PyUnicode_FromString(PyCursesVersion); |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 666 | PyDict_SetItemString(d, "version", v); |
| 667 | PyDict_SetItemString(d, "__version__", v); |
| 668 | Py_DECREF(v); |
Serhiy Storchaka | b00854c | 2018-05-10 11:27:23 +0300 | [diff] [blame] | 669 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 670 | Py_INCREF(get_curses_panelstate(m)->PyCursesPanel_Type); |
| 671 | PyModule_AddObject(m, "panel", |
| 672 | (PyObject *)get_curses_panelstate(m)->PyCursesPanel_Type); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 673 | return m; |
Martin v. Löwis | bc07cb8 | 2012-06-14 16:01:23 +0200 | [diff] [blame] | 674 | fail: |
| 675 | Py_XDECREF(m); |
| 676 | return NULL; |
Andrew M. Kuchling | 7b59ed2 | 2000-12-22 21:54:12 +0000 | [diff] [blame] | 677 | } |