1 /* === S Y N F I G ========================================================= */
2 /*! \file widget_gradient.cpp
3 ** \brief Template File
5 ** $Id: widget_gradient.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
31 #include "widget_gradient.h"
33 #include <gtkmm/menu.h>
34 #include <synfig/exception.h>
39 /* === U S I N G =========================================================== */
43 using namespace synfig;
44 using namespace studio;
46 /* === M A C R O S ========================================================= */
48 /* === G L O B A L S ======================================================= */
50 /* === P R O C E D U R E S ================================================= */
53 studio::render_gradient_to_window(const Glib::RefPtr<Gdk::Drawable>& window,const Gdk::Rectangle& ca,const synfig::Gradient &gradient)
55 int height = ca.get_height();
56 int width = ca.get_width()-4;
58 float sample_width(1.0f/(float)width);
59 Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(window));
60 const Color bg1(0.25, 0.25, 0.25);
61 const Color bg2(0.5, 0.5, 0.5);
66 const Color c(gradient(float(i)/float(width),sample_width));
67 const Color c1(Color::blend(c,bg1,1.0).clamped());
68 const Color c2(Color::blend(c,bg2,1.0).clamped());
69 gushort r1(256*App::gamma.r_F32_to_U8(c1.get_r()));
70 gushort g1(256*App::gamma.g_F32_to_U8(c1.get_g()));
71 gushort b1(256*App::gamma.b_F32_to_U8(c1.get_b()));
72 gushort r2(256*App::gamma.r_F32_to_U8(c2.get_r()));
73 gushort g2(256*App::gamma.g_F32_to_U8(c2.get_g()));
74 gushort b2(256*App::gamma.b_F32_to_U8(c2.get_b()));
78 gdk_c.set_rgb(r1,g1,b1);
79 gc->set_rgb_fg_color(gdk_c);
80 window->draw_rectangle(gc, true, ca.get_x()+i+2, ca.get_y(), 1, height/2);
82 gdk_c.set_rgb(r2,g2,b2);
83 gc->set_rgb_fg_color(gdk_c);
84 window->draw_rectangle(gc, true, ca.get_x()+i+2, ca.get_y()+height/2, 1, height/2);
88 gdk_c.set_rgb(r2,g2,b2);
89 gc->set_rgb_fg_color(gdk_c);
90 window->draw_rectangle(gc, true, ca.get_x()+i+2, ca.get_y(), 1, height/2);
92 gdk_c.set_rgb(r1,g1,b1);
93 gc->set_rgb_fg_color(gdk_c);
94 window->draw_rectangle(gc, true, ca.get_x()+i+2, ca.get_y()+height/2, 1, height/2);
97 gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
98 window->draw_rectangle(gc, false, ca.get_x()+1, ca.get_y()+1, ca.get_width()-3, height-3);
99 gc->set_rgb_fg_color(Gdk::Color("#000000"));
100 window->draw_rectangle(gc, false, ca.get_x(), ca.get_y(), ca.get_width()-1, height-1);
103 /* === M E T H O D S ======================================================= */
105 Widget_Gradient::Widget_Gradient():
108 set_size_request(-1,64);
109 signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Gradient::redraw));
110 add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
111 add_events(Gdk::BUTTON1_MOTION_MASK);
115 Widget_Gradient::~Widget_Gradient()
119 #define CONTROL_HEIGHT 16
121 Widget_Gradient::redraw(GdkEventExpose*bleh)
123 const int h(get_height());
124 const int w(get_width());
126 Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
127 Gdk::Rectangle area(0,0,w,h);
130 render_gradient_to_window(get_window(),area,gradient_);
134 render_gradient_to_window(get_window(),Gdk::Rectangle(0,0,w,h-CONTROL_HEIGHT),gradient_);
136 gc->set_rgb_fg_color(Gdk::Color("#7f7f7f"));
137 get_window()->draw_rectangle(gc, false, 0, h-CONTROL_HEIGHT, w, CONTROL_HEIGHT);
139 Gradient::iterator iter,selected_iter;
140 bool show_selected(false);
141 for(iter=gradient_.begin();iter!=gradient_.end();iter++)
143 if(*iter!=selected_cpoint)
144 get_style()->paint_arrow(
146 (*iter==selected_cpoint)?Gtk::STATE_SELECTED:Gtk::STATE_ACTIVE,
153 int(iter->pos*w)-CONTROL_HEIGHT/2+1,
165 // we do this so that we can be sure that
166 // the selected marker is shown on top
169 get_style()->paint_arrow(
178 round_to_int(selected_iter->pos*w)-CONTROL_HEIGHT/2+1,
189 Widget_Gradient::insert_cpoint(float x)
191 Gradient::CPoint new_cpoint;
193 new_cpoint.color=gradient_(x);
194 gradient_.push_back(new_cpoint);
197 set_selected_cpoint(new_cpoint);
202 Widget_Gradient::remove_cpoint(float x)
204 gradient_.erase(gradient_.proximity(x));
209 Widget_Gradient::popup_menu(float x)
211 Gtk::Menu* menu(manage(new Gtk::Menu()));
213 menu->items().clear();
215 menu->items().push_back(
216 Gtk::Menu_Helpers::MenuElem(
219 sigc::mem_fun(*this,&studio::Widget_Gradient::insert_cpoint),
225 if(!gradient_.empty())
227 menu->items().push_back(
228 Gtk::Menu_Helpers::MenuElem(
231 sigc::mem_fun(*this,&studio::Widget_Gradient::remove_cpoint),
242 Widget_Gradient::set_value(const synfig::Gradient& x)
246 set_selected_cpoint(*gradient_.proximity(0.0f));
251 Widget_Gradient::set_selected_cpoint(const synfig::Gradient::CPoint &x)
254 signal_cpoint_selected_(selected_cpoint);
259 Widget_Gradient::update_cpoint(const synfig::Gradient::CPoint &x)
263 Gradient::iterator iter(gradient_.find(x));
269 catch(synfig::Exception::NotFound)
276 Widget_Gradient::on_event(GdkEvent *event)
280 const int x(static_cast<int>(event->button.x));
281 const int y(static_cast<int>(event->button.y));
283 float pos((float)x/(float)get_width());
284 if(pos<0.0f)pos=0.0f;
285 if(pos>1.0f)pos=1.0f;
289 case GDK_MOTION_NOTIFY:
290 if(editable_ && y>get_height()-CONTROL_HEIGHT)
292 Gradient::iterator iter(gradient_.find(selected_cpoint));
294 if(event->button.state&GDK_SHIFT_MASK)
296 float begin(-100000000),end(100000000);
297 Gradient::iterator before(iter),after(iter);
299 if(iter!=gradient_.begin())
304 if(after!=gradient_.end())
322 // signal_value_changed_();
328 case GDK_BUTTON_PRESS:
330 if(event->button.button==1)
332 if(editable_ && y>get_height()-CONTROL_HEIGHT)
334 set_selected_cpoint(*gradient_.proximity(pos));
344 else if(editable_ && event->button.button==3)
350 case GDK_BUTTON_RELEASE:
351 if(editable_ && event->button.button==1 && y>get_height()-CONTROL_HEIGHT)
353 set_selected_cpoint(*gradient_.proximity(pos));
354 if(changed_)signal_value_changed_();