Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

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