1 /* File : Fl_Text_Buffer.i */
2 //%module Fl_Text_Buffer
3 
4 %feature("docstring") ::Fl_Text_Buffer
5 """
6 The Fl_Text_Buffer class is used by the Fl_Text_Display and Fl_Text_Editor
7 to manage complex text data and is based upon the excellent NEdit text
8 editor engine - see http://www.nedit.org/.
9 """ ;
10 
11 %{
12 #include "FL/Fl_Text_Buffer.H"
13 %}
14 
15 %{
16 #include "CallbackStruct.h"
17 
18 struct modify_link {
19   CallbackStruct *handle;
20   modify_link *next;
21   Fl_Text_Buffer* widget;
22 };
23 
24 static modify_link *py_modify_funcs = NULL;
25 
PythonModifyCallBack(int pos,int nInserted,int nDeleted,int nRestyled,const char * deletedText,void * cbArg)26 static void PythonModifyCallBack(int pos, int nInserted, int nDeleted, int nRestyled, const char* deletedText,  void* cbArg)
27 {
28    PyObject *func, *arglist;
29    PyObject *result;
30    PyObject *obj;
31    CallbackStruct *cb = (CallbackStruct*)cbArg;
32 
33    if (cb != NULL) {
34      func = cb->func;
35 
36 
37      // the user data
38      obj = cb->data;
39 
40      if (obj == NULL) {
41        arglist = Py_BuildValue("(iiiis)", pos, nInserted, nDeleted, nRestyled, deletedText );
42      }
43      else {
44        arglist = Py_BuildValue("(iiiisO)", pos, nInserted, nDeleted, nRestyled, deletedText, obj );
45      }
46      result =  PyEval_CallObject(func, arglist);
47 
48      Py_XDECREF(arglist);                           // Trash arglist
49      Py_XDECREF(result);
50      if (PyErr_Occurred())
51        {
52 	 PyErr_Print();
53        }
54 
55    }
56    else
57       PyErr_SetString(PyExc_TypeError, "PythonModifyCallBack: need a valid callback!");
58 
59    return;
60 }
61 %}
62 
63 %apply int *OUTPUT { int* foundPos };
64 
65 
66 
67 
68 %include "FL/Fl_Text_Buffer.H"
69 
70 %extend Fl_Text_Buffer {
71 
72 void
73 add_modify_callback(PyObject *PyFunc, PyObject *PyTarget=0)
74 {
75   if (!PyCallable_Check(PyFunc))
76     {
77       PyErr_SetString(PyExc_TypeError, "Need a callable object!");
78     }
79   else
80     {
81       CallbackStruct *cb = new CallbackStruct( PyFunc, PyTarget, 0, 0 );
82       self->add_modify_callback(PythonModifyCallBack, (void *)cb);
83 
84 
85       Py_INCREF(PyFunc);    /* Add a reference to new callback */
86       Py_XINCREF(PyTarget);
87 
88       // add it to global list and also add the widget!
89       modify_link *cb_link = new modify_link;
90       cb_link->next = py_modify_funcs;
91       cb_link->handle = cb;
92       cb_link->widget = self;
93       py_modify_funcs = cb_link;
94     }
95 }
96 
remove_modify_callback(PyObject * PyFunc,PyObject * PyWidget,PyObject * PyTarget)97 void remove_modify_callback(PyObject *PyFunc, PyObject *PyWidget, PyObject *PyTarget)
98 {
99   // Search for the handler in the list...
100   modify_link *l, *p;
101   for (l = py_modify_funcs, p = 0; l && !(l->handle->func == PyFunc && (0==PyObject_RichCompareBool(l->handle->data,PyTarget, Py_EQ)) && l->widget == self); p = l, l = l->next);
102   if (l) {
103     // Found it, so remove it from the list...
104     if (p)
105       p->next = l->next;
106     else
107       py_modify_funcs = l->next;
108 
109     // remove the callback
110     self->remove_modify_callback(PythonModifyCallBack, (void*)l->handle);
111 
112     // reference count
113     Py_DECREF(l->handle->func);
114     Py_XDECREF(l->handle->data);
115 
116     // And free the record...
117     delete l->handle;
118     delete l;
119   }
120 
121 }
122 }
123 
124 
125 %typemap(in) PyObject *PyFunc {
126   if (!PyCallable_Check($input)) {
127       PyErr_SetString(PyExc_TypeError, "Need a callable object!");
128       return NULL;
129   }
130   $1 = $input;
131 }
132 
133