Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/updater/src/gui/orxonox_gui_exec.cc @ 3296

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

orxonox/branches/updater: now the GUI just opens the first time, and the seccond time only if requested by —gui.

File size: 8.6 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 "orxonox_gui_exec.h"
27#include <iostream>
28#include <string>
29
30/**
31    \brief Creates the Exec-Frame
32    \param orxonoxGUI ExecFrame needs to know where to get the Options from
33*/
34OrxonoxGuiExec::OrxonoxGuiExec (Window* orxonoxGUI)
35{
36  configFile = (char*)malloc (512*sizeof (char));
37
38  execFrame = new Frame ("Execute-Tags:");
39  execBox = new Box ('v');
40  execFrame->setGroupName ("misc");
41 
42  start = new Button ("Start");
43#ifdef HAVE_GTK2
44  start->connectSignal ("clicked", startOrxonox);
45#endif /* HAVE_GTK2 */
46  execBox->fill (start);
47  saveSettings = new CheckButton ("Save Settings");
48  saveSettings->value = 1;
49  saveSettings->saveability();
50  execBox->fill (saveSettings);
51  verboseMode = new Menu ("verbose mode", "no output", "error", "warning", "info", "lastItem");
52  verboseMode->setFlagName ("verbose", "v", 0);
53  verboseMode->saveability();
54  execBox->fill (verboseMode);
55  alwaysShow = new CheckButton ("Always Show this Menu");
56  alwaysShow->setFlagName ("gui", "g", 0);
57  alwaysShow->saveability();
58  execBox->fill (alwaysShow);
59  quit = new Button ("Quit");
60#ifdef HAVE_GTK2
61  quit->connectSignal ("clicked", orxonoxGUI->orxonox_gui_quit);
62#endif /* HAVE_GTK2 */
63  execBox->fill (quit);
64
65  execFrame->fill (execBox);
66}
67
68/**
69   \brief Return the Frame
70   \return Returns the Exec-frame
71*/
72Widget* OrxonoxGuiExec::getWidget ()
73{
74  return execFrame;
75}
76
77/* FILE HANDLING */
78
79/**
80   \brief Sets the location of the configuration File.
81   \param filename the location of the configFile
82
83   The name will be parsed from ~/ to /home/[username] on unix and c:/Documents and Settings/username/Settings/ on Windows
84*/
85void OrxonoxGuiExec::setFilename (char* filename)
86{
87  char* buffer = (char*) malloc (512*sizeof(buffer));
88  sprintf (buffer, "%s", filename);
89  if (!strncmp (buffer, "~/", 2))
90  {
91#ifdef __WIN32__
92    sprintf (configFile, "%s/%s", getenv ("USERPROFILE"), buffer+2);
93#else
94    sprintf (configFile, "%s/%s", getenv ("HOME"), buffer+2);
95#endif
96  }
97  else if (buffer)
98    sprintf(configFile, "%s", buffer);
99  delete buffer;
100}
101
102/**
103   \returns The name of the Configuration-File
104*/
105char* OrxonoxGuiExec::getConfigFile(void)
106{
107  return configFile;
108}
109
110/**
111   \brief checks if a option should be saved.
112   \return 1 if it should 0 if not/
113*/
114int OrxonoxGuiExec::shouldsave ()
115{
116  return (static_cast<Option*>(saveSettings)->value);
117}
118
119/**
120    \brief Saves the configuration-file to the Disk.\n
121    \param widget from which Widget on should be saved.
122
123    this Function only opens and closes the file, in between OrxonoxGuiExec::writeFileText (Widget* widget) will execute the real writing process.
124*/
125void OrxonoxGuiExec::writeToFile (Widget* widget)
126{
127  CONFIG_FILE = fopen (configFile, "w");
128  if (CONFIG_FILE)
129    writeFileText (widget, 0);
130  fclose (CONFIG_FILE);
131}
132
133/**
134   \brief Actually writes into the configuration file to the disk.
135   \param widget from which Widget on should be saved.
136   \param depth initially "0", and grows higher, while new Groups are bundeled.
137*/
138void OrxonoxGuiExec::writeFileText (Widget* widget, int depth)
139{
140  int counter = 0;
141  while (counter < depth && ((widget->isOption>0
142                              && (static_cast<Option*>(widget)->isSaveable()) )
143                             || (widget->isOption<0
144                                 && static_cast<Packer*>(widget)->getGroupName())))
145    {
146      fprintf (CONFIG_FILE, "  ", depth);
147      counter++;
148    }
149 
150  // check if it is a Packer, and if it is, check if it has a name and if there is something in it.
151  if (widget->isOption <0)
152    {
153      if (static_cast<Packer*>(widget)->getGroupName())
154        {
155          fprintf (CONFIG_FILE, "[%s]\n", static_cast<Packer*>(widget)->getGroupName());
156          writeFileText (static_cast<Packer*>(widget)->down, depth+1);
157          fprintf(CONFIG_FILE, "\n");
158        }
159      else
160        {
161          writeFileText (static_cast<Packer*>(widget)->down, depth);
162        }
163    } 
164  //  if (widget->isOption == 0)
165  //    printf ("%s\n",widget->title);
166  if (widget->isOption >= 1)
167    if  (static_cast<Option*>(widget)->isSaveable())
168      {
169        char Buffer[256];
170        char* space2under;
171        strcpy (Buffer, static_cast<Option*>(widget)->title);
172        if (strchr (Buffer, '_'))
173          cout << "Warning Optionname" << Buffer << " is not Valid for Saving, because it includes an underscore" << endl;
174        while (space2under = strchr(Buffer, ' '))
175          {
176            space2under[0] = '_';
177          }
178        if (widget->isOption <=3)
179          fprintf (CONFIG_FILE, "%s = %d\n", Buffer, static_cast<Option*>(widget)->value);
180        else if (widget->isOption == 5)
181          fprintf (CONFIG_FILE, "%s = %s\n", Buffer, static_cast<OptionLabel*>(widget)->cValue);
182      }
183
184  if (widget->next != NULL)
185    writeFileText (widget->next, depth);
186}
187
188/**
189   \brief Reads in Configuration Data.
190   \param widget from which Widget on should be saved.
191*/
192void OrxonoxGuiExec::readFromFile (Widget* widget)
193{
194  CONFIG_FILE = fopen (configFile, "r");
195  VarInfo varInfo;
196  if (CONFIG_FILE)
197    {
198      Widget* groupWidget = widget;
199      char Buffer[256] = "";
200      char Variable[256]= "";
201      char* Value;
202      while (fscanf (CONFIG_FILE, "%s", Buffer) != EOF)
203        {
204          // group-search //
205          if (!strncmp (Buffer, "[", 1))
206            {
207              if ((groupWidget = locateGroup (widget, Buffer, 1))==NULL)
208                {
209                  cout << "!!There is no group called " << Buffer << " in this GUI.\n First best Widget will get the Infos assigned.\n Config-File will be updated in next Save\n";
210                  groupWidget = widget;
211                }
212            }
213          // option-setting //
214          if (!strcmp (Buffer, "="))
215            {
216              char* under2space;
217              while (under2space = strchr(Variable, '_'))
218                {
219                  sprintf (under2space, " %s", under2space+1);
220                }
221             
222              fscanf (CONFIG_FILE, "%s", Buffer);
223              varInfo.variableName = Variable;
224              varInfo.variableValue = Buffer;
225              groupWidget->walkThrough(readFileText, &varInfo, 0);
226              sprintf (Variable, "");
227            }
228          sprintf (Variable, "%s", Buffer);
229        }
230      widget->walkThrough(widget->setOptions, 0);
231    }
232}
233/**
234   \brief Maps Confugurations to the Options.
235   \param widget which widget downwards
236   \param varInfo Information about the Variable to read
237*/
238void OrxonoxGuiExec::readFileText (Widget* widget, void* varInfo)
239{
240  VarInfo* info = (VarInfo*)varInfo;
241
242  if (widget->isOption >= 1 && widget->isOption <= 3)
243    {
244      if (!strcmp (static_cast<Option*>(widget)->title, info->variableName))
245        static_cast<Option*>(widget)->value = atoi(info->variableValue);
246    }
247  else if (widget->isOption == 5)
248    {
249       if (!strcmp (static_cast<Option*>(widget)->title, info->variableName))
250        static_cast<OptionLabel*>(widget)->setValue(info->variableValue);
251    }
252}
253
254/**
255   \brief Locates a Group.
256   \param widget The Widget from where to search from
257   \param groupName The GroupName for which to search.
258   \param depth The Depth of the search seen from the first widget we searched from.
259   \returns The Widget that holds the Group, or the NULL if the Group wasn't found.
260*/
261Widget* OrxonoxGuiExec::locateGroup(Widget* widget, char* groupName, int depth)
262{
263  Widget* tmp;
264
265  // removes the trailing and ending [ ].
266  if (!strncmp (groupName, "[", 1))
267    {
268      groupName = groupName+1;
269      groupName[strlen(groupName)-1] = '\0';
270    }
271
272  if (widget->isOption < 0)
273    {
274      if (static_cast<Packer*>(widget)->getGroupName() && !strcmp(groupName, static_cast<Packer*>(widget)->getGroupName()))
275        {
276          return widget;
277        }
278      else
279        {
280          if ((tmp = locateGroup (static_cast<Packer*>(widget)->down, groupName, depth+1)) != NULL)
281            return tmp;
282        }
283    } 
284 
285  if (widget->next != NULL && depth != 0)
286    {
287      if ((tmp = locateGroup (widget->next, groupName, depth)) != NULL)
288        return tmp;
289    }
290  return NULL;
291}
292
293#ifdef HAVE_GTK2
294/**
295   \brief Starts ORXONOX. (not really implemented yet, but the function is there.\n
296   This is a Signal and can be executed through Widget::signal_connect
297   \param widget the widget that executed the start command
298   \param data additional data
299*/
300gint OrxonoxGuiExec::startOrxonox (GtkWidget *widget, Widget* data)
301{
302  cout << "Starting Orxonox" <<endl;
303}
304#endif /* HAVE_GTK2 */
Note: See TracBrowser for help on using the repository browser.