ElementFormControlDataSelect.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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(data_source), const Rocket::Core::String& table, int ROCKET_UNUSED(first_row_added), int ROCKET_UNUSED(num_rows_added))
  94. {
  95. if (table == data_table)
  96. BuildOptions();
  97. }
  98. // Rebuilds the available options from the data source.
  99. void ElementFormControlDataSelect::OnRowRemove(DataSource* ROCKET_UNUSED(data_source), const Rocket::Core::String& table, int ROCKET_UNUSED(first_row_removed), int ROCKET_UNUSED(num_rows_removed))
  100. {
  101. if (table == data_table)
  102. BuildOptions();
  103. }
  104. // Rebuilds the available options from the data source.
  105. void ElementFormControlDataSelect::OnRowChange(DataSource* ROCKET_UNUSED(data_source), const Rocket::Core::String& table, int ROCKET_UNUSED(first_row_changed), int ROCKET_UNUSED(num_rows_changed))
  106. {
  107. if (table == data_table)
  108. BuildOptions();
  109. }
  110. // Rebuilds the available options from the data source.
  111. void ElementFormControlDataSelect::OnRowChange(DataSource* ROCKET_UNUSED(data_source), const Rocket::Core::String& table)
  112. {
  113. if (table == data_table)
  114. BuildOptions();
  115. }
  116. // Builds the option list from the data source.
  117. void ElementFormControlDataSelect::BuildOptions()
  118. {
  119. widget->ClearOptions();
  120. if (data_source == NULL)
  121. return;
  122. // Store the old selection value and index. These will be used to restore the selection to the
  123. // most appropriate option after the options have been rebuilt.
  124. Rocket::Core::String old_value = GetValue();
  125. int old_selection = GetSelection();
  126. Rocket::Core::String fields_attribute = GetAttribute<Rocket::Core::String>("fields", "");
  127. Rocket::Core::String valuefield_attribute = GetAttribute<Rocket::Core::String>("valuefield", "");
  128. Rocket::Core::String data_formatter_attribute = GetAttribute<Rocket::Core::String>("formatter", "");
  129. DataFormatter* data_formatter = NULL;
  130. // Process the attributes.
  131. if (fields_attribute.Empty())
  132. {
  133. Core::Log::Message(Rocket::Core::Log::LT_ERROR, "DataQuery failed, no fields specified for %s.", GetTagName().CString());
  134. return;
  135. }
  136. if (valuefield_attribute.Empty())
  137. {
  138. valuefield_attribute = fields_attribute.Substring(0, fields_attribute.Find(","));
  139. }
  140. if (!data_formatter_attribute.Empty())
  141. {
  142. data_formatter = DataFormatter::GetDataFormatter(data_formatter_attribute);
  143. if (!data_formatter)
  144. Core::Log::Message(Rocket::Core::Log::LT_WARNING, "Unable to find data formatter named '%s', formatting skipped.", data_formatter_attribute.CString());
  145. }
  146. // Build a list of attributes
  147. Rocket::Core::String fields(valuefield_attribute);
  148. fields += ",";
  149. fields += fields_attribute;
  150. DataQuery query(data_source, data_table, fields);
  151. while (query.NextRow())
  152. {
  153. Rocket::Core::StringList fields;
  154. Rocket::Core::String value = query.Get<Rocket::Core::String>(0, "");
  155. for (size_t i = 1; i < query.GetNumFields(); ++i)
  156. fields.push_back(query.Get< Rocket::Core::String>(i, ""));
  157. Rocket::Core::String formatted("");
  158. if (fields.size() > 0)
  159. formatted = fields[0];
  160. if (data_formatter)
  161. data_formatter->FormatData(formatted, fields);
  162. // Add the data as an option.
  163. widget->AddOption(formatted, value, -1, false);
  164. }
  165. // If an option was selected before, attempt to restore the selection to it.
  166. if (old_selection > -1)
  167. {
  168. // Try to find a selection with the same value as the previous one.
  169. for (int i = 0; i < GetNumOptions(); ++i)
  170. {
  171. SelectOption* option = GetOption(i);
  172. if (option->GetValue() == old_value)
  173. {
  174. widget->SetSelection(i, true);
  175. return;
  176. }
  177. }
  178. // Failed to find an option with the same value. Attempt to at least set the same index.
  179. int new_selection = Rocket::Core::Math::Clamp(old_selection, 0, GetNumOptions() - 1);
  180. if (GetNumOptions() == 0)
  181. new_selection = -1;
  182. widget->SetSelection(new_selection, true);
  183. }
  184. }
  185. }
  186. }