Tutorial

Inti Logo

« Calendar
File Selections »

Color Selection

The color selection widget is, not surprisingly, a widget for interactive selection of colors. This composite widget lets the user select a color by manipulating RGB (Red, Green, Blue) and HSV (Hue, Saturation, Value) triples. This is done either by adjusting single values with sliders or entries, or by picking the desired color from a hue-saturation wheel/value bar. Optionally, the opacity of the color can also be set.

The color selection widget currently emits only one signal, color_changed, which is emitted whenever the current color in the widget changes, either when the user changes it or if it's set explicitly through Gtk::ColorSelection::set_color().

Lets have a look at what the color selection widget has to offer us. The widget comes in two flavours: Gtk::ColorSelection and Gtk::ColorSelectionDialog.

ColorSelection();

You'll probably not be using this constructor directly. It creates an orphan ColorSelection widget which you'll have to parent yourself. The ColorSelection widget inherits from the Gtk::VBox.

ColorSelectionDialog();

explicit ColorSelectionDialog(const String& title);

These are the most common color selection constructors. Both constructors create a ColorSelectionDialog, the first with the default title which is just the program name and the second with the title specified. A ColorSelectionDialog consists of a Frame containing a ColorSelection widget, an HSeparator and an HBox with three buttons, "Ok", "Cancel" and "Help". You can reach these buttons by using one of the corresponding accessors:

Gtk::Button* ok_button() const;

Gtk::Button* cancel_button() const;

Gtk::Button* help_button() const;


The color selection widget supports adjusting the opacity of a color (also known as the alpha channel). This is disabled by default. Calling the following method with has_opacity set to true enables opacity. Likewise, has_opacity set to false will disable opacity.

void set_has_opacity_control(bool has_opacity);

You can set the current color explicitly by calling Gtk::ColorSelection::set_current_color() with a reference to a Gdk::Color. Setting the opacity (alpha channel) is done with Gtk::ColorSelection::set_current_alpha(). The alpha value should be between 0 (fully transparent) and 65636 (fully opaque).

void set_current_color(const Gdk::Color& color);

void set_current_alpha(unsigned short alpha);

When you need to query the current color, typically when you've received a color_changed signal, you use these methods:

Gdk::Color get_current_color() const;

unsigned short get_current_alpha() const;

Here's a simple example demonstrating the use of the ColorSelectionDialog. The program displays a window containing a drawing area. Clicking on it opens a color selection dialog, and changing the color in the color selection dialog changes the background color.

Color Selection

The header file for the ColorSelection example is colorselection.h

#include<inti/main.h>
#include <inti/gtk/window.h>
#include <inti/gtk/colorselection.h>
#include <inti/gtk/drawingarea.h>
#include <inti/gdk/color.h>


using namespace Inti;

class ColorSelectionWindow : public Gtk::Window
{
    Gdk::Color color;
    Gtk::DrawingArea *area;

protected:
    bool on_drawing_area_event(GdkEvent *event);
    void on_color_changed(Gtk::ColorSelection *colorsel);

public:
    ColorSelectionWindow();
    virtual ~ColorSelectionWindow();
};


and then source file is colorselection.cc

#include"colorselection.h"
#include <inti/bind.h>


ColorSelectionWindow::ColorSelectionWindow()
{
    set_title("Color selection test");

    // Create drawing area
    area= new Gtk::DrawingArea(250, 200);

    // Set the initial background color    and events to receive
    color.set(0, 0, 65535);
    area->modify_bg(Gtk::STATE_NORMAL, color);
    area->set_events(Gdk::BUTTON_PRESS_MASK);
    area->sig_event().connect(slot(this, &ColorSelectionWindow::on_drawing_area_event));

    // Add drawing area to main window
    add(*area);
    area->show();
    show();
}

ColorSelectionWindow::~ColorSelectionWindow()
{
}

bool
ColorSelectionWindow::on_drawing_area_event(GdkEvent *event)
{
    bool handled = false;

    if (event->type == GDK_BUTTON_PRESS)
    {
        handled = true;
        Gtk::ColorSelectionDialog *dialog = new Gtk::ColorSelectionDialog("Select background color");
        Gtk::ColorSelection *colorsel = dialog->colorsel();
        colorsel->set_previous_color(color);
        colorsel->set_current_color(color);
        colorsel->set_has_palette(true);

        // Connect to the "color_changed" signal
        colorsel->sig_color_changed().connect(bind(slot(this, &ColorSelectionWindow::on_color_changed), colorsel));

        // Show the dialog
        if (dialog->run() == Gtk::RESPONSE_OK)
            color = colorsel->get_current_color();
        else
             area->modify_bg(Gtk::STATE_NORMAL, color);

        dialog->dispose();
    }
    return handled;
}

void
ColorSelectionWindow::on_color_changed(Gtk::ColorSelection *colorsel)
{
    Gdk::Color new_color = colorsel->get_current_color();
    area->modify_bg(Gtk::STATE_NORMAL, new_color);
}

int main (int argc, char *argv[])
{
    using namespace Main;

    init(&argc, &argv);

    ColorSelectionWindow window;
    window.sig_destroy().connect(slot(&Inti::Main::quit));

    run();
    return 0;
}




« Calendar Index
Miscellaneous Widgets
Top
File Selections »