Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk/gui: new isOption-tag of Widgets: optionType

File size: 10.5 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("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("Save Settings");
68      this->saveSettings->value = 1;
69      this->saveSettings->saveability();
70      execBox->fill(this->saveSettings);
71
72      verboseMode = new Menu("verbose mode", "nothing", "error", "warning", "info", "lastItem");
73      verboseMode->setFlagName("verbose", "v", 2);
74      verboseMode->saveability();
75      execBox->fill(verboseMode);
76
77      alwaysShow = new CheckButton("Always Show this Menu");
78      alwaysShow->setFlagName("gui", "g", 0);
79      alwaysShow->saveability();
80      execBox->fill(alwaysShow);
81
82      quit = new Button("Quit");
83#ifdef HAVE_GTK2
84      quit->connectSignal("clicked", this, GuiExec::quitGui);
85      //  Window::mainWindow->connectSignal("remove", this, GuiExec::quitGui);
86      Window::mainWindow->connectSignal("destroy", this, GuiExec::quitGui);
87#endif /* HAVE_GTK2 */
88      execBox->fill(quit);
89    }
90    execFrame->fill(execBox);
91  }
92  setMainWidget(execFrame);
93}
94
95/**
96   \brief Destructs the Execution-stuff
97*/
98GuiExec::~GuiExec(void)
99{
100  if(this->confFile)
101    delete []this->confFile;
102  if(this->confDir)
103    delete []this->confDir;
104}
105
106/* FILE HANDLING */
107
108/**
109   \brief sets the Directory of the configuration files
110   \param confDir the Directory for the configuration files
111*/
112void GuiExec::setConfDir(const char* confDir)
113{
114  this->confDir = ResourceManager::homeDirCheck(confDir);
115
116  PRINTF(3)("Config Directory is: %s.\n", this->confDir);
117  //! \todo F** Windows-support
118#ifndef __WIN32__
119  mkdir(this->confDir, 0755);
120#else /* __WiN32__ */
121  mkdir(this->confDir);
122#endif /* __WIN32__ */
123}
124
125/**
126   \brief Sets the location of the configuration File.
127   \param fileName the location of the configFile
128
129   The name will be parsed from ~/ to /home/[username] on unix and c:/Documents and Settings/username/Settings/ on Windows
130*/
131void GuiExec::setConfFile(const char* fileName)
132{
133  if (!this->confDir)
134    this->setConfDir("~/");
135  this->confFile = new char[strlen(this->confDir)+strlen(fileName)+2];
136  sprintf(this->confFile, "%s/%s", this->confDir, fileName);
137  PRINTF(3)("ConfigurationFile is %s.\n", this->confFile);
138}
139
140/**
141   \returns The name of the Configuration-File
142*/
143const char* GuiExec::getConfigFile(void) const
144{
145  return this->confFile;
146}
147
148/**
149   \brief checks if a option should be saved.
150   \return 1 if it should 0 if not/
151*/
152int GuiExec::shouldsave()
153{
154  return(static_cast<Option*>(this->saveSettings)->value);
155}
156
157/**
158    \brief Saves the configuration-file to the Disk.\n
159    \param widget from which Widget on should be saved.
160
161    this Function only opens and closes the file, in between GuiExec::writeFileText(Widget* widget) will execute the real writing process.
162*/
163void GuiExec::writeToFile(Widget* widget)
164{
165  this->CONFIG_FILE = fopen(this->confFile, "w");
166  if(this->CONFIG_FILE)
167    this->writeFileText(widget, 0);
168  fclose(this->CONFIG_FILE);
169}
170
171/**
172   \brief Actually writes into the configuration file to the disk.
173   \param widget from which Widget on should be saved.
174   \param depth initially "0", and grows higher, while new Groups are bundeled.
175*/
176void GuiExec::writeFileText(Widget* widget, int depth)
177{
178  int counter = 0;
179  while(counter < depth &&((widget->optionType > GUI_NOTHING
180                              &&(static_cast<Option*>(widget)->isSaveable()))
181                             ||(widget->optionType < GUI_NOTHING
182                                && static_cast<Packer*>(widget)->getGroupName())))
183    {
184      fprintf(this->CONFIG_FILE, "  ", depth);
185      counter++;
186    }
187 
188  // check if it is a Packer, and if it is, check if it has a name and if there is something in it.
189  if(widget->optionType < GUI_NOTHING)
190    {
191      if(static_cast<Packer*>(widget)->getGroupName())
192        {
193          fprintf(CONFIG_FILE, "[%s]\n", static_cast<Packer*>(widget)->getGroupName());
194          this->writeFileText(static_cast<Packer*>(widget)->down, depth+1);
195          fprintf(CONFIG_FILE, "\n");
196        }
197      else
198        {
199          this->writeFileText(static_cast<Packer*>(widget)->down, depth);
200        }
201    } 
202
203  if(widget->optionType > GUI_NOTHING)
204    if (static_cast<Option*>(widget)->isSaveable())
205      {
206        char Buffer[256];
207        char* space2under;
208        strcpy(Buffer, static_cast<Option*>(widget)->title);
209        if(strchr(Buffer, '_'))
210          PRINTF(2)("Optionname %s is not Valid for Saving, because it includes an underscore\n", Buffer);
211        while(space2under = strchr(Buffer, ' '))
212          {
213            space2under[0] = '_';
214          }
215          fprintf(CONFIG_FILE, "%s = %s\n", Buffer, static_cast<Option*>(widget)->save());
216      }
217
218  if(widget->next != NULL)
219    this->writeFileText(widget->next, depth);
220}
221
222/**
223   \brief Reads in Configuration Data.
224   \param widget from which Widget on should be saved.
225*/
226void GuiExec::readFromFile(Widget* widget)
227{
228  this->CONFIG_FILE = fopen(this->confFile, "r");
229  VarInfo varInfo;
230  if(this->CONFIG_FILE)
231    {
232      Widget* groupWidget = widget;
233      char Buffer[256] = "";
234      char Variable[256]= "";
235      char* Value;
236      while(fscanf(this->CONFIG_FILE, "%s", Buffer) != EOF)
237        {
238          // group-search //
239          if(!strncmp(Buffer, "[", 1))
240            {
241              if((groupWidget = locateGroup(widget, Buffer, 1))==NULL)
242                {
243                  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);
244                  groupWidget = widget;
245                }
246              else
247                PRINT(5)("Group %s located.\n", static_cast<Packer*>(groupWidget)->groupName);
248            }
249          // option-setting //
250          if(!strcmp(Buffer, "="))
251            {
252              char* under2space;
253              while(under2space = strchr(Variable, '_'))
254                {
255                  sprintf(under2space, " %s", under2space+1);
256                }
257             
258              fscanf(this->CONFIG_FILE, "%s", Buffer);
259              varInfo.variableName = Variable;
260              varInfo.variableValue = Buffer;
261              groupWidget->walkThrough(this->readFileText, &varInfo, 0);
262              sprintf(Variable, "");
263            }
264          sprintf(Variable, "%s", Buffer);
265        }
266      widget->walkThrough(widget->setOptions, 0);
267    }
268}
269
270/**
271   \brief Maps Confugurations to the Options.
272   \param widget which widget downwards
273   \param varInfo Information about the Variable to read
274*/
275void GuiExec::readFileText(Widget* widget, void* varInfo)
276{
277  VarInfo* info =(VarInfo*)varInfo;
278  if(widget->title && !strcmp(widget->title, info->variableName))
279    {
280      PRINT(5)("Located Option %s.\n", widget->title);
281      if(widget->optionType > GUI_NOTHING)
282        static_cast<Option*>(widget)->load(info->variableValue);
283    }
284}
285
286/**
287   \brief Locates a Group.
288   \param widget The Widget from where to search from
289   \param groupName The GroupName for which to search.
290   \param depth The Depth of the search seen from the first widget we searched from.
291   \returns The Widget that holds the Group, or the NULL if the Group wasn't found.
292
293   \todo do this in gui-gtk.
294*/
295Widget* GuiExec::locateGroup(Widget* widget, char* groupName, int depth)
296{
297  Widget* tmp;
298
299  // removes the trailing and ending [ ].
300  if(!strncmp(groupName, "[", 1))
301    {
302      groupName = groupName+1;
303      groupName[strlen(groupName)-1] = '\0';
304    }
305
306  if(widget->optionType < GUI_NOTHING)
307    {
308      if(static_cast<Packer*>(widget)->getGroupName() &&
309         !strcmp(groupName, static_cast<Packer*>(widget)->getGroupName()))
310        {
311          return widget;
312        }
313      else
314        {
315          if((tmp = locateGroup(static_cast<Packer*>(widget)->down, groupName, depth+1)) != NULL)
316            return tmp;
317        }
318    } 
319 
320  if(widget->next != NULL && depth != 0)
321    {
322      if((tmp = locateGroup(widget->next, groupName, depth)) != NULL)
323        return tmp;
324    }
325  return NULL;
326}
327
328/**
329   \brief Starts ORXONOX.(not really implemented yet, but the function is there.\n
330   \param widget the widget that executed the start command
331   \param data additional data
332
333   This is a Signal and can be executed through Widget::signal_connect
334*/
335#ifdef HAVE_GTK2
336int GuiExec::startOrxonox(GtkWidget* widget, void* data)
337#else /* HAVE_GTK2 */
338int GuiExec::startOrxonox(void* widget, void* data)
339#endif /* HAVE_GTK2 */
340{
341  Window::mainWindow->hide();
342
343#ifdef HAVE_GTK2
344  gtk_widget_destroy(Window::mainWindow->widget);
345#else
346  quitGui(widget, data);
347#endif /* HAVE_GTK2 */
348
349  PRINT(3)("Starting Orxonox\n");
350  Gui::startOrxonox = true;
351}
352
353/**
354   \brief Starts ORXONOX.(not really implemented yet, but the function is there.\n
355   \param widget the widget that executed the start command
356   \param data additional data
357
358   This is a Signal and can be executed through Widget::signal_connect
359*/
360#ifdef HAVE_GTK2
361int GuiExec::quitGui(GtkWidget* widget, void* data)
362#else /* HAVE_GTK2 */
363int GuiExec::quitGui(void* widget, void* data)
364#endif /* HAVE_GTK2 */
365{
366  GuiExec* exec = (GuiExec*)data;
367  if(exec->shouldsave())
368    exec->writeToFile(Window::mainWindow);
369#ifdef HAVE_GTK2
370  gtk_main_quit();
371  while(gtk_events_pending()) gtk_main_iteration();
372#endif /* HAVE_GTK2 */
373}
Note: See TracBrowser for help on using the repository browser.