forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathem-dc.cpp
More file actions
328 lines (255 loc) · 7.87 KB
/
em-dc.cpp
File metadata and controls
328 lines (255 loc) · 7.87 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* -*-c++-*-
Copyright (C) 2003-2013 Runtime Revolution Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "prefix.h"
#include "em-dc.h"
#include "em-view.h"
#include "em-async.h"
#include "em-event.h"
#include "em-util.h"
#include "osspec.h"
#include "eventqueue.h"
#include "redraw.h"
#include "dispatch.h"
/* ================================================================
* Construction/Destruction
* ================================================================ */
MCUIDC *
MCCreateScreenDC()
{
return new MCScreenDC;
}
MC_DLLEXPORT_DEF MCStack *
MCEmscriptenGetCurrentStack()
{
if (MCnoui) return nil;
MCScreenDC *t_dc = static_cast<MCScreenDC *>(MCscreen);
return t_dc->GetCurrentStack();
}
MCScreenDC::MCScreenDC()
: m_main_window(nil)
{
}
MCScreenDC::~MCScreenDC()
{
}
Boolean
MCScreenDC::open()
{
return
MCEmscriptenEventInitialize() &&
MCEmscriptenViewInitialize();
}
Boolean
MCScreenDC::close(Boolean force)
{
MCEmscriptenViewFinalize();
MCEmscriptenEventFinalize();
return true;
}
/* ================================================================
* Window management
* ================================================================ */
void
MCScreenDC::openwindow(Window p_window,
Boolean override)
{
/* FIXME Implement multiple windows */
if (nil != m_main_window)
{
if (m_main_window == p_window) {
return;
}
else
{
MCEmscriptenNotImplemented();
}
}
m_main_window = p_window;
MCStack *t_stack = MCdispatcher->findstackd(p_window);
/* Enable drawing */
t_stack->view_activatetilecache();
t_stack->setextendedstate(false, ECS_DONTDRAW);
/* Set mouse & keyboard focus */
UpdateFocus();
/* Set up view to match window, as far as possible */
/* FIXME Implement HiDPI support */
MCEmscriptenViewSetBounds(t_stack->view_getrect());
t_stack->view_configure(true);
t_stack->view_dirty_all();
}
void
MCScreenDC::closewindow(Window p_window)
{
/* FIXME Implement multiple windows */
MCAssert(p_window);
if (p_window != m_main_window)
{
return;
}
m_main_window = nil;
}
void
MCScreenDC::destroywindow(Window & x_window)
{
x_window = nil;
}
bool
MCScreenDC::platform_getwindowgeometry(Window p_window,
MCRectangle & r_rect)
{
/* FIXME Implement HiDPI support */
r_rect = MCEmscriptenViewGetBounds();
return true;
}
void
MCScreenDC::UpdateFocus()
{
MCAssert(nil != m_main_window);
MCStack *t_stack = GetCurrentStack();
MCEventQueuePostMouseFocus(t_stack, 0, true);
MCEventQueuePostKeyFocus(t_stack, true);
}
MCStack *
MCScreenDC::GetCurrentStack()
{
return MCdispatcher->findstackd(m_main_window);
}
/* ================================================================
* Event loop
* ================================================================ */
/* Returns true if quit is requested. */
Boolean
MCScreenDC::wait(real64_t p_duration,
Boolean p_allow_dispatch,
Boolean p_accept_any_event)
{
p_duration = MCMax(p_duration, 0.0);
/* We allow p_duration to be infinite, but only if
* p_accept_any_event is set. */
MCAssert(isfinite(p_duration) || p_accept_any_event);
MCDeletedObjectsEnterWait(p_allow_dispatch);
++MCwaitdepth;
real64_t t_current_time = MCS_time();
real64_t t_exit_time = t_current_time + p_duration;
real64_t t_event_time = t_exit_time;
bool t_first = true;
bool t_done = false;
while (!t_done && !MCquit)
{
/* If more than one iteration through the wait loop is needed
* (i.e. doing the work that was already queued didn't take
* long enough), wait for a timeout. */
if (!t_first)
{
/* Update estimate of time remaining */
t_current_time = MCS_time();
real64_t t_sleep_time = MCMin(t_event_time - t_current_time,
t_exit_time - t_current_time);
/* Check if the requested wait duration has already
* elapsed */
if (!t_done)
{
t_done = (t_sleep_time <= 0);
}
/* There's still some waiting required, so give up running the
* engine for now and wait for an event or timeout from the
* browser */
if (!t_done &&
MCEmscriptenAsyncYield(t_sleep_time))
{
t_done = p_accept_any_event;
}
/* Update the times based on the result of the yield */
t_current_time = MCS_time();
}
if (!t_done)
{
DoRunloopActions();
}
/* Dispatch pending engine messages */
t_event_time = t_exit_time;
if (!t_done &&
handlepending(t_current_time, t_event_time, p_allow_dispatch))
{
t_done = p_accept_any_event;
}
/* Dispatch queued incoming system events */
if (!t_done &&
p_allow_dispatch && MCEventQueueDispatch())
{
t_done = p_accept_any_event;
}
/* Redraw the screen */
MCRedrawUpdateScreen();
t_first = false;
}
--MCwaitdepth;
MCDeletedObjectsLeaveWait(p_allow_dispatch);
return MCquit;
}
/* ================================================================
* Ask/answer
* ================================================================ */
// These functions are implemented in javascript
extern "C" int32_t MCEmscriptenDialogShowAlert(const unichar_t* p_message, size_t p_message_length);
extern "C" int32_t MCEmscriptenDialogShowConfirm(const unichar_t* p_message, size_t p_message_length);
extern "C" int32_t MCEmscriptenDialogShowPrompt(const unichar_t* p_message, size_t p_message_length, const unichar_t* p_default, size_t p_default_length, unichar_t** r_result, size_t* r_result_length);
int32_t
MCScreenDC::popupanswerdialog(MCStringRef *p_buttons, uint32_t p_button_count, uint32_t p_type, MCStringRef p_title, MCStringRef p_message)
{
// Default to returning an unsuccessful result
int32_t t_result = -1;
// We need to have a UTF-16 string pointer for the message string. The other
// parameters are not supported by the JavaScript built-in dialogues.
MCAutoStringRefAsUTF16String t_message_u16;
t_message_u16.Lock(p_message);
switch (p_button_count)
{
case 1:
// Only one button - treat it as an "OK" button
t_result = MCEmscriptenDialogShowAlert(t_message_u16.Ptr(), t_message_u16.Size());
break;
case 2:
// Two buttons - treat it as an "OK"/"Cancel" button pair
t_result = MCEmscriptenDialogShowConfirm(t_message_u16.Ptr(), t_message_u16.Size());
break;
default:
// Not supported
break;
}
return t_result;
}
bool
MCScreenDC::popupaskdialog(uint32_t p_type, MCStringRef p_title, MCStringRef p_message, MCStringRef p_initial, bool p_hint, MCStringRef& r_result)
{
MCAutoStringRefAsUTF16String t_message_u16;
MCAutoStringRefAsUTF16String t_default_u16;
t_message_u16.Lock(p_message);
t_default_u16.Lock(p_initial);
unichar_t* t_result;
size_t t_result_length;
if (!MCEmscriptenDialogShowPrompt(t_message_u16.Ptr(), t_message_u16.Size(), t_default_u16.Ptr(), t_default_u16.Size(), &t_result, &t_result_length))
{
return false;
}
MCStringCreateWithBytesAndRelease((byte_t*)t_result, t_result_length, kMCStringEncodingUTF16, false, r_result);
return true;
}
void
MCScreenDC::platform_querymouse(int16_t& r_x, int16_t& r_y)
{
// There is no asynchronous mouse position in Emscripten; just whatever the
// browser has told us about.
r_x = MCmousex;
r_y = MCmousey;
}