home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Amiga_Misc / experimental_modules / simplegfxmodule.c < prev   
Encoding:
C/C++ Source or Header  |  2000-10-29  |  6.7 KB  |  322 lines

  1.  
  2. /********************************************************************
  3.  
  4.     Simple Graphics module.
  5.  
  6.     ©Irmen de Jong
  7.  
  8.  
  9.     29-okt-00    Fixup for Python 2.0
  10.  
  11. **************************************************************************/
  12.  
  13. #include <exec/types.h>
  14. #include <dos/dos.h>
  15. #include <intuition/intuition.h>
  16. #include <proto/intuition.h>
  17. #include <proto/exec.h>
  18. #include <proto/graphics.h>
  19. #include "Python.h"
  20.  
  21. typedef struct {
  22.     PyObject_HEAD
  23.     struct Window *win;
  24.     char *title;
  25.     short pen;
  26.     BOOL gzz;
  27.     ULONG signal;
  28. } windowobject;
  29.  
  30. static PyObject *error;    // Exception
  31.  
  32. /* Prototypes for functions defined in arexxmodule.c */
  33.  
  34. ///*** WINDOW OBJECT MEMBER FUNCTIONS ***/
  35.  
  36. static BOOL CheckOpen(const windowobject *w)
  37. {
  38.     if(w->win) return TRUE;
  39.     PyErr_SetString(error,"closed window");
  40.     return FALSE;
  41. }
  42.  
  43. static void FixGZZ(const windowobject *w, long *x, long *y)
  44. {
  45.     if(w->gzz)
  46.     {
  47.         *x += w->win->BorderLeft;
  48.         *y += w->win->BorderTop;
  49.     }
  50. }
  51.  
  52. static PyObject *win_close(windowobject *w, PyObject *args)
  53. {
  54.     if(!PyArg_NoArgs(args)) return NULL;
  55.  
  56.     if(w->win) CloseWindow(w->win);
  57.     w->win=NULL;
  58.     if(w->title) free(w->title);
  59.     w->title=NULL;
  60.  
  61.     Py_INCREF(Py_None);
  62.     return Py_None;
  63. }
  64.  
  65. static PyObject *win_wait(windowobject *w, PyObject *args)
  66. {
  67.     if(!PyArg_NoArgs(args)) return NULL;
  68.  
  69.     if(CheckOpen(w))
  70.     {
  71.         ULONG sigs = Wait(w->signal | SIGBREAKF_CTRL_C);   /* XXX Abort with ^C */
  72.         if(sigs & SIGBREAKF_CTRL_C)
  73.         {
  74.             PyErr_SetNone(PyExc_KeyboardInterrupt);
  75.             return NULL;
  76.         }
  77.         Py_INCREF(Py_None);
  78.         return Py_None;
  79.     } else return NULL;
  80. }
  81.  
  82. static PyObject *win_getmsg(windowobject *w, PyObject *args)
  83. {
  84.     if(!PyArg_NoArgs(args)) return NULL;
  85.  
  86.     if(CheckOpen(w))
  87.     {
  88.         struct IntuiMessage *msg;
  89.  
  90.         if(msg=(struct IntuiMessage*)GetMsg(w->win->UserPort))
  91.         {
  92.             PyObject *t = Py_BuildValue("(iiiiiii)",msg->Class,msg->Code,msg->Qualifier,msg->MouseX,msg->MouseY,msg->Seconds,msg->Micros);
  93.             ReplyMsg((struct Message*)msg);
  94.             return t;
  95.         }
  96.         Py_INCREF(Py_None);
  97.         return Py_None;
  98.     } else return NULL;
  99. }
  100.  
  101. static PyObject *win_pen(windowobject *w, PyObject *args)
  102. {
  103.     long pen;
  104.  
  105.     if (!PyArg_ParseTuple(args, "i", &pen))
  106.         return NULL;
  107.  
  108.     if(CheckOpen(w))
  109.     {
  110.         w->pen=pen;
  111.         SetAPen(w->win->RPort,pen);
  112.         Py_INCREF(Py_None);
  113.         return Py_None;
  114.     } else return NULL;
  115. }
  116.  
  117.  
  118. static PyObject *win_gzz(windowobject *w, PyObject *args)
  119. {
  120.     long gzz;
  121.  
  122.     if (!PyArg_ParseTuple(args, "i", &gzz))
  123.         return NULL;
  124.  
  125.     w->gzz=gzz;
  126.     Py_INCREF(Py_None);
  127.     return Py_None;
  128. }
  129.  
  130. static PyObject *win_clear(windowobject *w, PyObject *args)
  131. {
  132.     long pen=0;
  133.  
  134.     if (!PyArg_ParseTuple(args, "|i", &pen))
  135.         return NULL;
  136.  
  137.     if(CheckOpen(w))
  138.     {
  139.         SetAPen(w->win->RPort,pen);
  140.         RectFill(w->win->RPort,w->win->BorderLeft,w->win->BorderTop,
  141.             w->win->Width-w->win->BorderRight-1,w->win->Height-w->win->BorderBottom-1);
  142.         SetAPen(w->win->RPort,w->pen);
  143.         Py_INCREF(Py_None);
  144.         return Py_None;
  145.     } else return NULL;
  146. }
  147.  
  148. static PyObject *win_plot(windowobject *w, PyObject *args)
  149. {
  150.     long x,y;
  151.  
  152.     if (!PyArg_ParseTuple(args, "ii", &x,&y))
  153.         return NULL;
  154.  
  155.     if(CheckOpen(w))
  156.     {
  157.         FixGZZ(w,&x,&y);
  158.         WritePixel(w->win->RPort,x,y);
  159.         Py_INCREF(Py_None);
  160.         return Py_None;
  161.     } else return NULL;
  162. }
  163.  
  164. static PyObject *win_line(windowobject *w, PyObject *args)
  165. {
  166.     long x1,y1,x2,y2;
  167.  
  168.     if (!PyArg_ParseTuple(args, "iiii", &x1,&y1,&x2,&y2))
  169.         return NULL;
  170.  
  171.     if(CheckOpen(w))
  172.     {
  173.         FixGZZ(w,&x1,&y1);
  174.         FixGZZ(w,&x2,&y2);
  175.         Move(w->win->RPort,x1,y1);
  176.         Draw(w->win->RPort,x2,y2);
  177.         Py_INCREF(Py_None);
  178.         return Py_None;
  179.     } else return NULL;
  180. }
  181.  
  182. static PyObject *win_lineto(windowobject *w, PyObject *args)
  183. {
  184.     long x,y;
  185.  
  186.     if (!PyArg_ParseTuple(args, "ii", &x,&y))
  187.         return NULL;
  188.  
  189.     if(CheckOpen(w))
  190.     {
  191.         FixGZZ(w,&x,&y);
  192.         Draw(w->win->RPort,x,y);
  193.         Py_INCREF(Py_None);
  194.         return Py_None;
  195.     } else return NULL;
  196. }
  197.  
  198. static PyMethodDef win_methods[] = {
  199.     {"close", (PyCFunction)win_close, METH_OLDARGS},
  200.     {"getmsg", (PyCFunction)win_getmsg, METH_OLDARGS},
  201.     {"wait", (PyCFunction)win_wait, METH_OLDARGS},
  202.     {"gzz", (PyCFunction)win_gzz,METH_VARARGS},
  203.     {"pen", (PyCFunction)win_pen,METH_VARARGS},
  204.     {"clear", (PyCFunction)win_clear,METH_VARARGS},
  205.     {"plot", (PyCFunction)win_plot,METH_VARARGS},
  206.     {"line", (PyCFunction)win_line,METH_VARARGS},
  207.     {"lineto", (PyCFunction)win_lineto,METH_VARARGS},
  208.     {NULL,      NULL}       /* sentinel */
  209. };
  210.  
  211.  
  212. static void
  213. win_dealloc(windowobject *self)         // `destructor'
  214. {
  215.     if(self->win) CloseWindow(self->win);
  216.     PyMem_DEL(self);
  217. }
  218.  
  219. static PyObject *
  220. win_getattr(windowobject *w, char *name)
  221. {
  222.     if(w->win)
  223.     {
  224.         if(strcmp(name,"signal")==0)
  225.             return PyInt_FromLong(w->signal);
  226.     }
  227.     return Py_FindMethod(win_methods, (PyObject *)w, name);
  228. }
  229.  
  230. static PyObject *
  231. win_repr(windowobject *w)
  232. {
  233.     char buf[50];
  234.     sprintf(buf,"<window at %lx>",(long)w);
  235.     return PyString_FromString(buf);
  236. }
  237.  
  238. static PyTypeObject Windowtype = {
  239.     PyObject_HEAD_INIT(&PyType_Type)
  240.     0,          /*ob_size*/
  241.     "window",        /*tp_name*/
  242.     sizeof(windowobject),    /*tp_size*/
  243.     0,          /*tp_itemsize*/
  244.     /* methods */
  245.     (destructor)win_dealloc, /*tp_dealloc*/
  246.     0,          /*tp_print*/
  247.     (getattrfunc)win_getattr, /*tp_getattr*/
  248.     (setattrfunc)0, /*tp_setattr*/
  249.     0,          /*tp_compare*/
  250.     (reprfunc)win_repr,        /*tp_repr*/
  251. };
  252.  
  253.  
  254. static PyObject *
  255. newwindowobject(char *name, int x, int y, int w, int h) // `constructor'
  256. {
  257.     windowobject *wo;
  258.  
  259.     if(wo = PyObject_NEW(windowobject, &Windowtype))
  260.     {
  261.         wo->title=strdup(name);
  262.         wo->pen=1;
  263.         wo->gzz=1;
  264.         if(wo->win=OpenWindowTags(NULL,
  265.                 WA_Title,wo->title,
  266.                 WA_Left,x,WA_Top,y,WA_InnerWidth,w,WA_InnerHeight,h,
  267.                 WA_Flags,WFLG_SIZEGADGET | WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_SIZEBRIGHT | WFLG_ACTIVATE | WFLG_SIMPLE_REFRESH | WFLG_NOCAREREFRESH,
  268.                 WA_IDCMP,IDCMP_CLOSEWINDOW | IDCMP_VANILLAKEY | IDCMP_MOUSEBUTTONS,
  269.                 WA_MinWidth,50, WA_MinHeight,30, WA_MaxWidth,-1, WA_MaxHeight,-1,
  270.                 WA_AutoAdjust,1,TAG_DONE))
  271.         {
  272.             wo->signal = 1<<wo->win->UserPort->mp_SigBit;
  273.             SetAPen(wo->win->RPort,wo->pen);
  274.             return (PyObject*)wo;
  275.         }
  276.         else PyErr_SetString(error,"can't open window");
  277.  
  278.         PyMem_DEL(wo); wo=NULL;
  279.     }
  280.     return (PyObject*)wo;
  281. }
  282.  
  283.  
  284. ///
  285.  
  286.  
  287. ///******************************** MODULE FUNCTIONS ************************/
  288.  
  289. static PyObject *
  290. SimpleGFX_openwindow(PyObject *self, PyObject *args)
  291. {
  292.     char *n="Python Window"; long x=40,y=40,w=400,h=200;
  293.     
  294.     if (!PyArg_ParseTuple(args, "|siiii", &n,&x,&y,&w,&h))
  295.         return NULL;
  296.  
  297.     return newwindowobject(n,x,y,w,h);
  298. }
  299.  
  300.  
  301. /*** FUNCTIONS FROM THE MODULE ***/
  302.  
  303. static PyMethodDef SimpleGFX_global_methods[] = {
  304.     {"window",  SimpleGFX_openwindow, METH_VARARGS},
  305.     {NULL,      NULL}       /* sentinel */
  306. };
  307. ///
  308.  
  309. void
  310. initsimplegfx Py_PROTO((void))
  311. {
  312.     PyObject *m, *d;
  313.  
  314.     m = Py_InitModule("simplegfx", SimpleGFX_global_methods);
  315.     d = PyModule_GetDict(m);
  316.  
  317.     /* Initialize error exception */
  318.     error = PyString_FromString("simplegfx.error");
  319.     if (error == NULL || PyDict_SetItemString(d, "error", error) != 0)
  320.         Py_FatalError("can't define simplegfx.error");
  321. }
  322.