Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_07_rc1 / src / gtkmm / widget_color.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file widget_color.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "widget_color.h"
33 #include <cmath>
34 #include "app.h"
35 #include <gtkmm/drawingarea.h>
36
37 #endif
38
39 /* === U S I N G =========================================================== */
40
41 using namespace std;
42 using namespace etl;
43 using namespace synfig;
44 using namespace studio;
45
46 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 /* === P R O C E D U R E S ================================================= */
51
52 Gdk::Color
53 studio::colorconv_synfig2gdk(const synfig::Color &c_)
54 {
55         const synfig::Color c(c_.clamped());
56         Gdk::Color ret;
57         ret.set_rgb(
58                         256*App::gamma.r_F32_to_U8(c.get_r()),
59                         256*App::gamma.g_F32_to_U8(c.get_g()),
60                         256*App::gamma.b_F32_to_U8(c.get_b())
61                 );
62         return ret;
63 }
64
65 void
66 studio::render_color_to_window(const Glib::RefPtr<Gdk::Drawable>& window,const Gdk::Rectangle& ca,const synfig::Color &color)
67 {
68         const int height(ca.get_height());
69         const int width(ca.get_width());
70
71         const int square_size(height/2);
72
73         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(window));
74
75         if(color.get_alpha()!=1.0)
76         {
77                 // In this case we need to render the alpha squares
78
79                 const Color bg1(Color::blend(color,Color(0.75, 0.75, 0.75),1.0).clamped());
80                 const Color bg2(Color::blend(color,Color(0.5, 0.5, 0.5),1.0).clamped());
81
82                 Gdk::Color gdk_c1(colorconv_synfig2gdk(bg1));
83                 Gdk::Color gdk_c2(colorconv_synfig2gdk(bg2));
84
85                 bool toggle(false);
86                 for(int i=0;i<width;i+=square_size)
87                 {
88                         const int square_width(min(square_size,width-i));
89
90                         if(toggle)
91                         {
92                                 gc->set_rgb_fg_color(gdk_c1);
93                                 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y(), square_width, square_size);
94
95                                 gc->set_rgb_fg_color(gdk_c2);
96                                 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y()+square_size, square_width, square_size);
97                                 toggle=false;
98                         }
99                         else
100                         {
101                                 gc->set_rgb_fg_color(gdk_c2);
102                                 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y(), square_width, square_size);
103
104                                 gc->set_rgb_fg_color(gdk_c1);
105                                 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y()+square_size, square_width, square_size);
106                                 toggle=true;
107                         }
108                 }
109         }
110         else
111         {
112                 // In this case we have a solid color to use
113                 Gdk::Color gdk_c1(colorconv_synfig2gdk(color));
114
115                 gc->set_rgb_fg_color(gdk_c1);
116                 window->draw_rectangle(gc, true, ca.get_x(), ca.get_y(), width-1, height-1);
117         }
118         gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
119         window->draw_rectangle(gc, false, ca.get_x()+1, ca.get_y()+1, width-3, height-3);
120         gc->set_rgb_fg_color(Gdk::Color("#000000"));
121         window->draw_rectangle(gc, false, ca.get_x(), ca.get_y(), width-1, height-1);
122 }
123
124 /* === C L A S S E S ======================================================= */
125
126
127 /* === M E T H O D S ======================================================= */
128
129 Widget_Color::Widget_Color()
130 {
131         color=Color(0,0,0,0);
132         set_size_request(-1,16);
133
134         signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Color::redraw));
135         add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
136
137 }
138
139 Widget_Color::~Widget_Color()
140 {
141 }
142
143 void
144 Widget_Color::set_value(const synfig::Color &data)
145 {
146         assert(data.is_valid());
147         color=data;
148         queue_draw();
149 }
150
151 const synfig::Color &
152 Widget_Color::get_value()
153 {
154         assert(color.is_valid());
155         return color;
156 }
157
158 bool
159 Widget_Color::on_event(GdkEvent *event)
160 {
161         switch(event->type)
162         {
163         case GDK_BUTTON_PRESS:
164                 if(event->button.button==1)
165                 {
166                         signal_activate_();
167                         return true;
168                 }
169                 if(event->button.button==3)
170                 {
171                         signal_secondary_();
172                         return true;
173                 }
174                 break;
175
176         default:
177                 break;
178         }
179         return false;
180 }
181
182 bool
183 Widget_Color::redraw(GdkEventExpose */*bleh*/)
184 {
185         //Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
186
187         const int h(get_height());
188         const int w(get_width());
189
190         render_color_to_window(get_window(),Gdk::Rectangle(0,0,w,h),color);
191
192         return true;
193 }