/
app_window.cpp
217 lines (178 loc) · 4.59 KB
/
app_window.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
#include <cstdint>
extern "C" {
#include <aes.h>
#include <aes_window.h>
#include <tos.h>
#include <libc.h>
#include <string.h>
}
#include "app.h"
#include "app_window.h"
Window::Window() {
isopen = false;
maximised = false;
style = -1;
handle = -1;
title = "";
}
Window::~Window() {
}
void Window::update() {
// Calculate work area from window dimensions
wind_calc(WC_WORK, WIND_BASIC,
dimensions[0], dimensions[1], dimensions[2], dimensions[3],
&visible[0], &visible[1], &visible[2], &visible[3]);
}
void Window::size(int16_t x, int16_t y, int16_t w, int16_t h) {
dimensions[0] = x;
dimensions[1] = y;
dimensions[2] = w;
dimensions[3] = h;
update();
if (isopen) {
wind_set(handle, WF_CURRXYWH, x, y, w, h);
event_resized();
}
}
static inline int16_t min(int16_t a, int16_t b)
{
return a < b ? a : b;
}
static inline int16_t max(int16_t a, int16_t b)
{
return a > b ? a : b;
}
bool intersect_xywh(int16_t *dst, int16_t *src)
{
int16_t x1, y1, x2, y2;
x1 = max(dst[0], src[0]);
x2 = min(dst[0] + dst[2], src[0] + src[2]);
y1 = max(dst[1], src[1]);
y2 = min(dst[1] + dst[3], src[1] + src[3]);
if (x2 > x1 && y2 > y1) {
dst[0] = x1;
dst[1] = y1;
dst[2] = max(0, x2 - x1);
dst[3] = max(0, y2 - y1);
return true;
} else {
return false;
}
}
// Redraw part of the window contents
void Window::redraw(int16_t vhandle, int16_t x, int16_t y, int16_t w, int16_t h)
{
// Disable updates
graf_mouse(M_OFF, nullptr);
wind_update(BEG_UPDATE);
// set clipping rectangle to updated area
int16_t cliprect[4];
cliprect[0] = x;
cliprect[1] = y;
cliprect[2] = w;
cliprect[3] = h;
// Get first rectangle
int16_t r[4];
wind_get(handle, WF_FIRSTXYWH, &r[0], &r[1], &r[2], &r[3]);
// If rectangle has height or width
while (r[2] || r[3]) {
// Intersect with repaintable area
if (intersect_xywh(r, cliprect)) {
// set clipping rectangle to current rectangle
int16_t temp[4];
temp[0] = r[0];
temp[1] = r[1];
temp[2] = r[0] + r[2] - 1;
temp[3] = r[1] + r[3] - 1;
vs_clip(vhandle, 1, temp);
draw(vhandle, temp);
}
// Get next rectangle
wind_get(handle, WF_NEXTXYWH, &r[0], &r[1], &r[2], &r[3]);
}
// Enable updates
wind_update(END_UPDATE);
graf_mouse(M_ON, nullptr);
}
// Force a redraw by sending a WM_REDRAW message to the application
void Window::redraw_window() {
int16_t msg[8] = {
WM_REDRAW, app->app_id, 0, handle,
dimensions[0], dimensions[1], dimensions[2], dimensions[3]
};
appl_write(app->app_id, 16, msg);
}
// Topped as in brought to top (focused)
void Window::event_topped()
{
wind_set(handle, WF_TOP, 0, 0, 0, 0);
}
void Window::event_fulled()
{
if (!maximised) {
// Save current dimensions
memcpy(nm_dimensions, dimensions, sizeof(int16_t) * 4);
// Grow to size of window 0 (desktop)
RectXYWH rect;
app->get_screen_size(&rect);
size(rect.x, rect.y, rect.w, rect.h);
maximised = true;
} else {
// Return to non-maximised dimensions
size(nm_dimensions[0], nm_dimensions[1], nm_dimensions[2], nm_dimensions[3]);
maximised = false;
}
}
void Window::settitle(const char *t) {
title = t;
if (isopen) {
wind_set(handle, WF_NAME, (int32_t)t >> 16, (int32_t)t & 0xFFFF, 0, 0);
}
}
void Window::open() {
if (isopen)
return;
if (style == -1)
style = WIND_BASIC;
// Connect the window to AES
handle = wind_create(style, 0, 0, 640, 400);
wind_set(handle, WF_NAME, (int32_t)title >> 16,
(int32_t)title & 0xFFFF, 0, 0);
// Update the work area details
update();
// Open the window
wind_open(handle, dimensions[0], dimensions[1],
dimensions[2], dimensions[3]);
isopen = true;
}
void Window::close() {
if (isopen) {
wind_close(handle);
wind_delete(handle);
isopen = false;
}
}
void Window::event_closed() {
}
void Window::event_resized() {
}
// Draw client area
void Window::draw(int16_t vhandle, int16_t rect[])
{
// Set draw mode=REPLACE, fill style=solid
vswr_mode(vhandle, 1);
vsf_interior(vhandle, 1);
vs_clip(vhandle, 1, rect);
// Paint white
vsf_color(vhandle, 0);
//vr_recfl(vhandle, temp);
v_bar(vhandle, rect);
}
void Window::set_app(Application *a)
{
app = a;
}
void Window::setstyle(int16_t s)
{
style = s;
}