Fix a crash when running single-threaded and dragging the time slider.
[synfig.git] / synfig-studio / trunk / src / gtkmm / dockmanager.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file dockmanager.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 "dockmanager.h"
34 #include <stdexcept>
35 #include "dockable.h"
36 #include "dockdialog.h"
37 #include <synfigapp/settings.h>
38 #include <synfigapp/main.h>
39 #include <gdkmm/general.h>
40
41 #endif
42
43 /* === U S I N G =========================================================== */
44
45 using namespace std;
46 using namespace etl;
47 using namespace synfig;
48 using namespace studio;
49
50 /* === M A C R O S ========================================================= */
51
52 /* === P R O C E D U R E S ================================================= */
53
54 class studio::DockSettings : public synfigapp::Settings
55 {
56         DockManager* dock_manager;
57
58 public:
59         DockSettings(DockManager* dock_manager):dock_manager(dock_manager)
60         {
61                 synfigapp::Main::settings().add_domain(this,"dock");
62         }
63
64         virtual ~DockSettings()
65         {
66                 synfigapp::Main::settings().remove_domain("dock");
67         }
68 #define SCALE_FACTOR    (1280)
69         virtual bool get_value(const synfig::String& key_, synfig::String& value)const
70         {
71                 int screen_w(Gdk::screen_width());
72                 int screen_h(Gdk::screen_height());
73
74                 if(key_.size()>6 && String(key_.begin(),key_.begin()+6)=="dialog")try
75                 {
76                         synfig::String key(key_.begin()+7,key_.end());
77                         synfig::String::size_type separator=key.find_first_of('.');
78                         int id(atoi(synfig::String(key.begin(),key.begin()+separator).c_str()));
79                         key=synfig::String(key.begin()+separator+1,key.end());
80
81                         DockDialog& dock_dialog(dock_manager->find_dock_dialog(id));
82
83                         if(key=="contents_size")
84                         {
85                                 dock_dialog.rebuild_sizes();
86                                 vector<int>::const_iterator iter(dock_dialog.get_dock_book_sizes().begin());
87                                 vector<int>::const_iterator end(dock_dialog.get_dock_book_sizes().end());
88                                 value.clear();
89                                 for(;iter!=end;++iter)
90                                         value+=strprintf("%d ",(*iter)*SCALE_FACTOR/screen_h);
91                                 return true;
92                         }
93                         if(key=="pos")
94                         {
95                                 int x,y; dock_dialog.get_position(x,y);
96                                 value=strprintf("%d %d",x*SCALE_FACTOR/screen_w,y*SCALE_FACTOR/screen_h);
97                                 return true;
98                         }
99                         if(key=="size")
100                         {
101                                 int x,y; dock_dialog.get_size(x,y);
102                                 value=strprintf("%d %d",x*SCALE_FACTOR/screen_w,y*SCALE_FACTOR/screen_h);
103                                 return true;
104                         }
105                         if(key=="contents")
106                         {
107                                 value=dock_dialog.get_contents();
108                                 return true;
109                         }
110                         if(key=="comp_selector")
111                         {
112                                 value=dock_dialog.get_composition_selector()?"1":"0";
113                                 return true;
114                         }
115                 }catch (...) { return false; }
116                 return synfigapp::Settings::get_value(key_,value);
117         }
118
119         virtual bool set_value(const synfig::String& key_,const synfig::String& value)
120         {
121                 int screen_w(Gdk::screen_width());
122                 int screen_h(Gdk::screen_height());
123
124                 if(key_.size()>6 && String(key_.begin(),key_.begin()+6)=="dialog")
125                 {
126                         synfig::String key(key_.begin()+7,key_.end());
127                         synfig::String::size_type separator=key.find_first_of('.');
128                         int id(atoi(synfig::String(key.begin(),key.begin()+separator).c_str()));
129                         key=synfig::String(key.begin()+separator+1,key.end());
130
131                         DockDialog& dock_dialog(dock_manager->find_dock_dialog(id));
132
133                         if(key=="contents_size")
134                         {
135                                 try {
136
137                                 vector<int> data;
138                                 String::size_type n=0;
139                                 String value_(value);
140                                 while(value_.size() && value_.size()>n){
141                                         value_=String(value_.begin()+n,value_.end());
142                                         int size;
143                                         if(!strscanf(value_,"%d",&size))
144                                                 break;
145                                         size=size*screen_h/SCALE_FACTOR;
146                                         data.push_back(size);
147
148                                         n=value_.find(" ");
149                                         if(n==String::npos)
150                                                 break;
151                                         n++;
152                                 }
153                                 dock_dialog.set_dock_book_sizes(data);
154                                 }
155                                 catch(...)
156                                 {
157                                         synfig::error("Exception caught!!!");
158                                         return false;
159                                 }
160                                 return true;
161                         }
162                         if(key=="pos")
163                         {
164                                 int x,y;
165                                 if(!strscanf(value,"%d %d",&x, &y))
166                                         return false;
167                                 x=x*screen_w/SCALE_FACTOR;
168                                 y=y*screen_h/SCALE_FACTOR;
169                                 dock_dialog.move(x,y);
170                                 return true;
171                         }
172                         if(key=="size")
173                         {
174                                 int x,y;
175                                 if(!strscanf(value,"%d %d",&x, &y))
176                                         return false;
177                                 x=x*screen_w/SCALE_FACTOR;
178                                 y=y*screen_h/SCALE_FACTOR;
179                                 dock_dialog.set_default_size(x,y);
180                                 dock_dialog.resize(x,y);
181                                 return true;
182                         }
183                         if(key=="contents")
184                         {
185                                 dock_dialog.set_contents(value);
186                                 return true;
187                         }
188                         if(key=="comp_selector")
189                         {
190                                 if(value.empty() || value[0]=='0')
191                                         dock_dialog.set_composition_selector(false);
192                                 else
193                                         dock_dialog.set_composition_selector(true);
194                                 return true;
195                         }
196                 }
197                 return synfigapp::Settings::set_value(key_,value);
198         }
199
200         virtual KeyList get_key_list()const
201         {
202                 synfigapp::Settings::KeyList ret(synfigapp::Settings::get_key_list());
203
204                 std::list<DockDialog*>::const_iterator iter;
205                 for(iter=dock_manager->dock_dialog_list_.begin();iter!=dock_manager->dock_dialog_list_.end();++iter)
206                 {
207                         ret.push_back(strprintf("dialog.%d.contents",(*iter)->get_id()));
208                         ret.push_back(strprintf("dialog.%d.comp_selector",(*iter)->get_id()));
209                         ret.push_back(strprintf("dialog.%d.pos",(*iter)->get_id()));
210                         ret.push_back(strprintf("dialog.%d.size",(*iter)->get_id()));
211                         ret.push_back(strprintf("dialog.%d.contents_size",(*iter)->get_id()));
212                 }
213                 return ret;
214         }
215 };
216
217 /* === M E T H O D S ======================================================= */
218
219 DockManager::DockManager():
220         dock_settings(new DockSettings(this))
221 {
222 }
223
224 DockManager::~DockManager()
225 {
226         while(!dock_dialog_list_.empty())
227         {
228                 dock_dialog_list_.back()->close();
229         }
230         while(!dockable_list_.empty())
231         {
232                 Dockable* dockable(dockable_list_.back());
233                 // synfig::info("DockManager::~DockManager(): Deleting dockable \"%s\"",dockable->get_name().c_str());
234                 dockable_list_.pop_back();
235                 delete dockable;
236         }
237 }
238
239 void
240 DockManager::register_dockable(Dockable& x)
241 {
242         dockable_list_.push_back(&x);
243         // synfig::info("DockManager::register_dockable(): Registered dockable \"%s\"",dockable_list_.back()->get_name().c_str());
244         signal_dockable_registered()(&x);
245 }
246
247 bool
248 DockManager::unregister_dockable(Dockable& x)
249 {
250         std::list<Dockable*>::iterator iter;
251         for(iter=dockable_list_.begin();iter!=dockable_list_.end();++iter)
252         {
253                 if(&x==*iter)
254                 {
255                         x.detach();
256                         dockable_list_.erase(iter);
257                         synfig::info("DockManager::unregister_dockable(): \"%s\" has been Unregistered",x.get_name().c_str());
258                         return true;
259                 }
260         }
261         return false;
262 }
263
264 Dockable&
265 DockManager::find_dockable(const synfig::String& x)
266 {
267         std::list<Dockable*>::iterator iter;
268         for(iter=dockable_list_.begin();iter!=dockable_list_.end();++iter)
269                 if((*iter)->get_name()==x)
270                         return **iter;
271
272         throw std::runtime_error("DockManager::find_dockable(): not found");
273 }
274
275 void
276 DockManager::present(synfig::String x)
277 {
278         try
279         {
280                 find_dockable(x).present();
281         }
282         catch(...)
283         {
284         }
285 }
286
287 DockDialog&
288 DockManager::find_dock_dialog(int id)
289 {
290         std::list<DockDialog*>::iterator iter;
291         for(iter=dock_dialog_list_.begin();iter!=dock_dialog_list_.end();++iter)
292                 if((*iter)->get_id()==id)
293                         return **iter;
294
295         DockDialog* dock_dialog(new DockDialog());
296         dock_dialog->set_id(id);
297         dock_dialog->show();
298         return *dock_dialog;
299 }
300
301 const DockDialog&
302 DockManager::find_dock_dialog(int id)const
303 {
304         std::list<DockDialog*>::const_iterator iter;
305         for(iter=dock_dialog_list_.begin();iter!=dock_dialog_list_.end();++iter)
306                 if((*iter)->get_id()==id)
307                         return **iter;
308
309         throw std::runtime_error("DockManager::find_dock_dialog(int id)const: not found");
310 }