Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/guiMerge/src/lib/gui/gui/gui_exec.cc @ 4051

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

orxonox/branches/guiMerge: better fileReadIn (more like orxonox)

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