Merge remote branch 'mine/ext-python'
[vuplus_xbmc] / xbmc / interfaces / python / xbmcmodule / controlcheckmark.cpp
1 /*
2  *      Copyright (C) 2005-2008 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, write to
17  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18  *  http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21
22 #include <Python.h>
23
24 #include "../XBPythonDll.h"
25 #include "guilib/GUICheckMarkControl.h"
26 #include "guilib/GUIFontManager.h"
27 #include "control.h"
28 #include "pyutil.h"
29
30 using namespace std;
31
32 #ifndef __GNUC__
33 #pragma code_seg("PY_TEXT")
34 #pragma data_seg("PY_DATA")
35 #pragma bss_seg("PY_BSS")
36 #pragma const_seg("PY_RDATA")
37 #endif
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 namespace PYXBMC
44 {
45   PyObject* ControlCheckMark_New(PyTypeObject *type, PyObject *args, PyObject *kwds )
46   {
47     static const char *keywords[] = {
48       "x", "y", "width", "height", "label", "focusTexture", "noFocusTexture",
49       "checkWidth", "checkHeight", "alignment", "font", "textColor", "disabledColor", NULL };
50     ControlCheckMark *self;
51     char* cFont = NULL;
52     char* cTextureFocus = NULL;
53     char* cTextureNoFocus = NULL;
54     char* cTextColor = NULL;
55     char* cDisabledColor = NULL;
56
57     PyObject* pObjectText;
58
59     self = (ControlCheckMark*)type->tp_alloc(type, 0);
60     if (!self) return NULL;
61     new(&self->strFont) string();
62     new(&self->strText) string();
63     new(&self->strTextureFocus) string();
64     new(&self->strTextureNoFocus) string();
65
66     // set up default values in case they are not supplied
67     self->checkWidth = 30;
68     self->checkHeight = 30;
69     self->align = XBFONT_RIGHT;
70     self->strFont = "font13";
71     self->textColor = 0xffffffff;
72     self->disabledColor = 0x60ffffff;
73
74     // parse arguments to constructor
75     if (!PyArg_ParseTupleAndKeywords(
76       args,
77       kwds,
78       (char*)"llllO|sslllsss:ControlCheckMark",
79       (char**)keywords,
80       &self->dwPosX,
81       &self->dwPosY,
82       &self->dwWidth,
83       &self->dwHeight,
84       &pObjectText,
85       &cTextureFocus,
86       &cTextureNoFocus,
87       &self->checkWidth,
88       &self->checkHeight,
89       &self->align,
90       &cFont,
91       &cTextColor,
92       &cDisabledColor ))
93     {
94       Py_DECREF( self );
95       return NULL;
96     }
97     if (!PyXBMCGetUnicodeString(self->strText, pObjectText, 5))
98     {
99       Py_DECREF( self );
100       return NULL;
101     }
102
103     if (cFont) self->strFont = cFont;
104     if (cTextColor) sscanf(cTextColor, "%x", &self->textColor);
105     if (cDisabledColor)
106     {
107       sscanf( cDisabledColor, "%x", &self->disabledColor );
108     }
109     self->strTextureFocus = cTextureFocus ?
110       cTextureFocus :
111       PyXBMCGetDefaultImage((char*)"checkmark", (char*)"texturefocus", (char*)"check-box.png");
112     self->strTextureNoFocus = cTextureNoFocus ?
113       cTextureNoFocus :
114       PyXBMCGetDefaultImage((char*)"checkmark", (char*)"texturenofocus", (char*)"check-boxNF.png");
115
116     return (PyObject*)self;
117   }
118
119   void ControlCheckMark_Dealloc(ControlCheckMark* self)
120   {
121     self->strFont.~string();
122     self->strText.~string();
123     self->strTextureFocus.~string();
124     self->strTextureNoFocus.~string();
125     self->ob_type->tp_free((PyObject*)self);
126   }
127
128   CGUIControl* ControlCheckMark_Create(ControlCheckMark* pControl)
129   {
130     CLabelInfo label;
131     label.disabledColor = pControl->disabledColor;
132     label.textColor = label.focusedColor = pControl->textColor;
133     label.font = g_fontManager.GetFont(pControl->strFont);
134     label.align = pControl->align;
135     CTextureInfo imageFocus(pControl->strTextureFocus);
136     CTextureInfo imageNoFocus(pControl->strTextureNoFocus);
137     pControl->pGUIControl = new CGUICheckMarkControl(
138       pControl->iParentId,
139       pControl->iControlId,
140       (float)pControl->dwPosX,
141       (float)pControl->dwPosY,
142       (float)pControl->dwWidth,
143       (float)pControl->dwHeight,
144       imageFocus, imageNoFocus,
145       (float)pControl->checkWidth,
146       (float)pControl->checkHeight,
147       label );
148
149     CGUICheckMarkControl* pGuiCheckMarkControl = (CGUICheckMarkControl*)pControl->pGUIControl;
150     pGuiCheckMarkControl->SetLabel(pControl->strText);
151
152     return pControl->pGUIControl;
153   }
154
155   // setDisabledColor() Method
156   PyDoc_STRVAR(setDisabledColor__doc__,
157     "setDisabledColor(disabledColor) -- Set's this controls disabled color.\n"
158     "\n"
159     "disabledColor  : hexstring - color of disabled checkmark's label. (e.g. '0xFFFF3300')\n"
160     "\n"
161     "example:\n"
162     "  - self.checkmark.setDisabledColor('0xFFFF3300')\n");
163
164   PyObject* ControlCheckMark_SetDisabledColor(ControlCheckMark *self, PyObject *args)
165   {
166     char *cDisabledColor = NULL;
167
168     if (!PyArg_ParseTuple(args, (char*)"s", &cDisabledColor)) return NULL;
169
170     if (cDisabledColor)
171     {
172       sscanf(cDisabledColor, "%x", &self->disabledColor);
173     }
174
175     PyXBMCGUILock();
176     if (self->pGUIControl)
177     {
178       ((CGUICheckMarkControl*)self->pGUIControl)->PythonSetDisabledColor( self->disabledColor );
179     }
180     PyXBMCGUIUnlock();
181
182     Py_INCREF(Py_None);
183     return Py_None;
184   }
185
186   // setLabel() Method
187   PyDoc_STRVAR(setLabel__doc__,
188     "setLabel(label[, font, textColor, disabledColor]) -- Set's this controls text attributes.\n"
189     "\n"
190     "label          : string or unicode - text string.\n"
191     "font           : [opt] string - font used for label text. (e.g. 'font13')\n"
192     "textColor      : [opt] hexstring - color of enabled checkmark's label. (e.g. '0xFFFFFFFF')\n"
193     "disabledColor  : [opt] hexstring - color of disabled checkmark's label. (e.g. '0xFFFF3300')\n"
194     "\n"
195     "example:\n"
196     "  - self.checkmark.setLabel('Status', 'font14', '0xFFFFFFFF', '0xFFFF3300')\n");
197
198   PyObject* ControlCheckMark_SetLabel(ControlCheckMark *self, PyObject *args)
199   {
200     PyObject *pObjectText;
201     char *cFont = NULL;
202     char *cTextColor = NULL;
203     char* cDisabledColor = NULL;
204
205     if (!PyArg_ParseTuple(
206       args, (char*)"O|sss",
207       &pObjectText, &cFont,
208       &cTextColor,  &cDisabledColor))
209       return NULL;
210
211     if (!PyXBMCGetUnicodeString(self->strText, pObjectText, 1))
212       return NULL;
213
214     if (cFont) self->strFont = cFont;
215     if (cTextColor)
216     {
217       sscanf(cTextColor, "%x", &self->textColor);
218     }
219     if (cDisabledColor)
220     {
221       sscanf(cDisabledColor, "%x", &self->disabledColor);
222     }
223
224     PyXBMCGUILock();
225     if (self->pGUIControl)
226     {
227       ((CGUICheckMarkControl*)self->pGUIControl)->PythonSetLabel(
228         self->strFont,
229         self->strText,
230         self->textColor );
231       ((CGUICheckMarkControl*)self->pGUIControl)->PythonSetDisabledColor(
232         self->disabledColor );
233     }
234     PyXBMCGUIUnlock();
235
236     Py_INCREF(Py_None);
237     return Py_None;
238   }
239
240   // getSelected() Method
241   PyDoc_STRVAR(getSelected__doc__,
242     "getSelected() -- Returns the selected status for this checkmark as a bool.\n"
243     "\n"
244     "example:\n"
245     "  - selected = self.checkmark.getSelected()\n");
246
247   PyObject* ControlCheckMark_GetSelected( ControlCheckMark *self )
248   {
249     bool isSelected = 0;
250
251     PyXBMCGUILock();
252     if (self->pGUIControl)
253     {
254       isSelected = ((CGUICheckMarkControl*)self->pGUIControl)->GetSelected();
255     }
256     PyXBMCGUIUnlock();
257
258     return Py_BuildValue((char*)"b", isSelected);
259   }
260
261   // setSelected() Method
262   PyDoc_STRVAR(setSelected__doc__,
263     "setSelected(isOn) -- Sets this checkmark status to on or off.\n"
264     "\n"
265     "isOn           : bool - True=selected (on) / False=not selected (off)\n"
266     "\n"
267     "example:\n"
268     "  - self.checkmark.setSelected(True)\n");
269
270   PyObject* ControlCheckMark_SetSelected(ControlCheckMark *self, PyObject *args)
271   {
272     char isSelected = 0;
273
274     if (!PyArg_ParseTuple(args, (char*)"b", &isSelected))
275       return NULL;
276
277     PyXBMCGUILock();
278     if (self->pGUIControl)
279     {
280       ((CGUICheckMarkControl*)self->pGUIControl)->SetSelected(0 != isSelected);
281     }
282     PyXBMCGUIUnlock();
283
284     Py_INCREF(Py_None);
285     return Py_None;
286   }
287
288   PyMethodDef ControlCheckMark_methods[] = {
289     {(char*)"getSelected", (PyCFunction)ControlCheckMark_GetSelected, METH_NOARGS, getSelected__doc__},
290     {(char*)"setSelected", (PyCFunction)ControlCheckMark_SetSelected, METH_VARARGS, setSelected__doc__},
291     {(char*)"setLabel", (PyCFunction)ControlCheckMark_SetLabel, METH_VARARGS, setLabel__doc__},
292     {(char*)"setDisabledColor", (PyCFunction)ControlCheckMark_SetDisabledColor, METH_VARARGS, setDisabledColor__doc__},
293     {NULL, NULL, 0, NULL}
294   };
295
296   // ControlCheckMark class
297   PyDoc_STRVAR(controlCheckMark__doc__,
298     "ControlCheckMark class.\n"
299     "\n"
300     "ControlCheckMark(x, y, width, height, label[, focusTexture, noFocusTexture,\n"
301     "                 checkWidth, checkHeight, alignment, font, textColor, disabledColor])\n"
302     "\n"
303     "x              : integer - x coordinate of control.\n"
304     "y              : integer - y coordinate of control.\n"
305     "width          : integer - width of control.\n"
306     "height         : integer - height of control.\n"
307     "label          : string or unicode - text string.\n"
308     "focusTexture   : [opt] string - filename for focus texture.\n"
309     "noFocusTexture : [opt] string - filename for no focus texture.\n"
310     "checkWidth     : [opt] integer - width of checkmark.\n"
311     "checkHeight    : [opt] integer - height of checkmark.\n"
312     "alignment      : [opt] integer - alignment of label - *Note, see xbfont.h\n"
313     "font           : [opt] string - font used for label text. (e.g. 'font13')\n"
314     "textColor      : [opt] hexstring - color of enabled checkmark's label. (e.g. '0xFFFFFFFF')\n"
315     "disabledColor  : [opt] hexstring - color of disabled checkmark's label. (e.g. '0xFFFF3300')\n"
316     "\n"
317     "*Note, You can use the above as keywords for arguments and skip certain optional arguments.\n"
318     "       Once you use a keyword, all following arguments require the keyword.\n"
319     "       After you create the control, you need to add it to the window with addControl().\n"
320     "\n"
321     "example:\n"
322     "  - self.checkmark = xbmcgui.ControlCheckMark(100, 250, 200, 50, 'Status', font='font14')\n");
323
324
325 // Restore code and data sections to normal.
326 #ifndef __GNUC__
327 #pragma code_seg()
328 #pragma data_seg()
329 #pragma bss_seg()
330 #pragma const_seg()
331 #endif
332
333   PyTypeObject ControlCheckMark_Type;
334
335   void initControlCheckMark_Type()
336   {
337     PyXBMCInitializeTypeObject(&ControlCheckMark_Type);
338
339     ControlCheckMark_Type.tp_name = (char*)"xbmcgui.ControlCheckMark";
340     ControlCheckMark_Type.tp_basicsize = sizeof(ControlCheckMark);
341     ControlCheckMark_Type.tp_dealloc = (destructor)ControlCheckMark_Dealloc;
342     ControlCheckMark_Type.tp_compare = 0;
343     ControlCheckMark_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
344     ControlCheckMark_Type.tp_doc = controlCheckMark__doc__;
345     ControlCheckMark_Type.tp_methods = ControlCheckMark_methods;
346     ControlCheckMark_Type.tp_base = &Control_Type;
347     ControlCheckMark_Type.tp_new = ControlCheckMark_New;
348   }
349 }
350
351 #ifdef __cplusplus
352 }
353 #endif