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
61namespace Pt {
62
63namespace Forms {
64
65class Form;
66class Key;
67
106class PT_FORMS_API Control : public View
107{
108 typedef View Base;
109
110 friend class Form;
111
112 public:
118
123 virtual ~Control();
124
125 public:
134
140 void unparent();
141
147 void add(Control& control);
148
154 void remove(Control& control);
155
158 const std::vector<Control*>& controls() const;
159
160 private:
161 void setForm(Form* form);
162
163 public:
167
175
178 size_t focusIndex() const;
179
184 void setFocusIndex(size_t index);
185
188 bool hasFocus() const;
189
196 void focus();
197
200 Key actionKey() const;
201
206 void setActionKey(const Key& ak);
207
210 const Key* shortcut() const;
211
218 void setShortcut(const Key* k);
219
222 const Pt::Char* mnemonic() const;
223
229 void setMnemonic(const Char& ch);
230
238
244
251 void processShortcut(const Key& key);
252
260
261 public:
267 bool acceptsInput() const;
268
274 void raise();
275
276 //
277 // invalidation
278 //
279 protected:
286 virtual void onInvalidateEvent(const InvalidateEvent& ev);
287
293 virtual void onInvalidate();
294
295 //
296 // painting
297 //
298 protected:
305 virtual void onProcessPaintEvent(const PaintEvent& ev);
306
312 virtual void onPaintEvent(const PaintEvent& ev);
313
319 virtual void onPaint(PaintContext& context,
320 const Gfx::RectF& rect);
321
322 //
323 // layouting
324 //
325 public:
328 const Gfx::RectF geometry() const;
329
332 const SizePolicy& sizePolicy() const;
333
338 void setSizePolicy(const SizePolicy& policy);
339
342 Gfx::SizeF preferredSize() const;
343
352 Gfx::SizeF measure(const SizePolicy& policy);
353
359 void relayout();
360
361 protected:
367 virtual Gfx::SizeF onMeasure(const SizePolicy& policy);
368
376 virtual void onProcessLayoutEvent(const LayoutEvent& ev);
377
380 virtual void onLayoutEvent(const LayoutEvent& ev);
381
387 virtual void onLayout(const Gfx::RectF& rect);
388
389 public:
392 const Spacing& margin() const;
393
396 void setMargin(const Spacing& s);
397
400 void setMargin(double n);
401
404 void setMargin(double horiz, double vertical);
405
408 const Spacing& padding() const;
409
412 void setPadding(const Spacing& p);
413
416 void setPadding(double n);
417
420 void setPadding(double horiz, double vertical);
421
422 protected:
425 virtual void onSetSizeLimits(const Gfx::SizeF& minSize,
426 const Gfx::SizeF& maxSize);
427
428 protected:
434 virtual void onSetSurface(PaintSurface* surface, const Gfx::PointF& pos);
435
438 virtual void onAddControl(Control& control);
439
442 virtual void onRemoveControl(Control& control);
443
444
447 virtual void onActionKey(const KeyEvent& kev);
448
451 virtual void onShortcut(const Key& kev);
452
458 virtual void onMnemonic(Pt::Char m);
459
460 //
461 // Widget
462 //
463 protected:
466 virtual void onConnect(Screen& screen);
467
470 virtual void onDisconnect();
471
472
478 virtual Widget* onHitTest(const Gfx::PointF& p);
479
482 virtual Gfx::PointF onToParent(const Gfx::PointF& pos) const;
483
486 virtual Gfx::PointF onFromParent(const Gfx::PointF& pos) const;
487
488
491 virtual void onRequestRepaint(const Gfx::RectF& rect);
492
495 virtual void onRequestShow(bool isShown);
496
499 virtual void onRequestEnable(bool isEnable);
500
503 virtual void onRequestActivate(bool active);
504
507 virtual void onRequestMove(const Gfx::PointF& pos);
508
511 virtual void onRequestResize(const Gfx::SizeF& s);
512
513
516 virtual void onProcessEvent(const Pt::Event& ev);
517
518
523 virtual void onProcessEnableEvent(const EnableEvent& ev);
524
527 virtual void onEnableEvent(const EnableEvent& ev);
528
531 virtual void onEnable(bool isEnable);
532
533
536 virtual void onProcessShowEvent(const ShowEvent& ev);
537
540 virtual void onShowEvent(const ShowEvent& ev);
541
544 virtual void onShow(bool visible);
545
546
549 virtual void onProcessFocusEvent(const FocusEvent& ev);
550
553 virtual void onFocusEvent(const FocusEvent& ev);
554
555
558 virtual void onProcessRescaleEvent(const RescaleEvent& ev);
559
562 virtual void onRescaleEvent(const RescaleEvent& ev);
563
566 virtual void onRescale(double scaling);
567
568
571 virtual void onProcessMoveEvent(const MoveEvent& ev);
572
575 virtual void onMoveEvent(const MoveEvent& ev);
576
577
580 virtual void onProcessResizeEvent(const ResizeEvent& ev);
581
584 virtual void onResizeEvent(const ResizeEvent& ev);
585
586
593 virtual void onProcessMouseEvent(const MouseEvent& ev);
594
601 virtual void onProcessTouchEvent(const TouchEvent& ev);
602
605 virtual void onProcessScrollEvent(const ScrollEvent& ev);
606
609 virtual void onProcessEnterEvent(const EnterEvent& ev);
610
613 virtual void onProcessLeaveEvent(const LeaveEvent& ev);
614
617 virtual void onProcessKeyEvent(const KeyEvent& ev);
618
619 //
620 // Responder
621 //
622 protected:
628 virtual bool onMouseEvent(const MouseEvent& ev);
629
635 virtual bool onTouchEvent(const TouchEvent& ev);
636
639 virtual bool onScrollEvent( const ScrollEvent& ev);
640
643 virtual bool onKeyEvent(const KeyEvent& ev);
644
647 virtual bool onEnterEvent( const EnterEvent& ev);
648
651 virtual bool onLeaveEvent(const LeaveEvent& ev);
652
653 //
654 // View
655 //
656 protected:
659 virtual Gfx::PointF onToControl(const Control& control,
660 const Gfx::PointF& pos) const;
661
664 virtual Gfx::PointF onFromControl(const Control& control,
665 const Gfx::PointF& pos) const;
666
669 virtual void onAttach(Control& control);
670
673 virtual void onDetach(Control& control);
674
681 virtual void onInit(Control& control);
682
688 virtual void onRelease(Control& control);
689
690 protected:
693 virtual void onRepaintRequest(Control& control, const Gfx::RectF& rect);
694
697 virtual void onRelayoutRequest(Control& control);
698
701 virtual void onEnableRequest(Control& control, bool isEnable);
702
705 virtual void onActivateRequest(Control& control, bool active);
706
709 virtual void onShowRequest(Control& control, bool isShown);
710
713 virtual void onMoveRequest(Control& control, const Gfx::PointF& pos);
714
717 virtual void onResizeRequest(Control& control, const Gfx::SizeF& size);
718
721 virtual void onRaiseRequest(Control& control);
722
725 virtual const std::vector<Key> onGetShortcuts();
726
729 virtual const std::vector<Char> onGetMnemonics();
730
731 private:
732 View* _parent;
733 std::vector<Control*> _children;
734
735 Form* _form;
736
737 bool _isCapture;
738 bool _isLayoutInvalid;
739
740 bool _show;
741 bool _enabled;
742
743 Gfx::PointF _requestedPosition;
744 Gfx::SizeF _requestedSize;
745
746 bool _isMeasureInvalid;
747
748 SizePolicy _sizePolicy;
749 SizePolicy _lastPolicy;
750 Gfx::SizeF _preferredSize;
751
752 bool _hasFocus;
753 FocusPolicy _focusPolicy;
754 size_t _focusIndex;
755
756 Key _actionKey;
757 Key _shortcutKey;
758 Pt::Char _mnemonic;
759 Pt::Signal<Pt::Char> _mnemonicEntered;
760
761 Spacing _padding;
762 Spacing _margin;
763};
764
765} // namespace
766
767} // namespace
768
769#endif // include guard
Base class for typed events.
Definition Event.h:66
virtual void onProcessLeaveEvent(const LeaveEvent &ev)
Dispatches a pointer-leave event.
virtual bool onMouseEvent(const MouseEvent &ev)
Handles mouse input in local coordinates.
void setFocusPolicy(FocusPolicy f)
Sets this control's focus policy.
void setMnemonic(const Char &ch)
Registers ch as this control's Alt mnemonic.
virtual void onProcessShowEvent(const ShowEvent &ev)
Dispatches a visibility event.
void setShortcut(const Key *k)
Registers k as this control's form shortcut.
virtual void onRequestMove(const Gfx::PointF &pos)
Records and forwards a move request to the parent view.
virtual void onProcessRescaleEvent(const RescaleEvent &ev)
Dispatches a scale change to this control and all children.
void setPadding(double n)
Sets equal inner spacing on every side and requests relayout.
virtual void onRequestShow(bool isShown)
Records and forwards a visibility request to the parent view.
void add(Control &control)
Attaches control as a direct child.
virtual void onFocusEvent(const FocusEvent &ev)
Updates focus state and requests invalidation.
void setFocusIndex(size_t index)
Sets this control's focus-navigation index.
virtual void onMoveEvent(const MoveEvent &ev)
Updates parent-local position from a move event.
virtual void onDisconnect()
Disconnects this control and all direct descendants from the screen.
virtual void onRescaleEvent(const RescaleEvent &ev)
Handles a scale change.
virtual bool onTouchEvent(const TouchEvent &ev)
Handles touch input in local coordinates.
virtual void onEnable(bool isEnable)
Performs behavior associated with effective enabled state isEnable.
virtual Gfx::PointF onToControl(const Control &control, const Gfx::PointF &pos) const
Converts this control coordinates to direct child coordinates.
virtual void onEnableEvent(const EnableEvent &ev)
Updates enabled state and handles an enable event.
void relayout()
Invalidates this control's measurement and layout.
void processShortcut(const Key &key)
Invokes this control's shortcut hook for key.
void setPadding(double horiz, double vertical)
Sets horizontal and vertical inner spacing and requests relayout.
const Key * shortcut() const
Returns the registered form shortcut, or 0 when none is set.
void setParent(View &parent)
Attaches this control to parent.
void setActionKey(const Key &ak)
Sets the focused action key to ak.
Key actionKey() const
Returns the key that invokes onActionKey() while focused.
virtual Widget * onHitTest(const Gfx::PointF &p)
Returns the frontmost descendant hit at local position p.
void setPadding(const Spacing &p)
Sets the inner spacing to p and requests relayout.
void setMargin(double n)
Sets equal outer spacing on every side and requests relayout.
virtual void onPaint(PaintContext &context, const Gfx::RectF &rect)
Paints custom content in local coordinates.
virtual void onRequestEnable(bool isEnable)
Records and forwards an enable request to the parent view.
virtual bool onEnterEvent(const EnterEvent &ev)
Handles pointer entry or continues responder dispatch.
virtual void onShowRequest(Control &control, bool isShown)
Delivers a visibility request to direct child control.
virtual void onResizeEvent(const ResizeEvent &ev)
Updates local size and bounds from a resize event.
bool hasFocus() const
Returns true when this control currently has form focus.
const std::vector< Control * > & controls() const
Returns the non-owning, ordered list of direct children.
virtual void onRelayoutRequest(Control &control)
Requests relayout after a direct child changes its layout.
virtual void onEnableRequest(Control &control, bool isEnable)
Delivers an effective enable request to direct child control.
virtual void onProcessResizeEvent(const ResizeEvent &ev)
Dispatches a resize event.
virtual Gfx::PointF onFromControl(const Control &control, const Gfx::PointF &pos) const
Converts direct child coordinates to this control coordinates.
const Spacing & padding() const
Returns the inner spacing requested around this control's content.
virtual void onLayout(const Gfx::RectF &rect)
Assigns geometry to direct children within rect.
const Spacing & margin() const
Returns the outer spacing requested around this control.
virtual const std::vector< Char > onGetMnemonics()
Returns mnemonics registered by this control.
virtual void onConnect(Screen &screen)
Connects this control and all direct descendants to screen.
String setMnemonic(const String &text)
Extracts and registers a mnemonic marked by an ampersand.
virtual void onProcessMoveEvent(const MoveEvent &ev)
Dispatches a move event.
void setSizePolicy(const SizePolicy &policy)
Sets this control's requested size policy and requests relayout.
virtual void onAddControl(Control &control)
Notifies after control becomes a direct child.
virtual void onRepaintRequest(Control &control, const Gfx::RectF &rect)
Converts and forwards rect from control for repainting.
Control()
Creates an unparented control.
virtual void onProcessEvent(const Pt::Event &ev)
Dispatches ev through the inherited widget event dispatcher.
virtual void onShowEvent(const ShowEvent &ev)
Handles a visibility event and requests relayout.
virtual void onRequestResize(const Gfx::SizeF &s)
Records and forwards a resize request to the parent view.
virtual void onProcessFocusEvent(const FocusEvent &ev)
Dispatches a focus event.
virtual Gfx::PointF onFromParent(const Gfx::PointF &pos) const
Converts containing-view coordinates to local coordinates.
virtual void onProcessMouseEvent(const MouseEvent &ev)
Routes mouse input to the frontmost eligible child or this control.
virtual void onProcessEnterEvent(const EnterEvent &ev)
Dispatches a pointer-entry event.
Gfx::SizeF preferredSize() const
Returns the result of the most recent measure() call.
virtual void onDetach(Control &control)
Removes control as a direct child and invokes onRemoveControl().
virtual void onMoveRequest(Control &control, const Gfx::PointF &pos)
Forwards a direct child's move request to the parent view.
void processMnemonic(Pt::Char m)
Invokes this control's mnemonic hook for m.
virtual void onRemoveControl(Control &control)
Notifies after control stops being a direct child.
virtual void onActionKey(const KeyEvent &kev)
Handles the configured action key while this control has focus.
virtual ~Control()
Detaches this control from its parent and releases its children.
virtual const std::vector< Key > onGetShortcuts()
Returns shortcuts registered by this control.
virtual Gfx::SizeF onMeasure(const SizePolicy &policy)
Measures intrinsic content for effective policy policy.
virtual void onAttach(Control &control)
Stores control as a direct child and invokes onAddControl().
virtual bool onLeaveEvent(const LeaveEvent &ev)
Handles pointer leave or continues responder dispatch.
virtual void onInvalidateEvent(const InvalidateEvent &ev)
Handles the final coalesced invalidation event.
virtual void onSetSurface(PaintSurface *surface, const Gfx::PointF &pos)
Propagates a surface assignment to direct children.
virtual void onActivateRequest(Control &control, bool active)
Forwards a direct child's activation request to the parent view.
virtual void onProcessScrollEvent(const ScrollEvent &ev)
Delivers scroll input only while this control accepts input.
virtual void onRequestActivate(bool active)
Forwards an activation request to the parent view.
void setMnemonicControl(Control *control)
Forwards mnemonic activation to control.
void unparent()
Detaches this control from its current parent.
const SizePolicy & sizePolicy() const
Returns this control's requested size policy.
const Gfx::RectF geometry() const
Returns the position and size in parent logical coordinates.
virtual bool onKeyEvent(const KeyEvent &ev)
Handles focused action keys or continues responder dispatch.
virtual void onLayoutEvent(const LayoutEvent &ev)
Handles the aligned layout rectangle and requests repaint.
virtual void onShortcut(const Key &kev)
Handles an activated form shortcut.
virtual void onResizeRequest(Control &control, const Gfx::SizeF &size)
Forwards a direct child's resize request to the parent view.
virtual void onSetSizeLimits(const Gfx::SizeF &minSize, const Gfx::SizeF &maxSize)
Requests relayout after local size limits change.
virtual void onMnemonic(Pt::Char m)
Handles an activated form mnemonic.
virtual void onProcessLayoutEvent(const LayoutEvent &ev)
Processes a pending layout event.
const Pt::Char * mnemonic() const
Returns the registered mnemonic character, or 0 when none is set.
Gfx::SizeF measure(const SizePolicy &policy)
Measures the preferred size under parent constraint policy.
virtual void onRequestRepaint(const Gfx::RectF &rect)
Forwards a local repaint request to the parent view.
void setMargin(const Spacing &s)
Sets the outer spacing to s and requests relayout.
void focus()
Requests focus from the containing form.
virtual bool onScrollEvent(const ScrollEvent &ev)
Handles scroll input or returns false to continue dispatch.
virtual void onRaiseRequest(Control &control)
Moves direct child control to the front of child order.
virtual void onPaintEvent(const PaintEvent &ev)
Handles this control's own paint event.
virtual void onProcessPaintEvent(const PaintEvent &ev)
Processes a paint event and paints visible child controls.
void raise()
Requests that this control be painted above its siblings.
virtual void onRescale(double scaling)
Aligns local spacing and size-policy values to scaling.
virtual void onProcessEnableEvent(const EnableEvent &ev)
Dispatches effective enabled state to this control and its children.
virtual void onInit(Control &control)
Initializes runtime relationships for newly attached control.
virtual void onRelease(Control &control)
Releases runtime relationships for detached control.
virtual void onShow(bool visible)
Performs behavior associated with visibility state visible.
virtual void onProcessKeyEvent(const KeyEvent &ev)
Delivers key input only while this control accepts input.
virtual void onProcessTouchEvent(const TouchEvent &ev)
Routes touch input to the frontmost eligible child or this control.
virtual void onInvalidate()
Updates derived state after invalidation.
void remove(Control &control)
Detaches direct child control without destroying it.
size_t focusIndex() const
Returns this control's focus-navigation index.
void setMargin(double horiz, double vertical)
Sets horizontal and vertical outer spacing and requests relayout.
FocusPolicy focusPolicy() const
Returns this control's focus policy.
virtual Gfx::PointF onToParent(const Gfx::PointF &pos) const
Converts local coordinates to the containing view.
bool acceptsInput() const
Returns true when this control is visible and enabled.
Pointer entered a widget.
Definition EnterEvent.h:49
Reports that a widget gained or lost focus.
Definition FocusEvent.h:48
View that hosts one content control.
Definition Form.h:91
Key press or release.
Definition KeyEvent.h:50
Key code and modifiers.
Definition Key.h:23
Pointer left a widget.
Definition LeaveEvent.h:49
Pointer movement or button change.
Definition MouseEvent.h:134
Reports that a widget moved.
Definition MoveEvent.h:50
Active painting session on a Forms paint surface.
Definition PaintContext.h:51
Reports a dirty rectangle that a widget must paint.
Definition PaintEvent.h:48
Forms render target for painting.
Definition PaintSurface.h:50
Reports that a widget was resized.
Definition ResizeEvent.h:50
Display root for top-level windows.
Definition Screen.h:83
Scroll or wheel movement.
Definition ScrollEvent.h:51
Reports that a widget was shown or hidden.
Definition ShowEvent.h:48
Constraint used when measuring a control.
Definition SizePolicy.h:48
Stores four-sided inset values.
Definition Spacing.h:58
Touch press, move, or release.
Definition TouchEvent.h:51
PaintSurface & surface()
Returns this view's local paint-surface adapter.
View()
Creates a view for a derived content host.
FocusPolicy
Defines whether a control can receive or retain focus.
Definition View.h:95
Common type of every visual Forms object.
Definition Widget.h:105
Widget * parent()
Returns the non-owning parent, or 0 when unparented.
const Gfx::Scaling & scaling() const
Returns the current logical-to-device scaling.
const Gfx::SizeF & size() const
Returns the size in local logical coordinates.
Screen * screen()
Returns the connected screen, or 0 when disconnected.
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