1 #ifdef PYTHON
2 /* File : setMenu.i */
3 //%module setMenu
4
5 %native(setMenu) PyObject *setMenu(PyObject *self, PyObject *args);
6
7
8
9 %{
10
createFl_Menu_Item_Array(PyObject * self,PyObject * pyMenuList)11 Fl_Menu_Item *createFl_Menu_Item_Array(PyObject *self, PyObject *pyMenuList)
12 {
13 int numItems = PyTuple_Size(pyMenuList);
14 //if a list (mutable) and not a tuple (immutable) is passed,
15 // 'numItems' == -1, so if so, assume a list and covert it to a tuple
16 if (PyList_Check(pyMenuList))
17 {
18 //try a list
19 pyMenuList = PyList_AsTuple(pyMenuList);
20 numItems = PyTuple_Size(pyMenuList);
21 }
22 Fl_Menu_Item *arrayOfFMI = 0;
23 arrayOfFMI = new Fl_Menu_Item[numItems+1];
24 int index=0;
25 for (; index<numItems; index++)
26 {
27 PyObject *nextItem = PyTuple_GetItem( pyMenuList, index );
28 char *pyText=0;
29 int shortcut=0;
30 PyObject *callback=0;
31 PyObject *userData=0;
32 int flags=0;
33 unsigned char labelType=0;
34 unsigned char labelFont=0;
35 unsigned char labelSize=0;
36 unsigned char labelColor=0;
37 int ok = PyArg_ParseTuple( nextItem, "z|iOOibbbb",
38 &pyText, &shortcut, &callback, &userData, &flags,
39 &labelType, &labelFont, &labelSize, &labelColor);
40
41 Fl_Menu_Item *p = arrayOfFMI+index;
42 if (ok)
43 {
44 //have all the components, now set the values....
45
46 //// Yes, this is a memory leak
47 //// I don't know away around it, since strings from
48 //// the scripting language are all dynamically allocated
49 if ( !pyText )
50 {
51 p->text = 0;
52 }
53 else
54 {
55 p->text = strdup(pyText);
56 }
57
58 p->shortcut_ = shortcut;
59
60 if (callback && PyCallable_Check(callback))
61 {
62 CallbackStruct *cb = new CallbackStruct( callback, userData, SWIGTYPE_p_Fl_Menu_Item );
63 Py_INCREF(callback);
64 Py_XINCREF(userData);
65 //self->callback(PythonCallBack, (void *)cb);
66 p->callback_ = (Fl_Callback *)PythonCallBack;
67 p->user_data_ = (void *)cb;
68 }
69 else
70 {
71 p->callback_ = (Fl_Callback *)0;
72 }
73
74
75 p->flags = flags;
76 p-> labeltype_ = labelType;
77 p-> labelfont_ = labelFont;
78 p-> labelsize_ = labelSize;
79 p-> labelcolor_ = labelColor;
80 }
81 else
82 {
83 fprintf(stderr, "Could not convert menu item %d\n", index);
84 PyObject_Print(nextItem, stderr, 0);
85 fprintf(stderr, "\n");
86 p->text = 0;
87 delete [] arrayOfFMI;
88 return NULL;
89 }
90 }
91 arrayOfFMI[index].text = 0;
92 return arrayOfFMI;
93 }
94
95 // this is deprecated
setMenu(PyObject * self,PyObject * args)96 PyObject *setMenu(PyObject *self, PyObject *args)
97 {
98 PyObject *targetObject, *menuList;
99 printf("Warning: setMenu is deprecated, use Fl_Menu_.copy() instead!\n");
100 if (!PyTuple_Check(args))
101 {
102 printf("setMenuError: not a tup\n");
103 return NULL;
104 }
105
106 if (!PyArg_ParseTuple( args, "OO", &targetObject, &menuList))
107 {
108 printf("no conv args\n");
109 return NULL;
110 }
111
112 PyObject *thisPtrString = PyObject_GetAttrString( targetObject, "this");
113 #if PY_VERSION_HEX>=0x03000000
114 if (!PyUnicode_Check(thisPtrString))
115 #else
116 if (!PyString_Check(thisPtrString))
117 #endif
118 {
119 printf( "no get this str\n");
120 return NULL;
121 }
122
123 Fl_Menu_ *theMenu;
124 //char *thisPtrAsCString = PyString_AsString(thisPtrString);
125 //SWIG_GetPtr( thisPtrAsCString, (void **)&theMenu, "_Fl_Menu_p");
126 SWIG_ConvertPtr(thisPtrString, (void **)&theMenu, SWIGTYPE_p_Fl_Menu_, 0);
127
128 Fl_Menu_Item *theMenuItems = createFl_Menu_Item_Array( NULL, menuList);
129
130 // call the C++ object to add the menu
131 theMenu->copy( theMenuItems, NULL );
132
133 delete [] theMenuItems;
134
135 Py_INCREF(Py_None);
136 return Py_None;
137
138 }
139
140 %}
141
142
143
144 #endif
145
146
147