my log
[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: widget_compselect.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include <gtkmm/menu.h>
32 #include "widget_compselect.h"
33 #include <ETL/stringf>
34 #include <synfig/valuenode.h>
35 #include "instance.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_CompSelect::Widget_CompSelect()
55 {
56         App::signal_instance_created().connect(sigc::mem_fun(*this,&studio::Widget_CompSelect::new_instance));
57         App::signal_instance_deleted().connect(sigc::mem_fun(*this,&studio::Widget_CompSelect::delete_instance));
58         App::signal_instance_selected().connect(sigc::mem_fun(*this,&studio::Widget_CompSelect::set_selected_instance_signal));
59
60         set_menu(instance_list_menu);
61         refresh();
62 }
63
64 Widget_CompSelect::~Widget_CompSelect()
65 {
66 }
67
68 void
69 Widget_CompSelect::set_selected_instance_signal(etl::handle<studio::Instance> x)
70 {
71         set_selected_instance(x);
72 }
73
74 void
75 Widget_CompSelect::set_selected_instance_(etl::handle<studio::Instance> instance)
76 {
77         if(studio::App::shutdown_in_progress)
78                 return;
79
80         selected_instance=instance;
81 }
82
83 void
84 Widget_CompSelect::set_selected_instance(etl::loose_handle<studio::Instance> x)
85 {
86         if(studio::App::shutdown_in_progress)
87                 return;
88
89         // if it's already selected, don't select it again
90         if (x==selected_instance)
91                 return;
92
93         std::list<etl::handle<studio::Instance> >::iterator iter;
94
95         if(x)
96         {
97                 int i;
98                 for(i=0,iter=studio::App::instance_list.begin();iter!=studio::App::instance_list.end() && ((*iter)!=x);iter++,i++);
99
100                 assert(*iter==x);
101
102                 set_history(i);
103         }
104         else
105                 set_history(0);
106
107         set_selected_instance_(x);      
108 }
109
110 void
111 Widget_CompSelect::new_instance(etl::handle<studio::Instance> instance)
112 {
113         if(studio::App::shutdown_in_progress)
114                 return;
115         
116         assert(instance);
117         
118         etl::loose_handle<studio::Instance> loose_instance(instance);
119         
120         instance->synfigapp::Instance::signal_filename_changed().connect(sigc::mem_fun(*this,&Widget_CompSelect::refresh));
121         instance->synfigapp::Instance::signal_filename_changed().connect(
122                 sigc::bind<etl::loose_handle<studio::Instance> >(
123                         sigc::mem_fun(*this,&Widget_CompSelect::set_selected_instance),
124                         loose_instance
125                 )
126         );
127
128         {
129                 std::string name=basename(instance->get_file_name());
130
131                 instance_list_menu.items().push_back(Gtk::Menu_Helpers::MenuElem(name,
132                         sigc::bind<etl::loose_handle<studio::Instance> >(sigc::ptr_fun(&studio::App::set_selected_instance),loose_instance)     ));
133         }
134         
135 }
136
137 void
138 Widget_CompSelect::delete_instance(etl::handle<studio::Instance> instance)
139 {
140         DEBUGPOINT();
141         refresh();
142
143         if(selected_instance==instance)
144         {
145                 set_selected_instance(0);
146                 set_history(0);
147         }
148 }
149
150 void
151 Widget_CompSelect::refresh()
152 {
153         remove_menu();
154
155         if(!instance_list_menu.items().empty())
156                 instance_list_menu.items().clear();
157
158         if(studio::App::shutdown_in_progress)
159                 return;
160
161         std::list<etl::handle<studio::Instance> >::iterator iter;
162         for(iter=studio::App::instance_list.begin();iter!=studio::App::instance_list.end();iter++)
163         {
164                 std::string name=basename((*iter)->get_file_name());
165
166                 instance_list_menu.items().push_back(Gtk::Menu_Helpers::MenuElem(name,
167                         sigc::bind<etl::loose_handle<studio::Instance> >(sigc::ptr_fun(&studio::App::set_selected_instance),*iter)      ));
168         }
169         set_menu(instance_list_menu);
170 }