Widget.h
1/* Copyright (C) 2015-2026 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_WIDGET_H
30#define PT_FORMS_WIDGET_H
31
32#include <Pt/Forms/Api.h>
33#include <Pt/Forms/Responder.h>
34#include <Pt/Forms/Cursor.h>
35
36#include <Pt/Gfx/Scaling.h>
37#include <Pt/Gfx/Point.h>
38#include <Pt/Gfx/Size.h>
39#include <Pt/Gfx/Rect.h>
40
41#include <Pt/Connectable.h>
42#include <Pt/Signal.h>
43#include <Pt/Types.h>
44
45#include <string>
46#include <vector>
47
48namespace Pt {
49
50namespace Forms {
51
52class Screen;
53class MouseEvent;
54class TouchEvent;
55class ScrollEvent;
56class EnterEvent;
57class LeaveEvent;
58class KeyEvent;
59
103class PT_FORMS_API Widget : public Responder
104 , public Pt::Connectable
105{
106 protected:
110
111 public:
117 virtual ~Widget();
118
122
125 const std::string& name() const;
126
129 void setName(const std::string& n);
130
136
137 public:
140 bool isConnected() const;
141
145
148 const Screen* screen() const;
149
153
156 const Widget* parent() const;
157
160 bool isDescendantOf(const Widget& top) const;
161
164 bool isAncestorOf(const Widget& child) const;
165
171 Widget* hitTest(const Gfx::PointF& pos);
172
175 Gfx::PointF toParent(const Gfx::PointF& pos) const;
176
179 Gfx::PointF fromParent(const Gfx::PointF& pos) const;
180
185 Gfx::PointF toGlobal(const Gfx::PointF& pos) const;
186
191 Gfx::PointF fromGlobal(const Gfx::PointF& pos) const;
192
193 public:
200 void addPeer(Widget& peer);
201
206 void removePeer(Widget& peer);
207
208 public:
215
216 public:
222 virtual void repaint(const Gfx::RectF& rect);
223
226 virtual void repaint();
227
228 public:
231 double scaleFactor() const;
232
235 const Gfx::Scaling& scaling() const;
236
237 public:
240 bool isVisible() const;
241
247 virtual void show(bool b = true);
248
249 public:
252 bool isEnabled() const;
253
259 virtual void enable(bool isEnable = true);
260
261 public:
266 void activate(bool active = true);
267
268 public:
271 const Gfx::PointF& position() const;
272
277 virtual void move(const Gfx::PointF& pos);
278
281 const Gfx::SizeF& size() const;
282
285 const Gfx::RectF& bounds() const;
286
287
292 const Gfx::SizeF& minimumSize() const;
293
299 void setMinimumSize(const Gfx::SizeF& s);
300
303 void setMinimumSize(double w, double h);
304
307 void setMinimumWidth(double w);
308
311 void setMinimumHeight(double h);
312
313
318 const Gfx::SizeF& maximumSize() const;
319
325 void setMaximumSize(const Gfx::SizeF& s);
326
329 void setMaximumSize(double w, double h);
330
333 void setMaximumWidth(double w);
334
337 void setMaximumHeight(double h);
338
339
345 virtual void resize(const Gfx::SizeF& s);
346
347 public:
353 void setCapture(bool capture);
354
355 public:
361 const Cursor* cursor() const;
362
370 void setCursor(const Cursor* c);
371
372 public:
378 void processEvent(const Pt::Event& ev);
379
387
388 protected:
395 virtual void onSetParent(Widget* parent);
396
402 virtual void onConnect(Screen& screen);
403
410 virtual void onDisconnect();
411
417 virtual Widget* onHitTest(const Gfx::PointF& pos);
418
423 virtual Gfx::PointF onToParent(const Gfx::PointF& pos) const = 0;
424
429 virtual Gfx::PointF onFromParent(const Gfx::PointF& pos) const = 0;
430
436 virtual Gfx::PointF onToGlobal(const Gfx::PointF& pos) const;
437
443 virtual Gfx::PointF onFromGlobal(const Gfx::PointF& pos) const;
444
445 protected:
451 virtual void onAttachPeer(Widget& peer);
452
458 virtual void onDetachPeer(Widget& peer);
459
460 protected:
463 virtual void onRequestRepaint(const Gfx::RectF& rect);
464
467 virtual void onRequestShow(bool e);
468
471 virtual void onRequestEnable(bool isEnable);
472
475 virtual void onRequestActivate(bool active);
476
479 virtual void onRequestMove(const Gfx::PointF& pos);
480
486 virtual void onSetSizeLimits(const Gfx::SizeF& minSize,
487 const Gfx::SizeF& maxSize);
488
491 virtual void onRequestResize(const Gfx::SizeF& s);
492
495 virtual void onRequestCapture(bool capture);
496
497 protected:
500 virtual void onProcessEvent(const Pt::Event& ev);
501
502 protected:
505 virtual void onProcessInvalidateEvent(const InvalidateEvent& ev);
506
511 virtual void onInvalidateEvent(const InvalidateEvent& ev);
512
517 virtual void onInvalidate();
518
519 protected:
524 virtual void onProcessPaintEvent(const PaintEvent& ev);
525
528 virtual void onPaintEvent(const PaintEvent& ev);
529
530 protected:
533 virtual void onProcessRescaleEvent(const RescaleEvent& ev);
534
539 virtual void onRescaleEvent(const RescaleEvent& ev);
540
543 virtual void onRescale(double scaling);
544
545 protected:
548 virtual void onProcessShowEvent(const ShowEvent& ev);
549
554 virtual void onShowEvent(const ShowEvent& ev);
555
558 virtual void onShow(bool visible);
559
560 protected:
563 virtual void onProcessEnableEvent(const EnableEvent& ev);
564
570 virtual void onEnableEvent(const EnableEvent& ev);
571
574 virtual void onEnable(bool e);
575
576 protected:
579 virtual void onProcessMoveEvent(const MoveEvent& ev);
580
583 virtual void onMoveEvent(const MoveEvent& ev);
584
587 virtual void onProcessResizeEvent(const ResizeEvent& ev);
588
591 virtual void onResizeEvent(const ResizeEvent& ev);
592
593 protected:
596 virtual void onProcessMouseEvent(const MouseEvent& ev);
597
600 virtual void onProcessTouchEvent(const TouchEvent& ev);
601
604 virtual void onProcessScrollEvent(const ScrollEvent& ev);
605
608 virtual void onProcessEnterEvent(const EnterEvent& ev);
609
612 virtual void onProcessLeaveEvent(const LeaveEvent& ev);
613
616 virtual void onProcessKeyEvent(const KeyEvent& ev);
617
618 //
619 // Responder
620 //
621 protected:
625
631 virtual bool onMouseEvent(const MouseEvent& ev);
632
638 virtual bool onTouchEvent(const TouchEvent& ev);
639
645 virtual bool onScrollEvent( const ScrollEvent& ev);
646
652 virtual bool onEnterEvent( const EnterEvent& ev);
653
659 virtual bool onLeaveEvent(const LeaveEvent& ev);
660
666 virtual bool onKeyEvent(const KeyEvent& ev);
667
668 private:
669 void setR1(void* r)
670 { _r1 = r; }
671
672 private:
674
675 Pt::uint64_t _id;
676 std::string _name;
677
678 Screen* _screen;
679 Widget* _parent;
680 std::vector<Widget*> _peers;
681
682 Responder* _nextResponder;
683
684 int _invalidates;
685
686 Gfx::Scaling _scaling;
687
688 bool _enabledState;
689 bool _isVisible;
690
691 Gfx::PointF _pos;
692 Gfx::SizeF _size;
693 Gfx::RectF _bounds;
694
695 Gfx::SizeF _minimumSize;
696 Gfx::SizeF _maximumSize;
697
698 bool _hasCursor;
699 Forms::Cursor _cursor;
700
701 void* _r1;
702};
703
704} // namespace
705
706} // namespace
707
708#endif // include guard
Connection Management for Signal and Slot Objects.
Definition Connectable.h:50
Base class for typed events.
Definition Event.h:66
Pointer entered a widget.
Definition EnterEvent.h:49
Key press or release.
Definition KeyEvent.h:50
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
Reports a dirty rectangle that a widget must paint.
Definition PaintEvent.h:48
Reports that a widget was resized.
Definition ResizeEvent.h:50
Object that handles input or forwards it.
Definition Responder.h:60
Responder()
Creates a responder.
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
Touch press, move, or release.
Definition TouchEvent.h:51
Common type of every visual Forms object.
Definition Widget.h:105
virtual void onProcessLeaveEvent(const LeaveEvent &ev)
Delivers a leave event through the responder chain.
virtual bool onMouseEvent(const MouseEvent &ev)
Handles a mouse event in local coordinates.
virtual Responder * onNextResponder()
Returns the non-owning next responder, or 0.
const Widget * parent() const
Returns the non-owning parent, or 0 when unparented.
bool isDescendantOf(const Widget &top) const
Returns true when this widget is a strict descendant of top.
Gfx::PointF toParent(const Gfx::PointF &pos) const
Converts local position pos to parent coordinates.
virtual void onProcessShowEvent(const ShowEvent &ev)
Ignores redundant show events and dispatches other ones.
void setCapture(bool capture)
Requests pointer input capture or release.
virtual void onAttachPeer(Widget &peer)
Attaches non-owning peer peer.
virtual void onRequestMove(const Gfx::PointF &pos)
Forwards a request for parent-local position pos.
virtual void onProcessRescaleEvent(const RescaleEvent &ev)
Dispatches a rescale event.
virtual void onDetachPeer(Widget &peer)
Detaches non-owning peer peer.
virtual void onRequestShow(bool e)
Forwards a request to show or hide the widget.
const std::string & name() const
Returns the optional name.
virtual void onMoveEvent(const MoveEvent &ev)
Updates the parent-local position from a move event.
virtual void onDisconnect()
Clears the screen connection.
virtual void onRescaleEvent(const RescaleEvent &ev)
Handles a rescale event.
virtual void enable(bool isEnable=true)
Requests that the widget be enabled or disabled.
Gfx::PointF toGlobal(const Gfx::PointF &pos) const
Converts local position pos to global coordinates.
virtual bool onTouchEvent(const TouchEvent &ev)
Handles a touch event in local coordinates.
void setMinimumWidth(double w)
Sets the local minimum width.
virtual void onEnableEvent(const EnableEvent &ev)
Updates the enabled state and handles an enable event.
void activate(bool active=true)
Requests that the widget become active or inactive.
void setMinimumHeight(double h)
Sets the local minimum height.
Gfx::PointF fromGlobal(const Gfx::PointF &pos) const
Converts global position pos to local coordinates.
void setMaximumHeight(double h)
Sets the local maximum height.
Widget * hitTest(const Gfx::PointF &pos)
Returns the descendant hit at local position pos.
const Gfx::RectF & bounds() const
Returns local bounds, whose origin is always local zero.
Widget()
Creates and registers a widget with the Forms application.
virtual void onRequestEnable(bool isEnable)
Forwards a request to enable or disable the widget.
const Gfx::SizeF & maximumSize() const
Returns the locally stored maximum size.
virtual bool onEnterEvent(const EnterEvent &ev)
Handles pointer entry and installs the widget cursor.
virtual void onResizeEvent(const ResizeEvent &ev)
Updates the local size and bounds from a resize event.
Widget * parent()
Returns the non-owning parent, or 0 when unparented.
virtual void onProcessResizeEvent(const ResizeEvent &ev)
Dispatches a resize event.
void setName(const std::string &n)
Sets the optional name immediately.
Pt::uint64_t id() const
Returns the application-unique ID of this live widget.
virtual void onConnect(Screen &screen)
Records connection to screen.
virtual void onProcessMoveEvent(const MoveEvent &ev)
Dispatches a move event.
void setCursor(const Cursor *c)
Sets a local cursor override.
virtual void onProcessEvent(const Pt::Event &ev)
Sends ev to the widget event dispatcher.
virtual void onShowEvent(const ShowEvent &ev)
Handles a show event.
virtual void onRequestResize(const Gfx::SizeF &s)
Forwards a request for local size s.
const Gfx::PointF & position() const
Returns the position in parent logical coordinates.
virtual void onProcessMouseEvent(const MouseEvent &ev)
Updates pointer tracking and delivers a mouse event.
virtual void resize(const Gfx::SizeF &s)
Requests a new local size s.
virtual void onProcessEnterEvent(const EnterEvent &ev)
Delivers an enter event through the responder chain.
bool isConnected() const
Returns true when the widget is connected to a screen.
double scaleFactor() const
Returns the current logical-to-device scale factor.
void setNextResponder(Responder *r)
Sets the next responder for unhandled input.
void invalidate()
Queues a coalesced invalidation.
virtual bool onLeaveEvent(const LeaveEvent &ev)
Handles pointer leave and restores the default cursor.
bool isVisible() const
Returns whether the last show event made the widget visible.
virtual void onInvalidateEvent(const InvalidateEvent &ev)
Handles the final coalesced invalidation event.
Pt::Signal< const Pt::Event & > & eventReceived()
Returns the event dispatcher signal.
void setMinimumSize(double w, double h)
Sets the local minimum width and height.
const Gfx::Scaling & scaling() const
Returns the current logical-to-device scaling.
const Gfx::SizeF & size() const
Returns the size in local logical coordinates.
void setMaximumWidth(double w)
Sets the local maximum width.
virtual void move(const Gfx::PointF &pos)
Requests a new parent-local position pos.
virtual void onProcessScrollEvent(const ScrollEvent &ev)
Delivers a scroll event through the responder chain.
bool isEnabled() const
Returns whether the last enable event made the widget enabled.
virtual void onRequestActivate(bool active)
Forwards a request to activate or deactivate the widget.
bool isAncestorOf(const Widget &child) const
Returns true when this widget is a strict ancestor of child.
Screen * screen()
Returns the connected screen, or 0 when disconnected.
virtual void onEnable(bool e)
Performs behavior associated with enabled state e.
virtual Gfx::PointF onFromParent(const Gfx::PointF &pos) const =0
Converts parent position pos to local coordinates.
virtual void onSetParent(Widget *parent)
Records the non-owning parent relationship.
virtual void repaint(const Gfx::RectF &rect)
Requests repainting local dirty rectangle rect.
void processEvent(const Pt::Event &ev)
Processes ev through this widget's event dispatcher.
virtual bool onKeyEvent(const KeyEvent &ev)
Handles a key event.
virtual void onProcessInvalidateEvent(const InvalidateEvent &ev)
Coalesces and dispatches an invalidation event.
virtual void onSetSizeLimits(const Gfx::SizeF &minSize, const Gfx::SizeF &maxSize)
Notifies that locally stored size limits changed.
const Cursor * cursor() const
Returns this widget's cursor or the default cursor.
virtual void onRequestCapture(bool capture)
Requests application-managed pointer capture or release.
Gfx::PointF fromParent(const Gfx::PointF &pos) const
Converts parent position pos to local coordinates.
void addPeer(Widget &peer)
Adds a bidirectional, non-owning peer association with peer.
virtual void onRequestRepaint(const Gfx::RectF &rect)
Forwards a repaint request for local rectangle rect.
virtual bool onScrollEvent(const ScrollEvent &ev)
Handles a scroll event.
void setMaximumSize(double w, double h)
Sets the local maximum width and height.
virtual void onPaintEvent(const PaintEvent &ev)
Handles a paint event with a non-empty dirty rectangle.
virtual void onProcessPaintEvent(const PaintEvent &ev)
Filters and dispatches a paint event.
virtual Gfx::PointF onToGlobal(const Gfx::PointF &pos) const
Converts local position pos to global coordinates.
virtual Gfx::PointF onFromGlobal(const Gfx::PointF &pos) const
Converts global position pos to local coordinates.
virtual Gfx::PointF onToParent(const Gfx::PointF &pos) const =0
Converts local position pos to parent coordinates.
virtual void onRescale(double scaling)
Updates the local scaling to scaling.
virtual void onProcessEnableEvent(const EnableEvent &ev)
Dispatches an enable event.
virtual Widget * onHitTest(const Gfx::PointF &pos)
Returns the descendant hit at local position pos.
void setMaximumSize(const Gfx::SizeF &s)
Sets the local maximum size and notifies the size-limit hook.
virtual void onShow(bool visible)
Updates the local visibility state to visible.
void setMinimumSize(const Gfx::SizeF &s)
Sets the local minimum size and notifies the size-limit hook.
virtual void onProcessKeyEvent(const KeyEvent &ev)
Delivers a key event through the responder chain.
virtual void onProcessTouchEvent(const TouchEvent &ev)
Updates pointer tracking and delivers a touch event.
void removePeer(Widget &peer)
Removes the bidirectional peer association with peer.
virtual void show(bool b=true)
Requests that the widget be shown or hidden.
virtual void onInvalidate()
Updates derived state after invalidation.
virtual void repaint()
Requests repainting the complete local bounds.
virtual ~Widget()
Releases pointer capture and peer associations.
const Screen * screen() const
Returns the connected screen, or 0 when disconnected.
const Gfx::SizeF & minimumSize() const
Returns the locally stored minimum size.
Conversion between logical units and pixels.
Definition Scaling.h:52
Multicast Signal to call multiple slots.
Definition Signal.h:190
uint_type uint64_t
Unsigned 64-bit integer type.
Definition Api-Types.h:66
Graphical user interfaces.
Definition ActivateEvent.h:40
Core module.
Definition Allocator.h:33