acc61aef3e2c2949f1252e777ab27b2b27754e00
[synfig.git] / synfig-studio / trunk / src / gtkmm / dialogsettings.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file dialogsettings.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 "dialogsettings.h"
34 #include <synfigapp/main.h>
35 #include <gdkmm/general.h>
36
37 #include "general.h"
38
39 #endif
40
41 /* === U S I N G =========================================================== */
42
43 using namespace std;
44 using namespace etl;
45 using namespace synfig;
46 using namespace studio;
47
48 /* === M A C R O S ========================================================= */
49
50 /* === G L O B A L S ======================================================= */
51
52 /* === P R O C E D U R E S ================================================= */
53
54 /* === M E T H O D S ======================================================= */
55
56 DialogSettings::DialogSettings(Gtk::Window* window,const synfig::String& name):
57         window(window),
58         name(name)
59 {
60         synfigapp::Main::settings().add_domain(this,"window."+name);
61 }
62
63 DialogSettings::~DialogSettings()
64 {
65         synfigapp::Main::settings().remove_domain("window."+name);
66 }
67
68 bool
69 DialogSettings::get_value(const synfig::String& key, synfig::String& value)const
70 {
71         if(key=="pos")
72         {
73                 // if(!window->is_visible())return false;
74                 int x,y; window->get_position(x,y);
75                 value=strprintf("%d %d",x,y);
76                 return true;
77         }
78         if(key=="size")
79         {
80                 // if(!window->is_visible())return false;
81                 int x,y; window->get_size(x,y);
82                 value=strprintf("%d %d",x,y);
83                 return true;
84         }
85         if(key=="x")
86         {
87                 int x,y; window->get_position(x,y);
88                 value=strprintf("%d",x);
89                 return true;
90         }
91         if(key=="y")
92         {
93                 int x,y; window->get_position(x,y);
94                 value=strprintf("%d",y);
95                 return true;
96         }
97         if(key=="w")
98         {
99                 int x,y; window->get_size(x,y);
100                 value=strprintf("%d",x);
101                 return true;
102         }
103         if(key=="h")
104         {
105                 int x,y; window->get_size(x,y);
106                 value=strprintf("%d",y);
107                 return true;
108         }
109         if(key=="visible")
110         {
111                 value=window->is_visible()?"1":"0";
112                 return true;
113         }
114
115         return synfigapp::Settings::get_value(key,value);
116 }
117
118 bool
119 DialogSettings::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(value.empty())
125                 return false;
126
127         if(key=="pos")
128         {
129                 int x,y;
130                 if(!strscanf(value,"%d %d",&x, &y))
131                         return false;
132
133                 if (x > screen_w) x = screen_w - 150; if (x < 0) x = 0;
134                 if (y > screen_h) y = screen_h - 150; if (y < 0) y = 0;
135
136                 window->move(x,y);
137                 return true;
138         }
139         if(key=="size")
140         {
141                 int x,y;
142                 if(!strscanf(value,"%d %d",&x, &y))
143                         return false;
144
145                 if (x > screen_w) x = 150; if (x < 0) x = 0;
146                 if (y > screen_h) y = 150; if (y < 0) y = 0;
147
148                 window->set_default_size(x,y);
149                 return true;
150         }
151         if(key=="x")
152         {
153                 int x,y; window->get_position(x,y);
154                 x=atoi(value.c_str());
155                 window->move(x,y);
156                 return true;
157         }
158         if(key=="y")
159         {
160                 int x,y; window->get_position(x,y);
161                 y=atoi(value.c_str());
162                 window->move(x,y);
163                 return true;
164         }
165         if(key=="w")
166         {
167                 int x,y; window->get_size(x,y);
168                 x=atoi(value.c_str());
169                 window->set_default_size(x,y);
170                 return true;
171         }
172         if(key=="h")
173         {
174                 int x,y; window->get_size(x,y);
175                 y=atoi(value.c_str());
176                 window->set_default_size(x,y);
177                 return true;
178         }
179         if(key=="visible")
180         {
181                 if(value=="0")
182                         window->hide();
183                 else
184                         window->present();
185                 return true;
186         }
187
188         return synfigapp::Settings::set_value(key,value);
189 }
190
191 synfigapp::Settings::KeyList
192 DialogSettings::get_key_list()const
193 {
194         synfigapp::Settings::KeyList ret(synfigapp::Settings::get_key_list());
195
196         ret.push_back("size");
197         ret.push_back("pos");
198         ret.push_back("visible");
199
200         return ret;
201 }