1 /* File : Fl_Text_Display.i */ 2 //%module Fl_Text_Display 3 4 %feature("docstring") ::Fl_Text_Display 5 """ 6 This is the FLTK text display widget. It allows the user to view multiple 7 lines of text and supports highlighting and scrolling. The buffer that is 8 displayed in the widget is managed by the Fl_Text_Buffer class. 9 """ ; 10 11 // Redefine nested struct in global scope in order for SWIG to generate 12 // a proxy class. Only SWIG parses this definition. 13 struct Style_Table_Entry { 14 Fl_Color color; 15 Fl_Font font; 16 int size; 17 unsigned attr; 18 }; 19 20 21 %{ 22 #include "FL/Fl_Text_Display.H" 23 %} 24 25 %include "macros.i" 26 27 CHANGE_OWNERSHIP(Fl_Text_Display) 28 29 %ignore Fl_Text_Display::buffer(Fl_Text_Buffer& buf); 30 %ignore fl_text_drag_me(int pos, Fl_Text_Display* d); 31 // because of problems with Style_Table_Entry 32 %ignore Fl_Text_Display::highlight_data(Fl_Text_Buffer *styleBuffer,const Style_Table_Entry *styleTable,int nStyles, char unfinishedStyle,Unfinished_Style_Cb unfinishedHighlightCB,void *cbArg); 33 %ignore Fl_Text_Display::Style_Table_Entry; 34 35 // delegate ownership of passed argument (Fl_Text_Buffer) 36 %pythonappend Fl_Text_Display::buffer %{ 37 if len(args) > 0 and args[0] != None: 38 #delegate ownership to C++ 39 args[0].this.disown() 40 %} 41 42 // typemap to convert a Python array to 43 // an array of Style_Table_Entry 44 %typemap(in) Fl_Text_Display::Style_Table_Entry const * { 45 // Check if it is a list 46 if (PyList_Check($input)) { 47 int size = PyList_Size($input); 48 int i = 0; 49 $1 = (Style_Table_Entry*) malloc((size)*sizeof(Style_Table_Entry)); 50 for (i = 0; i < size; i++) { 51 PyObject *o = PyList_GetItem($input,i); 52 if (PyList_Check(o)) { 53 int item_size = PyList_Size(o); 54 $1[i].color = (Fl_Color)PyInt_AsLong(PyList_GetItem(o,0)); 55 $1[i].font = (Fl_Font)PyInt_AsLong(PyList_GetItem(o,1)); 56 $1[i].size = PyInt_AsLong(PyList_GetItem(o,2)); 57 if (item_size > 3) 58 $1[i].attr = PyInt_AsLong(PyList_GetItem(o,3)); 59 } 60 } 61 } 62 else { 63 PyErr_SetString(PyExc_TypeError,"not a list"); 64 return NULL; 65 } 66 } 67 68 // This cleans up the Style_Table_Entry array we malloc'd before the function call 69 %typemap(freearg) Style_Table_Entry const * { 70 //free((char *) $1); 71 } 72 73 74 // callback handling 75 %{ UnfinishedStyleCB(int arg1,void * clientdata)76 static void UnfinishedStyleCB(int arg1, void *clientdata) { 77 PyObject *func, *arglist; 78 PyObject *result; 79 80 // This is the function .... 81 func = (PyObject *)( ((CallbackStruct *)clientdata)->func); 82 if (((CallbackStruct *)clientdata)->data) 83 { 84 arglist = Py_BuildValue("(iO)", arg1, (PyObject *)(((CallbackStruct *)clientdata)->data) ); 85 } 86 else 87 { 88 arglist = Py_BuildValue("(iO)", arg1 ); 89 } 90 91 result = PyEval_CallObject(func, arglist); 92 93 Py_DECREF(arglist); // Trash arglist 94 Py_XDECREF(result); 95 if (PyErr_Occurred()) 96 { 97 PyErr_Print(); 98 } 99 100 return /*void*/; 101 102 } 103 %} 104 105 106 107 108 109 %{ 110 // SWIG thinks that Style_Table_Entry is a global class, so we need to trick the C++ 111 // compiler into understanding this so called global type. 112 typedef Fl_Text_Display::Style_Table_Entry Style_Table_Entry; 113 %} 114 115 %include "FL/Fl_Text_Display.H" 116 117 %extend Fl_Text_Display { 118 %rename(highlight_data) highlight_data_new; highlight_data_new(Fl_Text_Buffer * styleBuffer,const Style_Table_Entry * styleTable,int nStyles,char unfinishedStyle,PyObject * func,PyObject * cbArg)119 void highlight_data_new(Fl_Text_Buffer *styleBuffer, 120 //const Fl_Text_Display::Style_Table_Entry *styleTable, 121 const Style_Table_Entry *styleTable, 122 int nStyles, char unfinishedStyle, 123 PyObject *func, 124 PyObject *cbArg) { 125 if (!PyCallable_Check(func)) { 126 PyErr_SetString(PyExc_TypeError, "Need a callable object!"); 127 } 128 else { 129 CallbackStruct *cb = new CallbackStruct( func , cbArg, (PyObject*)0 ); 130 // add reference 131 132 Py_INCREF(func); 133 Py_XINCREF(cbArg); 134 self->highlight_data(styleBuffer, styleTable, nStyles, unfinishedStyle, UnfinishedStyleCB, (void*)cb); 135 } 136 137 } 138 //%rename(highlight_data) highlight_data_new; 139 } 140 141 %typemap(in) PyObject *PyFunc { 142 if (!PyCallable_Check($input)) { 143 PyErr_SetString(PyExc_TypeError, "Need a callable object!"); 144 return NULL; 145 } 146 $1 = $input; 147 } 148