Use ETL directory separator instead of forward slashes.
[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 **
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 "iconcontroller.h"
34 #include <synfig/valuenode_const.h>
35 #include <gtkmm/button.h>
36 #include <gtkmm/window.h>
37 #include <synfigapp/action.h>
38
39 #include "general.h"
40
41 #endif
42
43 /* === U S I N G =========================================================== */
44
45 using namespace etl;
46 using namespace std;
47 using namespace studio;
48 using namespace synfig;
49
50 /* === M A C R O S ========================================================= */
51
52 #ifdef WIN32
53 #       ifdef IMAGE_DIR
54 #               undef IMAGE_DIR
55 #               define IMAGE_DIR "share\\pixmaps"
56 #       endif
57 #endif
58
59 #ifndef IMAGE_DIR
60 #       define IMAGE_DIR "/usr/local/share/pixmaps"
61 #endif
62
63
64 #ifndef IMAGE_EXT
65 #       define IMAGE_EXT        "png"
66 #endif
67
68 /* === M E T H O D S ======================================================= */
69
70 static Glib::RefPtr<Gdk::Pixbuf> _tree_pixbuf_table_value_type[(int)synfig::ValueBase::TYPE_END];
71
72 #ifdef WIN32
73 IconController::IconController(const synfig::String& basepath)
74 #else
75 IconController::IconController(const synfig::String& /*basepath*/)
76 #endif
77 {
78         Gtk::IconSource icon_source;
79         icon_source.set_direction_wildcarded();
80         icon_source.set_state_wildcarded();
81         icon_source.set_size_wildcarded();
82         icon_factory=Gtk::IconFactory::create();
83
84         std::string path_to_icons;
85 #ifdef WIN32
86         path_to_icons=basepath+ETL_DIRECTORY_SEPARATOR+".."+ETL_DIRECTORY_SEPARATOR+IMAGE_DIR;
87 #else
88         path_to_icons=IMAGE_DIR;
89 #endif
90
91         char* synfig_root=getenv("SYNFIG_ROOT");
92         if(synfig_root) {
93                 path_to_icons=synfig_root;
94                 path_to_icons+=ETL_DIRECTORY_SEPARATOR;
95                 path_to_icons+="share";
96                 path_to_icons+=ETL_DIRECTORY_SEPARATOR;
97                 path_to_icons+="pixmaps";
98                 path_to_icons+=ETL_DIRECTORY_SEPARATOR;
99                 path_to_icons+="synfigstudio";
100         }
101         path_to_icons+=ETL_DIRECTORY_SEPARATOR;
102
103         try{
104         Gtk::Window::set_default_icon_from_file(path_to_icons+"synfig_icon."+IMAGE_EXT);
105         } catch(...)
106         {
107                 synfig::warning("Unable to open "+path_to_icons+"synfig_icon."+IMAGE_EXT);
108         }
109
110 /*
111 #define INIT_STOCK_ICON(name,iconfile,desc)                                                     \
112         stock_##name=Gtk::StockItem(Gtk::StockID("synfig-" #name),desc);                        \
113         Gtk::Stock::add(stock_##name);                                                          \
114         icon_source.set_filename(path_to_icons+iconfile);                                                       \
115         icon_##name.add_source(icon_source);                                            \
116         icon_factory->add(stock_##name.get_stock_id(),icon_##name)
117 */
118 #define INIT_STOCK_ICON(name,iconfile,desc){                                                    \
119         Gtk::StockItem stockitem(Gtk::StockID("synfig-" #name),desc); \
120         Gtk::Stock::add(stockitem);                                                             \
121         Gtk::IconSet icon_set;                                                                  \
122         icon_source.set_filename(path_to_icons+iconfile);                                                       \
123         icon_set.add_source(icon_source);                                               \
124         icon_factory->add(stockitem.get_stock_id(),icon_set); \
125         }
126
127 #define INIT_STOCK_ICON_CLONE(name,stockid,desc){                                                       \
128         Gtk::StockItem stockitem(Gtk::StockID("synfig-" #name),desc); \
129         Gtk::Stock::add(stockitem);                                                             \
130         Gtk::IconSet icon_set;                                                                  \
131         if(Gtk::Stock::lookup(stockitem.get_stock_id(),icon_set))       \
132         icon_factory->add(stockitem.get_stock_id(),icon_set); \
133         }
134
135 #define INIT_STOCK_ITEM(name,desc)                                                      \
136         stock_##name=Gtk::StockItem(Gtk::StockID("synfig-" #name),desc);                        \
137         Gtk::Stock::add(stock_##name);
138
139         INIT_STOCK_ICON(bool,"bool_icon."IMAGE_EXT,_("Bool"));
140         INIT_STOCK_ICON(integer,"integer_icon."IMAGE_EXT,_("Integer"));
141         INIT_STOCK_ICON(angle,"angle_icon."IMAGE_EXT,_("Angle"));
142         INIT_STOCK_ICON(time,"time_icon."IMAGE_EXT,_("Time"));
143         INIT_STOCK_ICON(real,"real_icon."IMAGE_EXT,_("Real"));
144         INIT_STOCK_ICON(vector,"vector_icon."IMAGE_EXT,_("Vector"));
145         INIT_STOCK_ICON(color,"color_icon."IMAGE_EXT,_("Color"));
146         INIT_STOCK_ICON(segment,"segment_icon."IMAGE_EXT,_("Segment"));
147         INIT_STOCK_ICON(blinepoint,"blinepoint_icon."IMAGE_EXT,_("BLine Point"));
148         INIT_STOCK_ICON(list,"list_icon."IMAGE_EXT,_("Rename"));
149         INIT_STOCK_ICON(canvas,"canvas_icon."IMAGE_EXT,_("Canvas"));
150         INIT_STOCK_ICON(string,"string_icon."IMAGE_EXT,_("Rename"));
151         INIT_STOCK_ICON(gradient,"gradient_icon."IMAGE_EXT,_("Gradient"));
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(star,"star_icon."IMAGE_EXT,"");
164         INIT_STOCK_ICON(plant,"plant_icon."IMAGE_EXT,"");
165         INIT_STOCK_ICON(text,"text_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_CLONE(cvs_add,"gtk-add",_("CVS Add"));
191         INIT_STOCK_ICON_CLONE(cvs_update,"gtk-open",_("CVS Update"));
192         INIT_STOCK_ICON_CLONE(cvs_commit,"gtk-save",_("CVS Commit"));
193         INIT_STOCK_ICON_CLONE(cvs_revert,"gtk-revert",_("CVS Revert"));
194
195         // Tools
196         INIT_STOCK_ICON(normal,"normal_icon."IMAGE_EXT,_("Normal Tool"));
197         INIT_STOCK_ICON(polygon,"polyline_icon."IMAGE_EXT,_("Polygon Tool"));
198         INIT_STOCK_ICON(bline,"bline_icon."IMAGE_EXT,_("BLine Tool"));
199         INIT_STOCK_ICON(eyedrop,"eyedrop_icon."IMAGE_EXT,_("Eyedrop Tool"));
200         INIT_STOCK_ICON(fill,"fill_icon."IMAGE_EXT,_("Fill Tool"));
201         INIT_STOCK_ICON(draw,"draw_icon."IMAGE_EXT,_("Draw Tool"));
202         INIT_STOCK_ICON(sketch,"sketch_icon."IMAGE_EXT,_("Sketch Tool"));
203         INIT_STOCK_ICON(circle,"circle_icon."IMAGE_EXT,_("Circle Tool"));
204         INIT_STOCK_ICON(rectangle,"rectangle_icon."IMAGE_EXT,_("Rectangle Tool"));
205         INIT_STOCK_ICON(smooth_move,"smooth_move_icon."IMAGE_EXT,_("SmoothMove Tool"));
206         INIT_STOCK_ICON(scale,"scale_icon."IMAGE_EXT,_("Scale Tool"));
207         INIT_STOCK_ICON(width,"width_icon."IMAGE_EXT,_("Width Tool"));
208         INIT_STOCK_ICON(rotate,"rotate_icon."IMAGE_EXT,_("Rotate Tool"));
209         INIT_STOCK_ICON(zoom,"zoom_icon."IMAGE_EXT,_("Zoom Tool"));
210         INIT_STOCK_ICON(info,"info_icon."IMAGE_EXT,_("Info Tool"));
211         INIT_STOCK_ICON(mirror,"mirror_icon."IMAGE_EXT,_("Mirror Tool"));
212
213         icon_factory->add_default();
214
215         Gtk::IconSize::register_new("synfig-small_icon",12,12);
216         for(int i(0);i<(int)ValueBase::TYPE_END;i++)
217                 _tree_pixbuf_table_value_type[i]=Gtk::Button().render_icon(value_icon(ValueBase::Type(i)),Gtk::ICON_SIZE_SMALL_TOOLBAR);
218
219 }
220
221 IconController::~IconController()
222 {
223         for(int i(0);i<(int)ValueBase::TYPE_END;i++)
224                 _tree_pixbuf_table_value_type[i]=Glib::RefPtr<Gdk::Pixbuf>();
225
226         icon_factory->remove_default();
227 }
228
229 Gdk::Cursor
230 IconController::get_normal_cursor()
231 {
232         return Gdk::Cursor(Gdk::TOP_LEFT_ARROW);
233 }
234
235 Gdk::Cursor
236 IconController::get_tool_cursor(const Glib::ustring& name,const Glib::RefPtr<Gdk::Window>& window)
237 {
238         Glib::RefPtr<Gdk::Pixmap> pixmap;
239         pixmap=Gdk::Pixmap::create(window, 64, 64, 8);
240         pixmap->set_colormap(window->get_colormap());
241         //pixmap->set_colormap(Gdk::Colormap::create(pixmap->get_visual(),false));
242         Glib::RefPtr<Gdk::Pixbuf> pixbuf;
243         pixbuf=Gtk::Button().render_icon(Gtk::StockID("synfig-"+name),Gtk::ICON_SIZE_SMALL_TOOLBAR);
244
245         pixbuf->render_to_drawable_alpha(
246                 pixmap,
247                 0,0,    // SOURCE X,Y
248                 0,0,    // DEST X Y
249                 -1,-1,  // WIDTH HEIGHT
250                 Gdk::PIXBUF_ALPHA_FULL, // (ignored)
251                 64,             //int alpha_threshold,
252                 Gdk::RGB_DITHER_MAX,            //RgbDither dither,
253                 2,2     //int x_dither, int y_dither
254         );
255 /*
256         pixmap->draw_pixbuf(
257                 Glib::RefPtr<const Gdk::GC>(0), // GC
258                 pixbuf,
259                 0, 0, // Source X,Y
260                 0, 0, // Dest X,Y
261                 -1, -1, // Width, Height
262                 Gdk::RGB_DITHER_MAX, // Dither
263                 0,0 // Dither X,Y
264         );
265 */
266
267         Gdk::Color FG("#000000");
268         Gdk::Color BG("#FF00FF");
269
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_BOOL:
279                         return Gtk::StockID("synfig-bool");
280                         break;
281                 case ValueBase::TYPE_INTEGER:
282                         return Gtk::StockID("synfig-integer");
283                         break;
284                 case ValueBase::TYPE_ANGLE:
285                         return Gtk::StockID("synfig-angle");
286                         break;
287                 case ValueBase::TYPE_TIME:
288                         return Gtk::StockID("synfig-time");
289                         break;
290                 case ValueBase::TYPE_REAL:
291                         return Gtk::StockID("synfig-real");
292                         break;
293                 case ValueBase::TYPE_VECTOR:
294                         return Gtk::StockID("synfig-vector");
295                         break;
296                 case ValueBase::TYPE_COLOR:
297                         return Gtk::StockID("synfig-color");
298                         break;
299                 case ValueBase::TYPE_SEGMENT:
300                         return Gtk::StockID("synfig-segment");
301                         break;
302                 case ValueBase::TYPE_BLINEPOINT:
303                         return Gtk::StockID("synfig-blinepoint");
304                         break;
305                 case ValueBase::TYPE_LIST:
306                         return Gtk::StockID("synfig-list");
307                         break;
308                 case ValueBase::TYPE_CANVAS:
309                         return Gtk::StockID("synfig-canvas_pointer");
310                         break;
311                 case ValueBase::TYPE_STRING:
312                         return Gtk::StockID("synfig-string");
313                         break;
314                 case ValueBase::TYPE_GRADIENT:
315                         return Gtk::StockID("synfig-gradient");
316                         break;
317                 case ValueBase::TYPE_NIL:
318                 default:
319                         return Gtk::StockID("synfig-unknown");
320                         break;
321                 }
322 }
323
324 Gtk::StockID
325 studio::valuenode_icon(etl::handle<synfig::ValueNode> value_node)
326 {
327         if(handle<ValueNode_Const>::cast_dynamic(value_node))
328         {
329                 return value_icon(value_node->get_type());
330         }
331         else
332         {
333                 return Gtk::StockID("synfig-value_node");
334         }
335 }
336
337 Glib::RefPtr<Gdk::Pixbuf>
338 studio::get_tree_pixbuf(synfig::ValueBase::Type type)
339 {
340         //return Gtk::Button().render_icon(value_icon(type),Gtk::ICON_SIZE_SMALL_TOOLBAR);
341         return _tree_pixbuf_table_value_type[int(type)];
342 }
343
344 #ifdef WIN32
345 #define TEMPORARY_DELETE_MACRO DELETE
346 #undef DELETE
347 #endif
348
349 Gtk::StockID
350 studio::get_action_stock_id(const synfigapp::Action::BookEntry& action)
351 {
352         Gtk::StockID stock_id;
353         if(action.task=="add")                          stock_id=Gtk::Stock::ADD;
354         else if(action.task=="connect")         stock_id=Gtk::Stock::CONNECT;
355         else if(action.task=="disconnect")      stock_id=Gtk::Stock::DISCONNECT;
356         else if(action.task=="insert")          stock_id=Gtk::Stock::ADD;
357         else if(action.task=="lower")           stock_id=Gtk::Stock::GO_DOWN;
358         else if(action.task=="move_bottom")     stock_id=Gtk::Stock::GOTO_BOTTOM;
359         else if(action.task=="move_top")        stock_id=Gtk::Stock::GOTO_TOP;
360         else if(action.task=="raise")           stock_id=Gtk::Stock::GO_UP;
361         else if(action.task=="remove")          stock_id=Gtk::Stock::DELETE;
362         else if(action.task=="set_off")         stock_id=Gtk::Stock::NO;
363         else if(action.task=="set_on")          stock_id=Gtk::Stock::YES;
364         else                                                            stock_id=Gtk::StockID("synfig-"+
365                                                                                                                           action.task);
366         return stock_id;
367 }
368
369 #ifdef WIN32
370 #define DELETE TEMPORARY_DELETE_MACRO
371 #undef TEMPORARY_DELETE_MACRO
372 #endif
373
374 Gtk::StockID
375 studio::layer_icon(const synfig::String &layer)
376 {
377         if(layer=="PasteCanvas" || layer=="pastecanvas" || layer=="paste_canvas")
378                 return Gtk::StockID("synfig-layer_pastecanvas");
379         else if(layer=="rotate")
380                 return Gtk::StockID("synfig-rotate");
381         else if(layer=="zoom")
382                 return Gtk::StockID("synfig-zoom");
383         else if(layer=="region")
384                 return Gtk::StockID("synfig-bline");
385         else if(layer=="polygon")
386                 return Gtk::StockID("synfig-polygon");
387         else if(layer=="outline")
388                 return Gtk::StockID("synfig-width");
389         else if(layer=="circle")
390                 return Gtk::StockID("synfig-circle");
391         else if(layer=="rectangle")
392                 return Gtk::StockID("synfig-rectangle");
393         else if(layer=="star")
394                 return Gtk::StockID("synfig-star");
395         else if(layer=="plant")
396                 return Gtk::StockID("synfig-plant");
397         else if(layer=="text")
398                 return Gtk::StockID("synfig-text");
399         else if(layer.find("gradient")!=String::npos)
400                 return Gtk::StockID("synfig-gradient");
401         else
402                 return Gtk::StockID("synfig-layer");
403 }
404
405 Glib::RefPtr<Gdk::Pixbuf>
406 studio::get_tree_pixbuf_layer(const synfig::String &layer)
407 {
408         return Gtk::Button().render_icon(layer_icon(layer),Gtk::ICON_SIZE_SMALL_TOOLBAR);
409 }