guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gcursor.hpp
1 //
2 // Mouse Cursor
3 // Guit GUI Toolkit
4 // Copyright © 2019/2020 Eric Lecolinet. All rights reserved.
5 // http://www.telecom-paris.fr/~elc
6 //
7 
8 #ifndef Guit_Cursor_hpp
9 #define Guit_Cursor_hpp
10 #include <gprop.hpp>
11 #include <gproplist.hpp>
12 namespace guit {
13 
14 class GPos;
15 
17 class GCursor : public GProp {
18 public:
19  using Shape = uint32_t;
20 
21  static const Shape
22  Arrow,
23  Crosshair,
24  IBeam,
25  Wait,
26  WaitArrow,
27  Resize_W_E,
28  Resize_N_S,
29  Resize_NE_SW,
30  Resize_NW_SE,
31  ResizeAll,
32  NoAction,
33  Hand;
34 
35  //static GPropType_<GCursor>& Type();
36  //GPropType& type() const override {return Type();}
37 
38  GCursor* clone(bool) const override {return nullptr;}
39 
41  static GDim screenSize();
42 
45  static GCursor* cursor(unsigned int channel = 0);
46 
53  static GCursor& obtainCursor(unsigned int channel = 0);
54 
57  static int cursorCount();
58 
60  unsigned int channel() const {return id_;}
61 
64  void move(GPoint const& pos);
65  void move(float x, float y) {move(GPoint(x,y));}
67 
70  GPoint const& pos() const;
71  float x() const {return pos().x;}
72  float y() const {return pos().y;}
74 
78  GPoint wpos() const;
79  float wx() const {return wpos().x;}
80  float wy() const {return wpos().y;}
82 
84  GCursor& operator<<(GFunction const& fun);
85 
87  friend GPos& operator<<=(GPos& pos, GCursor&);
88 
90  void show(bool state);
91 
94  void freeze(bool state);
95 
97  void setColor(GColor&);
98 
100  GColor& color() const {return *color_;}
101 
104  void setShape(Shape);
105 
107  Shape shape() const {return shape_;}
108 
117  void displayAsNative(bool);
118 
121  void startDrag(Gadget& from,
122  std::function<void(GCursor&)> const& onDrop,
123  Shape = Hand);
124 
126  bool isDragging() const;
127 
129  Gadget* source();
130 
133 
135  Gadget* target() {return target_;}
136 
138  GWindow* targetWindow() {return targetwin_;}
139 
141  GPoint const& targetPos() {return targetpos_;}
142 
143 protected:
144  friend class GChannel;
145  friend class GControlImpl;
146 
147  GCursor(unsigned int channel_id);
148  void channelDown(GMouseEvent&);
149  void channelUp(GMouseEvent&);
150  void channelMove(GMouseEvent&);
151  static void drawCursors(GRenderGraphics&);
152  void reset();
153  void endDrag(GPoint const& wpos);
154  void moveDrag(GPoint const& wpos);
155 
156 public:
157  unsigned int id_{};
158  bool shown_{}, pressed_{}, dragging_{}, nativecursor_{};
159  int32_t derive_{}, maxderive_{}, derivecount_{}; // NOT signed!
160  Shape shape_{Arrow};
161  gprotect<GColor> color_;
162  gprotect<GWindow> window_;
163  GPropList props_;
164  mutable GPoint pos_;
165  GPoint targetpos_;
166  GWindow *fromwin_{}, *targetwin_{};
167  Gadget *fromg_{}, *target_{};
168  std::function<void(GCursor&)> onend_;
169  std::function<bool(GMouseEvent&)> buttonhandler_{};
170  std::function<bool(GMouseEvent&)> movehandler_{};
171  std::function<bool(GMouseEvent&)> wheelhandler_{};
172  std::function<bool(GKeyEvent&)> keyhandler_{};
173 };
174 
175 }
176 #endif