ElementFormControlDataSelect.cpp 7.5 KB

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