Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk/gui: compiling as not-GTK possibe, but it is not usable yet

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