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