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 "file.h" |
---|
29 | #include "util/loading/resource_manager.h" |
---|
30 | #include "parser/ini_parser/ini_parser.h" |
---|
31 | |
---|
32 | #include <string.h> |
---|
33 | #include <stdlib.h> |
---|
34 | #include <sys/stat.h> |
---|
35 | #include <sys/types.h> |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | #ifdef __WIN32__ |
---|
40 | #include <direct.h> |
---|
41 | #endif /* __WIN32__ */ |
---|
42 | HashTable* orxonoxFlagHash; |
---|
43 | |
---|
44 | /** |
---|
45 | * Creates the Exec-Frame |
---|
46 | */ |
---|
47 | GuiExec::GuiExec() |
---|
48 | { |
---|
49 | Frame* execFrame; //!< The Frame that holds the ExecutionOptions. |
---|
50 | |
---|
51 | this->confFile = ""; |
---|
52 | this->confDir = ""; |
---|
53 | |
---|
54 | execFrame = new Frame("Execute-Tags:"); |
---|
55 | { |
---|
56 | Box* execBox; //!< The Box that holds the ExecutionOptions. |
---|
57 | |
---|
58 | execBox = new Box('v'); |
---|
59 | execFrame->setGroupName(CONFIG_SECTION_MISC); |
---|
60 | { |
---|
61 | Button* start; //!< The start Button of orxonox. |
---|
62 | Menu* verboseMode; //!< A Menu for setting the verbose-Mode. @todo setting up a verbose-class. |
---|
63 | CheckButton* alwaysShow; //!< A CheckButton, for if orxonox should start with or without gui. |
---|
64 | Button* quit; //!< A Button to quit the Gui without starting orxonox. |
---|
65 | |
---|
66 | start = new Button("Start"); |
---|
67 | #ifdef HAVE_GTK2 |
---|
68 | start->connectSignal("clicked", this, startOrxonox); |
---|
69 | #endif /* HAVE_GTK2 */ |
---|
70 | execBox->fill(start); |
---|
71 | this->saveSettings = new CheckButton(CONFIG_NAME_SAVE_SETTINGS); |
---|
72 | this->saveSettings->value = 1; |
---|
73 | this->saveSettings->saveability(); |
---|
74 | execBox->fill(this->saveSettings); |
---|
75 | |
---|
76 | #ifdef DEBUG |
---|
77 | verboseMode = new Menu(CONFIG_NAME_VERBOSE_MODE, "nothing", |
---|
78 | #if DEBUG_LEVEL >=1 |
---|
79 | "error", |
---|
80 | #endif |
---|
81 | #if DEBUG_LEVEL >=2 |
---|
82 | "warning", |
---|
83 | #endif |
---|
84 | #if DEBUG_LEVEL >=3 |
---|
85 | "info", |
---|
86 | #endif |
---|
87 | #if DEBUG_LEVEL >=4 |
---|
88 | "debug", |
---|
89 | #endif |
---|
90 | #if DEBUG_LEVEL >=5 |
---|
91 | "heavydebug", |
---|
92 | #endif |
---|
93 | "lastItem"); |
---|
94 | verboseMode->setFlagName("verbose", "v", 2); |
---|
95 | verboseMode->setDescription("Sets the Output Mode", "This Enables Outbug messages\n" |
---|
96 | "0: nothing will be displayed, but stuff one cannot do without (eg.GUI)\n" |
---|
97 | #if DEBUG_LEVEL >=1 |
---|
98 | "1: error: outputs all the above and errors" |
---|
99 | #endif |
---|
100 | #if DEBUG_LEVEL >=2 |
---|
101 | "2: warning: outputs all the above plus warnings" |
---|
102 | #endif |
---|
103 | #if DEBUG_LEVEL >=3 |
---|
104 | "3: info: outputs all the above plus Information" |
---|
105 | #endif |
---|
106 | #if DEBUG_LEVEL >=4 |
---|
107 | "4: debug: displays all the above plus debug information" |
---|
108 | #endif |
---|
109 | #if DEBUG_LEVEL >=5 |
---|
110 | "5: heavydebug: displays all the above plus heavy debug information: WARNING: the game will run very slow with this." |
---|
111 | #endif |
---|
112 | ); |
---|
113 | verboseMode->saveability(); |
---|
114 | execBox->fill(verboseMode); |
---|
115 | #endif |
---|
116 | |
---|
117 | alwaysShow = new CheckButton(CONFIG_NAME_ALWAYS_SHOW_GUI); |
---|
118 | alwaysShow->setFlagName("gui", "g", 0); |
---|
119 | alwaysShow->setDescription("shows the gui when starting orxonox"); |
---|
120 | alwaysShow->saveability(); |
---|
121 | execBox->fill(alwaysShow); |
---|
122 | |
---|
123 | quit = new Button("Quit"); |
---|
124 | #ifdef HAVE_GTK2 |
---|
125 | quit->connectSignal("clicked", this, GuiExec::quitGui); |
---|
126 | // Window::mainWindow->connectSignal("remove", this, GuiExec::quitGui); |
---|
127 | Window::mainWindow->connectSignal("destroy", this, GuiExec::quitGui); |
---|
128 | #endif /* HAVE_GTK2 */ |
---|
129 | execBox->fill(quit); |
---|
130 | } |
---|
131 | execFrame->fill(execBox); |
---|
132 | } |
---|
133 | setMainWidget(execFrame); |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Destructs the Execution-stuff |
---|
138 | */ |
---|
139 | GuiExec::~GuiExec() |
---|
140 | { |
---|
141 | } |
---|
142 | |
---|
143 | /* FILE HANDLING */ |
---|
144 | |
---|
145 | /** |
---|
146 | * sets the Directory of the configuration files |
---|
147 | * @param confDir the Directory for the configuration files |
---|
148 | */ |
---|
149 | void GuiExec::setConfDir(const char* confDir) |
---|
150 | { |
---|
151 | this->confDir = File(confDir).name(); |
---|
152 | |
---|
153 | PRINTF(5)("Config Directory is: %s.\n", this->confDir); |
---|
154 | //! @todo F** Windows-support |
---|
155 | #ifndef __WIN32__ |
---|
156 | mkdir(this->confDir.c_str(), 0755); |
---|
157 | #else /* __WiN32__ */ |
---|
158 | mkdir(this->confDir.c_str()); |
---|
159 | #endif /* __WIN32__ */ |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * Sets the location of the configuration File. |
---|
164 | * @param fileName the location of the configFile |
---|
165 | |
---|
166 | The name will be parsed from ~/ to /home/[username] on unix and c:/Documents and Settings/username/Settings/ on Windows |
---|
167 | */ |
---|
168 | void GuiExec::setConfFile(const char* fileName) |
---|
169 | { |
---|
170 | if (this->confDir.empty()) |
---|
171 | this->setConfDir("~/"); |
---|
172 | this->confFile = this->confDir + "/" + fileName; |
---|
173 | PRINTF(5)("ConfigurationFile is %s.\n", this->confFile); |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * @returns The name of the Configuration-File |
---|
178 | */ |
---|
179 | const char* GuiExec::getConfigFile() const |
---|
180 | { |
---|
181 | return this->confFile.c_str(); |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * checks if a option should be saved. |
---|
186 | * @return 1 if it should 0 if not/ |
---|
187 | */ |
---|
188 | int GuiExec::shouldsave() |
---|
189 | { |
---|
190 | return(static_cast<Option*>(this->saveSettings)->value); |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * Saves the configuration-file to the Disk.\n |
---|
195 | * @param widget from which Widget on should be saved. |
---|
196 | |
---|
197 | this Function only opens and closes the file, in between GuiExec::writeFileText(Widget* widget) will execute the real writing process. |
---|
198 | */ |
---|
199 | void GuiExec::writeToFile(Widget* widget) |
---|
200 | { |
---|
201 | IniParser iniParser; |
---|
202 | this->writeFileText(widget, &iniParser, 0); |
---|
203 | iniParser.writeFile(File(confFile).name()); |
---|
204 | } |
---|
205 | |
---|
206 | /** |
---|
207 | * Actually writes into the configuration file to the disk. |
---|
208 | * @param widget from which Widget on should be saved. |
---|
209 | * @param parser the IniParser to write to. |
---|
210 | * @param depth initially "0", and grows higher, while new Groups are bundeled. |
---|
211 | */ |
---|
212 | void GuiExec::writeFileText(Widget* widget, IniParser* parser, int depth) |
---|
213 | { |
---|
214 | // int counter = 0; |
---|
215 | // while(counter < depth &&((widget->optionType > GUI_NOTHING |
---|
216 | // &&(static_cast<Option*>(widget)->isSaveable())) |
---|
217 | // ||(widget->optionType < GUI_NOTHING |
---|
218 | // && static_cast<Packer*>(widget)->getGroupName()))) |
---|
219 | // { |
---|
220 | // fprintf(this->CONFIG_FILE, " ", depth); |
---|
221 | // counter++; |
---|
222 | // } |
---|
223 | |
---|
224 | // check if it is a Packer, and if it is, check if it has a name and if there is something in it. |
---|
225 | if(widget->optionType < GUI_NOTHING) |
---|
226 | { |
---|
227 | if(static_cast<Packer*>(widget)->getGroupName()) |
---|
228 | { |
---|
229 | parser->addSection(static_cast<Packer*>(widget)->getGroupName()); |
---|
230 | this->writeFileText(static_cast<Packer*>(widget)->down, parser, depth+1); |
---|
231 | } |
---|
232 | else |
---|
233 | { |
---|
234 | this->writeFileText(static_cast<Packer*>(widget)->down, parser, depth); |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | if(widget->optionType > GUI_NOTHING) |
---|
239 | if (static_cast<Option*>(widget)->isSaveable()) |
---|
240 | { |
---|
241 | char* saveName = static_cast<Option*>(widget)->save(); |
---|
242 | parser->addVar(static_cast<Option*>(widget)->title, saveName); |
---|
243 | delete[] saveName; |
---|
244 | } |
---|
245 | |
---|
246 | if(widget->next != NULL) |
---|
247 | this->writeFileText(widget->next, parser, depth); |
---|
248 | } |
---|
249 | |
---|
250 | /** |
---|
251 | * @brief Reads in Configuration Data. |
---|
252 | * @param widget from which Widget on should be saved. |
---|
253 | */ |
---|
254 | void GuiExec::readFromFile(Widget* widget) |
---|
255 | { |
---|
256 | std::string fileName = File(confFile).name(); |
---|
257 | IniParser iniParser(fileName); |
---|
258 | if (!iniParser.isOpen()) |
---|
259 | return; |
---|
260 | |
---|
261 | iniParser.firstSection(); |
---|
262 | Widget* groupWidget = widget; |
---|
263 | std::string groupName; |
---|
264 | std::string widgetName; |
---|
265 | VarInfo varInfo; |
---|
266 | while ((groupName = iniParser.getCurrentSection()) != "") |
---|
267 | { |
---|
268 | PRINTF(4)("GROUP:::%s\n", groupName.c_str()); |
---|
269 | if((groupWidget = locateGroup(widget, groupName.c_str(), 1))==NULL) |
---|
270 | { |
---|
271 | 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", groupName.c_str()); |
---|
272 | groupWidget = widget; |
---|
273 | continue; |
---|
274 | } |
---|
275 | else |
---|
276 | PRINT(4)("Group %s located.\n", static_cast<Packer*>(groupWidget)->groupName); |
---|
277 | |
---|
278 | std::string entryName; |
---|
279 | iniParser.firstVar(); |
---|
280 | while((entryName = iniParser.getCurrentName()) != "") |
---|
281 | { |
---|
282 | PRINTF(4)("ENTRY:::%s = %s\n", entryName.c_str(), iniParser.getCurrentValue().c_str()); |
---|
283 | varInfo.variableName = entryName.c_str(); |
---|
284 | varInfo.variableValue = iniParser.getCurrentValue().c_str(); |
---|
285 | groupWidget->walkThrough(this->readFileText, &varInfo, 0); |
---|
286 | iniParser.nextVar(); |
---|
287 | } |
---|
288 | |
---|
289 | iniParser.nextSection(); |
---|
290 | } |
---|
291 | widget->walkThrough(widget->setOptions, 0); |
---|
292 | } |
---|
293 | |
---|
294 | /** |
---|
295 | * Maps Confugurations to the Options. |
---|
296 | * @param widget which widget downwards |
---|
297 | * @param varInfo Information about the Variable to read |
---|
298 | */ |
---|
299 | void GuiExec::readFileText(Widget* widget, void* varInfo) |
---|
300 | { |
---|
301 | VarInfo* info =(VarInfo*)varInfo; |
---|
302 | if (info == NULL || info->variableName == NULL) |
---|
303 | return; |
---|
304 | |
---|
305 | if(widget->title && !strcmp(widget->title, info->variableName)) |
---|
306 | { |
---|
307 | PRINT(5)("Located Option %s.\n", widget->title); |
---|
308 | if(widget->optionType > GUI_NOTHING) |
---|
309 | if (info->variableValue != NULL) |
---|
310 | static_cast<Option*>(widget)->load(info->variableValue); |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | /** |
---|
315 | * Locates a Group. |
---|
316 | * @param widget The Widget from where to search from |
---|
317 | * @param groupName The GroupName for which to search. |
---|
318 | * @param depth The Depth of the search seen from the first widget we searched from. |
---|
319 | * @returns The Widget that holds the Group, or the NULL if the Group wasn't found. |
---|
320 | |
---|
321 | @todo do this in gui-gtk. |
---|
322 | */ |
---|
323 | Widget* GuiExec::locateGroup(Widget* widget, const char* groupName, int depth) |
---|
324 | { |
---|
325 | Widget* tmp; |
---|
326 | if (widget == NULL || groupName == NULL) |
---|
327 | return NULL; |
---|
328 | |
---|
329 | if(widget->optionType < GUI_NOTHING) |
---|
330 | { |
---|
331 | if(static_cast<Packer*>(widget)->getGroupName() != NULL && |
---|
332 | !strcmp(groupName, static_cast<Packer*>(widget)->getGroupName())) |
---|
333 | return widget; |
---|
334 | else |
---|
335 | { |
---|
336 | if((tmp = locateGroup(static_cast<Packer*>(widget)->down, |
---|
337 | groupName, depth+1)) != NULL) |
---|
338 | return tmp; |
---|
339 | } |
---|
340 | } |
---|
341 | |
---|
342 | if(widget->next != NULL && depth != 0) |
---|
343 | { |
---|
344 | if((tmp = locateGroup(widget->next, groupName, depth)) != NULL) |
---|
345 | return tmp; |
---|
346 | } |
---|
347 | return NULL; |
---|
348 | } |
---|
349 | |
---|
350 | /** |
---|
351 | * Starts ORXONOX.(not really implemented yet, but the function is there.\n |
---|
352 | * @param widget the widget that executed the start command |
---|
353 | * @param data additional data |
---|
354 | |
---|
355 | This is a Signal and can be executed through Widget::signal_connect |
---|
356 | */ |
---|
357 | #ifdef HAVE_GTK2 |
---|
358 | int GuiExec::startOrxonox(GtkWidget* widget, void* data) |
---|
359 | #else /* HAVE_GTK2 */ |
---|
360 | int GuiExec::startOrxonox(void* widget, void* data) |
---|
361 | #endif /* HAVE_GTK2 */ |
---|
362 | { |
---|
363 | Window::mainWindow->hide(); |
---|
364 | |
---|
365 | #ifdef HAVE_GTK2 |
---|
366 | gtk_widget_destroy(Window::mainWindow->widget); |
---|
367 | #else |
---|
368 | quitGui(widget, data); |
---|
369 | #endif /* HAVE_GTK2 */ |
---|
370 | |
---|
371 | PRINT(3)("Starting Orxonox\n"); |
---|
372 | Gui::startOrxonox = true; |
---|
373 | } |
---|
374 | |
---|
375 | /** |
---|
376 | * Starts ORXONOX.(not really implemented yet, but the function is there.\n |
---|
377 | * @param widget the widget that executed the start command |
---|
378 | * @param data additional data |
---|
379 | |
---|
380 | This is a Signal and can be executed through Widget::signal_connect |
---|
381 | */ |
---|
382 | #ifdef HAVE_GTK2 |
---|
383 | int GuiExec::quitGui(GtkWidget* widget, void* data) |
---|
384 | #else /* HAVE_GTK2 */ |
---|
385 | int GuiExec::quitGui(void* widget, void* data) |
---|
386 | #endif /* HAVE_GTK2 */ |
---|
387 | { |
---|
388 | GuiExec* exec = (GuiExec*)data; |
---|
389 | if(exec->shouldsave()) |
---|
390 | exec->writeToFile(Window::mainWindow); |
---|
391 | #ifdef HAVE_GTK2 |
---|
392 | gtk_main_quit(); |
---|
393 | while(gtk_events_pending()) gtk_main_iteration(); |
---|
394 | #endif /* HAVE_GTK2 */ |
---|
395 | } |
---|