1 // Copyright (C) 2007 Mark Pustjens <pustjens@dds.nl>
2 // Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
4 // This file is part of CenterIM.
6 // CenterIM is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // CenterIM is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /// @ingroup cppconsui
28 #include "MenuWindow.h"
34 /// Selection widget for choosing one value from several options.
35 class ComboBox
: public Button
{
37 ComboBox(int w
, int h
, const char *text_
= NULL
);
38 explicit ComboBox(const char *text_
= NULL
);
39 virtual ~ComboBox() override
;
41 /// Removes all options.
42 virtual void clearOptions();
44 /// Appends a new option.
45 virtual int addOption(const char *text
= NULL
, intptr_t data
= 0);
46 virtual int addOptionPtr(const char *text
= NULL
, void *data
= NULL
)
48 return addOption(text
, reinterpret_cast<intptr_t>(data
));
51 /// Returns last selected option.
52 virtual int getSelected() const { return selected_entry_
; };
53 virtual const char *getSelectedTitle() const;
54 virtual intptr_t getSelectedData() const;
55 virtual void *getSelectedDataPtr() const;
57 virtual int getOptionsCount() const { return options_
.size(); }
59 virtual const char *getTitle(int entry
) const;
60 virtual intptr_t getData(int entry
) const;
62 virtual void setSelected(int new_entry
);
63 virtual void setSelectedByData(intptr_t data
);
64 virtual void setSelectedByDataPtr(void *data
)
66 setSelectedByData(reinterpret_cast<intptr_t>(data
));
69 sigc::signal
<void, ComboBox
&, int, const char *, intptr_t>
70 signal_selection_changed
;
73 /// Keeps a pair of {display text, value}.
74 struct ComboBoxEntry
{
78 typedef std::vector
<ComboBoxEntry
> ComboBoxEntries
;
80 MenuWindow
*dropdown_
;
82 /// Number of currently selected entry.
86 ComboBoxEntries options_
;
88 /// Maximal option width. Used for dropdown menu width.
89 int max_option_width_
;
91 /// Prepares and displays the dropdown MenuWindow.
92 virtual void onDropDown(Button
&activator
);
94 virtual void dropDownOk(Button
&activator
, int new_entry
);
95 virtual void dropDownClose(Window
&window
);
98 CONSUI_DISABLE_COPY(ComboBox
);
101 } // namespace CppConsUI
105 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: