DPF

DISTRHO Plugin Framework
Log | Files | Refs | Submodules | README | LICENSE

FileBrowserDialog.cpp (5468B)


      1 /*
      2  * DISTRHO Plugin Framework (DPF)
      3  * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
      4  *
      5  * Permission to use, copy, modify, and/or distribute this software for any purpose with
      6  * or without fee is hereby granted, provided that the above copyright notice and this
      7  * permission notice appear in all copies.
      8  *
      9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
     10  * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
     11  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
     12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
     13  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
     14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     15  */
     16 
     17 #include "tests.hpp"
     18 
     19 #include "dgl/NanoVG.hpp"
     20 
     21 START_NAMESPACE_DGL
     22 
     23 // --------------------------------------------------------------------------------------------------------------------
     24 
     25 class NanoFilePicker : public NanoStandaloneWindow
     26 {
     27     Rectangle<uint> buttonBounds;
     28     bool buttonClick = false;
     29     bool buttonHover = false;
     30     String selectedFile;
     31 
     32 public:
     33     NanoFilePicker(Application& app)
     34       : NanoStandaloneWindow(app),
     35         selectedFile("No file selected yet")
     36     {
     37 #ifndef DGL_NO_SHARED_RESOURCES
     38         loadSharedResources();
     39 #endif
     40         setResizable(true);
     41         setTitle("FileBrowserDialog");
     42 
     43         const double scaleFactor = getScaleFactor();
     44         setGeometryConstraints(500 * scaleFactor, 200 * scaleFactor, true);
     45         setSize(500 * scaleFactor, 200 * scaleFactor);
     46 
     47         done();
     48     }
     49 
     50 protected:
     51     void onNanoDisplay() override
     52     {
     53         const double scaleFactor = getScaleFactor();
     54 
     55         // Selected file
     56         beginPath();
     57         fontSize(14 * scaleFactor);
     58         textAlign(ALIGN_LEFT | ALIGN_MIDDLE);
     59         fillColor(255, 255, 255, 255);
     60         text(20 * scaleFactor, getHeight()/2, selectedFile, NULL);
     61         closePath();
     62 
     63         // Button background
     64         beginPath();
     65         fillColor(Color(32, buttonClick ? 128 : 32, buttonHover ? 128 : 32));
     66         strokeColor(Color());
     67         rect(buttonBounds.getX(), buttonBounds.getY(), buttonBounds.getWidth(), buttonBounds.getHeight());
     68         fill();
     69         stroke();
     70         closePath();
     71 
     72         // Button label
     73         beginPath();
     74         fontSize(14 * scaleFactor);
     75         Rectangle<float> buttonTextBounds;
     76         textBounds(0, 0, "Press me", NULL, buttonTextBounds);
     77         textAlign(ALIGN_CENTER | ALIGN_MIDDLE);
     78         fillColor(255, 255, 255, 255);
     79         text(buttonBounds.getX() + buttonBounds.getWidth()/2,
     80              buttonBounds.getY() + buttonBounds.getHeight()/2,
     81              "Press me", NULL);
     82         closePath();
     83     }
     84 
     85     bool onMotion(const MotionEvent& ev) override
     86     {
     87         const bool newButtonHover = buttonBounds.contains(ev.pos);
     88 
     89         if (newButtonHover != buttonHover)
     90         {
     91             buttonHover = newButtonHover;
     92             repaint();
     93             return true;
     94         }
     95 
     96         return newButtonHover;
     97     }
     98 
     99     bool onMouse(const MouseEvent& ev) override
    100     {
    101         if (ev.button != 1)
    102             return false;
    103 
    104         if (! buttonBounds.contains(ev.pos))
    105         {
    106             if (buttonClick)
    107             {
    108                 buttonClick = false;
    109                 repaint();
    110                 return true;
    111             }
    112             return false;
    113         }
    114 
    115         const bool newButtonClick = ev.press;
    116 
    117         if (newButtonClick != buttonClick)
    118         {
    119             buttonClick = newButtonClick;
    120             repaint();
    121 
    122             if (newButtonClick)
    123             {
    124                 selectedFile = "(in progress)";
    125                 repaint();
    126 
    127                 FileBrowserOptions opts;
    128                 // opts.saving = true;
    129                 opts.title = "Look at me";
    130                 if (! openFileBrowser(opts))
    131                 {
    132                     selectedFile = "(Failed to start file browser)";
    133                     repaint();
    134                 }
    135             }
    136 
    137             return true;
    138         }
    139 
    140         return newButtonClick;
    141     }
    142 
    143     void onResize(const ResizeEvent& ev) override
    144     {
    145         const uint width  = ev.size.getWidth();
    146         const uint height = ev.size.getHeight();
    147         const double scaleFactor = getScaleFactor();
    148 
    149         buttonBounds = Rectangle<uint>(width - 120 * scaleFactor,
    150                                        height/2 - 20 * scaleFactor,
    151                                        100 * scaleFactor,
    152                                        40 * scaleFactor);
    153     }
    154 
    155     void onFocus(const bool focus, CrossingMode) override
    156     {
    157         if (focus)
    158             return;
    159 
    160         buttonClick = false;
    161         buttonHover = false;
    162         repaint();
    163     }
    164 
    165     void onFileSelected(const char* filename) override
    166     {
    167         if (filename == nullptr)
    168             filename = "Cancelled";
    169 
    170         if (selectedFile == filename)
    171             return;
    172 
    173         selectedFile = filename;
    174         repaint();
    175     }
    176 };
    177 
    178 // --------------------------------------------------------------------------------------------------------------------
    179 
    180 END_NAMESPACE_DGL
    181 
    182 int main()
    183 {
    184     USE_NAMESPACE_DGL;
    185 
    186     Application app(true);
    187     NanoFilePicker win(app);
    188     win.show();
    189     app.exec();
    190 
    191     return 0;
    192 }
    193 
    194 // --------------------------------------------------------------------------------------------------------------------