Merge remote branch 'mine/ext-python'
[vuplus_xbmc] / xbmc / interfaces / python / xbmcmodule / controllabel.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/GUILabelControl.h"
26 #include "guilib/GUIFontManager.h"
27 #include "guilib/GUIWindowManager.h"
28 #include "control.h"
29 #include "pyutil.h"
30
31 using namespace std;
32
33 #ifndef __GNUC__
34 #pragma code_seg("PY_TEXT")
35 #pragma data_seg("PY_DATA")
36 #pragma bss_seg("PY_BSS")
37 #pragma const_seg("PY_RDATA")
38 #endif
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 namespace PYXBMC
45 {
46   PyObject* ControlLabel_New(PyTypeObject *type, PyObject *args, PyObject *kwds)
47   {
48     static const char *keywords[] = {
49       "x", "y", "width", "height", "label", "font", "textColor",
50       "disabledColor", "alignment", "hasPath", "angle", NULL };
51
52     ControlLabel *self;
53     char *cFont = NULL;
54     char *cTextColor = NULL;
55     char *cDisabledColor = NULL;
56     PyObject* pObjectText;
57     char bHasPath = false;
58
59     self = (ControlLabel*)type->tp_alloc(type, 0);
60     if (!self) return NULL;
61     new(&self->strText) string();
62     new(&self->strFont) string();
63
64     // set up default values in case they are not supplied
65     self->strFont = "font13";
66     self->textColor = 0xffffffff;
67     self->disabledColor = 0x60ffffff;
68     self->align = XBFONT_LEFT;
69     self->iAngle = 0;
70
71     if (!PyArg_ParseTupleAndKeywords(
72       args,
73       kwds,
74       (char*)"llllO|ssslbl",
75       (char**)keywords,
76       &self->dwPosX,
77       &self->dwPosY,
78       &self->dwWidth,
79       &self->dwHeight,
80       &pObjectText,
81       &cFont,
82       &cTextColor,
83       &cDisabledColor,
84       &self->align,
85       &bHasPath,
86       &self->iAngle))
87     {
88         Py_DECREF( self );
89         return NULL;
90     }
91     self->bHasPath = (0 != bHasPath);
92     if (!PyXBMCGetUnicodeString(self->strText, pObjectText, 5))
93     {
94       Py_DECREF( self );
95       return NULL;
96     }
97
98     if (cFont) self->strFont = cFont;
99     if (cTextColor) sscanf(cTextColor, "%x", &self->textColor);
100     if (cDisabledColor)
101     {
102       sscanf( cDisabledColor, "%x", &self->disabledColor );
103     }
104
105     return (PyObject*)self;
106   }
107
108   void ControlLabel_Dealloc(ControlLabel* self)
109   {
110     self->strText.~string();
111     self->strFont.~string();
112     self->ob_type->tp_free((PyObject*)self);
113   }
114
115   CGUIControl* ControlLabel_Create(ControlLabel* pControl)
116   {
117     CLabelInfo label;
118     label.font = g_fontManager.GetFont(pControl->strFont);
119     label.textColor = label.focusedColor = pControl->textColor;
120     label.disabledColor = pControl->disabledColor;
121     label.align = pControl->align;
122     label.angle = (float)-pControl->iAngle;
123     pControl->pGUIControl = new CGUILabelControl(
124       pControl->iParentId,
125       pControl->iControlId,
126       (float)pControl->dwPosX,
127       (float)pControl->dwPosY,
128       (float)pControl->dwWidth,
129       (float)pControl->dwHeight,
130       label,
131       false,
132       pControl->bHasPath);
133     ((CGUILabelControl *)pControl->pGUIControl)->SetLabel(pControl->strText);
134     return pControl->pGUIControl;
135   }
136
137   // setLabel() Method
138   PyDoc_STRVAR(setLabel__doc__,
139     "setLabel(label) -- Set's text for this label.\n"
140     "\n"
141     "label          : string or unicode - text string.\n"
142     "\n"
143     "example:\n"
144     "  - self.label.setLabel('Status')\n");
145
146   PyObject* ControlLabel_SetLabel(ControlLabel *self, PyObject *args)
147   {
148     PyObject *pObjectText;
149
150     if (!PyArg_ParseTuple(args, (char*)"O", &pObjectText)) return NULL;
151     if (!PyXBMCGetUnicodeString(self->strText, pObjectText, 1)) return NULL;
152
153     ControlLabel *pControl = (ControlLabel*)self;
154     CGUIMessage msg(GUI_MSG_LABEL_SET, pControl->iParentId, pControl->iControlId);
155     msg.SetLabel(self->strText);
156     g_windowManager.SendThreadMessage(msg, pControl->iParentId);
157
158     Py_INCREF(Py_None);
159     return Py_None;
160   }
161
162   // getLabel() Method
163   PyDoc_STRVAR(getLabel__doc__,
164     "getLabel() -- Returns the text value for this label.\n"
165     "\n"
166     "example:\n"
167     "  - label = self.label.getLabel()\n");
168
169   PyObject* ControlLabel_GetLabel(ControlLabel *self, PyObject *args)
170   {
171     if (!self->pGUIControl) return NULL;
172     return Py_BuildValue((char*)"s", self->strText.c_str());
173   }
174
175   PyMethodDef ControlLabel_methods[] = {
176     {(char*)"setLabel", (PyCFunction)ControlLabel_SetLabel, METH_VARARGS, setLabel__doc__},
177     {(char*)"getLabel", (PyCFunction)ControlLabel_GetLabel, METH_VARARGS, getLabel__doc__},
178     {NULL, NULL, 0, NULL}
179   };
180
181   // ControlLabel class
182   PyDoc_STRVAR(controlLabel__doc__,
183     "ControlLabel class.\n"
184     "\n"
185     "ControlLabel(x, y, width, height, label[, font, textColor, \n"
186     "             disabledColor, alignment, hasPath, angle])\n"
187     "\n"
188     "x              : integer - x coordinate of control.\n"
189     "y              : integer - y coordinate of control.\n"
190     "width          : integer - width of control.\n"
191     "height         : integer - height of control.\n"
192     "label          : string or unicode - text string.\n"
193     "font           : [opt] string - font used for label text. (e.g. 'font13')\n"
194     "textColor      : [opt] hexstring - color of enabled label's label. (e.g. '0xFFFFFFFF')\n"
195     "disabledColor  : [opt] hexstring - color of disabled label's label. (e.g. '0xFFFF3300')\n"
196     "alignment      : [opt] integer - alignment of label - *Note, see xbfont.h\n"
197     "hasPath        : [opt] bool - True=stores a path / False=no path.\n"
198     "angle          : [opt] integer - angle of control. (+ rotates CCW, - rotates CW)"
199     "\n"
200     "*Note, You can use the above as keywords for arguments and skip certain optional arguments.\n"
201     "       Once you use a keyword, all following arguments require the keyword.\n"
202     "       After you create the control, you need to add it to the window with addControl().\n"
203     "\n"
204     "example:\n"
205     "  - self.label = xbmcgui.ControlLabel(100, 250, 125, 75, 'Status', angle=45)\n");
206
207   // Restore code and data sections to normal.
208 #ifndef __GNUC__
209 #pragma code_seg()
210 #pragma data_seg()
211 #pragma bss_seg()
212 #pragma const_seg()
213 #endif
214
215   PyTypeObject ControlLabel_Type;
216
217   void initControlLabel_Type()
218   {
219     PyXBMCInitializeTypeObject(&ControlLabel_Type);
220
221     ControlLabel_Type.tp_name = (char*)"xbmcgui.ControlLabel";
222     ControlLabel_Type.tp_basicsize = sizeof(ControlLabel);
223     ControlLabel_Type.tp_dealloc = (destructor)ControlLabel_Dealloc;
224     ControlLabel_Type.tp_compare = 0;
225     ControlLabel_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
226     ControlLabel_Type.tp_doc = controlLabel__doc__;
227     ControlLabel_Type.tp_methods = ControlLabel_methods;
228     ControlLabel_Type.tp_base = &Control_Type;
229     ControlLabel_Type.tp_new = ControlLabel_New;
230   }
231 }
232
233 #ifdef __cplusplus
234 }
235 #endif