forked from mika314/texteditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.cpp
226 lines (213 loc) · 6.22 KB
/
application.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "to_utf16.hpp"
#include "paint_event.hpp"
#include "resize_event.hpp"
#include "key_event.hpp"
#include "text_input_event.hpp"
#include "application.hpp"
#include "widget.hpp"
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL.h>
#include <stdexcept>
#include <string>
#include <algorithm>
#include <iostream>
Application *Application::instance_ = nullptr;
Application::Application(int &argc, char **argv):
focusWidget_(nullptr),
needUpdateWithoutRedraw_(nullptr),
lastUpdate_(0)
{
if (instance_ != nullptr)
throw std::runtime_error("The program can have only one instance of Application");
instance_ = this;
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
throw std::runtime_error("SDL_Init Error: " + std::string(SDL_GetError()));
if (TTF_Init() != 0)
throw std::runtime_error("TTF_Init error: " + std::string(SDL_GetError()));
}
Application::~Application()
{
SDL_Quit();
instance_ = nullptr;
}
int Application::exec()
{
bool done = false;
while (!done)
{
SDL_Event e;
if (SDL_WaitEvent(&e))
{
switch (e.type)
{
case SDL_WINDOWEVENT:
{
Widget *w = widgetByWindowId(e.window.windowID);
switch (e.window.event)
{
case SDL_WINDOWEVENT_SHOWN:
std::cout << "Window " << e.window.windowID << " shown" << std::endl;
break;
case SDL_WINDOWEVENT_HIDDEN:
std::cout << "Window " << e.window.windowID << " hidden" << std::endl;
break;
case SDL_WINDOWEVENT_EXPOSED:
{
needUpdateWithoutRedraw_ = w;
break;
}
case SDL_WINDOWEVENT_MOVED:
break;
case SDL_WINDOWEVENT_RESIZED:
{
w->resize(e.window.data1, e.window.data2);
#if __APPLE__==1
SDL_RenderPresent(w->renderer_); // hack for MacOS X
#endif
break;
}
case SDL_WINDOWEVENT_MINIMIZED:
std::cout << "Window " << e.window.windowID << " minimized" << std::endl;
break;
case SDL_WINDOWEVENT_MAXIMIZED:
std::cout << "Window " << e.window.windowID << " maximized" << std::endl;
break;
case SDL_WINDOWEVENT_RESTORED:
std::cout << "Window " << e.window.windowID << " restored" << std::endl;
break;
case SDL_WINDOWEVENT_ENTER:
std::cout << "Mouse entered window " << e.window.windowID << std::endl;
break;
case SDL_WINDOWEVENT_LEAVE:
std::cout << "Mouse left window " << e.window.windowID << std::endl;
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
std::cout << "Window " << e.window.windowID << " gained keyboard focus" << std::endl;
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
std::cout << "Window " << e.window.windowID << " lost keyboard focus" << std::endl;
break;
case SDL_WINDOWEVENT_CLOSE:
std::cout << "Window " << e.window.windowID << " closed" << std::endl;
break;
default:
std::cout << "Window " << e.window.windowID << " got unknown event " << static_cast<int>(e.window.event) << std::endl;
break;
}
break;
}
case SDL_KEYDOWN:
{
KeyEvent ke { static_cast<KeyEvent::Key>(e.key.keysym.sym), SDL_GetModState(), static_cast<bool>(e.key.repeat) };
auto w = focusWidget();
if (!w)
w = widgetByWindowId(e.key.windowID);
else if (w->ancestor() != widgetByWindowId(e.key.windowID))
{
std::cerr << "Unknown windowID " << e.key.windowID << std::endl;
break;
}
while (w)
{
if (w->keyPressEvent(ke))
break;
w = w->parent();
}
break;
}
case SDL_KEYUP:
{
KeyEvent ke { static_cast<KeyEvent::Key>(e.key.keysym.sym), SDL_GetModState(), static_cast<bool>(e.key.repeat) };
auto w = focusWidget();
if (!w)
w = widgetByWindowId(e.key.windowID);
else if (w->ancestor() != widgetByWindowId(e.key.windowID))
{
std::cerr << "Unknown windowID " << e.key.windowID << std::endl;
break;
}
while (w)
{
if (w->keyReleaseEvent(ke))
break;
w = w->parent();
}
break;
}
case SDL_TEXTINPUT:
{
TextInputEvent tie { toUtf16(e.text.text) };
auto w = focusWidget();
if (!w)
w = widgetByWindowId(e.key.windowID);
else if (w->ancestor() != widgetByWindowId(e.key.windowID))
{
std::cerr << "Unknown windowID " << e.key.windowID << std::endl;
break;
}
while (w)
{
if (w->textInputEvent(tie))
break;
w = w->parent();
}
break;
}
case SDL_QUIT:
done = true;
break;
}
}
const auto isEmpty = (SDL_PeepEvents(&e, 1, SDL_PEEKEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) == 0);
if (isEmpty || SDL_GetTicks() > lastUpdate_ + 50)
{
for (auto w: widgetList_)
if (w->needRepaint())
{
PaintEvent e;
w->internalPaint(e);
}
if (needUpdateWithoutRedraw_)
{
needUpdateWithoutRedraw_->updateWithoutRedraw();
needUpdateWithoutRedraw_ = nullptr;
}
lastUpdate_ = SDL_GetTicks();
}
for (auto obj: deletingObjects_)
delete obj;
deletingObjects_.clear();
}
return 0;
}
Application *Application::instance()
{
return instance_;
}
void Application::addWidget(Widget *w)
{
widgetList_.push_back(w);
}
void Application::removeWidget(Widget *w)
{
widgetList_.erase(std::remove(begin(widgetList_), end(widgetList_), w), end(widgetList_));
}
Widget *Application::widgetByWindowId(Uint32 id)
{
for (const auto w: widgetList_)
if (id == w->windowId())
return w;
return nullptr;
}
void Application::setFocusWidget(Widget *value)
{
focusWidget_ = value;
}
Widget *Application::focusWidget() const
{
return focusWidget_;
}
void Application::clearFocus()
{
focusWidget_ = nullptr;
}