summaryrefslogtreecommitdiff
path: root/libcore/pixmapcontainer.c
blob: c60ba54e5005a21193489683b3260453a3734bfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
#define __STL_CONFIG_H
#include <math.h>
#include "pixmapcontainer.h"
#include "../config.h"

cMutex cPixmapContainer::mutex;
cOsd *cPixmapContainer::osd = NULL;
eFlushState cPixmapContainer::flushState = fsOpen;

cPixmapContainer::cPixmapContainer(int numPixmaps) {
    this->numPixmaps = numPixmaps;
    pixContainerInit = true;
    mutex.Lock();
    pixmaps = new cPixmap*[numPixmaps];
    pixmapsTransparency = new int[numPixmaps];
    for(int i=0; i < numPixmaps; i++) {
        pixmaps[i] = NULL;
        pixmapsTransparency[i] = 0;
    }
    pixmapsLayer = NULL;
    mutex.Unlock();
    checkRunning = false;
    fadeTime = 0;
    shiftTime = 0;
    shiftType = stNone;
    shiftMode = smLinear;
    deleteOsdOnExit = false;
}

cPixmapContainer::~cPixmapContainer(void) {
    for (int i=0; i < numPixmaps; i++) {
        mutex.Lock();
        if (pixmaps[i] && osd) {
            osd->DestroyPixmap(pixmaps[i]);
            pixmaps[i] = NULL;
        }
        mutex.Unlock();
    }
    delete[] pixmaps;
    delete[] pixmapsTransparency;
    if (pixmapsLayer)
        delete[] pixmapsLayer;

    if (deleteOsdOnExit && osd) {
        mutex.Lock();
        delete osd;
        osd = NULL;
        mutex.Unlock();
    }
    flushState = fsOpen;
}

bool cPixmapContainer::CreateOsd(int Left, int Top, int Width, int Height) {
    if (osd) {
        return false;
    }
    cOsd *newOsd = cOsdProvider::NewOsd(Left, Top);
    if (newOsd) {
        tArea Area = { 0, 0, Width - 1, Height - 1,  32 };
        if (newOsd->SetAreas(&Area, 1) == oeOk) {
            osd = newOsd;
            return true;
        }
    }
    return false;
}

void cPixmapContainer::LockFlush(void) {
    flushState = fsLock; 
}

void cPixmapContainer::OpenFlush(void) { 
    flushState = fsOpen; 
}


void cPixmapContainer::DoFlush(void) {
    cMutexLock MutexLock(&mutex);
    if (!osd || (checkRunning && !Running()))
        return;
    if (flushState == fsOpen) {
        osd->Flush();
    }
}

void cPixmapContainer::HidePixmaps(void) {
    cMutexLock MutexLock(&mutex);
    pixmapsLayer = new int[numPixmaps];
    for(int i=0; i < numPixmaps; i++) {
        if (!pixmaps[i]) {
            pixmapsLayer[i] = 0;
            continue;
        }
        pixmapsLayer[i] = pixmaps[i]->Layer();
        pixmaps[i]->SetLayer(-1);
    }
}

void cPixmapContainer::ShowPixmaps(void) {
    cMutexLock MutexLock(&mutex);
    if (!pixmapsLayer)
        return;
    for(int i=0; i < numPixmaps; i++) {
        if (!pixmaps[i])
            continue;
        pixmaps[i]->SetLayer(pixmapsLayer[i]);
    }
}

/******************************************************************************************************
* Protected Functions
******************************************************************************************************/

bool cPixmapContainer::PixmapExists(int num) {
    cMutexLock MutexLock(&mutex);
    if (pixmaps[num])
        return true;
    return false;
}

void cPixmapContainer::CreatePixmap(int num, int Layer, const cRect &ViewPort, const cRect &DrawPort) {
    cMutexLock MutexLock(&mutex);
    if (!osd || (checkRunning && !Running()))
        return;
    pixmaps[num] = osd->CreatePixmap(Layer, ViewPort, DrawPort);
    if (!pixmaps[num])
        return;
    pixmaps[num]->Fill(clrTransparent);
    if (pixContainerInit && (fadeTime || shiftTime)) {
        pixmaps[num]->SetAlpha(0);
    } else if (pixmapsTransparency[num] > 0) {
        int alpha = (100 - pixmapsTransparency[num])*255/100;
        pixmaps[num]->SetAlpha(alpha);
    }
}

bool cPixmapContainer::DestroyPixmap(int num) {
    cMutexLock MutexLock(&mutex);
    if (pixmaps[num] && osd) {
        osd->DestroyPixmap(pixmaps[num]);
        pixmaps[num] = NULL;
        return true;
    }
    return false;
}

void cPixmapContainer::DrawText(int num, const cPoint &Point, const char *s, tColor ColorFg, tColor ColorBg, std::string fontName, int fontSize) {
    if (checkRunning && !Running())
        return;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return;
    fontManager->Lock();
    cFont *font = fontManager->Font(fontName, fontSize);
    if (font)
        pixmaps[num]->DrawText(Point, s, ColorFg, ColorBg, font);
    fontManager->Unlock();    
}


void cPixmapContainer::DrawRectangle(int num, const cRect &Rect, tColor Color) {
    if (checkRunning && !Running())
        return;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return;
    pixmaps[num]->DrawRectangle(Rect, Color);
}

void cPixmapContainer::DrawEllipse(int num, const cRect &Rect, tColor Color, int Quadrants) {
    if (checkRunning && !Running())
        return;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return;
    pixmaps[num]->DrawEllipse(Rect, Color, Quadrants);
}

void cPixmapContainer::DrawSlope(int num, const cRect &Rect, tColor Color, int Type) {
    if (checkRunning && !Running())
        return;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return;
    pixmaps[num]->DrawSlope(Rect, Color, Type);
}

void cPixmapContainer::DrawImage(int num, const cPoint &Point, const cImage &Image) {
    if (checkRunning && !Running())
        return;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return;
    pixmaps[num]->DrawImage(Point, Image);
}

void cPixmapContainer::DrawBitmap(int num, const cPoint &Point, const cBitmap &Bitmap, tColor ColorFg, tColor ColorBg, bool Overlay) {
    if (checkRunning && !Running())
        return;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return;
    pixmaps[num]->DrawBitmap(Point, Bitmap, ColorFg, ColorBg, Overlay);    
}

void cPixmapContainer::Fill(int num, tColor Color) {
    if (checkRunning && !Running())
        return;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return;
    pixmaps[num]->Fill(Color);
}

void cPixmapContainer::SetAlpha(int num, int Alpha) {
    if (checkRunning && !Running())
        return;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return;
    pixmaps[num]->SetAlpha(Alpha);
}

void cPixmapContainer::SetTransparency(int num, int Transparency) {
    if (Transparency < 0 && Transparency > 100)
        return;
    pixmapsTransparency[num] = Transparency;
}

void cPixmapContainer::SetLayer(int num, int Layer) {
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return;
    pixmaps[num]->SetLayer(Layer);
}

void cPixmapContainer::SetViewPort(int num, const cRect &rect) {
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return;
    pixmaps[num]->SetViewPort(rect);    
}

int cPixmapContainer::Layer(int num) {
    if (checkRunning && !Running())
        return 0;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return 0;
    return pixmaps[num]->Layer();
}

void cPixmapContainer::Pos(int num, cPoint &pos) {
    if (checkRunning && !Running())
        return;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return;
    pos.SetX(pixmaps[num]->ViewPort().X());
    pos.SetY(pixmaps[num]->ViewPort().Y());
}

cRect cPixmapContainer::ViewPort(int num) {
    cRect vp;
    if (checkRunning && !Running())
        return vp;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return vp;
    return pixmaps[num]->ViewPort();
}

int cPixmapContainer::Width(int num) {
    if (checkRunning && !Running())
        return 0;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return 0;
    int width = pixmaps[num]->ViewPort().Width();
    return width;
}

int cPixmapContainer::Height(int num) {
    if (checkRunning && !Running())
        return 0;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return 0;
    int height = pixmaps[num]->ViewPort().Height();
    return height;
}

int cPixmapContainer::DrawportWidth(int num) {
    if (checkRunning && !Running())
        return 0;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return 0;
    int width = pixmaps[num]->DrawPort().Width();
    return width;
}

int cPixmapContainer::DrawportHeight(int num) {
    if (checkRunning && !Running())
        return 0;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return 0;
    int height = pixmaps[num]->DrawPort().Height();
    return height;
}

int cPixmapContainer::DrawportX(int num) {
    if (checkRunning && !Running())
        return 0;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return 0;
    int x = pixmaps[num]->DrawPort().X();
    return x;
}

int cPixmapContainer::DrawportY(int num) {
    if (checkRunning && !Running())
        return 0;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return 0;
    int y = pixmaps[num]->DrawPort().Y();
    return y;
}

void cPixmapContainer::SetDrawPortPoint(int num, const cPoint &Point) {
    if (checkRunning && !Running())
        return;
    cMutexLock MutexLock(&mutex);
    if (!pixmaps[num])
        return;
    pixmaps[num]->SetDrawPortPoint(Point);
}

/***************************************************************************
* HELPERS -- do not access the pixmaps array directly, use wrapper functions
*            to ensure that a proper lock is set before accessing pixmaps
****************************************************************************/

int cPixmapContainer::AnimationDelay(void) {
    int animTime = max(shiftTime, fadeTime);
    return animTime + 100;
}

void cPixmapContainer::FadeIn(void) {
    if (!fadeTime) {
        for (int i = 0; i < numPixmaps; i++) {
            if (PixmapExists(i))
                SetAlpha(i, ALPHA_OPAQUE);
        }
        return;
    }
    int frames = fadeTime * config.framesPerSecond / 1000;
    if (frames <= 0) frames = 1;
    int frameTime = fadeTime / frames;
    uint64_t Start = cTimeMs::Now();
    while (Running()) {
        uint64_t Now = cTimeMs::Now();
        double t = min(double(Now - Start) / fadeTime, 1.0);
        int Alpha = t * ALPHA_OPAQUE;
        for (int i = 0; i < numPixmaps; i++) {
            if (!PixmapExists(i))
                continue;
            if (pixmapsTransparency[i] > 0) {
                int alpha = (100 - pixmapsTransparency[i])*Alpha/100;
                SetAlpha(i, alpha);
            } else {
                SetAlpha(i, Alpha);
            }
        }
        DoFlush();
        int Delta = cTimeMs::Now() - Now;
        if (Running() && (Delta < frameTime))
            cCondWait::SleepMs(frameTime - Delta);
        if ((int)(Now - Start) > fadeTime)
            break;
    }
}

void cPixmapContainer::FadeOut(void) {
    if (!fadeTime || IsAnimated())
        return;
    int frames = fadeTime * config.framesPerSecond / 1000;
    if (frames <= 0) frames = 1;
    int frameTime = fadeTime / frames;
    uint64_t Start = cTimeMs::Now();
    while (true) {
        uint64_t Now = cTimeMs::Now();
        double t = min(double(Now - Start) / fadeTime, 1.0);
        int Alpha = (1 - t) * ALPHA_OPAQUE;
        for (int i = 0; i < numPixmaps; i++) {
            if (!PixmapExists(i))
                continue;
            if (pixmapsTransparency[i] > 0) {
                int alpha = (100 - pixmapsTransparency[i])*Alpha/100;
                SetAlpha(i, alpha);
            } else {
                SetAlpha(i, Alpha);
            }
        }
        DoFlush();
        int Delta = cTimeMs::Now() - Now;
        if (Delta < frameTime)
            cCondWait::SleepMs(frameTime - Delta);
        if ((int)(Now - Start) > fadeTime)
            break;
    }
}

void cPixmapContainer::ShiftIn(void) {
    if (shiftTime < 1)
        return;

    int frames = shiftTime * config.framesPerSecond / 1000;
    if (frames <= 0) frames = 1;
    int frameTime = shiftTime / frames;

    if (shiftType > stNone) {
        ShiftInFromBorder(frames, frameTime);
    } else {
        ShiftInFromPoint(frames, frameTime);
    }
}

void cPixmapContainer::ShiftOut(void) {
    if (shiftTime < 1)
        return;

    int frames = shiftTime * config.framesPerSecond / 1000;
    if (frames <= 0) frames = 1;
    int frameTime = shiftTime / frames;

    if (shiftType > stNone) {
        ShiftOutToBorder(frames, frameTime);
    } else {
        ShiftOutToPoint(frames, frameTime);
    }
}

void cPixmapContainer::ShiftInFromBorder(int frames, int frameTime) {
    cRect unionArea = UnionPixmaps();
    //shifthing all pixmaps to dedicated start positions
    cPoint startPositions[numPixmaps];
    int osdWidth = osd->Width();
    int osdHeight = osd->Height();
    int xStart = 0;
    int yStart = 0;
    for (int i = 0; i < numPixmaps; i++) {
        if (!PixmapExists(i))
            continue;
        cPoint pos;
        Pos(i, pos);
        switch (shiftType) {
            case stLeft:
                xStart = pos.X() - (unionArea.X() + unionArea.Width());
                pos.SetX(xStart);
                break;
            case stRight:
                xStart = osdWidth + (pos.X() - unionArea.X());
                pos.SetX(xStart);
                break;
            case stTop:
                yStart = pos.Y() - (unionArea.Y() + unionArea.Height());
                pos.SetY(yStart);
                break;
            case stBottom:
                yStart = osdHeight + (pos.Y() - unionArea.Y());
                pos.SetY(yStart);
                break;
            default:
                break;
        }
        startPositions[i] = pos;
        cRect r = ViewPort(i);
        r.SetPoint(pos.X(), pos.Y());
        SetViewPort(i, r);
        SetAlpha(i, ALPHA_OPAQUE);
    }
    DoFlush();
    //Calculating total shifting distance
    int shiftTotal = 0;
    switch (shiftType) {
        case stLeft:
            shiftTotal = unionArea.X() + unionArea.Width();
            break;
        case stRight:
            shiftTotal = unionArea.Width() + (osdWidth - (unionArea.X() + unionArea.Width())); 
            break;
        case stTop:
            shiftTotal = unionArea.Y() + unionArea.Height();
            break;
        case stBottom:
            shiftTotal = unionArea.Height() + (osdHeight - (unionArea.Y() + unionArea.Height())); 
            break;
        default:
            break;
    }
    //Moving In
    uint64_t Start = cTimeMs::Now();
    while (Running()) {
        uint64_t Now = cTimeMs::Now();
        double t = min(double(Now - Start) / shiftTime, 1.0);
        if (shiftMode == smSlowedDown) {
            //using f(x) = -(x-1)^2 + 1 as mapping function
            t = (-1)*pow(t - 1, 2) + 1;
        }
        int xNew = 0;
        int yNew = 0;
        for (int i = 0; i < numPixmaps; i++) {
            if (!PixmapExists(i))
                continue;
            cRect r = ViewPort(i);
            switch (shiftType) {
                case stLeft:
                    xNew = startPositions[i].X() + t * shiftTotal;
                    r.SetPoint(xNew, r.Y());
                    break;
                case stRight:
                    xNew = startPositions[i].X() - t * shiftTotal;
                    r.SetPoint(xNew, r.Y());
                    break;
                case stTop:
                    yNew = startPositions[i].Y() + t * shiftTotal;
                    r.SetPoint(r.X(), yNew);
                    break;
                case stBottom:
                    yNew = startPositions[i].Y() - t * shiftTotal;
                    r.SetPoint(r.X(), yNew);
                    break;
                default:
                    break;
            }
            SetViewPort(i, r);
        }
        DoFlush();
        int Delta = cTimeMs::Now() - Now;
        if (Running() && (Delta < frameTime)) {
            cCondWait::SleepMs(frameTime - Delta);
        }
        if ((int)(Now - Start) > shiftTime)
            break;
    }
}

void cPixmapContainer::ShiftOutToBorder(int frames, int frameTime) {
    cRect unionArea = UnionPixmaps();
    //calculating end positions
    cPoint startPositions[numPixmaps];
    int osdWidth = osd->Width();
    int osdHeight = osd->Height();
    for (int i = 0; i < numPixmaps; i++) {
        if (!PixmapExists(i))
            continue;
        cPoint pos;
        Pos(i, pos);
        startPositions[i] = pos;
    }
    //Calculating total shifting distance
    int shiftTotal = 0;
    switch (shiftType) {
        case stLeft:
            shiftTotal = unionArea.X() + unionArea.Width();
            break;
        case stRight:
            shiftTotal = unionArea.Width() + (osdWidth - (unionArea.X() + unionArea.Width())); 
            break;
        case stTop:
            shiftTotal = unionArea.Y() + unionArea.Height();
            break;
        case stBottom:
            shiftTotal = unionArea.Height() + (osdHeight - (unionArea.Y() + unionArea.Height())); 
            break;
        default:
            break;
    }
    //Moving Out
    uint64_t Start = cTimeMs::Now();
    while (true) {
        uint64_t Now = cTimeMs::Now();
        double t = min(double(Now - Start) / shiftTime, 1.0);
        int xNew = 0;
        int yNew = 0;
        for (int i = 0; i < numPixmaps; i++) {
            if (!PixmapExists(i))
                continue;
            cRect r = ViewPort(i);
            switch (shiftType) {
                case stLeft:
                    xNew = startPositions[i].X() - t * shiftTotal;
                    r.SetPoint(xNew, r.Y());
                    break;
                case stRight:
                    xNew = startPositions[i].X() + t * shiftTotal;
                    r.SetPoint(xNew, r.Y());
                    break;
                case stTop:
                    yNew = startPositions[i].Y() - t * shiftTotal;
                    r.SetPoint(r.X(), yNew);
                    break;
                case stBottom:
                    yNew = startPositions[i].Y() + t * shiftTotal;
                    r.SetPoint(r.X(), yNew);
                    break;
                default:
                    break;
            }
            SetViewPort(i, r);
        }
        DoFlush();
        int Delta = cTimeMs::Now() - Now;
        if ((Delta < frameTime)) {
            cCondWait::SleepMs(frameTime - Delta);
        }
        if ((int)(Now - Start) > shiftTime)
            break;
    }    
}

void cPixmapContainer::ShiftInFromPoint(int frames, int frameTime) {
    //store original positions of pixmaps and move to StartPosition
    cPoint destPos[numPixmaps];
    for (int i = 0; i < numPixmaps; i++) {
        if (!PixmapExists(i))
            continue;
        cPoint pos;
        Pos(i, pos);
        destPos[i] = pos;
        cRect r = ViewPort(i);
        r.SetPoint(startPos);
        SetViewPort(i, r);
        SetAlpha(i, ALPHA_OPAQUE);
    }
    DoFlush();
    //Move In
    uint64_t Start = cTimeMs::Now();
    while (Running()) {
        uint64_t Now = cTimeMs::Now();
        double t = min(double(Now - Start) / shiftTime, 1.0);
        for (int i = 0; i < numPixmaps; i++) {
            if (!PixmapExists(i))
                continue;
            int x = startPos.X() + t * (destPos[i].X() - startPos.X());
            int y = startPos.Y() + t * (destPos[i].Y() - startPos.Y());
            cRect r = ViewPort(i);
            r.SetPoint(x, y);
            SetViewPort(i, r);
        }
        DoFlush();
        int Delta = cTimeMs::Now() - Now;
        if (Running() && (Delta < frameTime))
            cCondWait::SleepMs(frameTime - Delta);
        if ((int)(Now - Start) > shiftTime)
            break;
    }
}

void cPixmapContainer::ShiftOutToPoint(int frames, int frameTime) {
    //TODO
}

cRect cPixmapContainer::UnionPixmaps(void) {
    cRect unionArea;
    bool isNew = true;
    for (int i = 0; i < numPixmaps; i++) {
        if (!PixmapExists(i))
            continue;
        if (isNew) {
            unionArea = ViewPort(i);
            isNew = false;
        } else {
            unionArea.Combine(ViewPort(i));
        }
    }
    return unionArea;
}

/*****************************************
* scrollSpeed: 1 slow
*              2 medium
*              3 fast
******************************************/
void cPixmapContainer::ScrollHorizontal(int num, int scrollDelay, int scrollSpeed, int scrollMode) {
    bool carriageReturn = (scrollMode == 1) ? true : false;
    
    int scrollDelta = 1;
    int drawPortX;

    int FrameTime = 0;
    if (scrollSpeed == 1)
        FrameTime = 50;
    else if (scrollSpeed == 2)
        FrameTime = 30;
    else
        FrameTime = 15;
    if (!Running())
        return;
    int maxX = DrawportWidth(num) - Width(num);
    bool doSleep = false;
    while (Running()) {
        if (doSleep) {
            DoSleep(scrollDelay);
            doSleep = false;
        }
        if (!Running())
            return;
        uint64_t Now = cTimeMs::Now();
        drawPortX = DrawportX(num);
        drawPortX -= scrollDelta;

        if (abs(drawPortX) > maxX) {
            DoSleep(scrollDelay);
            if (carriageReturn)
                drawPortX = 0;
            else {
                scrollDelta *= -1;
                drawPortX -= scrollDelta;
            }
            doSleep = true;
        }
        if (!carriageReturn && (drawPortX == 0)) {
            scrollDelta *= -1;
            doSleep = true;
        }
        SetDrawPortPoint(num, cPoint(drawPortX, 0));
        int Delta = cTimeMs::Now() - Now;
        DoFlush();
        if (Running() && (Delta < FrameTime))
            cCondWait::SleepMs(FrameTime - Delta);
    }
}

/*****************************************
* scrollSpeed: 1 slow
*              2 medium
*              3 fast
******************************************/
void cPixmapContainer::ScrollVertical(int num, int scrollDelay, int scrollSpeed) {
    if (!scrollSpeed)
        return;
    DoSleep(scrollDelay);
    int drawPortY;
    int FrameTime = 0;
    if (scrollSpeed == 1)
        FrameTime = 50;
    else if (scrollSpeed == 2)
        FrameTime = 30;
    else 
        FrameTime = 15;
    int maxY = DrawportHeight(num) - Height(num);
    bool doSleep = false;
    while (Running()) {
        if (doSleep) {
            doSleep = false;
            DoSleep(scrollDelay);
        }
        uint64_t Now = cTimeMs::Now();
        drawPortY = DrawportY(num);
        drawPortY -= 1;
        if (abs(drawPortY) > maxY) {
            doSleep = true;
            DoSleep(scrollDelay);
            drawPortY = 0;
        }
        SetDrawPortPoint(num, cPoint(0, drawPortY));
        if (doSleep) {
            DoSleep(scrollDelay);
        }
        int Delta = cTimeMs::Now() - Now;
        DoFlush();
        if (Running() && (Delta < FrameTime))
            cCondWait::SleepMs(FrameTime - Delta);
    }
}

void cPixmapContainer::CancelSave(void) {
    Cancel(-1);
    while (Active())
        cCondWait::SleepMs(10);    
}

void cPixmapContainer::DoSleep(int duration) {
    int sleepSlice = 10;
    for (int i = 0; Running() && (i*sleepSlice < duration); i++)
        cCondWait::SleepMs(sleepSlice);
}

void cPixmapContainer::DrawBlendedBackground(int num, int xStart, int width, tColor color, tColor colorBlending, bool fromTop) {
    int height = Height(num);
    int numSteps = 16;
    int alphaStep = 0x0F;
    int alpha = 0x00;
    int step, begin, end;
    if (fromTop) {
        step = 1;
        begin = 0;
        end = numSteps;
    } else {
        step = -1;
        begin = height;
        end = height - numSteps;
    }
    tColor clr;
    bool cont = true;
    for (int i = begin; cont; i = i + step) {
        clr = AlphaBlend(color, colorBlending, alpha);
        DrawRectangle(num, cRect(xStart,i,width,1), clr);
        alpha += alphaStep;
        if (i == end)
            cont = false;
    }
}

void cPixmapContainer::DrawRoundedCorners(int num, int radius, int x, int y, int width, int height) {
    if (radius > 2) {
        DrawEllipse(num, cRect(x, y, radius, radius), clrTransparent, -2);
        DrawEllipse(num, cRect(x + width - radius, y , radius, radius), clrTransparent, -1);
        DrawEllipse(num, cRect(x, y + height - radius, radius, radius), clrTransparent, -3);
        DrawEllipse(num, cRect(x + width - radius, y + height - radius, radius, radius), clrTransparent, -4);
    }
}

void cPixmapContainer::DrawRoundedCornersWithBorder(int num, tColor borderColor, int radius, int width, int height) {
    if (radius < 3)
        return;
    DrawEllipse(num, cRect(0,0,radius,radius), borderColor, -2);
    DrawEllipse(num, cRect(-1,-1,radius,radius), clrTransparent, -2);

    DrawEllipse(num, cRect(width-radius,0,radius,radius), borderColor, -1);
    DrawEllipse(num, cRect(width-radius+1,-1,radius,radius), clrTransparent, -1);

    DrawEllipse(num, cRect(0,height-radius,radius,radius), borderColor, -3);
    DrawEllipse(num, cRect(-1,height-radius+1,radius,radius), clrTransparent, -3);

    DrawEllipse(num, cRect(width-radius,height-radius,radius,radius), borderColor, -4);
    DrawEllipse(num, cRect(width-radius+1,height-radius+1,radius,radius), clrTransparent, -4);
}