Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_05 / synfig-studio / src / gtkmm / widget_color.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file widget_color.cpp
3 **      \brief Template File
4 **
5 **      $Id: widget_color.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
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 #define use_colorspace_gamma()  App::use_colorspace_gamma
49 #define colorspace_gamma()      (2.2f)
50 #define gamma_in(x)             ((x>0)?pow((float)x,1.0f/colorspace_gamma()):-pow((float)-x,1.0f/colorspace_gamma()))
51 #define gamma_out(x)    ((x>0)?pow((float)x,colorspace_gamma()):-pow((float)-x,colorspace_gamma()))
52
53 /* === G L O B A L S ======================================================= */
54
55 /* === P R O C E D U R E S ================================================= */
56
57 Gdk::Color
58 studio::colorconv_synfig2gdk(const synfig::Color &c_)
59 {
60         const synfig::Color c(c_.clamped());
61         Gdk::Color ret;
62         ret.set_rgb(
63                         256*App::gamma.r_F32_to_U8(c.get_r()),
64                         256*App::gamma.g_F32_to_U8(c.get_g()),
65                         256*App::gamma.b_F32_to_U8(c.get_b())
66                 );
67         return ret;
68 }
69
70 void
71 studio::render_color_to_window(const Glib::RefPtr<Gdk::Drawable>& window,const Gdk::Rectangle& ca,const synfig::Color &color)
72 {
73         const int height(ca.get_height());
74         const int width(ca.get_width());
75         
76         const int square_size(height/2);
77         
78         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(window));
79         
80         if(color.get_alpha()!=1.0)
81         {
82                 // In this case we need to render the alpha squares
83                 
84                 const Color bg1(Color::blend(color,Color(0.75, 0.75, 0.75),1.0).clamped());
85                 const Color bg2(Color::blend(color,Color(0.5, 0.5, 0.5),1.0).clamped());
86         
87                 Gdk::Color gdk_c1(colorconv_synfig2gdk(bg1));
88                 Gdk::Color gdk_c2(colorconv_synfig2gdk(bg2));
89
90                 bool toggle(false);
91                 for(int i=0;i<width;i+=square_size)
92                 {
93                         const int square_width(min(square_size,width-i));
94                         
95                         if(toggle)
96                         {
97                                 gc->set_rgb_fg_color(gdk_c1);
98                                 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y(), square_width, square_size);                          
99                 
100                                 gc->set_rgb_fg_color(gdk_c2);
101                                 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y()+square_size, square_width, square_size);
102                                 toggle=false;
103                         }
104                         else
105                         {
106                                 gc->set_rgb_fg_color(gdk_c2);
107                                 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y(), square_width, square_size);                          
108                 
109                                 gc->set_rgb_fg_color(gdk_c1);
110                                 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y()+square_size, square_width, square_size);
111                                 toggle=true;
112                         }
113                 }
114         }
115         else
116         {
117                 // In this case we have a solid color to use
118                 Gdk::Color gdk_c1(colorconv_synfig2gdk(color));
119
120                 gc->set_rgb_fg_color(gdk_c1);   
121                 window->draw_rectangle(gc, true, ca.get_x(), ca.get_y(), width-1, height-1);
122         }
123         gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
124         window->draw_rectangle(gc, false, ca.get_x()+1, ca.get_y()+1, width-3, height-3);
125         gc->set_rgb_fg_color(Gdk::Color("#000000"));
126         window->draw_rectangle(gc, false, ca.get_x(), ca.get_y(), width-1, height-1);
127 }
128
129 /* === C L A S S E S ======================================================= */
130
131
132 /* === M E T H O D S ======================================================= */
133
134 Widget_Color::Widget_Color()
135 {
136         color=Color(0,0,0,0);
137         set_size_request(-1,16);
138
139         signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Color::redraw));
140         add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
141
142 }
143
144 Widget_Color::~Widget_Color()
145 {
146 }
147
148 void
149 Widget_Color::set_value(const synfig::Color &data)
150 {
151         assert(data.is_valid());
152         color=data;
153         queue_draw();
154 }
155
156 const synfig::Color &
157 Widget_Color::get_value()
158 {
159         assert(color.is_valid());
160         return color;
161 }
162
163 bool
164 Widget_Color::on_event(GdkEvent *event)
165 {
166         switch(event->type)
167         {
168         case GDK_BUTTON_PRESS:
169                 if(event->button.button==1)
170                 {
171                         signal_activate_();
172                         return true;
173                 }
174                 if(event->button.button==3)
175                 {
176                         signal_secondary_();
177                         return true;
178                 }
179                 break;
180                 
181         default:
182                 break;
183         }
184         return false;
185 }
186
187 bool
188 Widget_Color::redraw(GdkEventExpose*bleh)
189 {
190         //Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
191
192         const int h(get_height());
193         const int w(get_width());       
194         
195         render_color_to_window(get_window(),Gdk::Rectangle(0,0,w,h),color);
196
197         return true;
198 }