]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/modules/ogl/oglhelpers.cpp
*** empty log message ***
[wxWidgets.git] / utils / wxPython / modules / ogl / oglhelpers.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: oglhelpers.cpp
3 // Purpose: Some Helper functions to help in data conversions in OGL
4 //
5 // Author: Robin Dunn
6 //
7 // Created: 3-Sept-1999
8 // RCS-ID: $Id$
9 // Copyright: (c) 1998 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
12
13 #include <Python.h>
14 #include "helpers.h"
15
16 //---------------------------------------------------------------------------
17 // This one will work for any class for the VERY generic cases, but beyond that
18 // the helper needs to know more about the type.
19
20 wxList* wxPy_wxListHelper(PyObject* pyList, char* className) {
21 bool doSave = wxPyRestoreThread();
22 if (!PyList_Check(pyList)) {
23 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
24 wxPySaveThread(doSave);
25 return NULL;
26 }
27 int count = PyList_Size(pyList);
28 wxList* list = new wxList;
29 if (! list) {
30 PyErr_SetString(PyExc_MemoryError, "Unable to allocate wxList object");
31 wxPySaveThread(doSave);
32 return NULL;
33 }
34 for (int x=0; x<count; x++) {
35 PyObject* pyo = PyList_GetItem(pyList, x);
36 wxObject* wxo = NULL;
37
38 if (SWIG_GetPtrObj(pyo, (void **)&wxo, className)) {
39 char errmsg[1024];
40 sprintf(errmsg, "Type error, expected list of %s objects", className);
41 PyErr_SetString(PyExc_TypeError, errmsg);
42 wxPySaveThread(doSave);
43 return NULL;
44 }
45 list->Append(wxo);
46 }
47 wxPySaveThread(doSave);
48 return list;
49 }
50
51 //---------------------------------------------------------------------------
52
53 wxList* wxPy_wxRealPoint_ListHelper(PyObject* pyList) {
54 bool doSave = wxPyRestoreThread();
55 if (!PyList_Check(pyList)) {
56 PyErr_SetString(PyExc_TypeError, "Expected a list object.");
57 wxPySaveThread(doSave);
58 return NULL;
59 }
60 int count = PyList_Size(pyList);
61 wxList* list = new wxList;
62 if (! list) {
63 PyErr_SetString(PyExc_MemoryError, "Unable to allocate wxList object");
64 wxPySaveThread(doSave);
65 return NULL;
66 }
67 for (int x=0; x<count; x++) {
68 PyObject* pyo = PyList_GetItem(pyList, x);
69
70 if (PyTuple_Check(pyo)) {
71 PyObject* o1 = PyNumber_Float(PyTuple_GetItem(pyo, 0));
72 PyObject* o2 = PyNumber_Float(PyTuple_GetItem(pyo, 1));
73
74 double val1 = (o1 ? PyFloat_AsDouble(o1) : 0.0);
75 double val2 = (o2 ? PyFloat_AsDouble(o2) : 0.0);
76
77 list->Append((wxObject*) new wxRealPoint(val1, val2));
78
79 } else {
80 wxRealPoint* wxo = NULL;
81 if (SWIG_GetPtrObj(pyo, (void **)&wxo, "_wxRealPoint_p")) {
82 PyErr_SetString(PyExc_TypeError, "Type error, expected list of wxRealPoint objects or 2-tuples");
83 wxPySaveThread(doSave);
84 return NULL;
85 }
86 list->Append((wxObject*) new wxRealPoint(*wxo));
87 }
88 }
89 wxPySaveThread(doSave);
90 return list;
91 }
92
93
94 //---------------------------------------------------------------------------
95 // Convert a wxList to a Python List
96
97 #include <ogl.h>
98
99 PyObject* wxPy_ConvertList(wxList* list, char* className) {
100 PyObject* pyList;
101 PyObject* pyObj;
102 wxObject* wxObj;
103 wxNode* node = list->First();
104
105 bool doSave = wxPyRestoreThread();
106 pyList = PyList_New(0);
107 while (node) {
108 wxObj = node->Data();
109 // printf("%s class at %x : %x\n", wxObj->GetClassInfo()->GetClassName(), (void*)wxObj, (void*)((wxShape*)wxObj)->GetParent());
110 pyObj = wxPyConstructObject(wxObj, className);
111 PyList_Append(pyList, pyObj);
112 node = node->Next();
113 }
114 // for (int x=0; x<PyList_Size(pyList); x++) {
115 // PyObject* obj = PyList_GetItem(pyList, x);
116 // char* attr = PyString_AsString(PyObject_GetAttrString(obj, "this"));
117 // PyObject* par = PyObject_CallMethod(obj, "GetParent", NULL);
118 // char* parent;
119 // if (par != Py_None)
120 // parent = PyString_AsString(PyObject_GetAttrString(par, "this"));
121 // else
122 // parent = "None";
123 // printf("obj.this = %s obj.GetParent().this = %s\n", attr, parent);
124 // }
125 // printf("--------\n");
126 wxPySaveThread(doSave);
127 return pyList;
128 }
129
130 //---------------------------------------------------------------------------
131
132
133
134
135