ElementInterface.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #include "precompiled.h"
  28. #include "ElementInterface.h"
  29. #include "../../../Include/Rocket/Core/Python/ConverterScriptObject.h"
  30. #include "../../../Include/Rocket/Core/Factory.h"
  31. #include "../../../Include/Rocket/Core/Python/ElementInstancer.h"
  32. #include "../../../Include/Rocket/Core/Python/ElementWrapper.h"
  33. #include "../../../Include/Rocket/Controls/ElementDataGrid.h"
  34. #include "../../../Include/Rocket/Controls/ElementDataGridCell.h"
  35. #include "../../../Include/Rocket/Controls/ElementDataGridRow.h"
  36. #include "../../../Include/Rocket/Controls/ElementDataGridExpandButton.h"
  37. #include "../../../Include/Rocket/Controls/ElementForm.h"
  38. #include "../../../Include/Rocket/Controls/ElementFormControlDataSelect.h"
  39. #include "../../../Include/Rocket/Controls/ElementFormControlInput.h"
  40. #include "../../../Include/Rocket/Controls/ElementFormControlSelect.h"
  41. #include "../../../Include/Rocket/Controls/ElementFormControlTextArea.h"
  42. #include "../../../Include/Rocket/Controls/ElementTabSet.h"
  43. #include "SelectOptionProxy.h"
  44. #include "DataGridRowProxy.h"
  45. namespace Rocket {
  46. namespace Controls {
  47. namespace Python {
  48. typedef std::map< Rocket::Core::String, PyObject* > ClassDefinitions;
  49. ClassDefinitions class_definitions;
  50. void ElementInterface::InitialisePythonInterface()
  51. {
  52. // ElementDataGrid.
  53. bool (ElementDataGrid::*AddColumn)(const Rocket::Core::String&, const Rocket::Core::String&, float, const Rocket::Core::String&) = &ElementDataGrid::AddColumn;
  54. class_definitions["DataGrid"] = python::class_< ElementDataGrid, Core::Python::ElementWrapper< ElementDataGrid >, boost::noncopyable, python::bases< Core::Element > >("ElementDataGrid", python::init< const char* >())
  55. .def("AddColumn", AddColumn)
  56. .def("SetDataSource", &ElementDataGrid::SetDataSource)
  57. .add_property("rows", &ElementInterface::GetRows)
  58. .ptr();
  59. Rocket::Core::Python::ConverterScriptObject< ElementDataGrid > datagrid_converter;
  60. // ElementDataGridRow.
  61. class_definitions["DataGridRow"] = python::class_< ElementDataGridRow, Core::Python::ElementWrapper< ElementDataGridRow >, boost::noncopyable, python::bases< Core::Element > >("ElementDataGridRow", python::init< const char* >())
  62. .add_property("row_expanded", &ElementDataGridRow::IsRowExpanded, &ElementInterface::SetRowExpanded)
  63. .add_property("parent_grid", python::make_function(&ElementDataGridRow::GetParentGrid, python::return_value_policy< python::return_by_value >()))
  64. .add_property("parent_row", python::make_function(&ElementDataGridRow::GetParentRow, python::return_value_policy< python::return_by_value >()))
  65. .add_property("parent_relative_index", &ElementDataGridRow::GetParentRelativeIndex)
  66. .add_property("table_relative_index", &ElementDataGridRow::GetTableRelativeIndex)
  67. .ptr();
  68. DataGridRowProxy::InitialisePythonInterface();
  69. Rocket::Core::Python::ConverterScriptObject< ElementDataGridRow > datagridrow_converter;
  70. // ElementDataGridCell.
  71. class_definitions["DataGridCell"] = python::class_< ElementDataGridCell, Core::Python::ElementWrapper< ElementDataGridCell >, boost::noncopyable, python::bases< Core::Element > >("ElementDataGridCell", python::init< const char* >())
  72. .ptr();
  73. // ElementDataGridCell.
  74. class_definitions["DataGridExpand"] = python::class_< ElementDataGridExpandButton, Core::Python::ElementWrapper< ElementDataGridExpandButton >, boost::noncopyable, python::bases< Core::Element > >("ElementDataGridExpand", python::init< const char* >())
  75. .ptr();
  76. // ElementForm.
  77. class_definitions["Form"] = python::class_< ElementForm,Core::Python::ElementWrapper< ElementForm >, boost::noncopyable, python::bases< Core::Element > >("Form", python::init< const char* >())
  78. .def("Submit", &ElementForm::Submit)
  79. .def("Submit", &ElementInterface::Submit)
  80. .ptr();
  81. // ElementFormControl.
  82. python::class_< ElementFormControl, Core::Python::ElementWrapper< ElementFormControl >, boost::noncopyable, python::bases< Core::Element > >("IElementFormControl", python::no_init)
  83. .add_property("name", &ElementFormControl::GetName, &ElementFormControl::SetName)
  84. .add_property("value", &ElementFormControl::GetValue, &ElementFormControl::SetValue)
  85. .add_property("disabled", &ElementFormControl::IsDisabled, &ElementFormControl::SetDisabled)
  86. .ptr();
  87. // ElementFormControlInput.
  88. class_definitions["FormControlInput"] = python::class_< ElementFormControlInput, Core::Python::ElementWrapper< ElementFormControlInput >, boost::noncopyable, python::bases< ElementFormControl > >("ElementFormControlInput", python::init< const char* >())
  89. .add_property("checked", &ElementInterface::GetChecked, &ElementInterface::SetChecked)
  90. .add_property("maxlength", &ElementInterface::GetMaxLength, &ElementInterface::SetMaxLength)
  91. .add_property("size", &ElementInterface::GetSize, &ElementInterface::SetSize)
  92. .add_property("max", &ElementInterface::GetMax, &ElementInterface::SetMax)
  93. .add_property("min", &ElementInterface::GetMin, &ElementInterface::SetMin)
  94. .add_property("step", &ElementInterface::GetStep, &ElementInterface::SetStep)
  95. .ptr();
  96. // ElementFormControlTextArea.
  97. class_definitions["FormControlTextArea"] = python::class_< ElementFormControlTextArea,Core::Python::ElementWrapper< ElementFormControlTextArea >, boost::noncopyable, python::bases< ElementFormControl > >("ElementFormControlTextArea", python::init< const char* >())
  98. .add_property("cols", &ElementFormControlTextArea::GetNumColumns, &ElementFormControlTextArea::SetNumColumns)
  99. .add_property("rows", &ElementFormControlTextArea::GetNumRows, &ElementFormControlTextArea::SetNumRows)
  100. .add_property("wordwrap", &ElementFormControlTextArea::GetWordWrap, &ElementFormControlTextArea::SetWordWrap)
  101. .add_property("maxlength", &ElementFormControlTextArea::GetMaxLength, &ElementFormControlTextArea::SetMaxLength)
  102. .ptr();
  103. // ElementFormControlSelect.
  104. SelectOptionProxy::InitialisePythonInterface();
  105. class_definitions["FormControlSelect"] = python::class_< ElementFormControlSelect, Core::Python::ElementWrapper< ElementFormControlSelect >, boost::noncopyable, python::bases< ElementFormControl > >("ElementFormControlSelect", python::init< const char* >())
  106. .def("Add", &ElementFormControlSelect::Add)
  107. .def("Add", &ElementInterface::Add)
  108. .def("Remove", &ElementFormControlSelect::Remove)
  109. .add_property("options", &ElementInterface::GetOptions)
  110. .add_property("selection", &ElementFormControlSelect::GetSelection, &ElementFormControlSelect::SetSelection)
  111. .ptr();
  112. // ElementFormControlDataSelect.
  113. class_definitions["FormControlDataSelect"] = python::class_< ElementFormControlDataSelect, Core::Python::ElementWrapper< ElementFormControlDataSelect >, boost::noncopyable, python::bases< ElementFormControlSelect > >("ElementFormControlDataSelect", python::init< const char* >())
  114. .def("SetDataSource", &ElementFormControlDataSelect::SetDataSource)
  115. .ptr();
  116. // ElementTabSet.
  117. void (ElementTabSet::*SetTab)(int, const Rocket::Core::String&) = &ElementTabSet::SetTab;
  118. void (ElementTabSet::*SetPanel)(int, const Rocket::Core::String&) = &ElementTabSet::SetPanel;
  119. class_definitions["TabSet"] = python::class_< ElementTabSet, Core::Python::ElementWrapper< ElementTabSet >, boost::noncopyable, python::bases< Core::Element > >("ElementTabSet", python::init< const char* >())
  120. .add_property("num_tabs", &ElementTabSet::GetNumTabs)
  121. .def("SetTab", SetTab)
  122. .def("SetPanel", SetPanel)
  123. .add_property("active_tab", &ElementTabSet::GetActiveTab, &ElementTabSet::SetActiveTab)
  124. .ptr();
  125. }
  126. void ElementInterface::InitialiseRocketInterface()
  127. {
  128. Core::Factory::RegisterElementInstancer("datagrid", new Core::Python::ElementInstancer( (*class_definitions.find("DataGrid")).second))->RemoveReference();
  129. Core::Factory::RegisterElementInstancer("datagridexpand", new Core::Python::ElementInstancer( (*class_definitions.find("DataGridExpand")).second))->RemoveReference();
  130. Core::Factory::RegisterElementInstancer("#rktctl_datagridrow", new Core::Python::ElementInstancer( (*class_definitions.find("DataGridRow")).second))->RemoveReference();
  131. Core::Factory::RegisterElementInstancer("#rktctl_datagridcell", new Core::Python::ElementInstancer( (*class_definitions.find("DataGridCell")).second))->RemoveReference();
  132. Core::Factory::RegisterElementInstancer("form", new Core::Python::ElementInstancer((*class_definitions.find("Form")).second ))->RemoveReference();
  133. Core::Factory::RegisterElementInstancer("input", new Core::Python::ElementInstancer((*class_definitions.find("FormControlInput")).second ))->RemoveReference();
  134. Core::Factory::RegisterElementInstancer("textarea", new Core::Python::ElementInstancer((*class_definitions.find("FormControlTextArea")).second ))->RemoveReference();
  135. Core::Factory::RegisterElementInstancer("dataselect", new Core::Python::ElementInstancer((*class_definitions.find("FormControlDataSelect")).second ))->RemoveReference();
  136. Core::Factory::RegisterElementInstancer("select", new Core::Python::ElementInstancer((*class_definitions.find("FormControlSelect")).second ))->RemoveReference();
  137. Core::Factory::RegisterElementInstancer("tabset", new Core::Python::ElementInstancer((*class_definitions.find("TabSet")).second ))->RemoveReference();
  138. }
  139. // Sets the expanded state of a data grid row.
  140. void ElementInterface::SetRowExpanded(ElementDataGridRow* element, bool row_expanded)
  141. {
  142. if (row_expanded)
  143. element->ExpandRow();
  144. else
  145. element->CollapseRow();
  146. }
  147. // Returns the options proxy for a select element.
  148. SelectOptionProxy ElementInterface::GetOptions(ElementFormControlSelect* element)
  149. {
  150. return SelectOptionProxy(element);
  151. }
  152. // Override for ElementFormControlSelect's Add() without the last parameter.
  153. int ElementInterface::Add(ElementFormControlSelect* element, const Rocket::Core::String& rml, const Rocket::Core::String& value)
  154. {
  155. return element->Add(rml, value);
  156. }
  157. // Default parameter submit for forms
  158. void ElementInterface::Submit(ElementForm* element)
  159. {
  160. element->Submit();
  161. }
  162. bool ElementInterface::GetChecked(ElementFormControlInput* element)
  163. {
  164. return element->HasAttribute("checked");
  165. }
  166. void ElementInterface::SetChecked(ElementFormControlInput* element, bool checked)
  167. {
  168. if (checked)
  169. element->SetAttribute("checked", true);
  170. else
  171. element->RemoveAttribute("checked");
  172. }
  173. int ElementInterface::GetMaxLength(ElementFormControlInput* element)
  174. {
  175. return element->GetAttribute<int>("maxlength", -1);
  176. }
  177. void ElementInterface::SetMaxLength(ElementFormControlInput* element, int max_length)
  178. {
  179. element->SetAttribute("maxlength", max_length);
  180. }
  181. int ElementInterface::GetSize(ElementFormControlInput* element)
  182. {
  183. return element->GetAttribute<int>("size", 20);
  184. }
  185. void ElementInterface::SetSize(ElementFormControlInput* element, int size)
  186. {
  187. element->SetAttribute("size", size);
  188. }
  189. int ElementInterface::GetMin(ElementFormControlInput* element)
  190. {
  191. return element->GetAttribute<int>("min", 0);
  192. }
  193. void ElementInterface::SetMin(ElementFormControlInput* element, int min)
  194. {
  195. element->SetAttribute("min", min);
  196. }
  197. int ElementInterface::GetMax(ElementFormControlInput* element)
  198. {
  199. return element->GetAttribute<int>("max", 100);
  200. }
  201. void ElementInterface::SetMax(ElementFormControlInput* element, int max)
  202. {
  203. element->SetAttribute("max", max);
  204. }
  205. int ElementInterface::GetStep(ElementFormControlInput* element)
  206. {
  207. return element->GetAttribute<int>("step", 1);
  208. }
  209. void ElementInterface::SetStep(ElementFormControlInput* element, int step)
  210. {
  211. element->SetAttribute("step", step);
  212. }
  213. DataGridRowProxy ElementInterface::GetRows(ElementDataGrid* element)
  214. {
  215. return DataGridRowProxy(element);
  216. }
  217. }
  218. }
  219. }