4.3. Adding a Popup Menu

Example 4.3 is a variation of Example 3.3. Instead of clicking the mouse button inside the window to change the color of the box, the user can select a color from a popup menu. The menu is activated by pressing and releasing the right mouse button when the mouse pointer is within the window. Figure 4.4 is a picture of the running program, and Example 4.3 provides the source code.

Figure 4.4. Color Box with Popup Menu

Color Box with Popup Menu


Example 4.3. Changing Colors with a Popup Menu

#include <GL/sgl.hpp>

class ColorBoxWindow: public sgl::Window {
protected:
    //  Access to this custom popup menu class is restricted to
    //  code within this ColorBoxWindow class.
    class ColorSelectorPopup : public sgl::PopupMenu {
        ColorBoxWindow *win;
    public:
        //  The constructor associates a menu item with an action
        //  to perform.  Each action is a method within this class.
        ColorSelectorPopup(ColorBoxWindow *parent) : win(parent) {
            add_menu_item("Red", [this]() { win->set_color_red(); });
            add_menu_item("Green", [this]() { win->set_color_green(); });
            add_menu_item("Blue", [this]() { win->set_color_blue(); });
            add_menu_item("Black", [this]() { win->set_color_black(); });
            add_menu_item("Quit", [this]() { win->quit(); });
        }
    };
    sgl::Color color;
    ColorSelectorPopup menu;
public:
    ColorBoxWindow(): sgl::Window("Color Box", 100, 100, 400, 300,
        0, 100, 0, 100), color(sgl::RED), menu(this){}

    void paint() override {
        sgl::set_color(color);
        sgl::fill_rectangle(25, 25, 50, 50);
    }

    virtual void set_box_color(const sgl::Color& c) {
        color = c;
        repaint();
    }

    virtual void set_color_green() {
        color = sgl::GREEN;
        repaint();
    }

    virtual void set_color_blue() {
        color = sgl::BLUE;
        repaint();
    }

    virtual void set_color_red() {
        color = sgl::RED;
        repaint();
    }

    virtual void set_color_black() {
        color = sgl::BLACK;
        repaint();
    }

    virtual void quit() {
        exit(0);
    }

    //  Pressing the left mouse button lightens the box
    //  Pressing the left mouse button and the shift key
    //  darkens the box
    void mouse_pressed(double, double, sgl::MouseButton) override {
        double inc = (get_key_modifiers() == sgl::KeyModifier::SHIFT_KEY)
                   ? -0.05 : 0.05;
        color.red += inc;
        color.green += inc;
        color.blue += inc;
        repaint();
    }
};

int main() {
    sgl::run<ColorBoxWindow>();
}


The ColorSelectorPopup menu is associated with the application's main window. The constructor populates the menu with text labels and their associated actions. Each action corresponds to a method within the ColorSelectorPopup class. In turn, each action method can call one or more methods in the window object to change its state, in this case changing the color of the box.


Copyright  ©2019 Richard L. HaltermanVersion 0.9.5February 17, 2019
Creative Commons License This work is licensed under a Creative Commons License.