1 /* === S Y N F I G ========================================================= */
2 /*! \file dockmanager.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include "dockmanager.h"
36 #include "dockdialog.h"
37 #include <synfigapp/settings.h>
38 #include <synfigapp/main.h>
39 #include <gdkmm/general.h>
45 /* === U S I N G =========================================================== */
49 using namespace synfig;
50 using namespace studio;
52 /* === M A C R O S ========================================================= */
54 /* === P R O C E D U R E S ================================================= */
56 class studio::DockSettings : public synfigapp::Settings
58 DockManager* dock_manager;
61 DockSettings(DockManager* dock_manager):dock_manager(dock_manager)
63 synfigapp::Main::settings().add_domain(this,"dock");
66 virtual ~DockSettings()
68 synfigapp::Main::settings().remove_domain("dock");
70 #define SCALE_FACTOR (1280)
71 virtual bool get_value(const synfig::String& key_, synfig::String& value)const
73 int screen_w(Gdk::screen_width());
74 int screen_h(Gdk::screen_height());
76 if(key_.size()>6 && String(key_.begin(),key_.begin()+6)=="dialog")try
78 synfig::String key(key_.begin()+7,key_.end());
79 synfig::String::size_type separator=key.find_first_of('.');
80 int id(atoi(synfig::String(key.begin(),key.begin()+separator).c_str()));
81 key=synfig::String(key.begin()+separator+1,key.end());
83 DockDialog& dock_dialog(dock_manager->find_dock_dialog(id));
85 if(key=="contents_size")
87 dock_dialog.rebuild_sizes();
88 vector<int>::const_iterator iter(dock_dialog.get_dock_book_sizes().begin());
89 vector<int>::const_iterator end(dock_dialog.get_dock_book_sizes().end());
91 for(;iter!=end;++iter)
92 value+=strprintf("%d ",(*iter)*SCALE_FACTOR/screen_h);
97 int x,y; dock_dialog.get_position(x,y);
98 value=strprintf("%d %d",x*SCALE_FACTOR/screen_w,y*SCALE_FACTOR/screen_h);
103 int x,y; dock_dialog.get_size(x,y);
104 value=strprintf("%d %d",x*SCALE_FACTOR/screen_w,y*SCALE_FACTOR/screen_h);
109 value=dock_dialog.get_contents();
112 if(key=="comp_selector")
114 value=dock_dialog.get_composition_selector()?"1":"0";
117 }catch (...) { return false; }
118 return synfigapp::Settings::get_value(key_,value);
121 virtual bool set_value(const synfig::String& key_,const synfig::String& value)
123 int screen_w(Gdk::screen_width());
124 int screen_h(Gdk::screen_height());
126 if(key_.size()>6 && String(key_.begin(),key_.begin()+6)=="dialog")
128 synfig::String key(key_.begin()+7,key_.end());
129 synfig::String::size_type separator=key.find_first_of('.');
130 int id(atoi(synfig::String(key.begin(),key.begin()+separator).c_str()));
131 key=synfig::String(key.begin()+separator+1,key.end());
133 DockDialog& dock_dialog(dock_manager->find_dock_dialog(id));
135 if(key=="contents_size")
139 Gtk::IconSize::lookup(Gtk::IconSize(4),width,height);
141 String::size_type n=0;
142 String value_(value);
143 while(value_.size() && value_.size()>n){
144 value_=String(value_.begin()+n,value_.end());
146 if(!strscanf(value_,"%d",&size))
148 if (size > SCALE_FACTOR) size = SCALE_FACTOR - 150;
149 if (size < 0) size = 0;
150 size=size*screen_h/SCALE_FACTOR;
152 // prevent errors like this, by allowing space for at least the dockable's icon:
153 // ** CRITICAL **: clearlooks_style_draw_box_gap: assertion `height >= -1' failed
154 if (size < height + 9) size = height + 9;
156 data.push_back(size);
163 dock_dialog.set_dock_book_sizes(data);
167 synfig::error("Exception caught!!!");
175 if(!strscanf(value,"%d %d",&x, &y))
177 if (x > SCALE_FACTOR) x = SCALE_FACTOR - 150; if (x < 0) x = 0;
178 if (y > SCALE_FACTOR) y = SCALE_FACTOR - 150; if (y < 0) y = 0;
179 x=x*screen_w/SCALE_FACTOR;
180 y=y*screen_h/SCALE_FACTOR;
181 if(getenv("SYNFIG_WINDOW_POSITION_X_OFFSET"))
182 x += atoi(getenv("SYNFIG_WINDOW_POSITION_X_OFFSET"));
183 if(getenv("SYNFIG_WINDOW_POSITION_Y_OFFSET"))
184 y += atoi(getenv("SYNFIG_WINDOW_POSITION_Y_OFFSET"));
185 dock_dialog.move(x,y);
191 if(!strscanf(value,"%d %d",&x, &y))
193 if (x > SCALE_FACTOR) x = 150; if (x < 0) x = 0;
194 if (y > SCALE_FACTOR) y = 150; if (y < 0) y = 0;
195 x=x*screen_w/SCALE_FACTOR;
196 y=y*screen_h/SCALE_FACTOR;
197 dock_dialog.set_default_size(x,y);
198 dock_dialog.resize(x,y);
203 dock_dialog.set_contents(value);
206 if(key=="comp_selector")
208 if(value.empty() || value[0]=='0')
209 dock_dialog.set_composition_selector(false);
211 dock_dialog.set_composition_selector(true);
215 return synfigapp::Settings::set_value(key_,value);
218 virtual KeyList get_key_list()const
220 synfigapp::Settings::KeyList ret(synfigapp::Settings::get_key_list());
222 std::list<DockDialog*>::const_iterator iter;
223 for(iter=dock_manager->dock_dialog_list_.begin();iter!=dock_manager->dock_dialog_list_.end();++iter)
225 ret.push_back(strprintf("dialog.%d.contents",(*iter)->get_id()));
226 ret.push_back(strprintf("dialog.%d.comp_selector",(*iter)->get_id()));
227 ret.push_back(strprintf("dialog.%d.pos",(*iter)->get_id()));
228 ret.push_back(strprintf("dialog.%d.size",(*iter)->get_id()));
229 ret.push_back(strprintf("dialog.%d.contents_size",(*iter)->get_id()));
235 /* === M E T H O D S ======================================================= */
237 DockManager::DockManager():
238 dock_settings(new DockSettings(this))
242 DockManager::~DockManager()
244 while(!dock_dialog_list_.empty())
246 dock_dialog_list_.back()->close();
248 while(!dockable_list_.empty())
250 Dockable* dockable(dockable_list_.back());
251 // synfig::info("DockManager::~DockManager(): Deleting dockable \"%s\"",dockable->get_name().c_str());
252 dockable_list_.pop_back();
258 DockManager::register_dockable(Dockable& x)
260 dockable_list_.push_back(&x);
261 // synfig::info("DockManager::register_dockable(): Registered dockable \"%s\"",dockable_list_.back()->get_name().c_str());
262 signal_dockable_registered()(&x);
266 DockManager::unregister_dockable(Dockable& x)
268 std::list<Dockable*>::iterator iter;
269 for(iter=dockable_list_.begin();iter!=dockable_list_.end();++iter)
274 dockable_list_.erase(iter);
275 synfig::info("DockManager::unregister_dockable(): \"%s\" has been Unregistered",x.get_name().c_str());
283 DockManager::find_dockable(const synfig::String& x)
285 std::list<Dockable*>::iterator iter;
286 for(iter=dockable_list_.begin();iter!=dockable_list_.end();++iter)
287 if((*iter)->get_name()==x)
290 throw std::runtime_error("DockManager::find_dockable(): not found");
294 DockManager::present(synfig::String x)
298 find_dockable(x).present();
306 DockManager::find_dock_dialog(int id)
308 std::list<DockDialog*>::iterator iter;
309 for(iter=dock_dialog_list_.begin();iter!=dock_dialog_list_.end();++iter)
310 if((*iter)->get_id()==id)
313 DockDialog* dock_dialog(new DockDialog());
314 dock_dialog->set_id(id);
320 DockManager::find_dock_dialog(int id)const
322 std::list<DockDialog*>::const_iterator iter;
323 for(iter=dock_dialog_list_.begin();iter!=dock_dialog_list_.end();++iter)
324 if((*iter)->get_id()==id)
327 throw std::runtime_error("DockManager::find_dock_dialog(int id)const: not found");