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