Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk/gui: export to file now nicer. (danger heavy if-expression)

File size: 6.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  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        }
142      else
143        {
144          writeFileText (static_cast<Packer*>(widget)->down, depth);
145        }
146    } 
147 
148  if (widget->is_option >= 1)
149    if  (strcmp (static_cast<Option*>(widget)->flag_name, "") || strcmp (static_cast<Option*>(widget)->flag_name_short, ""))
150      {
151        char Buffer[256];
152        char* space2under;
153        sprintf (Buffer, "%s", static_cast<Option*>(widget)->label);
154        if (strchr (Buffer, '_'))
155          cout << "Warning Optionname" << Buffer << " is not Valid for Saving, because it includes an underscore" << endl;
156        while (space2under = strchr(Buffer, ' '))
157          {
158            sprintf (space2under, "_%s", space2under+1);
159          }
160        fprintf (CONFIG_FILE, "%s = %i\n", Buffer, static_cast<Option*>(widget)->value);
161      }
162
163  if (widget->next != NULL)
164    writeFileText (widget->next, depth);
165}
166
167/**
168   \brief Reads in Configuration Data.
169   \param widget from which Widget on should be saved.
170*/
171void OrxonoxGuiExec::readFromFile (Widget* widget)
172{
173  CONFIG_FILE = fopen (configFile, "r");
174  if (CONFIG_FILE)
175    {
176      char Buffer[256] = "";
177      char Variable[256]= "";
178      int Value;
179      while (fscanf (CONFIG_FILE, "%s", Buffer) != EOF)
180        {
181          if (!strcmp (Buffer, "="))
182            {
183              char* under2space;
184              while (under2space = strchr(Variable, '_'))
185                {
186                  sprintf (under2space, " %s", under2space+1);
187                }
188             
189              fscanf (CONFIG_FILE, "%s", Buffer);
190              Value = atoi(Buffer);
191              readFileText (widget, Variable, Value);
192              sprintf (Variable, "");
193            }
194          sprintf (Variable, "%s", Buffer);
195        }
196      widget->walkThrough(widget->setOptions);
197    }
198}
199
200/**
201   \brief Maps Confugurations to the Options.
202   \param widget which widget downwards
203   \param variableName the name of the Variable that should be set up.
204   \param variableValue the Value of the Variable that should be set up
205*/
206void OrxonoxGuiExec::readFileText (Widget* widget, char* variableName, int variableValue)
207{
208  if (widget->is_option >= 1)
209    if (!strcmp (static_cast<Option*>(widget)->label, variableName))
210        static_cast<Option*>(widget)->value = variableValue;
211
212  switch (widget->is_option)
213    {
214    case -1:
215      readFileText (static_cast<Container*>(widget)->down, variableName, variableValue);
216      break;
217    case -2:
218      readFileText (static_cast<Box*>(widget)->down, variableName, variableValue);
219      break;
220    } 
221
222  if (widget->next != NULL)
223    readFileText (widget->next, variableName, variableValue);
224}
225
226/**
227   \brief Starts ORXONOX. (not really implemented yet, but the function is there.\n
228   This is a Signal and can be executed through Widget::signal_connect
229   \param widget the widget that executed the start command
230   \param data additional data
231*/
232gint startOrxonox (GtkWidget *widget, Widget* data)
233{
234  cout << "Starting Orxonox" <<endl;
235}
Note: See TracBrowser for help on using the repository browser.