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 
83 class PT_FORMS_API Control : public View
84 {
85  typedef View Base;
86 
87  friend class Form;
88 
89  public:
90  Control();
91 
92  virtual ~Control();
93 
94  public:
95  void setParent(View& parent);
96 
97  void unparent();
98 
99  // implement add method in derived class
100  void add(Control& control);
101 
102  // implement remove method in derived class
103  void remove(Control& control);
104 
105  const std::vector<Control*>& controls() const;
106 
107  public:
108  //Gfx::PaintSurface& surface();
109 
110  //const Gfx::PaintSurface& surface() const;
111 
112  //void setSurface(Gfx::PaintSurface* surface, const Gfx::PointF& pos);
113 
114  private:
115  void setForm(Form* form);
116 
117  public:
118  //
119  // focus handling
120  //
121  FocusPolicy focusPolicy() const;
122 
123  void setFocusPolicy(FocusPolicy f);
124 
125  size_t focusIndex() const;
126 
127  void setFocusIndex(size_t index);
128 
129  bool hasFocus() const;
130 
131  void focus();
132 
133  //
134  // keyboard input
135  //
136  Key actionKey() const;
137 
138  void setActionKey(const Key& ak);
139 
140  const Key* shortcut() const;
141 
142  void setShortcut(const Key* k);
143 
144  const Pt::Char* mnemonic() const;
145 
146  void setMnemonic(const Char& ch);
147 
148  String setMnemonic(const String& text);
149 
150  void setMnemonicControl(Control* control);
151 
152  void processShortcut(const Key& key);
153 
154  void processMnemonic(Pt::Char m);
155 
156  public:
157  // TODO: obsolete when processEvent return true/false if consumed
158  bool acceptsInput() const;
159 
160  void raise();
161 
162  //
163  // invalidation
164  //
165  protected:
166  virtual void onInvalidateEvent(const InvalidateEvent& ev);
167 
168  virtual void onInvalidate();
169 
170  //
171  // painting
172  //
173  protected:
174  virtual void onProcessPaintEvent(const PaintEvent& ev);
175 
176  virtual void onPaintEvent(const PaintEvent& ev);
177 
178  virtual void onPaint(PaintContext& context,
179  const Gfx::RectF& rect);
180 
181  //
182  // layouting
183  //
184  public:
185  const Gfx::RectF geometry() const;
186 
187  const SizePolicy& sizePolicy() const;
188 
189  void setSizePolicy(const SizePolicy& policy);
190 
191  Gfx::SizeF preferredSize() const;
192 
193  Gfx::SizeF measure(const SizePolicy& policy);
194 
195  void relayout();
196 
197  protected:
198  virtual Gfx::SizeF onMeasure(const SizePolicy& policy);
199 
200  virtual void onProcessLayoutEvent(const LayoutEvent& ev);
201 
202  virtual void onLayoutEvent(const LayoutEvent& ev);
203 
204  virtual void onLayout(const Gfx::RectF& rect);
205 
206  public:
207  // outer spacing
208  const Spacing& margin() const;
209 
210  // outer spacing
211  void setMargin(const Spacing& s);
212 
213  // outer spacing
214  void setMargin(double n);
215 
216  // inner spacing
217  void setMargin(double horiz, double vertical);
218 
219  // inner spacing
220  const Spacing& padding() const;
221 
222  // inner spacing
223  void setPadding(const Spacing& p);
224 
225  // inner spacing
226  void setPadding(double n);
227 
228  // inner spacing
229  void setPadding(double horiz, double vertical);
230 
231  protected:
232  virtual void onSetSizeLimits(const Gfx::SizeF& minSize,
233  const Gfx::SizeF& maxSize);
234 
235  protected:
236  virtual void onSetSurface(PaintSurface* surface, const Gfx::PointF& pos);
237 
238  virtual void onAddControl(Control& control);
239 
240  virtual void onRemoveControl(Control& control);
241 
242 
243  virtual void onActionKey(const KeyEvent& kev);
244 
245  virtual void onShortcut(const Key& kev);
246 
247  virtual void onMnemonic(Pt::Char m);
248 
249  //
250  // Widget
251  //
252  protected:
253  virtual void onConnect(Screen& screen);
254 
255  virtual void onDisconnect();
256 
257 
258  virtual Widget* onHitTest(const Gfx::PointF& p);
259 
260  virtual Gfx::PointF onToParent(const Gfx::PointF& pos) const;
261 
262  virtual Gfx::PointF onFromParent(const Gfx::PointF& pos) const;
263 
264 
265  virtual void onRequestRepaint(const Gfx::RectF& rect);
266 
267  virtual void onRequestShow(bool isShown);
268 
269  virtual void onRequestEnable(bool isEnable);
270 
271  virtual void onRequestActivate(bool active);
272 
273  virtual void onRequestMove(const Gfx::PointF& pos);
274 
275  virtual void onRequestResize(const Gfx::SizeF& s);
276 
277 
278  virtual void onProcessEvent(const Pt::Event& ev);
279 
280 
281  virtual void onProcessEnableEvent(const EnableEvent& ev);
282 
283  virtual void onEnableEvent(const EnableEvent& ev);
284 
285  virtual void onEnable(bool isEnable);
286 
287 
288  virtual void onProcessShowEvent(const ShowEvent& ev);
289 
290  virtual void onShowEvent(const ShowEvent& ev);
291 
292  virtual void onShow(bool visible);
293 
294 
295  virtual void onProcessFocusEvent(const FocusEvent& ev);
296 
297  virtual void onFocusEvent(const FocusEvent& ev);
298 
299 
300  virtual void onProcessRescaleEvent(const RescaleEvent& ev);
301 
302  virtual void onRescaleEvent(const RescaleEvent& ev);
303 
304  virtual void onRescale(double scaling);
305 
306 
307  virtual void onProcessMoveEvent(const MoveEvent& ev);
308 
309  virtual void onMoveEvent(const MoveEvent& ev);
310 
311 
312  virtual void onProcessResizeEvent(const ResizeEvent& ev);
313 
314  virtual void onResizeEvent(const ResizeEvent& ev);
315 
316 
317  virtual void onProcessMouseEvent(const MouseEvent& ev);
318 
319  virtual void onProcessTouchEvent(const TouchEvent& ev);
320 
321  virtual void onProcessScrollEvent(const ScrollEvent& ev);
322 
323  virtual void onProcessEnterEvent(const EnterEvent& ev);
324 
325  virtual void onProcessLeaveEvent(const LeaveEvent& ev);
326 
327  virtual void onProcessKeyEvent(const KeyEvent& ev);
328 
329  //
330  // Responder
331  //
332  protected:
333  virtual bool onMouseEvent(const MouseEvent& ev);
334 
335  virtual bool onTouchEvent(const TouchEvent& ev);
336 
337  virtual bool onScrollEvent( const ScrollEvent& ev);
338 
339  virtual bool onKeyEvent(const KeyEvent& ev);
340 
341  virtual bool onEnterEvent( const EnterEvent& ev);
342 
343  virtual bool onLeaveEvent(const LeaveEvent& ev);
344 
345  //
346  // View
347  //
348  protected:
349  virtual Gfx::PointF onToControl(const Control& control,
350  const Gfx::PointF& pos) const;
351 
352  virtual Gfx::PointF onFromControl(const Control& control,
353  const Gfx::PointF& pos) const;
354 
355  virtual void onAttach(Control& control);
356 
357  virtual void onDetach(Control& control);
358 
359  virtual void onInit(Control& control);
360 
361  virtual void onRelease(Control& control);
362 
363  protected:
364  virtual void onRepaintRequest(Control& control, const Gfx::RectF& rect);
365 
366  virtual void onRelayoutRequest(Control& control);
367 
368  virtual void onEnableRequest(Control& control, bool isEnable);
369 
370  virtual void onActivateRequest(Control& control, bool active);
371 
372  virtual void onShowRequest(Control& control, bool isShown);
373 
374  virtual void onMoveRequest(Control& control, const Gfx::PointF& pos);
375 
376  virtual void onResizeRequest(Control& control, const Gfx::SizeF& size);
377 
378  virtual void onRaiseRequest(Control& control);
379 
380  virtual const std::vector<Key> onGetShortcuts();
381 
382  virtual const std::vector<Char> onGetMnemonics();
383 
384  private:
385  View* _parent;
386  std::vector<Control*> _children;
387 
388  Form* _form;
389 
390  bool _isCapture;
391  bool _isLayoutInvalid;
392 
393  bool _show;
394  bool _enabled;
395 
396  Gfx::PointF _requestedPosition;
397  Gfx::SizeF _requestedSize;
398 
399  bool _isMeasureInvalid;
400 
401  SizePolicy _sizePolicy;
402  SizePolicy _lastPolicy;
403  Gfx::SizeF _preferredSize;
404 
405  bool _hasFocus;
406  FocusPolicy _focusPolicy;
407  size_t _focusIndex;
408 
409  Key _actionKey;
410  Key _shortcutKey;
411  Pt::Char _mnemonic;
412  Pt::Signal<Pt::Char> _mnemonicEntered;
413 
414  Spacing _padding;
415  Spacing _margin;
416 };
417 
418 } // namespace
419 
420 } // namespace
421 
422 #endif // include guard
Core module.
Definition: Allocator.h:33
Base class for all event types.
Definition: Event.h:50
Paint surface.
Definition: PaintSurface.h:44
Paint context.
Definition: PaintContext.h:45
Common base for objects that participate in the Forms runtime.
Definition: Widget.h:73
Size with floating-point width and height.
Definition: Size.h:47
Point with floating-point X and Y coordinates.
Definition: Point.h:47
Unicode character type.
Definition: String.h:67
A view that can be attached as application content.
Definition: Control.h:84
Screen of a display.
Definition: Screen.h:64
A widget that connects controls to a paint surface.
Definition: View.h:66
Unicode capable basic_string.
Definition: Api-String.h:44
Rect with floating-point coordinates.
Definition: Rect.h:47
A view that hosts one content control and manages its form state.
Definition: Form.h:71