Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4733 was 4427, checked in by bensch, 20 years ago

orxonox/trunk: moved the gui to a more usefull directory

File size: 11.4 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    \brief Creates the Exec-Frame
42*/
43GuiExec::GuiExec(void)
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   \brief Destructs the Execution-stuff
134*/
135GuiExec::~GuiExec(void)
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   \brief 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   \brief 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(void) const
181{
182  return this->confFile;
183}
184
185/**
186   \brief 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    \brief 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   \brief 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   \brief 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}
306
307/**
308   \brief Maps Confugurations to the Options.
309   \param widget which widget downwards
310   \param varInfo Information about the Variable to read
311*/
312void GuiExec::readFileText(Widget* widget, void* varInfo)
313{
314  VarInfo* info =(VarInfo*)varInfo;
315  if(widget->title && !strcmp(widget->title, info->variableName))
316    {
317      PRINT(5)("Located Option %s.\n", widget->title);
318      if(widget->optionType > GUI_NOTHING)
319        static_cast<Option*>(widget)->load(info->variableValue);
320    }
321}
322
323/**
324   \brief Locates a Group.
325   \param widget The Widget from where to search from
326   \param groupName The GroupName for which to search.
327   \param depth The Depth of the search seen from the first widget we searched from.
328   \returns The Widget that holds the Group, or the NULL if the Group wasn't found.
329
330   \todo do this in gui-gtk.
331*/
332Widget* GuiExec::locateGroup(Widget* widget, char* groupName, int depth)
333{
334  Widget* tmp;
335
336  // removes the trailing and ending [ ].
337  if(!strncmp(groupName, "[", 1))
338    {
339      groupName = groupName+1;
340      groupName[strlen(groupName)-1] = '\0';
341    }
342
343  if(widget->optionType < GUI_NOTHING)
344    {
345      if(static_cast<Packer*>(widget)->getGroupName() &&
346         !strcmp(groupName, static_cast<Packer*>(widget)->getGroupName()))
347        {
348          return widget;
349        }
350      else
351        {
352          if((tmp = locateGroup(static_cast<Packer*>(widget)->down, groupName, depth+1)) != NULL)
353            return tmp;
354        }
355    } 
356 
357  if(widget->next != NULL && depth != 0)
358    {
359      if((tmp = locateGroup(widget->next, groupName, depth)) != NULL)
360        return tmp;
361    }
362  return NULL;
363}
364
365/**
366   \brief Starts ORXONOX.(not really implemented yet, but the function is there.\n
367   \param widget the widget that executed the start command
368   \param data additional data
369
370   This is a Signal and can be executed through Widget::signal_connect
371*/
372#ifdef HAVE_GTK2
373int GuiExec::startOrxonox(GtkWidget* widget, void* data)
374#else /* HAVE_GTK2 */
375int GuiExec::startOrxonox(void* widget, void* data)
376#endif /* HAVE_GTK2 */
377{
378  Window::mainWindow->hide();
379
380#ifdef HAVE_GTK2
381  gtk_widget_destroy(Window::mainWindow->widget);
382#else
383  quitGui(widget, data);
384#endif /* HAVE_GTK2 */
385
386  PRINT(3)("Starting Orxonox\n");
387  Gui::startOrxonox = true;
388}
389
390/**
391   \brief Starts ORXONOX.(not really implemented yet, but the function is there.\n
392   \param widget the widget that executed the start command
393   \param data additional data
394
395   This is a Signal and can be executed through Widget::signal_connect
396*/
397#ifdef HAVE_GTK2
398int GuiExec::quitGui(GtkWidget* widget, void* data)
399#else /* HAVE_GTK2 */
400int GuiExec::quitGui(void* widget, void* data)
401#endif /* HAVE_GTK2 */
402{
403  GuiExec* exec = (GuiExec*)data;
404  if(exec->shouldsave())
405    exec->writeToFile(Window::mainWindow);
406#ifdef HAVE_GTK2
407  gtk_main_quit();
408  while(gtk_events_pending()) gtk_main_iteration();
409#endif /* HAVE_GTK2 */
410}
Note: See TracBrowser for help on using the repository browser.