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/Button.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/Image.h>
40 #include <Pt/SmartPtr.h>
41 #include <cstddef>
42 
43 namespace Pt {
44 
45 namespace Forms {
46 
47 class PT_FORMS_API ListBoxItem : public Button
48 {
49  typedef Button Base;
50 
51  public:
52  ListBoxItem();
53 
54  virtual ~ListBoxItem();
55 
56  bool isSelectable() const;
57 
58  void setSelectable(bool b);
59 
60  bool isSelected() const;
61 
62  void setSelected(bool b);
63 
64  void setText(const Pt::String& t);
65 
66  const Pt::String& text() const;
67 
68  void setIcon(const Icon& icon, const Gfx::SizeF& size);
69 
70  const Gfx::SizeF& iconSize() const
71  { return _iconSize; }
72 
73  Pt::Signal<ListBoxItem&>& selected();
74 
75  public:
76  const Gfx::Brush& background() const;
77 
78  void setBackground(const Gfx::Brush& b);
79 
80  const Gfx::Pen& contour() const;
81 
82  void setContour(const Gfx::Pen& p);
83 
84  const Gfx::Color& textColor() const;
85 
86  void setTextColor(const Gfx::Color& color);
87 
88  const Gfx::Font& font() const;
89 
90  void setFont(const Gfx::Font& font);
91 
92  void setFontSize(std::size_t size);
93 
94  void setFontWeight(Gfx::Font::Weight weight);
95 
96  void setFontSlant(Gfx::Font::Slant slant);
97 
98  void setRenderer(ListBoxRenderer* renderer);
99 
100  protected:
101  const PixmapSurface& picture() const
102  { return _picture; }
103 
104  const Gfx::Font& currentFont() const
105  { return _font; }
106 
107  protected:
108  virtual void onPressed();
109 
110  virtual void onReleased();
111 
112  virtual void onCanceled();
113 
114  protected:
115  virtual Gfx::SizeF onMeasure(const SizePolicy& p);
116 
117  virtual void onInvalidate();
118 
119  virtual void onPaint(PaintSurface& surface, const Gfx::RectF& updateRect);
120 
121  // TODO: why pass PaintSurface?
122  virtual void onPaintContent(PaintSurface& surface, Gfx::Painter& painter);
123 
124  private:
125  Gfx::Font getFont() const;
126 
127  private:
128  enum FontOverride : unsigned
129  {
130  OverrideSize = 0x01,
131  OverrideWeight = 0x02,
132  OverrideSlant = 0x04,
133  OverrideAll = 0xFF
134  };
135 
136  Pt::Signal<ListBoxItem&> _selected;
137  bool _isSelectable;
138  bool _isSelected;
139  String _text;
140 
141  Icon _icon;
142  Gfx::SizeF _iconSize;
143 
144  FacetPtr<ListBoxRenderer> _renderer;
145  bool _hasRenderer;
146 
147  AutoPtr<Gfx::Brush> _background;
148  AutoPtr<Gfx::Pen> _contour;
149  AutoPtr<Gfx::Color> _textColor;
150  Gfx::Font _customFont;
151  unsigned _fontOverride;
152 
153  Gfx::Pen _textPen;
154  Gfx::Font _font;
155  Gfx::Brush _brush;
156  Gfx::Pen _pen;
157  PixmapSurface _picture;
158 };
159 
160 
161 class ListBoxLayout : public FlowLayout
162 {
163  friend class ListBox;
164 
165  public:
166  ListBoxLayout();
167 
168  const std::vector<ListBoxItem*>& selectedItems() const;
169 
170  Pt::Signal<ListBoxItem&>& selected();
171 
172  protected:
173  virtual void onAddControl(Control& control);
174 
175  virtual void onRemoveControl(Control& control);
176 
177  private:
178  void onItemSelected(ListBoxItem& item);
179 
180  private:
181  Pt::Signal<ListBoxItem&> _selected;
182  std::vector<ListBoxItem*> _selectedItems;
183 };
184 
185 
186 class PT_FORMS_API ListBox : public Control
187 {
188  typedef Control Base;
189 
190  public:
191  ListBox();
192 
193  virtual ~ListBox();
194 
195  void setScrollBars(bool hasScrollBars);
196 
197  void addItem(ListBoxItem& item);
198 
199  void removeItem(ListBoxItem& item);
200 
201  const std::vector<ListBoxItem*>& selectedItems() const;
202 
203  Pt::Signal<ListBoxItem&>& selected();
204 
205  void scrollX(int xpos);
206 
207  void scrollY(int ypos);
208 
209  int maximumX() const;
210 
211  int maximumY() const;
212 
213  public:
214  const Gfx::Brush* background() const;
215 
216  void setBackground(const Gfx::Brush& b);
217 
218  void setBackground(bool b);
219 
220  const Gfx::Pen* contour() const;
221 
222  void setContour(const Gfx::Pen& pen);
223 
224  void setFrame(bool b);
225 
226  void setRenderer(ListBoxRenderer* renderer);
227 
228  protected:
229  virtual void onInvalidate();
230 
231  virtual void onPaint(PaintSurface& surface, const Gfx::RectF& updateRect);
232 
233  virtual Gfx::SizeF onMeasure(const SizePolicy& policy);
234 
235  virtual void onLayout(const Gfx::RectF& rect);
236 
237  private:
238  ScrollView _scrollView;
239  ListBoxLayout _layout;
240  FacetPtr<ListBoxRenderer> _renderer;
241  bool _hasRenderer;
242  AutoPtr<Gfx::Brush> _background;
243  bool _hasBackground;
244  AutoPtr<Gfx::Pen> _contour;
245  bool _hasFrame;
246 
247  Spacing _frameSize;
248  Gfx::Brush _brush;
249  Gfx::Pen _pen;
250 };
251 
252 } // namespace
253 
254 } // namespace
255 
256 #endif
Unicode capable basic_string.
Definition: String.h:42