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 "orxonox_gui_exec.h" |
---|
27 | #include <iostream> |
---|
28 | #include <string> |
---|
29 | |
---|
30 | HashTable* orxonoxFlagHash; |
---|
31 | |
---|
32 | /** |
---|
33 | \brief Creates the Exec-Frame |
---|
34 | \param orxonoxGUI ExecFrame needs to know where to get the Options from |
---|
35 | */ |
---|
36 | OrxonoxGuiExec::OrxonoxGuiExec (Window* orxonoxGUI) |
---|
37 | { |
---|
38 | configFile = (char*)malloc (512*sizeof (char)); |
---|
39 | |
---|
40 | execFrame = new Frame ("Execute-Tags:"); |
---|
41 | execBox = new Box ('v'); |
---|
42 | execFrame->setGroupName ("misc"); |
---|
43 | |
---|
44 | start = new Button ("Start"); |
---|
45 | #ifdef HAVE_GTK2 |
---|
46 | start->connectSignal ("clicked", this, startOrxonox); |
---|
47 | #endif /* HAVE_GTK2 */ |
---|
48 | execBox->fill (start); |
---|
49 | saveSettings = new CheckButton ("Save Settings"); |
---|
50 | saveSettings->value = 1; |
---|
51 | saveSettings->saveability(); |
---|
52 | execBox->fill (saveSettings); |
---|
53 | verboseMode = new Menu ("verbose mode", "no output", "error", "warning", "info", "lastItem"); |
---|
54 | verboseMode->setFlagName ("verbose", "v", 0); |
---|
55 | verboseMode->saveability(); |
---|
56 | execBox->fill (verboseMode); |
---|
57 | alwaysShow = new CheckButton ("Always Show this Menu"); |
---|
58 | alwaysShow->setFlagName ("gui", "g", 0); |
---|
59 | alwaysShow->saveability(); |
---|
60 | execBox->fill (alwaysShow); |
---|
61 | quit = new Button ("Quit"); |
---|
62 | #ifdef HAVE_GTK2 |
---|
63 | quit->connectSignal ("clicked", this, OrxonoxGuiExec::quitOrxonox); |
---|
64 | orxonoxGUI->connectSignal ("destroy", this, OrxonoxGuiExec::quitOrxonox); |
---|
65 | //! orxonoxGUI->connectSignal ("delete_event", this, OrxonoxGuiExec::quitOrxonox); \todo fix this to work. |
---|
66 | #endif /* HAVE_GTK2 */ |
---|
67 | execBox->fill (quit); |
---|
68 | |
---|
69 | execFrame->fill (execBox); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | \brief Return the Frame |
---|
74 | \return Returns the Exec-frame |
---|
75 | */ |
---|
76 | Widget* OrxonoxGuiExec::getWidget () |
---|
77 | { |
---|
78 | return execFrame; |
---|
79 | } |
---|
80 | |
---|
81 | /* FILE HANDLING */ |
---|
82 | |
---|
83 | /** |
---|
84 | \brief Sets the location of the configuration File. |
---|
85 | \param filename the location of the configFile |
---|
86 | |
---|
87 | The name will be parsed from ~/ to /home/[username] on unix and c:/Documents and Settings/username/Settings/ on Windows |
---|
88 | */ |
---|
89 | void OrxonoxGuiExec::setFilename (char* filename) |
---|
90 | { |
---|
91 | char* buffer = (char*) malloc (512*sizeof(buffer)); |
---|
92 | sprintf (buffer, "%s", filename); |
---|
93 | if (!strncmp (buffer, "~/", 2)) |
---|
94 | { |
---|
95 | #ifdef __WIN32__ |
---|
96 | sprintf (configFile, "%s/%s", getenv ("USERPROFILE"), buffer+2); |
---|
97 | #else |
---|
98 | sprintf (configFile, "%s/%s", getenv ("HOME"), buffer+2); |
---|
99 | #endif |
---|
100 | } |
---|
101 | else if (buffer) |
---|
102 | sprintf(configFile, "%s", buffer); |
---|
103 | delete buffer; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | \returns The name of the Configuration-File |
---|
108 | */ |
---|
109 | char* OrxonoxGuiExec::getConfigFile(void) |
---|
110 | { |
---|
111 | return configFile; |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | \brief checks if a option should be saved. |
---|
116 | \return 1 if it should 0 if not/ |
---|
117 | */ |
---|
118 | int OrxonoxGuiExec::shouldsave () |
---|
119 | { |
---|
120 | return (static_cast<Option*>(saveSettings)->value); |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | \brief Saves the configuration-file to the Disk.\n |
---|
125 | \param widget from which Widget on should be saved. |
---|
126 | |
---|
127 | this Function only opens and closes the file, in between OrxonoxGuiExec::writeFileText (Widget* widget) will execute the real writing process. |
---|
128 | */ |
---|
129 | void OrxonoxGuiExec::writeToFile (Widget* widget) |
---|
130 | { |
---|
131 | CONFIG_FILE = fopen (configFile, "w"); |
---|
132 | if (CONFIG_FILE) |
---|
133 | writeFileText (widget, 0); |
---|
134 | fclose (CONFIG_FILE); |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | \brief Actually writes into the configuration file to the disk. |
---|
139 | \param widget from which Widget on should be saved. |
---|
140 | \param depth initially "0", and grows higher, while new Groups are bundeled. |
---|
141 | */ |
---|
142 | void OrxonoxGuiExec::writeFileText (Widget* widget, int depth) |
---|
143 | { |
---|
144 | int counter = 0; |
---|
145 | while (counter < depth && ((widget->isOption>0 |
---|
146 | && (static_cast<Option*>(widget)->isSaveable()) ) |
---|
147 | || (widget->isOption<0 |
---|
148 | && static_cast<Packer*>(widget)->getGroupName()))) |
---|
149 | { |
---|
150 | fprintf (CONFIG_FILE, " ", depth); |
---|
151 | counter++; |
---|
152 | } |
---|
153 | |
---|
154 | // check if it is a Packer, and if it is, check if it has a name and if there is something in it. |
---|
155 | if (widget->isOption <0) |
---|
156 | { |
---|
157 | if (static_cast<Packer*>(widget)->getGroupName()) |
---|
158 | { |
---|
159 | fprintf (CONFIG_FILE, "[%s]\n", static_cast<Packer*>(widget)->getGroupName()); |
---|
160 | writeFileText (static_cast<Packer*>(widget)->down, depth+1); |
---|
161 | fprintf(CONFIG_FILE, "\n"); |
---|
162 | } |
---|
163 | else |
---|
164 | { |
---|
165 | writeFileText (static_cast<Packer*>(widget)->down, depth); |
---|
166 | } |
---|
167 | } |
---|
168 | // if (widget->isOption == 0) |
---|
169 | // printf ("%s\n",widget->title); |
---|
170 | if (widget->isOption >= 1) |
---|
171 | if (static_cast<Option*>(widget)->isSaveable()) |
---|
172 | { |
---|
173 | char Buffer[256]; |
---|
174 | char* space2under; |
---|
175 | strcpy (Buffer, static_cast<Option*>(widget)->title); |
---|
176 | if (strchr (Buffer, '_')) |
---|
177 | cout << "Warning Optionname" << Buffer << " is not Valid for Saving, because it includes an underscore" << endl; |
---|
178 | while (space2under = strchr(Buffer, ' ')) |
---|
179 | { |
---|
180 | space2under[0] = '_'; |
---|
181 | } |
---|
182 | if (widget->isOption <=3) |
---|
183 | fprintf (CONFIG_FILE, "%s = %d\n", Buffer, static_cast<Option*>(widget)->value); |
---|
184 | else if (widget->isOption == 5) |
---|
185 | fprintf (CONFIG_FILE, "%s = %s\n", Buffer, static_cast<OptionLabel*>(widget)->cValue); |
---|
186 | } |
---|
187 | |
---|
188 | if (widget->next != NULL) |
---|
189 | writeFileText (widget->next, depth); |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | \brief Reads in Configuration Data. |
---|
194 | \param widget from which Widget on should be saved. |
---|
195 | */ |
---|
196 | void OrxonoxGuiExec::readFromFile (Widget* widget) |
---|
197 | { |
---|
198 | CONFIG_FILE = fopen (configFile, "r"); |
---|
199 | VarInfo varInfo; |
---|
200 | if (CONFIG_FILE) |
---|
201 | { |
---|
202 | Widget* groupWidget = widget; |
---|
203 | char Buffer[256] = ""; |
---|
204 | char Variable[256]= ""; |
---|
205 | char* Value; |
---|
206 | while (fscanf (CONFIG_FILE, "%s", Buffer) != EOF) |
---|
207 | { |
---|
208 | // group-search // |
---|
209 | if (!strncmp (Buffer, "[", 1)) |
---|
210 | { |
---|
211 | if ((groupWidget = locateGroup (widget, Buffer, 1))==NULL) |
---|
212 | { |
---|
213 | cout << "!!There is no group called " << Buffer << " in this GUI.\n First best Widget will get the Infos assigned.\n Config-File will be updated in next Save\n"; |
---|
214 | groupWidget = widget; |
---|
215 | } |
---|
216 | } |
---|
217 | // option-setting // |
---|
218 | if (!strcmp (Buffer, "=")) |
---|
219 | { |
---|
220 | char* under2space; |
---|
221 | while (under2space = strchr(Variable, '_')) |
---|
222 | { |
---|
223 | sprintf (under2space, " %s", under2space+1); |
---|
224 | } |
---|
225 | |
---|
226 | fscanf (CONFIG_FILE, "%s", Buffer); |
---|
227 | varInfo.variableName = Variable; |
---|
228 | varInfo.variableValue = Buffer; |
---|
229 | groupWidget->walkThrough(readFileText, &varInfo, 0); |
---|
230 | sprintf (Variable, ""); |
---|
231 | } |
---|
232 | sprintf (Variable, "%s", Buffer); |
---|
233 | } |
---|
234 | widget->walkThrough(widget->setOptions, 0); |
---|
235 | } |
---|
236 | } |
---|
237 | /** |
---|
238 | \brief Maps Confugurations to the Options. |
---|
239 | \param widget which widget downwards |
---|
240 | \param varInfo Information about the Variable to read |
---|
241 | */ |
---|
242 | void OrxonoxGuiExec::readFileText (Widget* widget, void* varInfo) |
---|
243 | { |
---|
244 | VarInfo* info = (VarInfo*)varInfo; |
---|
245 | |
---|
246 | if (widget->isOption >= 1 && widget->isOption <= 3) |
---|
247 | { |
---|
248 | if (!strcmp (static_cast<Option*>(widget)->title, info->variableName)) |
---|
249 | static_cast<Option*>(widget)->value = atoi(info->variableValue); |
---|
250 | } |
---|
251 | else if (widget->isOption == 5) |
---|
252 | { |
---|
253 | if (!strcmp (static_cast<Option*>(widget)->title, info->variableName)) |
---|
254 | static_cast<OptionLabel*>(widget)->setValue(info->variableValue); |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | /** |
---|
259 | \brief Locates a Group. |
---|
260 | \param widget The Widget from where to search from |
---|
261 | \param groupName The GroupName for which to search. |
---|
262 | \param depth The Depth of the search seen from the first widget we searched from. |
---|
263 | \returns The Widget that holds the Group, or the NULL if the Group wasn't found. |
---|
264 | */ |
---|
265 | Widget* OrxonoxGuiExec::locateGroup(Widget* widget, char* groupName, int depth) |
---|
266 | { |
---|
267 | Widget* tmp; |
---|
268 | |
---|
269 | // removes the trailing and ending [ ]. |
---|
270 | if (!strncmp (groupName, "[", 1)) |
---|
271 | { |
---|
272 | groupName = groupName+1; |
---|
273 | groupName[strlen(groupName)-1] = '\0'; |
---|
274 | } |
---|
275 | |
---|
276 | if (widget->isOption < 0) |
---|
277 | { |
---|
278 | if (static_cast<Packer*>(widget)->getGroupName() && !strcmp(groupName, static_cast<Packer*>(widget)->getGroupName())) |
---|
279 | { |
---|
280 | return widget; |
---|
281 | } |
---|
282 | else |
---|
283 | { |
---|
284 | if ((tmp = locateGroup (static_cast<Packer*>(widget)->down, groupName, depth+1)) != NULL) |
---|
285 | return tmp; |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | if (widget->next != NULL && depth != 0) |
---|
290 | { |
---|
291 | if ((tmp = locateGroup (widget->next, groupName, depth)) != NULL) |
---|
292 | return tmp; |
---|
293 | } |
---|
294 | return NULL; |
---|
295 | } |
---|
296 | |
---|
297 | #ifdef HAVE_GTK2 |
---|
298 | /** |
---|
299 | \brief Starts ORXONOX. (not really implemented yet, but the function is there.\n |
---|
300 | \param widget the widget that executed the start command |
---|
301 | \param data additional data |
---|
302 | |
---|
303 | This is a Signal and can be executed through Widget::signal_connect |
---|
304 | */ |
---|
305 | int OrxonoxGuiExec::startOrxonox (GtkWidget* widget, void* data) |
---|
306 | { |
---|
307 | OrxonoxGuiExec* exec = (OrxonoxGuiExec*)data; |
---|
308 | if (exec->shouldsave()) |
---|
309 | exec->writeToFile (Window::mainWindow); |
---|
310 | cout << "Starting Orxonox" <<endl; |
---|
311 | gtk_main_quit(); |
---|
312 | system("cd ..;./orxonox"); //!< \todo fix this. should execute orxonox for real (coded not over the shell) |
---|
313 | } |
---|
314 | |
---|
315 | /** |
---|
316 | \brief Starts ORXONOX. (not really implemented yet, but the function is there.\n |
---|
317 | \param widget the widget that executed the start command |
---|
318 | \param data additional data |
---|
319 | |
---|
320 | This is a Signal and can be executed through Widget::signal_connect |
---|
321 | */ |
---|
322 | int OrxonoxGuiExec::quitOrxonox (GtkWidget* widget, void* data) |
---|
323 | { |
---|
324 | OrxonoxGuiExec* exec = (OrxonoxGuiExec*)data; |
---|
325 | PRINT(3)( "Quitting Orxonox %p\n", exec); |
---|
326 | if (exec->shouldsave()) |
---|
327 | exec->writeToFile (Window::mainWindow); |
---|
328 | gtk_main_quit(); |
---|
329 | } |
---|
330 | #else /* HAVE_GTK2 */ |
---|
331 | /** |
---|
332 | \brief Starts ORXONOX. (not really implemented yet, but the function is there.\n |
---|
333 | \param widget the widget that executed the start command |
---|
334 | \param data additional data |
---|
335 | */ |
---|
336 | int OrxonoxGuiExec::startOrxonox (void* widget, void* data) |
---|
337 | { |
---|
338 | OrxonoxGuiExec* exec = (OrxonoxGuiExec*)data; |
---|
339 | COUT(3) << "Starting Orxonox" <<endl; |
---|
340 | if (exec->shouldsave()) |
---|
341 | exec->writeToFile (Window::mainWindow); |
---|
342 | } |
---|
343 | /** |
---|
344 | \brief Quits ORXONOX. |
---|
345 | \param widget the widget that executed the Quit command |
---|
346 | \param data additional data |
---|
347 | |
---|
348 | This is a Signal and can be executed through Widget::signal_connect |
---|
349 | */ |
---|
350 | int OrxonoxGuiExec::quitOrxonox (void* widget, void* data) |
---|
351 | { |
---|
352 | OrxonoxGuiExec* exec = (OrxonoxGuiExec*)data; |
---|
353 | COUT(3) << "Quiting Orxonox" <<endl; |
---|
354 | if (exec->shouldsave()) |
---|
355 | exec->writeToFile (Window::mainWindow); |
---|
356 | } |
---|
357 | |
---|
358 | #endif /* HAVE_GTK2 */ |
---|