Widget.h
1 /* Copyright (C) 2015-2024 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 
48 namespace Pt {
49 
50 namespace Forms {
51 
52 class Screen;
53 class MouseEvent;
54 class TouchEvent;
55 class ScrollEvent;
56 class EnterEvent;
57 class LeaveEvent;
58 class KeyEvent;
59 
60 
71 class PT_FORMS_API Widget : public Responder
72  , public Pt::Connectable
73 {
74  protected:
75  Widget();
76 
77  public:
78  virtual ~Widget();
79 
82  Pt::uint64_t id() const;
83 
86  const std::string& name() const;
87 
90  void setName(const std::string& n);
91 
94  void setNextResponder(Responder* r);
95 
96  public:
99  bool isConnected() const;
100 
104 
107  const Screen* screen() const;
108 
112 
115  const Widget* parent() const;
116 
119  bool isDescendantOf(const Widget& top) const;
120 
123  bool isAncestorOf(const Widget& child) const;
124 
127  Widget* hitTest(const Gfx::PointF& pos);
128 
131  Gfx::PointF toParent(const Gfx::PointF& pos) const;
132 
135  Gfx::PointF fromParent(const Gfx::PointF& pos) const;
136 
139  Gfx::PointF toGlobal(const Gfx::PointF& pos) const;
140 
143  Gfx::PointF fromGlobal(const Gfx::PointF& pos) const;
144 
145  public:
148  void addPeer(Widget& peer);
149 
152  void removePeer(Widget& peer);
153 
154  public:
157  void invalidate();
158 
159  public:
162  virtual void repaint(const Gfx::RectF& rect);
163 
166  virtual void repaint();
167 
168  public:
171  double scaleFactor() const;
172 
173  const Gfx::Scaling& scaling() const;
174 
175  public:
178  bool isVisible() const;
179 
182  virtual void show(bool b = true);
183 
184  public:
187  bool isEnabled() const;
188 
191  virtual void enable(bool isEnable = true);
192 
193  public:
194  void activate(bool active = true);
195 
196  public:
199  const Gfx::PointF& position() const;
200 
203  virtual void move(const Gfx::PointF& pos);
204 
207  const Gfx::SizeF& size() const;
208 
211  const Gfx::RectF& bounds() const;
212 
213 
214  const Gfx::SizeF& minimumSize() const;
215 
216  void setMinimumSize(const Gfx::SizeF& s);
217 
218  void setMinimumSize(double w, double h);
219 
220  void setMinimumWidth(double w);
221 
222  void setMinimumHeight(double h);
223 
224 
225  const Gfx::SizeF& maximumSize() const;
226 
227  void setMaximumSize(const Gfx::SizeF& s);
228 
229  void setMaximumSize(double w, double h);
230 
231  void setMaximumWidth(double w);
232 
233  void setMaximumHeight(double h);
234 
235 
238  virtual void resize(const Gfx::SizeF& s);
239 
240  public:
243  void setCapture(bool capture);
244 
245  public:
246  const Cursor* cursor() const;
247 
248  void setCursor(const Cursor* c);
249 
250  public:
253  void processEvent(const Pt::Event& ev);
254 
258 
259  protected:
260  virtual void onSetParent(Widget* parent);
261 
262  virtual void onConnect(Screen& screen);
263 
264  virtual void onDisconnect();
265 
266  virtual Widget* onHitTest(const Gfx::PointF& pos);
267 
268  virtual Gfx::PointF onToParent(const Gfx::PointF& pos) const = 0;
269 
270  virtual Gfx::PointF onFromParent(const Gfx::PointF& pos) const = 0;
271 
272  virtual Gfx::PointF onToGlobal(const Gfx::PointF& pos) const;
273 
274  virtual Gfx::PointF onFromGlobal(const Gfx::PointF& pos) const;
275 
276  protected:
277  virtual void onAttachPeer(Widget& peer);
278 
279  virtual void onDetachPeer(Widget& peer);
280 
281  protected:
282  // onRepaint
283  virtual void onRequestRepaint(const Gfx::RectF& rect);
284 
285  // onSetVisible, onShow
286  virtual void onRequestShow(bool e);
287 
288  // onSetEnabled, onEnable
289  virtual void onRequestEnable(bool isEnable);
290 
291  virtual void onRequestActivate(bool active);
292 
293  // onSetPosition, onMove
294  virtual void onRequestMove(const Gfx::PointF& pos);
295 
296  virtual void onSetSizeLimits(const Gfx::SizeF& minSize,
297  const Gfx::SizeF& maxSize);
298 
299  // onSetSize, onResize
300  virtual void onRequestResize(const Gfx::SizeF& s);
301 
302  // onSetCapture, onCapture
303  virtual void onRequestCapture(bool capture);
304 
305  protected:
306  virtual void onProcessEvent(const Pt::Event& ev);
307 
308  protected:
309  virtual void onProcessInvalidateEvent(const InvalidateEvent& ev);
310 
311  virtual void onInvalidateEvent(const InvalidateEvent& ev);
312 
313  virtual void onInvalidate();
314 
315  protected:
316  virtual void onProcessPaintEvent(const PaintEvent& ev);
317 
318  virtual void onPaintEvent(const PaintEvent& ev);
319 
320  protected:
321  virtual void onProcessRescaleEvent(const RescaleEvent& ev);
322 
323  virtual void onRescaleEvent(const RescaleEvent& ev);
324 
325  virtual void onRescale(double scaling);
326 
327  protected:
328  virtual void onProcessShowEvent(const ShowEvent& ev);
329 
330  virtual void onShowEvent(const ShowEvent& ev);
331 
332  virtual void onShow(bool visible);
333 
334  protected:
335  virtual void onProcessEnableEvent(const EnableEvent& ev);
336 
337  virtual void onEnableEvent(const EnableEvent& ev);
338 
339  virtual void onEnable(bool e);
340 
341  protected:
342  virtual void onProcessMoveEvent(const MoveEvent& ev);
343 
344  virtual void onMoveEvent(const MoveEvent& ev);
345 
346  virtual void onProcessResizeEvent(const ResizeEvent& ev);
347 
348  virtual void onResizeEvent(const ResizeEvent& ev);
349 
350  protected:
351  virtual void onProcessMouseEvent(const MouseEvent& ev);
352 
353  virtual void onProcessTouchEvent(const TouchEvent& ev);
354 
355  virtual void onProcessScrollEvent(const ScrollEvent& ev);
356 
357  virtual void onProcessEnterEvent(const EnterEvent& ev);
358 
359  virtual void onProcessLeaveEvent(const LeaveEvent& ev);
360 
361  virtual void onProcessKeyEvent(const KeyEvent& ev);
362 
363  //
364  // Responder
365  //
366  protected:
367  virtual Responder* onNextResponder();
368 
369  virtual bool onMouseEvent(const MouseEvent& ev);
370 
371  virtual bool onTouchEvent(const TouchEvent& ev);
372 
373  virtual bool onScrollEvent( const ScrollEvent& ev);
374 
375  virtual bool onEnterEvent( const EnterEvent& ev);
376 
377  virtual bool onLeaveEvent(const LeaveEvent& ev);
378 
379  virtual bool onKeyEvent(const KeyEvent& ev);
380 
381  private:
382  void setR1(void* r)
383  { _r1 = r; }
384 
385  private:
386  Pt::Signal<const Pt::Event&> _dispatcher;
387 
388  Pt::uint64_t _id;
389  std::string _name;
390 
391  Screen* _screen;
392  Widget* _parent;
393  std::vector<Widget*> _peers;
394 
395  Responder* _nextResponder;
396 
397  int _invalidates;
398 
399  Gfx::Scaling _scaling;
400 
401  bool _enabledState;
402  bool _isVisible;
403 
404  Gfx::PointF _pos;
405  Gfx::SizeF _size;
406  Gfx::RectF _bounds;
407 
408  Gfx::SizeF _minimumSize;
409  Gfx::SizeF _maximumSize;
410 
411  bool _hasCursor;
412  Forms::Cursor _cursor;
413 
414  void* _r1;
415 };
416 
417 } // namespace
418 
419 } // namespace
420 
421 #endif // include guard
const Widget * parent() const
Returns the parent.
Core module.
Definition: Allocator.h:33
bool isVisible() const
Indicates whether the widget is visible.
void processEvent(const Pt::Event &ev)
Process event.
bool isAncestorOf(const Widget &child) const
Returns true if an ancestor of @child.
Base class for all event types.
Definition: Event.h:50
void setName(const std::string &n)
Sets the name.
virtual void resize(const Gfx::SizeF &s)
Resizes the widget to a new size.
Multicast Signal to call multiple slots.
Definition: Signal.h:190
bool isDescendantOf(const Widget &top) const
Returns true if an descendant of @top.
virtual void show(bool b=true)
Shows the widget.
void setCapture(bool capture)
Pointer input capture.
void invalidate()
Invalidates the state.
Widget * hitTest(const Gfx::PointF &pos)
Returns the descendant hit at a position.
const Gfx::RectF & bounds() const
Returns the current inner bounds.
virtual void repaint()
Initiates a repaint cycle.
bool isConnected() const
Returns true if connected to a screen.
void addPeer(Widget &peer)
Adds a peer.
Logical-to-physical unit conversion.
Definition: Scaling.h:49
Size with floating-point width and height.
Definition: Size.h:47
Common base for objects that participate in the Forms runtime.
Definition: Widget.h:73
Point with floating-point X and Y coordinates.
Definition: Point.h:47
Pt::Signal< const Pt::Event & > & eventReceived()
Signals that an event needs to be processed.
Gfx::PointF fromParent(const Gfx::PointF &pos) const
Converts from parent coordinate.
bool isEnabled() const
Indicates whether the widget is enabled.
Gfx::PointF fromGlobal(const Gfx::PointF &pos) const
Converts to local coordinate.
Screen * screen()
Returns the connected screen.
const Gfx::SizeF & size() const
Returns the current size.
virtual void enable(bool isEnable=true)
Enables the widget.
Connection Management for Signal and Slot Objects.
Definition: Connectable.h:50
Screen of a display.
Definition: Screen.h:64
virtual void repaint(const Gfx::RectF &rect)
Initiates a repaint cycle.
const std::string & name() const
Returns the name.
void setNextResponder(Responder *r)
Sets the next responder.
void removePeer(Widget &peer)
Removes a peer.
Widget * parent()
Returns the parent.
Pt::uint64_t id() const
Returns the ID.
Rect with floating-point coordinates.
Definition: Rect.h:47
Gfx::PointF toGlobal(const Gfx::PointF &pos) const
Converts to global coordinate.
double scaleFactor() const
Returns the current scale factor.
Gfx::PointF toParent(const Gfx::PointF &pos) const
Converts to parent coordinate.
virtual void move(const Gfx::PointF &pos)
Moves the widget to a position.
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Api-Types.h:62
const Screen * screen() const
Returns the connected screen.
const Gfx::PointF & position() const
Returns the current position.