Menu

[b84aab]: / src / platform / tizen / display.cpp  Maximize  Restore  History

Download this file

387 lines (338 with data), 9.2 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// This file is part of SmallBASIC
//
// Copyright(C) 2001-2013 Chris Warren-Smith.
//
// This program is distributed under the terms of the GPL v2.0 or later
// Download the GNU Public License (GPL) from www.gnu.org
//
#include "config.h"
#include <time.h>
#include "platform/tizen/display.h"
#include "platform/common/form_ui.h"
#include "platform/common/utils.h"
#define SIZE_LIMIT 4
#define FONT_FACE_NAME "Envy Code R.ttf"
FormViewable *widget;
Drawable *drawTarget;
Font *activeFont;
bool mouseActive;
Color drawColor;
int drawColorRaw;
long long epoch;
using namespace Tizen::App;
using namespace Tizen::Ui::Controls;
//
// Drawable
//
Drawable::Drawable() :
_canvas(NULL),
_clip(NULL) {
}
Drawable::~Drawable() {
delete _canvas;
delete _clip;
}
void Drawable::beginDraw() {
_canvas->SetForegroundColor(drawColor);
if (_clip) {
_canvas->SetClipBounds(*_clip);
}
}
bool Drawable::create(int w, int h) {
Rectangle rect = Rectangle(0, 0, w, h);
_canvas = new Canvas();
return (_canvas && _canvas->Construct(rect) == E_SUCCESS &&
_canvas->FillRectangle(drawColor, rect) == E_SUCCESS);
}
void Drawable::drawImageRegion(Drawable *dst, const MAPoint2d *dstPoint, const MARect *srcRect) {
const Rectangle from = Rectangle(srcRect->left, srcRect->top, srcRect->width, srcRect->height);
const Rectangle to = Rectangle(dstPoint->x, dstPoint->y, srcRect->width, srcRect->height);
dst->beginDraw();
if (dst->_canvas->Copy(to, *_canvas, from) != E_SUCCESS) {
AppLog("Canvas copy error");
}
dst->endDraw();
}
void Drawable::drawLine(int startX, int startY, int endX, int endY) {
beginDraw();
if (_canvas->DrawLine(Point(startX, startY), Point(endX, endY)) != E_SUCCESS) {
AppLog("drawLine error");
}
endDraw();
}
void Drawable::drawPixel(int posX, int posY) {
beginDraw();
_canvas->SetPixel(Point(posX, posY));
endDraw();
}
void Drawable::drawRectFilled(int left, int top, int width, int height) {
beginDraw();
_canvas->FillRectangle(drawColor, Rectangle(left, top, width, height));
endDraw();
}
void Drawable::drawText(int left, int top, const char *str) {
if (activeFont) {
if (_canvas->SetFont(*activeFont) != E_SUCCESS) {
AppLog("Failed to set active font onto canvas");
}
}
beginDraw();
if (_canvas->DrawText(Point(left, top), Tizen::Base::String(str)) != E_SUCCESS) {
AppLog("drawText error");
}
endDraw();
}
void Drawable::endDraw() {
if (_clip) {
_canvas->SetClipBounds(widget->GetBounds());
}
}
int Drawable::getPixel(int x, int y) {
int result = 0;
Color color;
if (_canvas->GetPixel(Point(x, y), color) == E_SUCCESS) {
result = color.GetRGB32();
} else {
AppLog("getPixel error");
}
return result;
}
void Drawable::resize(int w, int h) {
if (_canvas) {
Canvas *old = _canvas;
create(w, h);
_canvas->Copy(_canvas->GetBounds(), *old, old->GetBounds());
delete old;
}
}
void Drawable::setClip(int x, int y, int w, int h) {
delete _clip;
Rectangle rect = _canvas->GetBounds();
if (rect.x != x || rect.y != y || rect.width != w || rect.height != h) {
_clip = new Rectangle(x, y, w, h);
} else {
_clip = NULL;
}
}
//
// FormViewable
//
FormViewable::FormViewable() :
Control(),
_canvasLock(NULL),
_screen(NULL),
_w(0),
_h(0) {
Tizen::System::SystemTime::GetTicks(epoch);
}
FormViewable::~FormViewable() {
logEntered();
delete _screen;
delete _canvasLock;
_screen = NULL;
_canvasLock = NULL;
}
result FormViewable::Construct(String &appRootPath, int w, int h) {
logEntered();
result r = Control::Construct();
if (!IsFailed(r)) {
_canvasLock = new Mutex();
r = _canvasLock != NULL ? _canvasLock->Create() : E_OUT_OF_MEMORY;
}
if (!IsFailed(r)) {
SetBounds(0, 0, w, h);
_w = w; _h = h;
_screen = new Drawable();
if (_screen && _screen->create(w, h)) {
drawTarget = _screen;
drawColorRaw = DEFAULT_BACKGROUND;
drawColor = Color(maSetColor(drawColorRaw));
widget = this;
_fontPath = appRootPath.c_str();
_fontPath += "res/";
_fontPath += FONT_FACE_NAME;
} else {
r = E_OUT_OF_MEMORY;
}
}
if (IsFailed(r)) {
AppLog("FormViewable::Construct failed");
}
return r;
}
Font *FormViewable::createFont(int style, int size) {
logEntered();
Font *font = new Font();
if (!font || font->Construct(_fontPath, style, size) != E_SUCCESS) {
delete font;
font = (Font *)-1;
}
return font;
}
result FormViewable::OnDraw() {
Canvas *canvas = GetCanvasN();
if (canvas) {
canvas->Copy(Point(0, 0), *_screen->_canvas, GetBounds());
}
delete canvas;
return E_SUCCESS;
}
void FormViewable::redraw() {
_canvasLock->Acquire();
OnDraw();
_canvasLock->Release();
}
MAHandle FormViewable::setDrawTarget(MAHandle maHandle) {
if (maHandle == (MAHandle) HANDLE_SCREEN ||
maHandle == (MAHandle) HANDLE_SCREEN_BUFFER) {
_canvasLock->Acquire();
drawTarget = _screen;
} else {
drawTarget = (Drawable *)maHandle;
if (drawTarget == _screen) {
// returning the screen
_canvasLock->Release();
}
}
delete drawTarget->_clip;
drawTarget->_clip = NULL;
return (MAHandle) drawTarget;
}
//
// form_ui implementation
//
void form_ui::optionsBox(StringList *items) {
ArrayList *args = new ArrayList();
args->Construct();
List_each(String *, it, *items) {
char *str = (char *)(* it)->c_str();
args->Add(new Tizen::Base::String(str));
}
Application::GetInstance()->SendUserEvent(MSG_ID_SHOW_MENU, args);
}
//
// maapi implementation
//
int maFontDelete(MAHandle maHandle) {
if (maHandle != -1) {
Font *font = (Font *) maHandle;
if (font == activeFont) {
activeFont = NULL;
}
AppLog("Delete font %x", font);
delete font;
}
return RES_FONT_OK;
}
int maSetColor(int c) {
int r = (c >> 16) & 0xFF;
int g = (c >> 8) & 0xFF;
int b = (c) & 0xFF;
drawColor = Color(r, g, b);
drawColorRaw = c;
return c;
}
void maSetClipRect(int left, int top, int width, int height) {
if (drawTarget) {
drawTarget->setClip(left, top, width, height);
}
}
void maPlot(int posX, int posY) {
if (drawTarget) {
drawTarget->drawPixel(posX, posY);
}
}
void maLine(int startX, int startY, int endX, int endY) {
if (drawTarget) {
drawTarget->drawLine(startX, startY, endX, endY);
}
}
void maFillRect(int left, int top, int width, int height) {
if (drawTarget) {
drawTarget->drawRectFilled(left, top, width, height);
}
}
void maDrawText(int left, int top, const char *str) {
if (drawTarget && str && str[0]) {
drawTarget->drawText(left, top, str);
}
}
void maUpdateScreen(void) {
Application::GetInstance()->SendUserEvent(MSG_ID_REDRAW, NULL);
}
void maResetBacklight(void) {
}
MAExtent maGetTextSize(const char *str) {
MAExtent result;
if (activeFont && str && str[0]) {
Dimension dim;
activeFont->GetTextExtent(str, strlen(str), dim);
result = (MAExtent)((dim.width << 16) + dim.height);
} else {
result = 0;
}
return result;
}
MAExtent maGetScrSize(void) {
short width = widget->getWidth();
short height = widget->getHeight();
return (MAExtent)((width << 16) + height);
}
MAHandle maFontLoadDefault(int type, int style, int size) {
return (MAHandle) widget->createFont(style, size);
}
MAHandle maFontSetCurrent(MAHandle maHandle) {
activeFont = (Font *) maHandle;
return maHandle;
}
void maDrawImageRegion(MAHandle maHandle, const MARect *srcRect,
const MAPoint2d *dstPoint, int transformMode) {
Drawable *drawable = (Drawable *)maHandle;
if (drawTarget != drawable) {
drawable->drawImageRegion(drawTarget, dstPoint, srcRect);
}
}
int maCreateDrawableImage(MAHandle maHandle, int width, int height) {
int result = RES_OK;
if (height > widget->GetHeight() * SIZE_LIMIT) {
result -= 1;
} else {
Drawable *canvas = (Drawable *)maHandle;
canvas->create(width, height);
}
return result;
}
MAHandle maCreatePlaceholder(void) {
MAHandle maHandle = (MAHandle) new Drawable();
return maHandle;
}
void maDestroyPlaceholder(MAHandle maHandle) {
Drawable *holder = (Drawable *)maHandle;
delete holder;
}
void maGetImageData(MAHandle maHandle, void *dst, const MARect *srcRect, int scanlength) {
Drawable *holder = (Drawable *)maHandle;
// maGetImageData is only used for getPixel()
*((int *)dst) = holder->getPixel(srcRect->left, srcRect->top);
}
MAHandle maSetDrawTarget(MAHandle maHandle) {
return widget->setDrawTarget(maHandle);
}
int maGetMilliSecondCount(void) {
long long result, ticks = 0;
Tizen::System::SystemTime::GetTicks(ticks);
result = ticks - epoch;
return result;
}
int maShowVirtualKeyboard(void) {
Application::GetInstance()->SendUserEvent(MSG_ID_SHOW_KEYPAD, NULL);
return 0;
}
void maAlert(const char *title, const char *message, const char *button1,
const char *button2, const char *button3) {
ArrayList *args = new ArrayList();
args->Construct();
args->Add(new Tizen::Base::String(title));
args->Add(new Tizen::Base::String(message));
Application::GetInstance()->SendUserEvent(MSG_ID_SHOW_ALERT, args);
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.