source: projects/paper-tape-project/trunk/visualisator/gtkpapertapecolor.cc @ 77

Last change on this file since 77 was 77, checked in by sven, 11 years ago

Bugfixes, Recovered Exporting function, ...

File size: 4.2 KB
Line 
1#include "gtkpapertapecolor.h"
2
3#include <gtkmm/box.h>
4#include <gtkmm/label.h>
5#include <gtkmm/menuitem.h>
6#include <gtkmm/colorbutton.h>
7#include <cairo/cairo.h>
8
9#include <iostream>
10
11using namespace Gtk;
12
13// initialize the constant member of the class, only once in the global
14// compilation process (in this object file)
15//const Glib::RefPtr<SizeGroup> PaperTapeColor::label_group = SizeGroup::create(SIZE_GROUP_HORIZONTAL);
16
17PaperTapeColor::PaperTapeColor(const Glib::ustring& name)
18:       Action(name + "_color") //, Gtk::IconTheme::get_default()->get_example_icon_name(), "labelbla", "tooltipbla")
19{
20        std::cout << "Initialized " <<name << "\n";
21}
22
23Glib::RefPtr<PaperTapeColor> PaperTapeColor::create(
24        PaperTapeView* view,
25        cairo_pattern_t** target,
26        const Glib::ustring& action_base_name,
27        const Glib::ustring& dialog_title,
28        const Glib::ustring& menu_label,
29        const Glib::ustring& toggle_label
30) {
31        PaperTapeColor* obj = new PaperTapeColor(action_base_name);
32        Glib::RefPtr<PaperTapeColor> ref(obj);
33       
34        obj->view = view;
35        obj->action_name = action_base_name;
36        obj->dialog_title = dialog_title;
37        obj->menu_label = menu_label;
38        obj->toggle_label = toggle_label;
39        obj->switchable = (toggle_label != "");
40        obj->target = target;
41       
42        obj->property_label() = menu_label;
43
44        // copy current target to value
45        obj->value = *target;
46       
47        // setup toggle action
48        obj->toggle_action = ToggleAction::create(
49                action_base_name + "_toggle",
50                toggle_label,
51                "This is a toggle :-)",
52                (*target != NULL)
53        );
54        obj->toggle_action->signal_toggled().connect(sigc::mem_fun(*obj, &PaperTapeColor::toggled));
55
56        // setup color action
57        //obj->color_action = ColorAction::create(this);
58        // no more need ;-)
59       
60        return ref;
61}
62
63Widget* PaperTapeColor::create_menu_item_vfunc() {
64        menu_item = new MenuItem();
65        // the horizontal menu item box
66        HBox* menu_box = manage(new HBox(false, 4));
67        // the color button on the left
68        //ColorButton* button = manage(new ColorButton);
69        color_button.set_use_alpha(true);
70        color_button.set_title(this->dialog_title);
71        menu_box->pack_start(color_button, false, false, 0);
72        // the label on the right
73        Label* label = manage(new Label);
74        label->set_markup(this->menu_label);
75        label->set_justify(JUSTIFY_CENTER);
76       
77        //label_group->add_widget(*label);
78       
79        menu_box->pack_start(*label, true, true, 0);
80        // now pack'em all
81        menu_item->add(*menu_box);     
82        menu_item->show_all();
83       
84        // make clicking the menuitem activating the color chooser button
85        menu_item->signal_activate().connect(sigc::mem_fun(color_button, &Gtk::Button::clicked));
86        // get color setting signal
87        color_button.signal_color_set().connect(sigc::mem_fun(*this, &PaperTapeColor::color_set));
88       
89        if(*target != NULL) {
90                std::cout << "ALREADY SET COLOR FOR " << dialog_title << "\n";
91                set(*target);
92        } else {
93                // some transparent value + disable!
94                set(0., 0., 0., 0.);
95                menu_item->set_sensitive(false);
96        }
97        return menu_item;
98}
99
100void PaperTapeColor::color_set() {
101        // a color was set!
102        cairo_pattern_destroy(this->value);
103        Gdk::Color color = color_button.get_color();
104        this->value = cairo_pattern_create_rgba(
105                color.get_red_p(),
106                color.get_green_p(),
107                color.get_blue_p(),
108                (double)color_button.get_alpha() / (double)G_MAXUINT16
109        );
110        std::cout << "Signal sent\n";
111        color_changed.emit(this);
112}
113
114void PaperTapeColor::set(cairo_pattern_t* pattern) {
115        // not very perfomant, but secure.
116        double r,g,b,a;
117        cairo_pattern_get_rgba(pattern, &r, &g, &b, &a);
118        set(r, g, b, a);
119}
120
121void PaperTapeColor::set(double red, double green, double blue, double alpha) {
122        // set color to button
123        Gdk::Color color;
124        color.set_rgb_p(red, green, blue);
125        color_button.set_color(color);
126        // save alpha, too
127        color_button.set_alpha((guint16)(alpha * (double)G_MAXUINT16));
128        // save color in our cairo pattern object
129        cairo_pattern_destroy(this->value);
130        this->value = cairo_pattern_create_rgba(red, green, blue, alpha);
131        // send the color changed signal
132        std::cout << "Signal sent\n";
133        color_changed.emit(this);
134}
135
136void PaperTapeColor::toggled() {
137        // Make Color Menuitem sensitivy dependent from toggle status
138        menu_item->set_sensitive(is_displayed());
139       
140        if(*target == NULL)
141                *target = value;
142        else
143                *target = NULL;
144
145        // send the color changed signal
146        std::cout << "Signal sent\n";
147        color_changed.emit(this);
148}
149
Note: See TracBrowser for help on using the repository browser.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License