Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/gui/gui_exec.cc @ 4918

Last change on this file since 4918 was 4836, checked in by bensch, 19 years ago

orxonox/trunk: renamed all the \param → @param and so on in Doxygen tags.
Thanks a lot to the kDevelop team. this took since the last commit :)

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