Documentation for target_scanline.h
[synfig.git] / synfig-studio / src / gui / docks / 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 "docks/dockmanager.h"
34 #include <stdexcept>
35 #include "docks/dockable.h"
36 #include "docks/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                                 //synfig::info("dock_manager. move to: %d, %d", x,y);
166                                 dock_dialog.move(x,y);
167                                 return true;
168                         }
169                         if(key=="size")
170                         {
171                                 int x,y;
172                                 if(!strscanf(value,"%d %d",&x, &y))
173                                         return false;
174                                 //synfig::info("dock_manager. size to: %d, %d", x,y);
175                                 dock_dialog.set_default_size(x,y);
176                                 dock_dialog.resize(x,y);
177                                 return true;
178                         }
179                         if(key=="contents")
180                         {
181                                 dock_dialog.set_contents(value);
182                                 return true;
183                         }
184                         if(key=="comp_selector")
185                         {
186                                 if(value.empty() || value[0]=='0')
187                                         dock_dialog.set_composition_selector(false);
188                                 else
189                                         dock_dialog.set_composition_selector(true);
190                                 return true;
191                         }
192                 }
193                 return synfigapp::Settings::set_value(key_,value);
194         }
195
196         virtual KeyList get_key_list()const
197         {
198                 synfigapp::Settings::KeyList ret(synfigapp::Settings::get_key_list());
199
200                 std::list<DockDialog*>::const_iterator iter;
201                 for(iter=dock_manager->dock_dialog_list_.begin();iter!=dock_manager->dock_dialog_list_.end();++iter)
202                 {
203                         ret.push_back(strprintf("dialog.%d.contents",(*iter)->get_id()));
204                         ret.push_back(strprintf("dialog.%d.comp_selector",(*iter)->get_id()));
205                         ret.push_back(strprintf("dialog.%d.pos",(*iter)->get_id()));
206                         ret.push_back(strprintf("dialog.%d.size",(*iter)->get_id()));
207                         ret.push_back(strprintf("dialog.%d.contents_size",(*iter)->get_id()));
208                 }
209                 return ret;
210         }
211 };
212
213 /* === M E T H O D S ======================================================= */
214
215 DockManager::DockManager():
216         dock_settings(new DockSettings(this))
217 {
218 }
219
220 DockManager::~DockManager()
221 {
222         while(!dock_dialog_list_.empty())
223         {
224                 dock_dialog_list_.back()->close();
225         }
226         while(!dockable_list_.empty())
227         {
228                 Dockable* dockable(dockable_list_.back());
229                 // synfig::info("DockManager::~DockManager(): Deleting dockable \"%s\"",dockable->get_name().c_str());
230                 dockable_list_.pop_back();
231                 delete dockable;
232         }
233 }
234
235 void
236 DockManager::register_dockable(Dockable& x)
237 {
238         dockable_list_.push_back(&x);
239         // synfig::info("DockManager::register_dockable(): Registered dockable \"%s\"",dockable_list_.back()->get_name().c_str());
240         signal_dockable_registered()(&x);
241 }
242
243 bool
244 DockManager::unregister_dockable(Dockable& x)
245 {
246         std::list<Dockable*>::iterator iter;
247         for(iter=dockable_list_.begin();iter!=dockable_list_.end();++iter)
248         {
249                 if(&x==*iter)
250                 {
251                         x.detach();
252                         dockable_list_.erase(iter);
253                         synfig::info("DockManager::unregister_dockable(): \"%s\" has been Unregistered",x.get_name().c_str());
254                         return true;
255                 }
256         }
257         return false;
258 }
259
260 Dockable&
261 DockManager::find_dockable(const synfig::String& x)
262 {
263         std::list<Dockable*>::iterator iter;
264         for(iter=dockable_list_.begin();iter!=dockable_list_.end();++iter)
265                 if((*iter)->get_name()==x)
266                         return **iter;
267
268         throw std::runtime_error("DockManager::find_dockable(): not found");
269 }
270
271 void
272 DockManager::present(synfig::String x)
273 {
274         try
275         {
276                 find_dockable(x).present();
277         }
278         catch(...)
279         {
280         }
281 }
282
283 DockDialog&
284 DockManager::find_dock_dialog(int id)
285 {
286         std::list<DockDialog*>::iterator iter;
287         for(iter=dock_dialog_list_.begin();iter!=dock_dialog_list_.end();++iter)
288                 if((*iter)->get_id()==id)
289                         return **iter;
290
291         DockDialog* dock_dialog(new DockDialog());
292         dock_dialog->set_id(id);
293         return *dock_dialog;
294 }
295
296 const DockDialog&
297 DockManager::find_dock_dialog(int id)const
298 {
299         std::list<DockDialog*>::const_iterator iter;
300         for(iter=dock_dialog_list_.begin();iter!=dock_dialog_list_.end();++iter)
301                 if((*iter)->get_id()==id)
302                         return **iter;
303
304         throw std::runtime_error("DockManager::find_dock_dialog(int id)const: not found");
305 }
306
307 void
308 DockManager::show_all_dock_dialogs()
309 {
310         std::list<DockDialog*>::iterator iter;
311         for(iter=dock_dialog_list_.begin();iter!=dock_dialog_list_.end();++iter)
312                 (*iter)->present();
313 }