ListBox.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_ListBox_H
30 #define Pt_Forms_ListBox_H
31 
32 #include <Pt/Forms/Control.h>
33 #include <Pt/Forms/ListBoxStyler.h>
34 #include <Pt/Forms/ScrollView.h>
35 #include <Pt/Forms/FlowLayout.h>
36 #include <Pt/Forms/Icon.h>
37 #include <Pt/Forms/PixmapSurface.h>
38 #include <Pt/Gfx/Color.h>
39 #include <Pt/Gfx/FontMetrics.h>
40 #include <Pt/Gfx/Image.h>
41 #include <Pt/SmartPtr.h>
42 #include <cstddef>
43 
44 #include <Pt/Forms/ProgressBar.h> // XXX
45 
46 namespace Pt {
47 
48 namespace Forms {
49 
50 class Painter;
51 
70 class PT_FORMS_API ListBoxItem : public Control
71 {
72  typedef Control Base;
73 
74  public:
78 
81  virtual ~ListBoxItem();
82 
85  bool isSelectable() const;
86 
89  void setSelectable(bool b);
90 
93  bool isSelected() const;
94 
100  void setSelected(bool b);
101 
104  void setText(const Pt::String& t);
105 
108  const Pt::String& text() const;
109 
114  void setIcon(const Icon& icon, const Gfx::SizeF& size);
115 
118  const Gfx::SizeF& iconSize() const
119  { return _iconSize; }
120 
123  bool isHovered() const;
124 
127  void click();
128 
132 
136 
137  public:
140  void setBackground(const Gfx::Brush& b);
141 
144  const Gfx::Color& textColor() const;
145 
148  void setTextColor(const Gfx::Color& color);
149 
152  Gfx::Font font() const;
153 
156  void setFont(const Gfx::Font& font);
157 
160  void setFontSize(std::size_t size);
161 
165 
169 
174  void setRenderer(ListItemRenderer* renderer);
175 
176  protected:
179  virtual void onActionKey(const KeyEvent& kev);
180 
183  virtual void onShortcut(const Key& key);
184 
187  virtual void onMnemonic(Pt::Char m);
188 
191  virtual void onPressed();
192 
195  virtual void onReleased();
196 
199  virtual void onCanceled();
200 
203  virtual bool onEnterEvent(const EnterEvent& ev);
204 
207  virtual bool onLeaveEvent(const LeaveEvent& ev);
208 
211  virtual bool onMouseEvent(const MouseEvent& ev);
212 
215  virtual bool onTouchEvent(const TouchEvent& ev);
216 
219  virtual bool onScrollEvent(const ScrollEvent& ev);
220 
221  protected:
224  virtual void onInvalidate();
225 
226  protected:
229  virtual Gfx::SizeF onMeasure(const SizePolicy& p);
230 
237 
243  virtual Gfx::SizeF onMeasureText(const String& text);
244 
250  virtual Gfx::SizeF onMeasureContent(const SizePolicy& policy,
251  const Gfx::SizeF& iconSz,
252  const Gfx::SizeF& textSz);
253 
254  protected:
257  virtual void onLayout(const Gfx::RectF& rect);
258 
266  virtual void onLayoutContent(const Gfx::RectF& innerRect,
267  const Gfx::SizeF& iconSz,
268  const Gfx::SizeF& textSz,
269  const Gfx::FontMetrics& fm,
270  Gfx::RectF& iconRect,
271  Gfx::RectF& textRect);
272 
273  protected:
276  virtual void onPaint(PaintContext& context, const Gfx::RectF& updateRect);
277 
282  virtual void onPaintBackground(PaintContext& context,
283  const ListItemState& state);
284 
289  virtual void onPaintHighlight(PaintContext& context,
290  const ListItemState& state);
291 
296  virtual void onPaintContent(PaintContext& context,
297  const ListItemState& state);
298 
303  virtual void onPaintIcon(PaintContext& context,
304  const Gfx::RectF& iconRect,
305  const PixmapSurface& picture,
306  const Gfx::PointF& iconPos,
307  const ListItemState& state);
308 
313  virtual void onPaintText(PaintContext& context,
314  const Gfx::RectF& textRect,
315  const String& text,
316  const Gfx::PointF& textPos,
317  const Gfx::FontMetrics& fm,
318  const ListItemState& state);
319 
320  private:
321  ListItemState getState() const;
322 
323  private:
324  Signal<> _clicked;
325  Pt::Signal<ListBoxItem&> _selected;
326  bool _onClickBegin;
327  bool _isHovered;
328  bool _isSelectable;
329  bool _isSelected;
330  bool _hasBackground;
331  String _text;
332 
333  Icon _icon;
334  Gfx::SizeF _iconSize;
335 
336  ListItemStyler _listItemStyle;
337 
338  PixmapSurface _picture;
339 
340  Gfx::SizeF _measuredIconSz;
341  Gfx::SizeF _measuredTextSz;
342 
343  Gfx::RectF _iconRect;
344  Gfx::RectF _textRect;
345  Gfx::PointF _iconPos;
346  Gfx::PointF _textPos;
347  Gfx::FontMetrics _fontMetrics;
348 };
349 
350 
358 class ListBoxLayout : public FlowLayout
359 {
360  friend class ListBox;
361 
362  public:
366 
369  const std::vector<ListBoxItem*>& selectedItems() const;
370 
374 
375  protected:
378  virtual void onAddControl(Control& control);
379 
382  virtual void onRemoveControl(Control& control);
383 
384  private:
385  void onItemSelected(ListBoxItem& item);
386 
387  private:
388  Pt::Signal<ListBoxItem&> _selected;
389  std::vector<ListBoxItem*> _selectedItems;
390 };
391 
392 
427 class PT_FORMS_API ListBox : public Control
428 {
429  typedef Control Base;
430 
431  public:
435 
438  virtual ~ListBox();
439 
442  void setScrollBars(bool hasScrollBars);
443 
446  void addItem(ListBoxItem& item);
447 
450  void removeItem(ListBoxItem& item);
451 
454  const std::vector<ListBoxItem*>& selectedItems() const;
455 
459 
462  void scrollX(int xpos);
463 
466  void scrollY(int ypos);
467 
470  int maximumX() const;
471 
474  int maximumY() const;
475 
476  public:
479  const Gfx::Brush* background() const;
480 
483  void setBackground(const Gfx::Brush& b);
484 
487  void setBackground(bool b);
488 
491  const Gfx::Pen* contour() const;
492 
495  void setContour(const Gfx::Pen& pen);
496 
499  void setFrame(bool b);
500 
505  void setRenderer(ListBoxRenderer* renderer);
506 
507  protected:
510  virtual void onInvalidate();
511 
514  virtual Gfx::SizeF onMeasure(const SizePolicy& policy);
515 
518  virtual void onLayout(const Gfx::RectF& rect);
519 
522  virtual void onPaint(PaintContext& context, const Gfx::RectF& updateRect);
523 
524  private:
525  ScrollView _scrollView;
526  ListBoxLayout _layout;
527  ListBoxStyler _styler;
528  bool _hasBackground;
529  bool _hasFrame;
530 };
531 
532 } // namespace
533 
534 } // namespace
535 
536 #endif
Viewport over larger content.
Definition: ScrollView.h:70
Core module.
Definition: Allocator.h:33
Key press or release.
Definition: KeyEvent.h:50
void setFontWeight(Gfx::Font::Weight weight)
Sets the widget-local font weight to weight.
virtual void onLayout(const Gfx::RectF &rect)
Places the icon and text in rect.
Touch press, move, or release.
Definition: TouchEvent.h:51
virtual void onLayout(const Gfx::RectF &rect)
Places the scroll view in rect.
virtual bool onScrollEvent(const ScrollEvent &ev)
Cancels an incomplete click when the item is scrolled.
Represents an image at one or more logical sizes.
Definition: Icon.h:144
Pointer entered a widget.
Definition: EnterEvent.h:49
Attributes for the drawing of outlines.
Definition: Pen.h:54
void setScrollBars(bool hasScrollBars)
Sets whether the list shows scroll bars.
int maximumY() const
Returns the maximum vertical scroll offset.
Transient visual state of a list item.
Definition: ListBoxStyler.h:260
void setContour(const Gfx::Pen &pen)
Sets the widget-local contour pen to pen.
Scroll or wheel movement.
Definition: ScrollEvent.h:51
bool isSelectable() const
Returns true if a click can change the selected state.
void setFontSlant(Gfx::Font::Slant slant)
Sets the widget-local font slant to slant.
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.
Metrics that describe a font face at a given size.
Definition: FontMetrics.h:46
virtual Gfx::SizeF onMeasure(const SizePolicy &p)
Measures the icon, text, and frame.
Pt::Signal< ListBoxItem & > & selected()
Returns the signal emitted when an item's selected state is set.
Multicast Signal to call multiple slots.
Definition: Signal.h:190
virtual void onLayoutContent(const Gfx::RectF &innerRect, const Gfx::SizeF &iconSz, const Gfx::SizeF &textSz, const Gfx::FontMetrics &fm, Gfx::RectF &iconRect, Gfx::RectF &textRect)
Lays out extra content after icon and text have been positioned.
const Gfx::Pen * contour() const
Returns the effective contour pen, or 0 when the frame is off.
Slant
Describes the requested font slant.
Definition: Font.h:76
virtual void onCanceled()
Cancels an incomplete click.
void setSelectable(bool b)
Sets whether a click can change the selected state.
void setIcon(const Icon &icon, const Gfx::SizeF &size)
Sets the icon shown beside the caption.
void setRenderer(ListItemRenderer *renderer)
Assigns renderer as the family renderer.
virtual void onPaint(PaintContext &context, const Gfx::RectF &updateRect)
Paints the background, highlight, icon, and text.
Active painting session on a Forms paint surface.
Definition: PaintContext.h:51
virtual void onPaintHighlight(PaintContext &context, const ListItemState &state)
Paints the list item highlight layer.
Binds a list box to the current style renderer.
Definition: ListBoxStyler.h:181
virtual Gfx::SizeF onMeasureIcon()
Measures the icon content size.
Size with floating-point width and height.
Definition: Size.h:47
const Pt::String & text() const
Returns the caption.
const std::vector< ListBoxItem * > & selectedItems() const
Returns the currently selected items.
Point with floating-point X and Y coordinates.
Definition: Point.h:47
Renders the look of a list item.
Definition: ListBoxStyler.h:324
bool isSelected() const
Returns true if the item is selected.
void setText(const Pt::String &t)
Sets the caption to t.
virtual ~ListBoxItem()
Destroys the list item.
void setBackground(bool b)
Sets whether the list paints a background.
Unicode character type.
Definition: String.h:67
Reusable view used as application content.
Definition: Control.h:107
ListBoxItem()
Creates an empty list item.
Binds a list item to the current style renderer.
Definition: ListBoxStyler.h:480
void scrollY(int ypos)
Scrolls the list vertically to ypos.
const Gfx::SizeF & iconSize() const
Returns the logical icon size.
Definition: ListBox.h:118
void addItem(ListBoxItem &item)
Adds caller-owned item to the list.
Weight
Describes the requested font weight.
Definition: Font.h:61
virtual void onPaintBackground(PaintContext &context, const ListItemState &state)
Paints the list item background layer.
virtual void onReleased()
Toggles selection at the end of a completed click.
Renders the look of a list box.
Definition: ListBoxStyler.h:95
virtual bool onTouchEvent(const TouchEvent &ev)
Completes or cancels a click from a touch press and release.
Pt::Signal< ListBoxItem & > & selected()
Returns the signal emitted when an item's selected state is set.
void setSelected(bool b)
Sets whether the item is selected and emits selected().
virtual void onActionKey(const KeyEvent &kev)
Completes a press or release from an action key.
virtual void onInvalidate()
Binds the styler.
virtual void onMnemonic(Pt::Char m)
Completes a press and release from a mnemonic.
Pt::Signal< ListBoxItem & > & selected()
Returns the signal emitted when the selected state is set.
virtual void onRemoveControl(Control &control)
Removes control from the selected items.
virtual void onPaint(PaintContext &context, const Gfx::RectF &updateRect)
Paints the background and frame.
void setFontSize(std::size_t size)
Sets the widget-local font size to size.
virtual bool onEnterEvent(const EnterEvent &ev)
Highlights the item when the pointer enters.
bool isHovered() const
Returns true if the pointer is over the item.
Arranges children in a single row or column.
Definition: FlowLayout.h:72
Widget that hosts controls on a paint surface.
Definition: View.h:86
Font request used for text drawing and measurement.
Definition: Font.h:56
Item in a list box.
Definition: ListBox.h:71
Key code and modifiers.
Definition: Key.h:23
const Gfx::Brush * background() const
Returns the effective background brush, or 0 when disabled.
virtual bool onMouseEvent(const MouseEvent &ev)
Completes or cancels a click from a pointer press and release.
Layout that tracks selected list items.
Definition: ListBox.h:359
void setRenderer(ListBoxRenderer *renderer)
Assigns renderer as the family renderer.
virtual void onPressed()
Marks the start of a click.
virtual void onShortcut(const Key &key)
Completes a press and release from a shortcut.
Pointer left a widget.
Definition: LeaveEvent.h:49
Constraint used when measuring a control.
Definition: SizePolicy.h:48
Standard color type.
Definition: Color.h:47
virtual ~ListBox()
Destroys the list box. Attached items are not destroyed.
Signal & clicked()
Returns the clicked signal.
void setBackground(const Gfx::Brush &b)
Sets the widget-local background brush to b.
virtual void onPaintContent(PaintContext &context, const ListItemState &state)
Paints the list item content layers.
virtual bool onLeaveEvent(const LeaveEvent &ev)
Clears the highlight when the pointer leaves.
Scrollable list of items.
Definition: ListBox.h:428
Unicode capable basic_string.
Definition: Api-String.h:44
void scrollX(int xpos)
Scrolls the list horizontally to xpos.
virtual void onPaintText(PaintContext &context, const Gfx::RectF &textRect, const String &text, const Gfx::PointF &textPos, const Gfx::FontMetrics &fm, const ListItemState &state)
Paints the list item text layer.
Fill description for shapes and text.
Definition: Brush.h:145
void setFrame(bool b)
Sets whether the list paints a frame.
const Gfx::Color & textColor() const
Returns the effective text color.
Rect with floating-point coordinates.
Definition: Rect.h:47
virtual void onPaintIcon(PaintContext &context, const Gfx::RectF &iconRect, const PixmapSurface &picture, const Gfx::PointF &iconPos, const ListItemState &state)
Paints the list item icon layer.
Off-screen Forms paint surface.
Definition: Pixmap.h:130
virtual Gfx::SizeF onMeasureContent(const SizePolicy &policy, const Gfx::SizeF &iconSz, const Gfx::SizeF &textSz)
Aggregates icon and text sizes into total content size.
void click()
Completes a press and release and toggles selection.
ListBox()
Creates an empty list box.
virtual void onInvalidate()
Binds the styler.
void setBackground(const Gfx::Brush &b)
Sets the widget-local background brush to b.
virtual void onAddControl(Control &control)
Forwards the added control to the base layout.
ListBoxLayout()
Creates an empty list layout.
void removeItem(ListBoxItem &item)
Removes item from the list.
virtual Gfx::SizeF onMeasure(const SizePolicy &policy)
Measures the items and frame.
Pointer movement or button change.
Definition: MouseEvent.h:134
int maximumX() const
Returns the maximum horizontal scroll offset.
Gfx::Font font() const
Returns the effective font.
virtual Gfx::SizeF onMeasureText(const String &text)
Measures the text content size.
const std::vector< ListBoxItem * > & selectedItems() const
Returns the currently selected items.