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 
61 class PT_FORMS_API Widget : public Responder
62  , public Pt::Connectable
63 {
64  protected:
65  Widget();
66 
67  public:
68  virtual ~Widget();
69 
72  Pt::uint64_t id() const;
73 
76  const std::string& name() const;
77 
80  void setName(const std::string& n);
81 
84  void setNextResponder(Responder* r);
85 
86  public:
89  bool isConnected() const;
90 
93  Screen* screen();
94 
97  const Screen* screen() const;
98 
101  Widget* parent();
102 
105  const Widget* parent() const;
106 
109  bool isDescendantOf(const Widget& top) const;
110 
113  bool isAncestorOf(const Widget& child) const;
114 
117  Widget* hitTest(const Gfx::PointF& pos);
118 
121  Gfx::PointF toParent(const Gfx::PointF& pos) const;
122 
125  Gfx::PointF fromParent(const Gfx::PointF& pos) const;
126 
129  Gfx::PointF toGlobal(const Gfx::PointF& pos) const;
130 
133  Gfx::PointF fromGlobal(const Gfx::PointF& pos) const;
134 
135  public:
138  void addPeer(Widget& peer);
139 
142  void removePeer(Widget& peer);
143 
144  public:
147  void invalidate();
148 
149  public:
152  virtual void repaint(const Gfx::RectF& rect);
153 
156  virtual void repaint();
157 
158  public:
161  double scaleFactor() const;
162 
163  const Gfx::Scaling& scaling() const;
164 
165  public:
168  bool isVisible() const;
169 
172  virtual void show(bool b = true);
173 
174  public:
177  bool isEnabled() const;
178 
181  virtual void enable(bool isEnable = true);
182 
183  public:
184  void activate(bool active = true);
185 
186  public:
189  const Gfx::PointF& position() const;
190 
193  virtual void move(const Gfx::PointF& pos);
194 
197  const Gfx::SizeF& size() const;
198 
201  const Gfx::RectF& bounds() const;
202 
203 
204  const Gfx::SizeF& minimumSize() const;
205 
206  void setMinimumSize(const Gfx::SizeF& s);
207 
208  void setMinimumSize(double w, double h);
209 
210  void setMinimumWidth(double w);
211 
212  void setMinimumHeight(double h);
213 
214 
215  const Gfx::SizeF& maximumSize() const;
216 
217  void setMaximumSize(const Gfx::SizeF& s);
218 
219  void setMaximumSize(double w, double h);
220 
221  void setMaximumWidth(double w);
222 
223  void setMaximumHeight(double h);
224 
225 
228  virtual void resize(const Gfx::SizeF& s);
229 
230  public:
233  void setCapture(bool capture);
234 
235  public:
236  const Cursor* cursor() const;
237 
238  void setCursor(const Cursor* c);
239 
240  public:
243  void processEvent(const Pt::Event& ev);
244 
247  Pt::Signal<const Pt::Event&>& eventReceived();
248 
249  protected:
250  virtual void onSetParent(Widget* parent);
251 
252  virtual void onConnect(Screen& screen);
253 
254  virtual void onDisconnect();
255 
256  virtual Widget* onHitTest(const Gfx::PointF& pos);
257 
258  virtual Gfx::PointF onToParent(const Gfx::PointF& pos) const = 0;
259 
260  virtual Gfx::PointF onFromParent(const Gfx::PointF& pos) const = 0;
261 
262  virtual Gfx::PointF onToGlobal(const Gfx::PointF& pos) const;
263 
264  virtual Gfx::PointF onFromGlobal(const Gfx::PointF& pos) const;
265 
266  protected:
267  virtual void onAttachPeer(Widget& peer);
268 
269  virtual void onDetachPeer(Widget& peer);
270 
271  protected:
272  // onRepaint
273  virtual void onRequestRepaint(const Gfx::RectF& rect);
274 
275  // onSetVisible, onShow
276  virtual void onRequestShow(bool e);
277 
278  // onSetEnabled, onEnable
279  virtual void onRequestEnable(bool isEnable);
280 
281  virtual void onRequestActivate(bool active);
282 
283  // onSetPosition, onMove
284  virtual void onRequestMove(const Gfx::PointF& pos);
285 
286  virtual void onSetSizeLimits(const Gfx::SizeF& minSize,
287  const Gfx::SizeF& maxSize);
288 
289  // onSetSize, onResize
290  virtual void onRequestResize(const Gfx::SizeF& s);
291 
292  // onSetCapture, onCapture
293  virtual void onRequestCapture(bool capture);
294 
295  protected:
296  virtual void onProcessEvent(const Pt::Event& ev);
297 
298  protected:
299  virtual void onProcessInvalidateEvent(const InvalidateEvent& ev);
300 
301  virtual void onInvalidateEvent(const InvalidateEvent& ev);
302 
303  virtual void onInvalidate();
304 
305  protected:
306  virtual void onProcessPaintEvent(const PaintEvent& ev);
307 
308  virtual void onPaintEvent(const PaintEvent& ev);
309 
310  protected:
311  virtual void onProcessRescaleEvent(const RescaleEvent& ev);
312 
313  virtual void onRescaleEvent(const RescaleEvent& ev);
314 
315  virtual void onRescale(double scaling);
316 
317  protected:
318  virtual void onProcessShowEvent(const ShowEvent& ev);
319 
320  virtual void onShowEvent(const ShowEvent& ev);
321 
322  virtual void onShow(bool visible);
323 
324  protected:
325  virtual void onProcessEnableEvent(const EnableEvent& ev);
326 
327  virtual void onEnableEvent(const EnableEvent& ev);
328 
329  virtual void onEnable(bool e);
330 
331  protected:
332  virtual void onProcessMoveEvent(const MoveEvent& ev);
333 
334  virtual void onMoveEvent(const MoveEvent& ev);
335 
336  virtual void onProcessResizeEvent(const ResizeEvent& ev);
337 
338  virtual void onResizeEvent(const ResizeEvent& ev);
339 
340  protected:
341  virtual void onProcessMouseEvent(const MouseEvent& ev);
342 
343  virtual void onProcessTouchEvent(const TouchEvent& ev);
344 
345  virtual void onProcessScrollEvent(const ScrollEvent& ev);
346 
347  virtual void onProcessEnterEvent(const EnterEvent& ev);
348 
349  virtual void onProcessLeaveEvent(const LeaveEvent& ev);
350 
351  virtual void onProcessKeyEvent(const KeyEvent& ev);
352 
353  //
354  // Responder
355  //
356  protected:
357  virtual Responder* onNextResponder();
358 
359  virtual bool onMouseEvent(const MouseEvent& ev);
360 
361  virtual bool onTouchEvent(const TouchEvent& ev);
362 
363  virtual bool onScrollEvent( const ScrollEvent& ev);
364 
365  virtual bool onEnterEvent( const EnterEvent& ev);
366 
367  virtual bool onLeaveEvent(const LeaveEvent& ev);
368 
369  virtual bool onKeyEvent(const KeyEvent& ev);
370 
371  private:
372  void setR1(void* r)
373  { _r1 = r; }
374 
375  private:
376  Pt::Signal<const Pt::Event&> _dispatcher;
377 
378  Pt::uint64_t _id;
379  std::string _name;
380 
381  Screen* _screen;
382  Widget* _parent;
383  std::vector<Widget*> _peers;
384 
385  Responder* _nextResponder;
386 
387  int _invalidates;
388 
389  Gfx::Scaling _scaling;
390 
391  bool _enabledState;
392  bool _isVisible;
393 
394  Gfx::PointF _pos;
395  Gfx::SizeF _size;
396  Gfx::RectF _bounds;
397 
398  Gfx::SizeF _minimumSize;
399  Gfx::SizeF _maximumSize;
400 
401  bool _hasCursor;
402  Forms::Cursor _cursor;
403 
404  void* _r1;
405 };
406 
407 } // namespace
408 
409 } // namespace
410 
411 #endif // include guard
Connection Management for Signal and Slot Objects.
Definition: Connectable.h:49
Base class for all event types.
Definition: Event.h:49
Multicast Signal to call multiple slots.
Definition: Signal.h:109
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Types.h:54