Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/gui/orxonox_gui_exec.cc @ 3161

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

orxonox/trunk/gui: OptionLabel now has a member cValue for saving its state beside its name

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