/Modules/_cursesmodule.c

http://unladen-swallow.googlecode.com/ · C · 2883 lines · 2343 code · 374 blank · 166 comment · 359 complexity · 17253a5759df49c249045e238fb1613f MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. * This is a curses module for Python.
  3. *
  4. * Based on prior work by Lance Ellinghaus and Oliver Andrich
  5. * Version 1.2 of this module: Copyright 1994 by Lance Ellinghouse,
  6. * Cathedral City, California Republic, United States of America.
  7. *
  8. * Version 1.5b1, heavily extended for ncurses by Oliver Andrich:
  9. * Copyright 1996,1997 by Oliver Andrich, Koblenz, Germany.
  10. *
  11. * Tidied for Python 1.6, and currently maintained by <amk@amk.ca>.
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining
  14. * a copy of this source file to use, copy, modify, merge, or publish it
  15. * subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included
  18. * in all copies or in any new file that contains a substantial portion of
  19. * this file.
  20. *
  21. * THE AUTHOR MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF
  22. * THE SOFTWARE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT
  23. * EXPRESS OR IMPLIED WARRANTY. THE AUTHOR DISCLAIMS ALL WARRANTIES
  24. * WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  25. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NON-INFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE
  27. * AUTHOR BE LIABLE TO YOU OR ANY OTHER PARTY FOR ANY SPECIAL,
  28. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  29. * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE, STRICT LIABILITY OR
  30. * ANY OTHER ACTION ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  31. * PERFORMANCE OF THIS SOFTWARE.
  32. */
  33. /* CVS: $Id: _cursesmodule.c 64406 2008-06-19 14:02:30Z andrew.kuchling $ */
  34. /*
  35. A number of SysV or ncurses functions don't have wrappers yet; if you
  36. need a given function, add it and send a patch. See
  37. http://www.python.org/dev/patches/ for instructions on how to submit
  38. patches to Python.
  39. Here's a list of currently unsupported functions:
  40. addchnstr addchstr color_set define_key
  41. del_curterm delscreen dupwin inchnstr inchstr innstr keyok
  42. mcprint mvaddchnstr mvaddchstr mvcur mvinchnstr
  43. mvinchstr mvinnstr mmvwaddchnstr mvwaddchstr
  44. mvwinchnstr mvwinchstr mvwinnstr newterm
  45. restartterm ripoffline scr_dump
  46. scr_init scr_restore scr_set scrl set_curterm set_term setterm
  47. tgetent tgetflag tgetnum tgetstr tgoto timeout tputs
  48. vidattr vidputs waddchnstr waddchstr
  49. wcolor_set winchnstr winchstr winnstr wmouse_trafo wscrl
  50. Low-priority:
  51. slk_attr slk_attr_off slk_attr_on slk_attr_set slk_attroff
  52. slk_attron slk_attrset slk_clear slk_color slk_init slk_label
  53. slk_noutrefresh slk_refresh slk_restore slk_set slk_touch
  54. Menu extension (ncurses and probably SYSV):
  55. current_item free_item free_menu item_count item_description
  56. item_index item_init item_name item_opts item_opts_off
  57. item_opts_on item_term item_userptr item_value item_visible
  58. menu_back menu_driver menu_fore menu_format menu_grey
  59. menu_init menu_items menu_mark menu_opts menu_opts_off
  60. menu_opts_on menu_pad menu_pattern menu_request_by_name
  61. menu_request_name menu_spacing menu_sub menu_term menu_userptr
  62. menu_win new_item new_menu pos_menu_cursor post_menu
  63. scale_menu set_current_item set_item_init set_item_opts
  64. set_item_term set_item_userptr set_item_value set_menu_back
  65. set_menu_fore set_menu_format set_menu_grey set_menu_init
  66. set_menu_items set_menu_mark set_menu_opts set_menu_pad
  67. set_menu_pattern set_menu_spacing set_menu_sub set_menu_term
  68. set_menu_userptr set_menu_win set_top_row top_row unpost_menu
  69. Form extension (ncurses and probably SYSV):
  70. current_field data_ahead data_behind dup_field
  71. dynamic_fieldinfo field_arg field_back field_buffer
  72. field_count field_fore field_index field_info field_init
  73. field_just field_opts field_opts_off field_opts_on field_pad
  74. field_status field_term field_type field_userptr form_driver
  75. form_fields form_init form_opts form_opts_off form_opts_on
  76. form_page form_request_by_name form_request_name form_sub
  77. form_term form_userptr form_win free_field free_form
  78. link_field link_fieldtype move_field new_field new_form
  79. new_page pos_form_cursor post_form scale_form
  80. set_current_field set_field_back set_field_buffer
  81. set_field_fore set_field_init set_field_just set_field_opts
  82. set_field_pad set_field_status set_field_term set_field_type
  83. set_field_userptr set_fieldtype_arg set_fieldtype_choice
  84. set_form_fields set_form_init set_form_opts set_form_page
  85. set_form_sub set_form_term set_form_userptr set_form_win
  86. set_max_field set_new_page unpost_form
  87. */
  88. /* Release Number */
  89. char *PyCursesVersion = "2.2";
  90. /* Includes */
  91. #include "Python.h"
  92. #ifdef __osf__
  93. #define STRICT_SYSV_CURSES /* Don't use ncurses extensions */
  94. #endif
  95. #ifdef __hpux
  96. #define STRICT_SYSV_CURSES
  97. #endif
  98. #define CURSES_MODULE
  99. #include "py_curses.h"
  100. /* These prototypes are in <term.h>, but including this header
  101. #defines many common symbols (such as "lines") which breaks the
  102. curses module in other ways. So the code will just specify
  103. explicit prototypes here. */
  104. extern int setupterm(char *,int,int *);
  105. #ifdef __sgi
  106. #include <term.h>
  107. #endif
  108. #if !defined(HAVE_NCURSES_H) && (defined(sgi) || defined(__sun) || defined(SCO5))
  109. #define STRICT_SYSV_CURSES /* Don't use ncurses extensions */
  110. typedef chtype attr_t; /* No attr_t type is available */
  111. #endif
  112. #if defined(_AIX)
  113. #define STRICT_SYSV_CURSES
  114. #endif
  115. /* Definition of exception curses.error */
  116. static PyObject *PyCursesError;
  117. /* Tells whether setupterm() has been called to initialise terminfo. */
  118. static int initialised_setupterm = FALSE;
  119. /* Tells whether initscr() has been called to initialise curses. */
  120. static int initialised = FALSE;
  121. /* Tells whether start_color() has been called to initialise color usage. */
  122. static int initialisedcolors = FALSE;
  123. /* Utility Macros */
  124. #define PyCursesSetupTermCalled \
  125. if (initialised_setupterm != TRUE) { \
  126. PyErr_SetString(PyCursesError, \
  127. "must call (at least) setupterm() first"); \
  128. return 0; }
  129. #define PyCursesInitialised \
  130. if (initialised != TRUE) { \
  131. PyErr_SetString(PyCursesError, \
  132. "must call initscr() first"); \
  133. return 0; }
  134. #define PyCursesInitialisedColor \
  135. if (initialisedcolors != TRUE) { \
  136. PyErr_SetString(PyCursesError, \
  137. "must call start_color() first"); \
  138. return 0; }
  139. #ifndef MIN
  140. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  141. #endif
  142. /* Utility Functions */
  143. /*
  144. * Check the return code from a curses function and return None
  145. * or raise an exception as appropriate. These are exported using the
  146. * CObject API.
  147. */
  148. static PyObject *
  149. PyCursesCheckERR(int code, char *fname)
  150. {
  151. if (code != ERR) {
  152. Py_INCREF(Py_None);
  153. return Py_None;
  154. } else {
  155. if (fname == NULL) {
  156. PyErr_SetString(PyCursesError, catchall_ERR);
  157. } else {
  158. PyErr_Format(PyCursesError, "%s() returned ERR", fname);
  159. }
  160. return NULL;
  161. }
  162. }
  163. static int
  164. PyCurses_ConvertToChtype(PyObject *obj, chtype *ch)
  165. {
  166. if (PyInt_Check(obj)) {
  167. *ch = (chtype) PyInt_AsLong(obj);
  168. } else if(PyString_Check(obj)
  169. && (PyString_Size(obj) == 1)) {
  170. *ch = (chtype) *PyString_AsString(obj);
  171. } else {
  172. return 0;
  173. }
  174. return 1;
  175. }
  176. /* Function versions of the 3 functions for tested whether curses has been
  177. initialised or not. */
  178. static int func_PyCursesSetupTermCalled(void)
  179. {
  180. PyCursesSetupTermCalled;
  181. return 1;
  182. }
  183. static int func_PyCursesInitialised(void)
  184. {
  185. PyCursesInitialised;
  186. return 1;
  187. }
  188. static int func_PyCursesInitialisedColor(void)
  189. {
  190. PyCursesInitialisedColor;
  191. return 1;
  192. }
  193. /*****************************************************************************
  194. The Window Object
  195. ******************************************************************************/
  196. /* Definition of the window type */
  197. PyTypeObject PyCursesWindow_Type;
  198. /* Function prototype macros for Window object
  199. X - function name
  200. TYPE - parameter Type
  201. ERGSTR - format string for construction of the return value
  202. PARSESTR - format string for argument parsing
  203. */
  204. #define Window_NoArgNoReturnFunction(X) \
  205. static PyObject *PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
  206. { return PyCursesCheckERR(X(self->win), # X); }
  207. #define Window_NoArgTrueFalseFunction(X) \
  208. static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self) \
  209. { \
  210. if (X (self->win) == FALSE) { Py_INCREF(Py_False); return Py_False; } \
  211. else { Py_INCREF(Py_True); return Py_True; } }
  212. #define Window_NoArgNoReturnVoidFunction(X) \
  213. static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self) \
  214. { \
  215. X(self->win); Py_INCREF(Py_None); return Py_None; }
  216. #define Window_NoArg2TupleReturnFunction(X, TYPE, ERGSTR) \
  217. static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self) \
  218. { \
  219. TYPE arg1, arg2; \
  220. X(self->win,arg1,arg2); return Py_BuildValue(ERGSTR, arg1, arg2); }
  221. #define Window_OneArgNoReturnVoidFunction(X, TYPE, PARSESTR) \
  222. static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
  223. { \
  224. TYPE arg1; \
  225. if (!PyArg_ParseTuple(args, PARSESTR, &arg1)) return NULL; \
  226. X(self->win,arg1); Py_INCREF(Py_None); return Py_None; }
  227. #define Window_OneArgNoReturnFunction(X, TYPE, PARSESTR) \
  228. static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
  229. { \
  230. TYPE arg1; \
  231. if (!PyArg_ParseTuple(args,PARSESTR, &arg1)) return NULL; \
  232. return PyCursesCheckERR(X(self->win, arg1), # X); }
  233. #define Window_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \
  234. static PyObject * PyCursesWindow_ ## X (PyCursesWindowObject *self, PyObject *args) \
  235. { \
  236. TYPE arg1, arg2; \
  237. if (!PyArg_ParseTuple(args,PARSESTR, &arg1, &arg2)) return NULL; \
  238. return PyCursesCheckERR(X(self->win, arg1, arg2), # X); }
  239. /* ------------- WINDOW routines --------------- */
  240. Window_NoArgNoReturnFunction(untouchwin)
  241. Window_NoArgNoReturnFunction(touchwin)
  242. Window_NoArgNoReturnFunction(redrawwin)
  243. Window_NoArgNoReturnFunction(winsertln)
  244. Window_NoArgNoReturnFunction(werase)
  245. Window_NoArgNoReturnFunction(wdeleteln)
  246. Window_NoArgTrueFalseFunction(is_wintouched)
  247. Window_NoArgNoReturnVoidFunction(wsyncup)
  248. Window_NoArgNoReturnVoidFunction(wsyncdown)
  249. Window_NoArgNoReturnVoidFunction(wstandend)
  250. Window_NoArgNoReturnVoidFunction(wstandout)
  251. Window_NoArgNoReturnVoidFunction(wcursyncup)
  252. Window_NoArgNoReturnVoidFunction(wclrtoeol)
  253. Window_NoArgNoReturnVoidFunction(wclrtobot)
  254. Window_NoArgNoReturnVoidFunction(wclear)
  255. Window_OneArgNoReturnVoidFunction(idcok, int, "i;True(1) or False(0)")
  256. Window_OneArgNoReturnVoidFunction(immedok, int, "i;True(1) or False(0)")
  257. Window_OneArgNoReturnVoidFunction(wtimeout, int, "i;delay")
  258. Window_NoArg2TupleReturnFunction(getyx, int, "ii")
  259. Window_NoArg2TupleReturnFunction(getbegyx, int, "ii")
  260. Window_NoArg2TupleReturnFunction(getmaxyx, int, "ii")
  261. Window_NoArg2TupleReturnFunction(getparyx, int, "ii")
  262. Window_OneArgNoReturnFunction(clearok, int, "i;True(1) or False(0)")
  263. Window_OneArgNoReturnFunction(idlok, int, "i;True(1) or False(0)")
  264. #if defined(__NetBSD__)
  265. Window_OneArgNoReturnVoidFunction(keypad, int, "i;True(1) or False(0)")
  266. #else
  267. Window_OneArgNoReturnFunction(keypad, int, "i;True(1) or False(0)")
  268. #endif
  269. Window_OneArgNoReturnFunction(leaveok, int, "i;True(1) or False(0)")
  270. #if defined(__NetBSD__)
  271. Window_OneArgNoReturnVoidFunction(nodelay, int, "i;True(1) or False(0)")
  272. #else
  273. Window_OneArgNoReturnFunction(nodelay, int, "i;True(1) or False(0)")
  274. #endif
  275. Window_OneArgNoReturnFunction(notimeout, int, "i;True(1) or False(0)")
  276. Window_OneArgNoReturnFunction(scrollok, int, "i;True(1) or False(0)")
  277. Window_OneArgNoReturnFunction(winsdelln, int, "i;nlines")
  278. Window_OneArgNoReturnFunction(syncok, int, "i;True(1) or False(0)")
  279. Window_TwoArgNoReturnFunction(mvwin, int, "ii;y,x")
  280. Window_TwoArgNoReturnFunction(mvderwin, int, "ii;y,x")
  281. Window_TwoArgNoReturnFunction(wmove, int, "ii;y,x")
  282. #ifndef STRICT_SYSV_CURSES
  283. Window_TwoArgNoReturnFunction(wresize, int, "ii;lines,columns")
  284. #endif
  285. /* Allocation and deallocation of Window Objects */
  286. static PyObject *
  287. PyCursesWindow_New(WINDOW *win)
  288. {
  289. PyCursesWindowObject *wo;
  290. wo = PyObject_NEW(PyCursesWindowObject, &PyCursesWindow_Type);
  291. if (wo == NULL) return NULL;
  292. wo->win = win;
  293. return (PyObject *)wo;
  294. }
  295. static void
  296. PyCursesWindow_Dealloc(PyCursesWindowObject *wo)
  297. {
  298. if (wo->win != stdscr) delwin(wo->win);
  299. PyObject_DEL(wo);
  300. }
  301. /* Addch, Addstr, Addnstr */
  302. static PyObject *
  303. PyCursesWindow_AddCh(PyCursesWindowObject *self, PyObject *args)
  304. {
  305. int rtn, x, y, use_xy = FALSE;
  306. PyObject *temp;
  307. chtype ch = 0;
  308. attr_t attr = A_NORMAL;
  309. long lattr;
  310. switch (PyTuple_Size(args)) {
  311. case 1:
  312. if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
  313. return NULL;
  314. break;
  315. case 2:
  316. if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &temp, &lattr))
  317. return NULL;
  318. attr = lattr;
  319. break;
  320. case 3:
  321. if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &temp))
  322. return NULL;
  323. use_xy = TRUE;
  324. break;
  325. case 4:
  326. if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr",
  327. &y, &x, &temp, &lattr))
  328. return NULL;
  329. attr = lattr;
  330. use_xy = TRUE;
  331. break;
  332. default:
  333. PyErr_SetString(PyExc_TypeError, "addch requires 1 to 4 arguments");
  334. return NULL;
  335. }
  336. if (!PyCurses_ConvertToChtype(temp, &ch)) {
  337. PyErr_SetString(PyExc_TypeError, "argument 1 or 3 must be a ch or an int");
  338. return NULL;
  339. }
  340. if (use_xy == TRUE)
  341. rtn = mvwaddch(self->win,y,x, ch | attr);
  342. else {
  343. rtn = waddch(self->win, ch | attr);
  344. }
  345. return PyCursesCheckERR(rtn, "addch");
  346. }
  347. static PyObject *
  348. PyCursesWindow_AddStr(PyCursesWindowObject *self, PyObject *args)
  349. {
  350. int rtn;
  351. int x, y;
  352. char *str;
  353. attr_t attr = A_NORMAL , attr_old = A_NORMAL;
  354. long lattr;
  355. int use_xy = FALSE, use_attr = FALSE;
  356. switch (PyTuple_Size(args)) {
  357. case 1:
  358. if (!PyArg_ParseTuple(args,"s;str", &str))
  359. return NULL;
  360. break;
  361. case 2:
  362. if (!PyArg_ParseTuple(args,"sl;str,attr", &str, &lattr))
  363. return NULL;
  364. attr = lattr;
  365. use_attr = TRUE;
  366. break;
  367. case 3:
  368. if (!PyArg_ParseTuple(args,"iis;int,int,str", &y, &x, &str))
  369. return NULL;
  370. use_xy = TRUE;
  371. break;
  372. case 4:
  373. if (!PyArg_ParseTuple(args,"iisl;int,int,str,attr", &y, &x, &str, &lattr))
  374. return NULL;
  375. attr = lattr;
  376. use_xy = use_attr = TRUE;
  377. break;
  378. default:
  379. PyErr_SetString(PyExc_TypeError, "addstr requires 1 to 4 arguments");
  380. return NULL;
  381. }
  382. if (use_attr == TRUE) {
  383. attr_old = getattrs(self->win);
  384. wattrset(self->win,attr);
  385. }
  386. if (use_xy == TRUE)
  387. rtn = mvwaddstr(self->win,y,x,str);
  388. else
  389. rtn = waddstr(self->win,str);
  390. if (use_attr == TRUE)
  391. wattrset(self->win,attr_old);
  392. return PyCursesCheckERR(rtn, "addstr");
  393. }
  394. static PyObject *
  395. PyCursesWindow_AddNStr(PyCursesWindowObject *self, PyObject *args)
  396. {
  397. int rtn, x, y, n;
  398. char *str;
  399. attr_t attr = A_NORMAL , attr_old = A_NORMAL;
  400. long lattr;
  401. int use_xy = FALSE, use_attr = FALSE;
  402. switch (PyTuple_Size(args)) {
  403. case 2:
  404. if (!PyArg_ParseTuple(args,"si;str,n", &str, &n))
  405. return NULL;
  406. break;
  407. case 3:
  408. if (!PyArg_ParseTuple(args,"sil;str,n,attr", &str, &n, &lattr))
  409. return NULL;
  410. attr = lattr;
  411. use_attr = TRUE;
  412. break;
  413. case 4:
  414. if (!PyArg_ParseTuple(args,"iisi;y,x,str,n", &y, &x, &str, &n))
  415. return NULL;
  416. use_xy = TRUE;
  417. break;
  418. case 5:
  419. if (!PyArg_ParseTuple(args,"iisil;y,x,str,n,attr", &y, &x, &str, &n, &lattr))
  420. return NULL;
  421. attr = lattr;
  422. use_xy = use_attr = TRUE;
  423. break;
  424. default:
  425. PyErr_SetString(PyExc_TypeError, "addnstr requires 2 to 5 arguments");
  426. return NULL;
  427. }
  428. if (use_attr == TRUE) {
  429. attr_old = getattrs(self->win);
  430. wattrset(self->win,attr);
  431. }
  432. if (use_xy == TRUE)
  433. rtn = mvwaddnstr(self->win,y,x,str,n);
  434. else
  435. rtn = waddnstr(self->win,str,n);
  436. if (use_attr == TRUE)
  437. wattrset(self->win,attr_old);
  438. return PyCursesCheckERR(rtn, "addnstr");
  439. }
  440. static PyObject *
  441. PyCursesWindow_Bkgd(PyCursesWindowObject *self, PyObject *args)
  442. {
  443. PyObject *temp;
  444. chtype bkgd;
  445. attr_t attr = A_NORMAL;
  446. long lattr;
  447. switch (PyTuple_Size(args)) {
  448. case 1:
  449. if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
  450. return NULL;
  451. break;
  452. case 2:
  453. if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr))
  454. return NULL;
  455. attr = lattr;
  456. break;
  457. default:
  458. PyErr_SetString(PyExc_TypeError, "bkgd requires 1 or 2 arguments");
  459. return NULL;
  460. }
  461. if (!PyCurses_ConvertToChtype(temp, &bkgd)) {
  462. PyErr_SetString(PyExc_TypeError, "argument 1 or 3 must be a ch or an int");
  463. return NULL;
  464. }
  465. return PyCursesCheckERR(wbkgd(self->win, bkgd | attr), "bkgd");
  466. }
  467. static PyObject *
  468. PyCursesWindow_AttrOff(PyCursesWindowObject *self, PyObject *args)
  469. {
  470. long lattr;
  471. if (!PyArg_ParseTuple(args,"l;attr", &lattr))
  472. return NULL;
  473. return PyCursesCheckERR(wattroff(self->win, (attr_t)lattr), "attroff");
  474. }
  475. static PyObject *
  476. PyCursesWindow_AttrOn(PyCursesWindowObject *self, PyObject *args)
  477. {
  478. long lattr;
  479. if (!PyArg_ParseTuple(args,"l;attr", &lattr))
  480. return NULL;
  481. return PyCursesCheckERR(wattron(self->win, (attr_t)lattr), "attron");
  482. }
  483. static PyObject *
  484. PyCursesWindow_AttrSet(PyCursesWindowObject *self, PyObject *args)
  485. {
  486. long lattr;
  487. if (!PyArg_ParseTuple(args,"l;attr", &lattr))
  488. return NULL;
  489. return PyCursesCheckERR(wattrset(self->win, (attr_t)lattr), "attrset");
  490. }
  491. static PyObject *
  492. PyCursesWindow_BkgdSet(PyCursesWindowObject *self, PyObject *args)
  493. {
  494. PyObject *temp;
  495. chtype bkgd;
  496. attr_t attr = A_NORMAL;
  497. long lattr;
  498. switch (PyTuple_Size(args)) {
  499. case 1:
  500. if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
  501. return NULL;
  502. break;
  503. case 2:
  504. if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr))
  505. return NULL;
  506. attr = lattr;
  507. break;
  508. default:
  509. PyErr_SetString(PyExc_TypeError, "bkgdset requires 1 or 2 arguments");
  510. return NULL;
  511. }
  512. if (!PyCurses_ConvertToChtype(temp, &bkgd)) {
  513. PyErr_SetString(PyExc_TypeError, "argument 1 must be a ch or an int");
  514. return NULL;
  515. }
  516. wbkgdset(self->win, bkgd | attr);
  517. return PyCursesCheckERR(0, "bkgdset");
  518. }
  519. static PyObject *
  520. PyCursesWindow_Border(PyCursesWindowObject *self, PyObject *args)
  521. {
  522. PyObject *temp[8];
  523. chtype ch[8];
  524. int i;
  525. /* Clear the array of parameters */
  526. for(i=0; i<8; i++) {
  527. temp[i] = NULL;
  528. ch[i] = 0;
  529. }
  530. if (!PyArg_ParseTuple(args,"|OOOOOOOO;ls,rs,ts,bs,tl,tr,bl,br",
  531. &temp[0], &temp[1], &temp[2], &temp[3],
  532. &temp[4], &temp[5], &temp[6], &temp[7]))
  533. return NULL;
  534. for(i=0; i<8; i++) {
  535. if (temp[i] != NULL && !PyCurses_ConvertToChtype(temp[i], &ch[i])) {
  536. PyErr_Format(PyExc_TypeError,
  537. "argument %i must be a ch or an int", i+1);
  538. return NULL;
  539. }
  540. }
  541. wborder(self->win,
  542. ch[0], ch[1], ch[2], ch[3],
  543. ch[4], ch[5], ch[6], ch[7]);
  544. Py_INCREF(Py_None);
  545. return Py_None;
  546. }
  547. static PyObject *
  548. PyCursesWindow_Box(PyCursesWindowObject *self, PyObject *args)
  549. {
  550. chtype ch1=0,ch2=0;
  551. switch(PyTuple_Size(args)){
  552. case 0: break;
  553. default:
  554. if (!PyArg_ParseTuple(args,"ll;vertint,horint", &ch1, &ch2))
  555. return NULL;
  556. }
  557. box(self->win,ch1,ch2);
  558. Py_INCREF(Py_None);
  559. return Py_None;
  560. }
  561. #if defined(HAVE_NCURSES_H) || defined(MVWDELCH_IS_EXPRESSION)
  562. #define py_mvwdelch mvwdelch
  563. #else
  564. int py_mvwdelch(WINDOW *w, int y, int x)
  565. {
  566. mvwdelch(w,y,x);
  567. /* On HP/UX, mvwdelch already returns. On other systems,
  568. we may well run into this return statement. */
  569. return 0;
  570. }
  571. #endif
  572. /* chgat, added by Fabian Kreutz <fabian.kreutz at gmx.net> */
  573. static PyObject *
  574. PyCursesWindow_ChgAt(PyCursesWindowObject *self, PyObject *args)
  575. {
  576. int rtn;
  577. int x, y;
  578. int num = -1;
  579. short color;
  580. attr_t attr = A_NORMAL;
  581. long lattr;
  582. int use_xy = FALSE;
  583. switch (PyTuple_Size(args)) {
  584. case 1:
  585. if (!PyArg_ParseTuple(args,"l;attr", &lattr))
  586. return NULL;
  587. attr = lattr;
  588. break;
  589. case 2:
  590. if (!PyArg_ParseTuple(args,"il;n,attr", &num, &lattr))
  591. return NULL;
  592. attr = lattr;
  593. break;
  594. case 3:
  595. if (!PyArg_ParseTuple(args,"iil;int,int,attr", &y, &x, &lattr))
  596. return NULL;
  597. attr = lattr;
  598. use_xy = TRUE;
  599. break;
  600. case 4:
  601. if (!PyArg_ParseTuple(args,"iiil;int,int,n,attr", &y, &x, &num, &lattr))
  602. return NULL;
  603. attr = lattr;
  604. use_xy = TRUE;
  605. break;
  606. default:
  607. PyErr_SetString(PyExc_TypeError, "chgat requires 1 to 4 arguments");
  608. return NULL;
  609. }
  610. color = (short)((attr >> 8) & 0xff);
  611. attr = attr - (color << 8);
  612. if (use_xy == TRUE) {
  613. rtn = mvwchgat(self->win,y,x,num,attr,color,NULL);
  614. touchline(self->win,y,1);
  615. } else {
  616. getyx(self->win,y,x);
  617. rtn = wchgat(self->win,num,attr,color,NULL);
  618. touchline(self->win,y,1);
  619. }
  620. return PyCursesCheckERR(rtn, "chgat");
  621. }
  622. static PyObject *
  623. PyCursesWindow_DelCh(PyCursesWindowObject *self, PyObject *args)
  624. {
  625. int rtn;
  626. int x, y;
  627. switch (PyTuple_Size(args)) {
  628. case 0:
  629. rtn = wdelch(self->win);
  630. break;
  631. case 2:
  632. if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x))
  633. return NULL;
  634. rtn = py_mvwdelch(self->win,y,x);
  635. break;
  636. default:
  637. PyErr_SetString(PyExc_TypeError, "delch requires 0 or 2 arguments");
  638. return NULL;
  639. }
  640. return PyCursesCheckERR(rtn, "[mv]wdelch");
  641. }
  642. static PyObject *
  643. PyCursesWindow_DerWin(PyCursesWindowObject *self, PyObject *args)
  644. {
  645. WINDOW *win;
  646. int nlines, ncols, begin_y, begin_x;
  647. nlines = 0;
  648. ncols = 0;
  649. switch (PyTuple_Size(args)) {
  650. case 2:
  651. if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
  652. return NULL;
  653. break;
  654. case 4:
  655. if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
  656. &nlines,&ncols,&begin_y,&begin_x))
  657. return NULL;
  658. break;
  659. default:
  660. PyErr_SetString(PyExc_TypeError, "derwin requires 2 or 4 arguments");
  661. return NULL;
  662. }
  663. win = derwin(self->win,nlines,ncols,begin_y,begin_x);
  664. if (win == NULL) {
  665. PyErr_SetString(PyCursesError, catchall_NULL);
  666. return NULL;
  667. }
  668. return (PyObject *)PyCursesWindow_New(win);
  669. }
  670. static PyObject *
  671. PyCursesWindow_EchoChar(PyCursesWindowObject *self, PyObject *args)
  672. {
  673. PyObject *temp;
  674. chtype ch;
  675. attr_t attr = A_NORMAL;
  676. long lattr;
  677. switch (PyTuple_Size(args)) {
  678. case 1:
  679. if (!PyArg_ParseTuple(args,"O;ch or int", &temp))
  680. return NULL;
  681. break;
  682. case 2:
  683. if (!PyArg_ParseTuple(args,"Ol;ch or int,attr", &temp, &lattr))
  684. return NULL;
  685. attr = lattr;
  686. break;
  687. default:
  688. PyErr_SetString(PyExc_TypeError, "echochar requires 1 or 2 arguments");
  689. return NULL;
  690. }
  691. if (!PyCurses_ConvertToChtype(temp, &ch)) {
  692. PyErr_SetString(PyExc_TypeError, "argument 1 must be a ch or an int");
  693. return NULL;
  694. }
  695. #ifdef WINDOW_HAS_FLAGS
  696. if (self->win->_flags & _ISPAD)
  697. return PyCursesCheckERR(pechochar(self->win, ch | attr),
  698. "echochar");
  699. else
  700. #endif
  701. return PyCursesCheckERR(wechochar(self->win, ch | attr),
  702. "echochar");
  703. }
  704. #ifdef NCURSES_MOUSE_VERSION
  705. static PyObject *
  706. PyCursesWindow_Enclose(PyCursesWindowObject *self, PyObject *args)
  707. {
  708. int x, y;
  709. if (!PyArg_ParseTuple(args,"ii;y,x", &y, &x))
  710. return NULL;
  711. return PyInt_FromLong( wenclose(self->win,y,x) );
  712. }
  713. #endif
  714. static PyObject *
  715. PyCursesWindow_GetBkgd(PyCursesWindowObject *self)
  716. {
  717. return PyInt_FromLong((long) getbkgd(self->win));
  718. }
  719. static PyObject *
  720. PyCursesWindow_GetCh(PyCursesWindowObject *self, PyObject *args)
  721. {
  722. int x, y;
  723. int rtn;
  724. switch (PyTuple_Size(args)) {
  725. case 0:
  726. Py_BEGIN_ALLOW_THREADS
  727. rtn = wgetch(self->win);
  728. Py_END_ALLOW_THREADS
  729. break;
  730. case 2:
  731. if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
  732. return NULL;
  733. Py_BEGIN_ALLOW_THREADS
  734. rtn = mvwgetch(self->win,y,x);
  735. Py_END_ALLOW_THREADS
  736. break;
  737. default:
  738. PyErr_SetString(PyExc_TypeError, "getch requires 0 or 2 arguments");
  739. return NULL;
  740. }
  741. return PyInt_FromLong((long)rtn);
  742. }
  743. static PyObject *
  744. PyCursesWindow_GetKey(PyCursesWindowObject *self, PyObject *args)
  745. {
  746. int x, y;
  747. int rtn;
  748. switch (PyTuple_Size(args)) {
  749. case 0:
  750. Py_BEGIN_ALLOW_THREADS
  751. rtn = wgetch(self->win);
  752. Py_END_ALLOW_THREADS
  753. break;
  754. case 2:
  755. if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
  756. return NULL;
  757. Py_BEGIN_ALLOW_THREADS
  758. rtn = mvwgetch(self->win,y,x);
  759. Py_END_ALLOW_THREADS
  760. break;
  761. default:
  762. PyErr_SetString(PyExc_TypeError, "getkey requires 0 or 2 arguments");
  763. return NULL;
  764. }
  765. if (rtn == ERR) {
  766. /* getch() returns ERR in nodelay mode */
  767. PyErr_SetString(PyCursesError, "no input");
  768. return NULL;
  769. } else if (rtn<=255)
  770. return Py_BuildValue("c", rtn);
  771. else
  772. #if defined(__NetBSD__)
  773. return PyString_FromString(unctrl(rtn));
  774. #else
  775. return PyString_FromString((char *)keyname(rtn));
  776. #endif
  777. }
  778. static PyObject *
  779. PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
  780. {
  781. int x, y, n;
  782. char rtn[1024]; /* This should be big enough.. I hope */
  783. int rtn2;
  784. switch (PyTuple_Size(args)) {
  785. case 0:
  786. Py_BEGIN_ALLOW_THREADS
  787. rtn2 = wgetnstr(self->win,rtn, 1023);
  788. Py_END_ALLOW_THREADS
  789. break;
  790. case 1:
  791. if (!PyArg_ParseTuple(args,"i;n", &n))
  792. return NULL;
  793. Py_BEGIN_ALLOW_THREADS
  794. rtn2 = wgetnstr(self->win,rtn,MIN(n, 1023));
  795. Py_END_ALLOW_THREADS
  796. break;
  797. case 2:
  798. if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
  799. return NULL;
  800. Py_BEGIN_ALLOW_THREADS
  801. #ifdef STRICT_SYSV_CURSES
  802. rtn2 = wmove(self->win,y,x)==ERR ? ERR : wgetnstr(self->win, rtn, 1023);
  803. #else
  804. rtn2 = mvwgetnstr(self->win,y,x,rtn, 1023);
  805. #endif
  806. Py_END_ALLOW_THREADS
  807. break;
  808. case 3:
  809. if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
  810. return NULL;
  811. #ifdef STRICT_SYSV_CURSES
  812. Py_BEGIN_ALLOW_THREADS
  813. rtn2 = wmove(self->win,y,x)==ERR ? ERR :
  814. wgetnstr(self->win, rtn, MIN(n, 1023));
  815. Py_END_ALLOW_THREADS
  816. #else
  817. Py_BEGIN_ALLOW_THREADS
  818. rtn2 = mvwgetnstr(self->win, y, x, rtn, MIN(n, 1023));
  819. Py_END_ALLOW_THREADS
  820. #endif
  821. break;
  822. default:
  823. PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 3 arguments");
  824. return NULL;
  825. }
  826. if (rtn2 == ERR)
  827. rtn[0] = 0;
  828. return PyString_FromString(rtn);
  829. }
  830. static PyObject *
  831. PyCursesWindow_Hline(PyCursesWindowObject *self, PyObject *args)
  832. {
  833. PyObject *temp;
  834. chtype ch;
  835. int n, x, y, code = OK;
  836. attr_t attr = A_NORMAL;
  837. long lattr;
  838. switch (PyTuple_Size(args)) {
  839. case 2:
  840. if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
  841. return NULL;
  842. break;
  843. case 3:
  844. if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &lattr))
  845. return NULL;
  846. attr = lattr;
  847. break;
  848. case 4:
  849. if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
  850. return NULL;
  851. code = wmove(self->win, y, x);
  852. break;
  853. case 5:
  854. if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
  855. &y, &x, &temp, &n, &lattr))
  856. return NULL;
  857. attr = lattr;
  858. code = wmove(self->win, y, x);
  859. break;
  860. default:
  861. PyErr_SetString(PyExc_TypeError, "hline requires 2 to 5 arguments");
  862. return NULL;
  863. }
  864. if (code != ERR) {
  865. if (!PyCurses_ConvertToChtype(temp, &ch)) {
  866. PyErr_SetString(PyExc_TypeError,
  867. "argument 1 or 3 must be a ch or an int");
  868. return NULL;
  869. }
  870. return PyCursesCheckERR(whline(self->win, ch | attr, n), "hline");
  871. } else
  872. return PyCursesCheckERR(code, "wmove");
  873. }
  874. static PyObject *
  875. PyCursesWindow_InsCh(PyCursesWindowObject *self, PyObject *args)
  876. {
  877. int rtn, x, y, use_xy = FALSE;
  878. PyObject *temp;
  879. chtype ch = 0;
  880. attr_t attr = A_NORMAL;
  881. long lattr;
  882. switch (PyTuple_Size(args)) {
  883. case 1:
  884. if (!PyArg_ParseTuple(args, "O;ch or int", &temp))
  885. return NULL;
  886. break;
  887. case 2:
  888. if (!PyArg_ParseTuple(args, "Ol;ch or int,attr", &temp, &lattr))
  889. return NULL;
  890. attr = lattr;
  891. break;
  892. case 3:
  893. if (!PyArg_ParseTuple(args,"iiO;y,x,ch or int", &y, &x, &temp))
  894. return NULL;
  895. use_xy = TRUE;
  896. break;
  897. case 4:
  898. if (!PyArg_ParseTuple(args,"iiOl;y,x,ch or int, attr", &y, &x, &temp, &lattr))
  899. return NULL;
  900. attr = lattr;
  901. use_xy = TRUE;
  902. break;
  903. default:
  904. PyErr_SetString(PyExc_TypeError, "insch requires 1 or 4 arguments");
  905. return NULL;
  906. }
  907. if (!PyCurses_ConvertToChtype(temp, &ch)) {
  908. PyErr_SetString(PyExc_TypeError,
  909. "argument 1 or 3 must be a ch or an int");
  910. return NULL;
  911. }
  912. if (use_xy == TRUE)
  913. rtn = mvwinsch(self->win,y,x, ch | attr);
  914. else {
  915. rtn = winsch(self->win, ch | attr);
  916. }
  917. return PyCursesCheckERR(rtn, "insch");
  918. }
  919. static PyObject *
  920. PyCursesWindow_InCh(PyCursesWindowObject *self, PyObject *args)
  921. {
  922. int x, y, rtn;
  923. switch (PyTuple_Size(args)) {
  924. case 0:
  925. rtn = winch(self->win);
  926. break;
  927. case 2:
  928. if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
  929. return NULL;
  930. rtn = mvwinch(self->win,y,x);
  931. break;
  932. default:
  933. PyErr_SetString(PyExc_TypeError, "inch requires 0 or 2 arguments");
  934. return NULL;
  935. }
  936. return PyInt_FromLong((long) rtn);
  937. }
  938. static PyObject *
  939. PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
  940. {
  941. int x, y, n;
  942. char rtn[1024]; /* This should be big enough.. I hope */
  943. int rtn2;
  944. switch (PyTuple_Size(args)) {
  945. case 0:
  946. rtn2 = winnstr(self->win,rtn, 1023);
  947. break;
  948. case 1:
  949. if (!PyArg_ParseTuple(args,"i;n", &n))
  950. return NULL;
  951. rtn2 = winnstr(self->win,rtn,MIN(n,1023));
  952. break;
  953. case 2:
  954. if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
  955. return NULL;
  956. rtn2 = mvwinnstr(self->win,y,x,rtn,1023);
  957. break;
  958. case 3:
  959. if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
  960. return NULL;
  961. rtn2 = mvwinnstr(self->win, y, x, rtn, MIN(n,1023));
  962. break;
  963. default:
  964. PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments");
  965. return NULL;
  966. }
  967. if (rtn2 == ERR)
  968. rtn[0] = 0;
  969. return PyString_FromString(rtn);
  970. }
  971. static PyObject *
  972. PyCursesWindow_InsStr(PyCursesWindowObject *self, PyObject *args)
  973. {
  974. int rtn;
  975. int x, y;
  976. char *str;
  977. attr_t attr = A_NORMAL , attr_old = A_NORMAL;
  978. long lattr;
  979. int use_xy = FALSE, use_attr = FALSE;
  980. switch (PyTuple_Size(args)) {
  981. case 1:
  982. if (!PyArg_ParseTuple(args,"s;str", &str))
  983. return NULL;
  984. break;
  985. case 2:
  986. if (!PyArg_ParseTuple(args,"sl;str,attr", &str, &lattr))
  987. return NULL;
  988. attr = lattr;
  989. use_attr = TRUE;
  990. break;
  991. case 3:
  992. if (!PyArg_ParseTuple(args,"iis;y,x,str", &y, &x, &str))
  993. return NULL;
  994. use_xy = TRUE;
  995. break;
  996. case 4:
  997. if (!PyArg_ParseTuple(args,"iisl;y,x,str,attr", &y, &x, &str, &lattr))
  998. return NULL;
  999. attr = lattr;
  1000. use_xy = use_attr = TRUE;
  1001. break;
  1002. default:
  1003. PyErr_SetString(PyExc_TypeError, "insstr requires 1 to 4 arguments");
  1004. return NULL;
  1005. }
  1006. if (use_attr == TRUE) {
  1007. attr_old = getattrs(self->win);
  1008. wattrset(self->win,attr);
  1009. }
  1010. if (use_xy == TRUE)
  1011. rtn = mvwinsstr(self->win,y,x,str);
  1012. else
  1013. rtn = winsstr(self->win,str);
  1014. if (use_attr == TRUE)
  1015. wattrset(self->win,attr_old);
  1016. return PyCursesCheckERR(rtn, "insstr");
  1017. }
  1018. static PyObject *
  1019. PyCursesWindow_InsNStr(PyCursesWindowObject *self, PyObject *args)
  1020. {
  1021. int rtn, x, y, n;
  1022. char *str;
  1023. attr_t attr = A_NORMAL , attr_old = A_NORMAL;
  1024. long lattr;
  1025. int use_xy = FALSE, use_attr = FALSE;
  1026. switch (PyTuple_Size(args)) {
  1027. case 2:
  1028. if (!PyArg_ParseTuple(args,"si;str,n", &str, &n))
  1029. return NULL;
  1030. break;
  1031. case 3:
  1032. if (!PyArg_ParseTuple(args,"sil;str,n,attr", &str, &n, &lattr))
  1033. return NULL;
  1034. attr = lattr;
  1035. use_attr = TRUE;
  1036. break;
  1037. case 4:
  1038. if (!PyArg_ParseTuple(args,"iisi;y,x,str,n", &y, &x, &str, &n))
  1039. return NULL;
  1040. use_xy = TRUE;
  1041. break;
  1042. case 5:
  1043. if (!PyArg_ParseTuple(args,"iisil;y,x,str,n,attr", &y, &x, &str, &n, &lattr))
  1044. return NULL;
  1045. attr = lattr;
  1046. use_xy = use_attr = TRUE;
  1047. break;
  1048. default:
  1049. PyErr_SetString(PyExc_TypeError, "insnstr requires 2 to 5 arguments");
  1050. return NULL;
  1051. }
  1052. if (use_attr == TRUE) {
  1053. attr_old = getattrs(self->win);
  1054. wattrset(self->win,attr);
  1055. }
  1056. if (use_xy == TRUE)
  1057. rtn = mvwinsnstr(self->win,y,x,str,n);
  1058. else
  1059. rtn = winsnstr(self->win,str,n);
  1060. if (use_attr == TRUE)
  1061. wattrset(self->win,attr_old);
  1062. return PyCursesCheckERR(rtn, "insnstr");
  1063. }
  1064. static PyObject *
  1065. PyCursesWindow_Is_LineTouched(PyCursesWindowObject *self, PyObject *args)
  1066. {
  1067. int line, erg;
  1068. if (!PyArg_ParseTuple(args,"i;line", &line))
  1069. return NULL;
  1070. erg = is_linetouched(self->win, line);
  1071. if (erg == ERR) {
  1072. PyErr_SetString(PyExc_TypeError,
  1073. "is_linetouched: line number outside of boundaries");
  1074. return NULL;
  1075. } else
  1076. if (erg == FALSE) {
  1077. Py_INCREF(Py_False);
  1078. return Py_False;
  1079. } else {
  1080. Py_INCREF(Py_True);
  1081. return Py_True;
  1082. }
  1083. }
  1084. static PyObject *
  1085. PyCursesWindow_NoOutRefresh(PyCursesWindowObject *self, PyObject *args)
  1086. {
  1087. int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
  1088. int rtn;
  1089. #ifndef WINDOW_HAS_FLAGS
  1090. if (0) {
  1091. #else
  1092. if (self->win->_flags & _ISPAD) {
  1093. #endif
  1094. switch(PyTuple_Size(args)) {
  1095. case 6:
  1096. if (!PyArg_ParseTuple(args,
  1097. "iiiiii;" \
  1098. "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
  1099. &pminrow, &pmincol, &sminrow,
  1100. &smincol, &smaxrow, &smaxcol))
  1101. return NULL;
  1102. Py_BEGIN_ALLOW_THREADS
  1103. rtn = pnoutrefresh(self->win,
  1104. pminrow, pmincol, sminrow,
  1105. smincol, smaxrow, smaxcol);
  1106. Py_END_ALLOW_THREADS
  1107. return PyCursesCheckERR(rtn, "pnoutrefresh");
  1108. default:
  1109. PyErr_SetString(PyCursesError,
  1110. "noutrefresh() called for a pad "
  1111. "requires 6 arguments");
  1112. return NULL;
  1113. }
  1114. } else {
  1115. if (!PyArg_ParseTuple(args, ":noutrefresh"))
  1116. return NULL;
  1117. Py_BEGIN_ALLOW_THREADS
  1118. rtn = wnoutrefresh(self->win);
  1119. Py_END_ALLOW_THREADS
  1120. return PyCursesCheckERR(rtn, "wnoutrefresh");
  1121. }
  1122. }
  1123. static PyObject *
  1124. PyCursesWindow_Overlay(PyCursesWindowObject *self, PyObject *args)
  1125. {
  1126. PyCursesWindowObject *temp;
  1127. int use_copywin = FALSE;
  1128. int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
  1129. int rtn;
  1130. switch (PyTuple_Size(args)) {
  1131. case 1:
  1132. if (!PyArg_ParseTuple(args, "O!;window object",
  1133. &PyCursesWindow_Type, &temp))
  1134. return NULL;
  1135. break;
  1136. case 7:
  1137. if (!PyArg_ParseTuple(args, "O!iiiiii;window object, int, int, int, int, int, int",
  1138. &PyCursesWindow_Type, &temp, &sminrow, &smincol,
  1139. &dminrow, &dmincol, &dmaxrow, &dmaxcol))
  1140. return NULL;
  1141. use_copywin = TRUE;
  1142. break;
  1143. default:
  1144. PyErr_SetString(PyExc_TypeError,
  1145. "overlay requires one or seven arguments");
  1146. return NULL;
  1147. }
  1148. if (use_copywin == TRUE) {
  1149. rtn = copywin(self->win, temp->win, sminrow, smincol,
  1150. dminrow, dmincol, dmaxrow, dmaxcol, TRUE);
  1151. return PyCursesCheckERR(rtn, "copywin");
  1152. }
  1153. else {
  1154. rtn = overlay(self->win, temp->win);
  1155. return PyCursesCheckERR(rtn, "overlay");
  1156. }
  1157. }
  1158. static PyObject *
  1159. PyCursesWindow_Overwrite(PyCursesWindowObject *self, PyObject *args)
  1160. {
  1161. PyCursesWindowObject *temp;
  1162. int use_copywin = FALSE;
  1163. int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
  1164. int rtn;
  1165. switch (PyTuple_Size(args)) {
  1166. case 1:
  1167. if (!PyArg_ParseTuple(args, "O!;window object",
  1168. &PyCursesWindow_Type, &temp))
  1169. return NULL;
  1170. break;
  1171. case 7:
  1172. if (!PyArg_ParseTuple(args, "O!iiiiii;window object, int, int, int, int, int, int",
  1173. &PyCursesWindow_Type, &temp, &sminrow, &smincol,
  1174. &dminrow, &dmincol, &dmaxrow, &dmaxcol))
  1175. return NULL;
  1176. use_copywin = TRUE;
  1177. break;
  1178. default:
  1179. PyErr_SetString(PyExc_TypeError,
  1180. "overwrite requires one or seven arguments");
  1181. return NULL;
  1182. }
  1183. if (use_copywin == TRUE) {
  1184. rtn = copywin(self->win, temp->win, sminrow, smincol,
  1185. dminrow, dmincol, dmaxrow, dmaxcol, FALSE);
  1186. return PyCursesCheckERR(rtn, "copywin");
  1187. }
  1188. else {
  1189. rtn = overwrite(self->win, temp->win);
  1190. return PyCursesCheckERR(rtn, "overwrite");
  1191. }
  1192. }
  1193. static PyObject *
  1194. PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *args)
  1195. {
  1196. PyObject *temp;
  1197. if (!PyArg_ParseTuple(args, "O;fileobj", &temp))
  1198. return NULL;
  1199. if (!PyFile_Check(temp)) {
  1200. PyErr_SetString(PyExc_TypeError, "argument must be a file object");
  1201. return NULL;
  1202. }
  1203. return PyCursesCheckERR(putwin(self->win, PyFile_AsFile(temp)),
  1204. "putwin");
  1205. }
  1206. static PyObject *
  1207. PyCursesWindow_RedrawLine(PyCursesWindowObject *self, PyObject *args)
  1208. {
  1209. int beg, num;
  1210. if (!PyArg_ParseTuple(args, "ii;beg,num", &beg, &num))
  1211. return NULL;
  1212. return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln");
  1213. }
  1214. static PyObject *
  1215. PyCursesWindow_Refresh(PyCursesWindowObject *self, PyObject *args)
  1216. {
  1217. int pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol;
  1218. int rtn;
  1219. #ifndef WINDOW_HAS_FLAGS
  1220. if (0) {
  1221. #else
  1222. if (self->win->_flags & _ISPAD) {
  1223. #endif
  1224. switch(PyTuple_Size(args)) {
  1225. case 6:
  1226. if (!PyArg_ParseTuple(args,
  1227. "iiiiii;" \
  1228. "pminrow,pmincol,sminrow,smincol,smaxrow,smaxcol",
  1229. &pminrow, &pmincol, &sminrow,
  1230. &smincol, &smaxrow, &smaxcol))
  1231. return NULL;
  1232. Py_BEGIN_ALLOW_THREADS
  1233. rtn = prefresh(self->win,
  1234. pminrow, pmincol, sminrow,
  1235. smincol, smaxrow, smaxcol);
  1236. Py_END_ALLOW_THREADS
  1237. return PyCursesCheckERR(rtn, "prefresh");
  1238. default:
  1239. PyErr_SetString(PyCursesError,
  1240. "refresh() for a pad requires 6 arguments");
  1241. return NULL;
  1242. }
  1243. } else {
  1244. if (!PyArg_ParseTuple(args, ":refresh"))
  1245. return NULL;
  1246. Py_BEGIN_ALLOW_THREADS
  1247. rtn = wrefresh(self->win);
  1248. Py_END_ALLOW_THREADS
  1249. return PyCursesCheckERR(rtn, "prefresh");
  1250. }
  1251. }
  1252. static PyObject *
  1253. PyCursesWindow_SetScrollRegion(PyCursesWindowObject *self, PyObject *args)
  1254. {
  1255. int x, y;
  1256. if (!PyArg_ParseTuple(args,"ii;top, bottom",&y,&x))
  1257. return NULL;
  1258. return PyCursesCheckERR(wsetscrreg(self->win,y,x), "wsetscrreg");
  1259. }
  1260. static PyObject *
  1261. PyCursesWindow_SubWin(PyCursesWindowObject *self, PyObject *args)
  1262. {
  1263. WINDOW *win;
  1264. int nlines, ncols, begin_y, begin_x;
  1265. nlines = 0;
  1266. ncols = 0;
  1267. switch (PyTuple_Size(args)) {
  1268. case 2:
  1269. if (!PyArg_ParseTuple(args,"ii;begin_y,begin_x",&begin_y,&begin_x))
  1270. return NULL;
  1271. break;
  1272. case 4:
  1273. if (!PyArg_ParseTuple(args, "iiii;nlines,ncols,begin_y,begin_x",
  1274. &nlines,&ncols,&begin_y,&begin_x))
  1275. return NULL;
  1276. break;
  1277. default:
  1278. PyErr_SetString(PyExc_TypeError, "subwin requires 2 or 4 arguments");
  1279. return NULL;
  1280. }
  1281. /* printf("Subwin: %i %i %i %i \n", nlines, ncols, begin_y, begin_x); */
  1282. #ifdef WINDOW_HAS_FLAGS
  1283. if (self->win->_flags & _ISPAD)
  1284. win = subpad(self->win, nlines, ncols, begin_y, begin_x);
  1285. else
  1286. #endif
  1287. win = subwin(self->win, nlines, ncols, begin_y, begin_x);
  1288. if (win == NULL) {
  1289. PyErr_SetString(PyCursesError, catchall_NULL);
  1290. return NULL;
  1291. }
  1292. return (PyObject *)PyCursesWindow_New(win);
  1293. }
  1294. static PyObject *
  1295. PyCursesWindow_Scroll(PyCursesWindowObject *self, PyObject *args)
  1296. {
  1297. int nlines;
  1298. switch(PyTuple_Size(args)) {
  1299. case 0:
  1300. return PyCursesCheckERR(scroll(self->win), "scroll");
  1301. case 1:
  1302. if (!PyArg_ParseTuple(args, "i;nlines", &nlines))
  1303. return NULL;
  1304. return PyCursesCheckERR(wscrl(self->win, nlines), "scroll");
  1305. default:
  1306. PyErr_SetString(PyExc_TypeError, "scroll requires 0 or 1 arguments");
  1307. return NULL;
  1308. }
  1309. }
  1310. static PyObject *
  1311. PyCursesWindow_TouchLine(PyCursesWindowObject *self, PyObject *args)
  1312. {
  1313. int st, cnt, val;
  1314. switch (PyTuple_Size(args)) {
  1315. case 2:
  1316. if (!PyArg_ParseTuple(args,"ii;start,count",&st,&cnt))
  1317. return NULL;
  1318. return PyCursesCheckERR(touchline(self->win,st,cnt), "touchline");
  1319. case 3:
  1320. if (!PyArg_ParseTuple(args, "iii;start,count,val", &st, &cnt, &val))
  1321. return NULL;
  1322. return PyCursesCheckERR(wtouchln(self->win, st, cnt, val), "touchline");
  1323. default:
  1324. PyErr_SetString(PyExc_TypeError, "touchline requires 2 or 3 arguments");
  1325. return NULL;
  1326. }
  1327. }
  1328. static PyObject *
  1329. PyCursesWindow_Vline(PyCursesWindowObject *self, PyObject *args)
  1330. {
  1331. PyObject *temp;
  1332. chtype ch;
  1333. int n, x, y, code = OK;
  1334. attr_t attr = A_NORMAL;
  1335. long lattr;
  1336. switch (PyTuple_Size(args)) {
  1337. case 2:
  1338. if (!PyArg_ParseTuple(args, "Oi;ch or int,n", &temp, &n))
  1339. return NULL;
  1340. break;
  1341. case 3:
  1342. if (!PyArg_ParseTuple(args, "Oil;ch or int,n,attr", &temp, &n, &lattr))
  1343. return NULL;
  1344. attr = lattr;
  1345. break;
  1346. case 4:
  1347. if (!PyArg_ParseTuple(args, "iiOi;y,x,ch or int,n", &y, &x, &temp, &n))
  1348. return NULL;
  1349. code = wmove(self->win, y, x);
  1350. break;
  1351. case 5:
  1352. if (!PyArg_ParseTuple(args, "iiOil; y,x,ch or int,n,attr",
  1353. &y, &x, &temp, &n, &lattr))
  1354. return NULL;
  1355. attr = lattr;
  1356. code = wmove(self->win, y, x);
  1357. break;
  1358. default:
  1359. PyErr_SetString(PyExc_TypeError, "vline requires 2 to 5 arguments");
  1360. return NULL;
  1361. }
  1362. if (code != ERR) {
  1363. if (!PyCurses_ConvertToChtype(temp, &ch)) {
  1364. PyErr_SetString(PyExc_TypeError,
  1365. "argument 1 or 3 must be a ch or an int");
  1366. return NULL;
  1367. }
  1368. return PyCursesCheckERR(wvline(self->win, ch | attr, n), "vline");
  1369. } else
  1370. return PyCursesCheckERR(code, "wmove");
  1371. }
  1372. static PyMethodDef PyCursesWindow_Methods[] = {
  1373. {"addch", (PyCFunction)PyCursesWindow_AddCh, METH_VARARGS},
  1374. {"addnstr", (PyCFunction)PyCursesWindow_AddNStr, METH_VARARGS},
  1375. {"addstr", (PyCFunction)PyCursesWindow_AddStr, METH_VARARGS},
  1376. {"attroff", (PyCFunction)PyCursesWindow_AttrOff, METH_VARARGS},
  1377. {"attron", (PyCFunction)PyCursesWindow_AttrOn, METH_VARARGS},
  1378. {"attrset", (PyCFunction)PyCursesWindow_AttrSet, METH_VARARGS},
  1379. {"bkgd", (PyCFunction)PyCursesWindow_Bkgd, METH_VARARGS},
  1380. {"chgat", (PyCFunction)PyCursesWindow_ChgAt, METH_VARARGS},
  1381. {"bkgdset", (PyCFunction)PyCursesWindow_BkgdSet, METH_VARARGS},
  1382. {"border", (PyCFunction)PyCursesWindow_Border, METH_VARARGS},
  1383. {"box", (PyCFunction)PyCursesWindow_Box, METH_VARARGS},
  1384. {"clear", (PyCFunction)PyCursesWindow_wclear, METH_NOARGS},
  1385. {"clearok", (PyCFunction)PyCursesWindow_clearok, METH_VARARGS},
  1386. {"clrtobot", (PyCFunction)PyCursesWindow_wclrtobot, METH_NOARGS},
  1387. {"clrtoeol", (PyCFunction)PyCursesWindow_wclrtoeol, METH_NOARGS},
  1388. {"cursyncup", (PyCFunction)PyCursesWindow_wcursyncup, METH_NOARGS},
  1389. {"delch", (PyCFunction)PyCursesWindow_DelCh, METH_VARARGS},
  1390. {"deleteln", (PyCFunction)PyCursesWindow_wdeleteln, METH_NOARGS},
  1391. {"derwin", (PyCFunction)PyCursesWindow_DerWin, METH_VARARGS},
  1392. {"echochar", (PyCFunction)PyCursesWindow_EchoChar, METH_VARARGS},
  1393. #ifdef NCURSES_MOUSE_VERSION
  1394. {"enclose", (PyCFunction)PyCursesWindow_Enclose, METH_VARARGS},
  1395. #endif
  1396. {"erase", (PyCFunction)PyCursesWindow_werase, METH_NOARGS},
  1397. {"getbegyx", (PyCFunction)PyCursesWindow_getbegyx, METH_NOARGS},
  1398. {"getbkgd", (PyCFunction)PyCursesWindow_GetBkgd, METH_NOARGS},
  1399. {"getch", (PyCFunction)PyCursesWindow_GetCh, METH_VARARGS},
  1400. {"getkey", (PyCFunction)PyCursesWindow_GetKey, METH_VARARGS},
  1401. {"getmaxyx", (PyCFunction)PyCursesWindow_getmaxyx, METH_NOARGS},
  1402. {"getparyx", (PyCFunction)PyCursesWindow_getparyx, METH_NOARGS},
  1403. {"getstr", (PyCFunction)PyCursesWindow_GetStr, METH_VARARGS},
  1404. {"getyx", (PyCFunction)PyCursesWindow_getyx, METH_NOARGS},
  1405. {"hline", (PyCFunction)PyCursesWindow_Hline, METH_VARARGS},
  1406. {"idcok", (PyCFunction)PyCursesWindow_idcok, METH_VARARGS},
  1407. {"idlok", (PyCFunction)PyCursesWindow_idlok, METH_VARARGS},
  1408. {"immedok", (PyCFunction)PyCursesWindow_immedok, METH_VARARGS},
  1409. {"inch", (PyCFunction)PyCursesWindow_InCh, METH_VARARGS},
  1410. {"insch", (PyCFunction)PyCursesWindow_InsCh, METH_VARARGS},
  1411. {"insdelln", (PyCFunction)PyCursesWindow_winsdelln, METH_VARARGS},
  1412. {"insertln", (PyCFunction)PyCursesWindow_winsertln, METH_NOARGS},
  1413. {"insnstr", (PyCFunction)PyCursesWindow_InsNStr, METH_VARARGS},
  1414. {"insstr", (PyCFunction)PyCursesWindow_InsStr, METH_VARARGS},
  1415. {"instr", (PyCFunction)PyCursesWindow_InStr, METH_VARARGS},
  1416. {"is_linetouched", (PyCFunction)PyCursesWindow_Is_LineTouched, METH_VARARGS},
  1417. {"is_wintouched", (PyCFunction)PyCursesWindow_is_wintouched, METH_NOARGS},
  1418. {"keypad", (PyCFunction)PyCursesWindow_keypad, METH_VARARGS},
  1419. {"leaveok", (PyCFunction)PyCursesWindow_leaveok, METH_VARARGS},
  1420. {"move", (PyCFunction)PyCursesWindow_wmove, METH_VARARGS},
  1421. {"mvderwin", (PyCFunction)PyCursesWindow_mvderwin, METH_VARARGS},
  1422. {"mvwin", (PyCFunction)PyCursesWindow_mvwin, METH_VARARGS},
  1423. {"nodelay", (PyCFunction)PyCursesWindow_nodelay, METH_VARARGS},
  1424. {"notimeout", (PyCFunction)PyCursesWindow_notimeout, METH_VARARGS},
  1425. {"noutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
  1426. /* Backward compatibility alias -- remove in Python 2.3 */
  1427. {"nooutrefresh", (PyCFunction)PyCursesWindow_NoOutRefresh, METH_VARARGS},
  1428. {"overlay", (PyCFunction)PyCursesWindow_Overlay, METH_VARARGS},
  1429. {"overwrite", (PyCFunction)PyCursesWindow_Overwrite,
  1430. METH_VARARGS},
  1431. {"putwin", (PyCFunction)PyCursesWindow_PutWin, METH_VARARGS},
  1432. {"redrawln", (PyCFunction)PyCursesWindow_RedrawLine, METH_VARARGS},
  1433. {"redrawwin", (PyCFunction)PyCursesWindow_redrawwin, METH_NOARGS},
  1434. {"refresh", (PyCFunction)PyCursesWindow_Refresh, METH_VARARGS},
  1435. #ifndef STRICT_SYSV_CURSES
  1436. {"resize", (PyCFunction)PyCursesWindow_wresize, METH_VARARGS},
  1437. #endif
  1438. {"scroll", (PyCFunction)PyCursesWindow_Scroll, METH_VARARGS},
  1439. {"scrollok", (PyCFunction)PyCursesWindow_scrollok, METH_VARARGS},
  1440. {"setscrreg", (PyCFunction)PyCursesWindow_SetScrollRegion, METH_VARARGS},
  1441. {"standend", (PyCFunction)PyCursesWindow_wstandend, METH_NOARGS},
  1442. {"standout", (PyCFunction)PyCursesWindow_wstandout, METH_NOARGS},
  1443. {"subpad", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
  1444. {"subwin", (PyCFunction)PyCursesWindow_SubWin, METH_VARARGS},
  1445. {"syncdown", (PyCFunction)PyCursesWindow_wsyncdown, METH_NOARGS},
  1446. {"syncok", (PyCFunction)PyCursesWindow_syncok, METH_VARARGS},
  1447. {"syncup", (PyCFunction)PyCursesWindow_wsyncup, METH_NOARGS},
  1448. {"timeout", (PyCFunction)PyCursesWindow_wtimeout, METH_VARARGS},
  1449. {"touchline", (PyCFunction)PyCursesWindow_TouchLine, METH_VARARGS},
  1450. {"touchwin", (PyCFunction)PyCursesWindow_touchwin, METH_NOARGS},
  1451. {"untouchwin", (PyCFunction)PyCursesWindow_untouchwin, METH_NOARGS},
  1452. {"vline", (PyCFunction)PyCursesWindow_Vline, METH_VARARGS},
  1453. {NULL, NULL} /* sentinel */
  1454. };
  1455. static PyObject *
  1456. PyCursesWindow_GetAttr(PyCursesWindowObject *self, char *name)
  1457. {
  1458. return Py_FindMethod(PyCursesWindow_Methods, (PyObject *)self, name);
  1459. }
  1460. /* -------------------------------------------------------*/
  1461. PyTypeObject PyCursesWindow_Type = {
  1462. PyVarObject_HEAD_INIT(NULL, 0)
  1463. "_curses.curses window", /*tp_name*/
  1464. sizeof(PyCursesWindowObject), /*tp_basicsize*/
  1465. 0, /*tp_itemsize*/
  1466. /* methods */
  1467. (destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/
  1468. 0, /*tp_print*/
  1469. (getattrfunc)PyCursesWindow_GetAttr, /*tp_getattr*/
  1470. (setattrfunc)0, /*tp_setattr*/
  1471. 0, /*tp_compare*/
  1472. 0, /*tp_repr*/
  1473. 0, /*tp_as_number*/
  1474. 0, /*tp_as_sequence*/
  1475. 0, /*tp_as_mapping*/
  1476. 0, /*tp_hash*/
  1477. };
  1478. /*********************************************************************
  1479. Global Functions
  1480. **********************************************************************/
  1481. NoArgNoReturnFunction(beep)
  1482. NoArgNoReturnFunction(def_prog_mode)
  1483. NoArgNoReturnFunction(def_shell_mode)
  1484. NoArgNoReturnFunction(doupdate)
  1485. NoArgNoReturnFunction(endwin)
  1486. NoArgNoReturnFunction(flash)
  1487. NoArgNoReturnFunction(nocbreak)
  1488. NoArgNoReturnFunction(noecho)
  1489. NoArgNoReturnFunction(nonl)
  1490. NoArgNoReturnFunction(noraw)
  1491. NoArgNoReturnFunction(reset_prog_mode)
  1492. NoArgNoReturnFunction(reset_shell_mode)
  1493. NoArgNoReturnFunction(resetty)
  1494. NoArgNoReturnFunction(savetty)
  1495. NoArgOrFlagNoReturnFunction(cbreak)
  1496. NoArgOrFlagNoReturnFunction(echo)
  1497. NoArgOrFlagNoReturnFunction(nl)
  1498. NoArgOrFlagNoReturnFunction(raw)
  1499. NoArgReturnIntFunction(baudrate)
  1500. NoArgReturnIntFunction(termattrs)
  1501. NoArgReturnStringFunction(termname)
  1502. NoArgReturnStringFunction(longname)
  1503. NoArgTrueFalseFunction(can_change_color)
  1504. NoArgTrueFalseFunction(has_colors)
  1505. NoArgTrueFalseFunction(has_ic)
  1506. NoArgTrueFalseFunction(has_il)
  1507. NoArgTrueFalseFunction(isendwin)
  1508. NoArgNoReturnVoidFunction(flushinp)
  1509. NoArgNoReturnVoidFunction(noqiflush)
  1510. static PyObject *
  1511. PyCurses_filter(PyObject *self)
  1512. {
  1513. /* not checking for PyCursesInitialised here since filter() must
  1514. be called before initscr() */
  1515. filter();
  1516. Py_INCREF(Py_None);
  1517. return Py_None;
  1518. }
  1519. static PyObject *
  1520. PyCurses_Color_Content(PyObject *self, PyObject *args)
  1521. {
  1522. short color,r,g,b;
  1523. PyCursesInitialised
  1524. PyCursesInitialisedColor
  1525. if (!PyArg_ParseTuple(args, "h:color_content", &color)) return NULL;
  1526. if (color_content(color, &r, &g, &b) != ERR)
  1527. return Py_BuildValue("(iii)", r, g, b);
  1528. else {
  1529. PyErr_SetString(PyCursesError,
  1530. "Argument 1 was out of range. Check value of COLORS.");
  1531. return NULL;
  1532. }
  1533. }
  1534. static PyObject *
  1535. PyCurses_color_pair(PyObject *self, PyObject *args)
  1536. {
  1537. int n;
  1538. PyCursesInitialised
  1539. PyCursesInitialisedColor
  1540. if (!PyArg_ParseTuple(args, "i:color_pair", &n)) return NULL;
  1541. return PyInt_FromLong((long) (n << 8));
  1542. }
  1543. static PyObject *
  1544. PyCurses_Curs_Set(PyObject *self, PyObject *args)
  1545. {
  1546. int vis,erg;
  1547. PyCursesInitialised
  1548. if (!PyArg_ParseTuple(args, "i:curs_set", &vis)) return NULL;
  1549. erg = curs_set(vis);
  1550. if (erg == ERR) return PyCursesCheckERR(erg, "curs_set");
  1551. return PyInt_FromLong((long) erg);
  1552. }
  1553. static PyObject *
  1554. PyCurses