Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/updater/src/gui/orxonox_gui_exec.cc @ 3294

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

orxonox/branches/updater: merged debug.h from trunk, and just some doxy-tags added

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->saveability();
50  execBox->fill (saveSettings);
51  verboseMode = new Menu ("verbose mode", "no output", "error", "warning", "info", "lastItem");
52  verboseMode->setFlagName ("verbose", "v", 0);
53  verboseMode->saveability();
54  execBox->fill (verboseMode);
55  alwaysShow = new CheckButton ("Always Show this Menu");
56  alwaysShow->setFlagName ("gui", "g", 0);
57  alwaysShow->saveability();
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    \param widget from which Widget on should be saved.
113
114    this Function only opens and closes the file, in between OrxonoxGuiExec::writeFileText (Widget* widget) will execute the real writing process.
115*/
116void OrxonoxGuiExec::writeToFile (Widget* widget)
117{
118  CONFIG_FILE = fopen (configFile, "w");
119  if (CONFIG_FILE)
120    writeFileText (widget, 0);
121  fclose (CONFIG_FILE);
122}
123
124/**
125   \brief Actually writes into the configuration file to the disk.
126   \param widget from which Widget on should be saved.
127   \param depth initially "0", and grows higher, while new Groups are bundeled.
128*/
129void OrxonoxGuiExec::writeFileText (Widget* widget, int depth)
130{
131  int counter = 0;
132  while (counter < depth && ((widget->isOption>0
133                              && (static_cast<Option*>(widget)->isSaveable()) )
134                             || (widget->isOption<0
135                                 && static_cast<Packer*>(widget)->getGroupName())))
136    {
137      fprintf (CONFIG_FILE, "  ", depth);
138      counter++;
139    }
140 
141  // check if it is a Packer, and if it is, check if it has a name and if there is something in it.
142  if (widget->isOption <0)
143    {
144      if (static_cast<Packer*>(widget)->getGroupName())
145        {
146          fprintf (CONFIG_FILE, "[%s]\n", static_cast<Packer*>(widget)->getGroupName());
147          writeFileText (static_cast<Packer*>(widget)->down, depth+1);
148          fprintf(CONFIG_FILE, "\n");
149        }
150      else
151        {
152          writeFileText (static_cast<Packer*>(widget)->down, depth);
153        }
154    } 
155  //  if (widget->isOption == 0)
156  //    printf ("%s\n",widget->title);
157  if (widget->isOption >= 1)
158    if  (static_cast<Option*>(widget)->isSaveable())
159      {
160        char Buffer[256];
161        char* space2under;
162        strcpy (Buffer, static_cast<Option*>(widget)->title);
163        if (strchr (Buffer, '_'))
164          cout << "Warning Optionname" << Buffer << " is not Valid for Saving, because it includes an underscore" << endl;
165        while (space2under = strchr(Buffer, ' '))
166          {
167            space2under[0] = '_';
168          }
169        if (widget->isOption <=3)
170          fprintf (CONFIG_FILE, "%s = %d\n", Buffer, static_cast<Option*>(widget)->value);
171        else if (widget->isOption == 5)
172          fprintf (CONFIG_FILE, "%s = %s\n", Buffer, static_cast<OptionLabel*>(widget)->cValue);
173      }
174
175  if (widget->next != NULL)
176    writeFileText (widget->next, depth);
177}
178
179/**
180   \brief Reads in Configuration Data.
181   \param widget from which Widget on should be saved.
182*/
183void OrxonoxGuiExec::readFromFile (Widget* widget)
184{
185  CONFIG_FILE = fopen (configFile, "r");
186  VarInfo varInfo;
187  if (CONFIG_FILE)
188    {
189      Widget* groupWidget = widget;
190      char Buffer[256] = "";
191      char Variable[256]= "";
192      char* Value;
193      while (fscanf (CONFIG_FILE, "%s", Buffer) != EOF)
194        {
195          // group-search //
196          if (!strncmp (Buffer, "[", 1))
197            {
198              if ((groupWidget = locateGroup (widget, Buffer, 1))==NULL)
199                {
200                  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";
201                  groupWidget = widget;
202                }
203            }
204          // option-setting //
205          if (!strcmp (Buffer, "="))
206            {
207              char* under2space;
208              while (under2space = strchr(Variable, '_'))
209                {
210                  sprintf (under2space, " %s", under2space+1);
211                }
212             
213              fscanf (CONFIG_FILE, "%s", Buffer);
214              varInfo.variableName = Variable;
215              varInfo.variableValue = Buffer;
216              groupWidget->walkThrough(readFileText, &varInfo, 0);
217              sprintf (Variable, "");
218            }
219          sprintf (Variable, "%s", Buffer);
220        }
221      widget->walkThrough(widget->setOptions, 0);
222    }
223}
224/**
225   \brief Maps Confugurations to the Options.
226   \param widget which widget downwards
227   \param varInfo Information about the Variable to read
228*/
229void OrxonoxGuiExec::readFileText (Widget* widget, void* varInfo)
230{
231  VarInfo* info = (VarInfo*)varInfo;
232
233  if (widget->isOption >= 1 && widget->isOption <= 3)
234    {
235      if (!strcmp (static_cast<Option*>(widget)->title, info->variableName))
236        static_cast<Option*>(widget)->value = atoi(info->variableValue);
237    }
238  else if (widget->isOption == 5)
239    {
240       if (!strcmp (static_cast<Option*>(widget)->title, info->variableName))
241        static_cast<OptionLabel*>(widget)->setValue(info->variableValue);
242    }
243}
244
245/**
246   \brief Locates a Group.
247   \param widget The Widget from where to search from
248   \param groupName The GroupName for which to search.
249   \param depth The Depth of the search seen from the first widget we searched from.
250   \returns The Widget that holds the Group, or the NULL if the Group wasn't found.
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.