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 / widget_compselect.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file widget_compselect.cpp
3 **      \brief Template File
4 **
5 **      $Id: widget_compselect.cpp,v 1.1.1.1 2005/01/07 03:34:37 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 <gtkmm/menu.h>
33 #include "widget_compselect.h"
34 #include <ETL/stringf>
35 #include <synfig/valuenode.h>
36 #include "instance.h"
37
38 #endif
39
40 /* === U S I N G =========================================================== */
41
42 using namespace std;
43 using namespace etl;
44 using namespace synfig;
45 using namespace studio;
46
47 /* === M A C R O S ========================================================= */
48
49 /* === G L O B A L S ======================================================= */
50
51 /* === P R O C E D U R E S ================================================= */
52
53 /* === M E T H O D S ======================================================= */
54
55 Widget_CompSelect::Widget_CompSelect()
56 {
57         App::signal_instance_created().connect(sigc::mem_fun(*this,&studio::Widget_CompSelect::new_instance));
58         App::signal_instance_deleted().connect(sigc::mem_fun(*this,&studio::Widget_CompSelect::delete_instance));
59         App::signal_instance_selected().connect(sigc::mem_fun(*this,&studio::Widget_CompSelect::set_selected_instance_signal));
60
61         set_menu(instance_list_menu);
62         refresh();
63 }
64
65 Widget_CompSelect::~Widget_CompSelect()
66 {
67 }
68
69 void
70 Widget_CompSelect::set_selected_instance_signal(etl::handle<studio::Instance> x)
71 {
72         set_selected_instance(x);
73 }
74
75 void
76 Widget_CompSelect::set_selected_instance_(etl::handle<studio::Instance> instance)
77 {
78         if(studio::App::shutdown_in_progress)
79                 return;
80
81         selected_instance=instance;
82 }
83
84 void
85 Widget_CompSelect::set_selected_instance(etl::loose_handle<studio::Instance> x)
86 {
87         if(studio::App::shutdown_in_progress)
88                 return;
89
90         // if it's already selected, don't select it again
91         if (x==selected_instance)
92                 return;
93
94         std::list<etl::handle<studio::Instance> >::iterator iter;
95
96         if(x)
97         {
98                 int i;
99                 for(i=0,iter=studio::App::instance_list.begin();iter!=studio::App::instance_list.end() && ((*iter)!=x);iter++,i++);
100
101                 assert(*iter==x);
102
103                 set_history(i);
104         }
105         else
106                 set_history(0);
107
108         set_selected_instance_(x);      
109 }
110
111 void
112 Widget_CompSelect::new_instance(etl::handle<studio::Instance> instance)
113 {
114         if(studio::App::shutdown_in_progress)
115                 return;
116         
117         assert(instance);
118         
119         etl::loose_handle<studio::Instance> loose_instance(instance);
120         
121         instance->synfigapp::Instance::signal_filename_changed().connect(sigc::mem_fun(*this,&Widget_CompSelect::refresh));
122         instance->synfigapp::Instance::signal_filename_changed().connect(
123                 sigc::bind<etl::loose_handle<studio::Instance> >(
124                         sigc::mem_fun(*this,&Widget_CompSelect::set_selected_instance),
125                         loose_instance
126                 )
127         );
128
129         {
130                 std::string name=basename(instance->get_file_name());
131
132                 instance_list_menu.items().push_back(Gtk::Menu_Helpers::MenuElem(name,
133                         sigc::bind<etl::loose_handle<studio::Instance> >(sigc::ptr_fun(&studio::App::set_selected_instance),loose_instance)     ));
134         }
135         
136 }
137
138 void
139 Widget_CompSelect::delete_instance(etl::handle<studio::Instance> instance)
140 {
141         DEBUGPOINT();
142         refresh();
143
144         if(selected_instance==instance)
145         {
146                 set_selected_instance(0);
147                 set_history(0);
148         }
149 }
150
151 void
152 Widget_CompSelect::refresh()
153 {
154         remove_menu();
155
156         if(!instance_list_menu.items().empty())
157                 instance_list_menu.items().clear();
158
159         if(studio::App::shutdown_in_progress)
160                 return;
161
162         std::list<etl::handle<studio::Instance> >::iterator iter;
163         for(iter=studio::App::instance_list.begin();iter!=studio::App::instance_list.end();iter++)
164         {
165                 std::string name=basename((*iter)->get_file_name());
166
167                 instance_list_menu.items().push_back(Gtk::Menu_Helpers::MenuElem(name,
168                         sigc::bind<etl::loose_handle<studio::Instance> >(sigc::ptr_fun(&studio::App::set_selected_instance),*iter)      ));
169         }
170         set_menu(instance_list_menu);
171 }