Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk/gui: new Widget for FileSelector

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->isOption>0
180                              &&(static_cast<Option*>(widget)->isSaveable()))
181                             ||(widget->isOption<0
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->isOption <0)
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  //  if(widget->isOption == 0)
203  //    printf("%s\n",widget->title);
204  if(widget->isOption >= 1)
205    if (static_cast<Option*>(widget)->isSaveable())
206      {
207        char Buffer[256];
208        char* space2under;
209        strcpy(Buffer, static_cast<Option*>(widget)->title);
210        if(strchr(Buffer, '_'))
211          PRINTF(2)("Optionname %s is not Valid for Saving, because it includes an underscore\n", Buffer);
212        while(space2under = strchr(Buffer, ' '))
213          {
214            space2under[0] = '_';
215          }
216          fprintf(CONFIG_FILE, "%s = %s\n", Buffer, static_cast<Option*>(widget)->save());
217      }
218
219  if(widget->next != NULL)
220    this->writeFileText(widget->next, depth);
221}
222
223/**
224   \brief Reads in Configuration Data.
225   \param widget from which Widget on should be saved.
226*/
227void GuiExec::readFromFile(Widget* widget)
228{
229  this->CONFIG_FILE = fopen(this->confFile, "r");
230  VarInfo varInfo;
231  if(this->CONFIG_FILE)
232    {
233      Widget* groupWidget = widget;
234      char Buffer[256] = "";
235      char Variable[256]= "";
236      char* Value;
237      while(fscanf(this->CONFIG_FILE, "%s", Buffer) != EOF)
238        {
239          // group-search //
240          if(!strncmp(Buffer, "[", 1))
241            {
242              if((groupWidget = locateGroup(widget, Buffer, 1))==NULL)
243                {
244                  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);
245                  groupWidget = widget;
246                }
247              else
248                PRINT(5)("Group %s located.\n", static_cast<Packer*>(groupWidget)->groupName);
249            }
250          // option-setting //
251          if(!strcmp(Buffer, "="))
252            {
253              char* under2space;
254              while(under2space = strchr(Variable, '_'))
255                {
256                  sprintf(under2space, " %s", under2space+1);
257                }
258             
259              fscanf(this->CONFIG_FILE, "%s", Buffer);
260              varInfo.variableName = Variable;
261              varInfo.variableValue = Buffer;
262              groupWidget->walkThrough(this->readFileText, &varInfo, 0);
263              sprintf(Variable, "");
264            }
265          sprintf(Variable, "%s", Buffer);
266        }
267      widget->walkThrough(widget->setOptions, 0);
268    }
269}
270
271/**
272   \brief Maps Confugurations to the Options.
273   \param widget which widget downwards
274   \param varInfo Information about the Variable to read
275*/
276void GuiExec::readFileText(Widget* widget, void* varInfo)
277{
278  VarInfo* info =(VarInfo*)varInfo;
279  if(widget->title && !strcmp(widget->title, info->variableName))
280    {
281      PRINT(5)("Located Option %s.\n", widget->title);
282      if(widget->isOption >= 1)
283        static_cast<Option*>(widget)->load(info->variableValue);
284    }
285}
286
287/**
288   \brief Locates a Group.
289   \param widget The Widget from where to search from
290   \param groupName The GroupName for which to search.
291   \param depth The Depth of the search seen from the first widget we searched from.
292   \returns The Widget that holds the Group, or the NULL if the Group wasn't found.
293
294   \todo do this in gui-gtk.
295*/
296Widget* GuiExec::locateGroup(Widget* widget, char* groupName, int depth)
297{
298  Widget* tmp;
299
300  // removes the trailing and ending [ ].
301  if(!strncmp(groupName, "[", 1))
302    {
303      groupName = groupName+1;
304      groupName[strlen(groupName)-1] = '\0';
305    }
306
307  if(widget->isOption < 0)
308    {
309      if(static_cast<Packer*>(widget)->getGroupName() &&
310         !strcmp(groupName, static_cast<Packer*>(widget)->getGroupName()))
311        {
312          return widget;
313        }
314      else
315        {
316          if((tmp = locateGroup(static_cast<Packer*>(widget)->down, groupName, depth+1)) != NULL)
317            return tmp;
318        }
319    } 
320 
321  if(widget->next != NULL && depth != 0)
322    {
323      if((tmp = locateGroup(widget->next, groupName, depth)) != NULL)
324        return tmp;
325    }
326  return NULL;
327}
328
329/**
330   \brief Starts ORXONOX.(not really implemented yet, but the function is there.\n
331   \param widget the widget that executed the start command
332   \param data additional data
333
334   This is a Signal and can be executed through Widget::signal_connect
335*/
336#ifdef HAVE_GTK2
337int GuiExec::startOrxonox(GtkWidget* widget, void* data)
338#else /* HAVE_GTK2 */
339int GuiExec::startOrxonox(void* widget, void* data)
340#endif /* HAVE_GTK2 */
341{
342  Window::mainWindow->hide();
343
344#ifdef HAVE_GTK2
345  gtk_widget_destroy(Window::mainWindow->widget);
346#else
347  quitGui(widget, data);
348#endif /* HAVE_GTK2 */
349
350  PRINT(3)("Starting Orxonox\n");
351  Gui::startOrxonox = true;
352}
353
354/**
355   \brief Starts ORXONOX.(not really implemented yet, but the function is there.\n
356   \param widget the widget that executed the start command
357   \param data additional data
358
359   This is a Signal and can be executed through Widget::signal_connect
360*/
361#ifdef HAVE_GTK2
362int GuiExec::quitGui(GtkWidget* widget, void* data)
363#else /* HAVE_GTK2 */
364int GuiExec::quitGui(void* widget, void* data)
365#endif /* HAVE_GTK2 */
366{
367  GuiExec* exec = (GuiExec*)data;
368  if(exec->shouldsave())
369    exec->writeToFile(Window::mainWindow);
370#ifdef HAVE_GTK2
371  gtk_main_quit();
372  while(gtk_events_pending()) gtk_main_iteration();
373#endif /* HAVE_GTK2 */
374}
Note: See TracBrowser for help on using the repository browser.