Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 andreas 1
/*
258 andreas 2
 * Copyright (C) 2020 to 2023 by Andreas Theofilu <andreas@theosys.at>
3 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
 */
18
 
5 andreas 19
#include <exception>
20
 
54 andreas 21
#include <include/core/SkFont.h>
22
#include <include/core/SkFontMetrics.h>
23
#include <include/core/SkTextBlob.h>
24
 
6 andreas 25
#include "tresources.h"
76 andreas 26
#include "texpat++.h"
26 andreas 27
#include "tpagemanager.h"
3 andreas 28
#include "tsubpage.h"
65 andreas 29
#include "tdrawimage.h"
3 andreas 30
#include "tconfig.h"
31
#include "terror.h"
343 andreas 32
#if TESTMODE == 1
33
#include "testmode.h"
34
#endif
3 andreas 35
 
186 andreas 36
#if __cplusplus < 201402L
37
#   error "This module requires at least C++14 standard!"
38
#else
39
#   if __cplusplus < 201703L
40
#       include <experimental/filesystem>
41
namespace fs = std::experimental::filesystem;
42
#       warning "Support for C++14 and experimental filesystem will be removed in a future version!"
43
#   else
44
#       include <filesystem>
45
#       ifdef __ANDROID__
46
namespace fs = std::__fs::filesystem;
47
#       else
48
namespace fs = std::filesystem;
49
#       endif
50
#   endif
51
#endif
52
 
3 andreas 53
using std::string;
6 andreas 54
using std::vector;
3 andreas 55
using namespace Button;
76 andreas 56
using namespace Expat;
3 andreas 57
 
26 andreas 58
extern TPageManager *gPageManager;
59
 
3 andreas 60
TSubPage::TSubPage(const string& name)
61
    : mFile(name)
62
{
63
    DECL_TRACER("TSubPage::TSubPage(const string& path)");
14 andreas 64
    TError::clear();
3 andreas 65
 
197 andreas 66
    string projectPath = ((gPageManager && gPageManager->isSetupActive()) ? TConfig::getSystemProjectPath() : TConfig::getProjectPath());
186 andreas 67
 
68
    if (!fs::exists(projectPath + "/prj.xma"))
197 andreas 69
    {
70
        MSG_ERROR("Directory " << projectPath << " doesn't exist!");
71
        return;
72
    }
186 andreas 73
 
74
    string path = makeFileName(projectPath, name);
75
 
3 andreas 76
    if (isValidFile())
77
        mFName = getFileName();
78
    else
79
    {
186 andreas 80
        MSG_ERROR("Either the path \"" << projectPath << "\" or the file name \"" << name << "\" is invalid!");
3 andreas 81
        TError::setError();
82
        return;
83
    }
84
 
38 andreas 85
    if (gPageManager)
86
    {
87
        if (!_displayButton)
88
            _displayButton = gPageManager->getCallbackDB();
89
 
90
        if (!_setBackground)
91
            _setBackground = gPageManager->getCallbackBG();
92
 
93
        if (!_playVideo)
94
            _playVideo = gPageManager->getCallbackPV();
95
 
96
        if (!_callDropSubPage)
97
            _callDropSubPage = gPageManager->getCallDropSubPage();
98
    }
99
 
3 andreas 100
    initialize();
101
}
102
 
103
TSubPage::~TSubPage()
104
{
105
    DECL_TRACER("TSubPage::~TSubPage()");
106
 
5 andreas 107
    if (mSubpage.name.empty())
108
    {
109
        MSG_WARNING("Invalid page found!");
110
        return;
111
    }
112
 
113
    MSG_DEBUG("Destroing subpage " << mSubpage.pageID << ": " << mSubpage.name);
114
 
201 andreas 115
    BUTTONS_T *b = TPageInterface::getButtons();
3 andreas 116
    BUTTONS_T *next = nullptr;
117
 
118
    while (b)
119
    {
120
        next = b->next;
5 andreas 121
 
122
        if (b->button)
296 andreas 123
        {
124
            // If this is a system button, we must remove it first from the list
125
            dropButton(b->button);
5 andreas 126
            delete b->button;
296 andreas 127
        }
5 andreas 128
 
3 andreas 129
        delete b;
130
        b = next;
131
    }
132
 
201 andreas 133
    setButtons(nullptr);
3 andreas 134
}
135
 
136
void TSubPage::initialize()
137
{
138
    DECL_TRACER("TSubPage::initialize()");
139
 
140
    if (mFName.empty())
141
        return;
142
 
143
    TError::clear();
76 andreas 144
    TExpat xml(mFName);
145
    xml.setEncoding(ENC_CP1250);
3 andreas 146
 
76 andreas 147
    if (!xml.parse())
3 andreas 148
        return;
149
 
76 andreas 150
    int depth = 0;
151
    size_t index = 0;
152
    size_t oldIndex = 0;
153
    string ename, content;
154
    vector<ATTRIBUTE_t> attrs;
3 andreas 155
 
76 andreas 156
    if ((index = xml.getElementIndex("page", &depth)) == TExpat::npos)
3 andreas 157
    {
158
        MSG_ERROR("Element \"page\" with attribute \"type\" was not found! Invalid XML file!");
159
        TError::setError();
160
        return;
161
    }
162
 
76 andreas 163
    attrs = xml.getAttributes();
164
    string stype = xml.getAttribute("type", attrs);
165
 
166
    if (stype.compare("subpage") != 0)
3 andreas 167
    {
76 andreas 168
        MSG_ERROR("The type " << stype << " is invalid for a subpage!");
3 andreas 169
        TError::setError();
170
        return;
171
    }
172
 
193 andreas 173
    mSubpage.popupType = xml.getAttribute("popupType", attrs);  // popup or subpage
3 andreas 174
 
76 andreas 175
    while ((index = xml.getNextElementFromIndex(index, &ename, &content, &attrs)) != TExpat::npos)
3 andreas 176
    {
177
        if (ename.compare("pageID") == 0)
76 andreas 178
            mSubpage.pageID = xml.convertElementToInt(content);
3 andreas 179
        else if (ename.compare("name") == 0)
76 andreas 180
            mSubpage.name = content;
7 andreas 181
        else if (ename.compare("left") == 0)
76 andreas 182
            mSubpage.left = xml.convertElementToInt(content);
7 andreas 183
        else if (ename.compare("top") == 0)
76 andreas 184
            mSubpage.top = xml.convertElementToInt(content);
3 andreas 185
        else if (ename.compare("width") == 0)
76 andreas 186
            mSubpage.width = xml.convertElementToInt(content);
3 andreas 187
        else if (ename.compare("height") == 0)
76 andreas 188
            mSubpage.height = xml.convertElementToInt(content);
3 andreas 189
        else if (ename.compare("group") == 0)
76 andreas 190
            mSubpage.group = content;
7 andreas 191
        else if (ename.compare("showEffect") == 0)
76 andreas 192
            mSubpage.showEffect = (SHOWEFFECT)xml.convertElementToInt(content);
3 andreas 193
        else if (ename.compare("showTime") == 0)
76 andreas 194
            mSubpage.showTime = xml.convertElementToInt(content);
3 andreas 195
        else if (ename.compare("hideTime") == 0)
76 andreas 196
            mSubpage.hideTime = xml.convertElementToInt(content);
7 andreas 197
        else if (ename.compare("hideEffect") == 0)
76 andreas 198
            mSubpage.hideEffect = (SHOWEFFECT)xml.convertElementToInt(content);
3 andreas 199
        else if (ename.compare("timeout") == 0)
76 andreas 200
            mSubpage.timeout = xml.convertElementToInt(content);
3 andreas 201
        else if (ename.compare("button") == 0)      // Read a button
202
        {
203
            try
204
            {
205
                TButton *button = new TButton();
204 andreas 206
                TPageInterface::registerListCallback<TSubPage>(button, this);
4 andreas 207
                button->setPalette(mPalette);
201 andreas 208
                button->setFonts(getFonts());
76 andreas 209
                index = button->initialize(&xml, index);
6 andreas 210
                button->setParentSize(mSubpage.width, mSubpage.height);
7 andreas 211
                button->registerCallback(_displayButton);
21 andreas 212
                button->regCallPlayVideo(_playVideo);
3 andreas 213
 
214
                if (TError::isError())
215
                {
16 andreas 216
                    MSG_ERROR("Dropping button because of previous errors!");
3 andreas 217
                    delete button;
218
                    return;
219
                }
220
 
5 andreas 221
                button->setHandle(((mSubpage.pageID << 16) & 0xffff0000) | button->getButtonIndex());
14 andreas 222
                button->createButtons();
3 andreas 223
                addButton(button);
76 andreas 224
                index++;    // Jump over the end tag of the button.
3 andreas 225
            }
226
            catch (std::exception& e)
227
            {
228
                MSG_ERROR("Memory exception: " << e.what());
229
                TError::setError();
230
                return;
231
            }
232
        }
6 andreas 233
        else if (ename.compare("sr") == 0)
234
        {
235
            SR_T sr;
76 andreas 236
            sr.number = xml.getAttributeInt("number", attrs);
3 andreas 237
 
76 andreas 238
            while ((index = xml.getNextElementFromIndex(index, &ename, &content, &attrs)) != TExpat::npos)
6 andreas 239
            {
240
                if (ename.compare("bs") == 0)
76 andreas 241
                    sr.bs = content;
6 andreas 242
                else if (ename.compare("cb") == 0)
76 andreas 243
                    sr.cb = content;
6 andreas 244
                else if (ename.compare("cf") == 0)
76 andreas 245
                    sr.cf = content;
6 andreas 246
                else if (ename.compare("ct") == 0)
76 andreas 247
                    sr.ct = content;
6 andreas 248
                else if (ename.compare("ec") == 0)
76 andreas 249
                    sr.ec = content;
6 andreas 250
                else if (ename.compare("bm") == 0)
76 andreas 251
                    sr.bm = content;
65 andreas 252
                else if (ename.compare("mi") == 0)
76 andreas 253
                    sr.mi = content;
10 andreas 254
                else if (ename.compare("ji") == 0)
76 andreas 255
                    sr.ji = xml.convertElementToInt(content);
10 andreas 256
                else if (ename.compare("jb") == 0)
76 andreas 257
                    sr.jb = xml.convertElementToInt(content);
6 andreas 258
                else if (ename.compare("fi") == 0)
76 andreas 259
                    sr.fi = xml.convertElementToInt(content);
10 andreas 260
                else if (ename.compare("ii") == 0)
76 andreas 261
                    sr.ii = xml.convertElementToInt(content);
10 andreas 262
                else if (ename.compare("ix") == 0)
76 andreas 263
                    sr.ix = xml.convertElementToInt(content);
10 andreas 264
                else if (ename.compare("iy") == 0)
76 andreas 265
                    sr.iy = xml.convertElementToInt(content);
10 andreas 266
                else if (ename.compare("oo") == 0)
76 andreas 267
                    sr.oo = xml.convertElementToInt(content);
54 andreas 268
                else if (ename.compare("te") == 0)
76 andreas 269
                    sr.te = content;
54 andreas 270
                else if (ename.compare("tx") == 0)
76 andreas 271
                    sr.tx = xml.convertElementToInt(content);
54 andreas 272
                else if (ename.compare("ty") == 0)
76 andreas 273
                    sr.ty = xml.convertElementToInt(content);
54 andreas 274
                else if (ename.compare("et") == 0)
76 andreas 275
                    sr.et = xml.convertElementToInt(content);
54 andreas 276
                else if (ename.compare("ww") == 0)
76 andreas 277
                    sr.ww = xml.convertElementToInt(content);
54 andreas 278
                else if (ename.compare("jt") == 0)
76 andreas 279
                    sr.jt = (Button::TEXT_ORIENTATION)xml.convertElementToInt(content);
6 andreas 280
 
76 andreas 281
                oldIndex = index;
6 andreas 282
            }
283
 
284
            mSubpage.sr.push_back(sr);
285
        }
286
 
76 andreas 287
        if (index == TExpat::npos)
288
            index = oldIndex + 1;
3 andreas 289
    }
8 andreas 290
 
204 andreas 291
    setSR(mSubpage.sr);
292
/*
76 andreas 293
    if (TStreamError::checkFilter(HLOG_DEBUG))
294
    {
295
        MSG_DEBUG("PageID: " << mSubpage.pageID);
296
        MSG_DEBUG("Name  : " << mSubpage.name);
297
        MSG_DEBUG("Left  : " << mSubpage.left);
298
        MSG_DEBUG("Top   : " << mSubpage.top);
299
        MSG_DEBUG("Width : " << mSubpage.width);
300
        MSG_DEBUG("Height: " << mSubpage.height);
301
 
302
        vector<SR_T>::iterator iter;
303
        size_t pos = 1;
304
 
305
        for (iter = mSubpage.sr.begin(); iter != mSubpage.sr.end(); ++iter)
306
        {
307
            MSG_DEBUG("   " << pos << ": id: " << iter->number);
308
            MSG_DEBUG("   " << pos << ": bs: " << iter->bs);
309
            MSG_DEBUG("   " << pos << ": cb: " << iter->cb);
310
            MSG_DEBUG("   " << pos << ": cf: " << iter->cf);
311
            MSG_DEBUG("   " << pos << ": ct: " << iter->ct);
312
            MSG_DEBUG("   " << pos << ": ec: " << iter->ec);
313
            MSG_DEBUG("   " << pos << ": bm: " << iter->bm);
314
            MSG_DEBUG("   " << pos << ": mi: " << iter->mi);
315
            MSG_DEBUG("   " << pos << ": fi: " << iter->fi);
316
            pos++;
317
        }
318
    }
204 andreas 319
*/
8 andreas 320
    // Here the sort function could be called. But it's not necessary because
321
    // the buttons are stored in ascending Z order. Therefor the following
322
    // method call is commented out.
323
    // sortButtons();
6 andreas 324
}
3 andreas 325
 
6 andreas 326
void TSubPage::show()
327
{
328
    DECL_TRACER("TSubPage::show()");
3 andreas 329
 
6 andreas 330
    if (!_setBackground)
3 andreas 331
    {
66 andreas 332
        if (gPageManager && gPageManager->getCallbackBG())
333
            _setBackground = gPageManager->getCallbackBG();
334
        else
335
        {
336
            MSG_WARNING("No callback \"setBackground\" was set!");
343 andreas 337
#if TESTMODE == 1
338
            setScreenDone();
339
#endif
66 andreas 340
            return;
341
        }
3 andreas 342
    }
343
 
65 andreas 344
    bool haveImage = false;
6 andreas 345
    ulong handle = (mSubpage.pageID << 16) & 0xffff0000;
53 andreas 346
    MSG_DEBUG("Processing subpage " << mSubpage.pageID << ": " << mSubpage.name);
66 andreas 347
    SkBitmap target;
254 andreas 348
 
349
    if (!allocPixels(mSubpage.width, mSubpage.height, &target))
343 andreas 350
    {
351
#if TESTMODE == 1
352
        setScreenDone();
353
#endif
254 andreas 354
        return;
343 andreas 355
    }
254 andreas 356
 
66 andreas 357
    target.eraseColor(TColor::getSkiaColor(mSubpage.sr[0].cf));
6 andreas 358
    // Draw the background, if any
65 andreas 359
    if (mSubpage.sr.size() > 0 && (!mSubpage.sr[0].bm.empty() || !mSubpage.sr[0].mi.empty()))
3 andreas 360
    {
65 andreas 361
        TDrawImage dImage;
66 andreas 362
        dImage.setWidth(mSubpage.width);
363
        dImage.setHeight(mSubpage.height);
3 andreas 364
 
65 andreas 365
        if (!mSubpage.sr[0].bm.empty())
3 andreas 366
        {
65 andreas 367
            MSG_DEBUG("Loading image " << mSubpage.sr[0].bm);
368
            sk_sp<SkData> rawImage = readImage(mSubpage.sr[0].bm);
6 andreas 369
            SkBitmap bm;
19 andreas 370
 
65 andreas 371
            if (rawImage)
372
            {
373
                MSG_DEBUG("Decoding image BM ...");
19 andreas 374
 
65 andreas 375
                if (!DecodeDataToBitmap(rawImage, &bm))
376
                {
377
                    MSG_WARNING("Problem while decoding image " << mSubpage.sr[0].bm);
378
                }
379
                else if (!bm.empty())
380
                {
381
                    dImage.setImageBm(bm);
382
                    SkImageInfo info = bm.info();
383
                    mSubpage.sr[0].bm_width = info.width();
384
                    mSubpage.sr[0].bm_height = info.height();
385
                    haveImage = true;
386
                }
387
                else
388
                {
389
                    MSG_WARNING("BM image " << mSubpage.sr[0].bm << " seems to be empty!");
390
                }
391
            }
392
        }
54 andreas 393
 
65 andreas 394
        if (!mSubpage.sr[0].mi.empty())
395
        {
396
            MSG_DEBUG("Loading image " << mSubpage.sr[0].mi);
397
            sk_sp<SkData> rawImage = readImage(mSubpage.sr[0].mi);
398
            SkBitmap mi;
399
 
400
            if (rawImage)
401
            {
402
                MSG_DEBUG("Decoding image MI ...");
403
 
404
                if (!DecodeDataToBitmap(rawImage, &mi))
405
                {
406
                    MSG_WARNING("Problem while decoding image " << mSubpage.sr[0].mi);
407
                }
408
                else if (!mi.empty())
409
                {
410
                    dImage.setImageMi(mi);
411
                    SkImageInfo info = mi.info();
412
                    mSubpage.sr[0].mi_width = info.width();
413
                    mSubpage.sr[0].mi_height = info.height();
414
                    haveImage = true;
415
                }
416
                else
417
                {
418
                    MSG_WARNING("MI image " << mSubpage.sr[0].mi << " seems to be empty!");
419
                }
420
            }
421
        }
422
 
423
        if (haveImage)
424
        {
425
            dImage.setSr(mSubpage.sr);
426
 
427
            if (!dImage.drawImage(&target))
428
                return;
429
 
54 andreas 430
            if (!mSubpage.sr[0].te.empty())
431
            {
201 andreas 432
                if (!drawText(mSubpage, &target))
54 andreas 433
                    return;
434
            }
262 andreas 435
#ifdef _OPAQUE_SKIA_
258 andreas 436
            if (mSubpage.sr[0].oo < 255 && mSubpage.sr[0].te.empty() && mSubpage.sr[0].bs.empty())
437
                setOpacity(&target, mSubpage.sr[0].oo);
262 andreas 438
#endif
65 andreas 439
 
43 andreas 440
#ifdef _SCALE_SKIA_
26 andreas 441
            if (gPageManager && gPageManager->getScaleFactor() != 1.0)
442
            {
443
                SkPaint paint;
65 andreas 444
                int left, top;
289 andreas 445
                SkImageInfo info = target.info();
65 andreas 446
 
26 andreas 447
                paint.setBlendMode(SkBlendMode::kSrc);
448
                paint.setFilterQuality(kHigh_SkFilterQuality);
28 andreas 449
                // Calculate new dimension
31 andreas 450
                double scaleFactor = gPageManager->getScaleFactor();
451
                MSG_DEBUG("Using scale factor " << scaleFactor);
452
                int lwidth = (int)((double)info.width() * scaleFactor);
453
                int lheight = (int)((double)info.height() * scaleFactor);
454
                int twidth = (int)((double)mSubpage.width * scaleFactor);
455
                int theight = (int)((double)mSubpage.height * scaleFactor);
65 andreas 456
                calcPosition(lwidth, lheight, &left, &top);
28 andreas 457
                // Create a canvas and draw new image
65 andreas 458
                sk_sp<SkImage> im = SkImage::MakeFromBitmap(target);
254 andreas 459
 
460
                if (!allocPixels(twidth, theight, &target))
343 andreas 461
                {
462
#if TESTMODE == 1
463
                    setScreenDone();
464
#endif
254 andreas 465
                    return;
343 andreas 466
                }
254 andreas 467
 
65 andreas 468
                target.eraseColor(TColor::getSkiaColor(mSubpage.sr[0].cf));
254 andreas 469
                SkCanvas can(target, SkSurfaceProps());
31 andreas 470
                SkRect rect = SkRect::MakeXYWH(left, top, lwidth, lheight);
26 andreas 471
                can.drawImageRect(im, rect, &paint);
31 andreas 472
                MSG_DEBUG("Scaled size of background image: " << left << ", " << top << ", " << lwidth << ", " << lheight);
26 andreas 473
            }
43 andreas 474
#endif
262 andreas 475
#ifdef _OPAQUE_SKIA_
258 andreas 476
            if (mSubpage.sr[0].te.empty() && mSubpage.sr[0].bs.empty())
289 andreas 477
            {
478
                TBitmap image((unsigned char *)target.getPixels(), target.info().width(), target.info().height());
479
                _setBackground(handle, image, target.info().width(), target.info().height(), TColor::getColor(mSubpage.sr[0].cf));
480
            }
262 andreas 481
#else
482
            if (mSubpage.sr[0].te.empty() && mSubpage.sr[0].bs.empty())
289 andreas 483
            {
484
                TBitmap image((unsigned char *)target.getPixels(), target.info().width(), target.info().height());
485
                _setBackground(handle, image, target.info().width(), target.info().height(), TColor::getColor(mSubpage.sr[0].cf), mSubpage.sr[0].oo);
486
            }
262 andreas 487
#endif
6 andreas 488
        }
489
    }
66 andreas 490
 
491
    if (mSubpage.sr.size() > 0 && !mSubpage.sr[0].te.empty())
54 andreas 492
    {
493
        MSG_DEBUG("Drawing a text only on background image ...");
494
 
258 andreas 495
        if (drawText(mSubpage, &target))
496
            haveImage = true;
497
    }
54 andreas 498
 
258 andreas 499
    // Check for a frame and draw it if there is one.
500
    if (!mSubpage.sr[0].bs.empty())
501
    {
502
        if (drawFrame(mSubpage, &target))
503
            haveImage = true;
504
    }
505
 
506
    if (haveImage)
507
    {
262 andreas 508
#ifdef _OPAQUE_SKIA_
258 andreas 509
        if (mSubpage.sr[0].oo < 255)
510
            setOpacity(&target, mSubpage.sr[0].oo);
262 andreas 511
#endif
289 andreas 512
        TBitmap image((unsigned char *)target.getPixels(), target.info().width(), target.info().height());
262 andreas 513
#ifdef _OPAQUE_SKIA_
289 andreas 514
        _setBackground(handle, image, target.info().width(), target.info().height(), TColor::getColor(mSubpage.sr[0].cf));
262 andreas 515
#else
289 andreas 516
        _setBackground(handle, image, target.info().width(), target.info().height(), TColor::getColor(mSubpage.sr[0].cf), mSubpage.sr[0].oo);
262 andreas 517
#endif
54 andreas 518
    }
258 andreas 519
    else if (mSubpage.sr.size() > 0 && !haveImage)
6 andreas 520
    {
521
        MSG_DEBUG("Calling \"setBackground\" with no image ...");
262 andreas 522
#ifdef _OPAQUE_SKIA_
289 andreas 523
        _setBackground(handle, TBitmap(), 0, 0, TColor::getColor(mSubpage.sr[0].cf));
262 andreas 524
#else
289 andreas 525
        _setBackground(handle, TBitmap(), 0, 0, TColor::getColor(mSubpage.sr[0].cf), mSubpage.sr[0].oo);
262 andreas 526
#endif
6 andreas 527
    }
3 andreas 528
 
6 andreas 529
    // Draw the buttons
201 andreas 530
    BUTTONS_T *button = TPageInterface::getButtons();
6 andreas 531
 
532
    while (button)
533
    {
534
        if (button->button)
535
        {
344 andreas 536
            MSG_DEBUG("Drawing button " << handleToString(button->button->getHandle()) << ": " << button->button->getButtonIndex() << " --> " << button->button->getButtonName());
204 andreas 537
            TPageInterface::registerListCallback<TSubPage>(button->button, this);
6 andreas 538
            button->button->registerCallback(_displayButton);
21 andreas 539
            button->button->regCallPlayVideo(_playVideo);
201 andreas 540
            button->button->setFonts(getFonts());
7 andreas 541
            button->button->setPalette(mPalette);
6 andreas 542
            button->button->createButtons();
10 andreas 543
 
544
            if (mSubpage.sr.size() > 0)
545
                button->button->setGlobalOpacity(mSubpage.sr[0].oo);
546
 
345 andreas 547
//            button->button->setChanged(true);
6 andreas 548
            button->button->show();
549
        }
550
 
551
        button = button->next;
3 andreas 552
    }
11 andreas 553
 
554
    // Mark page as visible
555
    mVisible = true;
271 andreas 556
 
557
    if (gPageManager && gPageManager->getPageFinished())
558
        gPageManager->getPageFinished()(handle);
3 andreas 559
}
560
 
289 andreas 561
SkBitmap& TSubPage::getBgImage()
281 andreas 562
{
563
    DECL_TRACER("TSubPage::getBgImage()");
564
 
289 andreas 565
    if (!mBgImage.empty())
566
        return mBgImage;
567
 
281 andreas 568
    bool haveImage = false;
569
    MSG_DEBUG("Creating image for subpage " << mSubpage.pageID << ": " << mSubpage.name);
570
    SkBitmap target;
571
 
572
    if (!allocPixels(mSubpage.width, mSubpage.height, &target))
289 andreas 573
        return mBgImage;
281 andreas 574
 
575
    target.eraseColor(TColor::getSkiaColor(mSubpage.sr[0].cf));
576
    // Draw the background, if any
577
    if (mSubpage.sr.size() > 0 && (!mSubpage.sr[0].bm.empty() || !mSubpage.sr[0].mi.empty()))
578
    {
579
        TDrawImage dImage;
580
        dImage.setWidth(mSubpage.width);
581
        dImage.setHeight(mSubpage.height);
582
 
583
        if (!mSubpage.sr[0].bm.empty())
584
        {
585
            MSG_DEBUG("Loading image " << mSubpage.sr[0].bm);
586
            sk_sp<SkData> rawImage = readImage(mSubpage.sr[0].bm);
587
            SkBitmap bm;
588
 
589
            if (rawImage)
590
            {
591
                MSG_DEBUG("Decoding image BM ...");
592
 
593
                if (!DecodeDataToBitmap(rawImage, &bm))
594
                {
595
                    MSG_WARNING("Problem while decoding image " << mSubpage.sr[0].bm);
596
                }
597
                else if (!bm.empty())
598
                {
599
                    dImage.setImageBm(bm);
600
                    SkImageInfo info = bm.info();
601
                    mSubpage.sr[0].bm_width = info.width();
602
                    mSubpage.sr[0].bm_height = info.height();
603
                    haveImage = true;
604
                }
605
                else
606
                {
607
                    MSG_WARNING("BM image " << mSubpage.sr[0].bm << " seems to be empty!");
608
                }
609
            }
610
        }
611
 
612
        if (!mSubpage.sr[0].mi.empty())
613
        {
614
            MSG_DEBUG("Loading image " << mSubpage.sr[0].mi);
615
            sk_sp<SkData> rawImage = readImage(mSubpage.sr[0].mi);
616
            SkBitmap mi;
617
 
618
            if (rawImage)
619
            {
620
                MSG_DEBUG("Decoding image MI ...");
621
 
622
                if (!DecodeDataToBitmap(rawImage, &mi))
623
                {
624
                    MSG_WARNING("Problem while decoding image " << mSubpage.sr[0].mi);
625
                }
626
                else if (!mi.empty())
627
                {
628
                    dImage.setImageMi(mi);
629
                    SkImageInfo info = mi.info();
630
                    mSubpage.sr[0].mi_width = info.width();
631
                    mSubpage.sr[0].mi_height = info.height();
632
                    haveImage = true;
633
                }
634
                else
635
                {
636
                    MSG_WARNING("MI image " << mSubpage.sr[0].mi << " seems to be empty!");
637
                }
638
            }
639
        }
640
 
641
        if (haveImage)
642
        {
643
            dImage.setSr(mSubpage.sr);
644
 
645
            if (!dImage.drawImage(&target))
289 andreas 646
                return mBgImage;
281 andreas 647
 
648
            if (!mSubpage.sr[0].te.empty())
649
            {
650
                if (!drawText(mSubpage, &target))
289 andreas 651
                    return mBgImage;
281 andreas 652
            }
653
 
654
            if (mSubpage.sr[0].oo < 255 && mSubpage.sr[0].te.empty() && mSubpage.sr[0].bs.empty())
655
                setOpacity(&target, mSubpage.sr[0].oo);
656
 
657
#ifdef _SCALE_SKIA_
658
            size_t rowBytes = info.minRowBytes();
659
            size_t size = info.computeByteSize(rowBytes);
289 andreas 660
            SkImageInfo info = target.info();
281 andreas 661
 
662
            if (gPageManager && gPageManager->getScaleFactor() != 1.0)
663
            {
664
                SkPaint paint;
665
                int left, top;
666
 
667
                paint.setBlendMode(SkBlendMode::kSrc);
668
                paint.setFilterQuality(kHigh_SkFilterQuality);
669
                // Calculate new dimension
670
                double scaleFactor = gPageManager->getScaleFactor();
671
                MSG_DEBUG("Using scale factor " << scaleFactor);
672
                int lwidth = (int)((double)info.width() * scaleFactor);
673
                int lheight = (int)((double)info.height() * scaleFactor);
674
                int twidth = (int)((double)mSubpage.width * scaleFactor);
675
                int theight = (int)((double)mSubpage.height * scaleFactor);
676
                calcPosition(lwidth, lheight, &left, &top);
677
                // Create a canvas and draw new image
678
                sk_sp<SkImage> im = SkImage::MakeFromBitmap(target);
679
 
680
                if (!allocPixels(twidth, theight, &target))
681
                    return;
682
 
683
                target.eraseColor(TColor::getSkiaColor(mSubpage.sr[0].cf));
684
                SkCanvas can(target, SkSurfaceProps());
685
                SkRect rect = SkRect::MakeXYWH(left, top, lwidth, lheight);
686
                can.drawImageRect(im, rect, &paint);
687
                MSG_DEBUG("Scaled size of background image: " << left << ", " << top << ", " << lwidth << ", " << lheight);
688
            }
689
#endif
690
        }
691
    }
692
 
693
    if (mSubpage.sr.size() > 0 && !mSubpage.sr[0].te.empty())
694
    {
695
        MSG_DEBUG("Drawing a text only on background image ...");
696
 
697
        if (drawText(mSubpage, &target))
698
            haveImage = true;
699
    }
700
 
701
    // Check for a frame and draw it if there is one.
702
    if (!mSubpage.sr[0].bs.empty())
703
    {
306 andreas 704
        if (!drawBorder(&target, mSubpage.sr[0].bs, mSubpage.width, mSubpage.height, mSubpage.sr[0].cb))
705
        {
706
            if (drawFrame(mSubpage, &target))
707
                haveImage = true;
708
        }
281 andreas 709
    }
710
 
711
    if (haveImage)
712
    {
713
        if (mSubpage.sr[0].oo < 255)
714
            setOpacity(&target, mSubpage.sr[0].oo);
715
 
289 andreas 716
        mBgImage = target;
281 andreas 717
    }
718
 
289 andreas 719
    return mBgImage;
281 andreas 720
}
721
 
11 andreas 722
void TSubPage::drop()
723
{
724
    DECL_TRACER("TSubPage::drop()");
725
 
295 andreas 726
    stopTimer();
727
 
14 andreas 728
    if (mVisible && _callDropSubPage)
350 andreas 729
        _callDropSubPage((mSubpage.pageID << 16) & 0xffff0000, mParent);
349 andreas 730
#if TESTMODE == 1
731
    else
732
    {
733
        __success = true;
734
        setScreenDone();
735
    }
736
#endif
11 andreas 737
 
15 andreas 738
    // Set all elements of subpage invisible
201 andreas 739
    BUTTONS_T *bt = TPageInterface::getButtons();
15 andreas 740
 
741
    while (bt)
742
    {
743
        bt->button->hide();
744
        bt = bt->next;
745
    }
746
 
14 andreas 747
    mZOrder = -1;
11 andreas 748
    mVisible = false;
749
}
750
 
53 andreas 751
void TSubPage::startTimer()
752
{
753
    DECL_TRACER("TSubPage::startTimer()");
754
 
755
    if (mSubpage.timeout <= 0 || mTimerRunning)
756
        return;
757
 
758
    try
759
    {
760
        mThreadTimer = std::thread([=] { runTimer(); });
761
        mThreadTimer.detach();
762
    }
763
    catch (std::exception& e)
764
    {
765
        MSG_ERROR("Error starting a timeout thread: " << e.what());
766
        return;
767
    }
768
}
769
 
770
void TSubPage::runTimer()
771
{
772
    DECL_TRACER("TSubPage::runTimer()");
773
 
93 andreas 774
    if (mTimerRunning)
775
        return;
776
 
53 andreas 777
    mTimerRunning = true;
778
    ulong tm = mSubpage.timeout * 100;
54 andreas 779
    ulong unit = 100;
780
    ulong total = 0;
781
 
782
    while (mTimerRunning && !prg_stopped && total < tm)
783
    {
784
        std::this_thread::sleep_for(std::chrono::milliseconds(unit));
785
        total += unit;
786
    }
787
 
788
    drop();
53 andreas 789
    mTimerRunning = false;
790
}
791
 
43 andreas 792
#ifdef _SCALE_SKIA_
31 andreas 793
void TSubPage::calcPosition(int im_width, int im_height, int *left, int *top, bool scale)
43 andreas 794
#else
795
void TSubPage::calcPosition(int im_width, int im_height, int *left, int *top)
796
#endif
31 andreas 797
{
43 andreas 798
    DECL_TRACER("TSubPage::calcPosition(int im_width, int im_height, int *left, int *top)");
31 andreas 799
 
800
    int nw = mSubpage.width;
801
    int nh = mSubpage.height;
43 andreas 802
#ifdef _SCALE_SKIA_
31 andreas 803
    if (scale && gPageManager && gPageManager->getScaleFactor() != 1.0)
804
    {
805
        nw = (int)((double)mSubpage.width * gPageManager->getScaleFactor());
806
        nh = (int)((double)mSubpage.height * gPageManager->getScaleFactor());
807
    }
43 andreas 808
#endif
31 andreas 809
    switch (mSubpage.sr[0].jb)
810
    {
811
        case 0: // absolute position
812
            *left = mSubpage.sr[0].bx;
813
            *top = mSubpage.sr[0].by;
43 andreas 814
#ifdef _SCALE_SKIA_
31 andreas 815
            if (scale && gPageManager && gPageManager->getScaleFactor() != 1.0)
816
            {
817
                *left = (int)((double)mSubpage.sr[0].bx * gPageManager->getScaleFactor());
818
                *left = (int)((double)mSubpage.sr[0].by * gPageManager->getScaleFactor());
819
            }
43 andreas 820
#endif
31 andreas 821
        break;
822
 
823
        case 1: // top, left
824
            *left = 0;
825
            *top = 0;
826
        break;
827
 
828
        case 2: // center, top
829
            *left = (nw - im_width) / 2;
830
            *top = 0;
831
        break;
832
 
833
        case 3: // right, top
834
            *left = nw - im_width;
835
            *top = 0;
836
        break;
837
 
838
        case 4: // left, middle
839
            *left = 0;
840
            *top = (nh - im_height) / 2;
841
        break;
842
 
843
        case 6: // right, middle
844
            *left = nw - im_width;
845
            *top = (nh - im_height) / 2;
846
        break;
847
 
848
        case 7: // left, bottom
849
            *left = 0;
850
            *top = nh - im_height;
851
        break;
852
 
853
        case 8: // center, bottom
854
            *left = (nw - im_width) / 2;
855
            *top = nh - im_height;
856
        break;
857
 
858
        case 9: // right, bottom
859
            *left = nw - im_width;
860
            *top = nh - im_height;
861
        break;
862
 
863
        default:    // center middle
864
            *left = (nw - im_width) / 2;
865
            *top = (nh - im_height) / 2;
866
    }
867
 
868
    if (*left < 0)
869
        *left = 0;
870
 
871
    if (*top < 0)
872
        *top = 0;
873
}
874
 
11 andreas 875
RECT_T TSubPage::getRegion()
876
{
877
    DECL_TRACER("TSubPage::getRegion()");
878
    return {mSubpage.left, mSubpage.top, mSubpage.width, mSubpage.height};
879
}
880
 
15 andreas 881
/**
882
 * This method is called indirectly from the GUI after a mouse click. If This
883
 * subpage matches the clicked coordinates, than the elements are tested. If
884
 * an element is found that matches the coordinates it gets the click. It
885
 * depends on the kind of element what happens.
886
 */
11 andreas 887
void TSubPage::doClick(int x, int y, bool pressed)
888
{
889
    DECL_TRACER("TSubPage::doClick(int x, int y)");
890
 
201 andreas 891
    BUTTONS_T *button = TPageInterface::getButtons();
205 andreas 892
 
893
    if (!button)
894
        return;
895
 
11 andreas 896
    // Find last button
39 andreas 897
    while (button && button->next)
11 andreas 898
        button = button->next;
899
 
900
    // Scan in reverse order
901
    while (button)
902
    {
903
        TButton *but = button->button;
146 andreas 904
        bool clickable = but->isClickable();
905
        MSG_DEBUG("Testing button " << but->getButtonIndex() << " (" << but->getButtonName() << "): " << (clickable ? "CLICKABLE" : "NOT CLICKABLE"));
11 andreas 906
 
154 andreas 907
        if (clickable && x > but->getLeftPosition() && x < (but->getLeftPosition() + but->getWidth()) &&
31 andreas 908
            y > but->getTopPosition() && y < (but->getTopPosition() + but->getHeight()))
11 andreas 909
        {
910
            MSG_DEBUG("Clicking button " << but->getButtonIndex() << ": " << but->getButtonName() << " to state " << (pressed ? "PRESS" : "RELEASE"));
15 andreas 911
            int btX = x - but->getLeftPosition();
912
            int btY = y - but->getTopPosition();
913
 
914
            if (but->doClick(btX, btY, pressed))
915
                break;
11 andreas 916
        }
917
 
918
        button = button->previous;
919
    }
920
}