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/Pixmap.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
46namespace Pt {
47
48namespace Forms {
49
50class Painter;
51
70class 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
153
156 void setFont(const Gfx::Font& font);
157
160 void setFontSize(std::size_t size);
161
165
169
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
236 virtual Gfx::SizeF onMeasureIcon();
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 Pixmap& 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 Pixmap _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
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
427class 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
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
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
Reusable view used as application content.
Definition Control.h:107
Control()
Creates an unparented control.
Pointer entered a widget.
Definition EnterEvent.h:49
FlowLayout(Direction d=Direction::Left)
Creates a flow layout in direction d.
Represents an image at one or more logical sizes.
Definition Icon.h:144
Key press or release.
Definition KeyEvent.h:50
Key code and modifiers.
Definition Key.h:23
Pointer left a widget.
Definition LeaveEvent.h:49
Item in a list box.
Definition ListBox.h:71
virtual bool onMouseEvent(const MouseEvent &ev)
Completes or cancels a click from a pointer press and release.
virtual void onPaint(PaintContext &context, const Gfx::RectF &updateRect)
Paints the background, highlight, icon, and text.
const Gfx::Color & textColor() const
Returns the effective text color.
virtual bool onTouchEvent(const TouchEvent &ev)
Completes or cancels a click from a touch press and release.
bool isSelectable() const
Returns true if a click can change the selected state.
void setText(const Pt::String &t)
Sets the caption to t.
void setSelected(bool b)
Sets whether the item is selected and emits selected().
void setRenderer(ListItemRenderer *renderer)
Assigns renderer as the family renderer.
void setIcon(const Icon &icon, const Gfx::SizeF &size)
Sets the icon shown beside the caption.
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.
virtual void onCanceled()
Cancels an incomplete click.
virtual bool onEnterEvent(const EnterEvent &ev)
Highlights the item when the pointer enters.
const Pt::String & text() const
Returns the caption.
bool isSelected() const
Returns true if the item is selected.
Gfx::Font font() const
Returns the effective font.
virtual void onLayout(const Gfx::RectF &rect)
Places the icon and text in rect.
virtual void onPressed()
Marks the start of a click.
Pt::Signal< ListBoxItem & > & selected()
Returns the signal emitted when the selected state is set.
Signal & clicked()
Returns the clicked signal.
void setBackground(const Gfx::Brush &b)
Sets the widget-local background brush to b.
virtual void onActionKey(const KeyEvent &kev)
Completes a press or release from an action key.
virtual void onShortcut(const Key &key)
Completes a press and release from a shortcut.
virtual bool onLeaveEvent(const LeaveEvent &ev)
Clears the highlight when the pointer leaves.
virtual void onPaintContent(PaintContext &context, const ListItemState &state)
Paints the list item content layers.
virtual Gfx::SizeF onMeasureContent(const SizePolicy &policy, const Gfx::SizeF &iconSz, const Gfx::SizeF &textSz)
Aggregates icon and text sizes into total content size.
virtual void onPaintBackground(PaintContext &context, const ListItemState &state)
Paints the list item background layer.
ListBoxItem()
Creates an empty list item.
virtual Gfx::SizeF onMeasureIcon()
Measures the icon content size.
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.
void click()
Completes a press and release and toggles selection.
const Gfx::SizeF & iconSize() const
Returns the logical icon size.
Definition ListBox.h:118
virtual void onPaintIcon(PaintContext &context, const Gfx::RectF &iconRect, const Pixmap &picture, const Gfx::PointF &iconPos, const ListItemState &state)
Paints the list item icon layer.
virtual void onMnemonic(Pt::Char m)
Completes a press and release from a mnemonic.
void setSelectable(bool b)
Sets whether a click can change the selected state.
void setTextColor(const Gfx::Color &color)
Sets the widget-local text color to color.
virtual void onPaintHighlight(PaintContext &context, const ListItemState &state)
Paints the list item highlight layer.
void setFont(const Gfx::Font &font)
Sets the widget-local font to font.
virtual Gfx::SizeF onMeasureText(const String &text)
Measures the text content size.
virtual bool onScrollEvent(const ScrollEvent &ev)
Cancels an incomplete click when the item is scrolled.
void setFontWeight(Gfx::Font::Weight weight)
Sets the widget-local font weight to weight.
virtual void onInvalidate()
Binds the styler.
virtual ~ListBoxItem()
Destroys the list item.
bool isHovered() const
Returns true if the pointer is over the item.
virtual Gfx::SizeF onMeasure(const SizePolicy &p)
Measures the icon, text, and frame.
void setFontSize(std::size_t size)
Sets the widget-local font size to size.
virtual void onReleased()
Toggles selection at the end of a completed click.
void setFontSlant(Gfx::Font::Slant slant)
Sets the widget-local font slant to slant.
Layout that tracks selected list items.
Definition ListBox.h:359
ListBoxLayout()
Creates an empty list layout.
virtual void onAddControl(Control &control)
Forwards the added control to the base layout.
Pt::Signal< ListBoxItem & > & selected()
Returns the signal emitted when an item's selected state is set.
const std::vector< ListBoxItem * > & selectedItems() const
Returns the currently selected items.
virtual void onRemoveControl(Control &control)
Removes control from the selected items.
Renders the look of a list box.
Definition ListBoxStyler.h:95
Binds a list box to the current style renderer.
Definition ListBoxStyler.h:181
void addItem(ListBoxItem &item)
Adds caller-owned item to the list.
int maximumX() const
Returns the maximum horizontal scroll offset.
virtual void onPaint(PaintContext &context, const Gfx::RectF &updateRect)
Paints the background and frame.
void setBackground(bool b)
Sets whether the list paints a background.
const Gfx::Brush * background() const
Returns the effective background brush, or 0 when disabled.
void setRenderer(ListBoxRenderer *renderer)
Assigns renderer as the family renderer.
virtual void onLayout(const Gfx::RectF &rect)
Places the scroll view in rect.
Pt::Signal< ListBoxItem & > & selected()
Returns the signal emitted when an item's selected state is set.
const std::vector< ListBoxItem * > & selectedItems() const
Returns the currently selected items.
void setBackground(const Gfx::Brush &b)
Sets the widget-local background brush to b.
void setScrollBars(bool hasScrollBars)
Sets whether the list shows scroll bars.
const Gfx::Pen * contour() const
Returns the effective contour pen, or 0 when the frame is off.
virtual Gfx::SizeF onMeasure(const SizePolicy &policy)
Measures the items and frame.
void setFrame(bool b)
Sets whether the list paints a frame.
virtual ~ListBox()
Destroys the list box. Attached items are not destroyed.
void setContour(const Gfx::Pen &pen)
Sets the widget-local contour pen to pen.
void scrollY(int ypos)
Scrolls the list vertically to ypos.
void scrollX(int xpos)
Scrolls the list horizontally to xpos.
ListBox()
Creates an empty list box.
int maximumY() const
Returns the maximum vertical scroll offset.
virtual void onInvalidate()
Binds the styler.
void removeItem(ListBoxItem &item)
Removes item from the list.
Renders the look of a list item.
Definition ListBoxStyler.h:324
Transient visual state of a list item.
Definition ListBoxStyler.h:260
Binds a list item to the current style renderer.
Definition ListBoxStyler.h:480
Pointer movement or button change.
Definition MouseEvent.h:134
Active painting session on a Forms paint surface.
Definition PaintContext.h:51
Draws on a Forms paint surface or paint context.
Definition Painter.h:53
Off-screen Forms paint surface.
Definition Pixmap.h:130
Scroll or wheel movement.
Definition ScrollEvent.h:51
Viewport over larger content.
Definition ScrollView.h:70
Constraint used when measuring a control.
Definition SizePolicy.h:48
Touch press, move, or release.
Definition TouchEvent.h:51
const Gfx::SizeF & size() const
Returns the size in local logical coordinates.
Fill color, gradient or texture.
Definition Brush.h:151
8-bit ARGB color.
Definition Color.h:65
Ascent, descent and line height of a font.
Definition FontMetrics.h:50
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
Unicode character type.
Definition String.h:67