1 /*
2 Copyright (C) 1998,1999,2000,2001
3 T. Scott Dattalo and Ralf Forsberg
4
5 This file is part of gpsim.
6
7 gpsim is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 gpsim is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with gpsim; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "../config.h"
23 #ifdef HAVE_GUI
24
25 #include <gtk/gtk.h>
26 #include <glib.h>
27
28 #include "gui.h"
29 #include "gui_object.h"
30 #include "settings.h"
31
32 //------------------------------------------------------------------------
33 // Helper functions for setting and retrieving variables stored in
34 // gpsim configuration file.
35
config_set_string(const char * module,const char * entry,const char * str)36 int config_set_string(const char *module, const char *entry, const char *str)
37 {
38 return settings->set(module, entry, str);
39 }
40
config_set_variable(const char * module,const char * entry,int value)41 int config_set_variable(const char *module, const char *entry, int value)
42 {
43 return settings->set(module, entry, value);
44 }
45
config_get_variable(const char * module,const char * entry,int * value)46 int config_get_variable(const char *module, const char *entry, int *value)
47 {
48 return settings->get(module, entry, value);
49 }
50
config_get_string(const char * module,const char * entry,char ** str)51 int config_get_string(const char *module, const char *entry, char **str)
52 {
53 return settings->get(module, entry, str);
54 }
55
config_remove(const char * module,const char * entry)56 int config_remove(const char *module, const char *entry)
57 {
58 return settings->remove(module, entry);
59 }
60
GUI_Object(const std::string & p_name)61 GUI_Object::GUI_Object(const std::string &p_name)
62 : gp(0), menu(0), x(0), y(0), width(100), height(100),
63 enabled(FALSE), bIsBuilt(false), m_name(p_name)
64 {
65 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
66 g_signal_connect(window, "delete_event",
67 G_CALLBACK(GUI_Object::delete_event_cb), this);
68 g_signal_connect_after(window, "configure_event",
69 G_CALLBACK(GUI_Object::gui_object_configure_event), this);
70
71 get_config();
72
73 gtk_window_move(GTK_WINDOW(window), x, y);
74 gtk_window_set_default_size(GTK_WINDOW(window), width, height);
75 }
76
~GUI_Object()77 GUI_Object::~GUI_Object()
78 {
79 gtk_widget_destroy(window);
80 }
81
UpdateMenuItem()82 void GUI_Object::UpdateMenuItem()
83 {
84 if (menu) {
85 GtkAction *menu_item = gtk_ui_manager_get_action(ui, menu);
86 gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(menu_item), enabled);
87 }
88 }
89
ChangeView(gboolean view_state)90 void GUI_Object::ChangeView(gboolean view_state)
91 {
92 if (view_state) {
93
94 if (!bIsBuilt) {
95
96 if(!get_config()) {
97 set_default_config();
98 }
99
100 enabled = TRUE;
101
102 Build();
103
104 } else {
105 // hmm, this call shouldn't be necessary, but I (Scott) found that
106 // in GTK+ 2.2.1 under Linux that it is.
107 gtk_window_move(GTK_WINDOW(window), x, y);
108 gtk_widget_show(window);
109
110 enabled = TRUE;
111
112 // Update the config database
113 set_config();
114 }
115
116 } else if (window && gtk_widget_get_visible(window)) {
117
118 enabled = FALSE;
119
120 // Update the config database
121 set_config();
122
123 gtk_widget_hide(window);
124 }
125
126 // Update menu item
127 UpdateMenuItem();
128 }
129
get_config()130 int GUI_Object::get_config()
131 {
132 const char *pName = name();
133 if (*pName == '\0')
134 return 0;
135
136 if (!config_get_variable(pName, "enabled", &enabled))
137 enabled = FALSE;
138 if (!config_get_variable(pName, "x", &x))
139 x = 10;
140 if (!config_get_variable(pName, "y", &y))
141 y = 10;
142 if (!config_get_variable(pName, "width", &width))
143 width = 300;
144 if (!config_get_variable(pName, "height", &height))
145 height = 100;
146
147 check();
148 return 1;
149 }
150
check()151 void GUI_Object::check()
152 {
153 #define MAX_REASONABLE 2000
154
155 if ((x + width < 0 || x > MAX_REASONABLE) ||
156 (y + height < 0 || y > MAX_REASONABLE) ||
157 (width < 0 || width > MAX_REASONABLE) ||
158 (height < 0 || height > MAX_REASONABLE) )
159
160 set_default_config();
161 }
162
set_default_config()163 void GUI_Object::set_default_config()
164 {
165 static int defaultX = 100;
166 static int defaultY = 100;
167
168 enabled = FALSE;
169 x = defaultX;
170 y = defaultY;
171 defaultX += 100;
172 defaultY += 100;
173
174 width = 100;
175 height = 100;
176 }
177
set_config()178 int GUI_Object::set_config()
179 {
180 check();
181
182 const char *pName = name();
183
184 if (*pName == '\0')
185 return 0;
186
187 if (window) {
188 gtk_window_get_position(GTK_WINDOW(window), &x, &y);
189 gtk_window_get_size(GTK_WINDOW(window), &width, &height);
190 }
191
192 config_set_variable(pName, "enabled", ((enabled) ? 1 : 0) );
193 config_set_variable(pName, "x", x);
194 config_set_variable(pName, "y", y);
195 config_set_variable(pName, "width", width);
196 config_set_variable(pName, "height", height);
197 return 1;
198 }
199
delete_event_cb(GtkWidget *,GdkEvent *,GUI_Object * sw)200 gboolean GUI_Object::delete_event_cb(GtkWidget *, GdkEvent *, GUI_Object *sw)
201 {
202 sw->ChangeView(VIEW_HIDE);
203 return TRUE;
204 }
205
gui_object_configure_event(GtkWidget * widget,GdkEventConfigure *,GUI_Object * go)206 gboolean GUI_Object::gui_object_configure_event(GtkWidget *widget,
207 GdkEventConfigure *, GUI_Object *go)
208 {
209 gtk_window_get_position(GTK_WINDOW(widget), &go->x, &go->y);
210 gtk_window_get_size(GTK_WINDOW(widget), &go->width, &go->height);
211
212 go->set_config();
213
214 return FALSE;
215 }
216
217 #endif // HAVE_GUI
218