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