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