1 /*
2  * This source file is part of libRocket, the HTML/CSS Interface Middleware
3  *
4  * For the latest information, see http://www.librocket.com
5  *
6  * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  *
26  */
27 
28 #include "precompiled.h"
29 #include "ElementInterface.h"
30 #include "../../../Include/Rocket/Core/Python/ConverterScriptObject.h"
31 #include "../../../Include/Rocket/Core/Factory.h"
32 #include "../../../Include/Rocket/Core/Python/ElementInstancer.h"
33 #include "../../../Include/Rocket/Core/Python/ElementWrapper.h"
34 #include "../../../Include/Rocket/Controls/ElementDataGrid.h"
35 #include "../../../Include/Rocket/Controls/ElementDataGridCell.h"
36 #include "../../../Include/Rocket/Controls/ElementDataGridRow.h"
37 #include "../../../Include/Rocket/Controls/ElementDataGridExpandButton.h"
38 #include "../../../Include/Rocket/Controls/ElementForm.h"
39 #include "../../../Include/Rocket/Controls/ElementFormControlDataSelect.h"
40 #include "../../../Include/Rocket/Controls/ElementFormControlInput.h"
41 #include "../../../Include/Rocket/Controls/ElementFormControlSelect.h"
42 #include "../../../Include/Rocket/Controls/ElementFormControlTextArea.h"
43 #include "../../../Include/Rocket/Controls/ElementTabSet.h"
44 #include "SelectOptionProxy.h"
45 #include "DataGridRowProxy.h"
46 
47 namespace Rocket {
48 namespace Controls {
49 namespace Python {
50 
51 typedef std::map< Rocket::Core::String, PyObject* > ClassDefinitions;
52 ClassDefinitions class_definitions;
53 
InitialisePythonInterface()54 void ElementInterface::InitialisePythonInterface()
55 {
56 	// ElementDataGrid.
57 	bool (ElementDataGrid::*AddColumn)(const Rocket::Core::String&, const Rocket::Core::String&, float, const Rocket::Core::String&) = &ElementDataGrid::AddColumn;
58 	class_definitions["DataGrid"] = python::class_< ElementDataGrid, Core::Python::ElementWrapper< ElementDataGrid >, boost::noncopyable, python::bases< Core::Element > >("ElementDataGrid", python::init< const char* >())
59 		.def("AddColumn", AddColumn)
60 		.def("SetDataSource", &ElementDataGrid::SetDataSource)
61 		.add_property("rows", &ElementInterface::GetRows)
62 		.ptr();
63 
64 	Rocket::Core::Python::ConverterScriptObject< ElementDataGrid > datagrid_converter;
65 
66 	// ElementDataGridRow.
67 	class_definitions["DataGridRow"] = python::class_< ElementDataGridRow, Core::Python::ElementWrapper< ElementDataGridRow >, boost::noncopyable, python::bases< Core::Element > >("ElementDataGridRow", python::init< const char* >())
68 		.add_property("row_expanded", &ElementDataGridRow::IsRowExpanded, &ElementInterface::SetRowExpanded)
69 		.add_property("parent_grid", python::make_function(&ElementDataGridRow::GetParentGrid, python::return_value_policy< python::return_by_value >()))
70 		.add_property("parent_row", python::make_function(&ElementDataGridRow::GetParentRow, python::return_value_policy< python::return_by_value >()))
71 		.add_property("parent_relative_index", &ElementDataGridRow::GetParentRelativeIndex)
72 		.add_property("table_relative_index", &ElementDataGridRow::GetTableRelativeIndex)
73 		.ptr();
74 
75 	DataGridRowProxy::InitialisePythonInterface();
76 	Rocket::Core::Python::ConverterScriptObject< ElementDataGridRow > datagridrow_converter;
77 
78 	// ElementDataGridCell.
79 	class_definitions["DataGridCell"] = python::class_< ElementDataGridCell, Core::Python::ElementWrapper< ElementDataGridCell >, boost::noncopyable, python::bases< Core::Element > >("ElementDataGridCell", python::init< const char* >())
80 		.ptr();
81 
82 	// ElementDataGridCell.
83 	class_definitions["DataGridExpand"] = python::class_< ElementDataGridExpandButton, Core::Python::ElementWrapper< ElementDataGridExpandButton >, boost::noncopyable, python::bases< Core::Element > >("ElementDataGridExpand", python::init< const char* >())
84 		.ptr();
85 
86 	// ElementForm.
87 	class_definitions["Form"] = python::class_< ElementForm,Core::Python::ElementWrapper< ElementForm >, boost::noncopyable, python::bases< Core::Element > >("Form", python::init< const char* >())
88 		.def("Submit", &ElementForm::Submit)
89 		.def("Submit", &ElementInterface::Submit)
90 		.ptr();
91 
92 	// ElementFormControl.
93 	python::class_< ElementFormControl, Core::Python::ElementWrapper< ElementFormControl >, boost::noncopyable, python::bases< Core::Element > >("IElementFormControl", python::no_init)
94 		.add_property("name", &ElementFormControl::GetName, &ElementFormControl::SetName)
95 		.add_property("value", &ElementFormControl::GetValue, &ElementFormControl::SetValue)
96 		.add_property("disabled", &ElementFormControl::IsDisabled, &ElementFormControl::SetDisabled)
97 		.ptr();
98 
99 	// ElementFormControlInput.
100 	class_definitions["FormControlInput"] = python::class_< ElementFormControlInput, Core::Python::ElementWrapper< ElementFormControlInput >, boost::noncopyable, python::bases< ElementFormControl > >("ElementFormControlInput", python::init< const char* >())
101 		.add_property("checked", &ElementInterface::GetChecked, &ElementInterface::SetChecked)
102 		.add_property("maxlength", &ElementInterface::GetMaxLength, &ElementInterface::SetMaxLength)
103 		.add_property("size", &ElementInterface::GetSize, &ElementInterface::SetSize)
104 		.add_property("max", &ElementInterface::GetMax, &ElementInterface::SetMax)
105 		.add_property("min", &ElementInterface::GetMin, &ElementInterface::SetMin)
106 		.add_property("step", &ElementInterface::GetStep, &ElementInterface::SetStep)
107 		.ptr();
108 
109 	// ElementFormControlTextArea.
110 	class_definitions["FormControlTextArea"] = python::class_< ElementFormControlTextArea,Core::Python::ElementWrapper< ElementFormControlTextArea >, boost::noncopyable, python::bases< ElementFormControl > >("ElementFormControlTextArea", python::init< const char* >())
111 		.add_property("cols", &ElementFormControlTextArea::GetNumColumns, &ElementFormControlTextArea::SetNumColumns)
112 		.add_property("rows", &ElementFormControlTextArea::GetNumRows, &ElementFormControlTextArea::SetNumRows)
113 		.add_property("wordwrap", &ElementFormControlTextArea::GetWordWrap, &ElementFormControlTextArea::SetWordWrap)
114 		.add_property("maxlength", &ElementFormControlTextArea::GetMaxLength, &ElementFormControlTextArea::SetMaxLength)
115 		.ptr();
116 
117 	// ElementFormControlSelect.
118 	SelectOptionProxy::InitialisePythonInterface();
119 	class_definitions["FormControlSelect"] = python::class_< ElementFormControlSelect, Core::Python::ElementWrapper< ElementFormControlSelect >, boost::noncopyable, python::bases< ElementFormControl > >("ElementFormControlSelect", python::init< const char* >())
120 		.def("Add", &ElementFormControlSelect::Add)
121 		.def("Add", &ElementInterface::Add)
122 		.def("Remove", &ElementFormControlSelect::Remove)
123 		.add_property("options", &ElementInterface::GetOptions)
124 		.add_property("selection", &ElementFormControlSelect::GetSelection, &ElementFormControlSelect::SetSelection)
125 		.ptr();
126 
127 	// ElementFormControlDataSelect.
128 	class_definitions["FormControlDataSelect"] = python::class_< ElementFormControlDataSelect, Core::Python::ElementWrapper< ElementFormControlDataSelect >, boost::noncopyable, python::bases< ElementFormControlSelect > >("ElementFormControlDataSelect", python::init< const char* >())
129 		.def("SetDataSource", &ElementFormControlDataSelect::SetDataSource)
130 		.ptr();
131 
132 	// ElementTabSet.
133 	void (ElementTabSet::*SetTab)(int, const Rocket::Core::String&) = &ElementTabSet::SetTab;
134 	void (ElementTabSet::*SetPanel)(int, const Rocket::Core::String&) = &ElementTabSet::SetPanel;
135 	class_definitions["TabSet"] = python::class_< ElementTabSet, Core::Python::ElementWrapper< ElementTabSet >, boost::noncopyable, python::bases< Core::Element > >("ElementTabSet", python::init< const char* >())
136 		.add_property("num_tabs", &ElementTabSet::GetNumTabs)
137 		.def("SetTab", SetTab)
138 		.def("SetPanel", SetPanel)
139 		.add_property("active_tab", &ElementTabSet::GetActiveTab, &ElementTabSet::SetActiveTab)
140 		.ptr();
141 }
142 
InitialiseRocketInterface()143 void ElementInterface::InitialiseRocketInterface()
144 {
145 	Core::Factory::RegisterElementInstancer("datagrid", new Core::Python::ElementInstancer( (*class_definitions.find("DataGrid")).second))->RemoveReference();
146 	Core::Factory::RegisterElementInstancer("datagridexpand", new Core::Python::ElementInstancer( (*class_definitions.find("DataGridExpand")).second))->RemoveReference();
147 	Core::Factory::RegisterElementInstancer("#rktctl_datagridrow", new Core::Python::ElementInstancer( (*class_definitions.find("DataGridRow")).second))->RemoveReference();
148 	Core::Factory::RegisterElementInstancer("#rktctl_datagridcell", new Core::Python::ElementInstancer( (*class_definitions.find("DataGridCell")).second))->RemoveReference();
149 
150 	Core::Factory::RegisterElementInstancer("form", new Core::Python::ElementInstancer((*class_definitions.find("Form")).second ))->RemoveReference();
151 	Core::Factory::RegisterElementInstancer("input", new Core::Python::ElementInstancer((*class_definitions.find("FormControlInput")).second ))->RemoveReference();
152 	Core::Factory::RegisterElementInstancer("textarea", new Core::Python::ElementInstancer((*class_definitions.find("FormControlTextArea")).second ))->RemoveReference();
153 	Core::Factory::RegisterElementInstancer("dataselect", new Core::Python::ElementInstancer((*class_definitions.find("FormControlDataSelect")).second ))->RemoveReference();
154 	Core::Factory::RegisterElementInstancer("select", new Core::Python::ElementInstancer((*class_definitions.find("FormControlSelect")).second ))->RemoveReference();
155 	Core::Factory::RegisterElementInstancer("tabset", new Core::Python::ElementInstancer((*class_definitions.find("TabSet")).second ))->RemoveReference();
156 }
157 
158 // Sets the expanded state of a data grid row.
SetRowExpanded(ElementDataGridRow * element,bool row_expanded)159 void ElementInterface::SetRowExpanded(ElementDataGridRow* element, bool row_expanded)
160 {
161 	if (row_expanded)
162 		element->ExpandRow();
163 	else
164 		element->CollapseRow();
165 }
166 
167 // Returns the options proxy for a select element.
GetOptions(ElementFormControlSelect * element)168 SelectOptionProxy ElementInterface::GetOptions(ElementFormControlSelect* element)
169 {
170 	return SelectOptionProxy(element);
171 }
172 
173 // Override for ElementFormControlSelect's Add() without the last parameter.
Add(ElementFormControlSelect * element,const Rocket::Core::String & rml,const Rocket::Core::String & value)174 int ElementInterface::Add(ElementFormControlSelect* element, const Rocket::Core::String& rml, const Rocket::Core::String& value)
175 {
176 	return element->Add(rml, value);
177 }
178 
179 // Default parameter submit for forms
Submit(ElementForm * element)180 void ElementInterface::Submit(ElementForm* element)
181 {
182 	element->Submit();
183 }
184 
GetChecked(ElementFormControlInput * element)185 bool ElementInterface::GetChecked(ElementFormControlInput* element)
186 {
187 	return element->HasAttribute("checked");
188 }
189 
SetChecked(ElementFormControlInput * element,bool checked)190 void ElementInterface::SetChecked(ElementFormControlInput* element, bool checked)
191 {
192 	if (checked)
193 		element->SetAttribute("checked", true);
194 	else
195 		element->RemoveAttribute("checked");
196 }
197 
GetMaxLength(ElementFormControlInput * element)198 int ElementInterface::GetMaxLength(ElementFormControlInput* element)
199 {
200 	return element->GetAttribute<int>("maxlength", -1);
201 }
202 
SetMaxLength(ElementFormControlInput * element,int max_length)203 void ElementInterface::SetMaxLength(ElementFormControlInput* element, int max_length)
204 {
205 	element->SetAttribute("maxlength", max_length);
206 }
207 
GetSize(ElementFormControlInput * element)208 int ElementInterface::GetSize(ElementFormControlInput* element)
209 {
210 	return element->GetAttribute<int>("size", 20);
211 }
212 
SetSize(ElementFormControlInput * element,int size)213 void ElementInterface::SetSize(ElementFormControlInput* element, int size)
214 {
215 	element->SetAttribute("size", size);
216 }
217 
GetMin(ElementFormControlInput * element)218 int ElementInterface::GetMin(ElementFormControlInput* element)
219 {
220 	return element->GetAttribute<int>("min", 0);
221 }
222 
SetMin(ElementFormControlInput * element,int min)223 void ElementInterface::SetMin(ElementFormControlInput* element, int min)
224 {
225 	element->SetAttribute("min", min);
226 }
227 
GetMax(ElementFormControlInput * element)228 int ElementInterface::GetMax(ElementFormControlInput* element)
229 {
230 	return element->GetAttribute<int>("max", 100);
231 }
232 
SetMax(ElementFormControlInput * element,int max)233 void ElementInterface::SetMax(ElementFormControlInput* element, int max)
234 {
235 	element->SetAttribute("max", max);
236 }
237 
GetStep(ElementFormControlInput * element)238 int ElementInterface::GetStep(ElementFormControlInput* element)
239 {
240 	return element->GetAttribute<int>("step", 1);
241 }
242 
SetStep(ElementFormControlInput * element,int step)243 void ElementInterface::SetStep(ElementFormControlInput* element, int step)
244 {
245 	element->SetAttribute("step", step);
246 }
247 
GetRows(ElementDataGrid * element)248 DataGridRowProxy ElementInterface::GetRows(ElementDataGrid* element)
249 {
250 	return DataGridRowProxy(element);
251 }
252 
253 }
254 }
255 }
256