1 /* PopupListBox.hpp
2  * Copyright (C) 2018, 2019  Sven Jähnichen
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef BWIDGETS_POPUPLISTBOX_HPP_
19 #define BWIDGETS_POPUPLISTBOX_HPP_
20 
21 #include "ItemBox.hpp"
22 #include "ListBox.hpp"
23 
24 #define BWIDGETS_DEFAULT_POPUPLISTBOX_WIDTH BWIDGETS_DEFAULT_CHOICEBOX_WIDTH
25 #define BWIDGETS_DEFAULT_POPUPLISTBOX_HEIGHTH (BWIDGETS_DEFAULT_LISTBOX_HEIGHTH + BWIDGETS_DEFAULT_ITEMBOX_HEIGHT)
26 #define BWIDGETS_DEFAULT_POPUPLISTBOX_BUTTON_WIDTH BWIDGETS_DEFAULT_ITEMBOX_HEIGHT
27 #define BWIDGETS_DEFAULT_POPUPLISTBOX_BUTTON_HEIGHT BWIDGETS_DEFAULT_ITEMBOX_HEIGHT
28 #define BWIDGETS_DEFAULT_POPUPLISTBOX_BUTTON_NAME "/button"
29 #define BWIDGETS_DEFAULT_POPUPLISTBOX_LISTBOX_NAME "/listbox"
30 
31 
32 namespace BWidgets
33 {
34 
35 /**
36  * Class BWidgets::PopupListBox
37  *
38  * Widget for selection of one item out of a vector of items.
39  * It is a composite value widget consisting of a the widget itself (a
40  * BWidgets::ItemBox) , a BWidgets::ListBox and a BWidgets::DownButton. The
41  * widget shows the result of the BWidgets::ListBox. The BWidgets::ListBox pops
42  * up on pressing on either the widget itself or BWidgets::DownButton and
43  * minimizes again after selection of an item or on pressing on either the
44  * widegt or BWidgets::DownButton again. The value of
45  * this widget reflects the value the item selected. On
46  * change, a value changed event is emitted.
47  */
48 class PopupListBox : public ItemBox
49 {
50 public:
51 	PopupListBox ();
52 	PopupListBox (const double x, const double y, const double width, const double height,
53 		      const double listWidth, const double listHeight, const std::string& name);
54 	PopupListBox (const double x, const double y, const double width, const double height,
55 		      const double listWidth, const double listHeight, const std::string& name,
56 		      const BItems::ItemList& items, double preselection = UNSELECTED);
57 	PopupListBox (const double x, const double y, const double width, const double height,
58 		      const double listXOffset, const double listYOffset, const double listWidth,
59 		      const double listHeight, const std::string& name);
60 	PopupListBox (const double x, const double y, const double width, const double height,
61 		      const double listXOffset, const double listYOffset, const double listWidth,
62 		      const double listHeight, const std::string& name,
63 		      const BItems::ItemList& items, double preselection = UNSELECTED);
64 
65 	/**
66 	 * Creates a new (orphan) PopupListBox and copies the properties from a
67 	 * source choice box widget.
68 	 * @param that Source choice box
69 	 */
70 	PopupListBox (const PopupListBox& that);
71 
72 	/**
73 	 * Assignment. Copies the properties from a source PopupListBox widget
74 	 * and keeps its name and its position within the widget tree. Emits a
75 	 * BEvents::ExposeEvent if the text widget is visible.
76 	 * @param that Source text widget
77 	 */
78 	PopupListBox& operator= (const PopupListBox& that);
79 
80 	/**
81 	 * Pattern cloning. Creates a new instance of the widget and copies all
82 	 * its properties.
83 	 */
84 	virtual Widget* clone () const override;
85 
86 	/**
87 	 * Gets (a pointer to) the vector of items and thus gets access to the
88 	 * internally stored list of items.
89 	 * @return Pointer to a string vector
90 	 */
91 	BItems::ItemList* getItemList ();
92 
93 	/**
94 	 * Gets (a pointer to) the internal BWidgets::ListBox
95 	 * @return Pointer to the internal BWidgets::ListBox
96 	 */
97 	ListBox* getListBox ();
98 
99 	/**
100 	 * Scans theme for widget properties and applies these properties.
101 	 * @param theme Theme to be scanned.
102 	 * @param name Name of the BStyles::StyleSet within the theme to be
103 	 * 		  	   applied.
104 	 */
105 	virtual void applyTheme (BStyles::Theme& theme) override;
106 	virtual void applyTheme (BStyles::Theme& theme, const std::string& name) override;
107 
108 	/**
109 	 * Changes the value of the widget. Emits a value changed event and (if
110 	 * visible) an expose event.
111 	 * @param val Value
112 	 */
113 	virtual void setValue (const double val) override;
114 
115 	virtual void moveListBox (const BUtilities::Point& offset);
116 
117 	virtual void resizeListBox (const BUtilities::Point& extends);
118 
119 	virtual void resizeListBoxItem (const double value, const BUtilities::Point& extends);
120 
121 	virtual void resizeListBoxItems (const BUtilities::Point& extends);
122 
123 	/**
124 	 * Calls a redraw of the widget and calls postRedisplay () if the the
125 	 * Widget is visible.
126 	 * This method should be called if the widgets properties are indirectly
127 	 * changed.
128 	 */
129 	virtual void update () override;
130 
131 	/**
132 	 * Method to handle a click on this widget via a
133 	 * BEvents::EventType::BUTTON_PRESS_EVENT.
134 	 */
135 	virtual void onButtonPressed (BEvents::PointerEvent* event) override;
136 
137 	/**
138 	 * Handles the BEvents::WHEEL_SCROLL_EVENT to scroll through the items
139 	 * in the ItemBox. Changes the widgets value. Scrolling in the ListBox
140 	 * is NOT handled by this method.
141 	 * @param event Pointer to a wheel event emitted by the same widget.
142 	 */
143 	virtual void onWheelScrolled (BEvents::WheelEvent* event) override;
144 
145 protected:
146 	void initItem ();
147 	static void handleValueChanged (BEvents::Event* event);
148 	static void handleDownButtonClicked (BEvents::Event* event);
149 
150 	DownButton downButton;
151 	ListBox listBox;
152 
153 };
154 
155 }
156 
157 #endif /* BWIDGETS_POPUPLISTBOX_HPP_ */
158