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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
/*
* Dillo Widget
*
* Copyright 2005-2007 Sebastian Geerken <sgeerken@dillo.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "fltkcore.hh"
#include <fltk/draw.h>
#include <fltk/run.h>
#include <fltk/events.h>
#include <fltk/Monitor.h>
#include <fltk/utf.h>
#include <stdio.h>
namespace dw {
namespace fltk {
using namespace ::fltk;
/**
* \todo Distinction between italics and oblique would be nice.
*/
container::typed::HashTable <dw::core::style::FontAttrs,
FltkFont> *FltkFont::fontsTable =
new container::typed::HashTable <dw::core::style::FontAttrs,
FltkFont> (false, false);
FltkFont::FltkFont (core::style::FontAttrs *attrs)
{
copyAttrs (attrs);
int fa = 0;
if (weight >= 500)
fa |= BOLD;
if (style != core::style::FONT_STYLE_NORMAL)
fa |= ITALIC;
font = ::fltk::font(name, fa);
if (font == NULL) {
/*
* If using xft, fltk::HELVETICA just means sans, fltk::COURIER
* means mono, and fltk::TIMES means serif.
*/
font = HELVETICA->plus (fa);
}
setfont(font, size);
spaceWidth = (int)getwidth(" ");
int xw, xh;
measure("x", xw, xh);
xHeight = xh;
ascent = (int)getascent();
descent = (int)getdescent();
/**
* \bug The code above does not seem to work, so this workaround.
*/
xHeight = ascent * 3 / 5;
}
FltkFont::~FltkFont ()
{
fontsTable->remove (this);
}
FltkFont*
FltkFont::create (core::style::FontAttrs *attrs)
{
FltkFont *font = fontsTable->get (attrs);
if (font == NULL) {
font = new FltkFont (attrs);
fontsTable->put (font, font);
}
return font;
}
container::typed::HashTable <dw::core::style::ColorAttrs,
FltkColor>
*FltkColor::colorsTable =
new container::typed::HashTable <dw::core::style::ColorAttrs,
FltkColor> (false, false);
FltkColor::FltkColor (int color): Color (color)
{
this->color = color;
/*
* fltk/setcolor.cxx:
* "A Color of zero (fltk::NO_COLOR) will draw black but is
* ambiguous. It is returned as an error value or to indicate portions
* of a Style that should be inherited, and it is also used as the
* default label color for everything so that changing color zero can
* be used by the -fg switch. You should use fltk::BLACK (56) to get
* black."
*
* i.e., zero only works sometimes.
*/
if (!(colors[SHADING_NORMAL] = shadeColor (color, SHADING_NORMAL) << 8))
colors[SHADING_NORMAL] = ::fltk::BLACK;
if (!(colors[SHADING_INVERSE] = shadeColor (color, SHADING_INVERSE) << 8))
colors[SHADING_INVERSE] = ::fltk::BLACK;
if (!(colors[SHADING_DARK] = shadeColor (color, SHADING_DARK) << 8))
colors[SHADING_DARK] = ::fltk::BLACK;
if (!(colors[SHADING_LIGHT] = shadeColor (color, SHADING_LIGHT) << 8))
colors[SHADING_LIGHT] = ::fltk::BLACK;
}
FltkColor::~FltkColor ()
{
colorsTable->remove (this);
}
FltkColor * FltkColor::create (int col)
{
ColorAttrs attrs(col);
FltkColor *color = colorsTable->get (&attrs);
if (color == NULL) {
color = new FltkColor (col);
colorsTable->put (color, color);
}
return color;
}
void FltkView::addFltkWidget (::fltk::Widget *widget,
core::Allocation *allocation)
{
}
void FltkView::removeFltkWidget (::fltk::Widget *widget)
{
}
void FltkView::allocateFltkWidget (::fltk::Widget *widget,
core::Allocation *allocation)
{
}
void FltkView::drawFltkWidget (::fltk::Widget *widget, core::Rectangle *area)
{
}
core::ui::LabelButtonResource *
FltkPlatform::FltkResourceFactory::createLabelButtonResource (const char
*label)
{
return new ui::FltkLabelButtonResource (platform, label);
}
core::ui::ComplexButtonResource *
FltkPlatform::FltkResourceFactory::createComplexButtonResource (core::Widget
*widget,
bool relief)
{
return new ui::FltkComplexButtonResource (platform, widget, relief);
}
core::ui::ListResource *
FltkPlatform::FltkResourceFactory::createListResource (core::ui
::ListResource
::SelectionMode
selectionMode, int rows)
{
return new ui::FltkListResource (platform, selectionMode, rows);
}
core::ui::OptionMenuResource *
FltkPlatform::FltkResourceFactory::createOptionMenuResource ()
{
return new ui::FltkOptionMenuResource (platform);
}
core::ui::EntryResource *
FltkPlatform::FltkResourceFactory::createEntryResource (int maxLength,
bool password,
const char *label)
{
return new ui::FltkEntryResource (platform, maxLength, password, label);
}
core::ui::MultiLineTextResource *
FltkPlatform::FltkResourceFactory::createMultiLineTextResource (int cols,
int rows)
{
return new ui::FltkMultiLineTextResource (platform, cols, rows);
}
core::ui::CheckButtonResource *
FltkPlatform::FltkResourceFactory::createCheckButtonResource (bool activated)
{
return new ui::FltkCheckButtonResource (platform, activated);
}
core::ui::RadioButtonResource
*FltkPlatform::FltkResourceFactory::createRadioButtonResource
(core::ui::RadioButtonResource *groupedWith, bool activated)
{
return
new ui::FltkRadioButtonResource (platform,
(ui::FltkRadioButtonResource*)
groupedWith,
activated);
}
// ----------------------------------------------------------------------
FltkPlatform::FltkPlatform ()
{
layout = NULL;
idleQueue = new container::typed::List <IdleFunc> (true);
idleFuncRunning = false;
idleFuncId = 0;
views = new container::typed::List <FltkView> (false);
resources = new container::typed::List <ui::FltkResource> (false);
resourceFactory.setPlatform (this);
}
FltkPlatform::~FltkPlatform ()
{
if (idleFuncRunning)
remove_idle (generalStaticIdle, (void*)this);
delete idleQueue;
delete views;
delete resources;
}
void FltkPlatform::setLayout (core::Layout *layout)
{
this->layout = layout;
}
void FltkPlatform::attachView (core::View *view)
{
views->append ((FltkView*)view);
for (container::typed::Iterator <ui::FltkResource> it =
resources->iterator (); it.hasNext (); ) {
ui::FltkResource *resource = it.getNext ();
resource->attachView ((FltkView*)view);
}
}
void FltkPlatform::detachView (core::View *view)
{
views->removeRef ((FltkView*)view);
for (container::typed::Iterator <ui::FltkResource> it =
resources->iterator (); it.hasNext (); ) {
ui::FltkResource *resource = it.getNext ();
resource->detachView ((FltkView*)view);
}
}
int FltkPlatform::textWidth (core::style::Font *font, const char *text,
int len)
{
FltkFont *ff = (FltkFont*) font;
setfont (ff->font, ff->size);
return (int) getwidth (text, len);
}
int FltkPlatform::nextGlyph (const char *text, int idx)
{
return utf8fwd (&text[idx + 1], text, &text[strlen (text)]) - text;
}
int FltkPlatform::prevGlyph (const char *text, int idx)
{
return utf8back (&text[idx - 1], text, &text[strlen (text)]) - text;
}
float FltkPlatform::dpiX ()
{
return ::fltk::Monitor::all ().dpi_x ();
}
float FltkPlatform::dpiY ()
{
return ::fltk::Monitor::all ().dpi_y ();
}
void FltkPlatform::generalStaticIdle (void *data)
{
((FltkPlatform*)data)->generalIdle();
}
void FltkPlatform::generalIdle ()
{
IdleFunc *idleFunc;
if (!idleQueue->isEmpty ()) {
/* Execute the first function in the list. */
idleFunc = idleQueue->getFirst ();
(layout->*(idleFunc->func)) ();
/* Remove this function. */
idleQueue->removeRef(idleFunc);
}
if (idleQueue->isEmpty()) {
idleFuncRunning = false;
remove_idle (generalStaticIdle, (void*)this);
}
}
/**
* \todo Incomplete comments.
*/
int FltkPlatform::addIdle (void (core::Layout::*func) ())
{
/*
* Since ... (todo) we have to wrap around fltk_add_idle. There is only one
* idle function, the passed idle function is put into a queue.
*/
if (!idleFuncRunning) {
add_idle (generalStaticIdle, (void*)this);
idleFuncRunning = true;
}
idleFuncId++;
IdleFunc *idleFunc = new IdleFunc();
idleFunc->id = idleFuncId;
idleFunc->func = func;
idleQueue->append (idleFunc);
return idleFuncId;
}
void FltkPlatform::removeIdle (int idleId)
{
bool found;
container::typed::Iterator <IdleFunc> it;
IdleFunc *idleFunc;
for (found = false, it = idleQueue->iterator(); !found && it.hasNext(); ) {
idleFunc = it.getNext();
if (idleFunc->id == idleId) {
idleQueue->removeRef (idleFunc);
found = true;
}
}
if (idleFuncRunning && idleQueue->isEmpty())
remove_idle (generalStaticIdle, (void*)this);
}
core::style::Font *FltkPlatform::createFont (core::style::FontAttrs
*attrs,
bool tryEverything)
{
return FltkFont::create (attrs);
}
core::style::Color *FltkPlatform::createColor (int color)
{
return FltkColor::create (color);
}
void FltkPlatform::copySelection(const char *text)
{
fltk::copy(text, strlen(text), false);
}
core::Imgbuf *FltkPlatform::createImgbuf (core::Imgbuf::Type type,
int width, int height)
{
return new FltkImgbuf (type, width, height);
}
core::ui::ResourceFactory *FltkPlatform::getResourceFactory ()
{
return &resourceFactory;
}
void FltkPlatform::attachResource (ui::FltkResource *resource)
{
resources->append (resource);
for (container::typed::Iterator <FltkView> it = views->iterator ();
it.hasNext (); ) {
FltkView *view = it.getNext ();
resource->attachView (view);
}
}
void FltkPlatform::detachResource (ui::FltkResource *resource)
{
resources->removeRef (resource);
}
} // namespace fltk
} // namespace dw
|