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 
40 namespace Pt {
41 
42 namespace Forms {
43 
82 class 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 
101  void removeItem(ListBoxItem& item);
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 
210  Gfx::Font font() const;
211 
214  void setFont(const Gfx::Font& font);
215 
218  void setFontSize(std::size_t size);
219 
223 
227 
232  void setRenderer(ComboBoxRenderer* renderer);
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:
311  Pt::Signal<const Pt::String&> _textChanged;
312  Pt::Signal<const Pt::String&> _textEdited;
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
Pt::Signal< const Pt::String & > & editingFinished()
Returns the signal emitted when focus loss completes an edit.
Core module.
Definition: Allocator.h:33
Key press or release.
Definition: KeyEvent.h:50
void removeItem(ListBoxItem &item)
Removes item from the popup list.
Touch press, move, or release.
Definition: TouchEvent.h:51
Single-line text, caret, and scroll helper.
Definition: LineEditor.h:57
Pointer entered a widget.
Definition: EnterEvent.h:49
virtual bool onLeaveEvent(const LeaveEvent &ev)
Clears the highlight when the pointer leaves.
Attributes for the drawing of outlines.
Definition: Pen.h:54
void setFontSize(std::size_t size)
Sets the widget-local font size to size.
void setFontWeight(Gfx::Font::Weight weight)
Sets the widget-local font weight to weight.
void setText(const Pt::String &str)
Sets the current text to str and emits textChanged().
Reports that a widget was resized.
Definition: ResizeEvent.h:50
void setFont(const Gfx::Font &font)
Sets the widget-local font to font.
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.
void setScrollBars(bool hasScrollBars)
Sets whether the popup list shows scroll bars.
virtual void onFocusEvent(const FocusEvent &ev)
Begins text input on focus, or emits editingFinished() on loss.
Slant
Describes the requested font slant.
Definition: Font.h:76
const Gfx::Brush & foreground() const
Returns the effective foreground brush.
Active painting session on a Forms paint surface.
Definition: PaintContext.h:51
Size with floating-point width and height.
Definition: Size.h:47
Provides a text entry field with a drop-down list.
Definition: ComboBox.h:83
Point with floating-point X and Y coordinates.
Definition: Point.h:47
const Gfx::Color & textColor() const
Returns the effective text color.
virtual void onProcessMouseEvent(const MouseEvent &ev)
Forwards the pointer event to the base control.
Reusable view used as application content.
Definition: Control.h:107
virtual bool onMouseEvent(const MouseEvent &ev)
Places the caret or opens the popup from a pointer press.
Transient visual state of a combo box.
Definition: ComboBoxStyler.h:49
void setForeground(const Gfx::Brush &b)
Sets the widget-local foreground brush to b.
void setEditable(bool e)
Sets whether the user can type the text.
void setTextAdjustment(Adjustment a)
Sets the horizontal adjustment of the text to a.
Weight
Describes the requested font weight.
Definition: Font.h:61
Pt::Signal< const Pt::String & > & textChanged()
Returns the signal emitted when setText() changes the text.
Pt::Signal< ListBoxItem & > & selected()
Returns the signal emitted when a list item is chosen.
Adjustment textAdjustment() const
Returns the horizontal adjustment of the text.
void setTextColor(const Gfx::Color &color)
Sets the widget-local text color to color.
Pt::Signal< const Pt::String & > & returnPressed()
Returns the signal emitted when Return completes an edit.
const Pt::String & text() const
Returns the current text.
void setAccepted(bool a)
Sets whether Return and focus loss complete an edit.
Gfx::Font font() const
Returns the effective font.
virtual bool onKeyEvent(const KeyEvent &ev)
Inserts, deletes, or moves the caret, or completes an edit.
void setContour(const Gfx::Pen &p)
Sets the widget-local contour pen to p.
virtual void onResizeEvent(const ResizeEvent &ev)
Forwards the resize to the base control.
bool isAccepted() const
Returns true if Return and focus loss complete an edit.
void setBackground(const Gfx::Brush &b)
Sets the widget-local background brush to b.
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.
virtual bool onEnterEvent(const EnterEvent &ev)
Highlights the box when the pointer enters.
void showPopup()
Shows the popup list.
Aligns content along one axis.
Definition: Adjustment.h:60
virtual void onInvalidate()
Binds the styler.
Font request used for text drawing and measurement.
Definition: Font.h:56
Item in a list box.
Definition: ListBox.h:71
const Gfx::Pen & contour() const
Returns the effective contour pen.
Pointer left a widget.
Definition: LeaveEvent.h:49
Constraint used when measuring a control.
Definition: SizePolicy.h:48
void addItem(ListBoxItem &item)
Adds caller-owned item to the popup list.
Standard color type.
Definition: Color.h:47
Reports that a widget gained or lost focus.
Definition: FocusEvent.h:48
ComboBox()
Creates a combo box.
void setRenderer(ComboBoxRenderer *renderer)
Assigns renderer as the family renderer.
virtual void onPaint(PaintContext &context, const Gfx::RectF &updateRect)
Paints the entry, button, text, and caret.
Scrollable list of items.
Definition: ListBox.h:428
Unicode capable basic_string.
Definition: Api-String.h:44
void setMaxHeight(double height)
Sets the maximum height of the popup to height.
void hidePopup()
Hides the popup list.
Transient visual state of a combo-box drop button.
Definition: ComboBoxStyler.h:113
Binds a combo box to the current style renderer.
Definition: ComboBoxStyler.h:295
bool isHighlighted() const
Returns true if the pointer is over the box.
Fill description for shapes and text.
Definition: Brush.h:145
virtual ~ComboBox()
Destroys the combo box.
Rect with floating-point coordinates.
Definition: Rect.h:47
void setFontSlant(Gfx::Font::Slant slant)
Sets the widget-local font slant to slant.
virtual void onLayout(const Gfx::RectF &rect)
Places the entry, button, and text in rect.
virtual Gfx::SizeF onMeasure(const SizePolicy &policy)
Measures the entry, button, and frame.
Pt::Signal< const Pt::String & > & textEdited()
Returns the signal emitted when the user changes the text.
const Gfx::Brush & background() const
Returns the effective background brush.
virtual bool onTouchEvent(const TouchEvent &ev)
Places the caret or opens the popup from a touch press.
Renders the look of a combo box.
Definition: ComboBoxStyler.h:155
Pointer movement or button change.
Definition: MouseEvent.h:134
Represents a borderless transient window.
Definition: Popup.h:48
Represents one laid-out line in a TextBlock.
Definition: TextBlock.h:58
bool isEditable() const
Returns true if the user can type the text.