Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: windows MKDIR-s*ck works again

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