1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | This program is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | GNU General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public License |
---|
17 | along with this program; if not, write to the Free Software Foundation, |
---|
18 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | |
---|
20 | |
---|
21 | ### File Specific: |
---|
22 | main-programmer: Benjamin Grauer |
---|
23 | |
---|
24 | */ |
---|
25 | |
---|
26 | #include "gui_exec.h" |
---|
27 | |
---|
28 | #include "resource_manager.h" |
---|
29 | |
---|
30 | #include <string.h> |
---|
31 | #include <stdlib.h> |
---|
32 | #include <sys/stat.h> |
---|
33 | #include <sys/types.h> |
---|
34 | |
---|
35 | HashTable* orxonoxFlagHash; |
---|
36 | |
---|
37 | /** |
---|
38 | \brief Creates the Exec-Frame |
---|
39 | */ |
---|
40 | GuiExec::GuiExec(void) |
---|
41 | { |
---|
42 | Frame* execFrame; //!< The Frame that holds the ExecutionOptions. |
---|
43 | |
---|
44 | this->confFile = NULL; |
---|
45 | this->confDir = NULL; |
---|
46 | |
---|
47 | execFrame = new Frame("Execute-Tags:"); |
---|
48 | { |
---|
49 | Box* execBox; //!< The Box that holds the ExecutionOptions. |
---|
50 | |
---|
51 | execBox = new Box('v'); |
---|
52 | execFrame->setGroupName("misc"); |
---|
53 | { |
---|
54 | Button* start; //!< The start Button of orxonox. |
---|
55 | Menu* verboseMode; //!< A Menu for setting the verbose-Mode. \todo setting up a verbose-class. |
---|
56 | CheckButton* alwaysShow; //!< A CheckButton, for if orxonox should start with or without gui. |
---|
57 | Button* quit; //!< A Button to quit the Gui without starting orxonox. |
---|
58 | |
---|
59 | start = new Button("Start"); |
---|
60 | #ifdef HAVE_GTK2 |
---|
61 | start->connectSignal("clicked", this, startOrxonox); |
---|
62 | #endif /* HAVE_GTK2 */ |
---|
63 | execBox->fill(start); |
---|
64 | this->saveSettings = new CheckButton("Save Settings"); |
---|
65 | this->saveSettings->value = 1; |
---|
66 | this->saveSettings->saveability(); |
---|
67 | execBox->fill(this->saveSettings); |
---|
68 | verboseMode = new Menu("verbose mode", "nothing", "error", "warning", "info", "lastItem"); |
---|
69 | verboseMode->setFlagName("verbose", "v", 2); |
---|
70 | verboseMode->saveability(); |
---|
71 | execBox->fill(verboseMode); |
---|
72 | alwaysShow = new CheckButton("Always Show this Menu"); |
---|
73 | alwaysShow->setFlagName("gui", "g", 0); |
---|
74 | alwaysShow->saveability(); |
---|
75 | execBox->fill(alwaysShow); |
---|
76 | quit = new Button("Quit"); |
---|
77 | #ifdef HAVE_GTK2 |
---|
78 | quit->connectSignal("clicked", this, GuiExec::quitGui); |
---|
79 | // Window::mainWindow->connectSignal("remove", this, GuiExec::quitGui); |
---|
80 | Window::mainWindow->connectSignal("destroy", this, GuiExec::quitGui); |
---|
81 | #endif /* HAVE_GTK2 */ |
---|
82 | execBox->fill(quit); |
---|
83 | } |
---|
84 | execFrame->fill(execBox); |
---|
85 | } |
---|
86 | setMainWidget(execFrame); |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | \brief Destructs the Execution-stuff |
---|
91 | */ |
---|
92 | GuiExec::~GuiExec(void) |
---|
93 | { |
---|
94 | if(this->confFile) |
---|
95 | delete []this->confFile; |
---|
96 | if(this->confDir) |
---|
97 | delete []this->confDir; |
---|
98 | } |
---|
99 | |
---|
100 | /* FILE HANDLING */ |
---|
101 | |
---|
102 | /** |
---|
103 | \brief sets the Directory of the configuration files |
---|
104 | \param confDir the Directory for the configuration files |
---|
105 | */ |
---|
106 | void GuiExec::setConfDir(const char* confDir) |
---|
107 | { |
---|
108 | this->confDir = ResourceManager::homeDirCheck(confDir); |
---|
109 | |
---|
110 | PRINTF(3)("Config Directory is: %s.\n", this->confDir); |
---|
111 | //! \todo F** Windows-support |
---|
112 | #ifndef __WIN32__ |
---|
113 | mkdir(this->confDir, 0755); |
---|
114 | #else /* __WiN32__ */ |
---|
115 | mkdir(this->confDir); |
---|
116 | #endif /* __WIN32__ */ |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | \brief Sets the location of the configuration File. |
---|
121 | \param fileName the location of the configFile |
---|
122 | |
---|
123 | The name will be parsed from ~/ to /home/[username] on unix and c:/Documents and Settings/username/Settings/ on Windows |
---|
124 | */ |
---|
125 | void GuiExec::setConfFile(const char* fileName) |
---|
126 | { |
---|
127 | if (!this->confDir) |
---|
128 | this->setConfDir("~/"); |
---|
129 | this->confFile = new char[strlen(this->confDir)+strlen(fileName)+2]; |
---|
130 | sprintf(this->confFile, "%s/%s", this->confDir, fileName); |
---|
131 | PRINTF(3)("ConfigurationFile is %s.\n", this->confFile); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | \returns The name of the Configuration-File |
---|
136 | */ |
---|
137 | const char* GuiExec::getConfigFile(void) const |
---|
138 | { |
---|
139 | return this->confFile; |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | \brief checks if a option should be saved. |
---|
144 | \return 1 if it should 0 if not/ |
---|
145 | */ |
---|
146 | int GuiExec::shouldsave() |
---|
147 | { |
---|
148 | return(static_cast<Option*>(this->saveSettings)->value); |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | \brief Saves the configuration-file to the Disk.\n |
---|
153 | \param widget from which Widget on should be saved. |
---|
154 | |
---|
155 | this Function only opens and closes the file, in between GuiExec::writeFileText(Widget* widget) will execute the real writing process. |
---|
156 | */ |
---|
157 | void GuiExec::writeToFile(Widget* widget) |
---|
158 | { |
---|
159 | this->CONFIG_FILE = fopen(this->confFile, "w"); |
---|
160 | if(this->CONFIG_FILE) |
---|
161 | this->writeFileText(widget, 0); |
---|
162 | fclose(this->CONFIG_FILE); |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | \brief Actually writes into the configuration file to the disk. |
---|
167 | \param widget from which Widget on should be saved. |
---|
168 | \param depth initially "0", and grows higher, while new Groups are bundeled. |
---|
169 | */ |
---|
170 | void GuiExec::writeFileText(Widget* widget, int depth) |
---|
171 | { |
---|
172 | int counter = 0; |
---|
173 | while(counter < depth &&((widget->isOption>0 |
---|
174 | &&(static_cast<Option*>(widget)->isSaveable())) |
---|
175 | ||(widget->isOption<0 |
---|
176 | && static_cast<Packer*>(widget)->getGroupName()))) |
---|
177 | { |
---|
178 | fprintf(this->CONFIG_FILE, " ", depth); |
---|
179 | counter++; |
---|
180 | } |
---|
181 | |
---|
182 | // check if it is a Packer, and if it is, check if it has a name and if there is something in it. |
---|
183 | if(widget->isOption <0) |
---|
184 | { |
---|
185 | if(static_cast<Packer*>(widget)->getGroupName()) |
---|
186 | { |
---|
187 | fprintf(CONFIG_FILE, "[%s]\n", static_cast<Packer*>(widget)->getGroupName()); |
---|
188 | this->writeFileText(static_cast<Packer*>(widget)->down, depth+1); |
---|
189 | fprintf(CONFIG_FILE, "\n"); |
---|
190 | } |
---|
191 | else |
---|
192 | { |
---|
193 | this->writeFileText(static_cast<Packer*>(widget)->down, depth); |
---|
194 | } |
---|
195 | } |
---|
196 | // if(widget->isOption == 0) |
---|
197 | // printf("%s\n",widget->title); |
---|
198 | if(widget->isOption >= 1) |
---|
199 | if (static_cast<Option*>(widget)->isSaveable()) |
---|
200 | { |
---|
201 | char Buffer[256]; |
---|
202 | char* space2under; |
---|
203 | strcpy(Buffer, static_cast<Option*>(widget)->title); |
---|
204 | if(strchr(Buffer, '_')) |
---|
205 | PRINTF(2)("Optionname %s is not Valid for Saving, because it includes an underscore\n", Buffer); |
---|
206 | while(space2under = strchr(Buffer, ' ')) |
---|
207 | { |
---|
208 | space2under[0] = '_'; |
---|
209 | } |
---|
210 | fprintf(CONFIG_FILE, "%s = %s\n", Buffer, static_cast<Option*>(widget)->save()); |
---|
211 | } |
---|
212 | |
---|
213 | if(widget->next != NULL) |
---|
214 | this->writeFileText(widget->next, depth); |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | \brief Reads in Configuration Data. |
---|
219 | \param widget from which Widget on should be saved. |
---|
220 | */ |
---|
221 | void GuiExec::readFromFile(Widget* widget) |
---|
222 | { |
---|
223 | this->CONFIG_FILE = fopen(this->confFile, "r"); |
---|
224 | VarInfo varInfo; |
---|
225 | if(this->CONFIG_FILE) |
---|
226 | { |
---|
227 | Widget* groupWidget = widget; |
---|
228 | char Buffer[256] = ""; |
---|
229 | char Variable[256]= ""; |
---|
230 | char* Value; |
---|
231 | while(fscanf(this->CONFIG_FILE, "%s", Buffer) != EOF) |
---|
232 | { |
---|
233 | // group-search // |
---|
234 | if(!strncmp(Buffer, "[", 1)) |
---|
235 | { |
---|
236 | if((groupWidget = locateGroup(widget, Buffer, 1))==NULL) |
---|
237 | { |
---|
238 | PRINTF(2)("!!There is no group called %s in this GUI.\n First best Widget will get the Infos assigned.\n Config-File will be updated in next Save\n", Buffer); |
---|
239 | groupWidget = widget; |
---|
240 | } |
---|
241 | else |
---|
242 | PRINT(5)("Group %s located.\n", static_cast<Packer*>(groupWidget)->groupName); |
---|
243 | } |
---|
244 | // option-setting // |
---|
245 | if(!strcmp(Buffer, "=")) |
---|
246 | { |
---|
247 | char* under2space; |
---|
248 | while(under2space = strchr(Variable, '_')) |
---|
249 | { |
---|
250 | sprintf(under2space, " %s", under2space+1); |
---|
251 | } |
---|
252 | |
---|
253 | fscanf(this->CONFIG_FILE, "%s", Buffer); |
---|
254 | varInfo.variableName = Variable; |
---|
255 | varInfo.variableValue = Buffer; |
---|
256 | groupWidget->walkThrough(this->readFileText, &varInfo, 0); |
---|
257 | sprintf(Variable, ""); |
---|
258 | } |
---|
259 | sprintf(Variable, "%s", Buffer); |
---|
260 | } |
---|
261 | widget->walkThrough(widget->setOptions, 0); |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | /** |
---|
266 | \brief Maps Confugurations to the Options. |
---|
267 | \param widget which widget downwards |
---|
268 | \param varInfo Information about the Variable to read |
---|
269 | */ |
---|
270 | void GuiExec::readFileText(Widget* widget, void* varInfo) |
---|
271 | { |
---|
272 | VarInfo* info =(VarInfo*)varInfo; |
---|
273 | if(widget->title && !strcmp(widget->title, info->variableName)) |
---|
274 | { |
---|
275 | PRINT(5)("Located Option %s.\n", widget->title); |
---|
276 | if(widget->isOption >= 1) |
---|
277 | static_cast<Option*>(widget)->load(info->variableValue); |
---|
278 | } |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | \brief Locates a Group. |
---|
283 | \param widget The Widget from where to search from |
---|
284 | \param groupName The GroupName for which to search. |
---|
285 | \param depth The Depth of the search seen from the first widget we searched from. |
---|
286 | \returns The Widget that holds the Group, or the NULL if the Group wasn't found. |
---|
287 | |
---|
288 | \todo do this in gui-gtk. |
---|
289 | */ |
---|
290 | Widget* GuiExec::locateGroup(Widget* widget, char* groupName, int depth) |
---|
291 | { |
---|
292 | Widget* tmp; |
---|
293 | |
---|
294 | // removes the trailing and ending [ ]. |
---|
295 | if(!strncmp(groupName, "[", 1)) |
---|
296 | { |
---|
297 | groupName = groupName+1; |
---|
298 | groupName[strlen(groupName)-1] = '\0'; |
---|
299 | } |
---|
300 | |
---|
301 | if(widget->isOption < 0) |
---|
302 | { |
---|
303 | if(static_cast<Packer*>(widget)->getGroupName() && |
---|
304 | !strcmp(groupName, static_cast<Packer*>(widget)->getGroupName())) |
---|
305 | { |
---|
306 | return widget; |
---|
307 | } |
---|
308 | else |
---|
309 | { |
---|
310 | if((tmp = locateGroup(static_cast<Packer*>(widget)->down, groupName, depth+1)) != NULL) |
---|
311 | return tmp; |
---|
312 | } |
---|
313 | } |
---|
314 | |
---|
315 | if(widget->next != NULL && depth != 0) |
---|
316 | { |
---|
317 | if((tmp = locateGroup(widget->next, groupName, depth)) != NULL) |
---|
318 | return tmp; |
---|
319 | } |
---|
320 | return NULL; |
---|
321 | } |
---|
322 | |
---|
323 | /** |
---|
324 | \brief Starts ORXONOX.(not really implemented yet, but the function is there.\n |
---|
325 | \param widget the widget that executed the start command |
---|
326 | \param data additional data |
---|
327 | |
---|
328 | This is a Signal and can be executed through Widget::signal_connect |
---|
329 | */ |
---|
330 | #ifdef HAVE_GTK2 |
---|
331 | int GuiExec::startOrxonox(GtkWidget* widget, void* data) |
---|
332 | #else /* HAVE_GTK2 */ |
---|
333 | int GuiExec::startOrxonox(void* widget, void* data) |
---|
334 | #endif /* HAVE_GTK2 */ |
---|
335 | { |
---|
336 | Window::mainWindow->hide(); |
---|
337 | |
---|
338 | #ifdef HAVE_GTK2 |
---|
339 | gtk_widget_destroy(Window::mainWindow->widget); |
---|
340 | #else |
---|
341 | quitGui(widget, data); |
---|
342 | #endif /* HAVE_GTK2 */ |
---|
343 | |
---|
344 | PRINT(3)("Starting Orxonox\n"); |
---|
345 | Gui::startOrxonox = true; |
---|
346 | } |
---|
347 | |
---|
348 | /** |
---|
349 | \brief Starts ORXONOX.(not really implemented yet, but the function is there.\n |
---|
350 | \param widget the widget that executed the start command |
---|
351 | \param data additional data |
---|
352 | |
---|
353 | This is a Signal and can be executed through Widget::signal_connect |
---|
354 | */ |
---|
355 | #ifdef HAVE_GTK2 |
---|
356 | int GuiExec::quitGui(GtkWidget* widget, void* data) |
---|
357 | #else /* HAVE_GTK2 */ |
---|
358 | int GuiExec::quitGui(void* widget, void* data) |
---|
359 | #endif /* HAVE_GTK2 */ |
---|
360 | { |
---|
361 | GuiExec* exec =(GuiExec*)data; |
---|
362 | if(exec->shouldsave()) |
---|
363 | exec->writeToFile(Window::mainWindow); |
---|
364 | #ifdef HAVE_GTK2 |
---|
365 | gtk_main_quit(); |
---|
366 | while(gtk_events_pending()) gtk_main_iteration(); |
---|
367 | #endif /* HAVE_GTK2 */ |
---|
368 | } |
---|