Control.h
1 /* Copyright (C) 2015 Marc Boris Duerner
2  Copyright (C) 2015 Laurentiu-Gheorghe Crisan
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  As a special exception, you may use this file as part of a free
10  software library without restriction. Specifically, if other files
11  instantiate templates or use macros or inline functions from this
12  file, or you compile this file and link it with other files to
13  produce an executable, this file does not by itself cause the
14  resulting executable to be covered by the GNU General Public
15  License. This exception does not however invalidate any other
16  reasons why the executable file might be covered by the GNU Library
17  General Public License.
18 
19  This library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  Lesser General Public License for more details.
23 
24  You should have received a copy of the GNU Lesser General Public
25  License along with this library; if not, write to the Free Software
26  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27  02110-1301 USA
28 */
29 
30 #ifndef PT_FORMS_CONTROL_H
31 #define PT_FORMS_CONTROL_H
32 
33 #include <Pt/Forms/View.h>
34 #include <Pt/Forms/SizePolicy.h>
35 #include <Pt/Forms/Spacing.h>
36 
37 #include <Pt/Forms/MouseEvent.h>
38 #include <Pt/Forms/TouchEvent.h>
39 #include <Pt/Forms/ScrollEvent.h>
40 #include <Pt/Forms/KeyEvent.h>
41 #include <Pt/Forms/ResizeEvent.h>
42 #include <Pt/Forms/MoveEvent.h>
43 #include <Pt/Forms/PaintEvent.h>
44 #include <Pt/Forms/EnterEvent.h>
45 #include <Pt/Forms/LeaveEvent.h>
46 #include <Pt/Forms/EnableEvent.h>
47 #include <Pt/Forms/InvalidateEvent.h>
48 #include <Pt/Forms/LayoutEvent.h>
49 #include <Pt/Forms/ShowEvent.h>
50 #include <Pt/Forms/FocusEvent.h>
51 
52 #include <Pt/Gfx/PaintSurface.h>
53 #include <Pt/Gfx/Point.h>
54 #include <Pt/Gfx/Size.h>
55 #include <Pt/Gfx/Color.h>
56 
57 #include <Pt/Signal.h>
58 #include <Pt/Delegate.h>
59 #include <vector>
60 
61 namespace Pt {
62 
63 namespace Forms {
64 
65 class Form;
66 class Key;
67 
68 class PT_FORMS_API Control : public View
69 {
70  typedef View Base;
71 
72  friend class Form;
73 
74  public:
75  Control();
76 
77  virtual ~Control();
78 
79  public:
80  void setParent(View& parent);
81 
82  void unparent();
83 
84  // implement add method in derived class
85  void add(Control& control);
86 
87  // implement remove method in derived class
88  void remove(Control& control);
89 
90  const std::vector<Control*>& controls() const;
91 
92  public:
93  //Gfx::PaintSurface& surface();
94 
95  //const Gfx::PaintSurface& surface() const;
96 
97  //void setSurface(Gfx::PaintSurface* surface, const Gfx::PointF& pos);
98 
99  private:
100  void setForm(Form* form);
101 
102  public:
103  //
104  // focus handling
105  //
106  FocusPolicy focusPolicy() const;
107 
108  void setFocusPolicy(FocusPolicy f);
109 
110  size_t focusIndex() const;
111 
112  void setFocusIndex(size_t index);
113 
114  bool hasFocus() const;
115 
116  void focus();
117 
118  //
119  // keyboard input
120  //
121  Key actionKey() const;
122 
123  void setActionKey(const Key& ak);
124 
125  const Key* shortcut() const;
126 
127  void setShortcut(const Key* k);
128 
129  const Pt::Char* mnemonic() const;
130 
131  void setMnemonic(const Char& ch);
132 
133  String setMnemonic(const String& text);
134 
135  void setMnemonicControl(Control* control);
136 
137  void processShortcut(const Key& key);
138 
139  void processMnemonic(Pt::Char m);
140 
141  public:
142  // TODO: obsolete when processEvent return true/false if consumed
143  bool acceptsInput() const;
144 
145 
146  void raise();
147 
148  //
149  // invalidation
150  //
151  protected:
152  virtual void onInvalidateEvent(const InvalidateEvent& ev);
153 
154  virtual void onInvalidate();
155 
156  //
157  // painting
158  //
159  protected:
160  virtual void onProcessPaintEvent(const PaintEvent& ev);
161 
162  virtual void onPaintEvent(const PaintEvent& ev);
163 
164  virtual void onPaint(PaintSurface& surface,
165  const Gfx::RectF& rect);
166 
167  //
168  // layouting
169  //
170  public:
171  const Gfx::RectF geometry() const;
172 
173  const SizePolicy& sizePolicy() const;
174 
175  void setSizePolicy(const SizePolicy& policy);
176 
177  Gfx::SizeF preferredSize() const;
178 
179  Gfx::SizeF measure(const SizePolicy& policy);
180 
181  void relayout();
182 
183  protected:
184  virtual Gfx::SizeF onMeasure(const SizePolicy& policy);
185 
186  virtual void onProcessLayoutEvent(const LayoutEvent& ev);
187 
188  virtual void onLayoutEvent(const LayoutEvent& ev);
189 
190  virtual void onLayout(const Gfx::RectF& rect);
191 
192  public:
193  // outer spacing
194  const Spacing& margin() const;
195 
196  // outer spacing
197  void setMargin(const Spacing& s);
198 
199  // outer spacing
200  void setMargin(double n);
201 
202  // inner spacing
203  void setMargin(double horiz, double vertical);
204 
205  // inner spacing
206  const Spacing& padding() const;
207 
208  // inner spacing
209  void setPadding(const Spacing& p);
210 
211  // inner spacing
212  void setPadding(double n);
213 
214  // inner spacing
215  void setPadding(double horiz, double vertical);
216 
217  protected:
218  virtual void onSetSizeLimits(const Gfx::SizeF& minSize,
219  const Gfx::SizeF& maxSize);
220 
221  protected:
222  virtual void onSetSurface(PaintSurface* surface, const Gfx::PointF& pos);
223 
224  virtual void onAddControl(Control& control);
225 
226  virtual void onRemoveControl(Control& control);
227 
228 
229  virtual void onActionKey(const KeyEvent& kev);
230 
231  virtual void onShortcut(const Key& kev);
232 
233  virtual void onMnemonic(Pt::Char m);
234 
235  //
236  // Widget
237  //
238  protected:
239  virtual void onConnect(Screen& screen);
240 
241  virtual void onDisconnect();
242 
243 
244  virtual Widget* onHitTest(const Gfx::PointF& p);
245 
246  virtual Gfx::PointF onToParent(const Gfx::PointF& pos) const;
247 
248  virtual Gfx::PointF onFromParent(const Gfx::PointF& pos) const;
249 
250 
251  virtual void onRequestRepaint(const Gfx::RectF& rect);
252 
253  virtual void onRequestShow(bool isShown);
254 
255  virtual void onRequestEnable(bool isEnable);
256 
257  virtual void onRequestActivate(bool active);
258 
259  virtual void onRequestMove(const Gfx::PointF& pos);
260 
261  virtual void onRequestResize(const Gfx::SizeF& s);
262 
263 
264  virtual void onProcessEvent(const Pt::Event& ev);
265 
266 
267  virtual void onProcessEnableEvent(const EnableEvent& ev);
268 
269  virtual void onEnableEvent(const EnableEvent& ev);
270 
271  virtual void onEnable(bool isEnable);
272 
273 
274  virtual void onProcessShowEvent(const ShowEvent& ev);
275 
276  virtual void onShowEvent(const ShowEvent& ev);
277 
278  virtual void onShow(bool visible);
279 
280 
281  virtual void onProcessFocusEvent(const FocusEvent& ev);
282 
283  virtual void onFocusEvent(const FocusEvent& ev);
284 
285 
286  virtual void onProcessRescaleEvent(const RescaleEvent& ev);
287 
288  virtual void onRescaleEvent(const RescaleEvent& ev);
289 
290  virtual void onRescale(double scaling);
291 
292 
293  virtual void onProcessMoveEvent(const MoveEvent& ev);
294 
295  virtual void onMoveEvent(const MoveEvent& ev);
296 
297 
298  virtual void onProcessResizeEvent(const ResizeEvent& ev);
299 
300  virtual void onResizeEvent(const ResizeEvent& ev);
301 
302 
303  virtual void onProcessMouseEvent(const MouseEvent& ev);
304 
305  virtual void onProcessTouchEvent(const TouchEvent& ev);
306 
307  virtual void onProcessScrollEvent(const ScrollEvent& ev);
308 
309  virtual void onProcessEnterEvent(const EnterEvent& ev);
310 
311  virtual void onProcessLeaveEvent(const LeaveEvent& ev);
312 
313  virtual void onProcessKeyEvent(const KeyEvent& ev);
314 
315  //
316  // Responder
317  //
318  protected:
319  virtual bool onMouseEvent(const MouseEvent& ev);
320 
321  virtual bool onTouchEvent(const TouchEvent& ev);
322 
323  virtual bool onScrollEvent( const ScrollEvent& ev);
324 
325  virtual bool onKeyEvent(const KeyEvent& ev);
326 
327  virtual bool onEnterEvent( const EnterEvent& ev);
328 
329  virtual bool onLeaveEvent(const LeaveEvent& ev);
330 
331  //
332  // View
333  //
334  protected:
335  virtual Gfx::PointF onToControl(const Control& control,
336  const Gfx::PointF& pos) const;
337 
338  virtual Gfx::PointF onFromControl(const Control& control,
339  const Gfx::PointF& pos) const;
340 
341  virtual void onAttach(Control& control);
342 
343  virtual void onDetach(Control& control);
344 
345  virtual void onInit(Control& control);
346 
347  virtual void onRelease(Control& control);
348 
349  protected:
350  virtual void onRepaintRequest(Control& control, const Gfx::RectF& rect);
351 
352  virtual void onRelayoutRequest(Control& control);
353 
354  virtual void onEnableRequest(Control& control, bool isEnable);
355 
356  virtual void onActivateRequest(Control& control, bool active);
357 
358  virtual void onShowRequest(Control& control, bool isShown);
359 
360  virtual void onMoveRequest(Control& control, const Gfx::PointF& pos);
361 
362  virtual void onResizeRequest(Control& control, const Gfx::SizeF& size);
363 
364  virtual void onRaiseRequest(Control& control);
365 
366  virtual const std::vector<Key> onGetShortcuts();
367 
368  virtual const std::vector<Char> onGetMnemonics();
369 
370  private:
371  View* _parent;
372  std::vector<Control*> _children;
373 
374  Form* _form;
375 
376  bool _isCapture;
377  bool _isLayoutInvalid;
378 
379  bool _show;
380  bool _enabled;
381 
382  Gfx::PointF _requestedPosition;
383  Gfx::SizeF _requestedSize;
384 
385  bool _isMeasureInvalid;
386 
387  SizePolicy _sizePolicy;
388  SizePolicy _lastPolicy;
389  Gfx::SizeF _preferredSize;
390 
391  bool _hasFocus;
392  FocusPolicy _focusPolicy;
393  size_t _focusIndex;
394 
395  Key _actionKey;
396  Key _shortcutKey;
397  Pt::Char _mnemonic;
398  Pt::Signal<Pt::Char> _mnemonicEntered;
399 
400  Spacing _padding;
401  Spacing _margin;
402 };
403 
404 } // namespace
405 
406 } // namespace
407 
408 #endif // include guard
Base class for all event types.
Definition: Event.h:49
Multicast Signal to call multiple slots.
Definition: Signal.h:109
Unicode character type.
Definition: String.h:66