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 #ifndef ROCKETCONTROLSWIDGETDROPDOWN_H
29 #define ROCKETCONTROLSWIDGETDROPDOWN_H
30 
31 #include "../../Include/Rocket/Core/String.h"
32 #include "../../Include/Rocket/Core/EventListener.h"
33 #include "../../Include/Rocket/Controls/SelectOption.h"
34 #include <vector>
35 
36 namespace Rocket {
37 namespace Controls {
38 
39 class ElementFormControl;
40 
41 /**
42 	Widget for drop-down functionality.
43 	@author Lloyd Weehuizen
44  */
45 
46 class WidgetDropDown : public Core::EventListener
47 {
48 public:
49 	WidgetDropDown(ElementFormControl* element);
50 	virtual ~WidgetDropDown();
51 
52 	/// Updates the selection box layout if necessary.
53 	void OnRender();
54 	/// Positions the drop-down's internal elements.
55 	void OnLayout();
56 
57 	/// Sets the value of the widget.
58 	/// @param[in] value The new value to set.
59 	void SetValue(const Rocket::Core::String& value);
60 	/// Returns the current value of the widget.
61 	/// @return The current value of the widget.
62 	const Rocket::Core::String& GetValue() const;
63 
64 	/// Sets the index of the selection. If the new index lies outside of the bounds, the selection index will be set to -1.
65 	/// @param[in] selection The new selection index.
66 	/// @param[in] force Forces the new selection, even if the widget believes the selection to not have changed.
67 	void SetSelection(int selection, bool force = false);
68 	/// Returns the index of the currently selected item.
69 	/// @return The index of the currently selected item.
70 	int GetSelection() const;
71 
72 	/// Adds a new option to the select control.
73 	/// @param[in] rml The RML content used to represent the option.
74 	/// @param[in] value The value of the option.
75 	/// @param[in] before The index of the element to insert the new option before.
76 	/// @param[in] select True to select the new option.
77 	/// @param[in] selectable If true this option can be selected. If false, this option is not selectable.
78 	/// @return The index of the new option.
79 	int AddOption(const Rocket::Core::String& rml, const Rocket::Core::String& value, int before, bool select, bool selectable = true);
80 	/// Removes an option from the select control.
81 	/// @param[in] index The index of the option to remove.
82 	void RemoveOption(int index);
83 	/// Removes all options from the list.
84 	void ClearOptions();
85 
86 	/// Returns on of the widget's options.
87 	/// @param[in] The index of the desired option.
88 	/// @return The option. This may be NULL if the index was out of bounds.
89 	SelectOption* GetOption(int index);
90 	/// Returns the number of options in the widget.
91 	/// @return The number of options.
92 	int GetNumOptions() const;
93 
94 	/// Processes the incoming event.
95 	virtual void ProcessEvent(Core::Event& event);
96 
97 private:
98 	typedef std::vector< SelectOption > OptionList;
99 
100 	// Shows or hides the selection box.
101 	void ShowSelectBox(bool show);
102 
103 	// Parent element that holds this widget
104 	ElementFormControl* parent_element;
105 
106 	// The elements making up the drop-down process.
107 	Core::Element* button_element;
108 	Core::Element* selection_element;
109 	Core::Element* value_element;
110 
111 	// The options in the drop down.
112 	OptionList options;
113 	int selected_option;
114 
115 	// The current value of the widget.
116 	Rocket::Core::String value;
117 
118 	bool box_layout_dirty;
119 	bool value_layout_dirty;
120 };
121 
122 }
123 }
124 
125 #endif
126