DPF

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

DemoWidgetClickable.hpp (3655B)


      1 /*
      2  * DISTRHO Plugin Framework (DPF)
      3  * Copyright (C) 2019-2021 Jean Pierre Cimalando <jp-dev@inbox.ru>
      4  * Copyright (C) 2012-2023 Filipe Coelho <falktx@falktx.com>
      5  *
      6  * Permission to use, copy, modify, and/or distribute this software for any purpose with
      7  * or without fee is hereby granted, provided that the above copyright notice and this
      8  * permission notice appear in all copies.
      9  *
     10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
     11  * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
     12  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
     13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
     14  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
     15  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     16  */
     17 
     18 #pragma once
     19 
     20 #include "Cairo.hpp"
     21 
     22 START_NAMESPACE_DGL
     23 
     24 // -----------------------------------------------------------------------
     25 
     26 class DemoWidgetClickable : public CairoSubWidget,
     27                             public ButtonEventHandler,
     28                             public ButtonEventHandler::Callback
     29 {
     30 public:
     31     class Callback
     32     {
     33     public:
     34         virtual ~Callback() {}
     35         virtual void demoWidgetClicked(DemoWidgetClickable* widget, uint8_t colorId) = 0;
     36     };
     37 
     38     explicit DemoWidgetClickable(SubWidget* const parent)
     39         : CairoSubWidget(parent),
     40           ButtonEventHandler(this),
     41           fCallback(nullptr),
     42           fColorId(0)
     43     {
     44         ButtonEventHandler::setCallback(this);
     45     }
     46 
     47     explicit DemoWidgetClickable(TopLevelWidget* const parent)
     48         : CairoSubWidget(parent),
     49           ButtonEventHandler(this),
     50           fCallback(nullptr),
     51           fColorId(0)
     52     {
     53         ButtonEventHandler::setCallback(this);
     54     }
     55 
     56     void setCallback(Callback* const callback) noexcept
     57     {
     58         fCallback = callback;
     59     }
     60 
     61     uint8_t getColorId() const noexcept
     62     {
     63         return fColorId;
     64     }
     65 
     66     void setColorId(uint8_t colorId) noexcept
     67     {
     68         fColorId = colorId;
     69         repaint();
     70     }
     71 
     72 protected:
     73     void onCairoDisplay(const CairoGraphicsContext& context) override
     74     {
     75         cairo_t* const cr = context.handle;
     76 
     77         const Size<uint> sz = getSize();
     78         const int w = sz.getWidth();
     79         const int h = sz.getHeight();
     80 
     81         switch (fColorId)
     82         {
     83         case 0:
     84             cairo_set_source_rgb(cr, 0.75, 0.0, 0.0);
     85             break;
     86         case 1:
     87             cairo_set_source_rgb(cr, 0.0, 0.75, 0.0);
     88             break;
     89         case 2:
     90             cairo_set_source_rgb(cr, 0.0, 0.0, 0.75);
     91             break;
     92         }
     93 
     94         cairo_rectangle(cr, 0, 0, w, h);
     95         cairo_fill(cr);
     96 
     97         cairo_set_source_rgb(cr, 0.9, 0.9, 0.9);
     98         cairo_new_path(cr);
     99         cairo_move_to(cr, 0.25 * w, 0.25 * h);
    100         cairo_line_to(cr, 0.75 * w, 0.75 * h);
    101         cairo_stroke(cr);
    102         cairo_new_path(cr);
    103         cairo_move_to(cr, 0.75 * w, 0.25 * h);
    104         cairo_line_to(cr, 0.25 * w, 0.75 * h);
    105         cairo_stroke(cr);
    106     }
    107 
    108     bool onMouse(const MouseEvent& event) override
    109     {
    110         return ButtonEventHandler::mouseEvent(event);
    111     }
    112 
    113     void buttonClicked(SubWidget*, int) override
    114     {
    115         fColorId = (fColorId + 1) % 3;
    116         repaint();
    117 
    118         if (fCallback != nullptr)
    119             fCallback->demoWidgetClicked(this, fColorId);
    120     }
    121 
    122 private:
    123     Callback* fCallback;
    124     uint8_t fColorId;
    125 };
    126 
    127 // -----------------------------------------------------------------------
    128 
    129 END_NAMESPACE_DGL
    130 
    131 using DGL_NAMESPACE::DemoWidgetClickable;