Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2739 was 2739, checked in by bensch, 20 years ago

orxonox/trunk/gui: modified savable rutine. now a widget has to be set saveable, otherwise it won't be saved

File size: 7.9 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->is_option>0
128                              && (static_cast<Option*>(widget)->saveable) )
129                             || (widget->is_option<0
130                                 && strcmp (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->is_option <0)
138    {
139      if (strcmp (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 
151  if (widget->is_option >= 1)
152    if  (static_cast<Option*>(widget)->saveable)
153      {
154        char Buffer[256];
155        char* space2under;
156        sprintf (Buffer, "%s", static_cast<Option*>(widget)->label);
157        if (strchr (Buffer, '_'))
158          cout << "Warning Optionname" << Buffer << " is not Valid for Saving, because it includes an underscore" << endl;
159        while (space2under = strchr(Buffer, ' '))
160          {
161            sprintf (space2under, "_%s", space2under+1);
162          }
163        fprintf (CONFIG_FILE, "%s = %i\n", Buffer, static_cast<Option*>(widget)->value);
164      }
165
166  if (widget->next != NULL)
167    writeFileText (widget->next, depth);
168}
169
170/**
171   \brief Reads in Configuration Data.
172   \param widget from which Widget on should be saved.
173*/
174void OrxonoxGuiExec::readFromFile (Widget* widget)
175{
176  CONFIG_FILE = fopen (configFile, "r");
177  if (CONFIG_FILE)
178    {
179      Widget* groupWidget = widget;
180      char Buffer[256] = "";
181      char Variable[256]= "";
182      int Value;
183      while (fscanf (CONFIG_FILE, "%s", Buffer) != EOF)
184        {
185          if (!strncmp (Buffer, "[", 1))
186            {
187              if ((groupWidget = locateGroup (widget, Buffer, 1))==NULL)
188                {
189                  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";
190                  groupWidget = widget;
191                }
192            }
193          if (!strcmp (Buffer, "="))
194            {
195              char* under2space;
196              while (under2space = strchr(Variable, '_'))
197                {
198                  sprintf (under2space, " %s", under2space+1);
199                }
200             
201              fscanf (CONFIG_FILE, "%s", Buffer);
202              Value = atoi(Buffer);
203              readFileText (groupWidget, Variable, Value, 0);
204              sprintf (Variable, "");
205            }
206          sprintf (Variable, "%s", Buffer);
207        }
208      widget->walkThrough(widget->setOptions);
209    }
210}
211/**
212   \brief Maps Confugurations to the Options.
213   \param widget which widget downwards
214   \param variableName the name of the Variable that should be set up.
215   \param variableValue the Value of the Variable that should be set up
216   \param depth the depth of the local Widget
217*/
218void OrxonoxGuiExec::readFileText (Widget* widget, char* variableName, int variableValue, int depth)
219{
220  if (widget->is_option >= 1)
221    if (!strcmp (static_cast<Option*>(widget)->label, variableName))
222        static_cast<Option*>(widget)->value = variableValue;
223
224  if (widget->is_option < 0)
225    {
226      readFileText (static_cast<Packer*>(widget)->down, variableName, variableValue, depth+1);
227    } 
228
229  if (widget->next != NULL && depth !=0)
230    readFileText (widget->next, variableName, variableValue, depth);
231}
232
233/**
234   \brief locates a Group member
235*/
236Widget* OrxonoxGuiExec::locateGroup(Widget* widget, char* groupName, int depth)
237{
238  Widget* tmp;
239
240  // removes the trailing and ending [ ].
241  if (!strncmp (groupName, "[", 1))
242    {
243      groupName = groupName+1;
244      groupName[strlen(groupName)-1] = '\0';
245    }
246
247  if (widget->is_option < 0)
248    {
249      if (!strcmp(groupName, static_cast<Packer*>(widget)->getGroupName()))
250        {
251          return widget;
252        }
253      else
254        {
255          if ((tmp = locateGroup (static_cast<Packer*>(widget)->down, groupName, depth+1)) != NULL)
256            return tmp;
257        }
258    } 
259 
260  if (widget->next != NULL && depth != 0)
261    {
262      if ((tmp = locateGroup (widget->next, groupName, depth)) != NULL)
263        return tmp;
264    }
265  return NULL;
266}
267
268/**
269   \brief Starts ORXONOX. (not really implemented yet, but the function is there.\n
270   This is a Signal and can be executed through Widget::signal_connect
271   \param widget the widget that executed the start command
272   \param data additional data
273*/
274gint startOrxonox (GtkWidget *widget, Widget* data)
275{
276  cout << "Starting Orxonox" <<endl;
277}
Note: See TracBrowser for help on using the repository browser.