Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 andreas 1
/*
268 andreas 2
 * Copyright (C) 2020 to 2023 by Andreas Theofilu <andreas@theosys.at>
2 andreas 3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
3 andreas 18
 
19
#include <string>
20
#include <memory>
40 andreas 21
#include <algorithm>
67 andreas 22
#include <codecvt>
169 andreas 23
#include <fstream>
6 andreas 24
 
25
#include <include/core/SkPixmap.h>
3 andreas 26
#include <include/core/SkSize.h>
4 andreas 27
#include <include/core/SkColor.h>
7 andreas 28
#include <include/core/SkFont.h>
29
#include <include/core/SkTypeface.h>
8 andreas 30
#include <include/core/SkFontMetrics.h>
19 andreas 31
#include <include/core/SkTextBlob.h>
8 andreas 32
#include <include/core/SkRegion.h>
33
#include <include/core/SkImageFilter.h>
34
#include <include/effects/SkImageFilters.h>
35
#include <include/core/SkPath.h>
10 andreas 36
#include <include/core/SkSurfaceProps.h>
365 andreas 37
//#ifndef __MACH__
38
//#include <include/core/SkFilterQuality.h>
39
//#endif
66 andreas 40
#include <include/core/SkMaskFilter.h>
365 andreas 41
//#include <include/core/SkImageEncoder.h>
179 andreas 42
#include <include/core/SkRRect.h>
365 andreas 43
#include <include/core/SkBlurTypes.h>
258 andreas 44
 
182 andreas 45
//#ifdef __ANDROID__
46
//#include <QtAndroidExtras/QAndroidJniObject>
47
//#include <QtAndroid>
48
//#endif
23 andreas 49
 
2 andreas 50
#include "tbutton.h"
94 andreas 51
#include "thttpclient.h"
3 andreas 52
#include "terror.h"
53
#include "tconfig.h"
4 andreas 54
#include "tresources.h"
8 andreas 55
#include "ticons.h"
14 andreas 56
#include "tamxnet.h"
57
#include "tpagemanager.h"
71 andreas 58
#include "tsystemsound.h"
97 andreas 59
#include "timgcache.h"
225 andreas 60
#include "turl.h"
291 andreas 61
#include "tlock.h"
332 andreas 62
#if TESTMODE == 1
63
#include "testmode.h"
64
#endif
2 andreas 65
 
15 andreas 66
using std::exception;
3 andreas 67
using std::string;
68
using std::vector;
69
using std::unique_ptr;
70
using std::map;
71
using std::pair;
15 andreas 72
using std::thread;
73
using std::atomic;
74
using std::mutex;
21 andreas 75
using std::bind;
2 andreas 76
using namespace Button;
76 andreas 77
using namespace Expat;
2 andreas 78
 
206 andreas 79
#define MAX_BUFFER          65536
21 andreas 80
 
192 andreas 81
#define RLOG_INFO           0x00fe
82
#define RLOG_WARNING        0x00fd
83
#define RLOG_ERROR          0x00fb
84
#define RLOG_TRACE          0x00f7
85
#define RLOG_DEBUG          0x00ef
86
#define RLOG_PROTOCOL       0x00f8
87
#define RLOG_ALL            0x00e0
88
 
8 andreas 89
extern TIcons *gIcons;
14 andreas 90
extern amx::TAmxNet *gAmxNet;
26 andreas 91
extern TPageManager *gPageManager;
8 andreas 92
 
21 andreas 93
THR_REFRESH_t *TButton::mThrRefresh = nullptr;
94 andreas 94
vector<BITMAP_CACHE> nBitmapCache;     // Holds the images who are delayed because they are external
21 andreas 95
 
110 andreas 96
SYSTEF_t sysTefs[] = {
97
    {  1, "Outline-S" },
98
    {  2, "Outline-M" },
99
    {  3, "Outline-L" },
100
    {  4, "Outline-X" },
101
    {  5, "Glow-S" },
102
    {  6, "Glow-M" },
103
    {  7, "Glow-L" },
104
    {  8, "Glow-X" },
105
    {  9, "Soft Drop Shadow 1" },
106
    { 10, "Soft Drop Shadow 2" },
107
    { 11, "Soft Drop Shadow 3" },
108
    { 12, "Soft Drop Shadow 4" },
109
    { 13, "Soft Drop Shadow 5" },
110
    { 14, "Soft Drop Shadow 6" },
111
    { 15, "Soft Drop Shadow 7" },
112
    { 16, "Soft Drop Shadow 8" },
113
    { 17, "Medium Drop Shadow 1" },
114
    { 18, "Medium Drop Shadow 2" },
115
    { 19, "Medium Drop Shadow 3" },
116
    { 20, "Medium Drop Shadow 4" },
117
    { 21, "Medium Drop Shadow 5" },
118
    { 22, "Medium Drop Shadow 6" },
119
    { 23, "Medium Drop Shadow 7" },
120
    { 24, "Medium Drop Shadow 8" },
121
    { 25, "Hard Drop Shadow 1" },
122
    { 26, "Hard Drop Shadow 2" },
123
    { 27, "Hard Drop Shadow 3" },
124
    { 28, "Hard Drop Shadow 4" },
125
    { 29, "Hard Drop Shadow 5" },
126
    { 30, "Hard Drop Shadow 6" },
127
    { 31, "Hard Drop Shadow 7" },
128
    { 32, "Hard Drop Shadow 8" },
129
    { 33, "Soft Drop Shadow 1 with outline" },
130
    { 34, "Soft Drop Shadow 2 with outline" },
131
    { 35, "Soft Drop Shadow 3 with outline" },
132
    { 36, "Soft Drop Shadow 4 with outline" },
133
    { 37, "Soft Drop Shadow 5 with outline" },
134
    { 38, "Soft Drop Shadow 6 with outline" },
135
    { 39, "Soft Drop Shadow 7 with outline" },
136
    { 40, "Soft Drop Shadow 8 with outline" },
137
    { 41, "Medium Drop Shadow 1 with outline" },
138
    { 42, "Medium Drop Shadow 2 with outline" },
139
    { 43, "Medium Drop Shadow 3 with outline" },
140
    { 44, "Medium Drop Shadow 4 with outline" },
141
    { 45, "Medium Drop Shadow 5 with outline" },
142
    { 46, "Medium Drop Shadow 6 with outline" },
143
    { 47, "Medium Drop Shadow 7 with outline" },
144
    { 48, "Medium Drop Shadow 8 with outline" },
145
    { 49, "Hard Drop Shadow 1 with outline" },
146
    { 50, "Hard Drop Shadow 2 with outline" },
147
    { 51, "Hard Drop Shadow 3 with outline" },
148
    { 52, "Hard Drop Shadow 4 with outline" },
149
    { 53, "Hard Drop Shadow 5 with outline" },
150
    { 54, "Hard Drop Shadow 6 with outline" },
151
    { 55, "Hard Drop Shadow 7 with outline" },
152
    { 56, "Hard Drop Shadow 8 with outline" },
153
    { 0,  "\0" }
154
};
155
 
3 andreas 156
TButton::TButton()
2 andreas 157
{
3 andreas 158
    DECL_TRACER("TButton::TButton()");
16 andreas 159
 
21 andreas 160
    mAniRunning = false;
16 andreas 161
    mLastBlink.clear();
2 andreas 162
}
163
 
3 andreas 164
TButton::~TButton()
2 andreas 165
{
3 andreas 166
    DECL_TRACER("TButton::~TButton()");
167
 
15 andreas 168
    if (ap == 0 && ad == 8)
169
    {
170
        if (gAmxNet)
171
            gAmxNet->deregNetworkState(mHandle);
172
    }
173
 
174
    if (ap == 0 && ((ad >= 141 && ad <= 143) || (ad >= 151 && ad <= 158)))
175
    {
176
        if (gAmxNet)
177
            gAmxNet->deregTimer(mHandle);
178
    }
179
 
36 andreas 180
    if (ap == 0 && ad == 81)    // Network state multi bargraph
181
    {
182
        if (gPageManager)
183
            gPageManager->unregCallbackNetState(mHandle);
184
    }
185
 
15 andreas 186
    if (mTimer)
187
    {
188
        mTimer->stop();
189
 
190
        while (mTimer->isRunning())
191
            usleep(50);
192
 
193
        delete mTimer;
194
    }
21 andreas 195
 
93 andreas 196
    if (mAniRunning)
197
    {
198
        ulong tm = nu * ru + nd * rd;
199
        mAniStop = true;
200
 
201
        while (mAniRunning)
202
            std::this_thread::sleep_for(std::chrono::milliseconds(tm * 100));
203
    }
204
 
21 andreas 205
    THR_REFRESH_t *next, *p = mThrRefresh;
206
 
207
    while (p)
208
    {
209
        if (p->mImageRefresh)
210
        {
211
            p->mImageRefresh->stop();
34 andreas 212
            int counter = 0;
21 andreas 213
 
34 andreas 214
            while (counter < 1000 && p->mImageRefresh->isRunning())
215
            {
21 andreas 216
                usleep(50);
34 andreas 217
                counter++;
218
            }
21 andreas 219
 
220
            delete p->mImageRefresh;
221
            p->mImageRefresh = nullptr;
222
        }
223
 
224
        next = p->next;
225
        delete p;
226
        p = next;
227
    }
35 andreas 228
 
229
    mThrRefresh = nullptr;
2 andreas 230
}
3 andreas 231
 
76 andreas 232
size_t TButton::initialize(TExpat *xml, size_t index)
3 andreas 233
{
78 andreas 234
    DECL_TRACER("TButton::initialize(TExpat *xml, size_t index)");
3 andreas 235
 
76 andreas 236
    if (!xml || index == TExpat::npos)
3 andreas 237
    {
238
        MSG_ERROR("Invalid NULL parameter passed!");
239
        TError::setError();
76 andreas 240
        return TExpat::npos;
3 andreas 241
    }
242
 
177 andreas 243
    mChanged = true;
76 andreas 244
    vector<ATTRIBUTE_t> attrs = xml->getAttributes(index);
245
    string stype = xml->getAttribute("type", attrs);
3 andreas 246
    type = getButtonType(stype);
247
    MSG_DEBUG("Button type: " << stype << " --> " << type);
76 andreas 248
    string ename, content;
249
    size_t oldIndex = index;
3 andreas 250
 
76 andreas 251
    while((index = xml->getNextElementFromIndex(index, &ename, &content, &attrs)) != TExpat::npos)
3 andreas 252
    {
253
        if (ename.compare("bi") == 0)
162 andreas 254
        {
76 andreas 255
            bi = xml->convertElementToInt(content);
162 andreas 256
            MSG_DEBUG("Processing button index: " << bi);
257
        }
193 andreas 258
        else if (ename.compare("na") == 0)          // Name
76 andreas 259
            na = content;
193 andreas 260
        else if (ename.compare("bd") == 0)          // Description
76 andreas 261
            bd = content;
193 andreas 262
        else if (ename.compare("lt") == 0)          // Left
76 andreas 263
            lt = xml->convertElementToInt(content);
193 andreas 264
        else if (ename.compare("tp") == 0)          // Top
76 andreas 265
            tp = xml->convertElementToInt(content);
193 andreas 266
        else if (ename.compare("wt") == 0)          // Width
76 andreas 267
            wt = xml->convertElementToInt(content);
193 andreas 268
        else if (ename.compare("ht") == 0)          // Height
76 andreas 269
            ht = xml->convertElementToInt(content);
193 andreas 270
        else if (ename.compare("zo") == 0)          // Z-Order
76 andreas 271
            zo = xml->convertElementToInt(content);
193 andreas 272
        else if (ename.compare("hs") == 0)          // bounding
76 andreas 273
            hs = content;
193 andreas 274
        else if (ename.compare("bs") == 0)          // border style
76 andreas 275
            bs = content;
193 andreas 276
        else if (ename.compare("fb") == 0)          // feedback type
76 andreas 277
            fb = getButtonFeedback(content);
193 andreas 278
        else if (ename.compare("ap") == 0)          // address port
76 andreas 279
            ap = xml->convertElementToInt(content);
193 andreas 280
        else if (ename.compare("ad") == 0)          // address code
76 andreas 281
            ad = xml->convertElementToInt(content);
193 andreas 282
        else if (ename.compare("ch") == 0)          // channel code
76 andreas 283
            ch = xml->convertElementToInt(content);
193 andreas 284
        else if (ename.compare("cp") == 0)          // channel port
76 andreas 285
            cp = xml->convertElementToInt(content);
193 andreas 286
        else if (ename.compare("lp") == 0)          // level port
76 andreas 287
            lp = xml->convertElementToInt(content);
193 andreas 288
        else if (ename.compare("lv") == 0)          // level code
76 andreas 289
            lv = xml->convertElementToInt(content);
193 andreas 290
        else if (ename.compare("dr") == 0)          // level direction
76 andreas 291
            dr = content;
193 andreas 292
        else if (ename.compare("co") == 0)          // command port
147 andreas 293
            co = xml->convertElementToInt(content);
193 andreas 294
        else if (ename.compare("cm") == 0)          // commands to send on button hit
149 andreas 295
            cm.push_back(content);
193 andreas 296
        else if (ename.compare("va") == 0)          // ?
76 andreas 297
            va = xml->convertElementToInt(content);
193 andreas 298
        else if (ename.compare("rm") == 0)          // State count
76 andreas 299
            rm = xml->convertElementToInt(content);
193 andreas 300
        else if (ename.compare("nu") == 0)          // Animate time up
76 andreas 301
            nu = xml->convertElementToInt(content);
193 andreas 302
        else if (ename.compare("nd") == 0)          // Animate time down
76 andreas 303
            nd = xml->convertElementToInt(content);
193 andreas 304
        else if (ename.compare("ar") == 0)          // Auto repeat state
76 andreas 305
            ar = xml->convertElementToInt(content);
193 andreas 306
        else if (ename.compare("ru") == 0)          // Animate time up (bargraph on click)
76 andreas 307
            ru = xml->convertElementToInt(content);
193 andreas 308
        else if (ename.compare("rd") == 0)          // Animate time down (bargraph on click)
76 andreas 309
            rd = xml->convertElementToInt(content);
193 andreas 310
        else if (ename.compare("lu") == 0)          // Animate time up (bargraph)
76 andreas 311
            lu = xml->convertElementToInt(content);
193 andreas 312
        else if (ename.compare("ld") == 0)          // Animate time down (bargraph)
76 andreas 313
            ld = xml->convertElementToInt(content);
193 andreas 314
        else if (ename.compare("rv") == 0)          // ?
76 andreas 315
            rv = xml->convertElementToInt(content);
193 andreas 316
        else if (ename.compare("rl") == 0)          // Bargraph range low
76 andreas 317
            rl = xml->convertElementToInt(content);
193 andreas 318
        else if (ename.compare("rh") == 0)          // Bargraph range high
76 andreas 319
            rh = xml->convertElementToInt(content);
193 andreas 320
        else if (ename.compare("ri") == 0)          // Bargraph inverted (0 = normal, 1 = inverted)
76 andreas 321
            ri = xml->convertElementToInt(content);
193 andreas 322
        else if (ename.compare("rn") == 0)          // Bargraph: Range drag increment
76 andreas 323
            rn = xml->convertElementToInt(content);
193 andreas 324
        else if (ename.compare("lf") == 0)          // Bargraph function
76 andreas 325
            lf = content;
193 andreas 326
        else if (ename.compare("sd") == 0)          // Name/Type of slider for a bargraph
76 andreas 327
            sd = content;
193 andreas 328
        else if (ename.compare("sc") == 0)          // Color of slider (for bargraph)
76 andreas 329
            sc = content;
193 andreas 330
        else if (ename.compare("mt") == 0)          // Length of text area
76 andreas 331
            mt = xml->convertElementToInt(content);
193 andreas 332
        else if (ename.compare("dt") == 0)          // Textarea multiple/single line
76 andreas 333
            dt = content;
193 andreas 334
        else if (ename.compare("im") == 0)          // Input mask of a text area
76 andreas 335
            im = content;
301 andreas 336
        else if (ename.compare("so") == 0)          // String output port
337
            so = xml->convertElementToInt(content);
193 andreas 338
        else if (ename.compare("op") == 0)          // String the button send
76 andreas 339
            op = content;
193 andreas 340
        else if (ename.compare("pc") == 0)          // Password character for text area (single line)
76 andreas 341
            pc = content;
193 andreas 342
        else if (ename.compare("ta") == 0)          // Listbox table channel
343
            ta = xml->convertElementToInt(content);
344
        else if (ename.compare("ti") == 0)          // Listbox table address channel of rows
345
            ti = xml->convertElementToInt(content);
346
        else if (ename.compare("tr") == 0)          // Listbox number of rows
347
            tr = xml->convertElementToInt(content);
348
        else if (ename.compare("tc") == 0)          // Listbox number of columns
349
            tc = xml->convertElementToInt(content);
350
        else if (ename.compare("tj") == 0)          // Listbox row height
351
            tj = xml->convertElementToInt(content);
352
        else if (ename.compare("tk") == 0)          // Listbox preferred row height
353
            tk = xml->convertElementToInt(content);
354
        else if (ename.compare("of") == 0)          // Listbox list offset: 0=disabled/1=enabled
355
            of = xml->convertElementToInt(content);
356
        else if (ename.compare("tg") == 0)          // Listbox managed: 0=no/1=yes
357
            tg = xml->convertElementToInt(content);
279 andreas 358
        else if (ename.compare("st") == 0)          // SubPageView index
359
            st = xml->convertElementToInt(content);
360
        else if (ename.compare("ws") == 0)          // SubPageView: Wrap subpages; 1 = YES
361
            ws = xml->convertElementToInt(content);
362
        else if (ename.compare("sa") == 0)          // SubPageView: Percent of space between items in list
363
            sa = xml->convertElementToInt(content);
364
        else if (ename.compare("dy") == 0)          // SubPageView: Allow dynamic reordering; 1 = YES
365
            dy = xml->convertElementToInt(content);
366
        else if (ename.compare("rs") == 0)          // SubPageView: Reset view on show; 1 = YES
367
            rs = xml->convertElementToInt(content);
368
        else if (ename.compare("on") == 0)          // SubPageView direction
369
            on = content;
300 andreas 370
        else if (ename.compare("ba") == 0)          // SubPageView scrollbar
371
            ba = xml->convertElementToInt(content);
372
        else if (ename.compare("bo") == 0)          // SubPageView scrollbar offset
373
            bo = xml->convertElementToInt(content);
374
        else if (ename.compare("we") == 0)          // SubViewPage Anchor position
375
            we = content;
193 andreas 376
        else if (ename.compare("hd") == 0)          // 1 = Hidden, 0 = Normal visible
76 andreas 377
            hd = xml->convertElementToInt(content);
193 andreas 378
        else if (ename.compare("da") == 0)          // 1 = Disabled, 0 = Normal active
76 andreas 379
            da = xml->convertElementToInt(content);
193 andreas 380
        else if (ename.compare("ac") == 0)          // Direction of text (guess)
51 andreas 381
        {
193 andreas 382
            ac_di = xml->getAttributeInt("di", attrs);  // 0 = left to right; 1 = right to left
51 andreas 383
        }
193 andreas 384
        else if (ename.compare("pf") == 0)          // Function call
3 andreas 385
        {
386
            PUSH_FUNC_T pf;
76 andreas 387
            pf.pfName = content;
388
            pf.pfType = xml->getAttribute("type", attrs);
3 andreas 389
            pushFunc.push_back(pf);
390
        }
193 andreas 391
        else if (ename.compare("sr") == 0)          // Section state resources
3 andreas 392
        {
393
            SR_T bsr;
193 andreas 394
            bsr.number = xml->getAttributeInt("number", attrs); // State number
76 andreas 395
            string e;
3 andreas 396
 
76 andreas 397
            while ((index = xml->getNextElementFromIndex(index, &e, &content, &attrs)) != TExpat::npos)
3 andreas 398
            {
193 andreas 399
                if (e.compare("do") == 0)           // Draw order
76 andreas 400
                    bsr._do = content;
193 andreas 401
                else if (e.compare("bs") == 0)      // Frame type
76 andreas 402
                    bsr.bs = content;
193 andreas 403
                else if (e.compare("mi") == 0)      // Chameleon image
76 andreas 404
                    bsr.mi = content;
193 andreas 405
                else if (e.compare("cb") == 0)      // Border color
76 andreas 406
                    bsr.cb = content;
193 andreas 407
                else if (e.compare("cf") == 0)      // Fill color
76 andreas 408
                    bsr.cf = content;
193 andreas 409
                else if (e.compare("ct") == 0)      // Text color
76 andreas 410
                    bsr.ct = content;
193 andreas 411
                else if (e.compare("ec") == 0)      // Text effect color
76 andreas 412
                    bsr.ec = content;
193 andreas 413
                else if (e.compare("bm") == 0)      // Bitmap image
3 andreas 414
                {
76 andreas 415
                    bsr.bm = content;
416
                    bsr.dynamic = ((xml->getAttributeInt("dynamic", attrs) == 1) ? true : false);
3 andreas 417
                }
193 andreas 418
                else if (e.compare("sd") == 0)      // Sound file
76 andreas 419
                    bsr.sd = content;
193 andreas 420
                else if (e.compare("sb") == 0)      // index external graphic
76 andreas 421
                    bsr.sb = xml->convertElementToInt(content);
193 andreas 422
                else if (e.compare("ii") == 0)      // Icon index
76 andreas 423
                    bsr.ii = xml->convertElementToInt(content);
193 andreas 424
                else if (e.compare("ji") == 0)      // Icon/bitmap orientation
76 andreas 425
                    bsr.ji = xml->convertElementToInt(content);
193 andreas 426
                else if (e.compare("jb") == 0)      // Bitmap orientation
76 andreas 427
                    bsr.jb = xml->convertElementToInt(content);
193 andreas 428
                else if (e.compare("bx") == 0)      // Absolute image position X
76 andreas 429
                    bsr.bx = xml->convertElementToInt(content);
193 andreas 430
                else if (e.compare("by") == 0)      // Absolute image position Y
76 andreas 431
                    bsr.by = xml->convertElementToInt(content);
193 andreas 432
                else if (e.compare("ix") == 0)      // Absolute Icon position X
76 andreas 433
                    bsr.ix = xml->convertElementToInt(content);
193 andreas 434
                else if (e.compare("iy") == 0)      // Absolute Icon position Y
76 andreas 435
                    bsr.iy = xml->convertElementToInt(content);
193 andreas 436
                else if (e.compare("fi") == 0)      // Font index
76 andreas 437
                    bsr.fi = xml->convertElementToInt(content);
193 andreas 438
                else if (e.compare("te") == 0)      // Text
76 andreas 439
                    bsr.te = content;
193 andreas 440
                else if (e.compare("jt") == 0)      // Text orientation
76 andreas 441
                    bsr.jt = (TEXT_ORIENTATION)xml->convertElementToInt(content);
193 andreas 442
                else if (e.compare("tx") == 0)      // Absolute text position X
76 andreas 443
                    bsr.tx = xml->convertElementToInt(content);
193 andreas 444
                else if (e.compare("ty") == 0)      // Absolute text position Y
76 andreas 445
                    bsr.ty = xml->convertElementToInt(content);
193 andreas 446
                else if (e.compare("ww") == 0)      // Word wrap
76 andreas 447
                    bsr.ww = xml->convertElementToInt(content);
193 andreas 448
                else if (e.compare("et") == 0)      // Text effects
76 andreas 449
                    bsr.et = xml->convertElementToInt(content);
193 andreas 450
                else if (e.compare("oo") == 0)      // Opacity
76 andreas 451
                    bsr.oo = xml->convertElementToInt(content);
3 andreas 452
 
76 andreas 453
                oldIndex = index;
3 andreas 454
            }
455
 
456
            sr.push_back(bsr);
457
        }
458
 
76 andreas 459
        if (index == TExpat::npos)
460
            index = oldIndex + 1;
461
    }
3 andreas 462
 
100 andreas 463
    visible = !hd;  // set the initial visibility
464
/*
83 andreas 465
    if (sr.size() > 0 && TStreamError::checkFilter(HLOG_DEBUG))
76 andreas 466
    {
467
        MSG_DEBUG("bi  : " << bi);
468
        MSG_DEBUG("na  : " << na);
469
        MSG_DEBUG("type: " << type);
470
        MSG_DEBUG("lt  : " << lt);
471
        MSG_DEBUG("tp  : " << tp);
472
        MSG_DEBUG("wt  : " << wt);
473
        MSG_DEBUG("ht  : " << ht);
474
 
475
        vector<SR_T>::iterator iter;
476
        size_t pos = 1;
477
 
478
        for (iter = sr.begin(); iter != sr.end(); ++iter)
3 andreas 479
        {
76 andreas 480
            MSG_DEBUG("   " << pos << ": id: " << iter->number);
481
            MSG_DEBUG("   " << pos << ": bs: " << iter->bs);
482
            MSG_DEBUG("   " << pos << ": cb: " << iter->cb);
483
            MSG_DEBUG("   " << pos << ": cf: " << iter->cf);
484
            MSG_DEBUG("   " << pos << ": ct: " << iter->ct);
485
            MSG_DEBUG("   " << pos << ": ec: " << iter->ec);
486
            MSG_DEBUG("   " << pos << ": bm: " << iter->bm);
487
            MSG_DEBUG("   " << pos << ": mi: " << iter->mi);
488
            MSG_DEBUG("   " << pos << ": fi: " << iter->fi);
489
            MSG_DEBUG("   " << pos << ": te: " << iter->te);
490
            pos++;
3 andreas 491
        }
492
    }
100 andreas 493
*/
162 andreas 494
    MSG_DEBUG("Added button " << bi << " --> " << na);
495
 
76 andreas 496
    if (index == TExpat::npos)
497
        return oldIndex + 1;
498
 
499
    return index;
3 andreas 500
}
501
 
43 andreas 502
bool TButton::createSoftButton(const EXTBUTTON_t& bt)
503
{
504
    DECL_TRACER("TButton::createSoftButton(const EXTBUTTON_t& bt)");
505
 
506
    if (bt.sr.size() < 2)
507
    {
508
        MSG_ERROR("Button " << bt.bi << ": " << bt.na << " has less than 2 states!");
509
        return false;
510
    }
511
 
512
    MSG_DEBUG("Adding soft button " << bt.bi << ": " << bt.na);
513
    type = bt.type;
514
    bi = bt.bi;
515
    na = bt.na;
516
    lt = bt.lt;
517
    tp = bt.tp;
518
    wt = bt.wt;
519
    ht = bt.ht;
520
    zo = bt.zo;
521
    hs = bt.hs;
522
    bs = bt.bs;
523
    fb = bt.fb;
524
    ap = bt.ap;
525
    ad = bt.ad;
526
    lp = bt.lp;
527
    lv = bt.lv;
528
    dr = bt.dr;
529
    lu = bt.lu;
530
    ld = bt.ld;
531
    rl = bt.rl;
532
    rh = bt.rh;
533
    rn = bt.rn;
534
    sc = bt.sc;
535
    sr = bt.sr;
177 andreas 536
    mChanged = true;
43 andreas 537
    return true;
538
}
539
 
50 andreas 540
BITMAP_t TButton::getLastImage()
541
{
542
    DECL_TRACER("TButton::getLastImage()");
543
 
285 andreas 544
    if (mLastImage.empty())
289 andreas 545
    {
285 andreas 546
        makeElement(mActInstance);
547
 
289 andreas 548
        if (mLastImage.empty())
549
            return BITMAP_t();
550
    }
551
 
50 andreas 552
    BITMAP_t image;
553
    image.buffer = (unsigned char *)mLastImage.getPixels();
554
    image.rowBytes = mLastImage.info().minRowBytes();
555
    image.width = mLastImage.info().width();
556
    image.height = mLastImage.info().height();
557
    return image;
558
}
559
 
289 andreas 560
TBitmap TButton::getLastBitmap()
561
{
562
    DECL_TRACER("TButton::getLastBitmap()");
563
 
564
    if (mLastImage.empty())
565
        makeElement(mActInstance);
566
 
567
    TBitmap bitmap((unsigned char *)mLastImage.getPixels(), mLastImage.info().width(), mLastImage.info().height());
568
    return bitmap;
569
}
570
 
50 andreas 571
FONT_T TButton::getFont()
572
{
573
    DECL_TRACER("TButton::getFont()");
574
 
575
    if (!mFonts)
576
    {
577
        MSG_ERROR("No fonts available!");
578
        return FONT_T();
579
    }
580
 
200 andreas 581
    if (type == LISTBOX && _getGlobalSettings)
582
    {
583
        _getGlobalSettings(this);
584
        mActInstance = 0;
585
    }
586
 
50 andreas 587
    return mFonts->getFont(sr[mActInstance].fi);
588
}
589
 
51 andreas 590
FONT_STYLE TButton::getFontStyle()
50 andreas 591
{
592
    DECL_TRACER("TButton::getFontStyle()");
593
 
594
    if (!mFonts)
595
    {
596
        MSG_ERROR("No fonts available!");
51 andreas 597
        return FONT_NONE;
50 andreas 598
    }
599
 
600
    return mFonts->getStyle(sr[mActInstance].fi);
601
}
602
 
268 andreas 603
void TButton::setBargraphLevel(int level)
604
{
605
    DECL_TRACER("TButton::setBargraphLevel(int level)");
606
 
607
    if (type != BARGRAPH && type != MULTISTATE_BARGRAPH && type != MULTISTATE_GENERAL)
608
        return;
609
 
610
    if (((type == BARGRAPH || type == MULTISTATE_BARGRAPH) && (level < rl || level > rh)) ||
611
        (type == MULTISTATE_GENERAL && (level < 0 || (size_t)level >= sr.size())))
612
    {
613
        MSG_WARNING("Level for bargraph " << na << " is out of range! (" << rl << " to " << rh << " or size " << sr.size() << ")");
614
        return;
615
    }
616
 
617
    if (((type == BARGRAPH || type == MULTISTATE_BARGRAPH) && mLastLevel != level) ||
618
        (type == MULTISTATE_BARGRAPH && mActInstance != level))
619
        mChanged = true;
269 andreas 620
 
621
    if (!mChanged)
622
        return;
623
 
268 andreas 624
    if (type == BARGRAPH)
625
    {
626
        mLastLevel = level;
627
        drawBargraph(mActInstance, level);
628
    }
629
    else if (type == MULTISTATE_BARGRAPH)
630
    {
631
        mLastLevel = level;
269 andreas 632
        mActInstance = level;
268 andreas 633
        drawMultistateBargraph(level);
634
    }
635
    else
636
        setActive(level);
637
}
638
 
271 andreas 639
bool TButton::invalidate()
640
{
641
    DECL_TRACER("TButton::invalidate()");
642
 
643
    if (prg_stopped)
644
        return true;
645
 
646
    ulong parent = mHandle & 0xffff0000;
647
    THR_REFRESH_t *tr = _findResource(mHandle, parent, bi);
648
 
649
    if (tr && tr->mImageRefresh)
650
    {
651
        if (tr->mImageRefresh->isRunning())
652
            tr->mImageRefresh->stop();
653
    }
654
 
655
    if (type == TEXT_INPUT)
656
    {
657
        if (gPageManager && gPageManager->getCallDropButton())
658
            gPageManager->getCallDropButton()(mHandle);
659
    }
660
 
661
    visible = false;
662
    return true;
663
}
664
 
332 andreas 665
string& TButton::getDrawOrder(int instance)
666
{
667
    DECL_TRACER("TButton::getDrawOrder(int instance)");
668
 
669
    if (instance < 0 || (size_t)instance > sr.size())
670
    {
671
        MSG_ERROR("Instance is out of range!");
672
        return dummy;
673
    }
674
 
675
    return sr[instance]._do;
676
}
677
 
3 andreas 678
BUTTONTYPE TButton::getButtonType(const string& bt)
679
{
680
    DECL_TRACER("TButton::getButtonType(const string& bt)");
681
 
300 andreas 682
    if (strCaseCompare(bt, "general") == 0)
3 andreas 683
        return GENERAL;
300 andreas 684
    else if (strCaseCompare(bt, "multi-state general") == 0 || strCaseCompare(bt, "multiGeneral") == 0)
3 andreas 685
        return MULTISTATE_GENERAL;
300 andreas 686
    else if (strCaseCompare(bt, "bargraph") == 0)
3 andreas 687
        return BARGRAPH;
300 andreas 688
    else if (strCaseCompare(bt, "multi-state bargraph") == 0 || strCaseCompare(bt, "multiBargraph") == 0)
3 andreas 689
        return MULTISTATE_BARGRAPH;
300 andreas 690
    else if (strCaseCompare(bt, "joistick") == 0)
3 andreas 691
        return JOISTICK;
300 andreas 692
    else if (strCaseCompare(bt, "text input") == 0 || strCaseCompare(bt, "textArea") == 0)
3 andreas 693
        return TEXT_INPUT;
300 andreas 694
    else if (strCaseCompare(bt, "computer control") == 0)
3 andreas 695
        return COMPUTER_CONTROL;
300 andreas 696
    else if (strCaseCompare(bt, "take note") == 0)
3 andreas 697
        return TAKE_NOTE;
300 andreas 698
    else if (strCaseCompare(bt, "sub-page view") == 0 || strCaseCompare(bt, "subPageView") == 0)
3 andreas 699
        return SUBPAGE_VIEW;
300 andreas 700
    else if (strCaseCompare(bt, "listBox") == 0)
192 andreas 701
        return LISTBOX;
3 andreas 702
 
703
    return NONE;
704
}
705
 
283 andreas 706
string TButton::buttonTypeToString()
707
{
708
    switch(type)
709
    {
710
        case NONE:                  return "NONE";
711
        case GENERAL:               return "GENERAL";
712
        case MULTISTATE_GENERAL:    return "MULTISTAE GENERAL";
713
        case BARGRAPH:              return "BARGRAPH";
714
        case MULTISTATE_BARGRAPH:   return "MULTISTATE BARGRAPH";
715
        case JOISTICK:              return "JOISTICK";
716
        case TEXT_INPUT:            return "TEXT INPUT";
717
        case COMPUTER_CONTROL:      return "COMPUTER CONTROL";
718
        case TAKE_NOTE:             return "TAKE NOTE";
719
        case SUBPAGE_VIEW:          return "SUBPAGE VIEW";
720
        case LISTBOX:               return "LISTBOX";
721
    }
722
 
723
    return "";
724
}
725
 
3 andreas 726
FEEDBACK TButton::getButtonFeedback(const string& fb)
727
{
728
    DECL_TRACER("TButton::getButtonFeedback(const string& fb)");
729
 
730
    if (fb.compare("channel") == 0)
731
        return FB_CHANNEL;
732
    else if (fb.compare("inverted channel") == 0)
733
        return FB_INV_CHANNEL;
734
    else if (fb.compare("always on") == 0)
735
        return FB_ALWAYS_ON;
736
    else if (fb.compare("momentary") == 0)
737
        return FB_MOMENTARY;
738
    else if (fb.compare("blink") == 0)
739
        return FB_BLINK;
740
 
741
    return FB_NONE;
742
}
743
 
40 andreas 744
bool TButton::createButtons(bool force)
3 andreas 745
{
177 andreas 746
    DECL_TRACER("TButton::createButtons(bool force)");
3 andreas 747
 
33 andreas 748
    if (prg_stopped)
749
        return false;
35 andreas 750
 
46 andreas 751
    if (force)
177 andreas 752
    {
753
        mChanged = true;
46 andreas 754
        MSG_TRACE("Creating of image is forced!");
177 andreas 755
    }
46 andreas 756
 
3 andreas 757
    // Get the images, if there any
350 andreas 758
    if (sr.empty())
759
        return true;
760
 
3 andreas 761
    vector<SR_T>::iterator srIter;
762
 
118 andreas 763
    for (srIter = sr.begin(); srIter != sr.end(); ++srIter)
3 andreas 764
    {
765
        int number = srIter->number;
766
 
21 andreas 767
        if (srIter->sb > 0)
768
            continue;
769
 
165 andreas 770
        bool bmExistMi = false;
771
        bool bmExistBm = false;
772
        bool reload = false;
46 andreas 773
 
165 andreas 774
        if (!srIter->mi.empty())
775
        {
776
            if ((bmExistMi = TImgCache::existBitmap(srIter->mi, _BMTYPE_CHAMELEON)) == false)
177 andreas 777
            {
778
                mChanged = true;
165 andreas 779
                reload = true;
177 andreas 780
            }
165 andreas 781
        }
782
 
783
        if (!srIter->bm.empty())
784
        {
785
            if ((bmExistBm = TImgCache::existBitmap(srIter->bm, _BMTYPE_BITMAP)) == false)
177 andreas 786
            {
787
                mChanged = true;
165 andreas 788
                reload = true;
177 andreas 789
            }
165 andreas 790
        }
791
 
40 andreas 792
        if (!force)
793
        {
165 andreas 794
            if (!reload)   // If the image already exist, do not load it again.
40 andreas 795
                continue;
796
        }
797
 
165 andreas 798
        if (!bmExistMi && !srIter->mi.empty())        // Do we have a chameleon image?
3 andreas 799
        {
4 andreas 800
            sk_sp<SkData> image;
165 andreas 801
            SkBitmap bm;
4 andreas 802
 
803
            if (!(image = readImage(srIter->mi)))
3 andreas 804
                return false;
805
 
165 andreas 806
            DecodeDataToBitmap(image, &bm);
3 andreas 807
 
165 andreas 808
            if (bm.empty())
3 andreas 809
            {
810
                MSG_WARNING("Could not create a picture for element " << number << " on button " << bi << " (" << na << ")");
811
                return false;
812
            }
813
 
167 andreas 814
            TImgCache::addImage(srIter->mi, bm, _BMTYPE_CHAMELEON);
815
            srIter->mi_width = bm.info().width();
816
            srIter->mi_height = bm.info().height();
177 andreas 817
            mChanged = true;
3 andreas 818
        }
819
 
165 andreas 820
        if (!bmExistBm && !srIter->bm.empty())        // Do we have a bitmap?
3 andreas 821
        {
4 andreas 822
            sk_sp<SkData> image;
165 andreas 823
            SkBitmap bm;
4 andreas 824
 
825
            if (!(image = readImage(srIter->bm)))
3 andreas 826
                return false;
827
 
165 andreas 828
            DecodeDataToBitmap(image, &bm);
3 andreas 829
 
165 andreas 830
            if (bm.empty())
3 andreas 831
            {
832
                MSG_WARNING("Could not create a picture for element " << number << " on button " << bi << " (" << na << ")");
833
                return false;
834
            }
835
 
167 andreas 836
            TImgCache::addImage(srIter->bm, bm, _BMTYPE_BITMAP);
837
            srIter->bm_width = bm.info().width();
838
            srIter->bm_height = bm.info().height();
177 andreas 839
            mChanged = true;
3 andreas 840
        }
841
    }
842
 
4 andreas 843
    return true;
844
}
3 andreas 845
 
16 andreas 846
void TButton::refresh()
847
{
848
    DECL_TRACER("TButton::refresh()");
849
 
177 andreas 850
    mChanged = true;
16 andreas 851
    makeElement();
852
}
853
 
15 andreas 854
bool TButton::makeElement(int instance)
855
{
192 andreas 856
    DECL_TRACER("TButton::makeElement(int instance)");
15 andreas 857
 
33 andreas 858
    if (prg_stopped)
859
        return false;
35 andreas 860
 
15 andreas 861
    int inst = mActInstance;
862
 
46 andreas 863
    if (instance >= 0 && (size_t)instance < sr.size())
177 andreas 864
    {
865
        if (mActInstance != instance)
866
            mChanged = true;
867
 
15 andreas 868
        inst = instance;
177 andreas 869
    }
15 andreas 870
 
192 andreas 871
    bool isSystem = isSystemButton();
872
 
15 andreas 873
    if (type == MULTISTATE_GENERAL && ar == 1)
874
        return drawButtonMultistateAni();
192 andreas 875
    else if (type == BARGRAPH && isSystem && lv == 9)   // System volume button
141 andreas 876
        return drawBargraph(inst, TConfig::getSystemVolume());
15 andreas 877
    else if (type == BARGRAPH)
878
        return drawBargraph(inst, mLastLevel);
38 andreas 879
    else if (type == MULTISTATE_BARGRAPH)
46 andreas 880
        return drawMultistateBargraph(mLastLevel, true);
50 andreas 881
    else if (type == TEXT_INPUT)
882
    {
192 andreas 883
        if (isSystem && !mSystemReg)
343 andreas 884
        {
51 andreas 885
            registerSystemButton();
343 andreas 886
            mChanged = true;
887
        }
51 andreas 888
 
310 andreas 889
        drawTextArea(inst);
890
        mActInstance = inst;
50 andreas 891
    }
193 andreas 892
    else if (type == LISTBOX)
893
    {
199 andreas 894
        if (_getListContent && !mSystemReg)
895
        {
200 andreas 896
            mListContent = _getListContent(mHandle, ap, ta, ti, tr, tc);
897
            mChanged = true;
199 andreas 898
        }
899
 
900
        if (isSystem)
901
            mSystemReg = true;
902
 
200 andreas 903
        drawList();
193 andreas 904
    }
192 andreas 905
    else if (isSystem && type == GENERAL)
71 andreas 906
    {
192 andreas 907
        TConfig::setTemporary(true);
71 andreas 908
 
195 andreas 909
        if (isSystemCheckBox(ch))
192 andreas 910
        {
195 andreas 911
            int in = getButtonInstance(0, ch);
912
 
913
            if (in >= 0)
914
            {
915
                inst = mActInstance = in;
916
#ifndef __ANDROID__
301 andreas 917
                if (ch == SYSTEM_ITEM_VIEWSCALEFIT && sr[0].oo < 0) // scale to fit disabled
195 andreas 918
                {
919
                    sr[0].oo = 128;
920
                    mChanged = true;
921
                }
922
#else
301 andreas 923
                if (ch == SYSTEM_ITEM_VIEWBANNER && sr[0].oo < 0)   // show banner disabled
195 andreas 924
                {
925
                    sr[0].oo = 128;
926
                    mChanged = true;
927
                }
928
#endif
301 andreas 929
                if (ch == SYSTEM_ITEM_VIEWTOOLBAR)  // Force toolbar is only available if toolbar is not suppressed
198 andreas 930
                {
931
                    if (TConfig::getToolbarSuppress() && sr[0].oo < 0)
932
                    {
933
                        sr[0].oo = 128;
934
                        mChanged = true;
935
                    }
936
                    else if (!TConfig::getToolbarSuppress() && sr[0].oo > 0)
937
                    {
938
                        sr[0].oo = -1;
939
                        mChanged = true;
940
                    }
941
                }
195 andreas 942
            }
192 andreas 943
        }
206 andreas 944
        else if (isSystemTextLine(ad) && ad != SYSTEM_ITEM_FTPSURFACE)
192 andreas 945
        {
195 andreas 946
            sr[0].te = sr[1].te = fillButtonText(ad, 0);
343 andreas 947
            mChanged = true;
192 andreas 948
        }
141 andreas 949
 
198 andreas 950
        TConfig::setTemporary(false);
343 andreas 951
 
952
        if (mLastImage.empty())
953
            mChanged = true;
954
 
192 andreas 955
        MSG_DEBUG("Drawing system button " << ch << " with instance " << inst);
141 andreas 956
        return drawButton(inst);
957
    }
15 andreas 958
    else
310 andreas 959
    {
343 andreas 960
        if (mLastImage.empty())
961
            mChanged = true;
962
 
15 andreas 963
        return drawButton(inst);
310 andreas 964
    }
15 andreas 965
 
966
    return false;
967
}
968
 
14 andreas 969
bool TButton::setActive(int instance)
970
{
971
    DECL_TRACER("TButton::setActive(int instance)");
972
 
53 andreas 973
    if (mAniRunning)
343 andreas 974
    {
975
#if TESTMODE == 1
976
        setScreenDone();
977
#endif
53 andreas 978
        return true;
343 andreas 979
    }
53 andreas 980
 
165 andreas 981
    if (instance < 0 || (size_t)instance >= sr.size())
14 andreas 982
    {
165 andreas 983
        MSG_ERROR("Instance " << instance << " is out of range from 0 to " << sr.size() << "!");
343 andreas 984
#if TESTMODE == 1
985
        setScreenDone();
986
#endif
14 andreas 987
        return false;
988
    }
989
 
318 andreas 990
    if (instance == mActInstance && !mLastImage.empty())
343 andreas 991
    {
992
#if TESTMODE == 1
993
        __success = true;
994
        setScreenDone();
995
#endif
14 andreas 996
        return true;
343 andreas 997
    }
14 andreas 998
 
999
    mActInstance = instance;
177 andreas 1000
    mChanged = true;
15 andreas 1001
    makeElement(instance);
14 andreas 1002
 
1003
    return true;
1004
}
1005
 
1006
bool TButton::setIcon(int id, int instance)
1007
{
1008
    DECL_TRACER("TButton::setIcon(int id, int instance)");
1009
 
167 andreas 1010
    if (instance >= 0 && (size_t)instance >= sr.size())
14 andreas 1011
    {
1012
        MSG_ERROR("Instance " << instance << " does not exist!");
1013
        return false;
1014
    }
1015
 
165 andreas 1016
    int inst = instance;
1017
    int loop = 1;
1018
 
1019
    if (inst < 0)
1020
    {
1021
        loop = (int)sr.size();
1022
        inst = 0;
1023
    }
1024
 
1025
    for (int i = 0; i < loop; ++i)
1026
    {
175 andreas 1027
        if (sr[inst].ii != id)
1028
            mChanged = true;
1029
 
165 andreas 1030
        sr[inst].ii = id;
1031
        inst++;
1032
    }
1033
 
16 andreas 1034
    return makeElement(instance);
14 andreas 1035
}
1036
 
1037
bool TButton::setIcon(const string& icon, int instance)
1038
{
1039
    DECL_TRACER("TButton::setIcon(const string& icon, int instance)");
1040
 
167 andreas 1041
    if (instance >= 0 && (size_t)instance >= sr.size())
14 andreas 1042
    {
1043
        MSG_ERROR("Instance " << instance << " does not exist!");
1044
        return false;
1045
    }
1046
 
1047
    if (!gIcons)
1048
    {
1049
        gIcons = new TIcons();
1050
 
1051
        if (TError::isError())
1052
        {
1053
            MSG_ERROR("Error initializing icons!");
1054
            return false;
1055
        }
1056
    }
1057
 
1058
    int id = gIcons->getNumber(icon);
1059
 
1060
    if (id == -1)
1061
    {
1062
        MSG_WARNING("Icon " << icon << " not found!");
1063
        return false;
1064
    }
1065
 
165 andreas 1066
    int inst = instance;
1067
    int loop = 1;
1068
 
1069
    if (inst < 0)
1070
    {
1071
        loop = (int)sr.size();
1072
        inst = 0;
1073
    }
1074
 
1075
    for (int i = 0; i < loop; ++i)
1076
    {
1077
        if (sr[inst].ii == id)
1078
        {
1079
            inst++;
1080
            continue;
1081
        }
1082
 
316 andreas 1083
        if (sr[inst].ii != id)
1084
            mChanged = true;
1085
 
165 andreas 1086
        sr[inst].ii = id;
1087
        inst++;
1088
    }
1089
 
16 andreas 1090
    return makeElement(instance);
14 andreas 1091
}
1092
 
1093
bool TButton::revokeIcon(int instance)
1094
{
1095
    DECL_TRACER("TButton::revokeIcon(int instance)");
1096
 
167 andreas 1097
    if (instance >= 0 && (size_t)instance >= sr.size())
14 andreas 1098
    {
1099
        MSG_ERROR("Instance " << instance << " does not exist!");
1100
        return false;
1101
    }
1102
 
165 andreas 1103
    int inst = instance;
1104
    int loop = 1;
1105
 
1106
    if (inst < 0)
1107
    {
1108
        loop = (int)sr.size();
1109
        inst = 0;
1110
    }
1111
 
1112
    for (int i = 0; i < loop; ++i)
1113
    {
1114
        if (sr[inst].ii == 0)
1115
        {
1116
            inst++;
1117
            continue;
1118
        }
1119
 
316 andreas 1120
        if (sr[inst].ii != 0)
1121
            mChanged = true;
1122
 
165 andreas 1123
        sr[inst].ii = 0;
1124
        inst++;
1125
    }
1126
 
16 andreas 1127
    return makeElement(instance);
14 andreas 1128
}
1129
 
1130
bool TButton::setText(const string& txt, int instance)
1131
{
1132
    DECL_TRACER("TButton::setText(const string& txt, int instance)");
1133
 
167 andreas 1134
    if (instance >= 0 && (size_t)instance >= sr.size())
14 andreas 1135
    {
1136
        MSG_ERROR("Instance " << instance << " does not exist!");
334 andreas 1137
#if TESTMODE == 1
1138
        setAllDone();
1139
#endif
14 andreas 1140
        return false;
1141
    }
1142
 
165 andreas 1143
    if (!setTextOnly(txt, instance))
334 andreas 1144
    {
1145
#if TESTMODE == 1
1146
        setAllDone();
1147
#endif
165 andreas 1148
        return false;
334 andreas 1149
    }
165 andreas 1150
 
333 andreas 1151
    if (!mChanged)      // Do not try to redraw the button if nothing changed
1152
    {
1153
#if TESTMODE == 1
334 andreas 1154
        MSG_INFO("Nothing changed!");
333 andreas 1155
        __success = true;
334 andreas 1156
        setScreenDone();
333 andreas 1157
#endif
1158
        return true;
1159
    }
1160
 
16 andreas 1161
    return makeElement(instance);
14 andreas 1162
}
1163
 
51 andreas 1164
bool TButton::setTextOnly(const string& txt, int instance)
1165
{
1166
    DECL_TRACER("TButton::setTextOnly(const string& txt, int instance)");
1167
 
167 andreas 1168
    if (instance >= 0 && (size_t)instance >= sr.size())
51 andreas 1169
    {
1170
        MSG_ERROR("Instance " << instance << " does not exist!");
1171
        return false;
1172
    }
1173
 
333 andreas 1174
    if (instance < 0)
165 andreas 1175
    {
333 andreas 1176
        for (size_t i = 0; i < sr.size(); ++i)
1177
        {
1178
            if (sr[i].te != txt && (int)i == mActInstance)
1179
                mChanged = true;
165 andreas 1180
 
333 andreas 1181
            sr[i].te = txt;
1182
        }
165 andreas 1183
    }
333 andreas 1184
    else
334 andreas 1185
    {
1186
        if (sr[instance].te != txt && (int)instance == mActInstance)
1187
            mChanged = true;
1188
 
333 andreas 1189
        sr[instance].te = txt;
334 andreas 1190
    }
165 andreas 1191
 
333 andreas 1192
    if (instance <= 0 && isSystemButton())
192 andreas 1193
    {
316 andreas 1194
        bool temp = TConfig::setTemporary(true);
209 andreas 1195
        // If we've an input line or the text line of a "combobox" then we'll
1196
        // save the changed value here.
192 andreas 1197
        switch(ad)
1198
        {
209 andreas 1199
            case SYSTEM_ITEM_NETLINX_IP:        TConfig::saveController(txt); break;
1200
            case SYSTEM_ITEM_NETLINX_CHANNEL:   TConfig::saveChannel(atoi(txt.c_str())); break;
1201
            case SYSTEM_ITEM_NETLINX_PORT:      TConfig::savePort(atoi(txt.c_str())); break;
1202
            case SYSTEM_ITEM_NETLINX_PTYPE:     TConfig::savePanelType(txt); break;
192 andreas 1203
 
209 andreas 1204
            case SYSTEM_ITEM_SYSTEMSOUND:       TConfig::saveSystemSoundFile(txt); break;
1205
            case SYSTEM_ITEM_SINGLEBEEP:        TConfig::saveSingleBeepFile(txt); break;
1206
            case SYSTEM_ITEM_DOUBLEBEEP:        TConfig::saveDoubleBeepFile(txt); break;
192 andreas 1207
 
209 andreas 1208
            case SYSTEM_ITEM_SIPPROXY:          TConfig::setSIPproxy(txt); break;
1209
            case SYSTEM_ITEM_SIPPORT:           TConfig::setSIPport(atoi(txt.c_str())); break;
1210
            case SYSTEM_ITEM_SIPSTUN:           TConfig::setSIPstun(txt); break;
1211
            case SYSTEM_ITEM_SIPDOMAIN:         TConfig::setSIPdomain(txt); break;
1212
            case SYSTEM_ITEM_SIPUSER:           TConfig::setSIPuser(txt); break;
1213
            case SYSTEM_ITEM_SIPPASSWORD:       TConfig::setSIPpassword(txt); break;
195 andreas 1214
 
209 andreas 1215
            case SYSTEM_ITEM_LOGLOGFILE:        TConfig::saveLogFile(txt); break;
192 andreas 1216
 
209 andreas 1217
            case SYSTEM_ITEM_FTPUSER:           TConfig::saveFtpUser(txt); break;
1218
            case SYSTEM_ITEM_FTPPASSWORD:       TConfig::saveFtpPassword(txt); break;
1219
            case SYSTEM_ITEM_FTPSURFACE:        TConfig::saveFtpSurface(txt); break;
192 andreas 1220
        }
316 andreas 1221
 
1222
        TConfig::setTemporary(temp);
192 andreas 1223
    }
1224
 
51 andreas 1225
    return true;
1226
}
1227
 
43 andreas 1228
bool TButton::appendText(const string &txt, int instance)
1229
{
1230
    DECL_TRACER("TButton::appendText(const string &txt, int instance)");
1231
 
331 andreas 1232
    if (instance >= 0 && (size_t)instance >= sr.size())
43 andreas 1233
    {
60 andreas 1234
        MSG_ERROR("Instance " << instance << " does not exist!");
43 andreas 1235
        return false;
1236
    }
1237
 
175 andreas 1238
    if (txt.empty())
333 andreas 1239
    {
1240
#if TESTMODE == 1
1241
        __success = true;
1242
        __done = true;
1243
#endif
175 andreas 1244
        return true;
165 andreas 1245
    }
1246
 
333 andreas 1247
    if (instance < 0)
165 andreas 1248
    {
333 andreas 1249
        for (size_t i = 0; i < sr.size(); ++i)
1250
            sr[i].te.append(txt);
165 andreas 1251
    }
333 andreas 1252
    else
1253
        sr[instance].te.append(txt);
165 andreas 1254
 
333 andreas 1255
    mChanged = true;
43 andreas 1256
    return makeElement(instance);
1257
}
1258
 
309 andreas 1259
void TButton::setTextCursorPosition(int oldPos, int newPos)
1260
{
1261
    DECL_TRACER("TButton::setTextCursorPosition(int oldPos, int newPos)");
1262
 
1263
    if (type != TEXT_INPUT)
1264
        return;
1265
 
1266
    if (oldPos == newPos && newPos == mCursorPosition)
1267
        return;
1268
 
1269
    mCursorPosition = newPos;
1270
}
1271
 
1272
void TButton::setTextFocus(bool in)
1273
{
1274
    DECL_TRACER("TButton::setTextFocus(bool in)");
1275
 
1276
    if (type != TEXT_INPUT)
1277
        return;
1278
 
1279
    mHasFocus = in;
1280
 
1281
    if (mHasFocus && mActInstance != STATE_ON)
1282
        makeElement(STATE_ON);
1283
    else if (!mHasFocus && mActInstance != STATE_OFF)
1284
        makeElement(STATE_OFF);
1285
}
1286
 
43 andreas 1287
bool TButton::setBorderColor(const string &color, int instance)
1288
{
1289
    DECL_TRACER("TButton::setBorderColor(const string &color, int instance)");
1290
 
167 andreas 1291
    if (instance >= 0 && (size_t)instance >= sr.size())
43 andreas 1292
    {
60 andreas 1293
        MSG_ERROR("Instance " << instance << " does not exist!");
351 andreas 1294
#if TESTMODE == 1
1295
        setScreenDone();
1296
#endif
43 andreas 1297
        return false;
1298
    }
1299
 
351 andreas 1300
    if (instance < 0)
1301
    {
1302
        for (size_t i = 0; i < sr.size(); ++i)
1303
        {
1304
            if (sr[i].cb.compare(color) == 0)
1305
                continue;
165 andreas 1306
 
351 andreas 1307
            if ((int)i == mActInstance)
1308
                mChanged = true;
1309
 
1310
            sr[i].cb = color;
1311
        }
1312
    }
1313
    else if (sr[instance].cb != color)
165 andreas 1314
    {
351 andreas 1315
        if (mActInstance != instance)
1316
            mChanged = true;
1317
 
1318
        sr[instance].cb = color;
165 andreas 1319
    }
1320
 
351 andreas 1321
    if (!mChanged)
165 andreas 1322
    {
351 andreas 1323
#if TESTMODE == 1
1324
        __success = true;
1325
        setScreenDone();
1326
#endif
1327
        return true;
165 andreas 1328
    }
1329
 
43 andreas 1330
    return makeElement(instance);
1331
}
1332
 
82 andreas 1333
string TButton::getBorderColor(int instance)
1334
{
1335
    DECL_TRACER("TButton::getBorderColor(int instance)");
1336
 
165 andreas 1337
    if (instance < 0 || (size_t)instance >= sr.size())
82 andreas 1338
    {
1339
        MSG_ERROR("Instance " << instance << " does not exist!");
1340
        return string();
1341
    }
1342
 
1343
    return sr[instance].cb;
1344
}
1345
 
60 andreas 1346
bool TButton::setFillColor(const string& color, int instance)
1347
{
1348
    DECL_TRACER("TButton::setFillColor(const string& color, int instance)");
1349
 
167 andreas 1350
    if (instance >= 0 && (size_t)instance >= sr.size())
60 andreas 1351
    {
1352
        MSG_ERROR("Instance " << instance << " does not exist!");
1353
        return false;
1354
    }
1355
 
351 andreas 1356
    if (instance < 0)
1357
    {
1358
        for (size_t i = 0; i < sr.size(); ++i)
1359
        {
1360
            if (sr[i].cf == color)
1361
                continue;
165 andreas 1362
 
351 andreas 1363
            if ((int)i == mActInstance)
1364
                mChanged = true;
1365
 
1366
            sr[i].cf = color;
1367
        }
1368
    }
1369
    else if (sr[instance].cf != color)
165 andreas 1370
    {
351 andreas 1371
        if (mActInstance != instance)
1372
            mChanged = true;
1373
 
1374
        sr[instance].cf = color;
165 andreas 1375
    }
1376
 
351 andreas 1377
    if (!mChanged)
165 andreas 1378
    {
351 andreas 1379
#if TESTMODE == 1
1380
        __success = true;
1381
        setScreenDone();
1382
#endif
1383
        return true;
165 andreas 1384
    }
1385
 
60 andreas 1386
    return makeElement(instance);
1387
}
1388
 
1389
bool TButton::setTextColor(const string& color, int instance)
1390
{
1391
    DECL_TRACER("TButton::setTextColor(const string& color, int instance)");
1392
 
205 andreas 1393
    if (!setTextColorOnly(color, instance))
1394
        return false;
1395
 
351 andreas 1396
    if (!mChanged)
1397
    {
1398
#if TESTMODE == 1
1399
        __success = true;
1400
        setScreenDone();
1401
#endif
1402
        return true;
1403
    }
1404
 
205 andreas 1405
    return makeElement(instance);
1406
}
1407
 
1408
bool TButton::setTextColorOnly(const string& color, int instance)
1409
{
1410
    DECL_TRACER("TButton::setTextColorOnly(const string& color, int instance)");
1411
 
167 andreas 1412
    if (instance >= 0 && (size_t)instance >= sr.size())
60 andreas 1413
    {
1414
        MSG_ERROR("Instance " << instance << " does not exist!");
1415
        return false;
1416
    }
1417
 
351 andreas 1418
    if (instance < 0)
1419
    {
1420
        for (size_t i = 0; i < sr.size(); ++i)
1421
        {
1422
            if (sr[i].ct == color)
1423
                continue;
165 andreas 1424
 
351 andreas 1425
            if ((int)i == mActInstance)
1426
                mChanged = true;
1427
 
1428
            sr[i].ct = color;
1429
        }
165 andreas 1430
    }
351 andreas 1431
    else if (sr[instance].ct != color)
165 andreas 1432
    {
351 andreas 1433
        if (mActInstance == instance)
1434
            mChanged = true;
165 andreas 1435
 
351 andreas 1436
        sr[instance].ct = color;
165 andreas 1437
    }
1438
 
205 andreas 1439
    return true;
60 andreas 1440
}
1441
 
1442
bool TButton::setDrawOrder(const string& order, int instance)
1443
{
1444
    DECL_TRACER("TButton::setDrawOrder(const string& order, int instance)");
1445
 
167 andreas 1446
    if (instance >= 0 && (size_t)instance >= sr.size())
60 andreas 1447
    {
1448
        MSG_ERROR("Instance " << instance << " does not exist!");
1449
        return false;
1450
    }
1451
 
351 andreas 1452
    if (instance < 0)
1453
    {
1454
        for (size_t i = 0; i < sr.size(); ++i)
1455
        {
1456
            if (sr[i]._do == order)
1457
                continue;
165 andreas 1458
 
351 andreas 1459
            if ((int)i == mActInstance)
1460
                mChanged = true;
1461
 
1462
            sr[i]._do = order;
1463
        }
1464
    }
1465
    else if (sr[instance]._do != order)
165 andreas 1466
    {
351 andreas 1467
        if (mActInstance == instance)
1468
            mChanged = true;
1469
 
1470
        sr[instance]._do = order;
165 andreas 1471
    }
1472
 
351 andreas 1473
    if (!mChanged)
165 andreas 1474
    {
351 andreas 1475
#if TESTMODE == 1
1476
        __success = true;
1477
        setScreenDone();
1478
#endif
1479
        return true;
165 andreas 1480
    }
1481
 
60 andreas 1482
    return makeElement(instance);
1483
}
1484
 
335 andreas 1485
FEEDBACK TButton::getFeedback()
1486
{
1487
    DECL_TRACER("TButton::getFeedback()");
1488
 
1489
    if (type != GENERAL)
1490
        return FB_NONE;
1491
 
1492
    return fb;
1493
}
1494
 
60 andreas 1495
bool TButton::setFeedback(FEEDBACK feedback)
1496
{
1497
    DECL_TRACER("TButton::setFeedback(FEEDBACK feedback)");
1498
 
335 andreas 1499
    if (type != GENERAL)
1500
    {
1501
#if TESTMODE == 1
1502
        setAllDone();
1503
#endif
1504
        return false;
1505
    }
1506
 
152 andreas 1507
    int oldFB = fb;
60 andreas 1508
    fb = feedback;
152 andreas 1509
 
1510
    if (mEnabled && !hd)
1511
    {
1512
        if ((feedback == FB_ALWAYS_ON || feedback == FB_INV_CHANNEL) && mActInstance != 1)
1513
        {
1514
            mActInstance = 1;
175 andreas 1515
            mChanged = true;
152 andreas 1516
            makeElement(1);
1517
        }
1518
        else if (oldFB == FB_ALWAYS_ON && feedback != FB_ALWAYS_ON && feedback != FB_INV_CHANNEL && mActInstance == 1)
1519
        {
1520
            mActInstance = 0;
175 andreas 1521
            mChanged = true;
152 andreas 1522
            makeElement(0);
1523
        }
1524
    }
335 andreas 1525
#if TESTMODE == 1
1526
    if (!mChanged)
1527
        __success = true;
152 andreas 1528
 
335 andreas 1529
    setScreenDone();
1530
#endif
60 andreas 1531
    return true;
1532
}
1533
 
1534
bool TButton::setBorderStyle(const string& style, int instance)
1535
{
1536
    DECL_TRACER("TButton::setBorderStyle(const string& style, int instance)");
1537
 
167 andreas 1538
    if (instance >= 0 && (size_t)instance >= sr.size())
60 andreas 1539
    {
1540
        MSG_ERROR("Instance " << instance << " does not exist!");
1541
        return false;
1542
    }
1543
 
175 andreas 1544
    mChanged = true;
336 andreas 1545
    MSG_DEBUG("Setting border " << style);
175 andreas 1546
 
60 andreas 1547
    if (strCaseCompare(style, "None") == 0)     // Clear the border?
1548
    {
152 andreas 1549
        if (instance < 0)
332 andreas 1550
        {
60 andreas 1551
            bs.clear();
332 andreas 1552
 
1553
            for (size_t i = 0; i < sr.size(); ++i)
1554
                sr[i].bs.clear();
1555
        }
60 andreas 1556
        else
332 andreas 1557
        {
152 andreas 1558
            sr[instance].bs.clear();
332 andreas 1559
            bs.clear();
1560
        }
60 andreas 1561
 
152 andreas 1562
        if (mEnabled && !hd)
1563
            makeElement(instance);
1564
 
60 andreas 1565
        return true;
1566
    }
1567
 
79 andreas 1568
    // Look in the system table and try to find the border.
1569
    if (gPageManager && gPageManager->getSystemDraw())
1570
    {
1571
        if (gPageManager->getSystemDraw()->existBorder(style))
1572
        {
152 andreas 1573
            if (instance < 0)
332 andreas 1574
            {
79 andreas 1575
                bs = style;
332 andreas 1576
 
1577
                for (size_t i = 0; i < sr.size(); ++i)
1578
                    sr[i].bs = style;
1579
            }
79 andreas 1580
            else
332 andreas 1581
            {
152 andreas 1582
                sr[instance].bs = style;
79 andreas 1583
 
332 andreas 1584
                if (bs != style)
1585
                    bs.clear();
1586
            }
1587
 
152 andreas 1588
            if (mEnabled && !hd)
1589
                makeElement(instance);
1590
 
79 andreas 1591
            return true;
1592
        }
1593
    }
1594
 
60 andreas 1595
    // Check whether it is a supported style or not. If the style is not
1596
    // supported, it will be ignored.
306 andreas 1597
    string corrName = getCorrectName(style);
60 andreas 1598
 
306 andreas 1599
    if (!style.empty())
60 andreas 1600
    {
306 andreas 1601
        if (instance < 0)
332 andreas 1602
        {
306 andreas 1603
            bs = corrName;
332 andreas 1604
 
1605
            for (size_t i = 0; i < sr.size(); ++i)
1606
                sr[i].bs = corrName;
1607
        }
306 andreas 1608
        else
332 andreas 1609
        {
306 andreas 1610
            sr[instance].bs = corrName;
60 andreas 1611
 
332 andreas 1612
            if (bs != corrName)
1613
                bs.clear();
1614
        }
1615
 
306 andreas 1616
        if (mEnabled && !hd)
1617
            makeElement(instance);
152 andreas 1618
 
306 andreas 1619
        return true;
60 andreas 1620
    }
332 andreas 1621
#if TESTMODE == 1
1622
    __done = true;
1623
#endif
60 andreas 1624
    return false;
1625
}
1626
 
332 andreas 1627
bool TButton::setBorderStyle(int style, int instance)
1628
{
1629
    DECL_TRACER("TButton::setBorderStyle(int style, int instance)");
1630
 
1631
    if (instance >= 0 && (size_t)instance >= sr.size())
1632
    {
1633
        MSG_ERROR("Instance " << instance << " does not exist!");
1634
        return false;
1635
    }
1636
 
1637
    if (style == 0)     // Clear the border?
1638
    {
1639
        if (instance < 0)
1640
        {
1641
            bs.clear();
1642
 
1643
            for (size_t i = 0; i < sr.size(); ++i)
334 andreas 1644
            {
1645
                if (!sr[i].bs.empty())
1646
                    mChanged = true;
1647
 
332 andreas 1648
                sr[i].bs.clear();
334 andreas 1649
            }
1650
 
1651
            if (!bs.empty())
1652
                mChanged = true;
1653
 
1654
            bs.clear();
332 andreas 1655
        }
1656
        else
1657
        {
334 andreas 1658
            if (!sr[instance].bs.empty())
1659
                mChanged = true;
1660
 
332 andreas 1661
            sr[instance].bs.clear();
1662
            bs.clear();
1663
        }
1664
 
1665
        if (mEnabled && !hd)
1666
            makeElement(instance);
1667
 
1668
        return true;
1669
    }
1670
 
1671
    string st = getBorderName(style);
1672
 
1673
    if (st.empty())
1674
    {
1675
        MSG_WARNING("The index " << style << " is not supported!");
1676
#if TESTMODE == 1
334 andreas 1677
        setAllDone();
332 andreas 1678
#endif
1679
        return false;
1680
    }
1681
 
1682
    // Look in the system table and try to find the border.
1683
    if (gPageManager && gPageManager->getSystemDraw())
1684
    {
1685
        if (gPageManager->getSystemDraw()->existBorder(st))
1686
        {
1687
            MSG_DEBUG("Found frame " << st << " and draw it ...");
1688
 
1689
            if (instance < 0)
1690
            {
1691
                bs = st;
1692
 
1693
                for (size_t i = 0; i < sr.size(); ++i)
1694
                    sr[i].bs = st;
1695
            }
1696
            else
1697
            {
1698
                sr[instance].bs = st;
1699
 
1700
                if (bs != st)
1701
                    bs.clear();
1702
            }
1703
 
1704
            mChanged = true;
1705
 
1706
            if (mEnabled && !hd)
1707
                makeElement(instance);
1708
 
1709
            return true;
1710
        }
1711
    }
1712
 
1713
    // Check whether it is a supported style or not. If the style is not
1714
    // supported, it will be ignored.
1715
    if (instance < 0)
1716
    {
1717
        bs = st;
1718
 
1719
        for (size_t i = 0; i < sr.size(); ++i)
1720
            sr[i].bs = st;
1721
    }
1722
    else
1723
    {
1724
        sr[instance].bs = st;
1725
 
1726
        if (bs != st)
1727
            bs.clear();
1728
    }
1729
 
1730
    mChanged = true;
1731
 
1732
    if (mEnabled && !hd)
1733
        makeElement(instance);
1734
 
1735
    return true;
1736
}
1737
 
106 andreas 1738
string TButton::getBorderStyle(int instance)
1739
{
1740
    DECL_TRACER("TButton::getBorderStyle(int instance)");
1741
 
1742
    if (instance < 0 || instance >= (int)sr.size())
1743
    {
1744
        MSG_ERROR("Invalid instance " << (instance + 1) << " submitted!");
1745
        return string();
1746
    }
1747
 
332 andreas 1748
    if (sr[instance].bs.empty())
1749
        return bs;
1750
 
106 andreas 1751
    return sr[instance].bs;
1752
}
1753
 
60 andreas 1754
bool TButton::setBargraphUpperLimit(int limit)
1755
{
1756
    DECL_TRACER("TButton::setBargraphUpperLimit(int limit)");
1757
 
1758
    if (limit < 1 || limit > 65535)
1759
    {
1760
        MSG_ERROR("Invalid upper limit " << limit);
1761
        return false;
1762
    }
1763
 
1764
    rh = limit;
1765
    return true;
1766
}
1767
 
1768
bool TButton::setBargraphLowerLimit(int limit)
1769
{
1770
    DECL_TRACER("TButton::setBargraphLowerLimit(int limit)");
1771
 
1772
    if (limit < 1 || limit > 65535)
1773
    {
1774
        MSG_ERROR("Invalid lower limit " << limit);
1775
        return false;
1776
    }
1777
 
1778
    rl = limit;
1779
    return true;
1780
}
1781
 
108 andreas 1782
bool TButton::setBargraphSliderColor(const string& color)
1783
{
1784
    DECL_TRACER("TButton::setBargraphSliderColor(const string& color, int inst)");
1785
 
1786
    if (!TColor::isValidAMXcolor(color))
1787
    {
1788
        MSG_PROTOCOL("Invalid color >" << color << "< ignored!");
1789
        return false;
1790
    }
1791
 
175 andreas 1792
    if (sc != color)
1793
        mChanged = true;
1794
 
108 andreas 1795
    sc = color;
1796
 
1797
    if (visible)
1798
        refresh();
1799
 
1800
    return true;
1801
}
1802
 
361 andreas 1803
bool TButton::setBargraphSliderName(const string& name)
1804
{
1805
    DECL_TRACER("TButton::setBargraphSliderName(const string& name)");
1806
 
1807
    if (name.empty())
1808
        return false;
1809
 
1810
    if (!gPageManager || !gPageManager->getSystemDraw()->existSlider(name))
1811
    {
1812
        MSG_ERROR("The slider " << name << " doesn't exist!");
1813
        return false;
1814
    }
1815
 
1816
    if (name == sd)
1817
        return true;
1818
 
1819
    sd = name;
1820
    mChanged = true;
1821
 
1822
    if (visible)
1823
        refresh();
1824
 
1825
    return true;
1826
}
1827
 
167 andreas 1828
bool TButton::setFontFileName(const string& name, int /*size*/, int instance)
149 andreas 1829
{
1830
    DECL_TRACER("TButton::setFontFileName(const string& name, int size)");
1831
 
1832
    if (name.empty() || !mFonts)
334 andreas 1833
    {
1834
#if TESTMODE == 1
1835
        setScreenDone();
1836
#endif
149 andreas 1837
        return false;
334 andreas 1838
    }
149 andreas 1839
 
165 andreas 1840
    if ((size_t)instance >= sr.size())
334 andreas 1841
    {
1842
#if TESTMODE == 1
1843
        setScreenDone();
1844
#endif
149 andreas 1845
        return false;
334 andreas 1846
    }
149 andreas 1847
 
1848
    int id = mFonts->getFontIDfromFile(name);
1849
 
1850
    if (id == -1)
334 andreas 1851
    {
1852
#if TESTMODE == 1
1853
        setScreenDone();
1854
#endif
149 andreas 1855
        return false;
334 andreas 1856
    }
149 andreas 1857
 
334 andreas 1858
    if (instance < 0)
1859
    {
1860
        for (size_t i = 0; i < sr.size(); ++i)
1861
        {
1862
            if (sr[i].fi != id)
1863
                mChanged = true;
165 andreas 1864
 
334 andreas 1865
            sr[i].fi = id;
1866
        }
1867
    }
1868
    else if (sr[instance].fi != id)
149 andreas 1869
    {
334 andreas 1870
        mChanged = true;
1871
        sr[instance].fi = id;
149 andreas 1872
    }
334 andreas 1873
#if TESTMODE == 1
1874
    setScreenDone();
1875
#endif
1876
    return true;
1877
}
165 andreas 1878
 
334 andreas 1879
bool TButton::setFontName(const string &name, int instance)
1880
{
1881
    DECL_TRACER("TButton::setFontName(const string &name, int instance)");
1882
 
1883
    if (name.empty() || !mFonts)
165 andreas 1884
    {
334 andreas 1885
#if TESTMODE == 1
1886
        setScreenDone();
1887
#endif
1888
        return false;
1889
    }
175 andreas 1890
 
334 andreas 1891
    if ((size_t)instance >= sr.size())
1892
    {
1893
#if TESTMODE == 1
1894
        setScreenDone();
1895
#endif
1896
        return false;
165 andreas 1897
    }
149 andreas 1898
 
334 andreas 1899
    int id = mFonts->getFontIDfromName(name);
1900
 
1901
    if (id == -1)
1902
    {
1903
#if TESTMODE == 1
1904
        setScreenDone();
1905
#endif
1906
        return false;
1907
    }
1908
 
1909
    if (instance < 0)
1910
    {
1911
        for (size_t i = 0; i < sr.size(); ++i)
1912
        {
1913
            if (sr[i].fi != id)
1914
                mChanged = true;
1915
 
1916
            sr[i].fi = id;
1917
        }
1918
    }
1919
    else if (sr[instance].fi != id)
1920
    {
1921
        mChanged = true;
1922
        sr[instance].fi = id;
1923
    }
1924
#if TESTMODE == 1
1925
    setScreenDone();
1926
#endif
149 andreas 1927
    return true;
1928
}
1929
 
16 andreas 1930
bool TButton::setBitmap(const string& file, int instance)
1931
{
1932
    DECL_TRACER("TButton::setBitmap(const string& file, int instance)");
1933
 
165 andreas 1934
    if (instance >= (int)sr.size())
16 andreas 1935
    {
1936
        MSG_ERROR("Invalid parameters!");
1937
        return false;
1938
    }
1939
 
165 andreas 1940
    int inst = instance;
1941
    int loop = 1;
104 andreas 1942
 
165 andreas 1943
    if (inst < 0)
1944
    {
1945
        loop = (int)sr.size();
1946
        inst = 0;
1947
    }
16 andreas 1948
 
165 andreas 1949
    for (int i = 0; i < loop; ++i)
1950
    {
1951
        if (sr[inst].bm == file)
1952
        {
1953
            inst++;
1954
            continue;
1955
        }
16 andreas 1956
 
175 andreas 1957
        mChanged = true;
165 andreas 1958
        sr[inst].bm = file;
1959
 
1960
        if (!file.empty() && !TImgCache::existBitmap(file, _BMTYPE_BITMAP))
1961
        {
1962
            sk_sp<SkData> image;
1963
            SkBitmap bm;
1964
 
1965
            image = readImage(file);
1966
 
1967
            if (image)
1968
            {
1969
                DecodeDataToBitmap(image, &bm);
1970
 
1971
                if (!bm.empty())
167 andreas 1972
                {
165 andreas 1973
                    TImgCache::addImage(sr[inst].bm, bm, _BMTYPE_BITMAP);
167 andreas 1974
                    sr[inst].bm_width = bm.info().width();
1975
                    sr[inst].bm_height = bm.info().height();
1976
                }
165 andreas 1977
            }
1978
        }
1979
 
1980
        inst++;
1981
    }
1982
 
40 andreas 1983
    if (!createButtons(true))   // We're forcing the image to load
16 andreas 1984
        return false;
1985
 
1986
    return makeElement(instance);
1987
}
1988
 
104 andreas 1989
bool TButton::setCameleon(const string& file, int instance)
1990
{
1991
    DECL_TRACER("TButton::setCameleon(const string& file, int instance)");
1992
 
1993
    if (file.empty() || instance >= (int)sr.size())
1994
    {
1995
        MSG_ERROR("Invalid parameters!");
1996
        return false;
1997
    }
1998
 
165 andreas 1999
    int inst = instance;
2000
    int loop = 1;
2001
 
2002
    if (inst < 0)
2003
    {
2004
        loop = (int)sr.size();
2005
        inst = 0;
2006
    }
2007
 
2008
    for (int i = 0; i < loop; ++i)
2009
    {
2010
        if (sr[inst].mi == file)
2011
        {
2012
            inst++;
2013
            continue;
2014
        }
2015
 
175 andreas 2016
        mChanged = true;
165 andreas 2017
        sr[inst].mi = file;
167 andreas 2018
 
2019
        if (!file.empty() && !TImgCache::existBitmap(file, _BMTYPE_CHAMELEON))
2020
        {
2021
            sk_sp<SkData> image;
2022
            SkBitmap bm;
2023
 
2024
            image = readImage(file);
2025
 
2026
            if (image)
2027
            {
2028
                DecodeDataToBitmap(image, &bm);
2029
 
2030
                if (!bm.empty())
2031
                {
2032
                    TImgCache::addImage(sr[inst].mi, bm, _BMTYPE_CHAMELEON);
2033
                    sr[inst].mi_width = bm.info().width();
2034
                    sr[inst].mi_height = bm.info().height();
2035
                }
2036
            }
2037
        }
2038
 
165 andreas 2039
        inst++;
2040
    }
2041
 
104 andreas 2042
    if (!createButtons(true))   // We're forcing the image to load
2043
        return false;
2044
 
2045
    return makeElement(instance);
2046
}
2047
 
224 andreas 2048
bool TButton::setInputMask(const std::string& mask)
2049
{
2050
    DECL_TRACER("TButton::setInputMask(const std::string& mask)");
2051
 
2052
    vector<char> mTable = { '0', '9', '#', 'L', '?', 'A', 'a', '&', 'C',
2053
                            '[', ']', '|', '{', '}', '<', '>', '^' };
2054
    vector<char>::iterator iter;
2055
 
2056
    for (size_t i = 0; i < mask.length(); ++i)
2057
    {
2058
        bool found = false;
2059
 
2060
        for (iter = mTable.begin(); iter != mTable.end(); ++iter)
2061
        {
2062
            if (mask[i] == *iter)
2063
            {
2064
                found = true;
2065
                break;
2066
            }
2067
        }
2068
 
2069
        if (!found)
2070
        {
2071
            MSG_WARNING("The mask letter " << mask[i] << " is invalid!");
351 andreas 2072
#if TESTMODE == 1
2073
            setScreenDone();
2074
#endif
224 andreas 2075
            return false;
2076
        }
2077
    }
2078
 
2079
    im = mask;
351 andreas 2080
#if TESTMODE == 1
2081
    __success = true;
2082
    setScreenDone();
2083
#endif
224 andreas 2084
    return true;
2085
}
2086
 
51 andreas 2087
void TButton::setActiveInstance(int inst)
2088
{
2089
    DECL_TRACER("TButton::setActiveInstance()");
2090
 
2091
    if (inst < 0 || (size_t)inst >= sr.size())
2092
        return;
2093
 
175 andreas 2094
    if (mActInstance != inst)
2095
        mChanged = true;
2096
 
51 andreas 2097
    mActInstance = inst;
2098
}
2099
 
300 andreas 2100
SUBVIEW_POSITION_t TButton::getSubViewAnchor()
2101
{
2102
    DECL_TRACER("TButton::getSubViewAnchor()");
2103
 
2104
    if (we.empty())
2105
        return SVP_CENTER;
2106
    else if (strCaseCompare(we, "l/t") == 0)
2107
        return SVP_LEFT_TOP;
2108
    else if (strCaseCompare(we, "r/b") == 0)
2109
        return SVP_RIGHT_BOTTOM;
2110
 
2111
    return SVP_CENTER;
2112
}
2113
 
110 andreas 2114
bool TButton::getDynamic(int inst)
2115
{
2116
    DECL_TRACER("TButton::getDynamic(int inst)");
2117
 
2118
    if (inst < 0 || inst >= (int)sr.size())
2119
    {
2120
        MSG_ERROR("Instance " << inst << " does not exist!");
2121
        return false;
2122
    }
2123
 
2124
    return sr[inst].dynamic;
2125
}
2126
 
107 andreas 2127
void TButton::setDynamic(int d, int inst)
2128
{
2129
    DECL_TRACER("TButton::setDynamic(int d, int inst)");
2130
 
2131
    if (inst >= (int)sr.size())
2132
    {
2133
        MSG_ERROR("Instance is out of size!");
2134
        return;
2135
    }
2136
 
2137
    bool dyn = (d != 0) ? true : false;
2138
 
2139
    if (inst < 0)
2140
    {
2141
        vector<SR_T>::iterator iter;
2142
        int instance = 0;
2143
 
2144
        for (iter = sr.begin(); iter != sr.end(); ++iter)
2145
        {
2146
            bool old = iter->dynamic;
2147
            iter->dynamic = dyn;
2148
 
2149
            if (old && old != dyn && mActInstance == instance)
2150
            {
2151
                THR_REFRESH_t *thref = _findResource(mHandle, getParent(), bi);
2152
 
2153
                if (thref)
2154
                {
2155
                    TImageRefresh *mImageRefresh = thref->mImageRefresh;
2156
 
2157
                    if (mImageRefresh)
2158
                        mImageRefresh->stop();
2159
                }
2160
 
175 andreas 2161
                mChanged = true;
107 andreas 2162
                makeElement(instance);
2163
            }
2164
 
2165
            instance++;
2166
        }
2167
    }
2168
    else
2169
    {
2170
        bool old = sr[inst].dynamic;
2171
        sr[inst].dynamic = dyn;
2172
 
2173
        if (old && old != dyn && mActInstance == inst)
2174
        {
2175
            THR_REFRESH_t *thref = _findResource(mHandle, getParent(), bi);
2176
 
2177
            if (thref)
2178
            {
2179
                TImageRefresh *mImageRefresh = thref->mImageRefresh;
2180
 
2181
                if (mImageRefresh)
2182
                    mImageRefresh->stop();
2183
            }
2184
 
175 andreas 2185
            mChanged = true;
107 andreas 2186
            makeElement(inst);
2187
        }
2188
    }
2189
}
2190
 
110 andreas 2191
int TButton::getOpacity(int inst)
2192
{
2193
    DECL_TRACER("TButoon::getOpacity(int inst)");
2194
 
2195
    if (inst < 0 || inst >= (int)sr.size())
2196
    {
2197
        MSG_ERROR("Instance " << inst << " does not exist!");
2198
        return 0;
2199
    }
2200
 
2201
    return sr[inst].oo;
2202
}
2203
 
16 andreas 2204
bool TButton::setOpacity(int op, int instance)
2205
{
2206
    DECL_TRACER("TButton::setOpacity(int op, int instance)");
2207
 
167 andreas 2208
    if (instance >= 0 && (size_t)instance >= sr.size())
16 andreas 2209
    {
2210
        MSG_ERROR("Instance " << instance << " does not exist!");
335 andreas 2211
#if TESTMODE == 1
2212
        setScreenDone();
2213
#endif
16 andreas 2214
        return false;
2215
    }
2216
 
335 andreas 2217
    if (op < 0 || op > 255)
16 andreas 2218
    {
2219
        MSG_ERROR("Invalid opacity " << op << "!");
335 andreas 2220
#if TESTMODE == 1
2221
        setScreenDone();
2222
#endif
16 andreas 2223
        return false;
2224
    }
2225
 
335 andreas 2226
    if (instance < 0)
2227
    {
2228
        for (size_t i = 0; i < sr.size(); ++i)
2229
        {
2230
            if (sr[i].oo == op)
2231
                continue;
165 andreas 2232
 
335 andreas 2233
            sr[i].oo = op;
2234
            mChanged = true;
2235
        }
2236
    }
2237
    else if (sr[instance].oo != op)
165 andreas 2238
    {
335 andreas 2239
        sr[instance].oo = op;
2240
        mChanged = true;
165 andreas 2241
    }
2242
 
335 andreas 2243
    if (!mChanged)
165 andreas 2244
    {
335 andreas 2245
#if TESTMODE == 1
2246
        __success = true;
2247
        setScreenDone();
2248
#endif
2249
        return true;
165 andreas 2250
    }
2251
 
16 andreas 2252
    return makeElement(instance);
2253
}
2254
 
2255
bool TButton::setFont(int id, int instance)
2256
{
2257
    DECL_TRACER("TButton::setFont(int id)");
2258
 
205 andreas 2259
    if (!setFontOnly(id, instance))
2260
        return false;
2261
 
2262
    return makeElement(instance);
2263
}
2264
 
2265
bool TButton::setFontOnly(int id, int instance)
2266
{
2267
    DECL_TRACER("TButton::setFontOnly(int id)");
2268
 
167 andreas 2269
    if (instance >= 0 && (size_t)instance >= sr.size())
16 andreas 2270
    {
2271
        MSG_ERROR("Instance " << instance << " does not exist!");
2272
        return false;
2273
    }
2274
 
342 andreas 2275
    if (instance < 0)
165 andreas 2276
    {
342 andreas 2277
        for (size_t i = 0; i < sr.size(); ++i)
2278
        {
2279
            if (sr[i].fi != id)
2280
            {
2281
                mChanged = true;
2282
                sr[i].fi = id;
2283
            }
2284
        }
165 andreas 2285
    }
342 andreas 2286
    else if (sr[instance].fi != id)
165 andreas 2287
    {
175 andreas 2288
        mChanged = true;
342 andreas 2289
        sr[instance].fi = id;
165 andreas 2290
    }
2291
 
205 andreas 2292
    return true;
16 andreas 2293
}
2294
 
2295
void TButton::setLeft(int left)
2296
{
2297
    DECL_TRACER("TButton::setLeft(int left)");
2298
 
2299
    if (left < 0)
2300
        return;
2301
 
175 andreas 2302
    if (lt != left)
2303
        mChanged = true;
2304
 
16 andreas 2305
    lt = left;
2306
    makeElement(mActInstance);
2307
}
2308
 
2309
void TButton::setTop(int top)
2310
{
2311
    DECL_TRACER("TButton::setTop(int top)");
2312
 
2313
    if (top < 0)
2314
        return;
2315
 
175 andreas 2316
    if (tp != top)
2317
        mChanged = true;
2318
 
16 andreas 2319
    tp = top;
2320
    makeElement(mActInstance);
2321
}
2322
 
2323
void TButton::setLeftTop(int left, int top)
2324
{
2325
    DECL_TRACER("TButton::setLeftTop(int left, int top)");
2326
 
2327
    if (top < 0 || left < 0)
2328
        return;
2329
 
175 andreas 2330
    if (lt != left || tp != top)
2331
        mChanged = true;
341 andreas 2332
    else
2333
        return;
175 andreas 2334
 
16 andreas 2335
    lt = left;
2336
    tp = top;
2337
    makeElement(mActInstance);
2338
}
2339
 
152 andreas 2340
void TButton::setRectangle(int left, int top, int right, int bottom)
2341
{
2342
    DECL_TRACER("setRectangle(int left, int top, int right, int bottom)");
2343
 
2344
    if (!gPageManager)
2345
        return;
2346
 
217 andreas 2347
    int screenWidth = gPageManager->getSettings()->getWidth();
152 andreas 2348
    int screenHeight = gPageManager->getSettings()->getHeight();
2349
    int width = right - left;
2350
    int height = bottom - top;
2351
 
2352
    if (left >= 0 && right > left && (left + width) < screenWidth)
2353
        lt = left;
2354
 
2355
    if (top >= 0 && bottom > top && (top + height) < screenHeight)
2356
        tp = top;
2357
 
2358
    if (left >= 0 && right > left)
2359
        wt = width;
2360
 
2361
    if (top >= 0 && bottom > top)
2362
        ht = height;
2363
}
2364
 
334 andreas 2365
void TButton::getRectangle(int *left, int *top, int *height, int *width)
2366
{
2367
    DECL_TRACER("TButton::getRectangle(int *left, int *top, int *height, int *width)");
2368
 
2369
    if (left)
2370
        *left = lt;
2371
 
2372
    if (top)
2373
        *top = tp;
2374
 
2375
    if (height)
2376
        *height = ht;
2377
 
2378
    if (width)
2379
        *width = wt;
2380
 
2381
}
2382
 
21 andreas 2383
void TButton::setResourceName(const string& name, int instance)
2384
{
2385
    DECL_TRACER("TButton::setResourceName(const string& name, int instance)");
2386
 
165 andreas 2387
    if (instance >= (int)sr.size())
21 andreas 2388
    {
2389
        MSG_ERROR("Invalid instance " << instance);
2390
        return;
2391
    }
2392
 
165 andreas 2393
    int inst = instance;
2394
    int loop = 1;
2395
 
2396
    if (inst < 0)
21 andreas 2397
    {
165 andreas 2398
        loop = (int)sr.size();
2399
        inst = 0;
21 andreas 2400
    }
2401
 
165 andreas 2402
    for (int i = 0; i < loop; ++i)
2403
    {
2404
        if (!sr[inst].dynamic)
2405
        {
2406
            inst++;
2407
            continue;
2408
        }
2409
 
176 andreas 2410
        if (sr[inst].bm != name)
2411
            mChanged = true;
2412
 
165 andreas 2413
        sr[inst].bm = name;
2414
        inst++;
2415
    }
21 andreas 2416
}
2417
 
106 andreas 2418
int TButton::getBitmapJustification(int* x, int* y, int instance)
104 andreas 2419
{
106 andreas 2420
    DECL_TRACER("TButton::getBitmapJustification(int* x, int* y, int instance)");
104 andreas 2421
 
106 andreas 2422
    if (instance < 0 || instance >= (int)sr.size())
2423
    {
2424
        MSG_ERROR("Invalid instance " << (instance + 1));
2425
        return -1;
2426
    }
2427
 
2428
    if (x)
334 andreas 2429
        *x = sr[instance].jb == 0 ? sr[instance].bx : 0;
106 andreas 2430
 
2431
    if (y)
334 andreas 2432
        *y = sr[instance].jb == 0 ? sr[instance].by : 0;
106 andreas 2433
 
2434
    return sr[instance].jb;
2435
}
2436
 
2437
void TButton::setBitmapJustification(int j, int x, int y, int instance)
2438
{
2439
    DECL_TRACER("TButton::setBitmapJustification(int j, int instance)");
2440
 
165 andreas 2441
    if (j < 0 || j > 9 || instance >= (int)sr.size())
334 andreas 2442
    {
2443
#if TESTMODE == 1
2444
        setScreenDone();
2445
#endif
104 andreas 2446
        return;
334 andreas 2447
    }
104 andreas 2448
 
106 andreas 2449
    if (instance < 0)
104 andreas 2450
    {
2451
        for (size_t i = 0; i < sr.size(); i++)
2452
        {
176 andreas 2453
            if (sr[i].jb != j)
2454
                mChanged = true;
2455
 
104 andreas 2456
            sr[i].jb = j;
2457
 
2458
            if (j == 0)
2459
            {
2460
                sr[i].bx = x;
2461
                sr[i].by = y;
2462
            }
2463
        }
2464
    }
2465
    else
2466
    {
176 andreas 2467
        if (sr[instance].jb != j)
2468
            mChanged = true;
2469
 
104 andreas 2470
        sr[instance].jb = j;
2471
 
2472
        if (j == 0)
2473
        {
2474
            sr[instance].bx = x;
2475
            sr[instance].by = y;
2476
        }
2477
    }
110 andreas 2478
 
2479
    makeElement();
104 andreas 2480
}
2481
 
106 andreas 2482
int TButton::getIconJustification(int* x, int* y, int instance)
2483
{
2484
    DECL_TRACER("TButton::getIconJustification(int* x, int* y, int instance)");
2485
 
2486
    if (instance < 0 || instance >= (int)sr.size())
2487
    {
2488
        MSG_ERROR("Invalid instance " << (instance + 1));
2489
        return -1;
2490
    }
2491
 
2492
    if (x)
334 andreas 2493
        *x = sr[instance].ji == 0 ? sr[instance].ix : 0;
106 andreas 2494
 
2495
    if (y)
334 andreas 2496
        *y = sr[instance].ji == 0 ? sr[instance].iy : 0;
106 andreas 2497
 
2498
    return sr[instance].ji;
2499
}
2500
 
104 andreas 2501
void TButton::setIconJustification(int j, int x, int y, int instance)
2502
{
2503
    DECL_TRACER("TButton::setIconJustification(int j, int x, int y, int instance)");
2504
 
165 andreas 2505
    if (j < 0 || j > 9 || instance >= (int)sr.size())
334 andreas 2506
    {
2507
#if TESTMODE == 1
2508
        setScreenDone();
2509
#endif
104 andreas 2510
        return;
334 andreas 2511
    }
104 andreas 2512
 
106 andreas 2513
    if (instance < 0)
104 andreas 2514
    {
2515
        for (size_t i = 0; i < sr.size(); i++)
2516
        {
176 andreas 2517
            if (sr[i].ji != j)
2518
                mChanged = true;
2519
 
104 andreas 2520
            sr[i].ji = j;
2521
 
2522
            if (j == 0)
2523
            {
2524
                sr[i].ix = x;
2525
                sr[i].iy = y;
2526
            }
2527
        }
2528
    }
2529
    else
2530
    {
176 andreas 2531
        if (sr[instance].ji != j)
2532
            mChanged = true;
2533
 
104 andreas 2534
        sr[instance].ji = j;
2535
 
2536
        if (j == 0)
2537
        {
2538
            sr[instance].ix = x;
2539
            sr[instance].iy = y;
2540
        }
2541
    }
110 andreas 2542
 
2543
    makeElement();
104 andreas 2544
}
2545
 
106 andreas 2546
int TButton::getTextJustification(int* x, int* y, int instance)
2547
{
2548
    DECL_TRACER("TButton::getTextJustification(int* x, int* y, int instance)");
2549
 
2550
    if (instance < 0 || instance >= (int)sr.size())
2551
    {
2552
        MSG_ERROR("Invalid instance " << (instance + 1));
2553
        return -1;
2554
    }
2555
 
2556
    if (x)
334 andreas 2557
        *x = sr[instance].jt == 0 ? sr[instance].tx : 0;
106 andreas 2558
 
2559
    if (y)
334 andreas 2560
        *y = sr[instance].jt == 0 ? sr[instance].ty : 0;
106 andreas 2561
 
2562
    return sr[instance].jt;
2563
}
2564
 
104 andreas 2565
void TButton::setTextJustification(int j, int x, int y, int instance)
2566
{
2567
    DECL_TRACER("TButton::setTextJustification(int j, int x, int y, int instance)");
2568
 
205 andreas 2569
    if (!setTextJustificationOnly(j, x, y, instance))
334 andreas 2570
    {
2571
#if TESTMODE == 1
2572
        setScreenDone();
2573
#endif
104 andreas 2574
        return;
334 andreas 2575
    }
104 andreas 2576
 
205 andreas 2577
    makeElement();
2578
}
2579
 
2580
bool TButton::setTextJustificationOnly(int j, int x, int y, int instance)
2581
{
2582
    DECL_TRACER("TButton::setTextJustificationOnly(int j, int x, int y, int instance)");
2583
 
2584
    if (j < 0 || j > 9 || instance >= (int)sr.size())
2585
        return false;
2586
 
106 andreas 2587
    if (instance < 0)
104 andreas 2588
    {
2589
        for (size_t i = 0; i < sr.size(); i++)
2590
        {
176 andreas 2591
            if (sr[i].jt != j)
2592
                mChanged = true;
2593
 
104 andreas 2594
            sr[i].jt = (TEXT_ORIENTATION)j;
2595
 
2596
            if (j == 0)
2597
            {
2598
                sr[i].tx = x;
2599
                sr[i].ty = y;
2600
            }
2601
        }
2602
    }
2603
    else
2604
    {
176 andreas 2605
        if (sr[instance].jt != j)
2606
            mChanged = true;
2607
 
104 andreas 2608
        sr[instance].jt = (TEXT_ORIENTATION)j;
2609
 
2610
        if (j == 0)
2611
        {
2612
            sr[instance].tx = x;
2613
            sr[instance].ty = y;
2614
        }
2615
    }
110 andreas 2616
 
205 andreas 2617
    return true;
104 andreas 2618
}
2619
 
110 andreas 2620
string TButton::getText(int inst)
2621
{
2622
    DECL_TRACER("TButton::getText(int inst)");
2623
 
2624
    if (inst < 0 || inst >= (int)sr.size())
2625
    {
2626
        MSG_ERROR("Instance " << inst << " does not exist!");
2627
        return string();
2628
    }
2629
 
2630
    return sr[inst].te;
2631
}
2632
 
2633
string TButton::getTextColor(int inst)
2634
{
2635
    DECL_TRACER("TButton::getTextColor(int const)");
2636
 
2637
    if (inst < 0 || inst >= (int)sr.size())
2638
    {
2639
        MSG_ERROR("Instance " << inst << " does not exist!");
2640
        return string();
2641
    }
2642
 
2643
    return sr[inst].ct;
2644
}
2645
 
2646
string TButton::getTextEffectColor(int inst)
2647
{
2648
    DECL_TRACER ("TButton::getTextEffectColor(int inst)");
2649
 
2650
    if (inst < 0 || inst >= (int)sr.size())
2651
    {
2652
        MSG_ERROR("Instance " << inst << " does not exist!");
2653
        return string();
2654
    }
2655
 
2656
    return sr[inst].ec;
2657
}
2658
 
165 andreas 2659
void TButton::setTextEffectColor(const string& ec, int instance)
108 andreas 2660
{
2661
    DECL_TRACER("TButton::setTextEffectColor(const string& ec, int inst)");
2662
 
205 andreas 2663
    if (!setTextEffectColorOnly(ec, instance))
2664
        return;
2665
 
2666
    if (visible)
2667
        makeElement();
2668
}
2669
 
2670
bool TButton::setTextEffectColorOnly(const string& ec, int instance)
2671
{
2672
    DECL_TRACER("TButton::setTextEffectColorOnly(const string& ec, int inst)");
2673
 
165 andreas 2674
    if ((size_t)instance >= sr.size())
108 andreas 2675
    {
165 andreas 2676
        MSG_ERROR("Instance " << instance << " does not exist!");
205 andreas 2677
        return false;
108 andreas 2678
    }
2679
 
2680
    if (!TColor::isValidAMXcolor(ec))
2681
    {
2682
        MSG_PROTOCOL("Invalid color >" << ec << "< ignored!");
205 andreas 2683
        return false;
108 andreas 2684
    }
2685
 
165 andreas 2686
    int inst = instance;
2687
    int loop = 1;
108 andreas 2688
 
2689
    if (inst < 0)
2690
    {
165 andreas 2691
        loop = (int)sr.size();
2692
        inst = 0;
108 andreas 2693
    }
165 andreas 2694
 
2695
    for (int i = 0; i < loop; ++i)
2696
    {
2697
        if (sr[inst].ec.compare(ec) == 0)
2698
        {
2699
            inst++;
2700
            continue;
2701
        }
2702
 
108 andreas 2703
        sr[inst].ec = ec;
176 andreas 2704
        mChanged = true;
165 andreas 2705
        inst++;
2706
    }
108 andreas 2707
 
205 andreas 2708
    return true;
108 andreas 2709
}
2710
 
110 andreas 2711
int TButton::getTextEffect(int inst)
16 andreas 2712
{
110 andreas 2713
    DECL_TRACER("TButton::getTextEffect(int inst)");
2714
 
2715
    if (inst < 0 || inst >= (int)sr.size())
2716
    {
2717
        MSG_ERROR("Instance " << inst << " does not exist!");
2718
        return 0;
2719
    }
2720
 
2721
    return sr[inst].et;
2722
}
2723
 
2724
void TButton::setTextEffect(int et, int inst)
2725
{
2726
    DECL_TRACER("TButton::setTextEffect(bool et, int inst)");
2727
 
2728
    if (inst >= (int)sr.size())
2729
    {
2730
        MSG_ERROR("instance " << inst << " is out of bounds!");
2731
        return;
2732
    }
2733
 
2734
    if (inst < 0)
2735
    {
2736
        for (size_t i = 0; i < sr.size(); i++)
176 andreas 2737
        {
2738
            if (sr[i].et != et)
2739
                mChanged = true;
2740
 
110 andreas 2741
            sr[i].et = et;
176 andreas 2742
        }
110 andreas 2743
    }
2744
    else
176 andreas 2745
    {
2746
        if (sr[inst].et != et)
2747
            mChanged = true;
2748
 
110 andreas 2749
        sr[inst].et = et;
176 andreas 2750
    }
110 andreas 2751
 
2752
    makeElement();
2753
}
2754
 
2755
string TButton::getTextEffectName(int inst)
2756
{
2757
    DECL_TRACER("TButton::getTextEffectName(int inst)");
2758
 
2759
    if (inst < 0 || inst >= (int)sr.size())
2760
        return string();
2761
 
2762
    int idx = 0;
2763
 
2764
    while (sysTefs[idx].idx)
2765
    {
2766
        if (sysTefs[idx].idx == sr[inst].et)
2767
            return sysTefs[idx].name;
2768
 
2769
        idx++;
2770
    }
2771
 
2772
    return string();
2773
}
2774
 
2775
void TButton::setTextEffectName(const string& name, int inst)
2776
{
2777
    DECL_TRACER("TButton::setTextEffectName(const string& name, int inst)");
2778
 
2779
    if (inst >= (int)sr.size())
2780
        return;
2781
 
2782
    int idx = 0;
2783
 
2784
    while (sysTefs[idx].idx)
2785
    {
2786
        if (strCaseCompare(sysTefs[idx].name, name) == 0)
2787
        {
2788
            if (inst < 0)
2789
            {
2790
                for (size_t i = 0; i < sr.size(); i++)
176 andreas 2791
                {
2792
                    if (sr[i].et != sysTefs[idx].idx)
2793
                        mChanged = true;
2794
 
110 andreas 2795
                    sr[i].et = sysTefs[idx].idx;
176 andreas 2796
                }
110 andreas 2797
            }
2798
            else
176 andreas 2799
            {
2800
                if (sr[inst].et != sysTefs[idx].idx)
2801
                    mChanged = true;
2802
 
110 andreas 2803
                sr[inst].et = sysTefs[idx].idx;
176 andreas 2804
            }
110 andreas 2805
 
2806
            makeElement();
2807
            break;
2808
        }
2809
 
2810
        idx++;
2811
    }
2812
}
2813
 
2814
string TButton::getBitmapName(int inst)
2815
{
2816
    DECL_TRACER("TButton::getBitmapName(int inst)");
2817
 
2818
    if (inst < 0 || inst >= (int)sr.size())
2819
    {
2820
        MSG_ERROR("Instance " << inst << " does not exist!");
2821
        return string();
2822
    }
2823
 
2824
    return sr[inst].bm;
2825
}
2826
 
2827
string TButton::getFillColor(int inst)
2828
{
2829
    DECL_TRACER("TButton::getFillColor(int inst)");
2830
 
2831
    if (inst < 0 || inst >= (int)sr.size())
2832
    {
2833
        MSG_ERROR("Instance " << inst << " does not exist!");
2834
        return string();
2835
    }
2836
 
2837
    return sr[inst].cf;
2838
}
2839
 
2840
bool TButton::setTextWordWrap(bool state, int instance)
2841
{
16 andreas 2842
    DECL_TRACER("TButton::setWorWrap(bool state, int instance)");
2843
 
110 andreas 2844
    if (instance >= (int)sr.size())
16 andreas 2845
    {
2846
        MSG_ERROR("Invalid instance " << instance);
2847
        return false;
2848
    }
2849
 
176 andreas 2850
    int stt = state ? 1 : 0;
2851
 
110 andreas 2852
    if (instance < 0)
2853
    {
2854
        for (size_t i = 0; i < sr.size(); i++)
176 andreas 2855
        {
2856
            if (sr[i].ww != stt)
2857
                mChanged = true;
2858
 
2859
            sr[i].ww = stt;
2860
        }
110 andreas 2861
    }
2862
    else
176 andreas 2863
    {
2864
        if (sr[instance].ww != stt)
2865
            mChanged = true;
110 andreas 2866
 
176 andreas 2867
        sr[instance].ww = stt;
2868
    }
2869
 
16 andreas 2870
    return makeElement(instance);
2871
}
2872
 
110 andreas 2873
bool TButton::getTextWordWrap(int inst)
2874
{
2875
    DECL_TRACER("TButton::getTextWordWrap(int inst)");
2876
 
2877
    if (inst < 0 || inst >= (int)sr.size())
2878
    {
2879
        MSG_ERROR("Instance " << inst << " does not exist!");
2880
        return false;
2881
    }
2882
 
2883
    return (sr[inst].ww == 1);
2884
}
2885
 
2886
int TButton::getFontIndex(int inst)
2887
{
2888
    DECL_TRACER("TButton::getFontIndex(int inst)");
2889
 
2890
    if (inst < 0 || inst >= (int)sr.size())
2891
    {
2892
        MSG_ERROR("Instance " << inst << " does not exist!");
2893
        return 0;
2894
    }
2895
 
2896
    return sr[inst].fi;
2897
}
2898
 
165 andreas 2899
bool TButton::setFontIndex(int fi, int instance)
110 andreas 2900
{
2901
    DECL_TRACER("TButton::setFontIndex(int fi, int inst)");
2902
 
165 andreas 2903
    if (instance >= (int)sr.size())
110 andreas 2904
    {
165 andreas 2905
        MSG_ERROR("Invalid instance " << instance);
110 andreas 2906
        return false;
2907
    }
2908
 
165 andreas 2909
    int inst = instance;
2910
    int loop = 1;
2911
 
110 andreas 2912
    if (inst < 0)
2913
    {
165 andreas 2914
        loop = (int)sr.size();
2915
        inst = 0;
110 andreas 2916
    }
165 andreas 2917
 
2918
    for (int i = 0; i < loop; ++i)
2919
    {
176 andreas 2920
        if (sr[inst].fi != fi)
2921
            mChanged = true;
2922
 
110 andreas 2923
        sr[inst].fi = fi;
165 andreas 2924
        inst++;
2925
    }
110 andreas 2926
 
2927
    return makeElement(inst);
2928
}
2929
 
2930
int TButton::getIconIndex(int inst)
2931
{
2932
    DECL_TRACER("TButton::getIconIndex(int inst)");
2933
 
2934
    if (inst < 0 || inst >= (int)sr.size())
2935
    {
2936
        MSG_ERROR("Instance " << inst << " does not exist!");
2937
        return 0;
2938
    }
2939
 
2940
    return sr[inst].ii;
2941
}
2942
 
2943
string TButton::getSound(int inst)
2944
{
2945
    DECL_TRACER("TButton::getSound(int inst)");
2946
 
2947
    if (inst < 0 || inst >= (int)sr.size())
2948
    {
2949
        MSG_ERROR("Instance " << inst << " does not exist!");
2950
        return string();
2951
    }
2952
 
2953
    return sr[inst].sd;
2954
}
2955
 
2956
void TButton::setSound(const string& sound, int inst)
2957
{
2958
    DECL_TRACER("TButton::setSound(const string& sound, int inst)");
2959
 
2960
    if (inst >= (int)sr.size())
2961
    {
2962
        MSG_ERROR("Invalid instance " << inst);
2963
        return;
2964
    }
2965
 
2966
    if (inst < 0)
2967
    {
2968
        for (size_t i = 0; i < sr.size(); i++)
2969
            sr[i].sd = sound;
2970
    }
2971
    else
2972
        sr[inst].sd = sound;
335 andreas 2973
#if TESTMODE == 1
2974
    __success = true;
2975
    setScreenDone();
2976
#endif
110 andreas 2977
}
2978
 
320 andreas 2979
bool TButton::startAnimation(int st, int end, int time)
38 andreas 2980
{
2981
    DECL_TRACER("TButton::startAnimation(int start, int end, int time)");
2982
 
320 andreas 2983
    if (st > end || st < 0 || (size_t)end > sr.size() || time < 0)
38 andreas 2984
    {
320 andreas 2985
        MSG_ERROR("Invalid parameter: start=" << st << ", end=" << end << ", time=" << time);
38 andreas 2986
        return false;
2987
    }
2988
 
320 andreas 2989
    if (time <= 1)
153 andreas 2990
    {
2991
        int inst = end - 1;
2992
 
2993
        if (inst >= 0 && (size_t)inst < sr.size())
2994
        {
2995
            if (mActInstance != inst)
2996
            {
2997
                mActInstance = inst;
320 andreas 2998
                mChanged = true;
153 andreas 2999
                drawButton(inst);
3000
            }
3001
        }
3002
 
3003
        return true;
3004
    }
3005
 
320 andreas 3006
    int start = std::max(1, st);
3007
 
38 andreas 3008
    if (mAniRunning || mThrAni.joinable())
3009
    {
99 andreas 3010
        MSG_PROTOCOL("Animation is already running!");
38 andreas 3011
        return true;
3012
    }
3013
 
3014
    int number = end - start;
3015
    ulong stepTime = ((ulong)time * 10L) / (ulong)number;
3016
    mAniRunTime = (ulong)time * 10L;
3017
 
3018
    try
3019
    {
93 andreas 3020
        mAniStop = false;
38 andreas 3021
        mThrAni = thread([=] { runAnimationRange(start, end, stepTime); });
3022
        mThrAni.detach();
3023
    }
3024
    catch (exception& e)
3025
    {
3026
        MSG_ERROR("Error starting the button animation thread: " << e.what());
3027
        return false;
3028
    }
3029
 
3030
    return true;
3031
}
3032
 
22 andreas 3033
void TButton::_TimerCallback(ulong)
15 andreas 3034
{
3035
    mLastBlink.second++;
3036
    int months[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
3037
 
3038
    if ((mLastBlink.year % 4) == 0)
3039
        months[1] = 29;
3040
 
3041
    if (mLastBlink.second > 59)
3042
    {
3043
        mLastBlink.minute++;
3044
        mLastBlink.second = 0;
3045
 
3046
        if (mLastBlink.minute > 59)
3047
        {
3048
            mLastBlink.hour++;
3049
            mLastBlink.minute = 0;
3050
 
3051
            if (mLastBlink.hour >= 24)
3052
            {
3053
                mLastBlink.hour = 0;
3054
                mLastBlink.weekday++;
3055
                mLastBlink.day++;
3056
 
3057
                if (mLastBlink.weekday > 7)
3058
                    mLastBlink.weekday = 0;
3059
 
3060
                if (mLastBlink.day > months[mLastBlink.month-1])
3061
                {
3062
                    mLastBlink.day = 1;
3063
                    mLastBlink.month++;
3064
 
3065
                    if (mLastBlink.month > 12)
3066
                    {
3067
                        mLastBlink.year++;
3068
                        mLastBlink.month = 1;
3069
                    }
3070
                }
3071
            }
3072
        }
3073
    }
3074
 
3075
    funcTimer(mLastBlink);
3076
}
3077
 
21 andreas 3078
void TButton::_imageRefresh(const string& url)
3079
{
3080
    DECL_TRACER("TButton::_imageRefresh(const string& url)");
3081
 
38 andreas 3082
    if (prg_stopped || killed || !visible)
33 andreas 3083
        return;
35 andreas 3084
 
3085
    if (!gPrjResources)
3086
    {
3087
        MSG_WARNING("No resources available!");
3088
        return;
3089
    }
3090
 
21 andreas 3091
    ulong parent = mHandle & 0xffff0000;
3092
    getDrawOrder(sr[mActInstance]._do, (DRAW_ORDER *)&mDOrder);
3093
 
3094
    if (TError::isError())
3095
    {
3096
        TError::clear();
3097
        return;
3098
    }
3099
 
3100
    SkBitmap imgButton;
3101
 
254 andreas 3102
    if (!allocPixels(wt, ht, &imgButton))
3103
        return;
3104
 
21 andreas 3105
    for (int i = 0; i < ORD_ELEM_COUNT; i++)
3106
    {
3107
        if (mDOrder[i] == ORD_ELEM_FILL)
3108
        {
3109
            if (!buttonFill(&imgButton, mActInstance))
3110
                return;
3111
        }
3112
        else if (mDOrder[i] == ORD_ELEM_BITMAP)
3113
        {
3114
            RESOURCE_T resource = gPrjResources->findResource(sr[mActInstance].bm);
3115
 
3116
            if (resource.protocol.empty())
3117
            {
3118
                MSG_ERROR("Resource " << sr[mActInstance].bm << " not found!");
3119
                return;
3120
            }
3121
 
94 andreas 3122
            THTTPClient *WEBClient = nullptr;
3123
 
21 andreas 3124
            try
3125
            {
3126
                char *content = nullptr;
3127
                size_t length = 0, contentlen = 0;
3128
 
94 andreas 3129
                WEBClient = new THTTPClient;
3130
 
3131
                if (WEBClient && (content = WEBClient->tcall(&length, url, resource.user, resource.password)) == nullptr)
3132
                {
3133
                    if (WEBClient)
3134
                        delete WEBClient;
3135
 
21 andreas 3136
                    return;
94 andreas 3137
                }
21 andreas 3138
 
94 andreas 3139
                contentlen = WEBClient->getContentSize();
21 andreas 3140
 
3141
                if (content == nullptr)
3142
                {
3143
                    MSG_ERROR("Server returned no or invalid content!");
94 andreas 3144
                    delete WEBClient;
21 andreas 3145
                    return;
3146
                }
3147
 
3148
                sk_sp<SkData> data = SkData::MakeWithCopy(content, contentlen);
3149
 
3150
                if (!data)
3151
                {
3152
                    MSG_ERROR("Could not make an image!");
94 andreas 3153
                    delete WEBClient;
21 andreas 3154
                    return;
3155
                }
3156
 
3157
                SkBitmap image;
3158
 
3159
                if (!DecodeDataToBitmap(data, &image))
3160
                {
3161
                    MSG_ERROR("Error creating an image!");
94 andreas 3162
                    delete WEBClient;
21 andreas 3163
                    return;
3164
                }
3165
 
3166
                loadImage(&imgButton, image, mActInstance);
94 andreas 3167
                delete WEBClient;
21 andreas 3168
            }
3169
            catch (std::exception& e)
3170
            {
94 andreas 3171
                if (WEBClient)
3172
                    delete WEBClient;
3173
 
21 andreas 3174
                MSG_ERROR(e.what());
3175
                return;
3176
            }
93 andreas 3177
            catch(...)
3178
            {
94 andreas 3179
                if (WEBClient)
3180
                    delete WEBClient;
3181
 
93 andreas 3182
                MSG_ERROR("Unexpected exception occured. [TButton::_imageRefresh()]");
3183
                return;
3184
            }
21 andreas 3185
        }
3186
        else if (mDOrder[i] == ORD_ELEM_ICON)
3187
        {
3188
            if (!buttonIcon(&imgButton, mActInstance))
3189
                return;
3190
        }
3191
        else if (mDOrder[i] == ORD_ELEM_TEXT)
3192
        {
3193
            if (!buttonText(&imgButton, mActInstance))
3194
                return;
3195
        }
3196
        else if (mDOrder[i] == ORD_ELEM_BORDER)
3197
        {
3198
            if (!buttonBorder(&imgButton, mActInstance))
3199
                return;
3200
        }
3201
    }
3202
 
3203
    if (mGlobalOO >= 0 || sr[mActInstance].oo >= 0) // Take overall opacity into consideration
3204
    {
3205
        SkBitmap ooButton;
3206
        int w = imgButton.width();
3207
        int h = imgButton.height();
254 andreas 3208
 
3209
        if (!allocPixels(w, h, &ooButton))
3210
            return;
3211
 
21 andreas 3212
        SkCanvas canvas(ooButton);
3213
        SkIRect irect = SkIRect::MakeXYWH(0, 0, w, h);
3214
        SkRegion region;
3215
        region.setRect(irect);
3216
        SkScalar oo;
3217
 
3218
        if (mGlobalOO >= 0 && sr[mActInstance].oo >= 0)
3219
        {
3220
            oo = std::min((SkScalar)mGlobalOO, (SkScalar)sr[mActInstance].oo);
3221
            MSG_DEBUG("Set global overal opacity to " << oo);
3222
        }
3223
        else if (sr[mActInstance].oo >= 0)
3224
        {
3225
            oo = (SkScalar)sr[mActInstance].oo;
3226
            MSG_DEBUG("Set overal opacity to " << oo);
3227
        }
3228
        else
3229
        {
3230
            oo = (SkScalar)mGlobalOO;
3231
            MSG_DEBUG("Set global overal opacity to " << oo);
3232
        }
3233
 
3234
        SkScalar alpha = 1.0 / 255.0 * oo;
3235
        MSG_DEBUG("Calculated alpha value: " << alpha);
3236
        SkPaint paint;
3237
        paint.setAlphaf(alpha);
365 andreas 3238
        //(SkImageFilters::AlphaThreshold(region, 0.0, alpha, nullptr));
3239
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(imgButton);
179 andreas 3240
        canvas.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);
21 andreas 3241
        imgButton.erase(SK_ColorTRANSPARENT, {0, 0, w, h});
3242
        imgButton = ooButton;
3243
    }
3244
 
3245
    mLastImage = imgButton;
177 andreas 3246
    mChanged = false;
21 andreas 3247
 
3248
    if (!prg_stopped && visible && _displayButton)
26 andreas 3249
    {
3250
        int rwidth = wt;
3251
        int rheight = ht;
3252
        int rleft = lt;
3253
        int rtop = tp;
43 andreas 3254
#ifdef _SCALE_SKIA_
26 andreas 3255
        if (gPageManager && gPageManager->getScaleFactor() != 1.0)
3256
        {
3257
            rwidth = (int)((double)wt * gPageManager->getScaleFactor());
3258
            rheight = (int)((double)ht * gPageManager->getScaleFactor());
3259
            rleft = (int)((double)lt * gPageManager->getScaleFactor());
3260
            rtop = (int)((double)tp * gPageManager->getScaleFactor());
3261
 
3262
            SkPaint paint;
3263
            paint.setBlendMode(SkBlendMode::kSrc);
3264
            paint.setFilterQuality(kHigh_SkFilterQuality);
28 andreas 3265
            // Calculate new dimension
26 andreas 3266
            SkImageInfo info = imgButton.info();
3267
            int width = (int)((double)info.width() * gPageManager->getScaleFactor());
3268
            int height = (int)((double)info.height() * gPageManager->getScaleFactor());
28 andreas 3269
            // Create a canvas and draw new image
3270
            sk_sp<SkImage> im = SkImage::MakeFromBitmap(imgButton);
3271
            imgButton.allocN32Pixels(width, height);
3272
            imgButton.eraseColor(SK_ColorTRANSPARENT);
254 andreas 3273
            SkCanvas can(imgButton, SkSurfaceProps());
26 andreas 3274
            SkRect rect = SkRect::MakeXYWH(0, 0, width, height);
179 andreas 3275
            can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
28 andreas 3276
            rowBytes = imgButton.info().minRowBytes();
3277
            mLastImage = imgButton;
26 andreas 3278
        }
43 andreas 3279
#endif
289 andreas 3280
        TBitmap image((unsigned char *)imgButton.getPixels(), imgButton.info().width(), imgButton.info().height());
298 andreas 3281
        _displayButton(mHandle, parent, image, rwidth, rheight, rleft, rtop, isPassThrough());
26 andreas 3282
    }
21 andreas 3283
}
3284
 
15 andreas 3285
void TButton::registerSystemButton()
3286
{
3287
    DECL_TRACER("TButton::registerSystemButton()");
3288
 
3289
    if (mSystemReg)
3290
        return;
3291
 
3292
    // If this is a special system button, register it to receive the state
206 andreas 3293
    if (ap == 0 && ad == SYSTEM_ITEM_CONNSTATE)     // Connection status?
15 andreas 3294
    {
3295
        MSG_TRACE("Try to register button " << na << " as connection status ...");
3296
 
3297
        if (gAmxNet)
3298
        {
3299
            gAmxNet->registerNetworkState(bind(&TButton::funcNetwork, this, std::placeholders::_1), mHandle);
3300
            mSystemReg = true;
3301
            MSG_TRACE("Button registered");
3302
        }
3303
        else
3304
            MSG_WARNING("Network class not initialized!");
3305
 
3306
    }
206 andreas 3307
    else if (ap == 0 && ((ad >= SYSTEM_ITEM_STANDARDTIME && ad <= SYSTEM_ITEM_TIME24) || (ad >= SYSTEM_ITEM_DATEWEEKDAY && ad <= SYSTEM_ITEM_DATEYYYYMMDD))) // time or date
15 andreas 3308
    {
3309
        MSG_TRACE("Try to register button " << na << " as time/date ...");
3310
 
3311
        if (gAmxNet)
3312
        {
3313
            gAmxNet->registerTimer(bind(&TButton::funcTimer, this, std::placeholders::_1), mHandle);
3314
            mSystemReg = true;
3315
            MSG_TRACE("Button registered");
3316
        }
3317
        else
3318
            MSG_WARNING("Network class not initialized!");
3319
 
206 andreas 3320
        if (ad >= SYSTEM_ITEM_STANDARDTIME && ad <= SYSTEM_ITEM_TIME24 && !mTimer)
15 andreas 3321
        {
3322
            mTimer = new TTimer;
3323
            mTimer->setInterval(std::chrono::milliseconds(1000));   // 1 second
3324
            mTimer->registerCallback(bind(&TButton::_TimerCallback, this, std::placeholders::_1));
3325
            mTimer->run();
3326
        }
3327
    }
206 andreas 3328
    else if (ap == 0 && (ad == SYSTEM_ITEM_BATTERYLEVEL || ad == SYSTEM_ITEM_BATTERYCHARGING))   // Battery status
23 andreas 3329
    {
38 andreas 3330
        if (gPageManager)
247 andreas 3331
        {
3332
#ifdef Q_OS_ANDROID
38 andreas 3333
            gPageManager->regCallbackBatteryState(bind(&TButton::funcBattery, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), mHandle);
247 andreas 3334
#endif
3335
#ifdef Q_OS_IOS
3336
            gPageManager->regCallbackBatteryState(bind(&TButton::funcBattery, this, std::placeholders::_1, std::placeholders::_2), mHandle);
3337
#endif
3338
        }
195 andreas 3339
 
3340
        mSystemReg = true;
23 andreas 3341
    }
206 andreas 3342
    else if (lp == 0 && lv == SYSTEM_ITEM_CONNSTRENGTH)       // Network connection strength
36 andreas 3343
    {
3344
        if (gPageManager)
3345
            gPageManager->regCallbackNetState(bind(&TButton::funcNetworkState, this, std::placeholders::_1), mHandle);
195 andreas 3346
 
3347
        mSystemReg = true;
36 andreas 3348
    }
206 andreas 3349
    else if (lp == 0 && lv == SYSTEM_ITEM_SYSVOLUME)        // System volume
192 andreas 3350
    {
3351
        mLastLevel = TConfig::getSystemVolume();
3352
        mChanged = true;
195 andreas 3353
        mSystemReg = true;
192 andreas 3354
    }
195 andreas 3355
    else if (cp == 0 && type == GENERAL && ch > 0 && isSystemCheckBox(ch))
192 andreas 3356
    {
195 andreas 3357
        int inst = getButtonInstance(0, ch);
3358
 
3359
        if (inst >= 0)
3360
        {
3361
            mActInstance = inst;
3362
            mChanged = true;
3363
            mSystemReg = true;
3364
        }
192 andreas 3365
    }
195 andreas 3366
    else if (ap == 0 && ad > 0 && isSystemTextLine(ad))
192 andreas 3367
    {
195 andreas 3368
        sr[0].te = sr[1].te = fillButtonText(ad, 0);
185 andreas 3369
        mChanged = true;
195 andreas 3370
        mSystemReg = true;
185 andreas 3371
    }
15 andreas 3372
}
3373
 
3374
void TButton::addPushFunction(string& func, string& page)
3375
{
3376
    DECL_TRACER("TButton::addPushFunction(string& func, string& page)");
3377
 
3378
    vector<string> allFunc = { "Stan", "Prev", "Show", "Hide", "Togg", "ClearG", "ClearP", "ClearA" };
3379
    vector<string>::iterator iter;
3380
 
3381
    for (iter = allFunc.begin(); iter != allFunc.end(); ++iter)
3382
    {
162 andreas 3383
        if (strCaseCompare(*iter, func) == 0)
15 andreas 3384
        {
3385
            bool found = false;
3386
            vector<PUSH_FUNC_T>::iterator iterPf;
3387
 
162 andreas 3388
            if (pushFunc.size() > 0)
15 andreas 3389
            {
162 andreas 3390
                for (iterPf = pushFunc.begin(); iterPf != pushFunc.end(); ++iterPf)
15 andreas 3391
                {
162 andreas 3392
                    if (strCaseCompare(iterPf->pfType, func) == 0)
3393
                    {
3394
                        iterPf->pfName = page;
3395
                        found = true;
3396
                        break;
3397
                    }
15 andreas 3398
                }
3399
            }
3400
 
3401
            if (!found)
3402
            {
3403
                PUSH_FUNC_T pf;
3404
                pf.pfType = func;
3405
                pf.pfName = page;
3406
                pushFunc.push_back(pf);
3407
            }
108 andreas 3408
 
3409
            break;
15 andreas 3410
        }
3411
    }
3412
}
3413
 
16 andreas 3414
void TButton::clearPushFunction(const string& action)
3415
{
3416
    DECL_TRACER("TButton::clearPushFunction(const string& action)");
3417
 
3418
    if (pushFunc.empty())
3419
        return;
3420
 
3421
    vector<PUSH_FUNC_T>::iterator iter;
3422
 
118 andreas 3423
    for (iter = pushFunc.begin(); iter != pushFunc.end(); ++iter)
16 andreas 3424
    {
162 andreas 3425
        if (strCaseCompare(iter->pfName, action) == 0)
16 andreas 3426
        {
3427
            pushFunc.erase(iter);
3428
            return;
3429
        }
3430
    }
3431
}
3432
 
8 andreas 3433
void TButton::getDrawOrder(const std::string& sdo, DRAW_ORDER *order)
4 andreas 3434
{
8 andreas 3435
    DECL_TRACER("TButton::getDrawOrder(const std::string& sdo, DRAW_ORDER *order)");
4 andreas 3436
 
8 andreas 3437
    if (!order)
3438
        return;
3439
 
3440
    if (sdo.empty() || sdo.length() != 10)
4 andreas 3441
    {
8 andreas 3442
        *order     = ORD_ELEM_FILL;
3443
        *(order+1) = ORD_ELEM_BITMAP;
3444
        *(order+2) = ORD_ELEM_ICON;
3445
        *(order+3) = ORD_ELEM_TEXT;
3446
        *(order+4) = ORD_ELEM_BORDER;
3447
        return;
3448
    }
3449
 
289 andreas 3450
    int elems = (int)(sdo.length() / 2);
8 andreas 3451
 
3452
    for (int i = 0; i < elems; i++)
3453
    {
3454
        int e = atoi(sdo.substr(i * 2, 2).c_str());
3455
 
3456
        if (e < 1 || e > 5)
3457
        {
3458
            MSG_ERROR("Invalid draw order \"" << sdo << "\"!");
3459
            TError::setError();
3460
            return;
3461
        }
3462
 
3463
        *(order+i) = (DRAW_ORDER)e;
3464
    }
3465
}
3466
 
3467
bool TButton::buttonFill(SkBitmap* bm, int instance)
3468
{
3469
    DECL_TRACER("TButton::buttonFill(SkBitmap* bm, int instance)");
3470
 
3471
    if (!bm)
3472
    {
3473
        MSG_ERROR("Invalid bitmap!");
4 andreas 3474
        return false;
3475
    }
3476
 
165 andreas 3477
    if (instance < 0 || (size_t)instance >= sr.size())
3478
    {
3479
        MSG_ERROR("Invalid instance " << instance);
3480
        return false;
3481
    }
3482
 
8 andreas 3483
    SkColor color = TColor::getSkiaColor(sr[instance].cf);
306 andreas 3484
    MSG_DEBUG("Fill color[" << instance << "]: " << sr[instance].cf << " (#" << std::setw(8) << std::setfill('0') << std::hex << color << ")" << std::dec << std::setfill(' ') << std::setw(1));
292 andreas 3485
    // We create a new bitmap and fill it with the given fill color. Then
3486
    // we put this image over the existing image "bm". In case this method is
3487
    // not the first in the draw order, it prevents the button from completely
3488
    // overwrite.
3489
    SkImageInfo info = bm->info();
3490
    SkBitmap bitmap;
3491
 
3492
    if (!allocPixels(info.width(), info.height(), &bitmap))
3493
    {
3494
        MSG_ERROR("Error allocating a bitmap with size " << info.width() << " x " << info.height() << "!");
3495
        return false;
3496
    }
3497
 
3498
    bitmap.eraseColor(color);                       // Fill the new bitmap with the fill color
3499
    SkCanvas ctx(*bm, SkSurfaceProps());            // Create a canvas
3500
    SkPaint paint;                                  // The paint "device"
3501
    paint.setBlendMode(SkBlendMode::kSrcOver);      // We're overwriting each pixel
365 andreas 3502
    sk_sp<SkImage> _image = SkImages::RasterFromBitmap(bitmap);    // Technically we need an image. So we convert our new bitmap into an image.
292 andreas 3503
    ctx.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);   // Now we put the new image over the existing one.
8 andreas 3504
    return true;
3505
}
3506
 
46 andreas 3507
bool TButton::buttonBitmap(SkBitmap* bm, int inst)
8 andreas 3508
{
3509
    DECL_TRACER("TButton::buttonBitmap(SkBitmap* bm, int instane)");
3510
 
240 andreas 3511
    if (prg_stopped || !bm)
33 andreas 3512
        return false;
35 andreas 3513
 
46 andreas 3514
    int instance = inst;
3515
 
3516
    if (inst < 0)
3517
        instance = 0;
3518
    else if ((size_t)inst >= sr.size())
289 andreas 3519
        instance = (int)(sr.size() - 1);
46 andreas 3520
 
184 andreas 3521
    /*
3522
     * Here we test if we have a cameleon image. If there is a mask (sr[].mi)
3523
     * and no frame (sr[].bs) then we have a cameleon image. A bitmap is
3524
     * optional. If there is one it will be used to draw with the mask.
3525
     * Otherwise the mask may be used as an overlay for a bitmap on another
3526
     * button below the mask.
3527
     */
29 andreas 3528
    if (!sr[instance].mi.empty() && sr[instance].bs.empty())       // Chameleon image?
4 andreas 3529
    {
184 andreas 3530
        MSG_DEBUG("Chameleon image consisting of mask " << sr[instance].mi << " and bitmap " << (sr[instance].bm.empty() ? "NONE" : sr[instance].bm) << " ...");
69 andreas 3531
 
165 andreas 3532
        SkBitmap bmMi;
3533
        SkBitmap bmBm;
3534
 
167 andreas 3535
        if (!TImgCache::getBitmap(sr[instance].mi, &bmMi, _BMTYPE_CHAMELEON, &sr[instance].mi_width, &sr[instance].mi_height))
69 andreas 3536
        {
165 andreas 3537
            sk_sp<SkData> data = readImage(sr[instance].mi);
3538
            bool loaded = false;
3539
 
3540
            if (data)
3541
            {
3542
                DecodeDataToBitmap(data, &bmMi);
3543
 
3544
                if (!bmMi.empty())
3545
                {
3546
                    TImgCache::addImage(sr[instance].mi, bmMi, _BMTYPE_CHAMELEON);
3547
                    loaded = true;
3548
                    sr[instance].mi_width = bmMi.info().width();
3549
                    sr[instance].mi_height = bmMi.info().height();
3550
                }
3551
            }
3552
 
3553
            if(!loaded)
3554
            {
3555
                MSG_ERROR("Missing image " << sr[instance].mi << "!");
3556
                TError::setError();
3557
                return false;
3558
            }
69 andreas 3559
        }
3560
 
240 andreas 3561
        MSG_DEBUG("Chameleon image size: " << bmMi.info().width() << " x " << bmMi.info().height());
165 andreas 3562
        SkBitmap imgRed(bmMi);
24 andreas 3563
        SkBitmap imgMask;
163 andreas 3564
        bool haveBothImages = true;
4 andreas 3565
 
24 andreas 3566
        if (!sr[instance].bm.empty())
163 andreas 3567
        {
167 andreas 3568
            if (!TImgCache::getBitmap(sr[instance].bm, &bmBm, _BMTYPE_BITMAP, &sr[instance].bm_width, &sr[instance].bm_height))
163 andreas 3569
            {
165 andreas 3570
                sk_sp<SkData> data = readImage(sr[instance].bm);
3571
                bool loaded = false;
3572
 
3573
                if (data)
3574
                {
3575
                    DecodeDataToBitmap(data, &bmBm);
3576
 
3577
                    if (!bmMi.empty())
3578
                    {
3579
                        TImgCache::addImage(sr[instance].bm, bmMi, _BMTYPE_BITMAP);
3580
                        loaded = true;
3581
                        sr[instance].bm_width = bmBm.info().width();
3582
                        sr[instance].bm_height = bmBm.info().height();
3583
                    }
3584
                }
3585
 
3586
                if (!loaded)
3587
                {
3588
                    MSG_ERROR("Missing image " << sr[instance].bm << "!");
3589
                    TError::setError();
3590
                    return false;
3591
                }
163 andreas 3592
            }
165 andreas 3593
 
184 andreas 3594
            if (!bmBm.empty())
240 andreas 3595
            {
3596
                if (!imgMask.installPixels(bmBm.pixmap()))
3597
                {
3598
                    MSG_ERROR("Error installing pixmap " << sr[instance].bm << " for chameleon image!");
254 andreas 3599
 
3600
                    if (!allocPixels(imgRed.info().width(), imgRed.info().height(), &imgMask))
3601
                        return false;
3602
 
240 andreas 3603
                    imgMask.eraseColor(SK_ColorTRANSPARENT);
3604
                    haveBothImages = false;
3605
                }
3606
            }
163 andreas 3607
            else
3608
            {
184 andreas 3609
                MSG_WARNING("No or invalid bitmap! Ignoring bitmap for cameleon image.");
254 andreas 3610
 
3611
                if (!allocPixels(imgRed.info().width(), imgRed.info().height(), &imgMask))
3612
                    return false;
3613
 
163 andreas 3614
                imgMask.eraseColor(SK_ColorTRANSPARENT);
3615
                haveBothImages = false;
3616
            }
3617
        }
3618
        else
3619
            haveBothImages = false;
24 andreas 3620
 
240 andreas 3621
        MSG_DEBUG("Bitmap image size: " << bmBm.info().width() << " x " << bmBm.info().height());
3622
        MSG_DEBUG("Bitmap mask size: " << imgMask.info().width() << " x " << imgMask.info().height());
167 andreas 3623
        SkBitmap img = drawImageButton(imgRed, imgMask, sr[instance].mi_width, sr[instance].mi_height, TColor::getSkiaColor(sr[instance].cf), TColor::getSkiaColor(sr[instance].cb));
20 andreas 3624
 
3625
        if (img.empty())
4 andreas 3626
        {
20 andreas 3627
            MSG_ERROR("Error creating the cameleon image \"" << sr[instance].mi << "\" / \"" << sr[instance].bm << "\"!");
3628
            TError::setError();
3629
            return false;
3630
        }
4 andreas 3631
 
240 andreas 3632
        MSG_DEBUG("Have both images: " << (haveBothImages ? "YES" : "NO"));
254 andreas 3633
        SkCanvas ctx(img, SkSurfaceProps());
20 andreas 3634
        SkImageInfo info = img.info();
3635
        SkPaint paint;
24 andreas 3636
        paint.setBlendMode(SkBlendMode::kSrcOver);
365 andreas 3637
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(imgMask);
179 andreas 3638
        ctx.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);
4 andreas 3639
 
20 andreas 3640
        POSITION_t position = calcImagePosition(sr[instance].mi_width, sr[instance].mi_height, SC_BITMAP, instance);
7 andreas 3641
 
20 andreas 3642
        if (!position.valid)
3643
        {
3644
            MSG_ERROR("Error calculating the position of the image for button number " << bi << ": " << na);
3645
            TError::setError();
3646
            return false;
4 andreas 3647
        }
10 andreas 3648
 
254 andreas 3649
        SkCanvas can(*bm, SkSurfaceProps());
20 andreas 3650
        paint.setBlendMode(SkBlendMode::kSrc);
21 andreas 3651
 
3652
        if (sr[instance].sb == 0)
163 andreas 3653
        {
3654
            if (!haveBothImages)
3655
            {
365 andreas 3656
                sk_sp<SkImage> _image = SkImages::RasterFromBitmap(img);
179 andreas 3657
                can.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);
163 andreas 3658
 
3659
                if (!sr[instance].bm.empty())
3660
                {
165 andreas 3661
                    imgMask.installPixels(bmBm.pixmap());
163 andreas 3662
                    paint.setBlendMode(SkBlendMode::kSrcOver);
365 andreas 3663
                    _image = SkImages::RasterFromBitmap(imgMask);
179 andreas 3664
                    can.drawImage(_image, position.left, position.top, SkSamplingOptions(), &paint);
163 andreas 3665
                }
3666
            }
3667
            else
179 andreas 3668
            {
365 andreas 3669
                sk_sp<SkImage> _image = SkImages::RasterFromBitmap(img);
179 andreas 3670
                can.drawImage(_image, position.left, position.top, SkSamplingOptions(), &paint);
3671
            }
163 andreas 3672
        }
21 andreas 3673
        else    // Scale to fit
3674
        {
163 andreas 3675
            if (!haveBothImages)
3676
            {
3677
                SkRect rect;
3678
                rect.setXYWH(0, 0, imgRed.info().width(), imgRed.info().height());
365 andreas 3679
                sk_sp<SkImage> im = SkImages::RasterFromBitmap(img);
179 andreas 3680
                can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
163 andreas 3681
 
3682
                if (!sr[instance].bm.empty())
3683
                {
165 andreas 3684
                    imgMask.installPixels(bmBm.pixmap());
163 andreas 3685
                    rect.setXYWH(position.left, position.top, position.width, position.height);
365 andreas 3686
                    im = SkImages::RasterFromBitmap(imgMask);
163 andreas 3687
                    paint.setBlendMode(SkBlendMode::kSrcOver);
179 andreas 3688
                    can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
163 andreas 3689
                }
3690
            }
3691
            else
3692
            {
3693
                SkRect rect = SkRect::MakeXYWH(position.left, position.top, position.width, position.height);
365 andreas 3694
                sk_sp<SkImage> im = SkImages::RasterFromBitmap(img);
179 andreas 3695
                can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
163 andreas 3696
            }
21 andreas 3697
        }
4 andreas 3698
    }
3699
    else if (!sr[instance].bm.empty())
3700
    {
69 andreas 3701
        MSG_TRACE("Drawing normal image " << sr[instance].bm << " ...");
3702
 
165 andreas 3703
        SkBitmap image;
3704
 
167 andreas 3705
        if (!TImgCache::getBitmap(sr[instance].bm, &image, _BMTYPE_BITMAP, &sr[instance].bm_width, &sr[instance].bm_height))
69 andreas 3706
        {
165 andreas 3707
            sk_sp<SkData> data = readImage(sr[instance].bm);
3708
            bool loaded = false;
3709
 
3710
            if (data)
3711
            {
3712
                DecodeDataToBitmap(data, &image);
3713
 
3714
                if (!image.empty())
3715
                {
3716
                    TImgCache::addImage(sr[instance].mi, image, _BMTYPE_BITMAP);
3717
                    loaded = true;
3718
                    sr[instance].bm_width = image.info().width();
3719
                    sr[instance].bm_height = image.info().height();
3720
                }
3721
            }
3722
 
3723
            if (!loaded)
3724
            {
3725
                MSG_ERROR("Missing image " << sr[instance].bm << "!");
3726
                return true;        // We want the button even without an image
3727
            }
69 andreas 3728
        }
3729
 
6 andreas 3730
        if (image.empty())
4 andreas 3731
        {
3732
            MSG_ERROR("Error creating the image \"" << sr[instance].bm << "\"!");
3733
            TError::setError();
3734
            return false;
3735
        }
3736
 
99 andreas 3737
        IMAGE_SIZE_t isize = calcImageSize(image.info().width(), image.info().height(), instance, true);
3738
        POSITION_t position = calcImagePosition((sr[instance].sb ? isize.width : image.info().width()), (sr[instance].sb ? isize.height : image.info().height()), SC_BITMAP, instance);
4 andreas 3739
 
3740
        if (!position.valid)
3741
        {
3742
            MSG_ERROR("Error calculating the position of the image for button number " << bi);
3743
            TError::setError();
3744
            return false;
3745
        }
3746
 
10 andreas 3747
        MSG_DEBUG("Putting bitmap on top of image ...");
3748
        SkPaint paint;
69 andreas 3749
        paint.setBlendMode(SkBlendMode::kSrcOver);
254 andreas 3750
        SkCanvas can(*bm, SkSurfaceProps());
21 andreas 3751
 
69 andreas 3752
        if (sr[instance].sb == 0)   // Scale bitmap?
3753
        {                           // No, keep size
46 andreas 3754
            if ((sr[instance].jb == 0 && sr[instance].bx >= 0 && sr[instance].by >= 0) || sr[instance].jb != 0)  // Draw the full image
179 andreas 3755
            {
365 andreas 3756
                sk_sp<SkImage> _image = SkImages::RasterFromBitmap(image);
179 andreas 3757
                can.drawImage(_image, position.left, position.top, SkSamplingOptions(), &paint);
3758
            }
49 andreas 3759
            else    // We need only a subset of the image
46 andreas 3760
            {
3761
                MSG_DEBUG("Create a subset of an image ...");
3762
 
3763
                // Create a new Info to have the size of the subset.
3764
                SkImageInfo info = SkImageInfo::Make(position.width, position.height, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
3765
                size_t byteSize = info.computeMinByteSize();
3766
 
3767
                if (byteSize == 0)
3768
                {
3769
                    MSG_ERROR("Unable to calculate size of image!");
3770
                    TError::setError();
3771
                    return false;
3772
                }
3773
 
3774
                MSG_DEBUG("Rectangle of part: x: " << position.left << ", y: " << position.top << ", w: " << position.width << ", h: " << position.height);
3775
                SkBitmap part;      // Bitmap receiving the wanted part from the whole image
3776
                SkIRect irect = SkIRect::MakeXYWH(position.left, position.top, position.width, position.height);
3777
                image.extractSubset(&part, irect);  // Extract the part of the image containg the pixels we want
365 andreas 3778
                sk_sp<SkImage> _image = SkImages::RasterFromBitmap(part);
179 andreas 3779
                can.drawImage(_image, 0, 0, SkSamplingOptions(), &paint); // Draw the image
46 andreas 3780
            }
3781
        }
21 andreas 3782
        else    // Scale to fit
3783
        {
99 andreas 3784
            SkRect rect = SkRect::MakeXYWH(position.left, position.top, isize.width, isize.height);
365 andreas 3785
            sk_sp<SkImage> im = SkImages::RasterFromBitmap(image);
179 andreas 3786
            can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
21 andreas 3787
        }
6 andreas 3788
    }
8 andreas 3789
    else
6 andreas 3790
    {
8 andreas 3791
        MSG_DEBUG("No bitmap defined.");
3792
    }
4 andreas 3793
 
8 andreas 3794
    return true;
3795
}
4 andreas 3796
 
97 andreas 3797
bool TButton::buttonDynamic(SkBitmap* bm, int instance, bool show, bool *state)
21 andreas 3798
{
97 andreas 3799
    DECL_TRACER("TButton::buttonDynamic(SkBitmap* bm, int instance, bool show, bool *state)");
21 andreas 3800
 
33 andreas 3801
    if (prg_stopped)
3802
        return false;
35 andreas 3803
 
21 andreas 3804
    if (!gPrjResources)
3805
    {
3806
        MSG_ERROR("Internal error: Global resource class not initialized!");
3807
        return false;
3808
    }
3809
 
165 andreas 3810
    if (instance < 0 || (size_t)instance >= sr.size())
3811
    {
3812
        MSG_ERROR("Invalid instance " << instance);
3813
        return false;
3814
    }
3815
 
21 andreas 3816
    if (!sr[instance].dynamic)
3817
    {
3818
        MSG_WARNING("Button " << bi << ": \"" << na << "\" is not for remote image!");
3819
        return false;
3820
    }
3821
 
98 andreas 3822
    if (!visible)
99 andreas 3823
    {
271 andreas 3824
        MSG_DEBUG("Dynamic button " << handleToString(mHandle) << " is invisible. Will not draw it.");
98 andreas 3825
        return true;
99 andreas 3826
    }
98 andreas 3827
 
271 andreas 3828
    MSG_DEBUG("Dynamic button " << handleToString(mHandle) << " will be drawn ...");
97 andreas 3829
    size_t idx = 0;
21 andreas 3830
 
97 andreas 3831
    if ((idx = gPrjResources->getResourceIndex("image")) == TPrjResources::npos)
3832
    {
3833
        MSG_ERROR("There exists no image resource!");
3834
        return false;
3835
    }
3836
 
289 andreas 3837
    RESOURCE_T resource = gPrjResources->findResource((int)idx, sr[instance].bm);
97 andreas 3838
 
21 andreas 3839
    if (resource.protocol.empty())
3840
    {
100 andreas 3841
        MSG_WARNING("Resource " << sr[instance].bm << " not found!");
3842
        return true;
21 andreas 3843
    }
3844
 
3845
    string path = resource.path;
3846
 
3847
    if (!resource.file.empty())
3848
        path += "/" + resource.file;
3849
 
94 andreas 3850
    string url = THTTPClient::makeURLs(toLower(resource.protocol), resource.host, 0, path);
21 andreas 3851
 
97 andreas 3852
    if (url.empty())
3853
    {
100 andreas 3854
        MSG_DEBUG("No URL, no bitmap!");
97 andreas 3855
        return true;    // We have no image but the button still exists
3856
    }
3857
 
3858
    SkBitmap image;
3859
 
165 andreas 3860
    if (TImgCache::getBitmap(url, &image, _BMTYPE_URL))
97 andreas 3861
    {
100 andreas 3862
        MSG_DEBUG("Found image \"" << url << "\" in the cache. Will reuse it.");
99 andreas 3863
        IMAGE_SIZE_t isize = calcImageSize(image.info().width(), image.info().height(), instance, true);
3864
        POSITION_t position = calcImagePosition((sr[instance].sb ? isize.width : image.info().width()), (sr[instance].sb ? isize.height : image.info().height()), SC_BITMAP, instance);
97 andreas 3865
 
3866
        if (!position.valid)
3867
        {
3868
            MSG_ERROR("Error calculating the position of the image for button number " << bi);
3869
            TError::setError();
3870
            return false;
3871
        }
3872
 
3873
        SkPaint paint;
3874
        paint.setBlendMode(SkBlendMode::kSrcOver);
254 andreas 3875
        SkCanvas can(*bm, SkSurfaceProps());
97 andreas 3876
 
3877
        if (sr[instance].sb == 0)   // Scale bitmap?
3878
        {                           // No, keep size
3879
            if ((sr[instance].jb == 0 && sr[instance].bx >= 0 && sr[instance].by >= 0) || sr[instance].jb != 0)  // Draw the full image
179 andreas 3880
            {
365 andreas 3881
                sk_sp<SkImage> _image = SkImages::RasterFromBitmap(image);
179 andreas 3882
                can.drawImage(_image, position.left, position.top, SkSamplingOptions(), &paint);
3883
            }
97 andreas 3884
            else    // We need only a subset of the image
3885
            {
3886
                MSG_DEBUG("Create a subset of an image ...");
3887
 
3888
                // Create a new Info to have the size of the subset.
3889
                SkImageInfo info = SkImageInfo::Make(position.width, position.height, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
3890
                size_t byteSize = info.computeMinByteSize();
3891
 
3892
                if (byteSize == 0)
3893
                {
3894
                    MSG_ERROR("Unable to calculate size of image!");
3895
                    TError::setError();
3896
                    return false;
3897
                }
3898
 
3899
                MSG_DEBUG("Rectangle of part: x: " << position.left << ", y: " << position.top << ", w: " << position.width << ", h: " << position.height);
3900
                SkBitmap part;      // Bitmap receiving the wanted part from the whole image
3901
                SkIRect irect = SkIRect::MakeXYWH(position.left, position.top, position.width, position.height);
3902
                image.extractSubset(&part, irect);  // Extract the part of the image containg the pixels we want
365 andreas 3903
                sk_sp<SkImage> _image = SkImages::RasterFromBitmap(part);
179 andreas 3904
                can.drawImage(_image, 0, 0, SkSamplingOptions(), &paint); // Draw the image
97 andreas 3905
            }
3906
        }
3907
        else    // Scale to fit
3908
        {
99 andreas 3909
            SkRect rect = SkRect::MakeXYWH(position.left, position.top, isize.width, isize.height);
365 andreas 3910
            sk_sp<SkImage> im = SkImages::RasterFromBitmap(image);
179 andreas 3911
            can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
97 andreas 3912
        }
3913
 
3914
        return true;
3915
    }
3916
 
21 andreas 3917
    try
3918
    {
94 andreas 3919
        // First me must add the credential for the image into a bitmap cache element
3920
        BITMAP_CACHE bc;
3921
        bc.top = tp;
3922
        bc.left = lt;
3923
        bc.width = wt;
3924
        bc.height = ht;
3925
        bc.bi = bi;
97 andreas 3926
        bc.show = show;
94 andreas 3927
        bc.handle = getHandle();
3928
        bc.parent = getParent();
3929
        bc.bitmap = *bm;
3930
        addToBitmapCache(bc);
97 andreas 3931
 
3932
        if (state)
3933
            *state = true;  // Prevent the calling method from displaying the button
3934
 
21 andreas 3935
        MSG_TRACE("Starting thread for loading a dynamic image ...");
94 andreas 3936
        mThrRes = std::thread([=] { this->funcResource(&resource, url, bc, instance); });
21 andreas 3937
        MSG_TRACE("Thread started. Detaching ...");
3938
        mThrRes.detach();
3939
        MSG_TRACE("Thread is running and detached.");
3940
    }
3941
    catch (std::exception& e)
3942
    {
28 andreas 3943
        MSG_ERROR("Error starting the resource thread: " << e.what());
21 andreas 3944
    }
3945
 
3946
    return true;
3947
}
3948
 
99 andreas 3949
/*
3950
 * Draws the elements of a button starting at the point where the bitmap was
3951
 * already drawed. Everything coming afterwards acording to the draw order
3952
 * is drawed in the desired order.
3953
 * This method is called out of a thread to draw a button with an external
3954
 * image coming from a WEB server.
3955
 */
3956
bool TButton::drawAlongOrder(SkBitmap *imgButton, int instance)
3957
{
3958
    DECL_TRACER("TButton::drawAlongOrder(SkBitmap *imgButton, int instance)");
3959
 
165 andreas 3960
    if (instance < 0 || (size_t)instance >= sr.size())
3961
    {
3962
        MSG_ERROR("Invalid instance " << instance);
3963
        return false;
3964
    }
3965
 
99 andreas 3966
    bool cont = false;
3967
 
3968
    for (int i = 0; i < ORD_ELEM_COUNT; i++)
3969
    {
3970
        if (!cont && mDOrder[i] == ORD_ELEM_BITMAP)
3971
        {
3972
            cont = true;
3973
            continue;
3974
        }
100 andreas 3975
        else if (!cont)
3976
            continue;
99 andreas 3977
 
3978
        if (mDOrder[i] == ORD_ELEM_FILL)
3979
        {
3980
            if (!buttonFill(imgButton, instance))
3981
                return false;
3982
        }
3983
        else if (mDOrder[i] == ORD_ELEM_ICON)
3984
        {
3985
            if (!buttonIcon(imgButton, instance))
3986
                return false;
3987
        }
3988
        else if (mDOrder[i] == ORD_ELEM_TEXT)
3989
        {
3990
            if (!buttonText(imgButton, instance))
3991
                return false;
3992
        }
3993
        else if (mDOrder[i] == ORD_ELEM_BORDER)
3994
        {
3995
            if (!buttonBorder(imgButton, instance))
3996
                return false;
3997
        }
3998
    }
3999
 
4000
    return true;
4001
}
4002
 
94 andreas 4003
void TButton::funcResource(const RESOURCE_T* resource, const std::string& url, BITMAP_CACHE bc, int instance)
21 andreas 4004
{
4005
    DECL_TRACER("TButton::funcResource(RESOURCE_T* resource, std::string& url, SkBitmap* bm, int instance)");
4006
 
97 andreas 4007
    if (prg_stopped || killed || _restart_ || !resource)
35 andreas 4008
        return;
4009
 
21 andreas 4010
    if (resource->refresh > 0 && !resource->dynamo)      // Periodically refreshing image?
4011
    {
4012
        MSG_DEBUG("Retrieving periodicaly refreshed image");
4013
 
97 andreas 4014
        if (!bc.handle || !bc.parent || bc.bi <= 1)
21 andreas 4015
        {
4016
            MSG_ERROR("Invalid button. Can't make a dynamo image!");
4017
            return;
4018
        }
4019
 
97 andreas 4020
        THR_REFRESH_t *thref = _findResource(bc.handle, bc.parent, bc.bi);
21 andreas 4021
        TImageRefresh *mImageRefresh = nullptr;
4022
 
4023
        if (!thref)
4024
        {
4025
            MSG_DEBUG("Creating a new refresh thread");
4026
            mImageRefresh = new TImageRefresh();
4027
            mImageRefresh->registerCallback(bind(&TButton::_imageRefresh, this, std::placeholders::_1));
4028
            mImageRefresh->setInterval(std::chrono::seconds(resource->refresh));
4029
            mImageRefresh->setUsername(resource->user);
4030
            mImageRefresh->setPassword(resource->password);
4031
 
4032
            if (resource->preserve)
4033
                mImageRefresh->setRunOnce();
4034
 
97 andreas 4035
            _addResource(mImageRefresh, bc.handle, bc.parent, bc.bi);
21 andreas 4036
        }
4037
        else
4038
        {
4039
            mImageRefresh = thref->mImageRefresh;
4040
 
4041
            if (!mImageRefresh)
4042
            {
4043
                MSG_ERROR("Error creating a new refresh class!");
4044
                return;
4045
            }
4046
 
4047
            mImageRefresh->setInterval(std::chrono::seconds(resource->refresh));
4048
            mImageRefresh->setUsername(resource->user);
4049
            mImageRefresh->setPassword(resource->password);
4050
 
4051
            if (resource->preserve)
4052
                mImageRefresh->setRunOnce();
4053
        }
4054
 
97 andreas 4055
        if (mImageRefresh->isRunning())
4056
            mImageRefresh->stopWait();
4057
 
4058
        if (!mImageRefresh->isRunning() && !_restart_)
21 andreas 4059
        {
4060
            MSG_DEBUG("Starting a refresh thread.");
4061
            mImageRefresh->run(url);
4062
        }
4063
    }
4064
    else if (resource->refresh == 0 && !resource->dynamo)
4065
    {
4066
        MSG_DEBUG("Retrieving single image");
4067
 
94 andreas 4068
        if (bc.handle == 0)
4069
        {
4070
            MSG_ERROR("Invalid bitmap cache!");
4071
            return;
4072
        }
4073
 
165 andreas 4074
        if (instance < 0 || (size_t)instance >= sr.size())
4075
        {
4076
            MSG_ERROR("Invalid instance " << instance);
4077
            return;
4078
        }
4079
 
97 andreas 4080
        // Check whether we have this image already
4081
        SkBitmap bitm;
4082
        bool cached = false;
4083
 
165 andreas 4084
        cached = TImgCache::getBitmap(url, &bitm, _BMTYPE_URL);
94 andreas 4085
        BITMAP_CACHE bmCache = getBCentryByHandle(bc.handle, bc.parent);
4086
 
97 andreas 4087
        if (!cached)    // If the bitmap was not in cache we must load it
94 andreas 4088
        {
97 andreas 4089
            MSG_DEBUG("Image not in cache. Downloading it ...");
4090
            THTTPClient *WEBClient = nullptr;
94 andreas 4091
 
97 andreas 4092
            if (bmCache.handle == 0)
4093
            {
271 andreas 4094
                MSG_ERROR("Couldn't find the handle " << handleToString(bc.handle) << " in bitmap cache!");
97 andreas 4095
                return;
4096
            }
4097
 
21 andreas 4098
            char *content = nullptr;
4099
            size_t length = 0, contentlen = 0;
94 andreas 4100
            WEBClient = new THTTPClient;
21 andreas 4101
 
94 andreas 4102
            if (!WEBClient || (content = WEBClient->tcall(&length, url, resource->user, resource->password)) == nullptr)
97 andreas 4103
            {
4104
                if (WEBClient)
4105
                    delete WEBClient;
4106
 
4107
                if (bc.show)
4108
                {
4109
                    setReady(bmCache.handle);
4110
                    showBitmapCache();
4111
                }
4112
                else
4113
                    setInvalid(bc.handle);
4114
 
21 andreas 4115
                return;
97 andreas 4116
            }
21 andreas 4117
 
94 andreas 4118
            contentlen = WEBClient->getContentSize();
21 andreas 4119
            MSG_DEBUG("Loaded " << contentlen << " bytes:");
4120
            sk_sp<SkData> data = SkData::MakeWithCopy(content, contentlen);
4121
 
97 andreas 4122
            if (!data || _restart_)
21 andreas 4123
            {
94 andreas 4124
                delete WEBClient;
21 andreas 4125
                MSG_ERROR("Error making image data!");
97 andreas 4126
 
4127
                if (bc.show)
4128
                {
4129
                    setReady(bmCache.handle);
4130
                    showBitmapCache();
4131
                }
4132
                else
4133
                    setInvalid(bc.handle);
4134
 
21 andreas 4135
                return;
4136
            }
4137
 
4138
            SkBitmap image;
4139
 
4140
            if (!DecodeDataToBitmap(data, &image))
4141
            {
97 andreas 4142
                delete WEBClient;
21 andreas 4143
                MSG_ERROR("Error creating an image!");
97 andreas 4144
 
4145
                if (bc.show)
4146
                {
4147
                    setReady(bmCache.handle);
4148
                    showBitmapCache();
4149
                }
4150
                else
4151
                    setInvalid(bc.handle);
4152
 
21 andreas 4153
                return;
4154
            }
4155
 
97 andreas 4156
            // Put this image into the static image cache
165 andreas 4157
            TImgCache::addImage(url, image, _BMTYPE_URL);
97 andreas 4158
            // Make the button complete
94 andreas 4159
            loadImage(&bmCache.bitmap, image, instance);
99 andreas 4160
            drawAlongOrder(&bmCache.bitmap, instance);
94 andreas 4161
            setBCBitmap(bmCache.handle, bmCache.bitmap);
4162
            setReady(bmCache.handle);
4163
            delete WEBClient;
97 andreas 4164
            // Display the image
94 andreas 4165
            showBitmapCache();
21 andreas 4166
            return;
4167
        }
97 andreas 4168
        else
21 andreas 4169
        {
97 andreas 4170
            MSG_DEBUG("Found image in cache. Using it ...");
165 andreas 4171
 
4172
            if (instance < 0 || (size_t)instance >= sr.size())
4173
            {
4174
                MSG_ERROR("Invalid instance " << instance);
4175
                return;
4176
            }
4177
 
97 andreas 4178
            loadImage(&bmCache.bitmap, bitm, instance);
4179
            setInvalid(bc.handle);
94 andreas 4180
 
97 andreas 4181
            if (bc.show && _displayButton)
176 andreas 4182
            {
289 andreas 4183
                TBitmap image((unsigned char *)bmCache.bitmap.getPixels(), bmCache.bitmap.info().width(), bmCache.bitmap.info().height());
298 andreas 4184
                _displayButton(bc.handle, bc.parent, image, bc.width, bc.height, bc.left, bc.top, isPassThrough());
176 andreas 4185
                mChanged = false;
4186
            }
21 andreas 4187
        }
4188
    }
97 andreas 4189
    else if (!_restart_)
21 andreas 4190
    {
4191
        MSG_DEBUG("Retrieving a video");
4192
 
4193
        if (_playVideo && !prg_stopped)
4194
        {
4195
            ulong parent = (mHandle >> 16) & 0x0000ffff;
4196
            _playVideo(mHandle, parent, lt, tp, wt, ht, url, resource->user, resource->password);
4197
        }
4198
    }
4199
}
247 andreas 4200
#ifdef Q_OS_ANDROID
38 andreas 4201
void TButton::funcBattery(int level, bool charging, int /* chargeType */)
23 andreas 4202
{
38 andreas 4203
    DECL_TRACER("TButton::funcBattery(int level, bool charging, int chargeType)");
59 andreas 4204
 
23 andreas 4205
    // Battery level is always a bargraph
249 andreas 4206
    if (ap == 0 && ad == SYSTEM_ITEM_BATTERYLEVEL)       // Not charging
23 andreas 4207
    {
38 andreas 4208
        mEnabled = !charging;
177 andreas 4209
        mChanged = true;
23 andreas 4210
 
4211
        if (!mEnabled && visible)
4212
            hide(true);
4213
        else if (mEnabled)
4214
        {
4215
            visible = true;
38 andreas 4216
            drawBargraph(mActInstance, level, visible);
23 andreas 4217
        }
4218
    }
249 andreas 4219
    else if (ap == 0 && ad == SYSTEM_ITEM_BATTERYCHARGING)  // Charging
23 andreas 4220
    {
38 andreas 4221
        mEnabled = charging;
177 andreas 4222
        mChanged = true;
23 andreas 4223
 
4224
        if (!mEnabled && visible)
4225
            hide(true);
4226
        else if (mEnabled)
4227
        {
4228
            visible = true;
38 andreas 4229
            drawBargraph(mActInstance, level, visible);
23 andreas 4230
        }
4231
    }
4232
}
247 andreas 4233
#endif
4234
#ifdef Q_OS_IOS
4235
void TButton::funcBattery(int level, int state)
4236
{
4237
    DECL_TRACER("TButton::funcBattery(int level, bool charging, int chargeType)");
23 andreas 4238
 
247 andreas 4239
    // Battery level is always a bargraph
249 andreas 4240
    if (ap == 0 && ad == SYSTEM_ITEM_BATTERYLEVEL)       // Not charging
247 andreas 4241
    {
249 andreas 4242
        mEnabled = (state == 1 || state == 3);
247 andreas 4243
        mChanged = true;
4244
 
4245
        if (!mEnabled && visible)
4246
            hide(true);
4247
        else if (mEnabled)
4248
        {
4249
            visible = true;
4250
            drawBargraph(mActInstance, level, visible);
4251
        }
4252
    }
249 andreas 4253
    else if (ap == 0 && ad == SYSTEM_ITEM_BATTERYCHARGING)  // Charging
247 andreas 4254
    {
4255
        mEnabled = (state == 2);
4256
        mChanged = true;
4257
 
4258
        if (!mEnabled && visible)
4259
            hide(true);
4260
        else if (mEnabled)
4261
        {
4262
            visible = true;
4263
            drawBargraph(mActInstance, level, visible);
4264
        }
4265
    }
4266
}
4267
#endif
36 andreas 4268
void TButton::funcNetworkState(int level)
4269
{
4270
    DECL_TRACER("TButton::funcNetworkState(int level)");
4271
 
38 andreas 4272
    if (level >= rl && level <= rh)
4273
    {
4274
        mLastLevel = level;
177 andreas 4275
        mChanged = true;
38 andreas 4276
        drawMultistateBargraph(mLastLevel);
4277
    }
36 andreas 4278
}
4279
 
21 andreas 4280
bool TButton::loadImage(SkBitmap* bm, SkBitmap& image, int instance)
4281
{
4282
    DECL_TRACER("TButton::loadImage(SkBitmap* bm, SkBitmap& image, int instance)");
4283
 
94 andreas 4284
    if (!bm)
4285
    {
4286
        MSG_WARNING("Got no image to load!");
4287
        return false;
4288
    }
4289
 
165 andreas 4290
    if (instance < 0 || (size_t)instance >= sr.size())
4291
    {
4292
        MSG_ERROR("Invalid instance " << instance);
4293
        return false;
4294
    }
4295
 
21 andreas 4296
    SkImageInfo info = image.info();
99 andreas 4297
    IMAGE_SIZE_t isize = calcImageSize(image.info().width(), image.info().height(), instance, true);
4298
    POSITION_t position = calcImagePosition((sr[instance].sb ? isize.width : info.width()), (sr[instance].sb ? isize.height : info.height()), SC_BITMAP, instance);
4299
//    POSITION_t position = calcImagePosition(info.width(), info.height(), SC_BITMAP, instance);
21 andreas 4300
 
4301
    if (!position.valid)
4302
    {
4303
        MSG_ERROR("Error calculating the position of the image for button number " << bi);
4304
        return false;
4305
    }
4306
 
94 andreas 4307
    MSG_DEBUG("New image position: left=" << position.left << ", top=" << position.top << ", width=" << position.width << ", height=" << position.height);
4308
    MSG_DEBUG("Image size : width=" << info.width() << ", height=" << info.height());
4309
    MSG_DEBUG("Bitmap size: width=" << bm->info().width() << ", height=" << bm->info().height());
21 andreas 4310
    MSG_DEBUG("Putting bitmap on top of image ...");
4311
    SkPaint paint;
4312
    paint.setBlendMode(SkBlendMode::kSrc);
4313
 
254 andreas 4314
    SkCanvas can(*bm, SkSurfaceProps());
21 andreas 4315
 
4316
    if (sr[instance].sb == 0)
179 andreas 4317
    {
365 andreas 4318
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(image);
179 andreas 4319
        can.drawImage(_image, position.left, position.top, SkSamplingOptions(), &paint);
4320
    }
21 andreas 4321
    else    // Scale to fit
4322
    {
179 andreas 4323
//        paint.setFilterQuality(kHigh_SkFilterQuality);
99 andreas 4324
        SkRect rect = SkRect::MakeXYWH(position.left, position.top, isize.width, isize.height);
365 andreas 4325
        sk_sp<SkImage> im = SkImages::RasterFromBitmap(image);
179 andreas 4326
        can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
21 andreas 4327
    }
4328
 
4329
    return true;
4330
}
4331
 
111 andreas 4332
bool TButton::barLevel(SkBitmap* bm, int, int level)
15 andreas 4333
{
100 andreas 4334
    DECL_TRACER("TButton::barLevel(SkBitmap* bm, int inst, int level)");
15 andreas 4335
 
30 andreas 4336
    if (!sr[0].mi.empty()  && sr[0].bs.empty() && !sr[1].bm.empty())       // Chameleon image?
15 andreas 4337
    {
38 andreas 4338
        MSG_TRACE("Chameleon image ...");
165 andreas 4339
        SkBitmap bmMi, bmBm;
15 andreas 4340
 
167 andreas 4341
        TImgCache::getBitmap(sr[0].mi, &bmMi, _BMTYPE_CHAMELEON, &sr[0].mi_width, &sr[0].mi_height);
4342
        TImgCache::getBitmap(sr[1].bm, &bmBm, _BMTYPE_BITMAP, &sr[1].bm_width, &sr[1].bm_height);
165 andreas 4343
        SkBitmap imgRed(bmMi);
4344
        SkBitmap imgMask(bmBm);
4345
 
20 andreas 4346
        SkBitmap img;
4347
        SkPixmap pixmapRed = imgRed.pixmap();
4348
        SkPixmap pixmapMask;
15 andreas 4349
 
20 andreas 4350
        if (!imgMask.empty())
4351
            pixmapMask = imgMask.pixmap();
15 andreas 4352
 
20 andreas 4353
        int width = sr[0].mi_width;
4354
        int height = sr[0].mi_height;
4355
        int startX = 0;
4356
        int startY = 0;
100 andreas 4357
        // Calculation: width / <effective pixels> * level
4358
        // Calculation: height / <effective pixels> * level
20 andreas 4359
        if (dr.compare("horizontal") == 0)
4360
            width = (int)((double)width / ((double)rh - (double)rl) * (double)level);
4361
        else
4362
            height = (int)((double)height / ((double)rh - (double)rl) * (double)level);
4363
 
59 andreas 4364
        if (ri && dr.compare("horizontal") == 0)
20 andreas 4365
        {
59 andreas 4366
            startX = sr[0].mi_width - width;
20 andreas 4367
            width = sr[0].mi_width;
59 andreas 4368
        }
4369
        else if (dr.compare("horizontal") != 0)
4370
        {
4371
            startY = sr[0].mi_height - height;
20 andreas 4372
            height = sr[0].mi_height;
4373
        }
15 andreas 4374
 
254 andreas 4375
        if (!allocPixels(sr[0].mi_width, sr[0].mi_height, &img))
4376
            return false;
4377
 
20 andreas 4378
        SkCanvas canvas(img);
4379
        SkColor col1 = TColor::getSkiaColor(sr[1].cf);
4380
        SkColor col2 = TColor::getSkiaColor(sr[1].cb);
15 andreas 4381
 
20 andreas 4382
        for (int ix = 0; ix < sr[0].mi_width; ix++)
4383
        {
4384
            for (int iy = 0; iy < sr[0].mi_height; iy++)
4385
            {
4386
                SkPaint paint;
4387
                SkColor pixel;
15 andreas 4388
 
20 andreas 4389
                if (ix >= startX && ix < width && iy >= startY && iy < height)
15 andreas 4390
                {
20 andreas 4391
                    SkColor pixelRed = pixmapRed.getColor(ix, iy);
4392
                    SkColor pixelMask;
15 andreas 4393
 
20 andreas 4394
                    if (!imgMask.empty())
4395
                        pixelMask = pixmapMask.getColor(ix, iy);
15 andreas 4396
                    else
20 andreas 4397
                        pixelMask = SK_ColorWHITE;
15 andreas 4398
 
20 andreas 4399
                    pixel = baseColor(pixelRed, pixelMask, col1, col2);
15 andreas 4400
                }
20 andreas 4401
                else
4402
                    pixel = SK_ColorTRANSPARENT;
15 andreas 4403
 
20 andreas 4404
                paint.setColor(pixel);
4405
                canvas.drawPoint(ix, iy, paint);
15 andreas 4406
            }
20 andreas 4407
        }
15 andreas 4408
 
20 andreas 4409
        if (img.empty())
4410
        {
4411
            MSG_ERROR("Error creating the cameleon image \"" << sr[0].mi << "\" / \"" << sr[0].bm << "\"!");
4412
            TError::setError();
4413
            return false;
4414
        }
15 andreas 4415
 
254 andreas 4416
        SkCanvas ctx(img, SkSurfaceProps());
20 andreas 4417
        SkPaint paint;
4418
        paint.setBlendMode(SkBlendMode::kSrcATop);
365 andreas 4419
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(imgMask);
179 andreas 4420
        ctx.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);
15 andreas 4421
 
20 andreas 4422
        POSITION_t position = calcImagePosition(sr[0].mi_width, sr[0].mi_height, SC_BITMAP, 0);
15 andreas 4423
 
20 andreas 4424
        if (!position.valid)
4425
        {
4426
            MSG_ERROR("Error calculating the position of the image for button number " << bi << ": " << na);
4427
            TError::setError();
4428
            return false;
15 andreas 4429
        }
4430
 
254 andreas 4431
        SkCanvas can(*bm, SkSurfaceProps());
20 andreas 4432
        paint.setBlendMode(SkBlendMode::kSrc);
365 andreas 4433
        _image = SkImages::RasterFromBitmap(img);
179 andreas 4434
        can.drawImage(_image, position.left, position.top, SkSamplingOptions(), &paint);
15 andreas 4435
    }
59 andreas 4436
    else if (!sr[0].bm.empty() && !sr[1].bm.empty())
15 andreas 4437
    {
23 andreas 4438
        MSG_TRACE("Drawing normal image ...");
165 andreas 4439
        SkBitmap image1, image2;
4440
 
167 andreas 4441
        TImgCache::getBitmap(sr[0].bm, &image1, _BMTYPE_BITMAP, &sr[0].bm_width, &sr[0].bm_height);   // State when level = 0%
4442
        TImgCache::getBitmap(sr[1].bm, &image2, _BMTYPE_BITMAP, &sr[1].bm_width, &sr[1].bm_height);   // State when level = 100%
254 andreas 4443
        SkCanvas can_bm(*bm, SkSurfaceProps());
15 andreas 4444
 
59 andreas 4445
 
23 andreas 4446
        if (image1.empty())
15 andreas 4447
        {
23 andreas 4448
            MSG_ERROR("Error creating the image \"" << sr[0].bm << "\"!");
15 andreas 4449
            TError::setError();
4450
            return false;
4451
        }
4452
 
23 andreas 4453
        if (image2.empty())
4454
        {
4455
            MSG_ERROR("Error creating the image \"" << sr[1].bm << "\"!");
4456
            TError::setError();
4457
            return false;
4458
        }
4459
 
4460
        int width = sr[1].bm_width;
4461
        int height = sr[1].bm_height;
15 andreas 4462
        int startX = 0;
4463
        int startY = 0;
4464
 
100 andreas 4465
        // Calculation: width / <effective pixels> * level
4466
        // Calculation: height / <effective pixels> * level
15 andreas 4467
        if (dr.compare("horizontal") == 0)
4468
            width = (int)((double)width / ((double)rh - (double)rl) * (double)level);
4469
        else
4470
            height = (int)((double)height / ((double)rh - (double)rl) * (double)level);
4471
 
59 andreas 4472
        if (ri && dr.compare("horizontal") == 0)     // range inverted?
15 andreas 4473
        {
59 andreas 4474
            startX = sr[0].mi_width - width;
15 andreas 4475
            width = sr[0].mi_width;
59 andreas 4476
        }
4477
        else if (dr.compare("horizontal") != 0)
4478
        {
4479
            startY = sr[0].mi_height - height;
15 andreas 4480
            height = sr[0].mi_height;
4481
        }
4482
 
28 andreas 4483
        MSG_DEBUG("dr=" << dr << ", startX=" << startX << ", startY=" << startY << ", width=" << width << ", height=" << height << ", level=" << level);
23 andreas 4484
        MSG_TRACE("Creating bargraph ...");
59 andreas 4485
        SkBitmap img_bar;
254 andreas 4486
 
4487
        if (!allocPixels(sr[1].bm_width, sr[1].bm_height, &img_bar))
4488
            return false;
4489
 
59 andreas 4490
        img_bar.eraseColor(SK_ColorTRANSPARENT);
254 andreas 4491
        SkCanvas bar(img_bar, SkSurfaceProps());
23 andreas 4492
 
4493
        for (int ix = 0; ix < sr[1].bm_width; ix++)
4494
        {
4495
            for (int iy = 0; iy < sr[1].bm_height; iy++)
4496
            {
4497
                SkPaint paint;
4498
                SkColor pixel;
4499
 
4500
                if (ix >= startX && ix < width && iy >= startY && iy < height)
4501
                    pixel = image2.getColor(ix, iy);
4502
                else
4503
                    pixel = SK_ColorTRANSPARENT;
4504
 
4505
                paint.setColor(pixel);
59 andreas 4506
                bar.drawPoint(ix, iy, paint);
23 andreas 4507
            }
4508
        }
4509
 
15 andreas 4510
        SkPaint paint;
59 andreas 4511
        paint.setBlendMode(SkBlendMode::kSrc);
365 andreas 4512
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(image1);
179 andreas 4513
        can_bm.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);
23 andreas 4514
        paint.setBlendMode(SkBlendMode::kSrcATop);
365 andreas 4515
        _image = SkImages::RasterFromBitmap(img_bar);
179 andreas 4516
        can_bm.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);       // Draw the above created image over the 0% image
142 andreas 4517
    }
4518
    else if (sr[0].bm.empty() && !sr[1].bm.empty())     // Only one bitmap in the second instance
4519
    {
4520
        MSG_TRACE("Drawing second image " << sr[1].bm << " ...");
165 andreas 4521
        SkBitmap image;
167 andreas 4522
        TImgCache::getBitmap(sr[1].bm, &image, _BMTYPE_BITMAP, &sr[1].bm_width, &sr[1].bm_height);   // State when level = 100%
254 andreas 4523
        SkCanvas can_bm(*bm, SkSurfaceProps());
142 andreas 4524
 
4525
        if (image.empty())
4526
        {
4527
            MSG_ERROR("Error creating the image \"" << sr[1].bm << "\"!");
4528
            TError::setError();
4529
            return false;
4530
        }
4531
 
4532
        int width = sr[1].bm_width;
4533
        int height = sr[1].bm_height;
4534
        int startX = 0;
4535
        int startY = 0;
4536
 
4537
        // Calculation: width / <effective pixels> * level
4538
        // Calculation: height / <effective pixels> * level
4539
        if (dr.compare("horizontal") == 0)
4540
            width = (int)((double)width / ((double)rh - (double)rl) * (double)level);
4541
        else
4542
            height = (int)((double)height / ((double)rh - (double)rl) * (double)level);
4543
 
4544
        if (ri && dr.compare("horizontal") == 0)     // range inverted?
4545
        {
4546
            startX = sr[1].mi_width - width;
4547
            width = sr[1].mi_width;
4548
        }
4549
        else if (dr.compare("horizontal") != 0)
4550
        {
4551
            startY = sr[1].mi_height - height;
4552
            height = sr[1].mi_height;
4553
        }
4554
 
4555
        MSG_DEBUG("dr=" << dr << ", startX=" << startX << ", startY=" << startY << ", width=" << width << ", height=" << height << ", level=" << level);
4556
        MSG_TRACE("Creating bargraph ...");
4557
        SkBitmap img_bar;
254 andreas 4558
 
4559
        if (!allocPixels(sr[1].bm_width, sr[1].bm_height, &img_bar))
4560
            return false;
4561
 
142 andreas 4562
        img_bar.eraseColor(SK_ColorTRANSPARENT);
254 andreas 4563
        SkCanvas bar(img_bar, SkSurfaceProps());
142 andreas 4564
        SkPaint pt;
4565
 
4566
        for (int ix = 0; ix < sr[1].bm_width; ix++)
4567
        {
4568
            for (int iy = 0; iy < sr[1].bm_height; iy++)
4569
            {
4570
                SkColor pixel;
4571
 
4572
                if (ix >= startX && ix < width && iy >= startY && iy < height)
4573
                    pixel = image.getColor(ix, iy);
4574
                else
4575
                    pixel = SK_ColorTRANSPARENT;
4576
 
4577
                pt.setColor(pixel);
4578
                bar.drawPoint(ix, iy, pt);
4579
            }
4580
        }
4581
 
4582
        SkPaint paint;
4583
        paint.setBlendMode(SkBlendMode::kSrcOver);
365 andreas 4584
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(img_bar);
179 andreas 4585
        can_bm.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);      // Draw the above created image over the 0% image
15 andreas 4586
    }
4587
    else
4588
    {
38 andreas 4589
        MSG_TRACE("No bitmap defined.");
4590
        int width = wt;
4591
        int height = ht;
15 andreas 4592
        int startX = 0;
4593
        int startY = 0;
4594
 
100 andreas 4595
        // Calculation: width / <effective pixels> * level = <level position>
4596
        // Calculation: height / <effective pixels> * level = <level position>
15 andreas 4597
        if (dr.compare("horizontal") == 0)
4598
            width = (int)((double)width / ((double)rh - (double)rl) * (double)level);
4599
        else
4600
            height = (int)((double)height / ((double)rh - (double)rl) * (double)level);
4601
 
59 andreas 4602
        if (ri && dr.compare("horizontal") == 0)
15 andreas 4603
        {
59 andreas 4604
            startX = wt - width;
38 andreas 4605
            width = wt;
59 andreas 4606
        }
4607
        else if (dr.compare("horizontal") != 0)
4608
        {
4609
            startY = ht - height;
38 andreas 4610
            height = ht;
15 andreas 4611
        }
4612
 
4613
        SkPaint paint;
4614
        paint.setBlendMode(SkBlendMode::kSrc);
254 andreas 4615
        SkCanvas can(*bm, SkSurfaceProps());
20 andreas 4616
        paint.setStyle(SkPaint::kFill_Style);
4617
        paint.setAntiAlias(true);
4618
        paint.setStrokeWidth(4);
38 andreas 4619
        paint.setColor(TColor::getSkiaColor(sr[1].cf));
4620
        MSG_DEBUG("Drawing rectangle: X=" << startX << ", Y=" << startY << ", W=" << width << ", H=" << height << ", level=" << level);
15 andreas 4621
        SkRect dst;
4622
        dst.setXYWH(startX, startY, width, height);
20 andreas 4623
        can.drawRect(dst, paint);
99 andreas 4624
        // If we have a slider button defined, we must draw it. To do it, we
4625
        // must look into the system resources to find the credentials to draw
4626
        // the button.
4627
        if (!sd.empty())
4628
        {
4629
            MSG_DEBUG("Attempt to draw the slider button \"" << sd << "\".");
100 andreas 4630
            int innerW = 0;
4631
            int innerH = 0;
4632
 
99 andreas 4633
            SkBitmap slButton = drawSliderButton(sd, TColor::getSkiaColor(sc));
4634
 
4635
            if (slButton.empty())
4636
            {
4637
                MSG_ERROR("Error drawing the slicer button " << sd);
4638
                return true;
4639
            }
4640
 
4641
            double scaleW, scaleH;
100 andreas 4642
            int border_size = getBorderSize(sr[0].bs);
99 andreas 4643
 
4644
            if (dr.compare("horizontal") != 0)
4645
            {
4646
                double scale;
100 andreas 4647
                innerH = (int)((double)(height - border_size * 2 - slButton.info().height() / 2) / ((double)rh - (double)rl) * (double)level) + border_size + slButton.info().height() / 2;
4648
                innerW = width;
4649
                scale = (double)(wt - border_size * 2) / (double)slButton.info().width();
99 andreas 4650
                scaleW = scale;
100 andreas 4651
                scaleH = 1.0;
4652
 
4653
                if (!ri)
4654
                    innerH = height - innerH;
99 andreas 4655
            }
4656
            else
4657
            {
4658
                double scale;
100 andreas 4659
                scale = (double)(ht - border_size * 2) / (double)slButton.info().height();
4660
                scaleW = 1.0;
99 andreas 4661
                scaleH = scale;
100 andreas 4662
                innerW = (int)((double)(width - border_size * 2 - slButton.info().width() / 2) / ((double)rh - (double)rl) * (double)level) + border_size + slButton.info().width() / 2;
4663
                innerH = height;
4664
 
4665
                if (!ri)
4666
                    innerW = width - innerW;
99 andreas 4667
            }
100 andreas 4668
 
99 andreas 4669
            if (scaleImage(&slButton, scaleW, scaleH))
4670
            {
4671
                int w = slButton.info().width();
4672
                int h = slButton.info().height();
4673
 
4674
                if (dr.compare("horizontal") == 0)
4675
                {
100 andreas 4676
                    int pos = innerW;
4677
                    dst.setXYWH(pos - w / 2, border_size, w, h);
99 andreas 4678
                }
4679
                else
4680
                {
100 andreas 4681
                    int pos = innerH;
4682
                    dst.setXYWH(border_size, pos - h / 2, w, h);
99 andreas 4683
                }
4684
 
4685
                SkPaint pnt;
100 andreas 4686
                pnt.setBlendMode(SkBlendMode::kSrcOver);
365 andreas 4687
                sk_sp<SkImage> _image = SkImages::RasterFromBitmap(slButton);
179 andreas 4688
                can.drawImageRect(_image, dst, SkSamplingOptions(), &pnt);
99 andreas 4689
            }
4690
        }
15 andreas 4691
    }
4692
 
4693
    return true;
4694
}
4695
 
99 andreas 4696
SkBitmap TButton::drawSliderButton(const string& slider, SkColor col)
4697
{
4698
    DECL_TRACER("TButton::drawSliderButton(const string& slider)");
4699
 
4700
    SkBitmap slButton;
4701
    // First we look for the slider button.
4702
    if (!gPageManager || !gPageManager->getSystemDraw()->existSlider(slider))
4703
        return slButton;
4704
 
4705
    // There exists one with the wanted name. We grab it and create
4706
    // the images from the files.
4707
    SLIDER_STYLE_t sst;
4708
 
4709
    if (!gPageManager->getSystemDraw()->getSlider(slider, &sst))    // should never be true!
4710
    {
4711
        MSG_ERROR("No slider entry found!");
4712
        return slButton;
4713
    }
4714
 
4715
    int width, height;
4716
 
4717
    if (dr.compare("horizontal") != 0)
4718
    {
100 andreas 4719
        width = (sst.fixedSize / 2) * 2 + sst.fixedSize;
99 andreas 4720
        height = sst.fixedSize;
4721
    }
4722
    else
4723
    {
4724
        width = sst.fixedSize;
100 andreas 4725
        height = (sst.fixedSize / 2) * 2 + sst.fixedSize;
99 andreas 4726
    }
4727
 
4728
    // Retrieve all available slider graphics files from the system
4729
    vector<SLIDER_t> sltList = gPageManager->getSystemDraw()->getSliderFiles(slider);
4730
 
4731
    if (sltList.empty())
4732
    {
4733
        MSG_ERROR("No system slider graphics found!");
4734
        return SkBitmap();
4735
    }
4736
 
4737
    SkPaint paint;
4738
    paint.setBlendMode(SkBlendMode::kSrc);
254 andreas 4739
 
4740
    if (!allocPixels(width, height, &slButton))
4741
        return slButton;
4742
 
99 andreas 4743
    slButton.eraseColor(SK_ColorTRANSPARENT);
254 andreas 4744
    SkCanvas slCan(slButton, SkSurfaceProps());
99 andreas 4745
    vector<SLIDER_t>::iterator sltIter;
4746
    // Loop through list of slider graphic files
4747
    for (sltIter = sltList.begin(); sltIter != sltList.end(); ++sltIter)
4748
    {
4749
        SkBitmap slPart;
4750
        SkBitmap slPartAlpha;
4751
        SkRect dst;
4752
 
4753
        if (dr.compare("horizontal") != 0 && (sltIter->type == SGR_LEFT || sltIter->type == SGR_RIGHT || sltIter->type == SGR_VERTICAL))    // vertical slider
4754
        {
4755
            if (!retrieveImage(sltIter->path, &slPart))     // Get the mask
4756
            {
4757
                MSG_ERROR("Missing slider button mask image " << sltIter->path);
4758
                return SkBitmap();
4759
            }
4760
 
4761
            if (!retrieveImage(sltIter->pathAlpha, &slPartAlpha))   // Get the alpha mask
4762
            {
4763
                MSG_ERROR("Missing slider button alpha image " << sltIter->pathAlpha);
4764
                return SkBitmap();
4765
            }
4766
 
4767
            SkBitmap sl = combineImages(slPart, slPartAlpha, col);
4768
 
4769
            if (sl.empty())
4770
                return sl;
4771
 
4772
            switch (sltIter->type)
4773
            {
4774
                case SGR_LEFT:      dst.setXYWH(0, 0, sl.info().width(), sl.info().height()); break;
4775
 
4776
                case SGR_VERTICAL:
100 andreas 4777
                    stretchImageWidth(&sl, sst.fixedSize);
99 andreas 4778
                    dst.setXYWH(sst.fixedSize / 2, 0, sl.info().width(), sl.info().height());
4779
                break;
4780
 
100 andreas 4781
                case SGR_RIGHT:     dst.setXYWH((sst.fixedSize / 2) + sst.fixedSize, 0, sl.info().width(), sl.info().height()); break;
99 andreas 4782
 
4783
                default:
4784
                    MSG_WARNING("Invalid type " << sltIter->type << " found!");
4785
            }
4786
 
365 andreas 4787
            sk_sp<SkImage> _image = SkImages::RasterFromBitmap(sl);
179 andreas 4788
            slCan.drawImageRect(_image, dst, SkSamplingOptions(), &paint);
99 andreas 4789
        }
4790
        else if (dr.compare("horizontal") == 0 && (sltIter->type == SGR_TOP || sltIter->type == SGR_BOTTOM || sltIter->type == SGR_HORIZONTAL)) // horizontal slider
4791
        {
4792
            if (!retrieveImage(sltIter->path, &slPart))
4793
            {
4794
                MSG_ERROR("Missing slider button image " << sltIter->path);
4795
                return SkBitmap();
4796
            }
4797
 
4798
            if (!retrieveImage(sltIter->pathAlpha, &slPartAlpha))
4799
            {
4800
                MSG_ERROR("Missing slider button image " << sltIter->pathAlpha);
4801
                return SkBitmap();
4802
            }
4803
 
4804
            SkBitmap sl = combineImages(slPart, slPartAlpha, col);
4805
 
4806
            if (sl.empty())
4807
                return sl;
4808
 
4809
            switch (sltIter->type)
4810
            {
4811
                case SGR_TOP:       dst.setXYWH(0, 0, sl.info().width(), sl.info().height()); break;
4812
 
4813
                case SGR_HORIZONTAL:
100 andreas 4814
                    stretchImageHeight(&sl, sst.fixedSize);
99 andreas 4815
                    dst.setXYWH(0, sst.fixedSize / 2, sl.info().width(), sl.info().height());
4816
                break;
4817
 
100 andreas 4818
                case SGR_BOTTOM:    dst.setXYWH(0, (sst.fixedSize / 2) + sst.fixedSize, sl.info().width(), sl.info().height()); break;
99 andreas 4819
 
4820
                default:
4821
                    MSG_WARNING("Invalid type " << sltIter->type << " found!");
4822
            }
4823
 
365 andreas 4824
            sk_sp<SkImage> _image = SkImages::RasterFromBitmap(sl);
179 andreas 4825
            slCan.drawImageRect(_image, dst, SkSamplingOptions(), &paint);
99 andreas 4826
        }
4827
    }
4828
 
4829
    return slButton;
4830
}
4831
 
8 andreas 4832
bool TButton::buttonIcon(SkBitmap* bm, int instance)
4833
{
4834
    DECL_TRACER("TButton::buttonIcon(SkBitmap* bm, int instance)");
4835
 
165 andreas 4836
    if (instance < 0 || (size_t)instance >= sr.size())
4837
    {
4838
        MSG_ERROR("Invalid instance " << instance);
4839
        return false;
4840
    }
4841
 
8 andreas 4842
    if (sr[instance].ii <= 0)
4843
    {
4844
        MSG_TRACE("No icon defined!");
4845
        return true;
7 andreas 4846
    }
4847
 
8 andreas 4848
    MSG_DEBUG("Drawing an icon ...");
4849
 
4850
    if (!gIcons)
4851
    {
192 andreas 4852
        MSG_WARNING("No icons were defined!");
4853
        return true;
4854
    }
8 andreas 4855
 
192 andreas 4856
    string file = gIcons->getFile(sr[instance].ii);
4857
 
4858
    if (file.empty())
4859
    {
4860
        MSG_WARNING("The icon " << sr[instance].ii << " was not found in table!");
4861
        return true;
8 andreas 4862
    }
4863
 
4864
    MSG_DEBUG("Loading icon file " << file);
4865
    sk_sp<SkData> image;
4866
    SkBitmap icon;
4867
 
4868
    if (!(image = readImage(file)))
192 andreas 4869
        return true;
8 andreas 4870
 
4871
    DecodeDataToBitmap(image, &icon);
4872
 
4873
    if (icon.empty())
4874
    {
4875
        MSG_WARNING("Could not create an icon for element " << sr[instance].ii << " on button " << bi << " (" << na << ")");
192 andreas 4876
        return true;
8 andreas 4877
    }
4878
 
4879
    SkImageInfo info = icon.info();
4880
    POSITION_t position = calcImagePosition(icon.width(), icon.height(), SC_ICON, instance);
4881
 
4882
    if (!position.valid)
4883
    {
4884
        MSG_ERROR("Error calculating the position of the image for button number " << bi);
4885
        TError::setError();
4886
        return false;
4887
    }
4888
 
10 andreas 4889
    MSG_DEBUG("Putting Icon on top of bitmap ...");
4890
    SkPaint paint;
4891
    paint.setBlendMode(SkBlendMode::kSrcOver);
254 andreas 4892
    SkCanvas can(*bm, SkSurfaceProps());
8 andreas 4893
 
17 andreas 4894
    if (position.overflow)
4895
    {
179 andreas 4896
        SkRect irect;
17 andreas 4897
        SkRect bdst;
4898
        SkBitmap dst;
4899
        int left = (position.left >= 0) ? 0 : position.left * -1;
4900
        int top = (position.top >= 0) ? 0 : position.top * -1;
4901
        int width = std::min(wt, info.width());
4902
        int height = std::min(ht, info.height());
4903
        irect.setXYWH(left, top, width, height);
4904
        bm->getBounds(&bdst);
365 andreas 4905
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(icon);
179 andreas 4906
        can.drawImageRect(_image, irect, bdst, SkSamplingOptions(), &paint, SkCanvas::kStrict_SrcRectConstraint);
17 andreas 4907
    }
4908
    else
179 andreas 4909
    {
365 andreas 4910
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(icon);
179 andreas 4911
        can.drawImage(_image, position.left, position.top, SkSamplingOptions(), &paint);
4912
    }
17 andreas 4913
 
8 andreas 4914
    return true;
4915
}
4916
 
38 andreas 4917
bool TButton::buttonText(SkBitmap* bm, int inst)
8 andreas 4918
{
38 andreas 4919
    DECL_TRACER("TButton::buttonText(SkBitmap* bm, int inst)");
8 andreas 4920
 
38 andreas 4921
    int instance = inst;
4922
 
4923
    if ((size_t)instance >= sr.size())
289 andreas 4924
        instance = (int)(sr.size() - 1);
38 andreas 4925
    else if (instance < 0)
4926
        instance = 0;
4927
 
289 andreas 4928
    if (sr[instance].te.empty())            // Is there a text?
4929
    {                                       // No, then return
67 andreas 4930
        MSG_DEBUG("Empty text string.");
4931
        return true;
4932
    }
4933
 
289 andreas 4934
    if (!mFonts)                            // Do we have any fonts?
4935
    {                                       // No, warn and return
4936
        MSG_WARNING("No fonts available to write a text!");
4937
        return true;
4938
    }
4939
 
67 andreas 4940
    MSG_DEBUG("Searching for font number " << sr[instance].fi << " with text " << sr[instance].te);
4941
    FONT_T font = mFonts->getFont(sr[instance].fi);
4942
 
161 andreas 4943
    if (font.file.empty())
7 andreas 4944
    {
161 andreas 4945
        MSG_WARNING("No font file name found for font " << sr[instance].fi);
4946
        return true;
4947
    }
7 andreas 4948
 
161 andreas 4949
    sk_sp<SkTypeface> typeFace = mFonts->getTypeFace(sr[instance].fi);
254 andreas 4950
    SkCanvas canvas(*bm);
161 andreas 4951
 
4952
    if (!typeFace)
4953
    {
164 andreas 4954
        MSG_WARNING("Error creating type face " << font.fullName);
161 andreas 4955
    }
4956
 
4957
    SkScalar fontSizePt = ((SkScalar)font.size * 1.322);
164 andreas 4958
    SkFont skFont;
4959
 
4960
    if (typeFace && typeFace->countTables() > 0)
4961
        skFont.setTypeface(typeFace);
4962
 
4963
    skFont.setSize(fontSizePt);
161 andreas 4964
    skFont.setEdging(SkFont::Edging::kAntiAlias);
4965
    MSG_DEBUG("Wanted font size: " << font.size << ", this is " << fontSizePt << " pt");
4966
 
4967
    SkPaint paint;
4968
    paint.setAntiAlias(true);
4969
    paint.setColor(TColor::getSkiaColor(sr[instance].ct));
4970
    paint.setStyle(SkPaint::kFill_Style);
4971
 
4972
    SkFontMetrics metrics;
4973
    skFont.getMetrics(&metrics);
4974
    int lines = numberLines(sr[instance].te);
164 andreas 4975
//    MSG_DEBUG("fAvgCharWidth: " << metrics.fAvgCharWidth);
4976
//    MSG_DEBUG("fCapHeight:    " << metrics.fCapHeight);
4977
//    MSG_DEBUG("fAscent:       " << metrics.fAscent);
4978
//    MSG_DEBUG("fDescent:      " << metrics.fDescent);
4979
//    MSG_DEBUG("fLeading:      " << metrics.fLeading);
4980
//    MSG_DEBUG("fXHeight:      " << metrics.fXHeight);
161 andreas 4981
 
4982
    MSG_DEBUG("Found " << lines << " lines.");
4983
 
4984
    if (lines > 1 || sr[instance].ww)
4985
    {
4986
        vector<string> textLines;
4987
 
4988
        if (!sr[instance].ww)
4989
            textLines = splitLine(sr[instance].te);
4990
        else
6 andreas 4991
        {
161 andreas 4992
            textLines = splitLine(sr[instance].te, wt, ht, skFont, paint);
289 andreas 4993
            lines = (int)textLines.size();
67 andreas 4994
        }
7 andreas 4995
 
161 andreas 4996
        MSG_DEBUG("Calculated number of lines: " << lines);
4997
        int lineHeight = (metrics.fAscent * -1) + metrics.fDescent;
4998
        int totalHeight = lineHeight * lines;
19 andreas 4999
 
161 andreas 5000
        if (totalHeight > ht)
5001
        {
5002
            lines = ht / lineHeight;
5003
            totalHeight = lineHeight * lines;
5004
        }
8 andreas 5005
 
161 andreas 5006
        MSG_DEBUG("Line height: " << lineHeight << ", total height: " << totalHeight);
5007
        vector<string>::iterator iter;
5008
        int line = 0;
5009
        int maxWidth = 0;
69 andreas 5010
 
161 andreas 5011
        if (textLines.size() > 0)
67 andreas 5012
        {
161 andreas 5013
            // Calculate the maximum width
5014
            for (iter = textLines.begin(); iter != textLines.end(); ++iter)
5015
            {
5016
                SkRect rect;
5017
                skFont.measureText(iter->c_str(), iter->length(), SkTextEncoding::kUTF8, &rect, &paint);
67 andreas 5018
 
161 andreas 5019
                if (rect.width() > maxWidth)
5020
                    maxWidth = rect.width();
67 andreas 5021
            }
8 andreas 5022
 
161 andreas 5023
            POSITION_t pos = calcImagePosition(maxWidth, totalHeight, SC_TEXT, instance);
8 andreas 5024
 
161 andreas 5025
            if (!pos.valid)
67 andreas 5026
            {
161 andreas 5027
                MSG_ERROR("Error calculating the text position!");
5028
                TError::setError();
5029
                return false;
67 andreas 5030
            }
40 andreas 5031
 
161 andreas 5032
            SkScalar lnHt = metrics.fAscent * -1;
96 andreas 5033
 
161 andreas 5034
            for (iter = textLines.begin(); iter != textLines.end(); ++iter)
69 andreas 5035
            {
161 andreas 5036
                sk_sp<SkTextBlob> blob = SkTextBlob::MakeFromString(iter->c_str(), skFont);
5037
                MSG_DEBUG("Trying to print line: " << *iter);
5038
                // We want to take care about the horizontal position.
5039
                SkRect rect;
5040
                skFont.measureText(iter->c_str(), iter->length(), SkTextEncoding::kUTF8, &rect, &paint);
5041
                SkScalar horizontal = 0.0;
5042
 
5043
                switch(sr[instance].jt)
96 andreas 5044
                {
161 andreas 5045
                    case ORI_BOTTOM_MIDDLE:
5046
                    case ORI_CENTER_MIDDLE:
5047
                    case ORI_TOP_MIDDLE:
5048
                        horizontal = (wt - rect.width()) / 2.0f;
5049
                    break;
40 andreas 5050
 
161 andreas 5051
                    case ORI_BOTTOM_RIGHT:
5052
                    case ORI_CENTER_RIGHT:
5053
                    case ORI_TOP_RIGHT:
5054
                        horizontal = wt - rect.width();
5055
                    break;
69 andreas 5056
 
161 andreas 5057
                    default:
5058
                        horizontal = pos.left;
96 andreas 5059
                }
40 andreas 5060
 
161 andreas 5061
                SkScalar startX = horizontal;
5062
                SkScalar startY = (SkScalar)pos.top + (SkScalar)lineHeight * (SkScalar)line;
5063
                MSG_DEBUG("x=" << startX << ", y=" << startY);
5064
                bool tEffect = false;
5065
                // Text effects
5066
                if (sr[instance].et > 0)
5067
                    tEffect = textEffect(&canvas, blob, startX, startY + lnHt, instance);
67 andreas 5068
 
161 andreas 5069
                if (!tEffect)
5070
                    canvas.drawTextBlob(blob.get(), startX, startY + lnHt, paint);
69 andreas 5071
 
161 andreas 5072
                line++;
69 andreas 5073
 
161 andreas 5074
                if (line > lines)
5075
                    break;
67 andreas 5076
            }
5077
        }
161 andreas 5078
    }
5079
    else    // single line
5080
    {
5081
        string text = sr[instance].te;
5082
        sk_sp<SkTextBlob> blob = SkTextBlob::MakeFromString(text.data(), skFont);
5083
        SkRect rect;
5084
        skFont.measureText(text.data(), text.size(), SkTextEncoding::kUTF8, &rect, &paint);
164 andreas 5085
        MSG_DEBUG("Calculated Skia rectangle of font: width=" << rect.width() << ", height=" << rect.height());
5086
        POSITION_t position;
66 andreas 5087
 
164 andreas 5088
        if (metrics.fCapHeight >= 1.0)
5089
            position = calcImagePosition(rect.width(), metrics.fCapHeight, SC_TEXT, instance);
5090
        else
5091
            position = calcImagePosition(rect.width(), rect.height(), SC_TEXT, instance);
5092
 
161 andreas 5093
        if (!position.valid)
5094
        {
5095
            MSG_ERROR("Error calculating the text position!");
5096
            TError::setError();
5097
            return false;
5098
        }
66 andreas 5099
 
161 andreas 5100
        MSG_DEBUG("Printing line " << text);
5101
        SkScalar startX = (SkScalar)position.left;
164 andreas 5102
        SkScalar startY = (SkScalar)position.top;
10 andreas 5103
 
164 andreas 5104
        if (metrics.fCapHeight >= 1.0)
5105
            startY += metrics.fCapHeight;   // This is the offset of the line
5106
        else
5107
            startY += rect.height();        // This is the offset of the line
5108
 
161 andreas 5109
        FONT_TYPE sym = TFont::isSymbol(typeFace);
5110
        bool tEffect = false;
5111
        // Text effects
5112
        if (sr[instance].et > 0)
164 andreas 5113
            tEffect = textEffect(&canvas, blob, startX, startY, instance);
67 andreas 5114
 
161 andreas 5115
        if (!tEffect && utf8Strlen(text) > 1)
164 andreas 5116
            canvas.drawTextBlob(blob.get(), startX, startY, paint);
161 andreas 5117
        else
5118
        {
5119
            int count = 0;
5120
            uint16_t *glyphs = nullptr;
5121
 
5122
            if (sym == FT_SYM_MS)
8 andreas 5123
            {
161 andreas 5124
                MSG_DEBUG("Microsoft proprietary symbol font detected.");
5125
                uint16_t *uni;
5126
                size_t num = TFont::utf8ToUtf16(text, &uni, true);
5127
                MSG_DEBUG("Got " << num << " unichars, first unichar: " << std::hex << std::setw(4) << std::setfill('0') << *uni << std::dec);
7 andreas 5128
 
161 andreas 5129
                if (num > 0)
8 andreas 5130
                {
161 andreas 5131
                    glyphs = new uint16_t[num];
5132
                    size_t glyphSize = sizeof(uint16_t) * num;
289 andreas 5133
                    count = skFont.textToGlyphs(uni, num, SkTextEncoding::kUTF16, glyphs, (int)glyphSize);
8 andreas 5134
 
161 andreas 5135
                    if (count <= 0)
67 andreas 5136
                    {
161 andreas 5137
                        delete[] glyphs;
5138
                        glyphs = TFont::textToGlyphs(text, typeFace, &num);
289 andreas 5139
                        count = (int)num;
67 andreas 5140
                    }
5141
                }
5142
                else
5143
                {
161 andreas 5144
                    canvas.drawTextBlob(blob.get(), startX, startY, paint);
5145
                    return true;
67 andreas 5146
                }
5147
 
161 andreas 5148
                if (uni)
5149
                    delete[] uni;
5150
            }
5151
            else if (tEffect)
5152
                return true;
5153
            else
5154
            {
5155
                glyphs = new uint16_t[text.size()];
5156
                size_t glyphSize = sizeof(uint16_t) * text.size();
289 andreas 5157
                count = skFont.textToGlyphs(text.data(), text.size(), SkTextEncoding::kUTF8, glyphs, (int)glyphSize);
161 andreas 5158
            }
156 andreas 5159
 
331 andreas 5160
            if (glyphs && count > 0)
161 andreas 5161
            {
5162
                MSG_DEBUG("1st glyph: 0x" << std::hex << std::setw(8) << std::setfill('0') << *glyphs << ", # glyphs: " << std::dec << count);
164 andreas 5163
                canvas.drawSimpleText(glyphs, sizeof(uint16_t) * count, SkTextEncoding::kGlyphID, startX, startY, skFont, paint);
8 andreas 5164
            }
161 andreas 5165
            else    // Try to print something
5166
            {
5167
                MSG_WARNING("Got no glyphs! Try to print: " << text);
331 andreas 5168
                canvas.drawString(text.data(), startX, startY, skFont, paint);
161 andreas 5169
            }
5170
 
331 andreas 5171
            if (glyphs)
5172
                delete[] glyphs;
8 andreas 5173
        }
5174
    }
5175
 
5176
    return true;
5177
}
5178
 
69 andreas 5179
int TButton::calcLineHeight(const string& text, SkFont& font)
40 andreas 5180
{
69 andreas 5181
    DECL_TRACER("TButton::calcLineHeight(const string& text, SkFont& font)");
40 andreas 5182
 
69 andreas 5183
    size_t pos = text.find("\n");       // Search for a line break.
5184
    string lText = text;
5185
 
5186
    if (pos != string::npos)            // Do we have found a line break?
5187
        lText = text.substr(0, pos - 1);// Yes, take only the text up to 1 before the line break (only 1 line).
5188
 
5189
    sk_sp<SkTextBlob> blob = SkTextBlob::MakeFromString(lText.c_str(), font);
40 andreas 5190
    SkRect rect = blob.get()->bounds();
5191
    return rect.height();
5192
}
5193
 
66 andreas 5194
bool TButton::textEffect(SkCanvas *canvas, sk_sp<SkTextBlob>& blob, SkScalar startX, SkScalar startY, int instance)
5195
{
5196
    DECL_TRACER("TButton::textEffect(SkBitmap *bm, int instance)");
5197
 
5198
    if (!canvas)
5199
        return false;
5200
 
165 andreas 5201
    if (instance < 0 || (size_t)instance >= sr.size())
5202
    {
5203
        MSG_ERROR("Invalid instance " << instance);
5204
        return false;
5205
    }
5206
 
66 andreas 5207
    // Drop Shadow
5208
    if (sr[instance].et >= 9 && sr[instance].et <= 32)
5209
    {
5210
        SkScalar gap = 0.0;
5211
        SkScalar sigma = 0.0;
5212
        SkScalar xDrop = 0.0;
5213
        SkScalar yDrop = 0.0;
5214
        uint8_t blurAlpha = 255;
5215
        SkPaint paint;
5216
        paint.setAntiAlias(true);
5217
        paint.setColor(TColor::getSkiaColor(sr[instance].ct));
5218
 
5219
        // Soft drop shadow
5220
        if (sr[instance].et >= 9 && sr[instance].et <= 16)
5221
        {
5222
            gap = (SkScalar)sr[instance].et - 8.0f;
5223
            sigma = 3.0f;
5224
            blurAlpha = 127;
5225
        }
5226
        else if (sr[instance].et >= 17 && sr[instance].et <= 24) // Medium drop shadow
5227
        {
5228
            gap = (SkScalar)sr[instance].et - 16.0f;
5229
            sigma = 2.0f;
5230
            blurAlpha = 159;
5231
        }
5232
        else    // Hard drop shadow
5233
        {
5234
            gap = (SkScalar)sr[instance].et - 24.0f;
5235
            sigma = 1.1f;
5236
            blurAlpha = 207;
5237
        }
5238
 
5239
        xDrop = gap;
5240
        yDrop = gap;
5241
        SkPaint blur(paint);
5242
        blur.setAlpha(blurAlpha);
5243
        blur.setColor(TColor::getSkiaColor(sr[instance].ec));
5244
        blur.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, 0));
365 andreas 5245
//        blur.setMaskFilter(SkImageFilters::Blur(sigma, sigma, 0));
66 andreas 5246
        canvas->drawTextBlob(blob.get(), startX + xDrop, startY + yDrop, blur);
5247
        canvas->drawTextBlob(blob.get(), startX, startY, paint);
5248
        return true;
5249
    }
161 andreas 5250
    else if (sr[instance].et >= 5 && sr[instance].et <= 8)  // Glow
5251
    {
5252
        SkScalar sigma = 0.0;
66 andreas 5253
 
161 andreas 5254
        switch(sr[instance].et)
5255
        {
5256
            case 5: sigma = 2.0; break;     // Glow-S
5257
            case 6: sigma = 4.0; break;     // Glow-M
5258
            case 7: sigma = 6.0; break;     // Glow-L
5259
            case 8: sigma = 8.0; break;     // Glow-X
5260
        }
5261
 
5262
        SkPaint paint, blur;
5263
        paint.setAntiAlias(true);
5264
        paint.setColor(TColor::getSkiaColor(sr[instance].ct));
5265
        blur.setColor(TColor::getSkiaColor(sr[instance].ec));
5266
        blur.setStyle(SkPaint::kStroke_Style);
5267
        blur.setStrokeWidth(sigma / 1.5);
5268
        blur.setMaskFilter(SkMaskFilter::MakeBlur(kOuter_SkBlurStyle, sigma));
5269
        canvas->drawTextBlob(blob.get(), startX, startY, paint);
5270
        canvas->drawTextBlob(blob.get(), startX, startY, blur);
5271
        return true;
5272
    }
5273
    else if (sr[instance].et >= 1 && sr[instance].et <= 4)  // Outline
5274
    {
5275
        SkScalar sigma = 0.0;
5276
 
5277
        switch(sr[instance].et)
5278
        {
5279
            case 1: sigma = 1.0; break;     // Outline-S
5280
            case 2: sigma = 2.0; break;     // Outline-M
5281
            case 3: sigma = 4.0; break;     // Outline-L
5282
            case 4: sigma = 6.0; break;     // Outline-X
5283
        }
5284
 
5285
        SkPaint paint, outline;
5286
        paint.setAntiAlias(true);
5287
        paint.setColor(TColor::getSkiaColor(sr[instance].ct));
5288
        outline.setAntiAlias(true);
5289
        outline.setColor(TColor::getSkiaColor(sr[instance].ec));
5290
        outline.setStyle(SkPaint::kStroke_Style);
5291
        outline.setStrokeWidth(sigma);
5292
        canvas->drawTextBlob(blob.get(), startX, startY, outline);
5293
        canvas->drawTextBlob(blob.get(), startX, startY, paint);
5294
        return true;
5295
    }
5296
 
66 andreas 5297
    return false;
5298
}
5299
 
317 andreas 5300
/**
5301
 * @brief TButton::buttonBorder - draw a border, if any.
5302
 * This method draws a border if there is one defined in \b sr[].bs. If there
5303
 * is also a global border defined in \b bs then this border is limiting the
5304
 * valid borders to it. The method does not check this, because it is subject
5305
 * to TPDesign.
5306
 *
5307
 * @param bm        Bitmap to draw the border on.
5308
 * @param inst      The instance where the border definitition should be taken from
5309
 *
5310
 * @return TRUE on success, otherwise FALSE.
5311
 */
159 andreas 5312
bool TButton::buttonBorder(SkBitmap* bm, int inst)
8 andreas 5313
{
5314
    DECL_TRACER("TButton::buttonBorder(SkBitmap* bm, int instance)");
5315
 
159 andreas 5316
    int instance = inst;
5317
 
5318
    if (instance < 0)
5319
        instance = 0;
5320
    else if ((size_t)instance > sr.size())
5321
        instance = (int)sr.size() - 1;
5322
 
317 andreas 5323
    if (sr[instance].bs.empty())
8 andreas 5324
    {
5325
        MSG_DEBUG("No border defined.");
5326
        return true;
5327
    }
5328
 
317 andreas 5329
    string bname = sr[instance].bs;
79 andreas 5330
    // Try to find the border in the system table
317 andreas 5331
    if (drawBorder(bm, bname, wt, ht, sr[instance].cb))
307 andreas 5332
        return true;
5333
 
5334
    // The border was not found or defined to be not drawn. Therefor we look
5335
    // into the system directory (__system/graphics/borders). If the wanted
5336
    // border exists there, we're drawing it.
160 andreas 5337
    BORDER_t bd, bda;
307 andreas 5338
    int numBorders = 0;
5339
    bool extBorder = false;
81 andreas 5340
 
339 andreas 5341
    if (sr.size() == 2)
5342
    {
317 andreas 5343
        if (gPageManager->getSystemDraw()->getBorder(bname, TSystemDraw::LT_OFF, &bd))
307 andreas 5344
            numBorders++;
339 andreas 5345
    }
5346
    else if (gPageManager->getSystemDraw()->getBorder(bname, TSystemDraw::LT_ON, &bd))
5347
        numBorders++;
169 andreas 5348
 
339 andreas 5349
    if (numBorders)
5350
        extBorder = true;
79 andreas 5351
 
307 andreas 5352
    if (extBorder)
5353
    {
5354
        SkColor color = TColor::getSkiaColor(sr[instance].cb);      // border color
5355
        MSG_DEBUG("Button color: #" << std::setw(6) << std::setfill('0') << std::hex << color);
5356
        // Load images
5357
        SkBitmap imgB, imgBR, imgR, imgTR, imgT, imgTL, imgL, imgBL;
169 andreas 5358
 
339 andreas 5359
        if (!retrieveImage(bd.b, &imgB) || imgB.empty())
307 andreas 5360
            return false;
79 andreas 5361
 
307 andreas 5362
        MSG_DEBUG("Got images " << bd.b << " and " << bda.b << " with size " << imgB.info().width() << " x " << imgB.info().height());
339 andreas 5363
        if (!retrieveImage(bd.br, &imgBR) || imgBR.empty())
307 andreas 5364
            return false;
160 andreas 5365
 
307 andreas 5366
        MSG_DEBUG("Got images " << bd.br << " and " << bda.br << " with size " << imgBR.info().width() << " x " << imgBR.info().height());
339 andreas 5367
        if (!retrieveImage(bd.r, &imgR) || imgR.empty())
307 andreas 5368
            return false;
79 andreas 5369
 
307 andreas 5370
        MSG_DEBUG("Got images " << bd.r << " and " << bda.r << " with size " << imgR.info().width() << " x " << imgR.info().height());
339 andreas 5371
        if (!retrieveImage(bd.tr, &imgTR) || imgTR.empty())
307 andreas 5372
            return false;
79 andreas 5373
 
307 andreas 5374
        MSG_DEBUG("Got images " << bd.tr << " and " << bda.tr << " with size " << imgTR.info().width() << " x " << imgTR.info().height());
339 andreas 5375
        if (!retrieveImage(bd.t, &imgT) || imgT.empty())
307 andreas 5376
            return false;
79 andreas 5377
 
307 andreas 5378
        MSG_DEBUG("Got images " << bd.t << " and " << bda.t << " with size " << imgT.info().width() << " x " << imgT.info().height());
339 andreas 5379
        if (!retrieveImage(bd.tl, &imgTL) || imgTL.empty())
307 andreas 5380
            return false;
79 andreas 5381
 
307 andreas 5382
        MSG_DEBUG("Got images " << bd.tl << " and " << bda.tl << " with size " << imgTL.info().width() << " x " << imgTL.info().height());
339 andreas 5383
        if (!retrieveImage(bd.l, &imgL) || imgL.empty())
307 andreas 5384
            return false;
79 andreas 5385
 
307 andreas 5386
        mBorderWidth = imgL.info().width();
339 andreas 5387
 
307 andreas 5388
        MSG_DEBUG("Got images " << bd.l << " and " << bda.l << " with size " << imgL.info().width() << " x " << imgL.info().height());
339 andreas 5389
        if (!retrieveImage(bd.bl, &imgBL) || imgBL.empty())
307 andreas 5390
            return false;
79 andreas 5391
 
307 andreas 5392
        MSG_DEBUG("Got images " << bd.bl << " and " << bda.bl << " with size " << imgBL.info().width() << " x " << imgBL.info().height());
5393
        MSG_DEBUG("Button image size: " << (imgTL.info().width() + imgT.info().width() + imgTR.info().width()) << " x " << (imgTL.info().height() + imgL.info().height() + imgBL.info().height()));
5394
        MSG_DEBUG("Total size: " << wt << " x " << ht);
5395
        stretchImageWidth(&imgB, wt - imgBL.info().width() - imgBR.info().width());
5396
        stretchImageWidth(&imgT, wt - imgTL.info().width() - imgTR.info().width());
5397
        stretchImageHeight(&imgL, ht - imgTL.info().height() - imgBL.info().height());
5398
        stretchImageHeight(&imgR, ht - imgTR.info().height() - imgBR.info().height());
5399
        MSG_DEBUG("Stretched button image size: " << (imgTL.info().width() + imgT.info().width() + imgTR.info().width()) << " x " << (imgTL.info().height() + imgL.info().height() + imgBL.info().height()));
5400
        // Draw the frame
5401
        SkBitmap frame;
5402
        allocPixels(bm->info().width(), bm->info().height(), &frame);
5403
        frame.eraseColor(SK_ColorTRANSPARENT);
338 andreas 5404
        SkCanvas target(*bm, SkSurfaceProps());
307 andreas 5405
        SkCanvas canvas(frame, SkSurfaceProps());
5406
        SkPaint paint;
79 andreas 5407
 
307 andreas 5408
        paint.setBlendMode(SkBlendMode::kSrcOver);
365 andreas 5409
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(imgB);
307 andreas 5410
        canvas.drawImage(_image, imgBL.info().width(), ht - imgB.info().height(), SkSamplingOptions(), &paint);
365 andreas 5411
        _image = SkImages::RasterFromBitmap(imgBR);
307 andreas 5412
        canvas.drawImage(_image, wt - imgBR.info().width(), ht - imgBR.info().height(), SkSamplingOptions(), &paint);
365 andreas 5413
        _image = SkImages::RasterFromBitmap(imgR);
307 andreas 5414
        canvas.drawImage(_image, wt - imgR.info().width(), imgTR.info().height(), SkSamplingOptions(), &paint);
365 andreas 5415
        _image = SkImages::RasterFromBitmap(imgTR);
307 andreas 5416
        canvas.drawImage(_image, wt - imgTR.info().width(), 0, SkSamplingOptions(), &paint);
365 andreas 5417
        _image = SkImages::RasterFromBitmap(imgT);
307 andreas 5418
        canvas.drawImage(_image, imgTL.info().width(), 0, SkSamplingOptions(), &paint);
365 andreas 5419
        _image = SkImages::RasterFromBitmap(imgTL);
307 andreas 5420
        canvas.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);
365 andreas 5421
        _image = SkImages::RasterFromBitmap(imgL);
307 andreas 5422
        canvas.drawImage(_image, 0, imgTL.info().height(), SkSamplingOptions(), &paint);
365 andreas 5423
        _image = SkImages::RasterFromBitmap(imgBL);
307 andreas 5424
        canvas.drawImage(_image, 0, ht - imgBL.info().height(), SkSamplingOptions(), &paint);
79 andreas 5425
 
307 andreas 5426
        erasePart(bm, frame, Border::ERASE_OUTSIDE);
339 andreas 5427
        backgroundFrame(bm, frame, color);
365 andreas 5428
        _image = SkImages::RasterFromBitmap(frame);
339 andreas 5429
        paint.setBlendMode(SkBlendMode::kSrcATop);
338 andreas 5430
        target.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);
79 andreas 5431
    }
307 andreas 5432
    else    // We try to draw a frame by forcing it to draw even the not to draw marked frames.
317 andreas 5433
        drawBorder(bm, bname, wt, ht, sr[instance].cb, true);
79 andreas 5434
 
3 andreas 5435
    return true;
5436
}
5437
 
8 andreas 5438
int TButton::numberLines(const string& str)
5439
{
5440
    DECL_TRACER("TButton::numberLines(const string& str)");
5441
 
5442
    int lines = 1;
5443
 
96 andreas 5444
    if (str.empty())
8 andreas 5445
    {
96 andreas 5446
        MSG_DEBUG("Found an empty string.");
5447
        return lines;
5448
    }
5449
 
5450
    string::const_iterator iter;
5451
 
118 andreas 5452
    for (iter = str.begin(); iter != str.end(); ++iter)
96 andreas 5453
    {
5454
        if (*iter == '\n')
8 andreas 5455
            lines++;
5456
    }
5457
 
5458
    return lines;
5459
}
5460
 
5461
SkRect TButton::calcRect(int width, int height, int pen)
5462
{
5463
    DECL_TRACER("TButton::calcRect(int width, int height, int pen)");
5464
    SkRect rect;
5465
 
5466
    SkScalar left = (SkScalar)pen / 2.0;
5467
    SkScalar top = (SkScalar)pen / 2.0;
101 andreas 5468
    SkScalar w = (SkScalar)width - (SkScalar)pen;
5469
    SkScalar h = (SkScalar)height - (SkScalar)pen;
8 andreas 5470
    rect.setXYWH(left, top, w, h);
5471
    return rect;
5472
}
5473
 
15 andreas 5474
void TButton::runAnimation()
8 andreas 5475
{
15 andreas 5476
    DECL_TRACER("TButton::runAnimation()");
8 andreas 5477
 
43 andreas 5478
    if (mAniRunning)
5479
        return;
5480
 
15 andreas 5481
    mAniRunning = true;
5482
    int instance = 0;
5483
    int max = (int)sr.size();
38 andreas 5484
    ulong tm = nu * ru + nd * rd;
15 andreas 5485
 
93 andreas 5486
    while (mAniRunning && !mAniStop && !prg_stopped)
15 andreas 5487
    {
38 andreas 5488
        mActInstance = instance;
176 andreas 5489
        mChanged = true;
38 andreas 5490
 
101 andreas 5491
        if (visible && !drawButton(instance))
15 andreas 5492
            break;
5493
 
5494
        instance++;
5495
 
5496
        if (instance >= max)
5497
            instance = 0;
5498
 
5499
        std::this_thread::sleep_for(std::chrono::milliseconds(tm));
5500
    }
5501
 
5502
    mAniRunning = false;
5503
}
5504
 
38 andreas 5505
void TButton::runAnimationRange(int start, int end, ulong step)
5506
{
5507
    DECL_TRACER("TButton::runAnimationRange(int start, int end, ulong step)");
5508
 
93 andreas 5509
    if (mAniRunning)
5510
        return;
5511
 
38 andreas 5512
    mAniRunning = true;
320 andreas 5513
    int instance = start - 1;
38 andreas 5514
    int max = std::min(end, (int)sr.size());
5515
    std::chrono::steady_clock::time_point startt = std::chrono::steady_clock::now();
5516
 
93 andreas 5517
    while (mAniRunning && !mAniStop && !prg_stopped)
38 andreas 5518
    {
5519
        mActInstance = instance;
177 andreas 5520
        mChanged = true;
101 andreas 5521
 
5522
        if (visible)
5523
            drawButton(instance);   // We ignore the state and try to draw the next instance
5524
 
38 andreas 5525
        instance++;
5526
 
5527
        if (instance >= max)
320 andreas 5528
            instance = start - 1;
38 andreas 5529
 
5530
        std::this_thread::sleep_for(std::chrono::milliseconds(step));
5531
 
5532
        if (mAniRunTime > 0)
5533
        {
5534
            std::chrono::steady_clock::time_point current = std::chrono::steady_clock::now();
5535
            std::chrono::nanoseconds difftime = current - startt;
5536
            ulong duration = std::chrono::duration_cast<std::chrono::milliseconds>(difftime).count();
5537
 
5538
            if (duration >= mAniRunTime)
5539
                break;
5540
        }
5541
    }
5542
 
5543
    mAniRunTime = 0;
5544
    mAniRunning = false;
5545
}
5546
 
15 andreas 5547
bool TButton::drawButtonMultistateAni()
5548
{
101 andreas 5549
    DECL_TRACER("TButton::drawButtonMultistateAni()");
15 andreas 5550
 
33 andreas 5551
    if (prg_stopped)
5552
        return true;
35 andreas 5553
 
100 andreas 5554
    if (!visible || hd)    // Do nothing if this button is invisible
5555
        return true;
5556
 
15 andreas 5557
    if (mAniRunning || mThrAni.joinable())
5558
    {
23 andreas 5559
        MSG_TRACE("Animation is already running!");
15 andreas 5560
        return true;
5561
    }
5562
 
5563
    try
5564
    {
93 andreas 5565
        mAniStop = false;
15 andreas 5566
        mThrAni = thread([=] { runAnimation(); });
5567
        mThrAni.detach();
5568
    }
5569
    catch (exception& e)
5570
    {
5571
        MSG_ERROR("Error starting the button animation thread: " << e.what());
5572
        return false;
5573
    }
5574
 
5575
    return true;
5576
}
5577
 
300 andreas 5578
bool TButton::drawButton(int instance, bool show, bool subview)
15 andreas 5579
{
300 andreas 5580
    DECL_TRACER("TButton::drawButton(int instance, bool show, bool subview)");
15 andreas 5581
 
33 andreas 5582
    if (prg_stopped)
5583
        return false;
35 andreas 5584
 
300 andreas 5585
    if (subview)
5586
        mSubViewPart = subview;
5587
 
8 andreas 5588
    if ((size_t)instance >= sr.size() || instance < 0)
5589
    {
5590
        MSG_ERROR("Instance " << instance << " is out of bounds!");
5591
        TError::setError();
334 andreas 5592
#if TESTMODE == 1
5593
        setScreenDone();
5594
#endif
8 andreas 5595
        return false;
5596
    }
5597
 
26 andreas 5598
    if (!_displayButton && gPageManager)
5599
        _displayButton = gPageManager->getCallbackDB();
5600
 
100 andreas 5601
    if (!visible || hd || instance != mActInstance || !_displayButton)
14 andreas 5602
    {
15 andreas 5603
        bool db = (_displayButton != nullptr);
23 andreas 5604
        MSG_DEBUG("Button " << bi << ", \"" << na << "\" at instance " << instance << " is not to draw!");
150 andreas 5605
        MSG_DEBUG("Visible: " << (visible ? "YES" : "NO") << ", Hidden: " << (hd ? "YES" : "NO") << ", Instance/actual instance: " << instance << "/" << mActInstance << ", callback: " << (db ? "PRESENT" : "N/A"));
334 andreas 5606
#if TESTMODE == 1
5607
        setScreenDone();
5608
#endif
14 andreas 5609
        return true;
5610
    }
5611
 
137 andreas 5612
    MSG_DEBUG("Drawing button " << bi << ", \"" << na << "\" at instance " << instance);
176 andreas 5613
 
5614
    if (!mChanged && !mLastImage.empty())
5615
    {
289 andreas 5616
        if (show)
291 andreas 5617
        {
289 andreas 5618
            showLastButton();
5619
 
291 andreas 5620
            if (type == SUBPAGE_VIEW)
5621
            {
5622
                if (gPageManager)
5623
                    gPageManager->showSubViewList(st, this);
5624
            }
5625
        }
5626
 
176 andreas 5627
        return true;
5628
    }
5629
 
8 andreas 5630
    ulong parent = mHandle & 0xffff0000;
5631
    getDrawOrder(sr[instance]._do, (DRAW_ORDER *)&mDOrder);
5632
 
5633
    if (TError::isError())
334 andreas 5634
    {
5635
#if TESTMODE == 1
5636
        setScreenDone();
5637
#endif
8 andreas 5638
        return false;
334 andreas 5639
    }
8 andreas 5640
 
5641
    SkBitmap imgButton;
254 andreas 5642
 
5643
    if (!allocPixels(wt, ht, &imgButton))
334 andreas 5644
    {
5645
#if TESTMODE == 1
5646
        setScreenDone();
5647
#endif
254 andreas 5648
        return false;
334 andreas 5649
    }
254 andreas 5650
 
292 andreas 5651
    // We create an empty (transparent) image here. Later it depends on the
351 andreas 5652
    // draw order of the elements. If, for example, the background fill is
292 andreas 5653
    // not the first thing, we must be sure to not destroy already drawn
5654
    // elemts of the button.
5655
    imgButton.eraseColor(SkColors::kTransparent);
97 andreas 5656
    bool dynState = false;
8 andreas 5657
 
5658
    for (int i = 0; i < ORD_ELEM_COUNT; i++)
5659
    {
5660
        if (mDOrder[i] == ORD_ELEM_FILL)
5661
        {
5662
            if (!buttonFill(&imgButton, instance))
334 andreas 5663
            {
5664
#if TESTMODE == 1
5665
                setScreenDone();
5666
#endif
8 andreas 5667
                return false;
334 andreas 5668
            }
8 andreas 5669
        }
5670
        else if (mDOrder[i] == ORD_ELEM_BITMAP)
5671
        {
21 andreas 5672
            if (!sr[instance].dynamic && !buttonBitmap(&imgButton, instance))
334 andreas 5673
            {
5674
#if TESTMODE == 1
5675
                setScreenDone();
5676
#endif
8 andreas 5677
                return false;
334 andreas 5678
            }
97 andreas 5679
            else if (sr[instance].dynamic && !buttonDynamic(&imgButton, instance, show, &dynState))
334 andreas 5680
            {
5681
#if TESTMODE == 1
5682
                setScreenDone();
5683
#endif
21 andreas 5684
                return false;
334 andreas 5685
            }
8 andreas 5686
        }
5687
        else if (mDOrder[i] == ORD_ELEM_ICON)
5688
        {
5689
            if (!buttonIcon(&imgButton, instance))
334 andreas 5690
            {
5691
#if TESTMODE == 1
5692
                setScreenDone();
5693
#endif
8 andreas 5694
                return false;
334 andreas 5695
            }
8 andreas 5696
        }
5697
        else if (mDOrder[i] == ORD_ELEM_TEXT)
5698
        {
5699
            if (!buttonText(&imgButton, instance))
334 andreas 5700
            {
5701
#if TESTMODE == 1
5702
                setScreenDone();
5703
#endif
8 andreas 5704
                return false;
334 andreas 5705
            }
8 andreas 5706
        }
5707
        else if (mDOrder[i] == ORD_ELEM_BORDER)
5708
        {
5709
            if (!buttonBorder(&imgButton, instance))
334 andreas 5710
            {
5711
#if TESTMODE == 1
5712
                setScreenDone();
5713
#endif
8 andreas 5714
                return false;
334 andreas 5715
            }
8 andreas 5716
        }
5717
    }
5718
 
10 andreas 5719
    if (mGlobalOO >= 0 || sr[instance].oo >= 0) // Take overall opacity into consideration
5720
    {
5721
        SkBitmap ooButton;
5722
        int w = imgButton.width();
5723
        int h = imgButton.height();
254 andreas 5724
 
5725
        if (!allocPixels(w, h, &ooButton))
334 andreas 5726
        {
5727
#if TESTMODE == 1
5728
            setScreenDone();
5729
#endif
254 andreas 5730
            return false;
334 andreas 5731
        }
254 andreas 5732
 
10 andreas 5733
        SkCanvas canvas(ooButton);
5734
        SkIRect irect = SkIRect::MakeXYWH(0, 0, w, h);
5735
        SkRegion region;
5736
        region.setRect(irect);
5737
        SkScalar oo;
5738
 
5739
        if (mGlobalOO >= 0 && sr[instance].oo >= 0)
5740
        {
5741
            oo = std::min((SkScalar)mGlobalOO, (SkScalar)sr[instance].oo);
5742
            MSG_DEBUG("Set global overal opacity to " << oo);
5743
        }
5744
        else if (sr[instance].oo >= 0)
5745
        {
5746
            oo = (SkScalar)sr[instance].oo;
5747
            MSG_DEBUG("Set overal opacity to " << oo);
5748
        }
5749
        else
5750
        {
5751
            oo = (SkScalar)mGlobalOO;
5752
            MSG_DEBUG("Set global overal opacity to " << oo);
5753
        }
5754
 
5755
        SkScalar alpha = 1.0 / 255.0 * oo;
5756
        MSG_DEBUG("Calculated alpha value: " << alpha);
5757
        SkPaint paint;
5758
        paint.setAlphaf(alpha);
365 andreas 5759
//        paint.setImageFilter(SkImageFilters::AlphaThreshold(region, 0.0, alpha, nullptr));
5760
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(imgButton);
179 andreas 5761
        canvas.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);
10 andreas 5762
        imgButton.erase(SK_ColorTRANSPARENT, {0, 0, w, h});
5763
        imgButton = ooButton;
5764
    }
5765
 
15 andreas 5766
    mLastImage = imgButton;
177 andreas 5767
    mChanged = false;
8 andreas 5768
 
97 andreas 5769
    if (!prg_stopped && !dynState)
26 andreas 5770
    {
5771
        int rwidth = wt;
5772
        int rheight = ht;
5773
        int rleft = lt;
5774
        int rtop = tp;
43 andreas 5775
#ifdef _SCALE_SKIA_
26 andreas 5776
        if (gPageManager && gPageManager->getScaleFactor() != 1.0)
5777
        {
5778
            rwidth = (int)((double)wt * gPageManager->getScaleFactor());
5779
            rheight = (int)((double)ht * gPageManager->getScaleFactor());
5780
            rleft = (int)((double)lt * gPageManager->getScaleFactor());
5781
            rtop = (int)((double)tp * gPageManager->getScaleFactor());
5782
 
5783
            SkPaint paint;
5784
            paint.setBlendMode(SkBlendMode::kSrc);
5785
            paint.setFilterQuality(kHigh_SkFilterQuality);
28 andreas 5786
            // Calculate new dimension
26 andreas 5787
            SkImageInfo info = imgButton.info();
5788
            int width = (int)((double)info.width() * gPageManager->getScaleFactor());
5789
            int height = (int)((double)info.height() * gPageManager->getScaleFactor());
28 andreas 5790
            // Create a canvas and draw new image
5791
            sk_sp<SkImage> im = SkImage::MakeFromBitmap(imgButton);
5792
            imgButton.allocN32Pixels(width, height);
5793
            imgButton.eraseColor(SK_ColorTRANSPARENT);
254 andreas 5794
            SkCanvas can(imgButton, SkSurfaceProps());
26 andreas 5795
            SkRect rect = SkRect::MakeXYWH(0, 0, width, height);
179 andreas 5796
            can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
28 andreas 5797
            rowBytes = imgButton.info().minRowBytes();
5798
            mLastImage = imgButton;
26 andreas 5799
        }
43 andreas 5800
#endif
31 andreas 5801
        if (show)
176 andreas 5802
        {
283 andreas 5803
            MSG_DEBUG("Button type: " << buttonTypeToString());
5804
 
300 andreas 5805
            if (type != SUBPAGE_VIEW && !mSubViewPart)
293 andreas 5806
            {
5807
                TBitmap image((unsigned char *)imgButton.getPixels(), imgButton.info().width(), imgButton.info().height());
298 andreas 5808
                _displayButton(mHandle, parent, image, rwidth, rheight, rleft, rtop, isPassThrough());
293 andreas 5809
            }
300 andreas 5810
            else if (type != SUBPAGE_VIEW && mSubViewPart)
5811
            {
5812
                if (gPageManager)
5813
                    gPageManager->updateSubViewItem(this);
5814
            }
176 andreas 5815
        }
26 andreas 5816
    }
5817
 
293 andreas 5818
    if (!prg_stopped && type == SUBPAGE_VIEW && show)
279 andreas 5819
    {
5820
        if (gPageManager)
280 andreas 5821
            gPageManager->showSubViewList(st, this);
279 andreas 5822
    }
5823
 
8 andreas 5824
    return true;
5825
}
5826
 
50 andreas 5827
bool TButton::drawTextArea(int instance)
5828
{
192 andreas 5829
    DECL_TRACER("TButton::drawTextArea(int instance)");
50 andreas 5830
 
5831
    if (prg_stopped)
334 andreas 5832
    {
5833
#if TESTMODE == 1
5834
        setScreenDone();
5835
#endif
50 andreas 5836
        return false;
334 andreas 5837
    }
50 andreas 5838
 
100 andreas 5839
    if (!visible || hd)
334 andreas 5840
    {
5841
#if TESTMODE == 1
5842
        setScreenDone();
5843
#endif
100 andreas 5844
        return true;
334 andreas 5845
    }
100 andreas 5846
 
50 andreas 5847
    if ((size_t)instance >= sr.size() || instance < 0)
5848
    {
5849
        MSG_ERROR("Instance " << instance << " is out of bounds!");
5850
        TError::setError();
334 andreas 5851
#if TESTMODE == 1
5852
        setScreenDone();
5853
#endif
50 andreas 5854
        return false;
5855
    }
5856
 
176 andreas 5857
    if (!mChanged)
5858
    {
5859
        showLastButton();
5860
        return true;
5861
    }
5862
 
50 andreas 5863
    getDrawOrder(sr[instance]._do, (DRAW_ORDER *)&mDOrder);
5864
 
5865
    if (TError::isError())
334 andreas 5866
    {
5867
#if TESTMODE == 1
5868
        setScreenDone();
5869
#endif
50 andreas 5870
        return false;
334 andreas 5871
    }
50 andreas 5872
 
5873
    SkBitmap imgButton;
5874
 
254 andreas 5875
    if (!allocPixels(wt, ht, &imgButton))
334 andreas 5876
    {
5877
#if TESTMODE == 1
5878
        setScreenDone();
5879
#endif
254 andreas 5880
        return false;
334 andreas 5881
    }
254 andreas 5882
 
50 andreas 5883
    for (int i = 0; i < ORD_ELEM_COUNT; i++)
5884
    {
5885
        if (mDOrder[i] == ORD_ELEM_FILL)
5886
        {
5887
            if (!buttonFill(&imgButton, instance))
334 andreas 5888
            {
5889
#if TESTMODE == 1
5890
                setScreenDone();
5891
#endif
50 andreas 5892
                return false;
334 andreas 5893
            }
50 andreas 5894
        }
5895
        else if (mDOrder[i] == ORD_ELEM_BITMAP)
5896
        {
5897
            if (!sr[instance].dynamic && !buttonBitmap(&imgButton, instance))
334 andreas 5898
            {
5899
#if TESTMODE == 1
5900
                setScreenDone();
5901
#endif
50 andreas 5902
                return false;
334 andreas 5903
            }
97 andreas 5904
            else if (sr[instance].dynamic && !buttonDynamic(&imgButton, instance, false))
334 andreas 5905
            {
5906
#if TESTMODE == 1
5907
                setScreenDone();
5908
#endif
50 andreas 5909
                return false;
334 andreas 5910
            }
50 andreas 5911
        }
5912
        else if (mDOrder[i] == ORD_ELEM_ICON)
5913
        {
5914
            if (!buttonIcon(&imgButton, instance))
334 andreas 5915
            {
5916
#if TESTMODE == 1
5917
                setScreenDone();
5918
#endif
50 andreas 5919
                return false;
334 andreas 5920
            }
50 andreas 5921
        }
5922
        else if (mDOrder[i] == ORD_ELEM_BORDER)
5923
        {
5924
            if (!buttonBorder(&imgButton, instance))
334 andreas 5925
            {
5926
#if TESTMODE == 1
5927
                setScreenDone();
5928
#endif
50 andreas 5929
                return false;
334 andreas 5930
            }
50 andreas 5931
        }
5932
    }
5933
 
5934
    if (mGlobalOO >= 0 || sr[instance].oo >= 0) // Take overall opacity into consideration
5935
    {
5936
        SkBitmap ooButton;
5937
        int w = imgButton.width();
5938
        int h = imgButton.height();
254 andreas 5939
 
5940
        if (!allocPixels(w, h, &ooButton))
334 andreas 5941
        {
5942
#if TESTMODE == 1
5943
            setScreenDone();
5944
#endif
254 andreas 5945
            return false;
334 andreas 5946
        }
254 andreas 5947
 
50 andreas 5948
        SkCanvas canvas(ooButton);
5949
        SkIRect irect = SkIRect::MakeXYWH(0, 0, w, h);
5950
        SkRegion region;
5951
        region.setRect(irect);
5952
        SkScalar oo;
5953
 
5954
        if (mGlobalOO >= 0 && sr[instance].oo >= 0)
5955
        {
5956
            oo = std::min((SkScalar)mGlobalOO, (SkScalar)sr[instance].oo);
5957
            MSG_DEBUG("Set global overal opacity to " << oo);
5958
        }
5959
        else if (sr[instance].oo >= 0)
5960
        {
5961
            oo = (SkScalar)sr[instance].oo;
5962
            MSG_DEBUG("Set overal opacity to " << oo);
5963
        }
5964
        else
5965
        {
5966
            oo = (SkScalar)mGlobalOO;
5967
            MSG_DEBUG("Set global overal opacity to " << oo);
5968
        }
5969
 
5970
        SkScalar alpha = 1.0 / 255.0 * oo;
5971
        MSG_DEBUG("Calculated alpha value: " << alpha);
5972
        SkPaint paint;
5973
        paint.setAlphaf(alpha);
365 andreas 5974
//        paint.setImageFilter(SkImageFilters::AlphaThreshold(region, 0.0, alpha, nullptr));
5975
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(imgButton);
179 andreas 5976
        canvas.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);
50 andreas 5977
        imgButton.erase(SK_ColorTRANSPARENT, {0, 0, w, h});
5978
        imgButton = ooButton;
5979
    }
5980
 
5981
    mLastImage = imgButton;
177 andreas 5982
    mChanged = false;
50 andreas 5983
 
52 andreas 5984
    if (!prg_stopped)
50 andreas 5985
    {
5986
        int rwidth = wt;
5987
        int rheight = ht;
5988
        int rleft = lt;
5989
        int rtop = tp;
5990
        size_t rowBytes = imgButton.info().minRowBytes();
52 andreas 5991
#ifdef _SCALE_SKIA_
5992
        if (gPageManager && gPageManager->getScaleFactor() != 1.0)
5993
        {
5994
            size_t rowBytes = imgButton.info().minRowBytes();
5995
            rwidth = (int)((double)wt * gPageManager->getScaleFactor());
5996
            rheight = (int)((double)ht * gPageManager->getScaleFactor());
5997
            rleft = (int)((double)lt * gPageManager->getScaleFactor());
5998
            rtop = (int)((double)tp * gPageManager->getScaleFactor());
50 andreas 5999
 
52 andreas 6000
            SkPaint paint;
6001
            paint.setBlendMode(SkBlendMode::kSrc);
6002
            paint.setFilterQuality(kHigh_SkFilterQuality);
6003
            // Calculate new dimension
6004
            SkImageInfo info = imgButton.info();
6005
            int width = (int)((double)info.width() * gPageManager->getScaleFactor());
6006
            int height = (int)((double)info.height() * gPageManager->getScaleFactor());
6007
            // Create a canvas and draw new image
6008
            sk_sp<SkImage> im = SkImage::MakeFromBitmap(imgButton);
6009
            imgButton.allocN32Pixels(width, height);
6010
            imgButton.eraseColor(SK_ColorTRANSPARENT);
254 andreas 6011
            SkCanvas can(imgButton, SkSurfaceProps());
52 andreas 6012
            SkRect rect = SkRect::MakeXYWH(0, 0, width, height);
179 andreas 6013
            can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
52 andreas 6014
            rowBytes = imgButton.info().minRowBytes();
6015
            mLastImage = imgButton;
6016
        }
6017
#endif
6018
        if (gPageManager && gPageManager->getCallbackInputText())
6019
        {
6020
            BITMAP_t bm;
6021
            bm.buffer = (unsigned char *)imgButton.getPixels();
6022
            bm.rowBytes = rowBytes;
6023
            bm.left = rleft;
6024
            bm.top = rtop;
6025
            bm.width = rwidth;
6026
            bm.height = rheight;
291 andreas 6027
            gPageManager->getCallbackInputText()(this, bm, mBorderWidth);
52 andreas 6028
        }
50 andreas 6029
    }
6030
 
6031
    return true;
6032
}
6033
 
38 andreas 6034
bool TButton::drawMultistateBargraph(int level, bool show)
6035
{
6036
    DECL_TRACER("TButton::drawMultistateBargraph(int level, bool show)");
6037
 
6038
    if (prg_stopped)
6039
    {
334 andreas 6040
#if TESTMODE == 1
6041
        setScreenDone();
6042
#endif
38 andreas 6043
        return false;
6044
    }
6045
 
6046
    if (!_displayButton && gPageManager)
6047
        _displayButton = gPageManager->getCallbackDB();
6048
 
100 andreas 6049
    if (!visible || hd || !_displayButton)
38 andreas 6050
    {
6051
        bool db = (_displayButton != nullptr);
6052
        MSG_DEBUG("Multistate bargraph " << bi << ", \"" << na << " is not to draw!");
6053
        MSG_DEBUG("Visible: " << (visible ? "YES" : "NO") << ", callback: " << (db ? "PRESENT" : "N/A"));
334 andreas 6054
#if TESTMODE == 1
6055
        setScreenDone();
6056
#endif
38 andreas 6057
        return true;
6058
    }
6059
 
6060
    int maxLevel = level;
6061
 
268 andreas 6062
    if (maxLevel > rh)
6063
        maxLevel = rh;
6064
    else if (maxLevel < rl)
6065
        maxLevel = rl;
45 andreas 6066
    else if (maxLevel < 0)
268 andreas 6067
        maxLevel = rl;
38 andreas 6068
 
46 andreas 6069
    MSG_DEBUG("Display instance " << maxLevel);
38 andreas 6070
    ulong parent = mHandle & 0xffff0000;
6071
    getDrawOrder(sr[maxLevel]._do, (DRAW_ORDER *)&mDOrder);
6072
 
6073
    if (TError::isError())
6074
    {
334 andreas 6075
#if TESTMODE == 1
6076
        setScreenDone();
6077
#endif
38 andreas 6078
        return false;
6079
    }
6080
 
6081
    SkBitmap imgButton;
6082
 
254 andreas 6083
    if (!allocPixels(wt, ht, &imgButton))
334 andreas 6084
    {
6085
#if TESTMODE == 1
6086
        setScreenDone();
6087
#endif
254 andreas 6088
        return false;
334 andreas 6089
    }
254 andreas 6090
 
38 andreas 6091
    for (int i = 0; i < ORD_ELEM_COUNT; i++)
6092
    {
6093
        if (mDOrder[i] == ORD_ELEM_FILL)
6094
        {
6095
            if (!buttonFill(&imgButton, maxLevel))
6096
            {
334 andreas 6097
#if TESTMODE == 1
6098
                setScreenDone();
6099
#endif
38 andreas 6100
                return false;
6101
            }
6102
        }
6103
        else if (mDOrder[i] == ORD_ELEM_BITMAP)
6104
        {
6105
            if (!buttonBitmap(&imgButton, maxLevel))
6106
            {
334 andreas 6107
#if TESTMODE == 1
6108
                setScreenDone();
6109
#endif
38 andreas 6110
                return false;
6111
            }
6112
        }
6113
        else if (mDOrder[i] == ORD_ELEM_ICON)
6114
        {
6115
            if (!buttonIcon(&imgButton, maxLevel))
6116
            {
334 andreas 6117
#if TESTMODE == 1
6118
                setScreenDone();
6119
#endif
38 andreas 6120
                return false;
6121
            }
6122
        }
6123
        else if (mDOrder[i] == ORD_ELEM_TEXT)
6124
        {
6125
            if (!buttonText(&imgButton, maxLevel))
6126
            {
334 andreas 6127
#if TESTMODE == 1
6128
                setScreenDone();
6129
#endif
38 andreas 6130
                return false;
6131
            }
6132
        }
6133
        else if (mDOrder[i] == ORD_ELEM_BORDER)
6134
        {
6135
            if (!buttonBorder(&imgButton, maxLevel))
6136
            {
334 andreas 6137
#if TESTMODE == 1
6138
                setScreenDone();
6139
#endif
38 andreas 6140
                return false;
6141
            }
6142
        }
6143
    }
6144
 
6145
    if (mGlobalOO >= 0 || sr[maxLevel].oo >= 0) // Take overall opacity into consideration
6146
    {
6147
        SkBitmap ooButton;
6148
        int w = imgButton.width();
6149
        int h = imgButton.height();
254 andreas 6150
 
6151
        if (!allocPixels(w, h, &ooButton))
334 andreas 6152
        {
6153
#if TESTMODE == 1
6154
            setScreenDone();
6155
#endif
254 andreas 6156
            return false;
334 andreas 6157
        }
254 andreas 6158
 
38 andreas 6159
        SkCanvas canvas(ooButton);
6160
        SkIRect irect = SkIRect::MakeXYWH(0, 0, w, h);
6161
        SkRegion region;
6162
        region.setRect(irect);
6163
        SkScalar oo;
6164
 
6165
        if (mGlobalOO >= 0 && sr[maxLevel].oo >= 0)
6166
        {
6167
            oo = std::min((SkScalar)mGlobalOO, (SkScalar)sr[maxLevel].oo);
6168
            MSG_DEBUG("Set global overal opacity to " << oo);
6169
        }
6170
        else if (sr[maxLevel].oo >= 0)
6171
        {
6172
            oo = (SkScalar)sr[maxLevel].oo;
6173
            MSG_DEBUG("Set overal opacity to " << oo);
6174
        }
6175
        else
6176
        {
6177
            oo = (SkScalar)mGlobalOO;
6178
            MSG_DEBUG("Set global overal opacity to " << oo);
6179
        }
6180
 
6181
        SkScalar alpha = 1.0 / 255.0 * oo;
6182
        MSG_DEBUG("Calculated alpha value: " << alpha);
6183
        SkPaint paint;
6184
        paint.setAlphaf(alpha);
365 andreas 6185
//        paint.setImageFilter(SkImageFilters::AlphaThreshold(region, 0.0, alpha, nullptr));
6186
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(imgButton);
179 andreas 6187
        canvas.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);
38 andreas 6188
        imgButton.erase(SK_ColorTRANSPARENT, {0, 0, w, h});
6189
        imgButton = ooButton;
6190
    }
6191
 
6192
    mLastImage = imgButton;
177 andreas 6193
    mChanged = false;
38 andreas 6194
 
6195
    if (!prg_stopped)
6196
    {
6197
        int rwidth = wt;
6198
        int rheight = ht;
6199
        int rleft = lt;
6200
        int rtop = tp;
43 andreas 6201
#ifdef _SCALE_SKIA_
38 andreas 6202
        if (gPageManager && gPageManager->getScaleFactor() != 1.0)
6203
        {
6204
            rwidth = (int)((double)wt * gPageManager->getScaleFactor());
6205
            rheight = (int)((double)ht * gPageManager->getScaleFactor());
6206
            rleft = (int)((double)lt * gPageManager->getScaleFactor());
6207
            rtop = (int)((double)tp * gPageManager->getScaleFactor());
6208
 
6209
            SkPaint paint;
6210
            paint.setBlendMode(SkBlendMode::kSrc);
6211
            paint.setFilterQuality(kHigh_SkFilterQuality);
6212
            // Calculate new dimension
6213
            SkImageInfo info = imgButton.info();
6214
            int width = (int)((double)info.width() * gPageManager->getScaleFactor());
6215
            int height = (int)((double)info.height() * gPageManager->getScaleFactor());
6216
            MSG_DEBUG("Button dimension: " << width << " x " << height);
6217
            // Create a canvas and draw new image
6218
            sk_sp<SkImage> im = SkImage::MakeFromBitmap(imgButton);
6219
            imgButton.allocN32Pixels(width, height);
6220
            imgButton.eraseColor(SK_ColorTRANSPARENT);
254 andreas 6221
            SkCanvas can(imgButton, SkSurfaceProps());
38 andreas 6222
            SkRect rect = SkRect::MakeXYWH(0, 0, width, height);
179 andreas 6223
            can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
38 andreas 6224
            MSG_DEBUG("Old rowBytes: " << rowBytes);
6225
            rowBytes = imgButton.info().minRowBytes();
6226
            MSG_DEBUG("New rowBytes: " << rowBytes);
6227
            mLastImage = imgButton;
6228
        }
43 andreas 6229
#endif
38 andreas 6230
        if (show)
289 andreas 6231
        {
6232
            TBitmap image((unsigned char *)imgButton.getPixels(), imgButton.info().width(), imgButton.info().height());
298 andreas 6233
            _displayButton(mHandle, parent, image, rwidth, rheight, rleft, rtop, isPassThrough());
289 andreas 6234
        }
334 andreas 6235
#if TESTMODE == 1
6236
        else
6237
            setScreenDone();
6238
#endif
38 andreas 6239
    }
6240
 
6241
    return true;
6242
}
6243
 
200 andreas 6244
bool TButton::drawList(bool show)
6245
{
6246
    DECL_TRACER("TButton::drawList(bool show)");
6247
 
6248
    if (!mChanged)
6249
    {
6250
        showLastButton();
6251
        return true;
6252
    }
6253
 
6254
    getDrawOrder(sr[0]._do, (DRAW_ORDER *)&mDOrder);
6255
 
6256
    if (TError::isError())
6257
        return false;
6258
 
6259
    SkBitmap imgButton;
6260
 
254 andreas 6261
    if (!allocPixels(wt, ht, &imgButton))
6262
        return false;
6263
 
200 andreas 6264
    for (int i = 0; i < ORD_ELEM_COUNT; i++)
6265
    {
6266
        if (mDOrder[i] == ORD_ELEM_FILL)
6267
        {
6268
            if (!buttonFill(&imgButton, 0))
6269
                return false;
6270
        }
6271
        else if (mDOrder[i] == ORD_ELEM_BITMAP)
6272
        {
6273
            if (!sr[0].dynamic && !buttonBitmap(&imgButton, 0))
6274
                return false;
6275
            else if (sr[0].dynamic && !buttonDynamic(&imgButton, 0, false))
6276
                return false;
6277
        }
6278
        else if (mDOrder[i] == ORD_ELEM_ICON)
6279
        {
6280
            if (!buttonIcon(&imgButton, 0))
6281
                return false;
6282
        }
6283
        else if (mDOrder[i] == ORD_ELEM_BORDER)
6284
        {
6285
            if (!buttonBorder(&imgButton, 0))
6286
                return false;
6287
        }
6288
    }
6289
 
6290
    if (mGlobalOO >= 0 || sr[0].oo >= 0) // Take overall opacity into consideration
6291
    {
6292
        SkBitmap ooButton;
6293
        int w = imgButton.width();
6294
        int h = imgButton.height();
254 andreas 6295
 
6296
        if (!allocPixels(w, h, &ooButton))
6297
            return false;
6298
 
200 andreas 6299
        SkCanvas canvas(ooButton);
6300
        SkIRect irect = SkIRect::MakeXYWH(0, 0, w, h);
6301
        SkRegion region;
6302
        region.setRect(irect);
6303
        SkScalar oo;
6304
 
6305
        if (mGlobalOO >= 0 && sr[0].oo >= 0)
6306
        {
6307
            oo = std::min((SkScalar)mGlobalOO, (SkScalar)sr[0].oo);
6308
            MSG_DEBUG("Set global overal opacity to " << oo);
6309
        }
6310
        else if (sr[0].oo >= 0)
6311
        {
6312
            oo = (SkScalar)sr[0].oo;
6313
            MSG_DEBUG("Set overal opacity to " << oo);
6314
        }
6315
        else
6316
        {
6317
            oo = (SkScalar)mGlobalOO;
6318
            MSG_DEBUG("Set global overal opacity to " << oo);
6319
        }
6320
 
6321
        SkScalar alpha = 1.0 / 255.0 * oo;
6322
        MSG_DEBUG("Calculated alpha value: " << alpha);
6323
        SkPaint paint;
6324
        paint.setAlphaf(alpha);
365 andreas 6325
//        paint.setImageFilter(SkImageFilters::AlphaThreshold(region, 0.0, alpha, nullptr));
6326
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(imgButton);
200 andreas 6327
        canvas.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);
6328
        imgButton.erase(SK_ColorTRANSPARENT, {0, 0, w, h});
6329
        imgButton = ooButton;
6330
    }
6331
 
6332
    mLastImage = imgButton;
6333
    mChanged = false;
6334
 
6335
    if (!prg_stopped)
6336
    {
6337
        int rwidth = wt;
6338
        int rheight = ht;
6339
        int rleft = lt;
6340
        int rtop = tp;
6341
        size_t rowBytes = imgButton.info().minRowBytes();
6342
#ifdef _SCALE_SKIA_
6343
        if (gPageManager && gPageManager->getScaleFactor() != 1.0)
6344
        {
6345
            size_t rowBytes = imgButton.info().minRowBytes();
6346
            rwidth = (int)((double)wt * gPageManager->getScaleFactor());
6347
            rheight = (int)((double)ht * gPageManager->getScaleFactor());
6348
            rleft = (int)((double)lt * gPageManager->getScaleFactor());
6349
            rtop = (int)((double)tp * gPageManager->getScaleFactor());
6350
 
6351
            SkPaint paint;
6352
            paint.setBlendMode(SkBlendMode::kSrc);
6353
            paint.setFilterQuality(kHigh_SkFilterQuality);
6354
            // Calculate new dimension
6355
            SkImageInfo info = imgButton.info();
6356
            int width = (int)((double)info.width() * gPageManager->getScaleFactor());
6357
            int height = (int)((double)info.height() * gPageManager->getScaleFactor());
6358
            // Create a canvas and draw new image
6359
            sk_sp<SkImage> im = SkImage::MakeFromBitmap(imgButton);
6360
            imgButton.allocN32Pixels(width, height);
6361
            imgButton.eraseColor(SK_ColorTRANSPARENT);
254 andreas 6362
            SkCanvas can(imgButton, SkSurfaceProps());
200 andreas 6363
            SkRect rect = SkRect::MakeXYWH(0, 0, width, height);
6364
            can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
6365
            rowBytes = imgButton.info().minRowBytes();
6366
            mLastImage = imgButton;
6367
        }
6368
#endif
6369
        if (show && gPageManager && gPageManager->getCallbackListBox())
6370
        {
6371
            BITMAP_t bm;
6372
            bm.buffer = (unsigned char *)imgButton.getPixels();
6373
            bm.rowBytes = rowBytes;
6374
            bm.left = rleft;
6375
            bm.top = rtop;
6376
            bm.width = rwidth;
6377
            bm.height = rheight;
291 andreas 6378
            gPageManager->getCallbackListBox()(this, bm, mBorderWidth);
200 andreas 6379
        }
6380
    }
6381
 
6382
    return true;
6383
}
6384
 
15 andreas 6385
bool TButton::drawBargraph(int instance, int level, bool show)
6386
{
177 andreas 6387
    DECL_TRACER("TButton::drawBargraph(int instance, int level, bool show)");
15 andreas 6388
 
6389
    if ((size_t)instance >= sr.size() || instance < 0)
6390
    {
6391
        MSG_ERROR("Instance " << instance << " is out of bounds!");
6392
        TError::setError();
6393
        return false;
6394
    }
6395
 
38 andreas 6396
    if (!_displayButton && gPageManager)
6397
        _displayButton = gPageManager->getCallbackDB();
6398
 
176 andreas 6399
    if (!mChanged && mLastLevel == level)
6400
    {
6401
        showLastButton();
6402
        return true;
6403
    }
6404
 
15 andreas 6405
    if (level < rl)
6406
        mLastLevel = rl;
6407
    else if (level > rh)
6408
        mLastLevel = rh;
6409
    else
6410
        mLastLevel = level;
6411
 
38 andreas 6412
    int inst = instance;
6413
 
100 andreas 6414
    if (!visible || hd || instance != mActInstance || !_displayButton)
15 andreas 6415
    {
6416
        bool db = (_displayButton != nullptr);
23 andreas 6417
        MSG_DEBUG("Bargraph " << bi << ", \"" << na << "\" at instance " << instance << " with level " << mLastLevel << " is not to draw!");
15 andreas 6418
        MSG_DEBUG("Visible: " << (visible ? "YES" : "NO") << ", Instance/actual instance: " << instance << "/" << mActInstance << ", callback: " << (db ? "PRESENT" : "N/A"));
6419
        return true;
6420
    }
6421
 
6422
    ulong parent = mHandle & 0xffff0000;
35 andreas 6423
 
20 andreas 6424
    if (type == BARGRAPH)
38 andreas 6425
    {
20 andreas 6426
        getDrawOrder(sr[1]._do, (DRAW_ORDER *)&mDOrder);
38 andreas 6427
        inst = 1;
6428
    }
20 andreas 6429
    else
6430
        getDrawOrder(sr[instance]._do, (DRAW_ORDER *)&mDOrder);
35 andreas 6431
 
15 andreas 6432
    if (TError::isError())
6433
        return false;
6434
 
6435
    SkBitmap imgButton;
254 andreas 6436
 
6437
    if (!allocPixels(wt, ht, &imgButton))
6438
        return false;
6439
 
38 andreas 6440
    imgButton.eraseColor(TColor::getSkiaColor(sr[0].cf));
6441
    bool haveFrame = false;
15 andreas 6442
 
6443
    for (int i = 0; i < ORD_ELEM_COUNT; i++)
6444
    {
38 andreas 6445
        if (mDOrder[i] == ORD_ELEM_FILL && !haveFrame)
15 andreas 6446
        {
38 andreas 6447
            if (!buttonFill(&imgButton, (type == BARGRAPH ? 0 : inst)))
15 andreas 6448
                return false;
6449
        }
6450
        else if (mDOrder[i] == ORD_ELEM_BITMAP)
6451
        {
38 andreas 6452
            if (!barLevel(&imgButton, inst, mLastLevel))
15 andreas 6453
                return false;
6454
        }
6455
        else if (mDOrder[i] == ORD_ELEM_ICON)
6456
        {
38 andreas 6457
            if (!buttonIcon(&imgButton, inst))
15 andreas 6458
                return false;
6459
        }
6460
        else if (mDOrder[i] == ORD_ELEM_TEXT)
6461
        {
38 andreas 6462
            if (!buttonText(&imgButton, inst))
15 andreas 6463
                return false;
6464
        }
6465
        else if (mDOrder[i] == ORD_ELEM_BORDER)
6466
        {
38 andreas 6467
            if (!buttonBorder(&imgButton, (type == BARGRAPH ? 0 : inst)))
15 andreas 6468
                return false;
38 andreas 6469
 
6470
            haveFrame = true;
15 andreas 6471
        }
6472
    }
6473
 
38 andreas 6474
    if (mGlobalOO >= 0 || sr[inst].oo >= 0) // Take overall opacity into consideration
15 andreas 6475
    {
6476
        SkBitmap ooButton;
6477
        int w = imgButton.width();
6478
        int h = imgButton.height();
254 andreas 6479
 
6480
        if (!allocPixels(w, h, &ooButton))
6481
            return false;
6482
 
15 andreas 6483
        SkCanvas canvas(ooButton);
6484
        SkIRect irect = SkIRect::MakeXYWH(0, 0, w, h);
6485
        SkRegion region;
6486
        region.setRect(irect);
6487
        SkScalar oo;
6488
 
38 andreas 6489
        if (mGlobalOO >= 0 && sr[inst].oo >= 0)
15 andreas 6490
        {
38 andreas 6491
            oo = std::min((SkScalar)mGlobalOO, (SkScalar)sr[inst].oo);
15 andreas 6492
            MSG_DEBUG("Set global overal opacity to " << oo);
6493
        }
38 andreas 6494
        else if (sr[inst].oo >= 0)
15 andreas 6495
        {
38 andreas 6496
            oo = (SkScalar)sr[inst].oo;
15 andreas 6497
            MSG_DEBUG("Set overal opacity to " << oo);
6498
        }
6499
        else
6500
        {
6501
            oo = (SkScalar)mGlobalOO;
6502
            MSG_DEBUG("Set global overal opacity to " << oo);
6503
        }
6504
 
6505
        SkScalar alpha = 1.0 / 255.0 * oo;
6506
        MSG_DEBUG("Calculated alpha value: " << alpha);
6507
        SkPaint paint;
6508
        paint.setAlphaf(alpha);
365 andreas 6509
//        paint.setImageFilter(SkImageFilters::AlphaThreshold(region, 0.0, alpha, nullptr));
6510
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(imgButton);
179 andreas 6511
        canvas.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);
15 andreas 6512
        imgButton.erase(SK_ColorTRANSPARENT, {0, 0, w, h});
6513
        imgButton = ooButton;
6514
    }
6515
 
6516
    mLastImage = imgButton;
177 andreas 6517
    mChanged = false;
15 andreas 6518
 
21 andreas 6519
    if (!prg_stopped && show && visible && instance == mActInstance && _displayButton)
26 andreas 6520
    {
6521
        int rwidth = wt;
6522
        int rheight = ht;
6523
        int rleft = lt;
6524
        int rtop = tp;
43 andreas 6525
#ifdef _SCALE_SKIA_
26 andreas 6526
        if (gPageManager && gPageManager->getScaleFactor() != 1.0)
6527
        {
6528
            rwidth = (int)((double)wt * gPageManager->getScaleFactor());
6529
            rheight = (int)((double)ht * gPageManager->getScaleFactor());
6530
            rleft = (int)((double)lt * gPageManager->getScaleFactor());
6531
            rtop = (int)((double)tp * gPageManager->getScaleFactor());
6532
 
6533
            SkPaint paint;
6534
            paint.setBlendMode(SkBlendMode::kSrc);
6535
            paint.setFilterQuality(kHigh_SkFilterQuality);
28 andreas 6536
            // Calculate new dimension
26 andreas 6537
            SkImageInfo info = imgButton.info();
6538
            int width = (int)((double)info.width() * gPageManager->getScaleFactor());
6539
            int height = (int)((double)info.height() * gPageManager->getScaleFactor());
28 andreas 6540
            // Create a canvas and draw new image
6541
            sk_sp<SkImage> im = SkImage::MakeFromBitmap(imgButton);
6542
            imgButton.allocN32Pixels(width, height);
6543
            imgButton.eraseColor(SK_ColorTRANSPARENT);
254 andreas 6544
            SkCanvas can(imgButton, SkSurfaceProps());
26 andreas 6545
            SkRect rect = SkRect::MakeXYWH(0, 0, width, height);
179 andreas 6546
            can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
28 andreas 6547
            mLastImage = imgButton;
26 andreas 6548
        }
43 andreas 6549
#endif
289 andreas 6550
        TBitmap image((unsigned char *)imgButton.getPixels(), imgButton.info().width(), imgButton.info().height());
298 andreas 6551
        _displayButton(mHandle, parent, image, rwidth, rheight, rleft, rtop, isPassThrough());
26 andreas 6552
    }
6553
 
15 andreas 6554
    return true;
6555
}
6556
 
40 andreas 6557
POSITION_t TButton::calcImagePosition(int width, int height, CENTER_CODE cc, int number, int line)
4 andreas 6558
{
6559
    DECL_TRACER("TButton::calcImagePosition(int with, int height, CENTER_CODE code, int number)");
6560
 
6561
    SR_T act_sr;
6562
    POSITION_t position;
69 andreas 6563
    int ix, iy, ln;
4 andreas 6564
 
6565
    if (sr.size() == 0)
6566
        return position;
6567
 
6568
    if (number <= 0)
6569
        act_sr = sr.at(0);
6570
    else if ((size_t)number < sr.size())
6571
        act_sr = sr.at(number);
46 andreas 6572
    else if ((size_t)number >= sr.size())
6573
        act_sr = sr.at(sr.size() - 1);
4 andreas 6574
    else
6575
        return position;
6576
 
69 andreas 6577
    if (line <= 0)
6578
        ln = 1;
6579
    else
6580
        ln = line;
6581
 
4 andreas 6582
    int border_size = getBorderSize(act_sr.bs);
8 andreas 6583
    int code, border = border_size;
10 andreas 6584
    string dbgCC;
17 andreas 6585
    int rwt = 0, rht = 0;
4 andreas 6586
 
6587
    switch (cc)
3 andreas 6588
    {
8 andreas 6589
        case SC_ICON:
6590
            code = act_sr.ji;
6591
            ix = act_sr.ix;
6592
            iy = act_sr.iy;
6593
            border = border_size = 0;
10 andreas 6594
            dbgCC = "ICON";
17 andreas 6595
            rwt = width;
6596
            rht = height;
8 andreas 6597
        break;
6598
 
6599
        case SC_BITMAP:
6600
            code = act_sr.jb;
10 andreas 6601
            ix = act_sr.bx;
6602
            iy = act_sr.by;
6603
            dbgCC = "BITMAP";
17 andreas 6604
            rwt = std::min(wt - border * 2, width);
6605
            rht = std::min(ht - border_size * 2, height);
8 andreas 6606
        break;
6607
 
6608
        case SC_TEXT:
6609
            code = act_sr.jt;
6610
            ix = act_sr.tx;
6611
            iy = act_sr.ty;
10 andreas 6612
            dbgCC = "TEXT";
164 andreas 6613
 
6614
            if (border < 4)
6615
                border = 4;
6616
//            border += 4;
69 andreas 6617
            rwt = std::min(wt - border * 2, width);         // We've always a minimum (invisible) border of 4 pixels.
6618
            rht = std::min(ht - border_size * 2, height);   // The height is calculated from a defined border, if any.
8 andreas 6619
        break;
3 andreas 6620
    }
4 andreas 6621
 
17 andreas 6622
    if (width > rwt || height > rht)
6623
        position.overflow = true;
46 andreas 6624
 
4 andreas 6625
    switch (code)
6626
    {
6627
        case 0: // absolute position
8 andreas 6628
            position.left = ix;
69 andreas 6629
            position.top = iy;
40 andreas 6630
 
46 andreas 6631
            if (cc == SC_BITMAP && ix < 0 && rwt < width)
6632
                position.left *= -1;
6633
 
6634
            if (cc == SC_BITMAP && iy < 0 && rht < height)
6635
                position.top += -1;
6636
 
10 andreas 6637
            position.width = rwt;
6638
            position.height = rht;
4 andreas 6639
        break;
6640
 
6641
        case 1: // top, left
10 andreas 6642
            if (cc == SC_TEXT)
40 andreas 6643
            {
10 andreas 6644
                position.left = border;
96 andreas 6645
                position.top = ht - ((ht - rht) / 2) - height * ln;
40 andreas 6646
            }
10 andreas 6647
 
6648
            position.width = rwt;
6649
            position.height = rht;
4 andreas 6650
        break;
6651
 
6652
        case 2: // center, top
40 andreas 6653
            if (cc == SC_TEXT)
96 andreas 6654
                position.top = ht - ((ht - rht) / 2) - height * ln;
40 andreas 6655
 
10 andreas 6656
            position.left = (wt - rwt) / 2;
6657
            position.height = rht;
6658
            position.width = rwt;
4 andreas 6659
        break;
6660
 
6661
        case 3: // right, top
10 andreas 6662
            position.left = wt - rwt;
19 andreas 6663
 
6664
            if (cc == SC_TEXT)
40 andreas 6665
            {
19 andreas 6666
                position.left = (((position.left - border) < 0) ? 0 : position.left - border);
96 andreas 6667
                position.top = ht - (ht - rht) - height * ln;
40 andreas 6668
            }
19 andreas 6669
 
10 andreas 6670
            position.width = rwt;
6671
            position.height = rht;
4 andreas 6672
        break;
6673
 
6674
        case 4: // left, middle
10 andreas 6675
            if (cc == SC_TEXT)
40 andreas 6676
            {
10 andreas 6677
                position.left = border;
69 andreas 6678
                position.top = (ht - height) / 2;
40 andreas 6679
            }
6680
            else
6681
                position.top = (ht - rht) / 2;
10 andreas 6682
 
6683
            position.width = rwt;
6684
            position.height = rht;
4 andreas 6685
        break;
6686
 
6687
        case 6: // right, middle
10 andreas 6688
            position.left = wt - rwt;
19 andreas 6689
 
6690
            if (cc == SC_TEXT)
40 andreas 6691
            {
19 andreas 6692
                position.left = (((position.left - border) < 0) ? 0 : position.left - border);
69 andreas 6693
                position.top = (ht - height) / 2;
40 andreas 6694
            }
6695
            else
6696
                position.top = (ht - rht) / 2;
19 andreas 6697
 
10 andreas 6698
            position.width = rwt;
6699
            position.height = rht;
4 andreas 6700
        break;
6701
 
6702
        case 7: // left, bottom
10 andreas 6703
            if (cc == SC_TEXT)
40 andreas 6704
            {
10 andreas 6705
                position.left = border_size;
69 andreas 6706
                position.top = (ht - rht) - height * ln;
40 andreas 6707
            }
6708
            else
6709
                position.top = ht - rht;
10 andreas 6710
 
6711
            position.width = rwt;
6712
            position.height = rht;
4 andreas 6713
        break;
6714
 
6715
        case 8: // center, bottom
10 andreas 6716
            position.left = (wt - rwt) / 2;
40 andreas 6717
 
6718
            if (cc == SC_TEXT)
69 andreas 6719
                position.top = (ht - rht) - height * ln;
40 andreas 6720
            else
6721
                position.top = ht - rht;
6722
 
10 andreas 6723
            position.width = rwt;
6724
            position.height = rht;
4 andreas 6725
        break;
6726
 
6727
        case 9: // right, bottom
10 andreas 6728
            position.left = wt - rwt;
19 andreas 6729
 
6730
            if (cc == SC_TEXT)
40 andreas 6731
            {
19 andreas 6732
                position.left = (((position.left - border) < 0) ? 0 : position.left - border);
69 andreas 6733
                position.top = (ht - rht) - height * ln;
40 andreas 6734
            }
6735
            else
6736
                position.top = ht - rht;
4 andreas 6737
        break;
6738
 
6739
        default: // center, middle
10 andreas 6740
            position.left = (wt - rwt) / 2;
40 andreas 6741
 
6742
            if (cc == SC_TEXT)
69 andreas 6743
                position.top = (ht - height) / 2;
40 andreas 6744
            else
6745
                position.top = (ht - rht) / 2;
6746
 
10 andreas 6747
            position.width = rwt;
6748
            position.height = rht;
4 andreas 6749
    }
6750
 
69 andreas 6751
    if (TStreamError::checkFilter(HLOG_DEBUG))
6752
    {
70 andreas 6753
        string format = getFormatString((TEXT_ORIENTATION)code);
6754
        MSG_DEBUG("Type: " << dbgCC << ", format: " << format <<
69 andreas 6755
            ", PosType=" << code << ", total height=" << ht << ", height object=" << height <<
6756
            ", Position: x=" << position.left << ", y=" << position.top << ", w=" << position.width <<
6757
            ", h=" << position.height << ", Overflow: " << (position.overflow ? "YES" : "NO"));
6758
    }
6759
 
4 andreas 6760
    position.valid = true;
6761
    return position;
6762
}
6763
 
99 andreas 6764
IMAGE_SIZE_t TButton::calcImageSize(int imWidth, int imHeight, int instance, bool aspect)
6765
{
6766
    DECL_TRACER("TButton::calcImageSize(int imWidth, int imHeight, bool aspect)");
6767
 
6768
    int border = getBorderSize(sr[instance].bs);
6769
    IMAGE_SIZE_t isize;
6770
 
6771
    if (!aspect)
6772
    {
6773
        isize.width = wt - border * 2;
6774
        isize.height = ht - border * 2;
6775
    }
6776
    else
6777
    {
6778
        int w = wt - border * 2;
6779
        int h = ht - border * 2;
6780
        double scale;
6781
 
100 andreas 6782
        if (w < h || imWidth > imHeight)
99 andreas 6783
            scale = (double)w / (double)imWidth;
6784
        else
6785
            scale = (double)h / (double)imHeight;
6786
 
6787
        isize.width = (int)((double)imWidth * scale);
6788
        isize.height = (int)((double)imHeight * scale);
6789
    }
6790
 
6791
    MSG_DEBUG("Sizing image: Original: " << imWidth << " x " << imHeight << " to " << isize.width << " x " << isize.height);
6792
    return isize;
6793
}
6794
 
69 andreas 6795
string TButton::getFormatString(TEXT_ORIENTATION to)
6796
{
6797
    DECL_TRACER("TButton::getFormatString(CENTER_CODE cc)");
6798
 
6799
    switch(to)
6800
    {
6801
        case ORI_ABSOLUT:       return "ABSOLUT";
6802
        case ORI_BOTTOM_LEFT:   return "BOTTOM/LEFT";
6803
        case ORI_BOTTOM_MIDDLE: return "BOTTOM/MIDDLE";
6804
        case ORI_BOTTOM_RIGHT:  return "BOTTOM/RIGHT";
6805
        case ORI_CENTER_LEFT:   return "CENTER/LEFT";
6806
        case ORI_CENTER_MIDDLE: return "CENTER/MIDDLE";
6807
        case ORI_CENTER_RIGHT:  return "CENTER/RIGHT";
6808
        case ORI_TOP_LEFT:      return "TOP/LEFT";
6809
        case ORI_TOP_MIDDLE:    return "TOP/MIDDLE";
6810
        case ORI_TOP_RIGHT:     return "TOP/RIGHT";
6811
    }
6812
 
6813
    return "UNKNOWN";   // Should not happen!
6814
}
6815
 
4 andreas 6816
int TButton::getBorderSize(const std::string& name)
6817
{
6818
    DECL_TRACER("TButton::getBorderSize(const std::string& name)");
6819
 
306 andreas 6820
    int width = getBorderWidth(name);
6821
 
6822
    if (width > 0)
6823
        return width;
6824
 
81 andreas 6825
    if (gPageManager && gPageManager->getSystemDraw())
6826
    {
6827
        if (gPageManager->getSystemDraw()->existBorder(name))
6828
            return gPageManager->getSystemDraw()->getBorderWidth(name);
6829
    }
6830
 
4 andreas 6831
    return 0;
6832
}
6833
 
6834
void TButton::calcImageSizePercent(int imWidth, int imHeight, int btWidth, int btHeight, int btFrame, int *realX, int *realY)
6835
{
6836
    DECL_TRACER("TButton::clacImageSizePercent(int imWidth, int imHeight, int btWidth, int btHeight, int btFrame, int *realX, int *realY)");
6837
 
6838
    int spX = btWidth - (btFrame * 2);
6839
    int spY = btHeight - (btFrame * 2);
6840
 
6841
    if (imWidth <= spX && imHeight <= spY)
6842
    {
6843
        *realX = imWidth;
6844
        *realY = imHeight;
6845
        return;
6846
    }
6847
 
6848
    int oversizeX = 0, oversizeY = 0;
6849
 
6850
    if (imWidth > spX)
6851
        oversizeX = imWidth - spX;
6852
 
6853
    if (imHeight > spY)
6854
        oversizeY = imHeight - spY;
6855
 
6856
    double percent = 0.0;
6857
 
6858
    if (oversizeX > oversizeY)
6859
        percent = 100.0 / (double)imWidth * (double)spX;
3 andreas 6860
    else
4 andreas 6861
        percent = 100.0 / (double)imHeight * (double)spY;
6862
 
6863
    *realX = (int)(percent / 100.0 * (double)imWidth);
6864
    *realY = (int)(percent / 100.0 * (double)imHeight);
6865
}
6866
 
10 andreas 6867
SkBitmap TButton::drawImageButton(SkBitmap& imgRed, SkBitmap& imgMask, int width, int height, SkColor col1, SkColor col2)
4 andreas 6868
{
6 andreas 6869
    DECL_TRACER("TButton::drawImageButton(SkImage& imgRed, SkImage& imgMask, int width, int height, SkColor col1, SkColor col2)");
4 andreas 6870
 
35 andreas 6871
    if (width <= 0 || height <= 0)
6872
    {
165 andreas 6873
        MSG_WARNING("Got invalid width of height! (width: " << width << ", height: " << height << ")");
35 andreas 6874
        return SkBitmap();
6875
    }
6876
 
162 andreas 6877
    if (imgRed.empty())
6878
    {
6879
        MSG_WARNING("Missing mask to draw image!");
6880
        return SkBitmap();
6881
    }
6882
 
6 andreas 6883
    SkPixmap pixmapRed = imgRed.pixmap();
7 andreas 6884
    SkPixmap pixmapMask;
163 andreas 6885
    bool haveBothImages = true;
4 andreas 6886
 
7 andreas 6887
    if (!imgMask.empty())
6888
        pixmapMask = imgMask.pixmap();
163 andreas 6889
    else
6890
        haveBothImages = false;
162 andreas 6891
 
7 andreas 6892
    SkBitmap maskBm;
254 andreas 6893
 
6894
    if (!allocPixels(width, height, &maskBm))
6895
        return SkBitmap();
6896
 
69 andreas 6897
    maskBm.eraseColor(SK_ColorTRANSPARENT);
3 andreas 6898
 
4 andreas 6899
    for (int ix = 0; ix < width; ix++)
6900
    {
6901
        for (int iy = 0; iy < height; iy++)
3 andreas 6902
        {
184 andreas 6903
            SkColor pixelRed;
7 andreas 6904
            SkColor pixelMask;
3 andreas 6905
 
184 andreas 6906
            if (ix < pixmapRed.info().width() && iy < pixmapRed.info().height())
240 andreas 6907
                pixelRed = pixmapRed.getColor(ix, iy);
184 andreas 6908
            else
6909
                pixelRed = 0;
6910
 
6911
            if (haveBothImages && !imgMask.empty() &&
6912
                    ix < pixmapMask.info().width() && iy < pixmapMask.info().height())
7 andreas 6913
                pixelMask = pixmapMask.getColor(ix, iy);
6914
            else
164 andreas 6915
                pixelMask = SkColorSetA(SK_ColorWHITE, 0);
3 andreas 6916
 
10 andreas 6917
            SkColor pixel = baseColor(pixelRed, pixelMask, col1, col2);
20 andreas 6918
            uint32_t alpha = SkColorGetA(pixel);
184 andreas 6919
            uint32_t *wpix = nullptr;
35 andreas 6920
 
184 andreas 6921
            if (ix < maskBm.info().width() && iy < maskBm.info().height())
6922
                wpix = maskBm.getAddr32(ix, iy);
6923
 
35 andreas 6924
            if (!wpix)
184 andreas 6925
                continue;
35 andreas 6926
 
20 andreas 6927
            if (alpha == 0)
6928
                pixel = pixelMask;
184 andreas 6929
 
262 andreas 6930
            *wpix = pixel;
3 andreas 6931
        }
6932
    }
6933
 
7 andreas 6934
    return maskBm;
3 andreas 6935
}
6 andreas 6936
 
99 andreas 6937
/**
6938
 * @brief Takes 2 images and combines them to one.
6939
 *
6940
 * The 2 images are a solid base image defining the basic form and an identical
6941
 * image defining the alpha channel.
6942
 *
6943
 * @param base  The base image containing the form as black pixels.
6944
 * @param alpha The image containing just an alpha channel.
6945
 * @param col   The color which should be used instead of a black pixel.
6946
 *
6947
 * @return On success a valid bitmap is returned containing the form.
6948
 * On error an empty bitmap is returned.
6949
 */
6950
SkBitmap TButton::combineImages(SkBitmap& base, SkBitmap& alpha, SkColor col)
6951
{
6952
    DECL_TRACER("TButton::combineImages(SkBitmap& base, SkBitmap& alpha, SkColor col)");
6953
 
6954
    int width = base.info().width();
6955
    int height = base.info().height();
6956
    SkBitmap Bm;    // The new bitmap. It will be returned in the end.
6957
 
6958
    if (width != alpha.info().width() || height != alpha.info().height())
6959
    {
6960
        MSG_ERROR("Mask and alpha have different size! [ " << width << " x " << height << " to " << alpha.info().width() << " x " << alpha.info().height());
6961
        return Bm;
6962
    }
6963
 
254 andreas 6964
    if (!allocPixels(width, height, &Bm))
6965
        return Bm;
6966
 
99 andreas 6967
    Bm.eraseColor(SK_ColorTRANSPARENT);
6968
 
6969
    for (int ix = 0; ix < width; ix++)
6970
    {
6971
        for (int iy = 0; iy < height; iy++)
6972
        {
6973
            SkColor pixelAlpha = alpha.getColor(ix, iy);
100 andreas 6974
            uint32_t *bpix = Bm.getAddr32(ix, iy);
99 andreas 6975
 
100 andreas 6976
            uchar al    = SkColorGetA(pixelAlpha);
99 andreas 6977
            uchar red   = SkColorGetR(col);
6978
            uchar green = SkColorGetG(col);
6979
            uchar blue  = SkColorGetB(col);
6980
 
100 andreas 6981
            if (pixelAlpha == 0)
6982
                red = green = blue = 0;
99 andreas 6983
 
254 andreas 6984
            // Skia reads image files in the natural byte order of the CPU.
6985
            // While on Intel CPUs the byte order is little endian it is
6986
            // mostly big endian on other CPUs. This means that the order of
260 andreas 6987
            // the colors is RGB on big endian CPUs (ARM, ...) and BGR on others.
6988
            // To compensate this, we check the endianess of the CPU and set
6989
            // the byte order according.
262 andreas 6990
 
260 andreas 6991
            if (isBigEndian())
6992
                *bpix = SkColorSetARGB(al, blue, green, red);
6993
            else
6994
                *bpix = SkColorSetARGB(al, red, green, blue);
99 andreas 6995
        }
6996
    }
6997
 
100 andreas 6998
    SkPaint paint;
6999
    paint.setBlendMode(SkBlendMode::kSrcOver);
7000
    SkCanvas can(Bm);
365 andreas 7001
    sk_sp<SkImage> _image = SkImages::RasterFromBitmap(base);
179 andreas 7002
    can.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);
99 andreas 7003
    return Bm;
7004
}
7005
 
307 andreas 7006
/**
7007
 * @brief TButton::colorImage: Colorize frame element
7008
 * This method colorizes a frame element. If there is, beside the base picture,
7009
 * also a an alpha mask picture present, the elemnt is colorized by taking the
7010
 * mask to find the pixels to colorize.
7011
 * Otherwise the pixel is melted with the target color. This means a pseudo mask
7012
 * is used.
7013
 *
7014
 * @param base      This is the base image and must be present.
7015
 * @param alpha     This is optional alpha mask. If present it is used to
7016
 *                  define the alpha value of the pixels.
7017
 * @param col       This is the color to be used.
7018
 * @param bg        This is the background color to be used on the transparent
7019
 *                  pixels inside an element. On the transparent pixels on the
7020
 *                  outside of the element the pixel is set to transparent.
7021
 * @param useBG     If this is TRUE, all transparent pixels are set to the
7022
 *                  background color \b bg.
7023
 * @return
7024
 * On success a new image containing the colorized element is returned.
7025
 * Otherwise an empty image is returned.
7026
 */
161 andreas 7027
SkBitmap TButton::colorImage(SkBitmap& base, SkBitmap& alpha, SkColor col, SkColor bg, bool useBG)
80 andreas 7028
{
81 andreas 7029
    DECL_TRACER("TButton::colorImage(SkBitmap *img, int width, int height, SkColor col, SkColor bg, bool useBG)");
80 andreas 7030
 
161 andreas 7031
    int width = base.info().width();
7032
    int height = base.info().height();
7033
 
80 andreas 7034
    if (width <= 0 || height <= 0)
7035
    {
169 andreas 7036
        MSG_WARNING("Got invalid width or height! (width: " << width << ", height: " << height << ")");
161 andreas 7037
        return SkBitmap();
80 andreas 7038
    }
7039
 
169 andreas 7040
    if (!alpha.empty())
161 andreas 7041
    {
169 andreas 7042
        if (width != alpha.info().width() || height != alpha.info().height())
7043
        {
7044
            MSG_ERROR("Base and alpha masks have different size!");
7045
            return SkBitmap();
7046
        }
161 andreas 7047
    }
7048
 
80 andreas 7049
    SkBitmap maskBm;
254 andreas 7050
 
7051
    if (!allocPixels(width, height, &maskBm))
7052
        return SkBitmap();
7053
 
160 andreas 7054
    maskBm.eraseColor(SK_ColorTRANSPARENT);
80 andreas 7055
 
7056
    for (int ix = 0; ix < width; ix++)
7057
    {
7058
        for (int iy = 0; iy < height; iy++)
7059
        {
169 andreas 7060
            SkColor pixelAlpha = 0;
7061
 
7062
            if (!alpha.empty())
7063
                pixelAlpha = alpha.getColor(ix, iy);
7064
            else
7065
                pixelAlpha = base.getColor(ix, iy);
7066
 
80 andreas 7067
            uint32_t *wpix = maskBm.getAddr32(ix, iy);
7068
 
7069
            if (!wpix)
7070
            {
7071
                MSG_ERROR("No pixel buffer!");
7072
                break;
7073
            }
7074
 
169 andreas 7075
            uint32_t ala = SkColorGetA(pixelAlpha);
81 andreas 7076
 
169 andreas 7077
            if (ala == 0 && !useBG)
307 andreas 7078
                pixelAlpha = SK_ColorTRANSPARENT; //col;
169 andreas 7079
            else if (ala == 0)
160 andreas 7080
                pixelAlpha = bg;
81 andreas 7081
            else
7082
            {
307 andreas 7083
                uint32_t red   = SkColorGetR(col);
159 andreas 7084
                uint32_t green = SkColorGetG(col);
307 andreas 7085
                uint32_t blue  = SkColorGetB(col);
169 andreas 7086
                pixelAlpha = SkColorSetARGB(ala, red, green, blue);
81 andreas 7087
            }
7088
 
160 andreas 7089
            *wpix = pixelAlpha;
80 andreas 7090
        }
7091
    }
7092
 
169 andreas 7093
    if (!alpha.empty())
7094
    {
7095
        SkPaint paint;
7096
        paint.setBlendMode(SkBlendMode::kSrcOver);
7097
        SkCanvas can(maskBm);
365 andreas 7098
        sk_sp<SkImage> _image = SkImages::RasterFromBitmap(base);
179 andreas 7099
        can.drawImage(_image, 0, 0, SkSamplingOptions(), &paint);
169 andreas 7100
    }
7101
 
160 andreas 7102
    return maskBm;
80 andreas 7103
}
7104
 
161 andreas 7105
SkBitmap TButton::retrieveBorderImage(const string& pa, const string& pb, SkColor color, SkColor bgColor)
160 andreas 7106
{
161 andreas 7107
    DECL_TRACER("TButton::retrieveBorderImage(const string& pa, const string& pb, SkColor color, SkColor bgColor)");
160 andreas 7108
 
7109
    SkBitmap bm, bma;
7110
 
161 andreas 7111
    if (!pa.empty() && !retrieveImage(pa, &bm))
160 andreas 7112
        return SkBitmap();
7113
 
161 andreas 7114
    if (!pb.empty() && !retrieveImage(pb, &bma))
339 andreas 7115
        return bm;
7116
//        return SkBitmap();
160 andreas 7117
 
161 andreas 7118
    return colorImage(bm, bma, color, bgColor, false);
160 andreas 7119
}
7120
 
99 andreas 7121
bool TButton::retrieveImage(const string& path, SkBitmap* image)
7122
{
7123
    DECL_TRACER("TButton::retrieveImage(const string& path, SkBitmap* image)");
7124
 
7125
    sk_sp<SkData> im;
7126
 
7127
    if (!(im = readImage(path)))
7128
        return false;
7129
 
7130
    DecodeDataToBitmap(im, image);
7131
 
7132
    if (image->empty())
7133
    {
7134
        MSG_WARNING("Could not create the image " << path);
7135
        return false;
7136
    }
7137
 
7138
    return true;
7139
}
7140
 
6 andreas 7141
void TButton::show()
7142
{
7143
    DECL_TRACER("TButton::show()");
7144
 
345 andreas 7145
    // First we detect whether we have a dynamic button or not.
7146
    // To do this, we find out the current active instance.
7147
    int inst = 0;
7148
 
7149
    if (mActInstance >= 0 && (size_t)mActInstance < sr.size())
7150
        inst = mActInstance;
7151
    // If the dynamic flag is not set and we have already an image of the
7152
    // button, we send just the saved image to the screen.
7153
    if (visible && !mChanged && !sr[inst].dynamic && !mLastImage.empty())
7154
    {
7155
        showLastButton();
334 andreas 7156
        return;
345 andreas 7157
    }
7158
    // Here the button, or the active instance was never drawn or it is a
7159
    // dynamic button. Then the button must be drawn.
15 andreas 7160
    visible = true;
7161
    makeElement();
6 andreas 7162
 
15 andreas 7163
    if (isSystemButton() && !mSystemReg)
7164
        registerSystemButton();
7165
}
7166
 
7167
void TButton::showLastButton()
7168
{
7169
    DECL_TRACER("TButton::showLastButton()");
7170
 
7171
    if (mLastImage.empty())
334 andreas 7172
    {
7173
#if TESTMODE == 1
7174
        setScreenDone();
7175
#endif
15 andreas 7176
        return;
334 andreas 7177
    }
15 andreas 7178
 
38 andreas 7179
    if (!_displayButton && gPageManager)
7180
        _displayButton = gPageManager->getCallbackDB();
7181
 
195 andreas 7182
    if (!prg_stopped && visible)
6 andreas 7183
    {
15 andreas 7184
        ulong parent = mHandle & 0xffff0000;
7185
        size_t rowBytes = mLastImage.info().minRowBytes();
26 andreas 7186
        int rwidth = wt;
7187
        int rheight = ht;
7188
        int rleft = lt;
7189
        int rtop = tp;
43 andreas 7190
#ifdef _SCALE_SKIA_
26 andreas 7191
        if (gPageManager && gPageManager->getScaleFactor() != 1.0)
7192
        {
7193
            rwidth = (int)((double)wt * gPageManager->getScaleFactor());
7194
            rheight = (int)((double)ht * gPageManager->getScaleFactor());
7195
            rleft = (int)((double)lt * gPageManager->getScaleFactor());
7196
            rtop = (int)((double)tp * gPageManager->getScaleFactor());
7197
        }
43 andreas 7198
#endif
195 andreas 7199
        if (type == TEXT_INPUT)
7200
        {
7201
            if (gPageManager && gPageManager->getCallbackInputText())
7202
            {
7203
                BITMAP_t bm;
7204
                bm.buffer = (unsigned char *)mLastImage.getPixels();
7205
                bm.rowBytes = rowBytes;
7206
                bm.left = rleft;
7207
                bm.top = rtop;
7208
                bm.width = rwidth;
7209
                bm.height = rheight;
291 andreas 7210
                gPageManager->getCallbackInputText()(this, bm, mBorderWidth);
195 andreas 7211
            }
7212
        }
206 andreas 7213
        else if (type == LISTBOX)
7214
        {
7215
            if (gPageManager && gPageManager->getCallbackListBox())
7216
            {
7217
                BITMAP_t bm;
7218
                bm.buffer = (unsigned char *)mLastImage.getPixels();
7219
                bm.rowBytes = rowBytes;
7220
                bm.left = rleft;
7221
                bm.top = rtop;
7222
                bm.width = rwidth;
7223
                bm.height = rheight;
291 andreas 7224
                gPageManager->getCallbackListBox()(this, bm, mBorderWidth);
206 andreas 7225
            }
7226
        }
291 andreas 7227
        else if (type == SUBPAGE_VIEW)
7228
        {
7229
            if (gPageManager && gPageManager->getDisplayViewButton())
7230
            {
7231
                TBitmap image((unsigned char *)mLastImage.getPixels(), mLastImage.info().width(), mLastImage.info().height());
7232
                TColor::COLOR_T bgcolor = TColor::getAMXColor(sr[mActInstance].cf);
7233
                gPageManager->getDisplayViewButton()(mHandle, getParent(), (on.empty() ? false : true), image, wt, ht, lt, tp, sa, bgcolor);
7234
            }
7235
        }
195 andreas 7236
        else if (_displayButton)
289 andreas 7237
        {
7238
            TBitmap image((unsigned char *)mLastImage.getPixels(), mLastImage.info().width(), mLastImage.info().height());
298 andreas 7239
            _displayButton(mHandle, parent, image, rwidth, rheight, rleft, rtop, isPassThrough());
289 andreas 7240
        }
195 andreas 7241
 
176 andreas 7242
        mChanged = false;
15 andreas 7243
    }
7244
}
7245
 
7246
void TButton::hide(bool total)
7247
{
7248
    DECL_TRACER("TButton::hide()");
7249
 
101 andreas 7250
//    if (type == MULTISTATE_GENERAL && ar == 1)
7251
//        mAniStop = true;
15 andreas 7252
 
21 andreas 7253
    if (!prg_stopped && total)
15 andreas 7254
    {
26 andreas 7255
        int rwidth = wt;
7256
        int rheight = ht;
7257
        int rleft = lt;
7258
        int rtop = tp;
7259
 
38 andreas 7260
        ulong parent = mHandle & 0xffff0000;
7261
        THR_REFRESH_t *tr = _findResource(mHandle, parent, bi);
7262
 
7263
        if (tr && tr->mImageRefresh)
7264
        {
7265
            if (tr->mImageRefresh->isRunning())
7266
                tr->mImageRefresh->stop();
7267
        }
43 andreas 7268
#ifdef _SCALE_SKIA_
26 andreas 7269
        if (gPageManager && gPageManager->getScaleFactor() != 1.0)
7270
        {
7271
            rwidth = (int)((double)wt * gPageManager->getScaleFactor());
7272
            rheight = (int)((double)ht * gPageManager->getScaleFactor());
7273
            rleft = (int)((double)lt * gPageManager->getScaleFactor());
7274
            rtop = (int)((double)tp * gPageManager->getScaleFactor());
7275
        }
43 andreas 7276
#endif
190 andreas 7277
        if (type == TEXT_INPUT)
7278
        {
7279
            if (gPageManager && gPageManager->getCallDropButton())
7280
                gPageManager->getCallDropButton()(mHandle);
7281
 
7282
            visible = false;
7283
            return;
7284
        }
7285
 
28 andreas 7286
        SkBitmap imgButton;
7287
 
165 andreas 7288
        if (rwidth < 0 || rheight < 0)
7289
        {
7290
            MSG_ERROR("Invalid size of image: " << rwidth << " x " << rheight);
7291
            return;
7292
        }
7293
 
7294
        try
7295
        {
254 andreas 7296
            if (!allocPixels(wt, ht, &imgButton))
7297
                return;
7298
 
165 andreas 7299
            imgButton.eraseColor(SK_ColorTRANSPARENT);
7300
        }
7301
        catch (std::exception& e)
7302
        {
7303
            MSG_ERROR("Error creating image: " << e.what());
7304
            visible = false;
7305
            return;
7306
        }
7307
 
38 andreas 7308
        if (!_displayButton && gPageManager)
7309
            _displayButton = gPageManager->getCallbackDB();
7310
 
7311
        if (_displayButton)
176 andreas 7312
        {
289 andreas 7313
            TBitmap image((unsigned char *)imgButton.getPixels(), imgButton.info().width(), imgButton.info().height());
298 andreas 7314
            _displayButton(mHandle, parent, image, rwidth, rheight, rleft, rtop, isPassThrough());
176 andreas 7315
            mChanged = false;
7316
        }
15 andreas 7317
    }
7318
 
7319
    visible = false;
7320
}
7321
 
154 andreas 7322
bool TButton::isClickable(int x, int y)
15 andreas 7323
{
7324
    DECL_TRACER("TButton::isClickable()");
7325
 
154 andreas 7326
    if (mEnabled && /*((cp != 0 && ch != 0) || (lp != 0 && lv != 0) || !cm.empty() || !op.empty() || !pushFunc.empty() || isSystemButton()) &&*/ hs.compare("passThru") != 0)
7327
    {
7328
        if (x != -1 && y != -1 && hs.empty() && !mLastImage.empty() && isPixelTransparent(x, y))
7329
            return false;
150 andreas 7330
 
15 andreas 7331
        return true;
154 andreas 7332
    }
15 andreas 7333
 
7334
    return false;
7335
}
7336
 
7337
/**
7338
 * Handling of system button "connection state". It consists of 12 states
7339
 * indicating the network status. The states have the following meaning:
7340
 *
7341
 * 0      Diconnected (never was connected before since startup)
7342
 * 1 - 6  Connected (blink may be shown with dark and light green)
7343
 * 7, 8   Disconnected (timeout or loss of connection)
7344
 * 9 - 11 Connection in progress
7345
 */
7346
void TButton::funcNetwork(int state)
7347
{
7348
    DECL_TRACER("TButton::funcNetwork(int state)");
7349
 
7350
    mLastLevel = state;
7351
    mActInstance = state;
176 andreas 7352
    mChanged = true;
15 andreas 7353
 
7354
    if (visible)
7355
        makeElement(state);
7356
}
7357
 
7358
/**
7359
 * Handling the timer event from the controller. This comes usualy every
7360
 * 20th part of a second (1 second / 20)
7361
 */
7362
void TButton::funcTimer(const amx::ANET_BLINK& blink)
7363
{
7364
    DECL_TRACER("TButton::funcTimer(const amx::ANET_BLINK& blink)");
7365
 
7366
    string tm;
7367
    std::stringstream sstr;
7368
 
7369
    switch (ad)
7370
    {
7371
        case 141:   // Standard time
7372
            sstr << std::setw(2) << std::setfill('0') << (int)blink.hour << ":"
7373
                 << std::setw(2) << std::setfill('0') << (int)blink.minute << ":"
7374
                 << std::setw(2) << std::setfill('0') << (int)blink.second;
7375
            mLastBlink = blink;
7376
        break;
7377
 
7378
        case 142:   // Time AM/PM
6 andreas 7379
        {
15 andreas 7380
            int hour = (blink.hour > 12) ? (blink.hour - 12) : blink.hour;
7381
            sstr << std::setw(2) << std::setfill('0') << hour << ":"
7382
                 << std::setw(2) << std::setfill('0') << (int)blink.minute << " ";
7383
 
7384
            if (blink.hour <= 12)
7385
                sstr << "AM";
7386
            else
7387
                sstr << "PM";
7388
 
7389
            mLastBlink = blink;
6 andreas 7390
        }
15 andreas 7391
        break;
6 andreas 7392
 
15 andreas 7393
        case 143:   // Time 24 hours
7394
            sstr << std::setw(2) << std::setfill('0') << (int)blink.hour << ":"
7395
                 << std::setw(2) << std::setfill('0') << (int)blink.minute;
7396
            mLastBlink = blink;
7397
        break;
7398
 
7399
        case 151:   // Weekday
7400
            switch (blink.weekday)
7401
            {
7402
                case 0: sstr << "Monday"; break;
7403
                case 1: sstr << "Tuesday"; break;
7404
                case 2: sstr << "Wednesday"; break;
7405
                case 3: sstr << "Thursday"; break;
7406
                case 4: sstr << "Friday"; break;
7407
                case 5: sstr << "Saturday"; break;
7408
                case 6: sstr << "Sunday"; break;
7409
            }
7410
        break;
7411
 
7412
        case 152:   // Date mm/dd
7413
            sstr << (int)blink.month << "/" << (int)blink.day;
7414
        break;
7415
 
7416
        case 153:   // Date dd/mm
7417
            sstr << (int)blink.day << "/" << (int)blink.month;
7418
        break;
7419
 
7420
        case 154:   // Date mm/dd/yyyy
7421
            sstr << (int)blink.month << "/" << (int)blink.day << "/" << (int)blink.year;
7422
        break;
7423
 
7424
        case 155:   // Date dd/mm/yyyy
7425
            sstr << blink.day << "/" << blink.month << "/" << blink.year;
7426
        break;
7427
 
7428
        case 156:   // Date month dd/yyyy
7429
            switch (blink.month)
7430
            {
7431
                case 1:  sstr << "January"; break;
7432
                case 2:  sstr << "February"; break;
7433
                case 3:  sstr << "March"; break;
7434
                case 4:  sstr << "April"; break;
7435
                case 5:  sstr << "May"; break;
7436
                case 6:  sstr << "June"; break;
7437
                case 7:  sstr << "July"; break;
7438
                case 8:  sstr << "August"; break;
7439
                case 9:  sstr << "September"; break;
7440
                case 10:  sstr << "October"; break;
7441
                case 11:  sstr << "November"; break;
7442
                case 12:  sstr << "December"; break;
7443
            }
7444
 
7445
            sstr << " " << (int)blink.day << "/" << (int)blink.year;
7446
        break;
7447
 
7448
        case 157:   // Date dd month yyyy
7449
            sstr << (int)blink.day;
7450
 
7451
            switch (blink.month)
7452
            {
7453
                case 1:  sstr << "January"; break;
7454
                case 2:  sstr << "February"; break;
7455
                case 3:  sstr << "March"; break;
7456
                case 4:  sstr << "April"; break;
7457
                case 5:  sstr << "May"; break;
7458
                case 6:  sstr << "June"; break;
7459
                case 7:  sstr << "July"; break;
7460
                case 8:  sstr << "August"; break;
7461
                case 9:  sstr << "September"; break;
7462
                case 10:  sstr << "October"; break;
7463
                case 11:  sstr << "November"; break;
7464
                case 12:  sstr << "December"; break;
7465
            }
7466
 
7467
            sstr << " " << (int)blink.year;
7468
        break;
7469
 
7470
        case 158:   // Date yyyy-mm-dd
7471
            sstr << (int)blink.year << "-" << (int)blink.month << "-" << (int)blink.day;
7472
        break;
176 andreas 7473
 
7474
        default:
7475
            return;
6 andreas 7476
    }
15 andreas 7477
 
7478
    vector<SR_T>::iterator iter;
7479
    tm = sstr.str();
7480
 
118 andreas 7481
    for (iter = sr.begin(); iter != sr.end(); ++iter)
15 andreas 7482
        iter->te = tm;
7483
 
176 andreas 7484
    mChanged = true;
7485
 
15 andreas 7486
    if (visible)
7487
        makeElement(mActInstance);
6 andreas 7488
}
7 andreas 7489
 
15 andreas 7490
bool TButton::isPixelTransparent(int x, int y)
7491
{
7492
    DECL_TRACER("TButton::isPixelTransparent(int x, int y)");
7493
 
54 andreas 7494
    // If there is no image we treat it as a non transpararent pixel.
7495
    if (sr[mActInstance].mi.empty() && sr[mActInstance].bm.empty())
7496
        return false;
7497
 
7498
    // The mLastImage must never be empty! Although this should never be true,
7499
    // we treat it as a transparent pixel if it happen.
15 andreas 7500
    if (mLastImage.empty())
7501
    {
7502
        MSG_ERROR("Internal error: No image for button available!");
7503
        return true;
7504
    }
7505
 
54 andreas 7506
    // Make sure the coordinates are inside the bounds. A test for a pixel
7507
    // outside the bounds would lead in an immediate exit because of an assert
7508
    // test in skia. Although this should not happen, we treat the pixel as
7509
    // transparent in this case, because the coordinates didn't hit the button.
31 andreas 7510
    if (x < 0 || x >= mLastImage.info().width() || y < 0 || y >= mLastImage.info().height())
7511
    {
7512
        MSG_ERROR("The X or Y coordinate is out of bounds!");
7513
        MSG_ERROR("X=" << x << ", Y=" << y << ", width=" << mLastImage.info().width() << ", height=" << mLastImage.info().height());
54 andreas 7514
        return true;
31 andreas 7515
    }
7516
 
54 andreas 7517
    float alpha = mLastImage.getAlphaf(x, y);   // Get the alpha value (0.0 to 1.0)
15 andreas 7518
 
7519
    if (alpha != 0.0)
7520
        return false;
7521
 
7522
    return true;
7523
}
7524
 
71 andreas 7525
bool TButton::checkForSound()
7526
{
7527
    DECL_TRACER("TButton::checkForSound()");
7528
 
7529
    vector<SR_T>::iterator iter;
7530
 
7531
    for (iter = sr.begin(); iter != sr.end(); ++iter)
7532
    {
7533
        if (!iter->sd.empty())
7534
            return true;
7535
    }
7536
 
7537
    return false;
7538
}
7539
 
79 andreas 7540
bool TButton::scaleImage(SkBitmap *bm, double scaleWidth, double scaleHeight)
7541
{
7542
    DECL_TRACER("TButton::scaleImage(SkBitmap *bm, double scaleWidth, double scaleHeight)");
7543
 
7544
    if (!bm)
7545
        return false;
7546
 
7547
    if (scaleWidth == 1.0 && scaleHeight == 1.0)
7548
        return true;
7549
 
7550
    SkPaint paint;
7551
    paint.setBlendMode(SkBlendMode::kSrc);
179 andreas 7552
//    paint.setFilterQuality(kHigh_SkFilterQuality);
79 andreas 7553
    // Calculate new dimension
7554
    SkImageInfo info = bm->info();
7555
    int width  = std::max(1, (int)((double)info.width() * scaleWidth));
7556
    int height = std::max(1, (int)((double)info.height() * scaleHeight));
80 andreas 7557
    MSG_DEBUG("Scaling image to size " << width << " x " << height);
79 andreas 7558
    // Create a canvas and draw new image
365 andreas 7559
    sk_sp<SkImage> im = SkImages::RasterFromBitmap(*bm);
254 andreas 7560
 
7561
    if (!allocPixels(width, height, bm))
7562
        return false;
7563
 
79 andreas 7564
    bm->eraseColor(SK_ColorTRANSPARENT);
254 andreas 7565
    SkCanvas can(*bm, SkSurfaceProps());
79 andreas 7566
    SkRect rect = SkRect::MakeXYWH(0, 0, width, height);
179 andreas 7567
    can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
79 andreas 7568
    return true;
7569
}
7570
 
7571
bool TButton::stretchImageWidth(SkBitmap *bm, int width)
7572
{
7573
    DECL_TRACER("TButton::stretchImageWidth(SkBitmap *bm, int width)");
7574
 
80 andreas 7575
    if (!bm)
7576
        return false;
7577
 
157 andreas 7578
    int rwidth = width;
79 andreas 7579
    SkPaint paint;
7580
    paint.setBlendMode(SkBlendMode::kSrc);
179 andreas 7581
//    paint.setFilterQuality(kHigh_SkFilterQuality);
79 andreas 7582
 
7583
    SkImageInfo info = bm->info();
365 andreas 7584
    sk_sp<SkImage> im = SkImages::RasterFromBitmap(*bm);
157 andreas 7585
 
7586
    if (width <= 0)
7587
        rwidth = info.width() + width;
7588
 
7589
    if (rwidth <= 0)
7590
        rwidth = 1;
7591
 
7592
    MSG_DEBUG("Width: " << rwidth << ", Height: " << info.height());
254 andreas 7593
 
7594
    if (!allocPixels(rwidth, info.height(), bm))
7595
        return false;
7596
 
79 andreas 7597
    bm->eraseColor(SK_ColorTRANSPARENT);
254 andreas 7598
    SkCanvas can(*bm, SkSurfaceProps());
157 andreas 7599
    SkRect rect = SkRect::MakeXYWH(0, 0, rwidth, info.height());
179 andreas 7600
    can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
80 andreas 7601
    return true;
79 andreas 7602
}
7603
 
7604
bool TButton::stretchImageHeight(SkBitmap *bm, int height)
7605
{
7606
    DECL_TRACER("TButton::stretchImageHeight(SkBitmap *bm, int height)");
7607
 
80 andreas 7608
    if (!bm)
7609
        return false;
7610
 
157 andreas 7611
    int rheight = height;
79 andreas 7612
    SkPaint paint;
7613
    paint.setBlendMode(SkBlendMode::kSrc);
179 andreas 7614
//    paint.setFilterQuality(kHigh_SkFilterQuality);
79 andreas 7615
 
7616
    SkImageInfo info = bm->info();
157 andreas 7617
 
7618
    if (height <= 0)
7619
        rheight = info.height() + height;
7620
 
7621
    if (rheight <= 0)
7622
        rheight = 1;
7623
 
365 andreas 7624
    sk_sp<SkImage> im = SkImages::RasterFromBitmap(*bm);
157 andreas 7625
    MSG_DEBUG("Width: " << info.width() << ", Height: " << rheight);
254 andreas 7626
 
7627
    if (!allocPixels(info.width(), rheight, bm))
7628
        return false;
7629
 
79 andreas 7630
    bm->eraseColor(SK_ColorTRANSPARENT);
254 andreas 7631
    SkCanvas can(*bm, SkSurfaceProps());
157 andreas 7632
    SkRect rect = SkRect::MakeXYWH(0, 0, info.width(), rheight);
179 andreas 7633
    can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
80 andreas 7634
    return true;
79 andreas 7635
}
7636
 
157 andreas 7637
bool TButton::stretchImageWH(SkBitmap *bm, int width, int height)
7638
{
7639
    DECL_TRACER("TButton::stretchImageWH(SkBitmap *bm, int width, int height)");
7640
 
7641
    if (!bm)
7642
        return false;
7643
 
7644
    int rwidth = width;
7645
    int rheight = height;
7646
    SkPaint paint;
7647
    paint.setBlendMode(SkBlendMode::kSrc);
179 andreas 7648
//    paint.setFilterQuality(kHigh_SkFilterQuality);
157 andreas 7649
 
7650
    SkImageInfo info = bm->info();
7651
 
7652
    if (width <= 0)
7653
        rwidth = info.width() + width;
7654
 
7655
    if (height <= 0)
7656
        rheight = info.height() + height;
7657
 
7658
    if (rheight <= 0)
7659
        rheight = 1;
7660
 
7661
    if (rwidth <= 0)
7662
        rwidth = 1;
7663
 
365 andreas 7664
    sk_sp<SkImage> im = SkImages::RasterFromBitmap(*bm);
157 andreas 7665
    MSG_DEBUG("Width: " << rwidth << ", Height: " << rheight);
254 andreas 7666
 
7667
    if (!allocPixels(rwidth, rheight, bm))
7668
        return false;
7669
 
157 andreas 7670
    bm->eraseColor(SK_ColorTRANSPARENT);
254 andreas 7671
    SkCanvas can(*bm, SkSurfaceProps());
157 andreas 7672
    SkRect rect = SkRect::MakeXYWH(0, 0, rwidth, rheight);
179 andreas 7673
    can.drawImageRect(im, rect, SkSamplingOptions(), &paint);
157 andreas 7674
    return true;
7675
}
7676
 
15 andreas 7677
/**
7678
 * This button got the click because it matches the coordinates of a mouse
7679
 * click. It checkes whether it is clickable or not. If it is clickable, it
7680
 * depends on the type of element what happens.
7681
 */
7682
bool TButton::doClick(int x, int y, bool pressed)
7683
{
146 andreas 7684
    DECL_TRACER("TButton::doClick(int x, int y, bool pressed)");
154 andreas 7685
 
7686
    if (!isClickable(x, y))
7687
        return false;
7688
 
14 andreas 7689
    amx::ANET_SEND scmd;
15 andreas 7690
    int instance = 0;
31 andreas 7691
    int sx = x, sy = y;
71 andreas 7692
    bool isSystem = isSystemButton();
11 andreas 7693
 
289 andreas 7694
    if (pressed && gPageManager && !checkForSound() && (ch > 0 || lv > 0 || !pushFunc.empty() || isSystem))
71 andreas 7695
    {
7696
        TSystemSound sysSound(TConfig::getSystemPath(TConfig::SOUNDS));
7697
 
289 andreas 7698
        if (gPageManager->havePlaySound() && sysSound.getSystemSoundState())
71 andreas 7699
            gPageManager->getCallPlaySound()(sysSound.getTouchFeedbackSound());
7700
    }
7701
 
43 andreas 7702
#ifdef _SCALE_SKIA_
31 andreas 7703
    // To be able to test a button for a transparent pixel, we must scale
7704
    // the coordinates because the test is made on the last drawn image and
7705
    // this image is scaled (if scaling is activated).
7706
    if (TConfig::getScale() && gPageManager && gPageManager->getScaleFactor() != 1.0)
7707
    {
7708
        double scaleFactor = gPageManager->getScaleFactor();
7709
        sx = (int)((double)x * scaleFactor);
7710
        sy = (int)((double)y * scaleFactor);
7711
    }
43 andreas 7712
#endif
292 andreas 7713
 
266 andreas 7714
    if (_buttonPress && mActInstance >= 0 && (size_t)mActInstance < sr.size() && cp == 0 && ch > 0)
268 andreas 7715
        _buttonPress(ch, mHandle, pressed);
266 andreas 7716
 
11 andreas 7717
    if (type == GENERAL)
7718
    {
206 andreas 7719
        MSG_DEBUG("Button type: GENERAL; System button: " << (isSystem ? "YES" : "NO") << "; CH: " << cp << ":" << ch << "; AD: " << ap << ":" << ad);
35 andreas 7720
 
206 andreas 7721
        if (isSystem && ch == SYSTEM_ITEM_SOUNDSWITCH)   // Button sounds on/off
11 andreas 7722
        {
7723
            if (pressed)
71 andreas 7724
            {
7725
                MSG_TRACE("System button sounds are toggled ...");
195 andreas 7726
                TConfig::setTemporary(false);
71 andreas 7727
                bool sstate = TConfig::getSystemSoundState();
7728
 
7729
                if (sstate)
7730
                    mActInstance = instance = 0;
7731
                else
7732
                    mActInstance = instance = 1;
7733
 
7734
                TConfig::saveSystemSoundState(!sstate);
7735
                TConfig::saveSettings();
176 andreas 7736
                mChanged = true;
198 andreas 7737
                drawButton(mActInstance, true);
71 andreas 7738
            }
7739
        }
206 andreas 7740
        else if (isSystem && ch == SYSTEM_ITEM_SETUPPAGE)  // Enter setup page
113 andreas 7741
        {
7742
            if (pressed)
7743
            {
7744
                if (gPageManager && gPageManager->haveSetupPage())
7745
                    gPageManager->callSetupPage();
7746
            }
7747
        }
206 andreas 7748
        else if (isSystem && ch == SYSTEM_ITEM_SHUTDOWN)  // Shutdown program
113 andreas 7749
        {
7750
            if (pressed)
7751
            {
7752
                if (gPageManager && gPageManager->haveShutdown())
7753
                    gPageManager->callShutdown();
7754
            }
7755
        }
206 andreas 7756
        else if (isSystem && ch == SYSTEM_ITEM_VOLUMEUP)     // System volume up
154 andreas 7757
        {
195 andreas 7758
            TConfig::setTemporary(true);
154 andreas 7759
            int vol = TConfig::getSystemVolume() + 10;
7760
 
7761
            if (vol > 100)
7762
                vol = 100;
7763
 
198 andreas 7764
            if (pressed)
7765
                TConfig::saveSystemVolume(vol);
154 andreas 7766
 
7767
            if (pressed)
7768
                mActInstance = instance = 1;
7769
            else
7770
                mActInstance = instance = 0;
7771
 
176 andreas 7772
            mChanged = true;
154 andreas 7773
            drawButton(mActInstance, true);
7774
 
198 andreas 7775
            if (pressed && gPageManager)
154 andreas 7776
            {
198 andreas 7777
                int channel = TConfig::getChannel();
7778
                int system = TConfig::getSystem();
7779
 
154 andreas 7780
                amx::ANET_COMMAND cmd;
7781
                cmd.MC = 0x000a;
7782
                cmd.device1 = channel;
163 andreas 7783
                cmd.port1 = 0;
154 andreas 7784
                cmd.system = system;
7785
                cmd.data.message_value.system = system;
7786
                cmd.data.message_value.value = 9;   // System volume
7787
                cmd.data.message_value.content.integer = vol;
7788
                cmd.data.message_value.device = channel;
163 andreas 7789
                cmd.data.message_value.port = 0;    // Must be the address port of button
154 andreas 7790
                cmd.data.message_value.type = 0x20; // Unsigned int
7791
                gPageManager->doCommand(cmd);
7792
            }
7793
        }
206 andreas 7794
        else if (isSystem && ch == SYSTEM_ITEM_VOLUMEDOWN)     // System volume down
154 andreas 7795
        {
195 andreas 7796
            TConfig::setTemporary(true);
154 andreas 7797
            int vol = TConfig::getSystemVolume() - 10;
7798
 
7799
            if (vol < 0)
7800
                vol = 0;
7801
 
198 andreas 7802
            if (pressed)
7803
                TConfig::saveSystemVolume(vol);
154 andreas 7804
 
7805
            if (pressed)
7806
                mActInstance = instance = 1;
7807
            else
7808
                mActInstance = instance = 0;
7809
 
176 andreas 7810
            mChanged = true;
154 andreas 7811
            drawButton(mActInstance, true);
7812
 
198 andreas 7813
            if (pressed && gPageManager)
154 andreas 7814
            {
198 andreas 7815
                int channel = TConfig::getChannel();
7816
                int system = TConfig::getSystem();
7817
 
154 andreas 7818
                amx::ANET_COMMAND cmd;
7819
                cmd.MC = 0x000a;
7820
                cmd.device1 = channel;
163 andreas 7821
                cmd.port1 = 0;
154 andreas 7822
                cmd.system = system;
7823
                cmd.data.message_value.system = system;
7824
                cmd.data.message_value.value = 9;   // System volume
7825
                cmd.data.message_value.content.integer = vol;
7826
                cmd.data.message_value.device = channel;
163 andreas 7827
                cmd.data.message_value.port = 0;    // Must be the address port of button
154 andreas 7828
                cmd.data.message_value.type = 0x20; // Unsigned int
7829
                gPageManager->doCommand(cmd);
7830
            }
7831
        }
206 andreas 7832
        else if (isSystem && ch == SYSTEM_ITEM_VOLUMEMUTE)     // System mute
141 andreas 7833
        {
7834
            if (pressed)
7835
            {
195 andreas 7836
                TConfig::setTemporary(true);
141 andreas 7837
                bool mute = TConfig::getMuteState();
7838
 
7839
                if (mute)
7840
                    mActInstance = instance = 0;
7841
                else
7842
                    mActInstance = instance = 1;
7843
 
7844
                TConfig::setMuteState(!mute);
7845
 
7846
                if (gPageManager && gPageManager->getCallMuteSound())
7847
                    gPageManager->getCallMuteSound()(!mute);
7848
 
176 andreas 7849
                mChanged = true;
195 andreas 7850
                drawButton(mActInstance, true);
141 andreas 7851
            }
7852
        }
206 andreas 7853
        else if (isSystem && ch == SYSTEM_ITEM_BTSAVESETTINGS)     // System button OK: Save settings
192 andreas 7854
        {
7855
            if (pressed)
7856
            {
7857
                mActInstance = instance = 1;
7858
                TConfig::setTemporary(true);
7859
                TConfig::saveSettings();
198 andreas 7860
                drawButton(mActInstance, true);
192 andreas 7861
 
217 andreas 7862
                if (gPageManager && gPageManager->getSettings() != gPageManager->getSystemSettings())
197 andreas 7863
                    gPageManager->hideSetup();
217 andreas 7864
                else if (gPageManager && gPageManager->getDisplayMessage())
7865
                    gPageManager->getDisplayMessage()("Settings were saved!", "Info");
7866
                else
7867
                    MSG_INFO("Settings were saved.");
192 andreas 7868
            }
7869
            else
7870
            {
7871
                mActInstance = instance = 0;
198 andreas 7872
                drawButton(mActInstance, true);
192 andreas 7873
            }
7874
        }
206 andreas 7875
        else if (isSystem && ch == SYSTEM_ITEM_BTCANCELSETTINGS)     // System button Cancel: Cancel settings changes
192 andreas 7876
        {
7877
            if (pressed)
7878
            {
7879
                mActInstance = instance = 1;
7880
                TConfig::reset();
198 andreas 7881
                drawButton(mActInstance, true);
192 andreas 7882
 
217 andreas 7883
                if (gPageManager && gPageManager->getSettings() != gPageManager->getSystemSettings())
197 andreas 7884
                    gPageManager->hideSetup();
192 andreas 7885
            }
7886
            else
7887
            {
7888
                mActInstance = instance = 0;
198 andreas 7889
                drawButton(mActInstance, true);
192 andreas 7890
            }
7891
        }
206 andreas 7892
        else if (isSystem && ch == SYSTEM_ITEM_SIPENABLE)     // SIP: enabled/disabled
198 andreas 7893
        {
7894
            if (pressed)
7895
            {
7896
                TConfig::setTemporary(true);
7897
                bool st = TConfig::getSIPstatus();
7898
                mActInstance = instance = (st ? 0 : 1);
7899
                mChanged = true;
7900
                TConfig::setSIPstatus(!st);
7901
                drawButton(mActInstance, true);
7902
            }
7903
        }
206 andreas 7904
        else if (isSystem && ch == SYSTEM_ITEM_DEBUGINFO)    // Debug info
192 andreas 7905
        {
7906
            if (pressed)
7907
            {
7908
                TConfig::setTemporary(true);
7909
                uint ll = TConfig::getLogLevelBits();
7910
                bool st = (ll & HLOG_INFO) ? true : false;
7911
                mActInstance = instance = (st ? 0 : 1);
7912
                ll = (st ? (ll &= RLOG_INFO) : (ll |= HLOG_INFO));
7913
                mChanged = true;
7914
                TConfig::saveLogLevel(ll);
195 andreas 7915
                drawButton(mActInstance, true);
192 andreas 7916
            }
7917
        }
206 andreas 7918
        else if (isSystem && ch == SYSTEM_ITEM_DEBUGWARNING)    // Debug warning
192 andreas 7919
        {
7920
            if (pressed)
7921
            {
7922
                TConfig::setTemporary(true);
7923
                uint ll = TConfig::getLogLevelBits();
7924
                bool st = (ll & HLOG_WARNING) ? true : false;
7925
                mActInstance = instance = (st ? 0 : 1);
7926
                ll = (st ? (ll &= RLOG_WARNING) : (ll |= HLOG_WARNING));
7927
                mChanged = true;
7928
                TConfig::saveLogLevel(ll);
195 andreas 7929
                drawButton(mActInstance, true);
192 andreas 7930
            }
7931
        }
206 andreas 7932
        else if (isSystem && ch == SYSTEM_ITEM_DEBUGERROR)    // Debug error
192 andreas 7933
        {
7934
            if (pressed)
7935
            {
7936
                TConfig::setTemporary(true);
7937
                uint ll = TConfig::getLogLevelBits();
7938
                bool st = (ll & HLOG_ERROR) ? true : false;
7939
                mActInstance = instance = (st ? 0 : 1);
7940
                ll = (st ? (ll &= RLOG_ERROR) : (ll |= HLOG_ERROR));
7941
                mChanged = true;
7942
                TConfig::saveLogLevel(ll);
195 andreas 7943
                drawButton(mActInstance, true);
192 andreas 7944
            }
7945
        }
206 andreas 7946
        else if (isSystem && ch == SYSTEM_ITEM_DEBUGTRACE)    // Debug trace
192 andreas 7947
        {
7948
            if (pressed)
7949
            {
7950
                TConfig::setTemporary(true);
7951
                uint ll = TConfig::getLogLevelBits();
7952
                bool st = (ll & HLOG_TRACE) ? true : false;
7953
                mActInstance = instance = (st ? 0 : 1);
7954
                ll = (st ? (ll &= RLOG_TRACE) : (ll |= HLOG_TRACE));
7955
                mChanged = true;
7956
                TConfig::saveLogLevel(ll);
195 andreas 7957
                drawButton(mActInstance, true);
192 andreas 7958
            }
7959
        }
206 andreas 7960
        else if (isSystem && ch == SYSTEM_ITEM_DEBUGDEBUG)    // Debug debug
192 andreas 7961
        {
7962
            if (pressed)
7963
            {
7964
                TConfig::setTemporary(true);
7965
                uint ll = TConfig::getLogLevelBits();
7966
                bool st = (ll & HLOG_DEBUG) ? true : false;
7967
                mActInstance = instance = (st ? 0 : 1);
7968
                ll = (st ? (ll &= RLOG_DEBUG) : (ll |= HLOG_DEBUG));
7969
                mChanged = true;
7970
                TConfig::saveLogLevel(ll);
195 andreas 7971
                drawButton(mActInstance, true);
192 andreas 7972
            }
7973
        }
206 andreas 7974
        else if (isSystem && ch == SYSTEM_ITEM_DEBUGPROTOCOL)    // Debug protocol
192 andreas 7975
        {
7976
            if (pressed)
7977
            {
7978
                TConfig::setTemporary(true);
7979
                uint ll = TConfig::getLogLevelBits();
7980
                bool st = (ll & HLOG_PROTOCOL) == HLOG_PROTOCOL ? true : false;
7981
                mActInstance = instance = (st ? 0 : 1);
7982
                ll = (st ? (ll &= RLOG_PROTOCOL) : (ll |= HLOG_PROTOCOL));
7983
                mChanged = true;
7984
                TConfig::saveLogLevel(ll);
195 andreas 7985
                drawButton(mActInstance, true);
192 andreas 7986
 
7987
                if (gPageManager)
7988
                    gPageManager->updateActualPage();
7989
            }
7990
        }
206 andreas 7991
        else if (isSystem && ch == SYSTEM_ITEM_DEBUGALL)    // Debug all
192 andreas 7992
        {
7993
            if (pressed)
7994
            {
7995
                TConfig::setTemporary(true);
7996
                uint ll = TConfig::getLogLevelBits();
7997
                bool st = (ll & HLOG_ALL) == HLOG_ALL ? true : false;
7998
                mActInstance = instance = (st ? 0 : 1);
7999
                ll = (st ? (ll &= RLOG_ALL) : (ll |= HLOG_ALL));
8000
                mChanged = true;
8001
                TConfig::saveLogLevel(ll);
195 andreas 8002
                drawButton(mActInstance, true);
192 andreas 8003
 
8004
                if (gPageManager)
8005
                    gPageManager->updateActualPage();
8006
            }
8007
        }
206 andreas 8008
        else if (isSystem && ch == SYSTEM_ITEM_DEBUGPROFILE)    // Log profiling
192 andreas 8009
        {
8010
            if (pressed)
8011
            {
8012
                TConfig::setTemporary(true);
8013
                bool st = TConfig::getProfiling();
8014
                mActInstance = instance = (st ? 0 : 1);
8015
                mChanged = true;
8016
                TConfig::saveProfiling(!st);
195 andreas 8017
                drawButton(mActInstance, true);
192 andreas 8018
            }
8019
        }
206 andreas 8020
        else if (isSystem && ch == SYSTEM_ITEM_DEBUGLONG)    // Log long format
192 andreas 8021
        {
8022
            if (pressed)
8023
            {
8024
                TConfig::setTemporary(true);
8025
                bool st = TConfig::isLongFormat();
8026
                mActInstance = instance = (st ? 0 : 1);
8027
                mChanged = true;
8028
                TConfig::saveFormat(!st);
195 andreas 8029
                drawButton(mActInstance, true);
192 andreas 8030
            }
8031
        }
208 andreas 8032
        else if (isSystem && ch == SYSTEM_ITEM_LOGRESET)    // Log reset path
8033
        {
209 andreas 8034
            if (pressed)
208 andreas 8035
            {
209 andreas 8036
                char *HOME = getenv("HOME");
8037
                string logFile = TConfig::getLogFile();
208 andreas 8038
 
209 andreas 8039
                if (HOME)
8040
                {
8041
                    logFile = HOME;
8042
                    logFile += "/tpanel/tpanel.log";
8043
                }
208 andreas 8044
 
209 andreas 8045
                ulong handle = (SYSTEM_PAGE_LOGGING << 16) | SYSTEM_PAGE_LOG_TXLOGFILE;
8046
                TConfig::setTemporary(true);
8047
                TConfig::saveLogFile(logFile);
271 andreas 8048
                MSG_DEBUG("Setting text \"" << logFile << "\" to button " << handleToString(handle));
209 andreas 8049
 
8050
                if (gPageManager)
8051
                    gPageManager->setTextToButton(handle, logFile, true);
8052
            }
208 andreas 8053
        }
8054
        else if (isSystem && ch == SYSTEM_ITEM_LOGFILEOPEN) // Log file dialog
8055
        {
209 andreas 8056
            if (pressed && gPageManager && gPageManager->getFileDialogFunction())
8057
            {
8058
                TConfig::setTemporary(true);
8059
                ulong handle = (SYSTEM_PAGE_LOGGING << 16) | SYSTEM_PAGE_LOG_TXLOGFILE;
8060
                string currFile = TConfig::getLogFile();
8061
                gPageManager->getFileDialogFunction()(handle, currFile, "*.log *.txt", "log");
8062
            }
208 andreas 8063
        }
206 andreas 8064
        else if (isSystem && ch == SYSTEM_ITEM_FTPDOWNLOAD)    // FTP download surface button
195 andreas 8065
        {
8066
            if (pressed)
8067
            {
206 andreas 8068
                TConfig::setTemporary(false);
8069
                string surfaceOld = TConfig::getFtpSurface();
8070
                TConfig::setTemporary(true);
8071
                string surfaceNew = TConfig::getFtpSurface();
8072
 
8073
                MSG_DEBUG("Surface difference: Old: " << surfaceOld << ", New: " << surfaceNew);
8074
 
8075
                if (gPageManager && gPageManager->getDownloadSurface())
8076
                {
8077
                    size_t size = gPageManager->getFtpSurfaceSize(surfaceNew);
208 andreas 8078
                    gPageManager->getDownloadSurface()(surfaceNew, size);
206 andreas 8079
                }
8080
            }
8081
        }
8082
        else if (isSystem && ch == SYSTEM_ITEM_FTPPASSIVE)    // FTP passive mode
8083
        {
8084
            if (pressed)
8085
            {
8086
                TConfig::setTemporary(true);
195 andreas 8087
                bool st = TConfig::getFtpPassive();
8088
                mActInstance = instance = (st ? 0 : 1);
8089
                mChanged = true;
8090
                TConfig::saveFtpPassive(!st);
8091
                drawButton(mActInstance, true);
8092
            }
8093
        }
206 andreas 8094
        else if (isSystem && ch == SYSTEM_ITEM_SOUNDPLAYSYSSOUND)    // Play system sound
195 andreas 8095
        {
8096
            if (pressed)
8097
            {
8098
                TConfig::setTemporary(true);
223 andreas 8099
                string sound = TConfig::getProjectPath() + "/__system/graphics/sounds/" + TConfig::getSystemSound();
195 andreas 8100
 
223 andreas 8101
                if (!sound.empty() && gPageManager && gPageManager->getCallPlaySound())
195 andreas 8102
                    gPageManager->getCallPlaySound()(sound);
8103
            }
8104
        }
206 andreas 8105
        else if (isSystem && ch == SYSTEM_ITEM_SOUNDPLAYBEEP)    // Play single beep
195 andreas 8106
        {
8107
            if (pressed)
8108
            {
8109
                TConfig::setTemporary(true);
223 andreas 8110
                string sound = TConfig::getProjectPath() + "/__system/graphics/sounds/" + TConfig::getSingleBeepSound();
195 andreas 8111
 
8112
                if (!sound.empty() && gPageManager && gPageManager->getCallPlaySound())
8113
                    gPageManager->getCallPlaySound()(sound);
8114
            }
8115
        }
206 andreas 8116
        else if (isSystem && ch == SYSTEM_ITEM_SOUNDPLAYDBEEP)    // Play double beep
195 andreas 8117
        {
8118
            if (pressed)
8119
            {
8120
                TConfig::setTemporary(true);
223 andreas 8121
                string sound = TConfig::getProjectPath() + "/__system/graphics/sounds/" + TConfig::getDoubleBeepSound();
195 andreas 8122
 
8123
                if (!sound.empty() && gPageManager && gPageManager->getCallPlaySound())
8124
                    gPageManager->getCallPlaySound()(sound);
8125
            }
8126
        }
223 andreas 8127
        else if (isSystem && ch == SYSTEM_ITEM_SOUNDPLAYTESTSOUND)    // Play test sound
8128
        {
8129
            if (pressed)
8130
            {
8131
                TConfig::setTemporary(true);
8132
                string sound = TConfig::getProjectPath() + "/__system/graphics/sounds/audioTest.wav";
8133
 
8134
                if (gPageManager && gPageManager->getCallPlaySound())
8135
                    gPageManager->getCallPlaySound()(sound);
8136
            }
8137
        }
206 andreas 8138
        else if (isSystem && ch == SYSTEM_ITEM_SIPIPV4)    // SIP: IPv4
198 andreas 8139
        {
8140
            if (pressed)
8141
            {
8142
                TConfig::setTemporary(true);
8143
                bool st = TConfig::getSIPnetworkIPv4();
8144
                mActInstance = instance = (st ? 0 : 1);
8145
                mChanged = true;
8146
                TConfig::setSIPnetworkIPv4(!st);
8147
                drawButton(mActInstance, true);
8148
            }
8149
        }
206 andreas 8150
        else if (isSystem && ch == SYSTEM_ITEM_SIPIPV6)    // SIP: IPv6
198 andreas 8151
        {
8152
            if (pressed)
8153
            {
8154
                TConfig::setTemporary(true);
8155
                bool st = TConfig::getSIPnetworkIPv6();
8156
                mActInstance = instance = (st ? 0 : 1);
8157
                mChanged = true;
8158
                TConfig::setSIPnetworkIPv6(!st);
8159
                drawButton(mActInstance, true);
8160
            }
8161
        }
206 andreas 8162
        else if (isSystem && ch == SYSTEM_ITEM_SIPIPHONE)    // SIP: internal phone
198 andreas 8163
        {
8164
            if (pressed)
8165
            {
8166
                TConfig::setTemporary(true);
8167
                bool st = TConfig::getSIPiphone();
8168
                mActInstance = instance = (st ? 0 : 1);
8169
                mChanged = true;
8170
                TConfig::setSIPiphone(!st);
8171
                drawButton(mActInstance, true);
8172
            }
8173
        }
195 andreas 8174
#ifdef __ANDROID__
206 andreas 8175
        else if (isSystem && ch == SYSTEM_ITEM_VIEWSCALEFIT)    // Scale to fit
195 andreas 8176
        {
8177
            if (pressed)
8178
            {
8179
                TConfig::setTemporary(true);
8180
                bool st = TConfig::getScale();
8181
                mActInstance = instance = (st ? 0 : 1);
8182
                mChanged = true;
8183
                TConfig::saveScale(!st);
8184
                drawButton(mActInstance, true);
8185
            }
8186
        }
206 andreas 8187
        else if (isSystem && ch == SYSTEM_ITEM_VIEWBANNER)    // Show banner (disabled)
195 andreas 8188
        {
8189
            if (sr[0].oo < 0)
8190
            {
8191
                sr[0].oo = 128;
8192
                mChanged = true;
8193
                mActInstance = 0;
8194
                drawButton(mActInstance, true);
8195
            }
8196
        }
8197
#else
206 andreas 8198
        else if (isSystem && ch == SYSTEM_ITEM_VIEWSCALEFIT)    // Scale to fit (disabled)
195 andreas 8199
        {
8200
            if (sr[0].oo < 0)
8201
            {
8202
                sr[0].oo = 128;
8203
                mChanged = true;
8204
                mActInstance = 0;
8205
                drawButton(mActInstance, true);
8206
            }
8207
        }
206 andreas 8208
        else if (isSystem && ch == SYSTEM_ITEM_VIEWBANNER)    // Show banner
195 andreas 8209
        {
8210
            if (pressed)
8211
            {
8212
                TConfig::setTemporary(true);
8213
                bool st = TConfig::showBanner();
8214
                mActInstance = instance = (st ? 0 : 1);
8215
                mChanged = true;
8216
                TConfig::saveBanner(st);
8217
                drawButton(mActInstance, true);
8218
            }
8219
        }
8220
#endif
206 andreas 8221
        else if (isSystem && ch == SYSTEM_ITEM_VIEWNOTOOLBAR)    // Suppress toolbar
195 andreas 8222
        {
8223
            if (pressed)
8224
            {
8225
                TConfig::setTemporary(true);
8226
                bool st = TConfig::getToolbarSuppress();
8227
                mActInstance = instance = (st ? 0 : 1);
8228
                mChanged = true;
8229
                TConfig::saveToolbarSuppress(!st);
8230
                drawButton(mActInstance, true);
8231
            }
8232
        }
206 andreas 8233
        else if (isSystem && ch == SYSTEM_ITEM_VIEWTOOLBAR)    // Force toolbar
195 andreas 8234
        {
8235
            if (pressed)
8236
            {
8237
                TConfig::setTemporary(true);
198 andreas 8238
 
8239
                if (TConfig::getToolbarSuppress())
8240
                {
8241
                    if (sr[0].oo < 0)
8242
                    {
8243
                        sr[0].oo = 128;
8244
                        mChanged = true;
8245
                        mActInstance = 0;
8246
                        drawButton(mActInstance, true);
8247
                    }
8248
                }
8249
                else
8250
                {
8251
                    if (sr[0].oo >= 0)
8252
                        sr[0].oo = -1;
8253
 
8254
                    bool st = TConfig::getToolbarForce();
8255
                    mActInstance = instance = (st ? 0 : 1);
8256
                    mChanged = true;
8257
                    TConfig::saveToolbarForce(!st);
8258
                    drawButton(mActInstance, true);
8259
                }
195 andreas 8260
            }
8261
        }
206 andreas 8262
        else if (isSystem && ch == SYSTEM_ITEM_VIEWROTATE)    // Lock rotation
195 andreas 8263
        {
8264
            if (pressed)
8265
            {
8266
                TConfig::setTemporary(true);
8267
                bool st = TConfig::getRotationFixed();
8268
                mActInstance = instance = (st ? 0 : 1);
8269
                mChanged = true;
8270
                TConfig::setRotationFixed(!st);
8271
                drawButton(mActInstance, true);
8272
            }
8273
        }
71 andreas 8274
        else if (fb == FB_MOMENTARY)
8275
        {
8276
            if (pressed)
15 andreas 8277
                instance = 1;
14 andreas 8278
            else
15 andreas 8279
                instance = 0;
8280
 
71 andreas 8281
            MSG_DEBUG("Flavor FB_MOMENTARY, instance=" << instance);
15 andreas 8282
            mActInstance = instance;
176 andreas 8283
            mChanged = true;
163 andreas 8284
 
8285
            if (pushFunc.empty() || (!pushFunc.empty() && instance == 0))
8286
                drawButton(instance);
8287
 
15 andreas 8288
            // If there is nothing in "hs", then it depends on the pixel of the
8289
            // layer. Only if the pixel the coordinates point to are not
8290
            // transparent, the button takes the click.
31 andreas 8291
            if (hs.empty() && isPixelTransparent(sx, sy))
15 andreas 8292
                return false;
8293
 
79 andreas 8294
            // Play sound, if one is defined
90 andreas 8295
            if (gPageManager)
8296
            {
165 andreas 8297
                if (pressed && gPageManager->havePlaySound() && !sr[0].sd.empty() && strCaseCompare(sr[0].sd, "None") != 0)
141 andreas 8298
                    gPageManager->getCallPlaySound()(TConfig::getProjectPath() + "/sounds/" + sr[0].sd);
165 andreas 8299
                else if (!pressed && gPageManager->havePlaySound() && !sr[1].sd.empty() && strCaseCompare(sr[1].sd, "None") != 0)
141 andreas 8300
                    gPageManager->getCallPlaySound()(TConfig::getProjectPath() + "/sounds/" + sr[1].sd);
90 andreas 8301
            }
79 andreas 8302
 
15 andreas 8303
            if (pushFunc.empty())   // Don't draw the button if it has a push function defined
8304
                showLastButton();
8305
            else
14 andreas 8306
                mActInstance = 0;
15 andreas 8307
        }
8308
        else if (fb == FB_CHANNEL || fb == FB_NONE)
8309
        {
8310
            if (pressed)
8311
                instance = 1;
8312
            else
8313
                instance = 0;
14 andreas 8314
 
71 andreas 8315
            MSG_DEBUG("Flavor FB_CHANNEL, instance=" << instance);
15 andreas 8316
            // If there is nothing in "hs", then it depends on the pixel of the
8317
            // layer. Only if the pixel the coordinates point to are not
8318
            // transparent, the button takes the click.
31 andreas 8319
            if (hs.empty() && isPixelTransparent(sx, sy))
15 andreas 8320
                return false;
14 andreas 8321
        }
15 andreas 8322
        else if (fb == FB_INV_CHANNEL)
8323
        {
8324
            if (pressed)
8325
                instance = 0;
8326
            else
8327
                instance = 1;
14 andreas 8328
 
71 andreas 8329
            MSG_DEBUG("Flavor FB_INV_CHANNEL, instance=" << instance);
15 andreas 8330
            // If there is nothing in "hs", then it depends on the pixel of the
8331
            // layer. Only if the pixel the coordinates point to are not
8332
            // transparent, the button takes the click.
31 andreas 8333
            if (hs.empty() && isPixelTransparent(sx, sy))
15 andreas 8334
                return false;
79 andreas 8335
 
8336
            // Play sound, if one is defined
90 andreas 8337
            if (gPageManager)
8338
            {
165 andreas 8339
                if (pressed && gPageManager->havePlaySound() && !sr[1].sd.empty() && strCaseCompare(sr[0].sd, "None") != 0)
141 andreas 8340
                    gPageManager->getCallPlaySound()(TConfig::getProjectPath() + "/sounds/" + sr[1].sd);
165 andreas 8341
                else if (!pressed && gPageManager->havePlaySound() && !sr[0].sd.empty() && strCaseCompare(sr[1].sd, "None") != 0)
141 andreas 8342
                    gPageManager->getCallPlaySound()(TConfig::getProjectPath() + "/sounds/" + sr[0].sd);
90 andreas 8343
            }
15 andreas 8344
        }
8345
        else if (fb == FB_ALWAYS_ON)
14 andreas 8346
        {
154 andreas 8347
            int oldInst = mActInstance;
15 andreas 8348
            instance = 1;
8349
            mActInstance = 1;
71 andreas 8350
            MSG_DEBUG("Flavor FB_ALWAYS_ON, instance=" << instance);
154 andreas 8351
 
8352
            if (oldInst != mActInstance)        // This should never become true!
176 andreas 8353
            {
8354
                mChanged = true;
154 andreas 8355
                drawButton(instance, false);
176 andreas 8356
            }
154 andreas 8357
 
15 andreas 8358
            // If there is nothing in "hs", then it depends on the pixel of the
8359
            // layer. Only if the pixel the coordinates point to are not
8360
            // transparent, the button takes the click.
31 andreas 8361
            if (hs.empty() && isPixelTransparent(sx, sy))
15 andreas 8362
                return false;
8363
 
79 andreas 8364
            // Play sound, if one is defined
165 andreas 8365
            if (pressed && gPageManager && gPageManager->havePlaySound() && !sr[1].sd.empty() && strCaseCompare(sr[1].sd, "None") != 0)
141 andreas 8366
                gPageManager->getCallPlaySound()(TConfig::getProjectPath() + "/sounds/" + sr[1].sd);
71 andreas 8367
        }
8368
 
15 andreas 8369
        if ((cp && ch) || !op.empty())
8370
        {
14 andreas 8371
            scmd.device = TConfig::getChannel();
8372
            scmd.port = cp;
8373
            scmd.channel = ch;
8374
 
15 andreas 8375
            if (op.empty())
8376
            {
8377
                if (instance)
8378
                    scmd.MC = 0x0084;
8379
                else
8380
                    scmd.MC = 0x0085;
8381
            }
14 andreas 8382
            else
15 andreas 8383
            {
8384
                scmd.MC = 0x008b;
8385
                scmd.msg = op;
8386
            }
14 andreas 8387
 
271 andreas 8388
            MSG_DEBUG("Button " << bi << ", " << na << " with handle " << handleToString(mHandle));
14 andreas 8389
            MSG_DEBUG("Sending to device <" << scmd.device << ":" << scmd.port << ":0> channel " << scmd.channel << " value 0x" << std::setw(2) << std::setfill('0') << std::hex << scmd.MC << " (" << (pressed?"PUSH":"RELEASE") << ")");
8390
 
8391
            if (gAmxNet)
15 andreas 8392
            {
8393
                if (scmd.MC != 0x008b || (pressed && scmd.MC == 0x008b))
8394
                    gAmxNet->sendCommand(scmd);
8395
            }
14 andreas 8396
            else
8397
                MSG_WARNING("Missing global class TAmxNet. Can't send a message!");
8398
        }
8399
    }
15 andreas 8400
    else if (type == MULTISTATE_GENERAL)
8401
    {
79 andreas 8402
        // Play sound, if one is defined
165 andreas 8403
        if (pressed && gPageManager && gPageManager->havePlaySound() && !sr[mActInstance].sd.empty() && strCaseCompare(sr[mActInstance].sd, "None") != 0)
141 andreas 8404
            gPageManager->getCallPlaySound()(TConfig::getProjectPath() + "/sounds/" + sr[mActInstance].sd);
79 andreas 8405
 
15 andreas 8406
        if ((cp && ch) || !op.empty())
8407
        {
8408
            scmd.device = TConfig::getChannel();
8409
            scmd.port = cp;
8410
            scmd.channel = ch;
14 andreas 8411
 
15 andreas 8412
            if (op.empty())
8413
            {
8414
                if (pressed || fb == FB_ALWAYS_ON)
8415
                    scmd.MC = 0x0084;
8416
                else
8417
                    scmd.MC = 0x0085;
8418
            }
8419
            else
8420
            {
8421
                scmd.MC = 0x008b;
8422
                scmd.msg = op;
8423
            }
8424
 
271 andreas 8425
            MSG_DEBUG("Button " << bi << ", " << na << " with handle " << handleToString(mHandle));
15 andreas 8426
            MSG_DEBUG("Sending to device <" << scmd.device << ":" << scmd.port << ":0> channel " << scmd.channel << " value 0x" << std::setw(2) << std::setfill('0') << std::hex << scmd.MC << " (" << (pressed?"PUSH":"RELEASE") << ")");
8427
 
8428
            if (gAmxNet)
8429
            {
8430
                if (scmd.MC != 0x008b || (pressed && scmd.MC == 0x008b))
8431
                    gAmxNet->sendCommand(scmd);
8432
            }
8433
            else
8434
                MSG_WARNING("Missing global class TAmxNet. Can't send a message!");
8435
        }
8436
    }
49 andreas 8437
    else if (type == BARGRAPH && lf.find("active") != string::npos)
8438
    {
8439
        // Find the click position
8440
        int level = 0;
15 andreas 8441
 
49 andreas 8442
        if (dr.compare("horizontal") == 0)
8443
        {
8444
            level = (ri ? (wt - x) : x);
8445
            level = (int)((double)(rh - rl) / (double)wt * (double)level);
8446
        }
8447
        else
8448
        {
8449
            level = (ri ? y : (ht - y));
8450
            level = (int)((double)(rh - rl) / (double)ht * (double)level);
8451
        }
8452
 
8453
        // Draw the bargraph
8454
        if (!drawBargraph(mActInstance, level, visible))
8455
            return false;
8456
 
8457
        // Handle click
192 andreas 8458
        if (isSystem && lv == 9)    // System volume button
49 andreas 8459
        {
141 andreas 8460
            TConfig::saveSystemVolume(level);
8461
            TConfig::saveSettings();
8462
        }
8463
        else if ((cp && ch) || !op.empty())
8464
        {
49 andreas 8465
            scmd.device = TConfig::getChannel();
8466
            scmd.port = cp;
8467
            scmd.channel = ch;
8468
 
8469
            if (op.empty())
8470
            {
8471
                if (pressed || fb == FB_ALWAYS_ON)
8472
                    scmd.MC = 0x0084;
8473
                else
8474
                    scmd.MC = 0x0085;
8475
            }
8476
            else
8477
            {
8478
                scmd.MC = 0x008b;
8479
                scmd.msg = op;
8480
            }
8481
 
8482
            if (gAmxNet)
8483
            {
8484
                if (scmd.MC != 0x008b || (pressed && scmd.MC == 0x008b))
8485
                    gAmxNet->sendCommand(scmd);
8486
            }
8487
            else
8488
                MSG_WARNING("Missing global class TAmxNet. Can't send a message!");
8489
        }
8490
 
8491
        // Send the level
318 andreas 8492
        if (lp && lv && gPageManager && gPageManager->getLevelSendState())
49 andreas 8493
        {
8494
            scmd.device = TConfig::getChannel();
8495
            scmd.port = lp;
8496
            scmd.channel = lv;
8497
            scmd.level = lv;
8498
            scmd.value = level;
8499
            scmd.MC = 0x008a;
8500
 
8501
            if (gAmxNet)
8502
                gAmxNet->sendCommand(scmd);
8503
        }
8504
    }
50 andreas 8505
    else if (type == TEXT_INPUT)
8506
    {
8507
        MSG_DEBUG("Text area detected. Switching on keyboard");
8508
        // Drawing background graphic (visible part of area)
8509
        drawTextArea(mActInstance);
8510
    }
49 andreas 8511
 
15 andreas 8512
    /* FIXME: Move the following to class TPageManager!
8513
     *        To do that, the preconditions are to be implemented. It must be
8514
     *        possible to find the button and get access to the credentials
8515
     *        of it.
8516
     */
14 andreas 8517
    if (!pushFunc.empty() && pressed)
8518
    {
53 andreas 8519
        MSG_DEBUG("Executing a push function ...");
14 andreas 8520
        vector<PUSH_FUNC_T>::iterator iter;
8521
 
118 andreas 8522
        for (iter = pushFunc.begin(); iter != pushFunc.end(); ++iter)
14 andreas 8523
        {
168 andreas 8524
            MSG_DEBUG("Testing for function \"" << iter->pfType << "\"");
53 andreas 8525
 
163 andreas 8526
            if (fb == FB_MOMENTARY || fb == FB_NONE)
8527
                mActInstance = 0;
8528
            else if (fb == FB_ALWAYS_ON || fb == FB_INV_CHANNEL)
8529
                mActInstance = 1;
8530
 
168 andreas 8531
            if (strCaseCompare(iter->pfType, "SSHOW") == 0)            // show popup
11 andreas 8532
            {
14 andreas 8533
                if (gPageManager)
8534
                    gPageManager->showSubPage(iter->pfName);
11 andreas 8535
            }
168 andreas 8536
            else if (strCaseCompare(iter->pfType, "SHIDE") == 0)       // hide popup
11 andreas 8537
            {
14 andreas 8538
                if (gPageManager)
8539
                    gPageManager->hideSubPage(iter->pfName);
11 andreas 8540
            }
168 andreas 8541
            else if (strCaseCompare(iter->pfType, "SCGROUP") == 0)     // hide group
14 andreas 8542
            {
8543
                if (gPageManager)
8544
                    gPageManager->closeGroup(iter->pfName);
8545
            }
271 andreas 8546
            else if (strCaseCompare(iter->pfType, "SCPAGE") == 0)      // flip to page
8547
            {
8548
                if (gPageManager && !iter->pfName.empty())
8549
                    gPageManager->setPage(iter->pfName);
8550
            }
168 andreas 8551
            else if (strCaseCompare(iter->pfType, "STAN") == 0)        // Flip to standard page
14 andreas 8552
            {
8553
                if (gPageManager)
8554
                {
168 andreas 8555
                    if (!iter->pfName.empty())
8556
                        gPageManager->setPage(iter->pfName);
8557
                    else
15 andreas 8558
                    {
168 andreas 8559
                        TPage *page = gPageManager->getActualPage();
15 andreas 8560
 
168 andreas 8561
                        if (!page)
8562
                        {
8563
                            MSG_DEBUG("Internal error: No actual page found!");
8564
                            return false;
8565
                        }
15 andreas 8566
 
168 andreas 8567
                        TSettings *settings = gPageManager->getSettings();
8568
 
205 andreas 8569
                        if (settings && settings->getPowerUpPage().compare(page->getName()) != 0)
168 andreas 8570
                            gPageManager->setPage(settings->getPowerUpPage());
8571
                    }
15 andreas 8572
                }
8573
            }
168 andreas 8574
            else if (strCaseCompare(iter->pfType, "FORGET") == 0)      // Flip to page and forget
15 andreas 8575
            {
168 andreas 8576
                if (gPageManager && !iter->pfName.empty())
8577
                        gPageManager->setPage(iter->pfName, true);
8578
            }
8579
            else if (strCaseCompare(iter->pfType, "PREV") == 0)        // Flip to previous page
8580
            {
15 andreas 8581
                if (gPageManager)
8582
                {
8583
                    int old = gPageManager->getPreviousPageNumber();
8584
 
8585
                    if (old > 0)
8586
                        gPageManager->setPage(old);
8587
                }
8588
            }
168 andreas 8589
            else if (strCaseCompare(iter->pfType, "STOGGLE") == 0)     // Toggle popup state
15 andreas 8590
            {
146 andreas 8591
                if (!iter->pfName.empty() && gPageManager)
15 andreas 8592
                {
14 andreas 8593
                    TSubPage *page = gPageManager->getSubPage(iter->pfName);
11 andreas 8594
 
146 andreas 8595
                    if (!page)      // Is the page not in cache?
8596
                    {               // No, then load it
8597
                        gPageManager->showSubPage(iter->pfName);
8598
                        return true;
8599
                    }
8600
 
8601
                    if (page->isVisible())
14 andreas 8602
                        gPageManager->hideSubPage(iter->pfName);
146 andreas 8603
                    else
14 andreas 8604
                        gPageManager->showSubPage(iter->pfName);
8605
                }
8606
            }
168 andreas 8607
            else if (strCaseCompare(iter->pfType, "SCPANEL") == 0)   // Hide all popups
146 andreas 8608
            {
8609
                if (gPageManager)
8610
                {
8611
                    TSubPage *page = gPageManager->getFirstSubPage();
8612
 
8613
                    while (page)
8614
                    {
8615
                        page->drop();
8616
                        page = gPageManager->getNextSubPage();
8617
                    }
8618
                }
8619
            }
8620
            else
8621
            {
8622
                MSG_WARNING("Unknown page flip command " << iter->pfType);
8623
            }
11 andreas 8624
        }
8625
    }
15 andreas 8626
 
147 andreas 8627
    if (!cm.empty() && co == 0 && pressed)      // Feed command to ourself?
8628
    {                                           // Yes, then feed it into command queue.
149 andreas 8629
        MSG_DEBUG("Button has a self feed command");
147 andreas 8630
 
151 andreas 8631
        int channel = TConfig::getChannel();
8632
        int system = TConfig::getSystem();
8633
 
147 andreas 8634
        if (gPageManager)
8635
        {
8636
            amx::ANET_COMMAND cmd;
8637
            cmd.MC = 0x000c;
151 andreas 8638
            cmd.device1 = channel;
301 andreas 8639
            cmd.port1 = 1;
151 andreas 8640
            cmd.system = system;
8641
            cmd.data.message_string.device = channel;
301 andreas 8642
            cmd.data.message_string.port = 1;   // Must be 1
151 andreas 8643
            cmd.data.message_string.system = system;
8644
            cmd.data.message_string.type = 1;   // 8 bit char string
149 andreas 8645
 
8646
            vector<string>::iterator iter;
8647
 
8648
            for (iter = cm.begin(); iter != cm.end(); ++iter)
8649
            {
8650
                cmd.data.message_string.length = iter->length();
8651
                memset(&cmd.data.message_string.content, 0, sizeof(cmd.data.message_string.content));
205 andreas 8652
                strncpy((char *)&cmd.data.message_string.content, iter->c_str(), sizeof(cmd.data.message_string.content)-1);
318 andreas 8653
                MSG_DEBUG("Executing system command: " << *iter);
149 andreas 8654
                gPageManager->doCommand(cmd);
8655
            }
147 andreas 8656
        }
8657
    }
8658
    else if (!cm.empty() && pressed)
8659
    {
149 andreas 8660
        MSG_DEBUG("Button sends a command on port " << co);
147 andreas 8661
 
8662
        if (gPageManager)
149 andreas 8663
        {
8664
            vector<string>::iterator iter;
8665
 
8666
            for (iter = cm.begin(); iter != cm.end(); ++iter)
8667
                gPageManager->sendCommandString(co, *iter);
8668
        }
147 andreas 8669
    }
8670
 
15 andreas 8671
    return true;
11 andreas 8672
}
8673
 
7 andreas 8674
/**
8675
 * Based on the pixels in the \a basePix, the function decides whether to return
8676
 * the value of \a col1 or \a col2. A red pixel returns the color \a col1 and
8677
 * a green pixel returns the color \a col2. If there is no red and no green
8678
 * pixel, a transparent pixel is returned.
8679
 *
8680
 * @param basePix
8681
 * This is a pixel from a mask containing red and/or green pixels.
8682
 *
8683
 * @param maskPix
8684
 * This is a pixel from a mask containing more or less tranparent pixels. If
8685
 * the alpha channel of \a basePix is 0 (transparent) this pixel is returned.
8686
 *
8687
 * @param col1
8688
 * The first color.
8689
 *
8690
 * @param col2
8691
 * The second color.
8692
 *
8693
 * @return
8694
 * An array containing the color for one pixel.
8695
 */
10 andreas 8696
SkColor TButton::baseColor(SkColor basePix, SkColor maskPix, SkColor col1, SkColor col2)
7 andreas 8697
{
8698
    uint alpha = SkColorGetA(basePix);
23 andreas 8699
    uint green = SkColorGetG(basePix);
260 andreas 8700
    uint red = 0;
8701
//#if (defined(__x86_64__) || defined(_M_X64)) && !defined(__MACH__)
8702
    if (isBigEndian())
8703
        red = SkColorGetB(basePix);
8704
    else
8705
        red = SkColorGetR(basePix);
8706
//#endif
7 andreas 8707
 
241 andreas 8708
    if (alpha == 0)
7 andreas 8709
        return maskPix;
8710
 
8711
    if (red && green)
8712
    {
241 andreas 8713
        if (red < green)
8714
            return col2;
8715
 
171 andreas 8716
        return col1;
7 andreas 8717
    }
8718
 
8719
    if (red)
8720
        return col1;
8721
 
8722
    if (green)
8723
        return col2;
8724
 
8725
    return SK_ColorTRANSPARENT; // transparent pixel
8726
}
8727
 
8728
TEXT_EFFECT TButton::textEffect(const std::string& effect)
8729
{
8730
    DECL_TRACER("TButton::textEffect(const std::string& effect)");
8731
 
8732
    if (effect == "Outline-S")
8733
        return EFFECT_OUTLINE_S;
8734
    else if (effect == "Outline-M")
8735
        return EFFECT_OUTLINE_M;
8736
    else if (effect == "Outline-L")
8737
        return EFFECT_OUTLINE_L;
8738
    else if (effect == "Outline-X")
8739
        return EFFECT_OUTLINE_X;
8740
    else if (effect == "Glow-S")
8741
        return EFFECT_GLOW_S;
8742
    else if (effect == "Glow-M")
8743
        return EFFECT_GLOW_M;
8744
    else if (effect == "Glow-L")
8745
        return EFFECT_GLOW_L;
8746
    else if (effect == "Glow-X")
8747
        return EFFECT_GLOW_X;
8748
    else if (effect == "Soft Drop Shadow 1")
8749
        return EFFECT_SOFT_DROP_SHADOW_1;
8750
    else if (effect == "Soft Drop Shadow 2")
8751
        return EFFECT_SOFT_DROP_SHADOW_2;
8752
    else if (effect == "Soft Drop Shadow 3")
8753
        return EFFECT_SOFT_DROP_SHADOW_3;
8754
    else if (effect == "Soft Drop Shadow 4")
8755
        return EFFECT_SOFT_DROP_SHADOW_4;
8756
    else if (effect == "Soft Drop Shadow 5")
8757
        return EFFECT_SOFT_DROP_SHADOW_5;
8758
    else if (effect == "Soft Drop Shadow 6")
8759
        return EFFECT_SOFT_DROP_SHADOW_6;
8760
    else if (effect == "Soft Drop Shadow 7")
8761
        return EFFECT_SOFT_DROP_SHADOW_7;
8762
    else if (effect == "Soft Drop Shadow 8")
8763
        return EFFECT_SOFT_DROP_SHADOW_8;
8764
    else if (effect == "Medium Drop Shadow 1")
8765
        return EFFECT_MEDIUM_DROP_SHADOW_1;
8766
    else if (effect == "Medium Drop Shadow 2")
8767
        return EFFECT_MEDIUM_DROP_SHADOW_2;
8768
    else if (effect == "Medium Drop Shadow 3")
8769
        return EFFECT_MEDIUM_DROP_SHADOW_3;
8770
    else if (effect == "Medium Drop Shadow 4")
8771
        return EFFECT_MEDIUM_DROP_SHADOW_4;
8772
    else if (effect == "Medium Drop Shadow 5")
8773
        return EFFECT_MEDIUM_DROP_SHADOW_5;
8774
    else if (effect == "Medium Drop Shadow 6")
8775
        return EFFECT_MEDIUM_DROP_SHADOW_6;
8776
    else if (effect == "Medium Drop Shadow 7")
8777
        return EFFECT_MEDIUM_DROP_SHADOW_7;
8778
    else if (effect == "Medium Drop Shadow 8")
8779
        return EFFECT_MEDIUM_DROP_SHADOW_8;
8780
    else if (effect == "Hard Drop Shadow 1")
8781
        return EFFECT_HARD_DROP_SHADOW_1;
8782
    else if (effect == "Hard Drop Shadow 2")
8783
        return EFFECT_HARD_DROP_SHADOW_2;
8784
    else if (effect == "Hard Drop Shadow 3")
8785
        return EFFECT_HARD_DROP_SHADOW_3;
8786
    else if (effect == "Hard Drop Shadow 4")
8787
        return EFFECT_HARD_DROP_SHADOW_4;
8788
    else if (effect == "Hard Drop Shadow 5")
8789
        return EFFECT_HARD_DROP_SHADOW_5;
8790
    else if (effect == "Hard Drop Shadow 6")
8791
        return EFFECT_HARD_DROP_SHADOW_6;
8792
    else if (effect == "Hard Drop Shadow 7")
8793
        return EFFECT_HARD_DROP_SHADOW_7;
8794
    else if (effect == "Hard Drop Shadow 8")
8795
        return EFFECT_HARD_DROP_SHADOW_8;
8796
    else if (effect == "Soft Drop Shadow 1 with outline")
8797
        return EFFECT_SOFT_DROP_SHADOW_1_WITH_OUTLINE;
8798
    else if (effect == "Soft Drop Shadow 2 with outline")
8799
        return EFFECT_SOFT_DROP_SHADOW_2_WITH_OUTLINE;
8800
    else if (effect == "Soft Drop Shadow 3 with outline")
8801
        return EFFECT_SOFT_DROP_SHADOW_3_WITH_OUTLINE;
8802
    else if (effect == "Soft Drop Shadow 4 with outline")
8803
        return EFFECT_SOFT_DROP_SHADOW_4_WITH_OUTLINE;
8804
    else if (effect == "Soft Drop Shadow 5 with outline")
8805
        return EFFECT_SOFT_DROP_SHADOW_5_WITH_OUTLINE;
8806
    else if (effect == "Soft Drop Shadow 6 with outline")
8807
        return EFFECT_SOFT_DROP_SHADOW_6_WITH_OUTLINE;
8808
    else if (effect == "Soft Drop Shadow 7 with outline")
8809
        return EFFECT_SOFT_DROP_SHADOW_7_WITH_OUTLINE;
8810
    else if (effect == "Soft Drop Shadow 8 with outline")
8811
        return EFFECT_SOFT_DROP_SHADOW_8_WITH_OUTLINE;
8812
    else if (effect == "Medium Drop Shadow 1 with outline")
8813
        return EFFECT_MEDIUM_DROP_SHADOW_1_WITH_OUTLINE;
8814
    else if (effect == "Medium Drop Shadow 2 with outline")
8815
        return EFFECT_MEDIUM_DROP_SHADOW_2_WITH_OUTLINE;
8816
    else if (effect == "Medium Drop Shadow 3 with outline")
8817
        return EFFECT_MEDIUM_DROP_SHADOW_3_WITH_OUTLINE;
8818
    else if (effect == "Medium Drop Shadow 4 with outline")
8819
        return EFFECT_MEDIUM_DROP_SHADOW_4_WITH_OUTLINE;
8820
    else if (effect == "Medium Drop Shadow 5 with outline")
8821
        return EFFECT_MEDIUM_DROP_SHADOW_5_WITH_OUTLINE;
8822
    else if (effect == "Medium Drop Shadow 6 with outline")
8823
        return EFFECT_MEDIUM_DROP_SHADOW_6_WITH_OUTLINE;
8824
    else if (effect == "Medium Drop Shadow 7 with outline")
8825
        return EFFECT_MEDIUM_DROP_SHADOW_7_WITH_OUTLINE;
8826
    else if (effect == "Medium Drop Shadow 8 with outline")
8827
        return EFFECT_MEDIUM_DROP_SHADOW_8_WITH_OUTLINE;
8828
    else if (effect == "Hard Drop Shadow 1 with outline")
8829
        return EFFECT_HARD_DROP_SHADOW_1_WITH_OUTLINE;
8830
    else if (effect == "Hard Drop Shadow 2 with outline")
8831
        return EFFECT_HARD_DROP_SHADOW_2_WITH_OUTLINE;
8832
    else if (effect == "Hard Drop Shadow 3 with outline")
8833
        return EFFECT_HARD_DROP_SHADOW_3_WITH_OUTLINE;
8834
    else if (effect == "Hard Drop Shadow 4 with outline")
8835
        return EFFECT_HARD_DROP_SHADOW_4_WITH_OUTLINE;
8836
    else if (effect == "Hard Drop Shadow 5 with outline")
8837
        return EFFECT_HARD_DROP_SHADOW_5_WITH_OUTLINE;
8838
    else if (effect == "Hard Drop Shadow 6 with outline")
8839
        return EFFECT_HARD_DROP_SHADOW_6_WITH_OUTLINE;
8840
    else if (effect == "Hard Drop Shadow 7 with outline")
8841
        return EFFECT_HARD_DROP_SHADOW_7_WITH_OUTLINE;
8842
    else if (effect == "Hard Drop Shadow 8 with outline")
8843
        return EFFECT_HARD_DROP_SHADOW_8_WITH_OUTLINE;
8844
 
8845
    return EFFECT_NONE;
8846
}
15 andreas 8847
 
8848
bool TButton::isSystemButton()
8849
{
8850
    DECL_TRACER("TButton::isSystemButton()");
8851
 
195 andreas 8852
    if (type == MULTISTATE_BARGRAPH && lp == 0 && TSystem::isSystemButton(lv))
8853
        return true;
8854
    else if (type == BARGRAPH && lp == 0 && TSystem::isSystemButton(lv))
8855
        return true;
206 andreas 8856
    else if (type == LISTBOX && ap == 0 && ad > 0 && ti >= SYSTEM_PAGE_START)
198 andreas 8857
        return true;
195 andreas 8858
    else if (ap == 0 && TSystem::isSystemButton(ad))
8859
        return true;
8860
    else if (cp == 0 && TSystem::isSystemButton(ch))
8861
        return true;
15 andreas 8862
 
8863
    return false;
8864
}
21 andreas 8865
 
8866
THR_REFRESH_t *TButton::_addResource(TImageRefresh* refr, ulong handle, ulong parent, int bi)
8867
{
8868
    DECL_TRACER("TButton::_addResource(TImageRefresh* refr, ulong handle, ulong parent, int bi)");
8869
 
8870
    THR_REFRESH_t *p = mThrRefresh;
8871
    THR_REFRESH_t *r, *last = p;
8872
 
8873
    if (!refr || !handle || !parent || bi <= 0)
8874
    {
8875
        MSG_ERROR("Invalid parameter!");
8876
        return nullptr;
8877
    }
8878
 
8879
    r = new THR_REFRESH_t;
8880
    r->mImageRefresh = refr;
8881
    r->handle = handle;
8882
    r->parent = parent;
8883
    r->bi = bi;
8884
    r->next = nullptr;
8885
 
8886
    // If the chain is empty, add the new item;
8887
    if (!mThrRefresh)
8888
        mThrRefresh = r;
8889
    else    // Find the end and append the item
8890
    {
8891
        while (p)
8892
        {
8893
            last = p;
8894
 
8895
            if (p->handle == handle && p->parent == parent && p->bi == bi)
8896
            {
8897
                MSG_WARNING("Duplicate button found! Didn't add it again.");
8898
                delete r;
8899
                return p;
8900
            }
8901
 
8902
            p = p->next;
8903
        }
8904
 
8905
        last->next = r;
8906
    }
8907
 
8908
    MSG_DEBUG("New dynamic button added.");
8909
    return r;
8910
}
8911
 
8912
THR_REFRESH_t *TButton::_findResource(ulong handle, ulong parent, int bi)
8913
{
8914
    DECL_TRACER("TButton::_findResource(ulong handle, ulong parent, int bi)");
8915
 
8916
    THR_REFRESH_t *p = mThrRefresh;
8917
 
8918
    while (p)
8919
    {
8920
        if (p->handle == handle && p->parent == parent && p->bi == bi)
8921
            return p;
8922
 
8923
        p = p->next;
8924
    }
8925
 
8926
    return nullptr;
8927
}
94 andreas 8928
 
8929
void TButton::addToBitmapCache(BITMAP_CACHE& bc)
8930
{
8931
    DECL_TRACER("TButton::addToBitmapCache(BITMAP_CACHE& bc)");
8932
 
8933
    if (nBitmapCache.size() == 0)
8934
    {
8935
        nBitmapCache.push_back(bc);
8936
        return;
8937
    }
8938
 
8939
    vector<BITMAP_CACHE>::iterator iter;
8940
 
8941
    for (iter = nBitmapCache.begin(); iter != nBitmapCache.end(); ++iter)
8942
    {
8943
        if (iter->handle == bc.handle && iter->parent == bc.parent && iter->bi == bc.bi)
8944
        {
97 andreas 8945
            nBitmapCache.erase(iter);
8946
            nBitmapCache.push_back(bc);
94 andreas 8947
            return;
8948
        }
8949
    }
8950
 
8951
    nBitmapCache.push_back(bc);
8952
}
8953
 
8954
BITMAP_CACHE& TButton::getBCentryByHandle(ulong handle, ulong parent)
8955
{
8956
    DECL_TRACER("TButton::getBCentryByHandle(ulong handle, ulong parent)");
8957
 
8958
    if (nBitmapCache.size() == 0)
8959
        return mBCDummy;
8960
 
8961
    vector<BITMAP_CACHE>::iterator iter;
8962
 
8963
    for (iter = nBitmapCache.begin(); iter != nBitmapCache.end(); ++iter)
8964
    {
8965
        if (iter->handle == handle && iter->parent == parent)
8966
            return *iter;
8967
    }
8968
 
8969
    return mBCDummy;
8970
}
8971
 
8972
BITMAP_CACHE& TButton::getBCentryByBI(int bIdx)
8973
{
8974
    DECL_TRACER("TButton::getBCentryByBI(int bIdx)");
8975
 
8976
    if (nBitmapCache.size() == 0)
8977
        return mBCDummy;
8978
 
8979
    vector<BITMAP_CACHE>::iterator iter;
8980
 
8981
    for (iter = nBitmapCache.begin(); iter != nBitmapCache.end(); ++iter)
8982
    {
8983
        if (iter->bi == bIdx)
8984
            return *iter;
8985
    }
8986
 
369 andreas 8987
        return mBCDummy;
94 andreas 8988
}
8989
 
8990
void TButton::removeBCentry(std::vector<BITMAP_CACHE>::iterator *elem)
8991
{
8992
    DECL_TRACER("TButton::removeBCentry(std::vector<BITMAP_CACHE>::iterator *elem)");
8993
 
8994
    if (nBitmapCache.size() == 0 || !elem)
8995
        return;
8996
 
8997
    vector<BITMAP_CACHE>::iterator iter;
8998
 
8999
    for (iter = nBitmapCache.begin(); iter != nBitmapCache.end(); ++iter)
9000
    {
9001
        if (iter == *elem)
9002
        {
9003
            nBitmapCache.erase(iter);
9004
            return;
9005
        }
9006
    }
9007
}
9008
 
9009
void TButton::setReady(ulong handle)
9010
{
9011
    DECL_TRACER("TButton::setReady(ulong handle)");
9012
 
9013
    if (nBitmapCache.size() == 0)
9014
        return;
9015
 
9016
    vector<BITMAP_CACHE>::iterator iter;
9017
 
9018
    for (iter = nBitmapCache.begin(); iter != nBitmapCache.end(); ++iter)
9019
    {
9020
        if (iter->handle == handle)
9021
        {
9022
            iter->ready = true;
9023
            return;
9024
        }
9025
    }
9026
}
9027
 
97 andreas 9028
void TButton::setInvalid(ulong handle)
9029
{
9030
    DECL_TRACER("TButton::setInvalid(ulong handle)");
9031
 
9032
    if (nBitmapCache.size() == 0)
9033
        return;
9034
 
9035
    vector<BITMAP_CACHE>::iterator iter;
9036
 
9037
    for (iter = nBitmapCache.begin(); iter != nBitmapCache.end(); ++iter)
9038
    {
9039
        if (iter->handle == handle)
9040
        {
9041
            nBitmapCache.erase(iter);
9042
            return;
9043
        }
9044
    }
9045
}
9046
 
94 andreas 9047
void TButton::setBCBitmap(ulong handle, SkBitmap& bm)
9048
{
9049
    DECL_TRACER("TButton::setBCBitmap(ulong handle, SkBitmap& bm)");
9050
 
9051
    if (nBitmapCache.size() == 0)
9052
        return;
9053
 
9054
    vector<BITMAP_CACHE>::iterator iter;
9055
 
9056
    for (iter = nBitmapCache.begin(); iter != nBitmapCache.end(); ++iter)
9057
    {
9058
        if (iter->handle == handle)
9059
        {
9060
            iter->bitmap = bm;
9061
            return;
9062
        }
9063
    }
9064
}
9065
 
9066
void TButton::showBitmapCache()
9067
{
9068
    DECL_TRACER("TButton::showBitmapCache()");
9069
 
9070
    vector<BITMAP_CACHE>::iterator iter;
9071
    bool found;
9072
 
9073
    while (nBitmapCache.size() > 0)
9074
    {
9075
        found = false;
9076
 
9077
        for (iter = nBitmapCache.begin(); iter != nBitmapCache.end(); ++iter)
9078
        {
9079
            if (iter->ready)
9080
            {
9081
                if (_displayButton)
176 andreas 9082
                {
289 andreas 9083
                    TBitmap image((unsigned char *)iter->bitmap.getPixels(), iter->bitmap.info().width(), iter->bitmap.info().height());
298 andreas 9084
                    _displayButton(iter->handle, iter->parent, image, iter->width, iter->height, iter->left, iter->top, isPassThrough());
176 andreas 9085
                    mChanged = false;
9086
                }
94 andreas 9087
 
9088
                nBitmapCache.erase(iter);
9089
                found = true;
9090
                break;
9091
            }
9092
        }
9093
 
9094
        if (!found)
9095
            break;
9096
    }
9097
}
159 andreas 9098
 
9099
uint32_t TButton::pixelMix(uint32_t s, uint32_t d, uint32_t a, PMIX mix)
9100
{
9101
    DECL_TRACER("TButton::pixelMultiply(uint32_t s, uint32_t d)");
9102
 
9103
    uint32_t r = 0;
9104
 
9105
    switch(mix)
9106
    {
9107
        case PMIX_SRC:          r = s; break;                                                   // SRC
9108
        case PMIX_DST:          r = d; break;                                                   // DST
9109
        case PMIX_MULTIPLY:     r = s * (255 - (d * a)) + d * (255 - (s * a)) + s * d; break;   // Multiply
9110
        case PMIX_PLUS:         r = std::min(s + d, (uint32_t)255); break;                      // plus
9111
        case PMIX_XOR:          r = s * (255 - (d * a)) + d * (255 - (s * a)); break;           // XOr
9112
        case PMIX_DSTTOP:       r = d * (s * a) + s * (255 - (d * a)); break;                   // DstATop
9113
        case PMIX_SRCTOP:       r = s * (d * a) + d * (255 - (s * a)); break;                   // SrcATop
9114
        case PMIX_SRCOVER:      r = s + (255 - (s * a)) * d; break;                             // SrcOver
9115
        case PMIX_SCREEN:       r = s + d - s * d; break;                                       // Screen
9116
    }
9117
 
9118
    return r & 0x00ff;
9119
}
225 andreas 9120
 
298 andreas 9121
bool TButton::isPassThrough()
9122
{
9123
    DECL_TRACER("TButton::isPassThrough()");
9124
 
9125
    if (hs.empty())
9126
        return false;
9127
 
9128
    if (strCaseCompare(hs, "passThru") == 0)
9129
        return true;
9130
 
9131
    return false;
9132
}
9133
 
225 andreas 9134
bool TButton::setListSource(const string &source, const vector<string>& configs)
9135
{
9136
    DECL_TRACER("TButton::setListSource(const string &source, const vector<string>& configs)");
9137
 
9138
    TUrl url;
9139
 
227 andreas 9140
    listSourceUser.clear();
9141
    listSourcePass.clear();
9142
    listSourceCsv = false;
9143
    listSourceHasHeader = false;
9144
 
9145
    if (configs.size() > 0)
9146
    {
9147
        vector<string>::const_iterator iter;
9148
 
9149
        for (iter = configs.begin(); iter != configs.end(); ++iter)
9150
        {
9151
            size_t pos;
9152
 
9153
            if ((pos = iter->find("user=")) != string::npos)
9154
                listSourceUser = iter->substr(pos + 5);
9155
            else if ((pos = iter->find("pass=")) != string::npos)
9156
                listSourcePass = iter->substr(pos + 5);
9157
            else if (iter->find("csv=") != string::npos)
9158
            {
9159
                string str = *iter;
9160
                string low = toLower(str);
9161
 
9162
                if (low.find("true") != string::npos || low.find("1") != string::npos)
9163
                    listSourceCsv = true;
9164
            }
9165
            else if (iter->find("has_header=") != string::npos)
9166
            {
9167
                string str = *iter;
9168
                string low = toLower(str);
9169
 
9170
                if (low.find("true") != string::npos || low.find("1") != string::npos)
9171
                    listSourceHasHeader = true;
9172
            }
9173
        }
9174
    }
9175
 
225 andreas 9176
    if (!url.setUrl(source))    // Dynamic source?
9177
    {
9178
        size_t idx = 0;
9179
 
9180
        if (!gPrjResources)
9181
            return false;
9182
 
9183
        if ((idx = gPrjResources->getResourceIndex("image")) == TPrjResources::npos)
9184
        {
9185
            MSG_ERROR("There exists no image resource!");
9186
            return false;
9187
        }
9188
 
9189
        RESOURCE_T resource = gPrjResources->findResource(idx, source);
9190
 
9191
        if (resource.protocol.empty())
9192
        {
9193
            MSG_WARNING("Resource " << source << " not found!");
9194
            return false;
9195
        }
9196
 
9197
        listSource = resource.protocol + "://";
9198
 
227 andreas 9199
        if (!resource.user.empty() || !listSourceUser.empty())
225 andreas 9200
        {
227 andreas 9201
            listSource += ((listSourceUser.empty() == false) ? listSourceUser : resource.user);
225 andreas 9202
 
227 andreas 9203
            if ((!resource.password.empty() && !resource.encrypted) || !listSourcePass.empty())
9204
                listSource += ":" + ((listSourcePass.empty() == false) ? listSourcePass : resource.password);
225 andreas 9205
 
9206
            listSource += "@";
9207
        }
9208
 
9209
        listSource += resource.host;
9210
 
9211
        if (!resource.path.empty())
9212
            listSource += "/" + resource.path;
9213
 
9214
        if (!resource.file.empty())
9215
            listSource += "/" + resource.file;
9216
 
9217
        return true;
9218
    }
9219
 
9220
    listSource = source;
9221
    return true;
9222
}
227 andreas 9223
 
9224
bool TButton::setListSourceFilter(const string& filter)
9225
{
9226
    DECL_TRACER("TButton::setListSourceFilter(const string& filter)");
9227
 
9228
    if (filter.empty())
9229
        return false;
9230
 
230 andreas 9231
    listFilter = filter;
9232
    MSG_DEBUG("listSourceFilter: " << listFilter);
227 andreas 9233
    return true;
9234
}
233 andreas 9235
 
9236
void TButton::setListViewColumns(int cols)
9237
{
9238
    DECL_TRACER("TButton::setListViewColumns(int cols)");
9239
 
9240
    if (cols <= 0)
9241
        return;
9242
 
9243
    tc = cols;
9244
}
9245
 
9246
void TButton::setListViewLayout(int layout)
9247
{
9248
    DECL_TRACER("TButton::setListViewLayout(int layout)");
9249
 
9250
    if (layout < 1 || layout > 6)
9251
        return;
9252
 
9253
    listLayout = layout;
9254
}
9255
 
9256
void TButton::setListViewComponent(int comp)
9257
{
9258
    DECL_TRACER("TButton::setListViewComponent(int comp)");
9259
 
9260
    if (comp < 0 || comp > 7)
9261
        return;
9262
 
9263
    listComponent = comp;
9264
}
9265
 
9266
void TButton::setListViewCellheight(int height, bool percent)
9267
{
9268
    DECL_TRACER("TButton::setListViewCellheight(int height, bool percent)");
9269
 
9270
    int minHeight = ht / tr;    // Total height / number of rows
9271
    int maxHeight = (int)((double)ht / 100.0 * 95.0);
9272
 
9273
    if (!percent && (height < minHeight || height > maxHeight))
9274
        return;
9275
 
9276
    if (percent)
9277
    {
9278
        int h = (int)((double)ht / 100.0 * (double)height);
9279
 
9280
        if (h >= minHeight && h <= maxHeight)
9281
            tj = h;
9282
 
9283
        return;
9284
    }
9285
 
9286
    tj = height;
9287
}
9288
 
9289
void TButton::setListViewFilterHeight(int height, bool percent)
9290
{
9291
    DECL_TRACER("TButton::setListViewFilterHeight(int height, bool percent)");
9292
 
9293
    if (percent && (height < 5 || height > 25))
9294
        return;
9295
 
9296
    if (!percent && height < 24)
9297
        return;
9298
 
9299
    if (percent)
9300
    {
9301
        listViewColFilterHeight = (int)((double)ht / 100.0 * (double)height);
9302
        return;
9303
    }
9304
    else
9305
    {
9306
        int maxHeight = (int)((double)ht / 100.0 * 25.0);
9307
 
9308
        if (height < maxHeight)
9309
            listViewColFilterHeight = height;
9310
    }
9311
}
9312
 
9313
void TButton::setListViewP1(int p1)
9314
{
9315
    DECL_TRACER("TButton::setListViewP1(int p1)");
9316
 
9317
    if (p1 < 10 || p1 > 90)
9318
        return;
9319
 
9320
    listViewP1 = p1;
9321
}
9322
 
9323
void TButton::setListViewP2(int p2)
9324
{
9325
    DECL_TRACER("TButton::setListViewP2(int p2)");
9326
 
9327
    if (p2 < 10 || p2 > 90)
9328
        return;
9329
 
9330
    listViewP2 = p2;
9331
}
9332
 
9333
void TButton::listViewNavigate(const string &command, bool select)
9334
{
9335
    DECL_TRACER("TButton::listViewNavigate(const string &command, bool select)");
9336
 
9337
    string cmd = command;
9338
    string upCmd = toUpper(cmd);
9339
 
9340
    if (upCmd != "T" && upCmd != "B" && upCmd != "D" && upCmd != "U" && !isNumeric(upCmd, true))
9341
        return;
9342
 
9343
    // TODO: Add code to navigate a list
9344
    MSG_WARNING("ListView navigation is not supported!" << " [" << upCmd << ", " << (select ? "TRUE" : "FALSE") << "]");
9345
}
9346
 
9347
void TButton::listViewRefresh(int interval, bool force)
9348
{
9349
    DECL_TRACER("TButton::listViewRefresh(int interval, bool force)");
9350
 
238 andreas 9351
    Q_UNUSED(interval);
9352
    Q_UNUSED(force);
9353
 
233 andreas 9354
    // TODO: Add code to load list data and display / refresh them
9355
}
9356
 
9357
void TButton::listViewSortData(const vector<string> &columns, LIST_SORT order, const string &override)
9358
{
9359
    DECL_TRACER("TButton::listViewSortData(const vector<string> &columns, LIST_SORT order, const string &override)");
9360
 
238 andreas 9361
    Q_UNUSED(columns);
9362
    Q_UNUSED(order);
9363
    Q_UNUSED(override);
9364
 
233 andreas 9365
    // TODO: Insert code to sort the data in the list
9366
}
279 andreas 9367