ElementFormControlDataSelect.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "../../Include/Rocket/Controls/ElementFormControlDataSelect.h"
  28. #include "../../Include/Rocket/Controls/DataQuery.h"
  29. #include "../../Include/Rocket/Controls/DataSource.h"
  30. #include "../../Include/Rocket/Controls/DataFormatter.h"
  31. #include "WidgetDropDown.h"
  32. namespace Rocket {
  33. namespace Controls {
  34. // Constructs a new ElementFormControlDataSelect.
  35. ElementFormControlDataSelect::ElementFormControlDataSelect(const Rocket::Core::String& tag) : ElementFormControlSelect(tag)
  36. {
  37. data_source = NULL;
  38. initialised = false;
  39. }
  40. ElementFormControlDataSelect::~ElementFormControlDataSelect()
  41. {
  42. if (data_source != NULL) {
  43. data_source->DetachListener(this);
  44. data_source = NULL;
  45. }
  46. }
  47. // Sets the data source the control's options are driven from.
  48. void ElementFormControlDataSelect::SetDataSource(const Rocket::Core::String& _data_source)
  49. {
  50. SetAttribute("source", _data_source);
  51. }
  52. // If a new data source has been set on the control, this will attach to it and build the initial
  53. // options.
  54. void ElementFormControlDataSelect::OnUpdate()
  55. {
  56. if (!initialised)
  57. {
  58. initialised = true;
  59. if (ParseDataSource(data_source, data_table, GetAttribute< Rocket::Core::String >("source", "")))
  60. {
  61. data_source->AttachListener(this);
  62. BuildOptions();
  63. }
  64. }
  65. }
  66. // Checks for changes to the data source or formatting attributes.
  67. void ElementFormControlDataSelect::OnAttributeChange(const Core::AttributeNameList& changed_attributes)
  68. {
  69. ElementFormControlSelect::OnAttributeChange(changed_attributes);
  70. if (changed_attributes.find("source") != changed_attributes.end())
  71. {
  72. if (data_source != NULL) {
  73. data_source->DetachListener(this);
  74. data_source = NULL;
  75. }
  76. initialised = false;
  77. }
  78. else if (changed_attributes.find("fields") != changed_attributes.end() ||
  79. changed_attributes.find("valuefield") != changed_attributes.end() ||
  80. changed_attributes.find("formatter") != changed_attributes.end())
  81. {
  82. BuildOptions();
  83. }
  84. }
  85. // Detaches from the data source and rebuilds the options.
  86. void ElementFormControlDataSelect::OnDataSourceDestroy(DataSource* _data_source)
  87. {
  88. if (data_source == _data_source)
  89. {
  90. data_source->DetachListener(this);
  91. data_source = NULL;
  92. data_table = "";
  93. BuildOptions();
  94. }
  95. }
  96. // Rebuilds the available options from the data source.
  97. void ElementFormControlDataSelect::OnRowAdd(DataSource* ROCKET_UNUSED_PARAMETER(data_source), const Rocket::Core::String& table, int ROCKET_UNUSED_PARAMETER(first_row_added), int ROCKET_UNUSED_PARAMETER(num_rows_added))
  98. {
  99. ROCKET_UNUSED(data_source);
  100. ROCKET_UNUSED(first_row_added);
  101. ROCKET_UNUSED(num_rows_added);
  102. if (table == data_table)
  103. BuildOptions();
  104. }
  105. // Rebuilds the available options from the data source.
  106. void ElementFormControlDataSelect::OnRowRemove(DataSource* ROCKET_UNUSED_PARAMETER(data_source), const Rocket::Core::String& table, int ROCKET_UNUSED_PARAMETER(first_row_removed), int ROCKET_UNUSED_PARAMETER(num_rows_removed))
  107. {
  108. ROCKET_UNUSED(data_source);
  109. ROCKET_UNUSED(first_row_removed);
  110. ROCKET_UNUSED(num_rows_removed);
  111. if (table == data_table)
  112. BuildOptions();
  113. }
  114. // Rebuilds the available options from the data source.
  115. void ElementFormControlDataSelect::OnRowChange(DataSource* ROCKET_UNUSED_PARAMETER(data_source), const Rocket::Core::String& table, int ROCKET_UNUSED_PARAMETER(first_row_changed), int ROCKET_UNUSED_PARAMETER(num_rows_changed))
  116. {
  117. ROCKET_UNUSED(data_source);
  118. ROCKET_UNUSED(first_row_changed);
  119. ROCKET_UNUSED(num_rows_changed);
  120. if (table == data_table)
  121. BuildOptions();
  122. }
  123. // Rebuilds the available options from the data source.
  124. void ElementFormControlDataSelect::OnRowChange(DataSource* ROCKET_UNUSED_PARAMETER(data_source), const Rocket::Core::String& table)
  125. {
  126. ROCKET_UNUSED(data_source);
  127. if (table == data_table)
  128. BuildOptions();
  129. }
  130. // Builds the option list from the data source.
  131. void ElementFormControlDataSelect::BuildOptions()
  132. {
  133. widget->ClearOptions();
  134. if (data_source == NULL)
  135. return;
  136. // Store the old selection value and index. These will be used to restore the selection to the
  137. // most appropriate option after the options have been rebuilt.
  138. Rocket::Core::String old_value = GetValue();
  139. int old_selection = GetSelection();
  140. Rocket::Core::String fields_attribute = GetAttribute<Rocket::Core::String>("fields", "");
  141. Rocket::Core::String valuefield_attribute = GetAttribute<Rocket::Core::String>("valuefield", "");
  142. Rocket::Core::String data_formatter_attribute = GetAttribute<Rocket::Core::String>("formatter", "");
  143. DataFormatter* data_formatter = NULL;
  144. // Process the attributes.
  145. if (fields_attribute.Empty())
  146. {
  147. Core::Log::Message(Rocket::Core::Log::LT_ERROR, "DataQuery failed, no fields specified for %s.", GetTagName().CString());
  148. return;
  149. }
  150. if (valuefield_attribute.Empty())
  151. {
  152. valuefield_attribute = fields_attribute.Substring(0, fields_attribute.Find(","));
  153. }
  154. if (!data_formatter_attribute.Empty())
  155. {
  156. data_formatter = DataFormatter::GetDataFormatter(data_formatter_attribute);
  157. if (!data_formatter)
  158. Core::Log::Message(Rocket::Core::Log::LT_WARNING, "Unable to find data formatter named '%s', formatting skipped.", data_formatter_attribute.CString());
  159. }
  160. // Build a list of attributes
  161. Rocket::Core::String fields(valuefield_attribute);
  162. fields += ",";
  163. fields += fields_attribute;
  164. DataQuery query(data_source, data_table, fields);
  165. while (query.NextRow())
  166. {
  167. Rocket::Core::StringList fields;
  168. Rocket::Core::String value = query.Get<Rocket::Core::String>(0, "");
  169. for (size_t i = 1; i < query.GetNumFields(); ++i)
  170. fields.push_back(query.Get< Rocket::Core::String>(i, ""));
  171. Rocket::Core::String formatted("");
  172. if (fields.size() > 0)
  173. formatted = fields[0];
  174. if (data_formatter)
  175. data_formatter->FormatData(formatted, fields);
  176. // Add the data as an option.
  177. widget->AddOption(formatted, value, -1, false);
  178. }
  179. // If an option was selected before, attempt to restore the selection to it.
  180. if (old_selection > -1)
  181. {
  182. // Try to find a selection with the same value as the previous one.
  183. for (int i = 0; i < GetNumOptions(); ++i)
  184. {
  185. SelectOption* option = GetOption(i);
  186. if (option->GetValue() == old_value)
  187. {
  188. widget->SetSelection(i, true);
  189. return;
  190. }
  191. }
  192. // Failed to find an option with the same value. Attempt to at least set the same index.
  193. int new_selection = Rocket::Core::Math::Clamp(old_selection, 0, GetNumOptions() - 1);
  194. if (GetNumOptions() == 0)
  195. new_selection = -1;
  196. widget->SetSelection(new_selection, true);
  197. }
  198. }
  199. }
  200. }