Merge remote branch 'mine/ext-python'
[vuplus_xbmc] / xbmc / interfaces / python / xbmcmodule / controlslider.cpp
1 /*
2  *      Copyright (C) 2005-2010 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 "guilib/GUISliderControl.h"
25 #include "control.h"
26 #include "pyutil.h"
27 #include "utils/log.h"
28 using namespace std;
29
30 #ifndef __GNUC__
31 #pragma code_seg("PY_TEXT")
32 #pragma data_seg("PY_DATA")
33 #pragma bss_seg("PY_BSS")
34 #pragma const_seg("PY_RDATA")
35 #endif
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 namespace PYXBMC
42 {
43   PyObject * ControlSlider_New (PyTypeObject *type, PyObject *args, PyObject *kwds)
44   {
45     static const char* keywords[] = { "x", "y", "width", "height", "textureback", "texture", "texturefocus", NULL };
46         
47     ControlSlider *self;
48     char *cTextureBack = NULL;
49     char *cTexture = NULL;
50     char *cTextureFoc  = NULL;
51         
52     self = (ControlSlider *) type->tp_alloc (type, 0);
53     if (!self) return NULL;
54     new(&self->strTextureBack) string();
55     new(&self->strTexture) string();
56     new(&self->strTextureFoc) string();    
57         
58     if (!PyArg_ParseTupleAndKeywords(args, kwds,
59                                      (char*)"llll|sss",
60                                      (char**)keywords,
61                                      &self->dwPosX,
62                                      &self->dwPosY,
63                                      &self->dwWidth,
64                                      &self->dwHeight,
65                                      &cTextureBack,
66                                      &cTexture,
67                                      &cTextureFoc))
68     {
69       Py_DECREF( self );
70       return NULL;
71     }
72     // if texture is supplied use it, else get default ones
73     self->strTextureBack = cTextureBack ? cTextureBack : PyXBMCGetDefaultImage((char*)"slider", (char*)"texturesliderbar", (char*)"osd_slider_bg_2.png");
74     self->strTexture = cTexture ? cTexture : PyXBMCGetDefaultImage((char*)"slider", (char*)"textureslidernib", (char*)"osd_slider_nibNF.png");
75     self->strTextureFoc = cTextureFoc ? cTextureFoc : PyXBMCGetDefaultImage((char*)"slider", (char*)"textureslidernibfocus", (char*)"osd_slider_nib.png");
76     
77     return (PyObject*)self;
78   }
79   
80   void ControlSlider_Dealloc(ControlSlider* self)
81   {
82     self->strTextureBack.~string();
83     self->strTexture.~string();
84     self->strTextureFoc.~string();
85     self->ob_type->tp_free((PyObject*)self);
86   }
87
88   CGUIControl* ControlSlider_Create (ControlSlider* pControl)
89   {
90     
91     pControl->pGUIControl = new CGUISliderControl(pControl->iParentId, pControl->iControlId,(float)pControl->dwPosX, (float)pControl->dwPosY,
92               (float)pControl->dwWidth,(float)pControl->dwHeight,
93               (CStdString)pControl->strTextureBack,(CStdString)pControl->strTexture,
94               (CStdString)pControl->strTextureFoc,0);   
95     
96     
97     return pControl->pGUIControl;
98   }  
99
100   PyDoc_STRVAR(getPercent__doc__,
101     "getPercent() -- Returns a float of the percent of the slider.\n"
102     "\n"
103     "example:\n"
104     "  - print self.slider.getPercent()\n");
105
106   PyObject* ControlSlider_GetPercent(ControlSlider*self, PyObject *args)
107   {
108     if (self->pGUIControl)
109     {
110       float fPercent = (float)((CGUISliderControl*)self->pGUIControl)->GetPercentage();
111       return Py_BuildValue((char*)"f", fPercent);
112     }
113     return Py_BuildValue((char*)"f", 0);
114   }
115
116   PyDoc_STRVAR(setPercent__doc__,
117     "setPercent(50) -- Sets the percent of the slider.\n"
118     "\n"
119     "example:\n"
120     "self.slider.setPercent(50)\n");
121
122   PyObject* ControlSlider_SetPercent(ControlProgress *self, PyObject *args)
123   {
124     float fPercent = 0;
125     if (!PyArg_ParseTuple(args, (char*)"f", &fPercent)) return NULL;
126         
127     if (self->pGUIControl)
128       ((CGUISliderControl*)self->pGUIControl)->SetPercentage((int)fPercent);
129         
130     Py_INCREF(Py_None);
131     return Py_None;
132   }  
133         
134         
135   PyMethodDef ControlSlider_methods[] = {
136     {(char*)"getPercent", (PyCFunction)ControlSlider_GetPercent, METH_VARARGS, getPercent__doc__},
137     {(char*)"setPercent", (PyCFunction)ControlSlider_SetPercent, METH_VARARGS, setPercent__doc__},  
138     {NULL, NULL, 0, NULL}
139   };
140
141   // ControlProgress class
142   PyDoc_STRVAR(ControlSlider__doc__,
143    "ControlSlider class.\n"
144    "\n"
145    "ControlSlider(x, y, width, height[, textureback, texture, texturefocus])\n"
146    "\n"
147    "x              : integer - x coordinate of control.\n"
148    "y              : integer - y coordinate of control.\n"
149    "width          : integer - width of control.\n"
150    "height         : integer - height of control.\n"
151    "textureback    : [opt] string - image filename.\n"
152    "texture        : [opt] string - image filename.\n"
153    "texturefocus   : [opt] string - image filename.\n"               
154    "*Note, You can use the above as keywords for arguments and skip certain optional arguments.\n"
155    "       Once you use a keyword, all following arguments require the keyword.\n"
156    "       After you create the control, you need to add it to the window with addControl().\n"
157    "\n"
158    "example:\n"
159    "  - self.slider = xbmcgui.ControlSlider(100, 250, 350, 40)\n");
160
161 // Restore code and data sections to normal.
162 #ifndef __GNUC__
163 #pragma code_seg()
164 #pragma data_seg()
165 #pragma bss_seg()
166 #pragma const_seg()
167 #endif
168
169   PyTypeObject ControlSlider_Type;
170
171   void initControlSlider_Type()
172   {
173     PyXBMCInitializeTypeObject(&ControlSlider_Type);
174
175     ControlSlider_Type.tp_name = (char*)"xbmcgui.ControlSlider";
176     ControlSlider_Type.tp_basicsize = sizeof(ControlSlider);
177     ControlSlider_Type.tp_dealloc = (destructor)ControlSlider_Dealloc;
178     ControlSlider_Type.tp_compare = 0;
179     ControlSlider_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
180     ControlSlider_Type.tp_doc = ControlSlider__doc__;
181     ControlSlider_Type.tp_methods = ControlSlider_methods;
182     ControlSlider_Type.tp_base = &Control_Type;
183     ControlSlider_Type.tp_new = ControlSlider_New;
184   }
185 }
186
187 #ifdef __cplusplus
188 }
189 #endif