ComboBox.h
1/* Copyright (C) 2017 Marc Boris Duerner
2
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 2.1 of the License, or (at your option) any later version.
7
8 As a special exception, you may use this file as part of a free
9 software library without restriction. Specifically, if other files
10 instantiate templates or use macros or inline functions from this
11 file, or you compile this file and link it with other files to
12 produce an executable, this file does not by itself cause the
13 resulting executable to be covered by the GNU General Public
14 License. This exception does not however invalidate any other
15 reasons why the executable file might be covered by the GNU Library
16 General Public License.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 MA 02110-1301 USA
27*/
28
29#ifndef Pt_Forms_ComboBox_H
30#define Pt_Forms_ComboBox_H
31
32#include <Pt/Forms/Control.h>
33#include <Pt/Forms/ComboBoxStyler.h>
34#include <Pt/Forms/Popup.h>
35#include <Pt/Forms/ListBox.h>
36#include <Pt/Forms/LineEditor.h>
37#include <Pt/SmartPtr.h>
38#include <Pt/String.h>
39
40namespace Pt {
41
42namespace Forms {
43
82class PT_FORMS_API ComboBox : public Control
83{
84 typedef Control Base;
85
86 public:
90
93 virtual ~ComboBox();
94
97 void addItem(ListBoxItem& item);
98
102
105 bool isEditable() const;
106
109 void setEditable(bool e);
110
113 bool isAccepted() const;
114
117 void setAccepted(bool a);
118
121 const Pt::String& text() const;
122
125 void setText(const Pt::String& str);
126
130
134
137 void setScrollBars(bool hasScrollBars);
138
141 void setMaxHeight(double height);
142
145 void showPopup();
146
149 void hidePopup();
150
153 bool isHighlighted() const;
154
158
162
166
170
174
175 public:
178 const Gfx::Brush& background() const;
179
182 void setBackground(const Gfx::Brush& b);
183
186 const Gfx::Pen& contour() const;
187
190 void setContour(const Gfx::Pen& p);
191
194 const Gfx::Brush& foreground() const;
195
198 void setForeground(const Gfx::Brush& b);
199
202 const Gfx::Color& textColor() const;
203
206 void setTextColor(const Gfx::Color& color);
207
211
214 void setFont(const Gfx::Font& font);
215
218 void setFontSize(std::size_t size);
219
223
227
233
234 protected:
237 virtual Gfx::SizeF onMeasure(const SizePolicy& policy);
238
241 virtual void onLayout(const Gfx::RectF& rect);
242
245 virtual void onInvalidate();
246
249 virtual void onPaint(PaintContext& context, const Gfx::RectF& updateRect);
250
253 virtual void onPaintChrome(PaintContext& context,
254 const Gfx::RectF& rect,
255 const Gfx::RectF& entryRect,
256 const Gfx::RectF& buttonRect,
257 const ComboBoxState& state,
258 const ComboBoxButtonState& buttonState);
259
262 virtual void onPaintText(PaintContext& context,
263 const Gfx::RectF& textRect,
264 const String& text,
265 const Gfx::PointF& textPos,
266 const Gfx::RectF& cursor,
267 const ComboBoxState& state);
268
269 protected:
272 virtual void onProcessMouseEvent(const MouseEvent& ev);
273
274 protected:
277 virtual void onResizeEvent(const ResizeEvent& ev);
278
281 virtual bool onKeyEvent(const KeyEvent& ev);
282
285 virtual bool onMouseEvent(const MouseEvent& ev);
286
289 virtual bool onTouchEvent(const TouchEvent& ev);
290
293 virtual bool onEnterEvent(const EnterEvent& ev);
294
297 virtual bool onLeaveEvent(const LeaveEvent& ev);
298
301 virtual void onFocusEvent(const FocusEvent& ev);
302
303 private:
304 void onItemSelected(ListBoxItem& item);
305
306 void processKeyEvent(const KeyEvent& ev);
307
308 ComboBoxState comboBoxState() const;
309
310 private:
313 Pt::Signal<const Pt::String&> _returnPressed;
314 Pt::Signal<const Pt::String&> _editingFinished;
315
316 LineEditor _editor;
317 TextLine _line;
318 Popup _popup;
319 ListBox _items;
320 Gfx::RectF _entryRect;
321 Gfx::RectF _buttonRect;
322 Gfx::RectF _textRect;
323 Gfx::RectF _cursorRect;
324 double _maxHeight;
325 bool _isEditable;
326 bool _isAccepted;
327 bool _isTextChanged;
328 bool _isHighlighted;
329 bool _isButtonHighlighted;
330 double _pendingCursorX;
331
332 ComboBoxStyler _styler;
333};
334
335} // namespace
336
337} // namespace
338
339#endif
Aligns content along one axis.
Definition Adjustment.h:60
Transient visual state of a combo-box drop button.
Definition ComboBoxStyler.h:113
Renders the look of a combo box.
Definition ComboBoxStyler.h:155
Transient visual state of a combo box.
Definition ComboBoxStyler.h:49
Binds a combo box to the current style renderer.
Definition ComboBoxStyler.h:295
virtual bool onMouseEvent(const MouseEvent &ev)
Places the caret or opens the popup from a pointer press.
bool isEditable() const
Returns true if the user can type the text.
Pt::Signal< const Pt::String & > & returnPressed()
Returns the signal emitted when Return completes an edit.
Adjustment textAdjustment() const
Returns the horizontal adjustment of the text.
void addItem(ListBoxItem &item)
Adds caller-owned item to the popup list.
virtual void onPaint(PaintContext &context, const Gfx::RectF &updateRect)
Paints the entry, button, text, and caret.
Pt::Signal< const Pt::String & > & textChanged()
Returns the signal emitted when setText() changes the text.
virtual void onFocusEvent(const FocusEvent &ev)
Begins text input on focus, or emits editingFinished() on loss.
const Gfx::Color & textColor() const
Returns the effective text color.
virtual bool onTouchEvent(const TouchEvent &ev)
Places the caret or opens the popup from a touch press.
void setRenderer(ComboBoxRenderer *renderer)
Assigns renderer as the family renderer.
Pt::Signal< const Pt::String & > & editingFinished()
Returns the signal emitted when focus loss completes an edit.
virtual void onPaintText(PaintContext &context, const Gfx::RectF &textRect, const String &text, const Gfx::PointF &textPos, const Gfx::RectF &cursor, const ComboBoxState &state)
Paints the entry text and caret for state.
void setForeground(const Gfx::Brush &b)
Sets the widget-local foreground brush to b.
virtual bool onEnterEvent(const EnterEvent &ev)
Highlights the box when the pointer enters.
const Pt::String & text() const
Returns the current text.
virtual void onResizeEvent(const ResizeEvent &ev)
Forwards the resize to the base control.
const Gfx::Brush & background() const
Returns the effective background brush.
const Gfx::Brush & foreground() const
Returns the effective foreground brush.
Gfx::Font font() const
Returns the effective font.
Pt::Signal< const Pt::String & > & textEdited()
Returns the signal emitted when the user changes the text.
virtual void onLayout(const Gfx::RectF &rect)
Places the entry, button, and text in rect.
bool isHighlighted() const
Returns true if the pointer is over the box.
Pt::Signal< ListBoxItem & > & selected()
Returns the signal emitted when a list item is chosen.
virtual ~ComboBox()
Destroys the combo box.
void setEditable(bool e)
Sets whether the user can type the text.
void setBackground(const Gfx::Brush &b)
Sets the widget-local background brush to b.
void setScrollBars(bool hasScrollBars)
Sets whether the popup list shows scroll bars.
virtual void onProcessMouseEvent(const MouseEvent &ev)
Forwards the pointer event to the base control.
void setContour(const Gfx::Pen &p)
Sets the widget-local contour pen to p.
virtual Gfx::SizeF onMeasure(const SizePolicy &policy)
Measures the entry, button, and frame.
virtual void onPaintChrome(PaintContext &context, const Gfx::RectF &rect, const Gfx::RectF &entryRect, const Gfx::RectF &buttonRect, const ComboBoxState &state, const ComboBoxButtonState &buttonState)
Paints the combo-box chrome for state.
virtual bool onLeaveEvent(const LeaveEvent &ev)
Clears the highlight when the pointer leaves.
void showPopup()
Shows the popup list.
void setAccepted(bool a)
Sets whether Return and focus loss complete an edit.
void hidePopup()
Hides the popup list.
virtual bool onKeyEvent(const KeyEvent &ev)
Inserts, deletes, or moves the caret, or completes an edit.
void setText(const Pt::String &str)
Sets the current text to str and emits textChanged().
void setTextColor(const Gfx::Color &color)
Sets the widget-local text color to color.
void setFont(const Gfx::Font &font)
Sets the widget-local font to font.
const Gfx::Pen & contour() const
Returns the effective contour pen.
bool isAccepted() const
Returns true if Return and focus loss complete an edit.
void setMaxHeight(double height)
Sets the maximum height of the popup to height.
void setFontWeight(Gfx::Font::Weight weight)
Sets the widget-local font weight to weight.
void setTextAdjustment(Adjustment a)
Sets the horizontal adjustment of the text to a.
virtual void onInvalidate()
Binds the styler.
void removeItem(ListBoxItem &item)
Removes item from the popup list.
ComboBox()
Creates a combo box.
void setFontSize(std::size_t size)
Sets the widget-local font size to size.
void setFontSlant(Gfx::Font::Slant slant)
Sets the widget-local font slant to slant.
Control()
Creates an unparented control.
Pointer entered a widget.
Definition EnterEvent.h:49
Reports that a widget gained or lost focus.
Definition FocusEvent.h:48
Key press or release.
Definition KeyEvent.h:50
Pointer left a widget.
Definition LeaveEvent.h:49
Single-line text, caret, and scroll helper.
Definition LineEditor.h:57
Item in a list box.
Definition ListBox.h:71
Scrollable list of items.
Definition ListBox.h:428
Pointer movement or button change.
Definition MouseEvent.h:134
Active painting session on a Forms paint surface.
Definition PaintContext.h:51
Represents a borderless transient window.
Definition Popup.h:48
Reports that a widget was resized.
Definition ResizeEvent.h:50
Constraint used when measuring a control.
Definition SizePolicy.h:48
Represents one laid-out line in a TextBlock.
Definition TextBlock.h:58
Touch press, move, or release.
Definition TouchEvent.h:51
const Gfx::SizeF & size() const
Returns the size in local logical coordinates.
const Cursor * cursor() const
Returns this widget's cursor or the default cursor.
Fill color, gradient or texture.
Definition Brush.h:151
8-bit ARGB color.
Definition Color.h:65
Font family, size and style.
Definition Font.h:63
Slant
Describes the requested font slant.
Definition Font.h:83
Weight
Describes the requested font weight.
Definition Font.h:68
Outline color, width and style.
Definition Pen.h:59
Multicast Signal to call multiple slots.
Definition Signal.h:190
Unicode capable basic_string.
Definition Api-String.h:67
Graphical user interfaces.
Definition ActivateEvent.h:40
Core module.
Definition Allocator.h:33