9ca73a4bb3403c15684e4d1b1fafdbd23e4e594c
[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 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "dialogsettings.h"
33 #include <synfigapp/main.h>
34 #include <gdkmm/general.h>
35
36 #include "general.h"
37
38 #endif
39
40 /* === U S I N G =========================================================== */
41
42 using namespace std;
43 using namespace etl;
44 using namespace synfig;
45 using namespace studio;
46
47 /* === M A C R O S ========================================================= */
48
49 /* === G L O B A L S ======================================================= */
50
51 /* === P R O C E D U R E S ================================================= */
52
53 /* === M E T H O D S ======================================================= */
54
55 DialogSettings::DialogSettings(Gtk::Window* window,const synfig::String& name):
56         window(window),
57         name(name)
58 {
59         synfigapp::Main::settings().add_domain(this,"window."+name);
60 }
61
62 DialogSettings::~DialogSettings()
63 {
64         synfigapp::Main::settings().remove_domain("window."+name);
65 }
66
67 bool
68 DialogSettings::get_value(const synfig::String& key, synfig::String& value)const
69 {
70         if(key=="pos")
71         {
72                 // if(!window->is_visible())return false;
73                 int x,y; window->get_position(x,y);
74                 value=strprintf("%d %d",x,y);
75                 return true;
76         }
77         if(key=="size")
78         {
79                 // if(!window->is_visible())return false;
80                 int x,y; window->get_size(x,y);
81                 value=strprintf("%d %d",x,y);
82                 return true;
83         }
84         if(key=="x")
85         {
86                 int x,y; window->get_position(x,y);
87                 value=strprintf("%d",x);
88                 return true;
89         }
90         if(key=="y")
91         {
92                 int x,y; window->get_position(x,y);
93                 value=strprintf("%d",y);
94                 return true;
95         }
96         if(key=="w")
97         {
98                 int x,y; window->get_size(x,y);
99                 value=strprintf("%d",x);
100                 return true;
101         }
102         if(key=="h")
103         {
104                 int x,y; window->get_size(x,y);
105                 value=strprintf("%d",y);
106                 return true;
107         }
108         if(key=="visible")
109         {
110                 value=window->is_visible()?"1":"0";
111                 return true;
112         }
113
114         return synfigapp::Settings::get_value(key,value);
115 }
116
117 bool
118 DialogSettings::set_value(const synfig::String& key,const synfig::String& value)
119 {
120         int screen_w(Gdk::screen_width());
121         int screen_h(Gdk::screen_height());
122
123         if(value.empty())
124                 return false;
125
126         if(key=="pos")
127         {
128                 int x,y;
129                 if(!strscanf(value,"%d %d",&x, &y))
130                         return false;
131
132                 if (x > screen_w) x = screen_w - 150; if (x < 0) x = 0;
133                 if (y > screen_h) y = screen_h - 150; if (y < 0) y = 0;
134
135                 window->move(x,y);
136                 return true;
137         }
138         if(key=="size")
139         {
140                 int x,y;
141                 if(!strscanf(value,"%d %d",&x, &y))
142                         return false;
143
144                 if (x > screen_w) x = 150; if (x < 0) x = 0;
145                 if (y > screen_h) y = 150; if (y < 0) y = 0;
146
147                 window->set_default_size(x,y);
148                 return true;
149         }
150         if(key=="x")
151         {
152                 int x,y; window->get_position(x,y);
153                 x=atoi(value.c_str());
154                 window->move(x,y);
155                 return true;
156         }
157         if(key=="y")
158         {
159                 int x,y; window->get_position(x,y);
160                 y=atoi(value.c_str());
161                 window->move(x,y);
162                 return true;
163         }
164         if(key=="w")
165         {
166                 int x,y; window->get_size(x,y);
167                 x=atoi(value.c_str());
168                 window->set_default_size(x,y);
169                 return true;
170         }
171         if(key=="h")
172         {
173                 int x,y; window->get_size(x,y);
174                 y=atoi(value.c_str());
175                 window->set_default_size(x,y);
176                 return true;
177         }
178         if(key=="visible")
179         {
180                 if(value=="0")
181                         window->hide();
182                 else
183                         window->present();
184                 return true;
185         }
186
187         return synfigapp::Settings::set_value(key,value);
188 }
189
190 synfigapp::Settings::KeyList
191 DialogSettings::get_key_list()const
192 {
193         synfigapp::Settings::KeyList ret(synfigapp::Settings::get_key_list());
194
195         ret.push_back("size");
196         ret.push_back("pos");
197         ret.push_back("visible");
198
199         return ret;
200 }