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