home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_include_py_curses.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-26  |  3.6 KB  |  134 lines

  1.  
  2. #ifndef Py_CURSES_H
  3. #define Py_CURSES_H
  4.  
  5. #ifdef HAVE_NCURSES_H
  6. #include <ncurses.h>
  7. #else
  8. #include <curses.h>
  9. #endif
  10.  
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14.  
  15. #define PyCurses_API_pointers 4
  16.  
  17. /* Type declarations */
  18.  
  19. typedef struct {
  20.     PyObject_HEAD
  21.     WINDOW *win;
  22. } PyCursesWindowObject;
  23.  
  24. #define PyCursesWindow_Check(v)     ((v)->ob_type == &PyCursesWindow_Type)
  25.  
  26. #ifdef CURSES_MODULE
  27. /* This section is used when compiling _cursesmodule.c */
  28.  
  29. #else
  30. /* This section is used in modules that use the _cursesmodule API */
  31.  
  32. static void **PyCurses_API;
  33.  
  34. #define PyCursesWindow_Type (*(PyTypeObject *) PyCurses_API[0])
  35. #define PyCursesSetupTermCalled  {if (! ((int (*)(void))PyCurses_API[1]) () ) return NULL;}
  36. #define PyCursesInitialised      {if (! ((int (*)(void))PyCurses_API[2]) () ) return NULL;}
  37. #define PyCursesInitialisedColor {if (! ((int (*)(void))PyCurses_API[3]) () ) return NULL;}
  38.  
  39. #define import_curses() \
  40. { \
  41.   PyObject *module = PyImport_ImportModule("_curses"); \
  42.   if (module != NULL) { \
  43.     PyObject *module_dict = PyModule_GetDict(module); \
  44.     PyObject *c_api_object = PyDict_GetItemString(module_dict, "_C_API"); \
  45.     if (PyCObject_Check(c_api_object)) { \
  46.       PyCurses_API = (void **)PyCObject_AsVoidPtr(c_api_object); \
  47.     } \
  48.   } \
  49. }
  50. #endif
  51.  
  52. /* general error messages */
  53. static char *catchall_ERR  = "curses function returned ERR";
  54. static char *catchall_NULL = "curses function returned NULL";
  55.  
  56. /* Utility macros */
  57. #define ARG_COUNT(X) \
  58.     (((X) == NULL) ? 0 : (PyTuple_Check(X) ? PyTuple_Size(X) : 1))
  59.  
  60. /* Function Prototype Macros - They are ugly but very, very useful. ;-)
  61.  
  62.    X - function name
  63.    TYPE - parameter Type
  64.    ERGSTR - format string for construction of the return value
  65.    PARSESTR - format string for argument parsing
  66.    */
  67.  
  68. #define NoArgNoReturnFunction(X) \
  69. static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \
  70. { \
  71.   PyCursesInitialised \
  72.   if (!PyArg_NoArgs(args)) return NULL; \
  73.   return PyCursesCheckERR(X(), # X); }
  74.  
  75. #define NoArgOrFlagNoReturnFunction(X) \
  76. static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \
  77. { \
  78.   int flag = 0; \
  79.   PyCursesInitialised \
  80.   switch(ARG_COUNT(args)) { \
  81.   case 0: \
  82.     return PyCursesCheckERR(X(), # X); \
  83.   case 1: \
  84.     if (!PyArg_Parse(args, "i;True(1) or False(0)", &flag)) return NULL; \
  85.     if (flag) return PyCursesCheckERR(X(), # X); \
  86.     else return PyCursesCheckERR(no ## X (), # X); \
  87.   default: \
  88.     PyErr_SetString(PyExc_TypeError, # X " requires 0 or 1 arguments"); \
  89.     return NULL; } }
  90.  
  91. #define NoArgReturnIntFunction(X) \
  92. static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \
  93. { \
  94.  PyCursesInitialised \
  95.  if (!PyArg_NoArgs(args)) return NULL; \
  96.  return PyInt_FromLong((long) X()); }
  97.  
  98.  
  99. #define NoArgReturnStringFunction(X) \
  100. static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \
  101. { \
  102.   PyCursesInitialised \
  103.   if (!PyArg_NoArgs(args)) return NULL; \
  104.   return PyString_FromString(X()); }
  105.  
  106. #define NoArgTrueFalseFunction(X) \
  107. static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \
  108. { \
  109.   PyCursesInitialised \
  110.   if (!PyArg_NoArgs(args)) return NULL; \
  111.   if (X () == FALSE) { \
  112.     Py_INCREF(Py_False); \
  113.     return Py_False; \
  114.   } \
  115.   Py_INCREF(Py_True); \
  116.   return Py_True; }
  117.  
  118. #define NoArgNoReturnVoidFunction(X) \
  119. static PyObject *PyCurses_ ## X (PyObject *self, PyObject *args) \
  120. { \
  121.   PyCursesInitialised \
  122.   if (!PyArg_NoArgs(args)) return NULL; \
  123.   X(); \
  124.   Py_INCREF(Py_None); \
  125.   return Py_None; }
  126.  
  127. #ifdef __cplusplus
  128. }
  129. #endif
  130.  
  131. #endif /* !defined(Py_CURSES_H) */
  132.  
  133.  
  134.