Fix a crash when running single-threaded and dragging the time slider.
[synfig.git] / synfig-studio / trunk / src / gtkmm / iconcontroler.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file iconcontroler.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "iconcontroler.h"
34 #include <synfig/valuenode_const.h>
35 #include <gtkmm/button.h>
36 #include <gtkmm/window.h>
37 #include <synfigapp/action.h>
38
39 #endif
40
41 /* === U S I N G =========================================================== */
42
43 using namespace etl;
44 using namespace std;
45 using namespace studio;
46 using namespace synfig;
47
48 /* === M A C R O S ========================================================= */
49
50 // Quick hack to keep stuff working until gettext support is added
51 #ifndef _
52 #define _(x)    (x)
53 #define N_(x)   (x)
54 #endif
55
56 #ifdef WIN32
57 #       ifdef IMAGE_DIR
58 #               undef IMAGE_DIR
59 #               define IMAGE_DIR "share\\pixmaps"
60 #       endif
61 #endif
62
63 #ifndef IMAGE_DIR
64 #       define IMAGE_DIR "/usr/local/share/pixmaps"
65 #endif
66
67
68 #ifndef IMAGE_EXT
69 #       define IMAGE_EXT        "png"
70 #endif
71
72 /* === M E T H O D S ======================================================= */
73
74 static Glib::RefPtr<Gdk::Pixbuf> _tree_pixbuf_table_value_type[(int)synfig::ValueBase::TYPE_END];
75
76 #ifdef WIN32
77 IconControler::IconControler(const synfig::String& basepath)
78 #else
79 IconControler::IconControler(const synfig::String& /*basepath*/)
80 #endif
81 {
82         Gtk::IconSource icon_source;
83         icon_source.set_direction_wildcarded();
84         icon_source.set_state_wildcarded();
85         icon_source.set_size_wildcarded();
86         icon_factory=Gtk::IconFactory::create();
87
88         std::string path_to_icons;
89 #ifdef WIN32
90         path_to_icons=basepath+"/../"+IMAGE_DIR;
91 #else
92         path_to_icons=IMAGE_DIR;
93 #endif
94
95         char* synfig_root=getenv("SYNFIG_ROOT");
96         if(synfig_root) {
97                 path_to_icons=synfig_root;
98                 path_to_icons+=ETL_DIRECTORY_SEPARATOR;
99
100                 path_to_icons+="share/pixmaps/synfigstudio";
101         }
102         path_to_icons+=ETL_DIRECTORY_SEPARATOR;
103
104         try{
105         Gtk::Window::set_default_icon_from_file(path_to_icons+"synfig_icon."+IMAGE_EXT);
106         } catch(...)
107         {
108                 synfig::warning("Unable to open "+path_to_icons+"synfig_icon."+IMAGE_EXT);
109         }
110
111 /*
112 #define INIT_STOCK_ICON(name,iconfile,desc)                                                     \
113         stock_##name=Gtk::StockItem(Gtk::StockID("synfig-" #name),desc);                        \
114         Gtk::Stock::add(stock_##name);                                                          \
115         icon_source.set_filename(path_to_icons+iconfile);                                                       \
116         icon_##name.add_source(icon_source);                                            \
117         icon_factory->add(stock_##name.get_stock_id(),icon_##name)
118 */
119 #define INIT_STOCK_ICON(name,iconfile,desc){                                                    \
120         Gtk::StockItem stockitem(Gtk::StockID("synfig-" #name),desc); \
121         Gtk::Stock::add(stockitem);                                                             \
122         Gtk::IconSet icon_set;                                                                  \
123         icon_source.set_filename(path_to_icons+iconfile);                                                       \
124         icon_set.add_source(icon_source);                                               \
125         icon_factory->add(stockitem.get_stock_id(),icon_set); \
126         }
127
128 #define INIT_STOCK_ICON_CLONE(name,stockid,desc){                                                       \
129         Gtk::StockItem stockitem(Gtk::StockID("synfig-" #name),desc); \
130         Gtk::Stock::add(stockitem);                                                             \
131         Gtk::IconSet icon_set;                                                                  \
132         if(Gtk::Stock::lookup(stockitem.get_stock_id(),icon_set))       \
133         icon_factory->add(stockitem.get_stock_id(),icon_set); \
134         }
135
136 #define INIT_STOCK_ITEM(name,desc)                                                      \
137         stock_##name=Gtk::StockItem(Gtk::StockID("synfig-" #name),desc);                        \
138         Gtk::Stock::add(stock_##name);
139
140         INIT_STOCK_ICON(swap_colors,"swap_colors_icon."IMAGE_EXT,_("Swap Colors"));
141         INIT_STOCK_ICON(canvas,"canvas_icon."IMAGE_EXT,_("Canvas"));
142         INIT_STOCK_ICON(value_node,"valuenode_icon."IMAGE_EXT,_("ValueNode"));
143         INIT_STOCK_ICON(real,"real_icon."IMAGE_EXT,_("Real"));
144         INIT_STOCK_ICON(integer,"integer_icon."IMAGE_EXT,_("Integer"));
145         INIT_STOCK_ICON(vector,"vector_icon."IMAGE_EXT,_("Vector"));
146         INIT_STOCK_ICON(color,"color_icon."IMAGE_EXT,_("Color"));
147         INIT_STOCK_ICON(angle,"angle_icon."IMAGE_EXT,_("Angle"));
148         INIT_STOCK_ICON(segment,"segment_icon."IMAGE_EXT,_("Segment"));
149         INIT_STOCK_ICON(about,"about_icon."IMAGE_EXT,_("About"));
150         INIT_STOCK_ICON(rename,"rename_icon."IMAGE_EXT,_("Rename"));
151         INIT_STOCK_ICON(list,"list_icon."IMAGE_EXT,_("Rename"));
152         INIT_STOCK_ICON(canvas_pointer,"canvas_pointer_icon."IMAGE_EXT,_("Rename"));
153         INIT_STOCK_ICON(string,"string_icon."IMAGE_EXT,_("Rename"));
154         INIT_STOCK_ICON(canvas_new,"canvas_icon."IMAGE_EXT,_("New Canvas"));
155         INIT_STOCK_ICON(saveall,"saveall_icon."IMAGE_EXT,_("Save All"));
156         INIT_STOCK_ICON(bool,"bool_icon."IMAGE_EXT,_("Bool"));
157         INIT_STOCK_ICON(layer,"layer_icon."IMAGE_EXT,_("Layer"));
158         INIT_STOCK_ICON(layer_pastecanvas,"pastecanvas_icon."IMAGE_EXT,_("Paste Canvas"));
159         INIT_STOCK_ICON(group,"group_icon."IMAGE_EXT,_("Group"));
160         INIT_STOCK_ICON(grid_enable,"grid_enable_icon."IMAGE_EXT,_("Show Grid"));
161         INIT_STOCK_ICON(grid_disable,"grid_disable_icon."IMAGE_EXT,_("Hide Grid"));
162         INIT_STOCK_ICON(grid_snap_enable,"grid_snap_enable_icon."IMAGE_EXT,_("Enable Grid Snap"));
163         INIT_STOCK_ICON(grid_snap_disable,"grid_snap_disable_icon."IMAGE_EXT,_("Disable Grid Snap"));
164         INIT_STOCK_ICON(duplicate,"duplicate_icon."IMAGE_EXT,_("Duplicate"));
165         INIT_STOCK_ICON(gradient,"gradient_icon."IMAGE_EXT,_("Gradient"));
166         INIT_STOCK_ICON(blinepoint,"blinepoint_icon."IMAGE_EXT,_("BLine Point"));
167
168         INIT_STOCK_ICON(clear_undo,"clear_undo_icon."IMAGE_EXT,_("Clear Undo Stack"));
169         INIT_STOCK_ICON(clear_redo,"clear_redo_icon."IMAGE_EXT,_("Clear Redo Stack"));
170
171         INIT_STOCK_ICON(children,"children_icon."IMAGE_EXT,_("Children"));
172         INIT_STOCK_ICON(curves,"curves_icon."IMAGE_EXT,_("Curves"));
173         INIT_STOCK_ICON(keyframes,"keyframe_icon."IMAGE_EXT,_("Keyframes"));
174         INIT_STOCK_ICON(meta_data,"meta_data_icon."IMAGE_EXT,_("MetaData"));
175         INIT_STOCK_ICON(navigator,"navigator_icon."IMAGE_EXT,_("Navigator"));
176         INIT_STOCK_ICON(timetrack,"time_track_icon."IMAGE_EXT,_("Time Track"));
177
178         INIT_STOCK_ICON(keyframe_lock_all,"keyframe_lock_all."IMAGE_EXT,_("All Keyframes Locked"));
179         INIT_STOCK_ICON(keyframe_lock_past,"keyframe_lock_past."IMAGE_EXT,_("Past Keyframes Locked"));
180         INIT_STOCK_ICON(keyframe_lock_future,"keyframe_lock_future."IMAGE_EXT,_("Future Keyframes Locked"));
181         INIT_STOCK_ICON(keyframe_lock_none,"keyframe_lock_none."IMAGE_EXT,_("No Keyframes Locked"));
182
183         INIT_STOCK_ICON_CLONE(cvs_add,"gtk-add",_("CVS Add"));
184         INIT_STOCK_ICON_CLONE(cvs_update,"gtk-open",_("CVS Update"));
185         INIT_STOCK_ICON_CLONE(cvs_commit,"gtk-save",_("CVS Commit"));
186         INIT_STOCK_ICON_CLONE(cvs_revert,"gtk-revert",_("CVS Revert"));
187
188         // Tools
189         INIT_STOCK_ICON(normal,"normal_icon."IMAGE_EXT,_("Normal Tool"));
190         INIT_STOCK_ICON(polygon,"polygon_icon."IMAGE_EXT,_("Polygon Tool"));
191         INIT_STOCK_ICON(bline,"bline_icon."IMAGE_EXT,_("BLine Tool"));
192         INIT_STOCK_ICON(eyedrop,"eyedrop_icon."IMAGE_EXT,_("Eyedrop Tool"));
193         INIT_STOCK_ICON(fill,"fill_icon."IMAGE_EXT,_("Fill Tool"));
194         INIT_STOCK_ICON(draw,"draw_icon."IMAGE_EXT,_("Draw Tool"));
195         INIT_STOCK_ICON(sketch,"sketch_icon."IMAGE_EXT,_("Sketch Tool"));
196         INIT_STOCK_ICON(circle,"circle_icon."IMAGE_EXT,_("Circle Tool"));
197         INIT_STOCK_ICON(rectangle,"rectangle_icon."IMAGE_EXT,_("Rectangle Tool"));
198         INIT_STOCK_ICON(smooth_move,"smooth_move_icon."IMAGE_EXT,_("SmoothMove Tool"));
199         INIT_STOCK_ICON(scale,"scale_icon."IMAGE_EXT,_("Scale Tool"));
200         INIT_STOCK_ICON(width,"width_icon."IMAGE_EXT,_("Width Tool"));
201         INIT_STOCK_ICON(rotate,"rotate_icon."IMAGE_EXT,_("Rotate Tool"));
202         INIT_STOCK_ICON(zoom,"zoom_icon."IMAGE_EXT,_("Zoom Tool"));
203         INIT_STOCK_ICON(info,"info_icon."IMAGE_EXT,_("Info Tool"));
204         INIT_STOCK_ICON(mirror,"mirror_icon."IMAGE_EXT,_("Mirror Tool"));
205
206         icon_factory->add_default();
207
208         Gtk::IconSize::register_new("synfig-small_icon",12,12);
209         for(int i(0);i<(int)ValueBase::TYPE_END;i++)
210                 _tree_pixbuf_table_value_type[i]=Gtk::Button().render_icon(value_icon(ValueBase::Type(i)),Gtk::ICON_SIZE_SMALL_TOOLBAR);
211
212 }
213
214 IconControler::~IconControler()
215 {
216         for(int i(0);i<(int)ValueBase::TYPE_END;i++)
217                 _tree_pixbuf_table_value_type[i]=Glib::RefPtr<Gdk::Pixbuf>();
218
219         icon_factory->remove_default();
220 }
221
222 Gdk::Cursor
223 IconControler::get_normal_cursor()
224 {
225         return Gdk::Cursor(Gdk::TOP_LEFT_ARROW);
226 }
227
228 Gdk::Cursor
229 IconControler::get_tool_cursor(const Glib::ustring& name,const Glib::RefPtr<Gdk::Window>& window)
230 {
231         Glib::RefPtr<Gdk::Pixmap> pixmap;
232         DEBUGPOINT();
233         pixmap=Gdk::Pixmap::create(window, 64, 64, 8);
234         DEBUGPOINT();
235         pixmap->set_colormap(window->get_colormap());
236         //pixmap->set_colormap(Gdk::Colormap::create(pixmap->get_visual(),false));
237         DEBUGPOINT();
238         Glib::RefPtr<Gdk::Pixbuf> pixbuf;
239         DEBUGPOINT();
240         pixbuf=Gtk::Button().render_icon(Gtk::StockID("synfig-"+name),Gtk::ICON_SIZE_SMALL_TOOLBAR);
241         DEBUGPOINT();
242
243         pixbuf->render_to_drawable_alpha(
244                 pixmap,
245                 0,0,    // SOURCE X,Y
246                 0,0,    // DEST X Y
247                 -1,-1,  // WIDTH HEIGHT
248                 Gdk::PIXBUF_ALPHA_FULL, // (ignored)
249                 64,             //int alpha_threshold,
250                 Gdk::RGB_DITHER_MAX,            //RgbDither dither,
251                 2,2     //int x_dither, int y_dither
252         );
253 /*
254         pixmap->draw_pixbuf(
255                 Glib::RefPtr<const Gdk::GC>(0), // GC
256                 pixbuf,
257                 0, 0, // Source X,Y
258                 0, 0, // Dest X,Y
259                 -1, -1, // Width, Height
260                 Gdk::RGB_DITHER_MAX, // Dither
261                 0,0 // Dither X,Y
262         );
263 */
264         DEBUGPOINT();
265
266         Gdk::Color FG("#000000");
267         Gdk::Color BG("#FF00FF");
268
269         DEBUGPOINT();
270         return Gdk::Cursor(pixmap, pixmap, FG, BG, 0, 0);
271 }
272
273 Gtk::StockID
274 studio::value_icon(synfig::ValueBase::Type type)
275 {
276                 switch(type)
277                 {
278                 case ValueBase::TYPE_REAL:
279                         return Gtk::StockID("synfig-real");
280                         break;
281                 case ValueBase::TYPE_INTEGER:
282                         return Gtk::StockID("synfig-integer");
283                         break;
284                 case ValueBase::TYPE_BOOL:
285                         return Gtk::StockID("synfig-bool");
286                         break;
287                 case ValueBase::TYPE_ANGLE:
288                         return Gtk::StockID("synfig-angle");
289                         break;
290                 case ValueBase::TYPE_VECTOR:
291                         return Gtk::StockID("synfig-vector");
292                         break;
293                 case ValueBase::TYPE_COLOR:
294                         return Gtk::StockID("synfig-color");
295                         break;
296                 case ValueBase::TYPE_STRING:
297                         return Gtk::StockID("synfig-string");
298                         break;
299                 case ValueBase::TYPE_CANVAS:
300                         return Gtk::StockID("synfig-canvas_pointer");
301                         break;
302                 case ValueBase::TYPE_LIST:
303                         return Gtk::StockID("synfig-list");
304                         break;
305                 case ValueBase::TYPE_SEGMENT:
306                         return Gtk::StockID("synfig-segment");
307                         break;
308                 case ValueBase::TYPE_GRADIENT:
309                         return Gtk::StockID("synfig-gradient");
310                         break;
311                 case ValueBase::TYPE_BLINEPOINT:
312                         return Gtk::StockID("synfig-blinepoint");
313                         break;
314                 case ValueBase::TYPE_NIL:
315                 default:
316                         return Gtk::StockID("synfig-unknown");
317                         break;
318                 }
319 }
320
321 Gtk::StockID
322 studio::valuenode_icon(etl::handle<synfig::ValueNode> value_node)
323 {
324         if(handle<ValueNode_Const>::cast_dynamic(value_node))
325         {
326                 return value_icon(value_node->get_type());
327         }
328         else
329         {
330                 return Gtk::StockID("synfig-value_node");
331         }
332 }
333
334 Glib::RefPtr<Gdk::Pixbuf>
335 studio::get_tree_pixbuf(synfig::ValueBase::Type type)
336 {
337         //return Gtk::Button().render_icon(value_icon(type),Gtk::ICON_SIZE_SMALL_TOOLBAR);
338         return _tree_pixbuf_table_value_type[int(type)];
339 }
340
341 Gtk::StockID
342 studio::get_action_stock_id(const synfigapp::Action::BookEntry& action)
343 {
344         Gtk::StockID stock_id;
345         if(action.task=="raise")                        stock_id=Gtk::Stock::GO_UP;
346         else if(action.task=="lower")           stock_id=Gtk::Stock::GO_DOWN;
347         else if(action.task=="add")                     stock_id=Gtk::Stock::ADD;
348         else if(action.task=="insert")          stock_id=Gtk::Stock::ADD;
349         else if(action.task=="move_top")        stock_id=Gtk::Stock::GOTO_TOP;
350         else if(action.task=="move_bottom")     stock_id=Gtk::Stock::GOTO_BOTTOM;
351         else if(action.task=="remove")          stock_id=Gtk::Stock::DELETE;
352         else if(action.task=="set_on")          stock_id=Gtk::Stock::YES;
353         else if(action.task=="set_off")         stock_id=Gtk::Stock::NO;
354         else if(action.task=="remove")          stock_id=Gtk::Stock::DELETE;
355         else                                                            stock_id=Gtk::StockID("synfig-"+action.task);
356
357         return stock_id;
358 }
359
360 Gtk::StockID
361 studio::layer_icon(const synfig::String &layer)
362 {
363         if(layer=="PasteCanvas" || layer=="pastecanvas" || layer=="paste_canvas")
364                 return Gtk::StockID("synfig-layer_pastecanvas");
365         else if(layer=="rotate")
366                 return Gtk::StockID("synfig-rotate");
367         else if(layer=="zoom")
368                 return Gtk::StockID("synfig-zoom");
369         else if(layer=="region")
370                 return Gtk::StockID("synfig-bline");
371         else if(layer=="polygon")
372                 return Gtk::StockID("synfig-polygon");
373         else if(layer=="outline")
374                 return Gtk::StockID("synfig-width");
375         else if(layer=="circle")
376                 return Gtk::StockID("synfig-circle");
377         else if(layer=="rectangle")
378                 return Gtk::StockID("synfig-rectangle");
379         else if(layer.find("gradient")!=String::npos)
380                 return Gtk::StockID("synfig-gradient");
381         else
382                 return Gtk::StockID("synfig-layer");
383 }
384
385 Glib::RefPtr<Gdk::Pixbuf>
386 studio::get_tree_pixbuf_layer(const synfig::String &layer)
387 {
388         return Gtk::Button().render_icon(layer_icon(layer),Gtk::ICON_SIZE_SMALL_TOOLBAR);
389 }