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