1 /**
2  * File name: RkEvent.h
3  * Project: Redkite (A small GUI toolkit)
4  *
5  * Copyright (C) 2019 Iurie Nistor <http://geontime.com>
6  *
7  * This file is part of Redkite.
8  *
9  * Redkite is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #ifndef RK_EVENT_H
25 #define RK_EVENT_H
26 
27 #include "Rk.h"
28 #include "RkPoint.h"
29 
30 class RkCloseEvent;
31 class RkKeyEvent;
32 class RkMouseEvent;
33 class RkWheelEvent;
34 class RkMoveEvent;
35 class RkResizeEvent;
36 class RkPaintEvent;
37 class RkShowEvent;
38 class RkHideEvent;
39 class RkHoverEvent;
40 class RkWidget;
41 
42 class RK_EXPORT RkEvent {
43  public:
44         enum class Type: int {
45                 NoEvent = 0,
46                 Close = 1,
47                 KeyPressed = 2,
48                 KeyReleased = 3,
49                 MouseMove = 4,
50                 MouseButtonPress = 5,
51                 MouseButtonRelease = 6,
52                 MouseDoubleClick = 7,
53                 Wheel = 8,
54                 Move = 9,
55                 Resize = 10,
56                 Paint = 11,
57                 Show = 12,
58                 Hide = 13,
59                 DeleteChild = 14,
60                 FocusedIn = 15,
61                 FocusedOut = 16,
62                 Hover = 17,
63                 Shortcut = 18,
64                 Drop = 19,
65                 ScaleFactor = 20
66       };
67 
68         explicit RkEvent(Type type = Type::NoEvent)
69               : eventType{type}
70               , eventTime{std::chrono::system_clock::now()} {}
71         virtual ~RkEvent() = default;
72 
setType(Type type)73         void setType(Type type) { eventType = type; }
type()74         Type type() const { return eventType; }
time()75         std::chrono::system_clock::time_point time() const { return eventTime; }
setTime(const std::chrono::system_clock::time_point & time)76         void setTime(const std::chrono::system_clock::time_point &time) {  eventTime = time; }
77 
78   private:
79         Type eventType;
80         std::chrono::system_clock::time_point eventTime;
81 };
82 
83 class RkCloseEvent: public RkEvent {
84  public:
RkCloseEvent()85         RkCloseEvent() : RkEvent(Type::Close) {
86 	}
87 };
88 
89 class RkKeyEvent: public RkEvent {
90    public:
91         RkKeyEvent(Type type = Type::KeyPressed)
RkEvent(type)92                 : RkEvent(type)
93                 , keyValue{Rk::Key::Key_None}
94                 , keyModifiers{0}
95         {
96         }
97 
setKey(Rk::Key key)98         void setKey(Rk::Key key)
99         {
100                 keyValue = key;
101         }
102 
key()103         Rk::Key key() const { return keyValue; }
setModifiers(int mod)104         void setModifiers(int mod) { keyModifiers = mod; }
modifiers()105         int modifiers() const { return keyModifiers; }
106 
107  private:
108         Rk::Key keyValue;
109         int keyModifiers;
110 };
111 
112 class RkMouseEvent: public RkEvent {
113   public:
114 
115         enum class ButtonType : int {
116                 Unknown = 0,
117                 Left = 1,
118                 Middle = 2,
119                 Right = 3,
120                 WheelUp  = 4,
121                 WheelDown = 5
122         };
123 
124       RkMouseEvent(Type type = Type::MouseButtonPress)
RkEvent(type)125               : RkEvent(type)
126               , buttonType{ButtonType::Unknown} {}
127 
x()128       int x() const { return mouseCoordinates.x(); }
setX(int x)129       void setX(int x) { mouseCoordinates.setX(x); }
y()130       int y() const { return mouseCoordinates.y(); }
setY(int y)131       void setY(int y) { mouseCoordinates.setY(y); }
button()132       ButtonType button() const { return buttonType; }
setButton(ButtonType type)133       void setButton(ButtonType type) { buttonType = type; }
134 
135  private:
136         RkPoint mouseCoordinates;
137         ButtonType buttonType;
138 };
139 
140 class RkDropEvent: public RkEvent {
141   public:
142 
RkDropEvent()143       RkDropEvent()
144               : RkEvent(Type::Drop) {}
145 
x()146       int x() const { return position.x(); }
setX(int x)147       void setX(int x) { position.setX(x); }
y()148       int y() const { return position.y(); }
setY(int y)149       void setY(int y) { position.setY(y); }
setFilePath(const std::string & path)150       void setFilePath(const std::string &path) { filePath = path; }
getFilePath()151       std::string getFilePath() const { return filePath; }
152 
153  private:
154       RkPoint position;
155       std::string filePath;
156 };
157 
158 class RkWheelEvent: public RkEvent {
159 };
160 
161 class RkMoveEvent: public RkEvent {
162 };
163 
164 class RkResizeEvent: public RkEvent {
165 public:
RkResizeEvent()166       RkResizeEvent() : RkEvent(Type::Resize) {
167       }
168 };
169 
170 class RkPaintEvent: public RkEvent {
171  public:
RkPaintEvent()172        RkPaintEvent() : RkEvent(Type::Paint) {
173        }
174 };
175 
176 class RkShowEvent: public RkEvent {
177 };
178 
179 class RkHideEvent: public RkEvent {
180 };
181 
182 class RkDeleteChild: public RkEvent {
183  public:
RkDeleteChild(RkWidget * parent,RkWidget * child)184          RkDeleteChild(RkWidget *parent, RkWidget* child)
185                  : RkEvent(Type::DeleteChild)
186                  , parentWidget{parent}
187                  , childWidget{child} {}
child()188         RkWidget* child() const { return childWidget; }
parent()189         RkWidget* parent() const { return parentWidget; }
190  private:
191         RkWidget *parentWidget;
192         RkWidget *childWidget;
193 };
194 
195 class RkFocusEvent: public RkEvent {
196  public:
197       RkFocusEvent(Type type = Type::FocusedIn)
RkEvent(type)198               : RkEvent(type) {};
199 };
200 
201 class RkHoverEvent: public RkEvent {
202  public:
RkHoverEvent()203       RkHoverEvent()
204               : RkEvent(Type::Hover),
205                 hOver{false} {}
setHover(bool b)206       void setHover(bool b) { hOver = b; }
isHover()207       bool isHover() const { return hOver; }
208  private:
209       bool hOver;
210 };
211 
212 class RkScaleFactorEvent: public RkEvent {
213  public:
RkScaleFactorEvent()214       RkScaleFactorEvent()
215               : RkEvent(Type::ScaleFactor),
216                 scaleFactor{1.0} {}
setFactor(double f)217       void setFactor(double f) { scaleFactor = f; }
factor()218       double factor() const { return scaleFactor; }
219  private:
220       double scaleFactor;
221 };
222 
223 #endif // RK_EVENT_H
224 
225