Sorted the cases in studio::value_icon() into the same order as the types appear...
[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 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+"/../"+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
96                 path_to_icons+="share/pixmaps/synfigstudio";
97         }
98         path_to_icons+=ETL_DIRECTORY_SEPARATOR;
99
100         try{
101         Gtk::Window::set_default_icon_from_file(path_to_icons+"synfig_icon."+IMAGE_EXT);
102         } catch(...)
103         {
104                 synfig::warning("Unable to open "+path_to_icons+"synfig_icon."+IMAGE_EXT);
105         }
106
107 /*
108 #define INIT_STOCK_ICON(name,iconfile,desc)                                                     \
109         stock_##name=Gtk::StockItem(Gtk::StockID("synfig-" #name),desc);                        \
110         Gtk::Stock::add(stock_##name);                                                          \
111         icon_source.set_filename(path_to_icons+iconfile);                                                       \
112         icon_##name.add_source(icon_source);                                            \
113         icon_factory->add(stock_##name.get_stock_id(),icon_##name)
114 */
115 #define INIT_STOCK_ICON(name,iconfile,desc){                                                    \
116         Gtk::StockItem stockitem(Gtk::StockID("synfig-" #name),desc); \
117         Gtk::Stock::add(stockitem);                                                             \
118         Gtk::IconSet icon_set;                                                                  \
119         icon_source.set_filename(path_to_icons+iconfile);                                                       \
120         icon_set.add_source(icon_source);                                               \
121         icon_factory->add(stockitem.get_stock_id(),icon_set); \
122         }
123
124 #define INIT_STOCK_ICON_CLONE(name,stockid,desc){                                                       \
125         Gtk::StockItem stockitem(Gtk::StockID("synfig-" #name),desc); \
126         Gtk::Stock::add(stockitem);                                                             \
127         Gtk::IconSet icon_set;                                                                  \
128         if(Gtk::Stock::lookup(stockitem.get_stock_id(),icon_set))       \
129         icon_factory->add(stockitem.get_stock_id(),icon_set); \
130         }
131
132 #define INIT_STOCK_ITEM(name,desc)                                                      \
133         stock_##name=Gtk::StockItem(Gtk::StockID("synfig-" #name),desc);                        \
134         Gtk::Stock::add(stock_##name);
135
136         INIT_STOCK_ICON(swap_colors,"swap_colors_icon."IMAGE_EXT,_("Swap Colors"));
137         INIT_STOCK_ICON(canvas,"canvas_icon."IMAGE_EXT,_("Canvas"));
138         INIT_STOCK_ICON(value_node,"valuenode_icon."IMAGE_EXT,_("ValueNode"));
139         INIT_STOCK_ICON(real,"real_icon."IMAGE_EXT,_("Real"));
140         INIT_STOCK_ICON(integer,"integer_icon."IMAGE_EXT,_("Integer"));
141         INIT_STOCK_ICON(vector,"vector_icon."IMAGE_EXT,_("Vector"));
142         INIT_STOCK_ICON(color,"color_icon."IMAGE_EXT,_("Color"));
143         INIT_STOCK_ICON(angle,"angle_icon."IMAGE_EXT,_("Angle"));
144         INIT_STOCK_ICON(segment,"segment_icon."IMAGE_EXT,_("Segment"));
145         INIT_STOCK_ICON(about,"about_icon."IMAGE_EXT,_("About"));
146         INIT_STOCK_ICON(rename,"rename_icon."IMAGE_EXT,_("Rename"));
147         INIT_STOCK_ICON(list,"list_icon."IMAGE_EXT,_("Rename"));
148         INIT_STOCK_ICON(canvas_pointer,"canvas_pointer_icon."IMAGE_EXT,_("Rename"));
149         INIT_STOCK_ICON(string,"string_icon."IMAGE_EXT,_("Rename"));
150         INIT_STOCK_ICON(canvas_new,"canvas_icon."IMAGE_EXT,_("New Canvas"));
151         INIT_STOCK_ICON(saveall,"saveall_icon."IMAGE_EXT,_("Save All"));
152         INIT_STOCK_ICON(bool,"bool_icon."IMAGE_EXT,_("Bool"));
153         INIT_STOCK_ICON(layer,"layer_icon."IMAGE_EXT,_("Layer"));
154         INIT_STOCK_ICON(layer_pastecanvas,"pastecanvas_icon."IMAGE_EXT,_("Paste Canvas"));
155         INIT_STOCK_ICON(group,"group_icon."IMAGE_EXT,_("Group"));
156         INIT_STOCK_ICON(grid_enable,"grid_enable_icon."IMAGE_EXT,_("Show Grid"));
157         INIT_STOCK_ICON(grid_disable,"grid_disable_icon."IMAGE_EXT,_("Hide Grid"));
158         INIT_STOCK_ICON(grid_snap_enable,"grid_snap_enable_icon."IMAGE_EXT,_("Enable Grid Snap"));
159         INIT_STOCK_ICON(grid_snap_disable,"grid_snap_disable_icon."IMAGE_EXT,_("Disable Grid Snap"));
160         INIT_STOCK_ICON(duplicate,"duplicate_icon."IMAGE_EXT,_("Duplicate"));
161         INIT_STOCK_ICON(gradient,"gradient_icon."IMAGE_EXT,_("Gradient"));
162         INIT_STOCK_ICON(blinepoint,"blinepoint_icon."IMAGE_EXT,_("BLine Point"));
163
164         INIT_STOCK_ICON(clear_undo,"clear_undo_icon."IMAGE_EXT,_("Clear Undo Stack"));
165         INIT_STOCK_ICON(clear_redo,"clear_redo_icon."IMAGE_EXT,_("Clear Redo Stack"));
166
167         INIT_STOCK_ICON(children,"children_icon."IMAGE_EXT,_("Children"));
168         INIT_STOCK_ICON(curves,"curves_icon."IMAGE_EXT,_("Curves"));
169         INIT_STOCK_ICON(keyframes,"keyframe_icon."IMAGE_EXT,_("Keyframes"));
170         INIT_STOCK_ICON(meta_data,"meta_data_icon."IMAGE_EXT,_("MetaData"));
171         INIT_STOCK_ICON(navigator,"navigator_icon."IMAGE_EXT,_("Navigator"));
172         INIT_STOCK_ICON(timetrack,"time_track_icon."IMAGE_EXT,_("Time Track"));
173
174         INIT_STOCK_ICON(keyframe_lock_all,"keyframe_lock_all."IMAGE_EXT,_("All Keyframes Locked"));
175         INIT_STOCK_ICON(keyframe_lock_past,"keyframe_lock_past."IMAGE_EXT,_("Past Keyframes Locked"));
176         INIT_STOCK_ICON(keyframe_lock_future,"keyframe_lock_future."IMAGE_EXT,_("Future Keyframes Locked"));
177         INIT_STOCK_ICON(keyframe_lock_none,"keyframe_lock_none."IMAGE_EXT,_("No Keyframes Locked"));
178
179         INIT_STOCK_ICON_CLONE(cvs_add,"gtk-add",_("CVS Add"));
180         INIT_STOCK_ICON_CLONE(cvs_update,"gtk-open",_("CVS Update"));
181         INIT_STOCK_ICON_CLONE(cvs_commit,"gtk-save",_("CVS Commit"));
182         INIT_STOCK_ICON_CLONE(cvs_revert,"gtk-revert",_("CVS Revert"));
183
184         // Tools
185         INIT_STOCK_ICON(normal,"normal_icon."IMAGE_EXT,_("Normal Tool"));
186         INIT_STOCK_ICON(polygon,"polygon_icon."IMAGE_EXT,_("Polygon Tool"));
187         INIT_STOCK_ICON(bline,"bline_icon."IMAGE_EXT,_("BLine Tool"));
188         INIT_STOCK_ICON(eyedrop,"eyedrop_icon."IMAGE_EXT,_("Eyedrop Tool"));
189         INIT_STOCK_ICON(fill,"fill_icon."IMAGE_EXT,_("Fill Tool"));
190         INIT_STOCK_ICON(draw,"draw_icon."IMAGE_EXT,_("Draw Tool"));
191         INIT_STOCK_ICON(sketch,"sketch_icon."IMAGE_EXT,_("Sketch Tool"));
192         INIT_STOCK_ICON(circle,"circle_icon."IMAGE_EXT,_("Circle Tool"));
193         INIT_STOCK_ICON(rectangle,"rectangle_icon."IMAGE_EXT,_("Rectangle Tool"));
194         INIT_STOCK_ICON(smooth_move,"smooth_move_icon."IMAGE_EXT,_("SmoothMove Tool"));
195         INIT_STOCK_ICON(scale,"scale_icon."IMAGE_EXT,_("Scale Tool"));
196         INIT_STOCK_ICON(width,"width_icon."IMAGE_EXT,_("Width Tool"));
197         INIT_STOCK_ICON(rotate,"rotate_icon."IMAGE_EXT,_("Rotate Tool"));
198         INIT_STOCK_ICON(zoom,"zoom_icon."IMAGE_EXT,_("Zoom Tool"));
199         INIT_STOCK_ICON(info,"info_icon."IMAGE_EXT,_("Info Tool"));
200         INIT_STOCK_ICON(mirror,"mirror_icon."IMAGE_EXT,_("Mirror Tool"));
201
202         icon_factory->add_default();
203
204         Gtk::IconSize::register_new("synfig-small_icon",12,12);
205         for(int i(0);i<(int)ValueBase::TYPE_END;i++)
206                 _tree_pixbuf_table_value_type[i]=Gtk::Button().render_icon(value_icon(ValueBase::Type(i)),Gtk::ICON_SIZE_SMALL_TOOLBAR);
207
208 }
209
210 IconController::~IconController()
211 {
212         for(int i(0);i<(int)ValueBase::TYPE_END;i++)
213                 _tree_pixbuf_table_value_type[i]=Glib::RefPtr<Gdk::Pixbuf>();
214
215         icon_factory->remove_default();
216 }
217
218 Gdk::Cursor
219 IconController::get_normal_cursor()
220 {
221         return Gdk::Cursor(Gdk::TOP_LEFT_ARROW);
222 }
223
224 Gdk::Cursor
225 IconController::get_tool_cursor(const Glib::ustring& name,const Glib::RefPtr<Gdk::Window>& window)
226 {
227         Glib::RefPtr<Gdk::Pixmap> pixmap;
228         DEBUGPOINT();
229         pixmap=Gdk::Pixmap::create(window, 64, 64, 8);
230         DEBUGPOINT();
231         pixmap->set_colormap(window->get_colormap());
232         //pixmap->set_colormap(Gdk::Colormap::create(pixmap->get_visual(),false));
233         DEBUGPOINT();
234         Glib::RefPtr<Gdk::Pixbuf> pixbuf;
235         DEBUGPOINT();
236         pixbuf=Gtk::Button().render_icon(Gtk::StockID("synfig-"+name),Gtk::ICON_SIZE_SMALL_TOOLBAR);
237         DEBUGPOINT();
238
239         pixbuf->render_to_drawable_alpha(
240                 pixmap,
241                 0,0,    // SOURCE X,Y
242                 0,0,    // DEST X Y
243                 -1,-1,  // WIDTH HEIGHT
244                 Gdk::PIXBUF_ALPHA_FULL, // (ignored)
245                 64,             //int alpha_threshold,
246                 Gdk::RGB_DITHER_MAX,            //RgbDither dither,
247                 2,2     //int x_dither, int y_dither
248         );
249 /*
250         pixmap->draw_pixbuf(
251                 Glib::RefPtr<const Gdk::GC>(0), // GC
252                 pixbuf,
253                 0, 0, // Source X,Y
254                 0, 0, // Dest X,Y
255                 -1, -1, // Width, Height
256                 Gdk::RGB_DITHER_MAX, // Dither
257                 0,0 // Dither X,Y
258         );
259 */
260         DEBUGPOINT();
261
262         Gdk::Color FG("#000000");
263         Gdk::Color BG("#FF00FF");
264
265         DEBUGPOINT();
266         return Gdk::Cursor(pixmap, pixmap, FG, BG, 0, 0);
267 }
268
269 Gtk::StockID
270 studio::value_icon(synfig::ValueBase::Type type)
271 {
272                 switch(type)
273                 {
274                 case ValueBase::TYPE_BOOL:
275                         return Gtk::StockID("synfig-bool");
276                         break;
277                 case ValueBase::TYPE_INTEGER:
278                         return Gtk::StockID("synfig-integer");
279                         break;
280                 case ValueBase::TYPE_ANGLE:
281                         return Gtk::StockID("synfig-angle");
282                         break;
283                 case ValueBase::TYPE_REAL:
284                         return Gtk::StockID("synfig-real");
285                         break;
286                 case ValueBase::TYPE_VECTOR:
287                         return Gtk::StockID("synfig-vector");
288                         break;
289                 case ValueBase::TYPE_COLOR:
290                         return Gtk::StockID("synfig-color");
291                         break;
292                 case ValueBase::TYPE_SEGMENT:
293                         return Gtk::StockID("synfig-segment");
294                         break;
295                 case ValueBase::TYPE_BLINEPOINT:
296                         return Gtk::StockID("synfig-blinepoint");
297                         break;
298                 case ValueBase::TYPE_LIST:
299                         return Gtk::StockID("synfig-list");
300                         break;
301                 case ValueBase::TYPE_CANVAS:
302                         return Gtk::StockID("synfig-canvas_pointer");
303                         break;
304                 case ValueBase::TYPE_STRING:
305                         return Gtk::StockID("synfig-string");
306                         break;
307                 case ValueBase::TYPE_GRADIENT:
308                         return Gtk::StockID("synfig-gradient");
309                         break;
310                 case ValueBase::TYPE_NIL:
311                 default:
312                         return Gtk::StockID("synfig-unknown");
313                         break;
314                 }
315 }
316
317 Gtk::StockID
318 studio::valuenode_icon(etl::handle<synfig::ValueNode> value_node)
319 {
320         if(handle<ValueNode_Const>::cast_dynamic(value_node))
321         {
322                 return value_icon(value_node->get_type());
323         }
324         else
325         {
326                 return Gtk::StockID("synfig-value_node");
327         }
328 }
329
330 Glib::RefPtr<Gdk::Pixbuf>
331 studio::get_tree_pixbuf(synfig::ValueBase::Type type)
332 {
333         //return Gtk::Button().render_icon(value_icon(type),Gtk::ICON_SIZE_SMALL_TOOLBAR);
334         return _tree_pixbuf_table_value_type[int(type)];
335 }
336
337 Gtk::StockID
338 studio::get_action_stock_id(const synfigapp::Action::BookEntry& action)
339 {
340         Gtk::StockID stock_id;
341         if(action.task=="add")                          stock_id=Gtk::Stock::ADD;
342         else if(action.task=="connect")         stock_id=Gtk::Stock::CONNECT;
343         else if(action.task=="disconnect")      stock_id=Gtk::Stock::DISCONNECT;
344         else if(action.task=="insert")          stock_id=Gtk::Stock::ADD;
345         else if(action.task=="lower")           stock_id=Gtk::Stock::GO_DOWN;
346         else if(action.task=="move_bottom")     stock_id=Gtk::Stock::GOTO_BOTTOM;
347         else if(action.task=="move_top")        stock_id=Gtk::Stock::GOTO_TOP;
348         else if(action.task=="raise")           stock_id=Gtk::Stock::GO_UP;
349         else if(action.task=="remove")          stock_id=Gtk::Stock::DELETE;
350         else if(action.task=="set_off")         stock_id=Gtk::Stock::NO;
351         else if(action.task=="set_on")          stock_id=Gtk::Stock::YES;
352         else                                                            stock_id=Gtk::StockID("synfig-"+
353                                                                                                                           action.task);
354         return stock_id;
355 }
356
357 Gtk::StockID
358 studio::layer_icon(const synfig::String &layer)
359 {
360         if(layer=="PasteCanvas" || layer=="pastecanvas" || layer=="paste_canvas")
361                 return Gtk::StockID("synfig-layer_pastecanvas");
362         else if(layer=="rotate")
363                 return Gtk::StockID("synfig-rotate");
364         else if(layer=="zoom")
365                 return Gtk::StockID("synfig-zoom");
366         else if(layer=="region")
367                 return Gtk::StockID("synfig-bline");
368         else if(layer=="polygon")
369                 return Gtk::StockID("synfig-polygon");
370         else if(layer=="outline")
371                 return Gtk::StockID("synfig-width");
372         else if(layer=="circle")
373                 return Gtk::StockID("synfig-circle");
374         else if(layer=="rectangle")
375                 return Gtk::StockID("synfig-rectangle");
376         else if(layer.find("gradient")!=String::npos)
377                 return Gtk::StockID("synfig-gradient");
378         else
379                 return Gtk::StockID("synfig-layer");
380 }
381
382 Glib::RefPtr<Gdk::Pixbuf>
383 studio::get_tree_pixbuf_layer(const synfig::String &layer)
384 {
385         return Gtk::Button().render_icon(layer_icon(layer),Gtk::ICON_SIZE_SMALL_TOOLBAR);
386 }