Implements PXEGeek's http://wiki.synfig.com/Wish_list entry: "Optionally display...
[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 **
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 "widget_canvaschooser.h"
33 #include <gtkmm/menu.h>
34 #include "app.h"
35
36 #endif
37
38 /* === U S I N G =========================================================== */
39
40 using namespace std;
41 using namespace etl;
42 using namespace synfig;
43 using namespace studio;
44
45 /* === M A C R O S ========================================================= */
46
47 /* === G L O B A L S ======================================================= */
48
49 /* === P R O C E D U R E S ================================================= */
50
51 /* === M E T H O D S ======================================================= */
52
53 Widget_CanvasChooser::Widget_CanvasChooser()
54 {
55 }
56
57 Widget_CanvasChooser::~Widget_CanvasChooser()
58 {
59 }
60
61 void
62 Widget_CanvasChooser::set_parent_canvas(etl::handle<synfig::Canvas> x)
63 {
64         assert(x);
65         parent_canvas=x;
66 }
67
68 void
69 Widget_CanvasChooser::set_value_(etl::handle<synfig::Canvas> data)
70 {
71         set_value(data);
72         activate();
73 }
74
75 void
76 Widget_CanvasChooser::set_value(etl::handle<synfig::Canvas> data)
77 {
78         assert(parent_canvas);
79         canvas=data;
80
81         canvas_menu=manage(new class Gtk::Menu());
82
83         synfig::Canvas::Children::iterator iter;
84         synfig::Canvas::Children &children(parent_canvas->children());
85         String label;
86
87         if(canvas)
88         {
89                 label=canvas->get_name().empty()?canvas->get_id():canvas->get_name();
90                 canvas_menu->items().push_back(Gtk::Menu_Helpers::MenuElem(label));
91         }
92
93         for(iter=children.begin();iter!=children.end();iter++)
94                 if(*iter!=canvas)
95                 {
96                         label=(*iter)->get_name().empty()?(*iter)->get_id():(*iter)->get_name();
97                         canvas_menu->items().push_back(
98                                 Gtk::Menu_Helpers::MenuElem(
99                                         label,
100                                         sigc::bind(
101                                                 sigc::mem_fun(
102                                                         *this,
103                                                         &Widget_CanvasChooser::set_value_
104                                                 ),
105                                                 *iter
106                                         )
107                                 )
108                         );
109                 }
110         canvas_menu->items().push_back(
111                 Gtk::Menu_Helpers::MenuElem(
112                         _("Other..."),
113                         sigc::mem_fun(*this,&Widget_CanvasChooser::chooser_menu)
114                 )
115         );
116         set_menu(*canvas_menu);
117
118         if(canvas)
119                 set_history(0);
120 }
121
122 const etl::handle<synfig::Canvas> &
123 Widget_CanvasChooser::get_value()
124 {
125         return canvas;
126 }
127
128 void
129 Widget_CanvasChooser::chooser_menu()
130 {
131         String canvas_name;
132         App::dialog_entry(_("Choose Canvas"),_("Enter the relative name of the canvas that you want"),canvas_name);
133         Canvas::Handle new_canvas;
134         try
135         {
136                 new_canvas=parent_canvas->find_canvas(canvas_name);
137                 set_value_(new_canvas);
138         }
139         catch(std::runtime_error x)
140         {
141                 App::dialog_error_blocking(_("Error:Exception Thrown"),x.what());
142                 set_value_(canvas);
143         }
144         catch(...)
145         {
146                 App::dialog_error_blocking(_("Error"),_("Unknown Exception"));
147                 set_value_(canvas);
148         }
149 }