Fix a crash when running single-threaded and dragging the time slider.
[synfig.git] / synfig-studio / trunk / src / gtkmm / widget_canvaschooser.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file widget_canvaschooser.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 "widget_canvaschooser.h"
34 #include <gtkmm/menu.h>
35 #include "app.h"
36
37 #endif
38
39 /* === U S I N G =========================================================== */
40
41 using namespace std;
42 using namespace etl;
43 using namespace synfig;
44 using namespace studio;
45
46 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 /* === P R O C E D U R E S ================================================= */
51
52 /* === M E T H O D S ======================================================= */
53
54 Widget_CanvasChooser::Widget_CanvasChooser()
55 {
56 }
57
58 Widget_CanvasChooser::~Widget_CanvasChooser()
59 {
60 }
61
62 void
63 Widget_CanvasChooser::set_parent_canvas(etl::handle<synfig::Canvas> x)
64 {
65         assert(x);
66         parent_canvas=x;
67 }
68
69 void
70 Widget_CanvasChooser::set_value_(etl::handle<synfig::Canvas> data)
71 {
72         set_value(data);
73         activate();
74 }
75
76 void
77 Widget_CanvasChooser::set_value(etl::handle<synfig::Canvas> data)
78 {
79         assert(parent_canvas);
80         canvas=data;
81
82         canvas_menu=manage(new class Gtk::Menu());
83
84         synfig::Canvas::Children::iterator iter;
85         synfig::Canvas::Children &children(parent_canvas->children());
86         String label;
87
88         if(canvas)
89         {
90                 label=canvas->get_name().empty()?canvas->get_id():canvas->get_name();
91                 canvas_menu->items().push_back(Gtk::Menu_Helpers::MenuElem(label));
92         }
93
94         for(iter=children.begin();iter!=children.end();iter++)
95                 if(*iter!=canvas)
96                 {
97                         label=(*iter)->get_name().empty()?(*iter)->get_id():(*iter)->get_name();
98                         canvas_menu->items().push_back(
99                                 Gtk::Menu_Helpers::MenuElem(
100                                         label,
101                                         sigc::bind(
102                                                 sigc::mem_fun(
103                                                         *this,
104                                                         &Widget_CanvasChooser::set_value_
105                                                 ),
106                                                 *iter
107                                         )
108                                 )
109                         );
110                 }
111         canvas_menu->items().push_back(
112                 Gtk::Menu_Helpers::MenuElem(
113                         _("Other..."),
114                         sigc::mem_fun(*this,&Widget_CanvasChooser::chooser_menu)
115                 )
116         );
117         set_menu(*canvas_menu);
118
119         if(canvas)
120                 set_history(0);
121 }
122
123 const etl::handle<synfig::Canvas> &
124 Widget_CanvasChooser::get_value()
125 {
126         return canvas;
127 }
128
129 void
130 Widget_CanvasChooser::chooser_menu()
131 {
132         String canvas_name;
133
134         if (!App::dialog_entry(_("Choose Canvas"),_("Enter the relative name of the canvas that you want"),canvas_name))
135         {
136                 // the user hit 'cancel', so set the parameter back to its previous value
137                 set_value_(canvas);
138                 return;
139         }
140
141         if (canvas_name == "")
142         {
143                 App::dialog_error_blocking(_("Error"),_("No canvas name was specified"));
144                 set_value_(canvas);
145                 return;
146         }
147                 
148         Canvas::Handle new_canvas;
149         try
150         {
151                 new_canvas=parent_canvas->find_canvas(canvas_name);
152                 set_value_(new_canvas);
153         }
154         catch(std::runtime_error x)
155         {
156                 App::dialog_error_blocking(_("Error:Exception Thrown"),x.what());
157                 set_value_(canvas);
158         }
159         catch(...)
160         {
161                 App::dialog_error_blocking(_("Error"),_("Unknown Exception"));
162                 set_value_(canvas);
163         }
164 }