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
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger                                  |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF       |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN    |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)    |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGE.                                            |
+----------------------------------------------------------------------------+
*/

#include "gate/gatemain.h"
#include "gate/results.h"

#include "gate/applications.hpp"
#include "gate/maps.hpp"
#include "gate/strings.hpp"
#include "gate/streams.hpp"
#include "gate/times.hpp"
#include "gate/files.hpp"
#include "gate/environments.hpp"
#include "gate/wrappers.hpp"

#include "gate/ui/gateui.hpp"
#include "gate/ui/forms.hpp"
#include "gate/ui/buttons.hpp"
#include "gate/ui/labels.hpp"
#include "gate/ui/textboxes.hpp"
#include "gate/ui/treeviews.hpp"
#include "gate/ui/listviews.hpp"
#include "gate/ui/splitters.hpp"
#include "gate/ui/dialogs.hpp"
#include "gate/ui/toolbars.hpp"
#include "gate/ui/statusbars.hpp"
#include "gate/ui/tabs.hpp"
#include "gate/ui/paintsurfaces.hpp"
#include "gate/ui/panels.hpp"
#include "gate/ui/comboboxes.hpp"
#include "gate/ui/timers.hpp"

#include "gate/graphics/pixmapimages.hpp"
#include "gate/graphics/bitmapimages.hpp"
#include "gate/graphics/jpegimages.hpp"
#include "gate/graphics/pngimages.hpp"
#include "gate/graphics/gifimages.hpp"

#include "gate/io/videosources.hpp"

#include "gate/tech/barcodes.hpp"

#include "gate/icons/camera.xpm"
#include "gate/icons/picture.xpm"
#include "gate/icons/layout.xpm"
#include "gate/icons/tv.xpm"
#include "gate/icons/control_eject.xpm"
#include "gate/icons/control_end.xpm"
#include "gate/icons/control_equalizer.xpm"
#include "gate/icons/control_forward.xpm"
#include "gate/icons/control_pause.xpm"
#include "gate/icons/control_play.xpm"
#include "gate/icons/control_repeat.xpm"
#include "gate/icons/control_rewind.xpm"
#include "gate/icons/control_stop.xpm"
#include "gate/icons/rotate.xpm"
#include "gate/icons/flip.xpm"
#include "gate/icons/eye.xpm"

#include "gate/icons/refresh.xpm"
#include "gate/icons/house.xpm"
#include "gate/icons/folder.xpm"
#include "gate/icons/fileopen.xpm"
#include "gate/icons/drive.xpm"
#include "gate/icons/cancel.xpm"

#if defined(GATE_COMPILER_MSVC) && defined(GATE_LEAK_DETECTION) && defined(GATE_SYS_WIN) && !defined(GATE_SYS_WINCE) && (GATE_ARCH != GATE_ARCH_ARM32) && (GATE_ARCH != GATE_ARCH_ARM64)
#	include <vld.h>
#endif

namespace gate
{
    namespace apps
    {
        static String const dirhome_name("[[HOME]]");
        static String const dirhome_key;
        static String const dirparent_name("[..]");
        static String const dirparent_key("..");
        static String const default_extensions("*.bmp;*.gif;*.jpg;*.png;*.jpeg;*.xpm");


        class GATE_API_LOCAL FileViewer : public ui::Panel
        {
        private:
            ui::Layout			layoutLandscape;
            ui::Layout			layoutPortrait;

            ui::Splitter		splitter;
            ui::StringItemview	fileView;
            ui::PaintSurface	painterLandscape;
            ui::PaintSurface	painterPortrait;

            intptr_t			iconHome;
            intptr_t			iconDrive;
            intptr_t			iconFolder;
            intptr_t			iconFolderOpen;
            intptr_t			iconImage;

            String				currentDirectory;

            Ptr<ui::Graphics>	currentImage;

            bool isDirectoryPresent(String const& path)
            {
                if (path.empty())
                {
                    return false;
                }
                return File::isDirectory(path);
            }

            String getHomeDirectory()
            {
                String home;

                try {
                    home = Environment::getDataPath(Environment::DataPathId_Pictures);
                }
                catch (...) {}

                if (!isDirectoryPresent(home))
                {
                    try {
                        home = Environment::getHomePath();
                    }
                    catch (...) {}
                }
                if (!isDirectoryPresent(home))
                {
                    try {
                        home = Environment::getAppExecutableDirPath();
                    }
                    catch (...) {}
                }
                if (!isDirectoryPresent(home))
                {
                    try {
                        home = Environment::getWorkPath();
                    }
                    catch (...) {}
                }
                return home;
            }


            void updateFileView(String folder, String pattern)
            {
                this->fileView.removeAllItems();

                this->currentDirectory = folder;

                static String const nameHomeFolder = String::createStaticFrom("Home folder");
                this->fileView.addItem(dirhome_key, dirhome_name, nameHomeFolder, String(), this->iconHome);

                {
                    String parent;
                    if (File::getParent(folder, parent))
                    {
                        static String const nameParentFolder = String::createStaticFrom("Parent folder");
                        this->fileView.addItem(dirparent_key, dirparent_name, nameParentFolder, String(), this->iconFolderOpen);
                    }
                }

                File::pathmap_t entryPaths;
                try
                {
                    entryPaths = File::listEntryPaths(folder);
                }
                catch (...) {}

                File::pathmap_t filePaths;
                for (File::pathmap_t::const_iterator iter = entryPaths.cbegin(), end = entryPaths.cend(); iter != end; ++iter)
                {
                    String const& path = iter.key();
                    String const& name = iter.value();
                    try
                    {
                        if (!File::isHidden(path))
                        {
                            if (File::isDirectory(path))
                            {
                                StringBuilder nameBuilder;
                                nameBuilder << "[" << name << "]";
                                String textName = nameBuilder.toString();
                                static String nameSubfolder = String::createStaticFrom("Subfolder");
                                this->fileView.addItem(path, textName, nameSubfolder, String(), this->iconFolder);
                            }
                            else
                            {
                                if (!pattern.empty())
                                {
                                    String lowerName = name.toLower();
                                    if (!lowerName.likeOneOf(pattern))
                                    {
                                        continue;
                                    }
                                }
                                filePaths.add(path, name);
                            }
                        }
                    }
                    catch (...) {}
                }

                for (File::pathmap_t::const_iterator iter = filePaths.cbegin(), end = filePaths.cend(); iter != end; ++iter)
                {
                    String const& path = iter.key();
                    String const& name = iter.value();

                    File::Entry entry;
                    try
                    {
                        entry = File::getEntry(path);
                    }
                    catch (...) {}
                    try
                    {
                        size_t pos = path.positionOfLast('.');
                        StringBuilder sb;
                        if (pos != String::npos)
                        {
                            sb << path.substr(pos + 1).toUpper();
                        }
                        else
                        {
                            sb << "Unknown";
                        }
                        sb << " file";
                        String descr = sb.toString();

                        sb << util::printByteSize(entry.getSize()) << ", " << entry.getTimeModified().toString("{YYYY}-{MM}-{DD}  {hh}:{mm}");
                        String info = sb.toString();
                        this->fileView.addItem(path, name, descr, info, this->iconImage);
                    }
                    catch (...) {}
                }

                File::pathmap_t rootPaths;
                try
                {
                    rootPaths = File::listRootEntryPaths();
                }
                catch (...) {}

                for (File::pathmap_t::const_iterator iter = rootPaths.cbegin(), end = rootPaths.cend(); iter != end; ++iter)
                {
                    String const& path = iter.key();
                    String const& name = iter.value();
                    StringBuilder nameBuilder;
                    try
                    {
                        nameBuilder << "[" << name << "]";
                        String textName = nameBuilder.toString();
                        static String nameRoot = String::createStaticFrom("Storage root");
                        this->fileView.addItem(path, textName, nameRoot, String(), this->iconDrive);
                    }
                    catch (...) {}
                }
            }

            void updatePainter(String const& path)
            {
                graph::RasterImage image;
                if (path.endsWithIC(graph::JpegImages::FileExtJpeg)
                    || path.endsWithIC(graph::JpegImages::FileExtJpg))
                {
                    FileStream stream(path);
                    graph::JpegImages::load(image, stream);
                }
                else if (path.endsWithIC(graph::PngImages::FileExt))
                {
                    FileStream stream(path);
                    graph::PngImages::load(image, stream);
                }
                else if (path.endsWithIC(graph::GifImages::FileExt))
                {
                    FileStream stream(path);
                    graph::GifImages::load(image, stream);
                }
                else if (path.endsWithIC(graph::BitmapImages::FileExt))
                {
                    FileStream stream(path);
                    graph::BitmapImages::load(image, stream);
                }
                else if (path.endsWithIC(graph::PixmapImages::FileExt))
                {
                    FileStream stream(path);
                    graph::PixmapImages::load(image, stream);
                }
                else
                {
                    return;
                }

                Ptr<ui::Graphics> imageGraphics(new ui::Graphics(ui::Host::getHost(*this), image));
                this->currentImage.swap(imageGraphics);

                if (this->painterLandscape.isVisible())
                {
                    this->painterLandscape.redraw();
                }
                else
                {
                    this->painterPortrait.redraw();
                }
            }

            void painter_paint(ui::PaintSurface* sender, ui::PaintSurface::PaintEventArg* arg)
            {
                if (this->currentImage)
                {
                    int32_t dst_width = arg->Context.getWidth();
                    int32_t dst_height = arg->Context.getHeight();
                    ui::Color col = ui::Color::Black;
                    ui::Position rect(0, 0, dst_width, dst_height);
                    arg->Context.rect(rect, ui::Color::Black, 0, ui::Color::Black);

                    int32_t src_width = this->currentImage->getWidth();
                    int32_t src_height = this->currentImage->getHeight();

                    if ((src_width > dst_width) || (src_height > dst_height))
                    {
                        float srcRatio = (float)src_width / (float)src_height;
                        float dstRatio = (float)dst_width / (float)dst_height;
                        int32_t width;
                        int32_t height;
                        if (srcRatio > dstRatio)
                        {
                            width = dst_width;
                            height = int32_t((float)width / srcRatio);
                        }
                        else
                        {
                            height = dst_height;
                            width = int32_t((float)height * srcRatio);
                        }
                        ui::Position pos((dst_width - width) / 2, (dst_height - height) / 2, width, height);
                        arg->Context.drawEx(*this->currentImage, pos);
                    }
                    else
                    {
                        ui::Point srcPos((dst_width - src_width) / 2, (dst_height - src_height) / 2);
                        arg->Context.draw(*this->currentImage, srcPos);
                    }
                }
            }

        public:

            void activateLandscapeView()
            {
                this->painterPortrait.setVisible(false);

                this->painterLandscape.setVisible(true);
                this->splitter.setVisible(true);
                this->setLayout(&this->layoutLandscape);
            }
            void activatePortraitView()
            {
                this->splitter.setVisible(false);
                this->painterLandscape.setVisible(false);
                this->painterPortrait.setVisible(true);
                this->setLayout(&this->layoutPortrait);
            }

            void viewPathEntry(String path)
            {
                if (path == dirparent_key)
                {
                    String parent;
                    if (!File::getParent(this->currentDirectory, parent))
                    {
                        return;
                    }
                    path.swap(parent);
                }
                if (path == dirhome_key)
                {
                    path = this->getHomeDirectory();
                }

                if (File::isDirectory(path))
                {
                    updateFileView(path, default_extensions);
                }
                else
                {
                    updatePainter(path);
                }
            }



            void fileView_select(ui::StringItemview* sender, ui::StringItemview::KeyArg* arg)
            {
                if (arg)
                {
                    viewPathEntry(arg->Key);
                }
            }

            FileViewer()
                : Panel(),
                iconHome(ui::Itemview::InvalidIcon),
                iconDrive(ui::Itemview::InvalidIcon),
                iconFolder(ui::Itemview::InvalidIcon),
                iconFolderOpen(ui::Itemview::InvalidIcon),
                iconImage(ui::Itemview::InvalidIcon)
            {
                {	// setup: landscape
                    uint32_t row = this->layoutLandscape.addRow();
                    uint32_t col = this->layoutLandscape.addColumn();

                    this->layoutLandscape.setControl(this->splitter, row, col);
                }

                {
                    uint32_t col = this->layoutPortrait.addColumn();
                    uint32_t rowTop = this->layoutPortrait.addRow();
                    uint32_t rowBottom = this->layoutPortrait.addRow(ui::Layout::Type_UnitScale, 1.5);<--- Variable 'rowBottom' is assigned a value that is never used.

                    this->layoutPortrait.setControl(this->painterPortrait, rowTop, col);
                }

                this->fileView.SelectEvent += ui::StringItemview::SelectEventHandler(this, &FileViewer::fileView_select);
                this->painterLandscape.PaintEvent += ui::PaintSurface::PaintEventHandler(this, &FileViewer::painter_paint);
                this->painterPortrait.PaintEvent += ui::PaintSurface::PaintEventHandler(this, &FileViewer::painter_paint);
            }
            ~FileViewer() noexcept
            {
                this->destroy();
            }

        protected:

            void addFileIcon(char const* const* pixmap, intptr_t& targetKey)
            {
                try
                {
                    ui::Host& host = ui::Host::getHost(*this);<--- Variable 'host' is assigned a value that is never used.

                    graph::RasterImage image;
                    graph::PixmapImages::parse(image, pixmap);
                    targetKey = this->fileView.addIcon(image);
                }
                catch (...)
                {
                    targetKey = ui::Itemview::InvalidIcon;
                }
            }

            void addFileIcon(ui::Icon::StockIdEnum id, intptr_t& targetKey)
            {
                try
                {
                    ui::Host& host = ui::Host::getHost(*this);
                    ui::Icon icon(host, id, false);
                    targetKey = this->fileView.addIcon(icon);
                }
                catch (...)
                {
                    targetKey = ui::Itemview::InvalidIcon;
                }
            }

            void onCreated()
            {
                this->splitter.create(*this, ui::Position(), false);
                this->fileView.create(this->splitter, ui::Position(),
                    ui::Itemview::Flag_Enabled | ui::Itemview::Flag_Visible | ui::Itemview::Flag_Icons);


                //addFileIcon(ui::Icon::StockId_Folder, this->iconFolder);
                //addFileIcon(ui::Icon::StockId_Image, this->iconImage);
                addFileIcon(gate_icon_house_xpm, this->iconHome);
                addFileIcon(gate_icon_drive_xpm, this->iconDrive);
                addFileIcon(gate_icon_folder_xpm, this->iconFolder);
                addFileIcon(gate_icon_fileopen_xpm, this->iconFolderOpen);
                addFileIcon(gate_icon_picture_xpm, this->iconImage);

                this->painterLandscape.create(this->splitter, ui::Position());
                this->splitter.setFirst(this->fileView);
                this->splitter.setSecond(painterLandscape);
                this->splitter.setMinimum(192);

                this->painterPortrait.create(*this, ui::Position());

                this->activateLandscapeView();

                this->updateFileView(this->getHomeDirectory(), default_extensions);
            }

        };


        class GATE_API_LOCAL DeviceViewer : public ui::Panel
        {
        private:
            ui::Layout				layout;

            ui::Toolbar				tbOptions;
            ui::PaintSurface		painter;
            ui::Timer				timer;

            Optional<String>		selectedId;
            Optional<String>		selectedName;
            Optional<io::VideoFormat>	selectedFormat;<--- Shadowed declaration
            Optional<io::VideoSource>	openedSource;
            Ptr<ui::Graphics>	currentGraphics;

            uint32_t				rotate;
            uint32_t				flip;
            bool					detectBarcode;

            static uint16_t const	ToolbarId_Source = 0x01;
            static uint16_t const	ToolbarId_Format = 0x02;
            static uint16_t const	ToolbarId_Start = 0x03;
            static uint16_t const	ToolbarId_Stop = 0x04;
            static uint16_t const	ToolbarId_Rotate = 0x05;
            static uint16_t const	ToolbarId_Flip = 0x06;
            static uint16_t const	ToolbarId_Barcode = 0x07;

            void addToolbarItem(char const* const* const icon_xpm, gate_uint16_t toolbarId)
            {
                graph::RasterImage image;
                graph::PixmapImages::parse(image, icon_xpm);
                this->tbOptions.add(image, (void*)(uintptr_t)toolbarId);
            }

            void selectDevice()
            {
                static String const key(String::createStatic("__key"));
                static String const keyName(String::createStatic("Name"));
                static String const keyPath(String::createStatic("Path"));
                //static String const key

                PropTable table;
                size_t colKey = table.insertColumn(key, Property::Type_String);
                size_t colName = table.insertColumn(keyName, Property::Type_String);
                size_t colId = table.insertColumn(keyPath, Property::Type_String);


                Array<io::VideoSource> sources = io::VideoSources::enumSources();
                Array<io::VideoSource>::enumerator_t enumerator = sources.enumerate();
                while (enumerator.valid())
                {
                    String id = enumerator.get()->getId();
                    String name = enumerator->getName();

                    size_t row = table.insertRow();
                    table.setItemAt(row, colKey, id);
                    table.setItemAt(row, colName, name);
                    table.setItemAt(row, colId, id);

                    enumerator.next();
                }

                ui::ChoiceDialog dlgSelect;
                Optional<Property> selectedSource = dlgSelect.show(*this, table, "Select a video source device", "Select Video Source");
                if (selectedSource)
                {
                    selectedId.create(selectedSource->getObjectMember(key).getString());
                    selectedName.create(selectedSource->getObjectMember(keyName).getString());
                }
            }

            void selectFormat()
            {
                if (!selectedId)
                {
                    return;
                }
                static String const key(String::createStatic("__key"));
                static String const keyDim(String::createStatic("Dimension"));
                static String const keyFPS(String::createStatic("FPS"));
                static String const keyEncoding(String::createStatic("Encoding"));

                PropTable table;
                size_t colKey = table.insertColumn(key, Property::Type_Int);
                size_t colDim = table.insertColumn(keyDim, Property::Type_String);
                size_t colFPS = table.insertColumn(keyFPS, Property::Type_Real);
                size_t colEnc = table.insertColumn(keyEncoding, Property::Type_Int);

                io::VideoSource source(io::VideoSources::getSource(*selectedId));

                Array<io::VideoFormat> formats = source.getSupportedFormats();
                Array<io::VideoFormat>::enumerator_t enumerator = formats.enumerate();

                while (enumerator.valid())
                {
                    size_t row = table.insertRow();
                    table.setItemAt(row, colKey, int64_t(enumerator->hashCode()));

                    StringBuilder fmtBuilder;
                    fmtBuilder << enumerator->width << "x" << enumerator->height << "x" << enumerator->bits_per_pixel;
                    table.setItemAt(row, colDim, fmtBuilder.toString());

                    table.setItemAt(row, colFPS, enumerator->frames_per_second);
                    table.setItemAt(row, colEnc, int64_t(enumerator->encoding_id));

                    enumerator.next();
                }

                ui::ChoiceDialog dlgSelect;
                Optional<Property> selectedFormat = dlgSelect.show(*this, table,<--- Shadow variable
                    "Select a video frame capture format",
                    "Select Video Format");
                if (selectedFormat)
                {
                    hash_code_t selectedHashCode = static_cast<hash_code_t>(selectedFormat->getObjectMember(key).getInt());
                    for (enumerator = formats.enumerate(); enumerator.valid(); enumerator.next())
                    {
                        if (selectedHashCode == enumerator->hashCode())
                        {
                            this->selectedFormat.create(*enumerator);
                            break;
                        }
                    }
                }
            }

            void startCapture()
            {
                if (!this->selectedId || !this->selectedFormat)
                {
                    return;
                }

                io::VideoSource source(io::VideoSources::getSource(*this->selectedId));
                source.open(*this->selectedFormat);
                this->openedSource.create(source);
                this->timer.start(80);
            }

            void stopCapture()
            {
                if (this->openedSource)
                {
                    this->openedSource->close();
                    this->openedSource.reset();
                }
            }

            void detectBarcodeInNextImage()
            {
                this->detectBarcode = true;
            }

            void detectBarcodeInImage(ui::RasterImage const& image)
            {
                try
                {
                    /*
                    DtmxBarcodeProcessor processor;
                    Optional<String> text = processor.scan(image);
                    if(text)
                    {
                        ui::MsgBox msg;
                        msg.show(*this, *text, "DMTX Code detected");
                    }
                    */
                    /*
                    QRCodeBarcodeProcessor processor;
                    Optional<String> text = processor.scan(image);
                    if(text)
                    {
                        ui::MsgBox msg;
                        msg.show(*this, *text, "QR Code detected");
                    }
                    */
                    tech::EanBarcodeProcessor processor;
                    Optional<String> text = processor.scan(image, tech::EanBarcodeProcessor::ScanFlag_Intensive);
                    if (text)
                    {
                        ui::MsgBox msg;
                        msg.show(*this, *text, "Barcode detected");
                    }
                }
                catch (...)
                {

                }
            }

            void tbOptions_Click(ui::Toolbar* sender, ui::Toolbar::ToolbarEventArg* arg)
            {
                uint16_t toolbarId = 0;
                if (arg)
                {
                    toolbarId = (uint16_t)(uintptr_t)arg->button_param;
                    if (toolbarId == ToolbarId_Source)
                    {
                        this->selectDevice();
                    }
                    else if (toolbarId == ToolbarId_Format)
                    {
                        this->selectFormat();
                    }
                    else if (toolbarId == ToolbarId_Start)
                    {
                        this->startCapture();
                    }
                    else if (toolbarId == ToolbarId_Stop)
                    {
                        this->stopCapture();
                    }
                    else if (toolbarId == ToolbarId_Rotate)
                    {
                        ++this->rotate;
                    }
                    else if (toolbarId == ToolbarId_Flip)
                    {
                        ++this->flip;
                    }
                    else if (toolbarId == ToolbarId_Barcode)
                    {
                        this->detectBarcodeInNextImage();
                    }
                }
            }

            virtual void onCreated()
            {
                ui::Host& host = ui::Host::getHost(*this);<--- Variable 'host' is assigned a value that is never used.

                this->tbOptions.create(*this, ui::Position());
                this->timer.create(*this);
                this->painter.create(*this, ui::Position());

                this->addToolbarItem(gate_icon_camera_xpm, ToolbarId_Source);
                this->addToolbarItem(gate_icon_layout_xpm, ToolbarId_Format);
                this->tbOptions.addSeparator();
                this->addToolbarItem(gate_icon_control_play_xpm, ToolbarId_Start);
                this->addToolbarItem(gate_icon_control_stop_xpm, ToolbarId_Stop);
                this->tbOptions.addSeparator();
                this->addToolbarItem(gate_icon_rotate_xpm, ToolbarId_Rotate);
                this->addToolbarItem(gate_icon_flip_xpm, ToolbarId_Flip);
                this->tbOptions.addSeparator();
                this->addToolbarItem(gate_icon_eye_xpm, ToolbarId_Barcode);

                //this->tbOptions.add(ui::Icon(host, ui::Icon::StockId_Video));
                //this->tbOptions.add(ui::Icon(host, ui::Icon::StockId_Settings));
                //this->tbOptions.add(ui::Icon(host, ui::Icon::StockId_App));

                this->setLayout(&this->layout);
            }

            void timer_Interval(ui::Timer* sender, ui::Timer::TimerArg* arg)
            {
                if (this->openedSource)
                {
                    io::VideoFrame frame(*this->selectedFormat);
                    this->openedSource->read(frame);
                    graph::RasterImage image = frame.getImage();

                    uint32_t rotateType = this->rotate % 4;
                    switch (rotateType)
                    {
                    default:
                    case 0:
                        break;
                    case 1:
                        image = image.rotateRight();
                        break;
                    case 2:
                        image = image.rollOver();
                        break;
                    case 3:
                        image = image.rotateLeft();
                        break;
                    }

                    uint32_t flipType = this->flip % 3;
                    switch (flipType)
                    {
                    default:
                    case 0:
                        break;
                    case 1:
                        image = image.flipX();
                        break;
                    case 2:
                        image = image.flipY();
                        break;
                    }

                    if (this->detectBarcode)
                    {
                        this->detectBarcode = false;
                        this->detectBarcodeInImage(image);
                    }

                    this->currentGraphics.reset(new ui::Graphics(ui::Host::getHost(*this), image));
                    this->painter.redraw();
                }
            }

            void painter_Paint(ui::PaintSurface* sender, ui::PaintSurface::PaintEventArg* arg)
            {
                if (this->currentGraphics)
                {
                    arg->Context.draw(*this->currentGraphics);
                }
            }

        public:
            DeviceViewer()
                : ui::Panel(),
                rotate(0),
                flip(0),
                detectBarcode(false)
            {
                uint32_t col = this->layout.addColumn();
                uint32_t row1 = this->layout.addRow(ui::Layout::Type_UnitScale, 1.5f);
                uint32_t row2 = this->layout.addRow();

                this->layout.setControl(this->tbOptions, row1, col);
                this->layout.setControl(this->painter, row2, col);

                this->tbOptions.ClickEvent += ui::Toolbar::ClickEventHandler(this, &DeviceViewer::tbOptions_Click);

                this->timer.IntervalEvent += ui::Timer::IntervalEventHandler(this, &DeviceViewer::timer_Interval);

                this->painter.PaintEvent += ui::PaintSurface::PaintEventHandler(this, &DeviceViewer::painter_Paint);
            }

            ~DeviceViewer() noexcept
            {
                this->destroy();
            }
        };


        class GATE_API_LOCAL VPix : public App
        {
        private:
            Optional<ui::Host>	uihost;

            ui::Form			form;
            ui::Menu			menu;
            ui::Menu			menuFile;
            ui::Menu			menuEdit;
            ui::Menu			menuHelp;

            ui::Layout			layout;

            ui::TabControl		tabs;

            FileViewer			fileViewer;
            ui::Layout			fileViewerLayout;

            DeviceViewer		devicesViewer;
            ui::Layout			devicesLayout;



            static gate_uint16_t const	MenuKey_FileNewDir = 0x01;
            static gate_uint16_t const	MenuKey_FileNewFile = 0x02;
            static gate_uint16_t const	MenuKey_FileOpen = 0x03;
            static gate_uint16_t const	MenuKey_FileRun = 0x04;
            static gate_uint16_t const 	MenuKey_FileSearch = 0x05;
            static gate_uint16_t const	MenuKey_FileExit = 0x0f;

            static gate_uint16_t const	MenuKey_EditRename = 0x11;
            static gate_uint16_t const	MenuKey_EditCut = 0x12;
            static gate_uint16_t const	MenuKey_EditCopy = 0x13;
            static gate_uint16_t const	MenuKey_EditPaste = 0x14;
            static gate_uint16_t const	MenuKey_EditDelete = 0x15;
            static gate_uint16_t const	MenuKey_EditCopyTo = 0x16;
            static gate_uint16_t const	MenuKey_EditMoveTo = 0x17;
            static gate_uint16_t const	MenuKey_EditProperties = 0x18;

            static gate_uint16_t const	MenuKey_HelpInfo = 0x91;

            void Form_Closed(ui::Form* sender, ui::EventArg* arg)
            {
                this->fileExit();
            }

            void fileExit()
            {
                this->form.destroy();
                this->uihost->quit();
            }
            void fileNewDir()
            {

            }
            void fileOpen()
            {

            }
            void fileRun()
            {

            }
            void fileSearch()
            {

            }
            void editRename()
            {

            }
            void editCopyTo()
            {

            }
            void editMoveTo()
            {

            }
            void editCut()
            {

            }
            void editCopy()
            {

            }
            void editPaste()
            {

            }
            void editDelete()
            {

            }
            void editProperties()
            {

            }

            void handleMenuAction(gate_uint16_t menuid)
            {
                if (menuid == MenuKey_FileExit) { this->fileExit(); }
                else if (menuid == MenuKey_FileNewDir) { this->fileNewDir(); }
                else if (menuid == MenuKey_FileOpen) { this->fileOpen(); }
                else if (menuid == MenuKey_FileRun) { this->fileRun(); }
                else if (menuid == MenuKey_FileSearch) { this->fileSearch(); }

                else if (menuid == MenuKey_EditRename) { this->editRename(); }
                else if (menuid == MenuKey_EditCopyTo) { this->editCopyTo(); }
                else if (menuid == MenuKey_EditMoveTo) { this->editMoveTo(); }
                else if (menuid == MenuKey_EditCut) { this->editCut(); }
                else if (menuid == MenuKey_EditCopy) { this->editCopy(); }
                else if (menuid == MenuKey_EditPaste) { this->editPaste(); }
                else if (menuid == MenuKey_EditDelete) { this->editDelete(); }
                else if (menuid == MenuKey_EditProperties) { this->editProperties(); }
            }

            void Form_Menu(ui::Form* sender, ui::MenuArg* arg)
            {
                this->handleMenuAction(arg->MenuEntry.id);
            }

            String currentDirectory;



            void show_image(graph::RasterImage& image)
            {

            }

            void view_file_entry(String const& filePath)
            {
                if (!File::isDirectory(filePath))
                {
                    graph::RasterImage image;

                    try
                    {
                        if (filePath.endsWithIC(graph::PngImages::FileExt))
                        {
                            Stream stream = File::open(filePath);
                            graph::PngImages::load(image, stream);
                        }
                        else if (filePath.endsWithIC(graph::JpegImages::FileExtJpg)
                            || filePath.endsWithIC(graph::JpegImages::FileExtJpeg))
                        {
                            Stream stream = File::open(filePath);
                            graph::JpegImages::load(image, stream);
                        }
                        else if (filePath.endsWithIC(graph::PixmapImages::FileExt))
                        {
                            Stream stream = File::open(filePath);
                            graph::PixmapImages::load(image, stream);
                        }
                        else if (filePath.endsWithIC(graph::BitmapImages::FileExt))
                        {
                            Stream stream = File::open(filePath);
                            graph::BitmapImages::load(image, stream);
                        }
                        else
                        {
                            return;
                        }
                    }
                    catch (Throwable const& xcpt)
                    {
                        (void)xcpt;
                        return;
                    }

                    show_image(image);
                }
            }


        public:

            VPix()
            {
                menuFile.add("New Directory", MenuKey_FileNewDir);
                menuFile.add("New File", MenuKey_FileNewFile);
                menuFile.add("Open", MenuKey_FileOpen);
                menuFile.add("Run", MenuKey_FileRun);
                menuFile.addSeparator();
                menuFile.add("Search...", MenuKey_FileSearch);
                menuFile.addSeparator();
                menuFile.add("Exit", MenuKey_FileExit);

                menuEdit.add("Rename", MenuKey_EditRename);
                menuEdit.add("Copy To", MenuKey_EditCopyTo);
                menuEdit.add("Move To", MenuKey_EditMoveTo);
                menuEdit.addSeparator();
                menuEdit.add("Cut", MenuKey_EditCut);
                menuEdit.add("Copy", MenuKey_EditCopy);
                menuEdit.add("Paste", MenuKey_EditPaste);
                menuEdit.add("Delete", MenuKey_EditDelete);
                menuEdit.addSeparator();
                menuEdit.add("Properties", MenuKey_EditProperties);

                menuHelp.add("Info", MenuKey_HelpInfo);

                menu.add("File", 0, 0, &menuFile);
                menu.add("Edit", 0, 0, &menuEdit);
                menu.add("Help", 0, 0, &menuHelp);

                this->form.MenuEvent += ui::Form::MenuEventHandler(this, &VPix::Form_Menu);

                //this->lbFiles.DblClickEvent += ui::StringListbox::DblClickEventHandler(this, &VPix::lbFiles_dblclick);

            }
            ~VPix() noexcept
            {
            }

            virtual void onInit()
            {
                this->uihost.emplace<uintptr_t const>(this->getHandle());

                uint32_t lineHeight = this->uihost->getLineHeight();

                this->layout.clear();
                uint32_t colMain = this->layout.addColumn();
                uint32_t rowTabs = this->layout.addRow();
                uint32_t rowStatusbar = this->layout.addRow(ui::Layout::Type_Pixel, real32_t(lineHeight + (lineHeight / 2.0f) + 2.0f));<--- Variable 'rowStatusbar' is assigned a value that is never used.

                this->layout.setControl(this->tabs, rowTabs, colMain);
                //this->layout.setControl(this->statusbar, rowStatusbar, colMain);

                this->form.CloseEvent += ui::Form::CloseEventHandler(this, &VPix::Form_Closed);
                this->form.create(*this->uihost, NULL, "vPix - Image Viewer");
                this->form.setMenu(&this->menu);

                this->tabs.create(this->form, ui::Position());

                {
                    this->fileViewerLayout.clear();
                    uint32_t col = this->fileViewerLayout.addColumn();
                    uint32_t row = this->fileViewerLayout.addRow();
                    this->fileViewerLayout.setControl(this->fileViewer, row, col);

                    this->tabs.addTab("File Browser");
                    ui::TabPanel panel = this->tabs.getTabPanel(0);
                    this->fileViewer.create(panel, ui::Position());
                    panel.setLayout(&this->fileViewerLayout);
                }

                {
                    this->devicesLayout.clear();
                    uint32_t col = this->devicesLayout.addColumn();
                    uint32_t row = this->devicesLayout.addRow();
                    this->devicesLayout.setControl(this->devicesViewer, row, col);

                    this->tabs.addTab("Devices");
                    ui::TabPanel panel = this->tabs.getTabPanel(1);
                    this->devicesViewer.create(panel, ui::Position());
                    panel.setLayout(&this->devicesLayout);
                }


                this->form.setLayout(&this->layout);

            }

            virtual void run()
            {
                this->uihost->run();
            }
        };

    }	// end of namespace apps
}	// end of namespace gate


int gate_main(char const* program, char const* const* arguments, gate_size_t argcount, gate_uintptr_t apphandle)
{
    gate::apps::VPix vfilemgr;
    return gate::App::runApp(vfilemgr, program, arguments, argcount, apphandle);
}