Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk/gui: minor optimization

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