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