Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/updater: gtk-assertion-error-FIX. (lazy)

File size: 10.7 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
30HashTable* orxonoxFlagHash;
31
32/**
33    \brief Creates the Exec-Frame
34    \param orxonoxGUI ExecFrame needs to know where to get the Options from
35*/
36OrxonoxGuiExec::OrxonoxGuiExec(void)
37{
38  configFile =(char*)malloc(512*sizeof(char));
39
40  this->execFrame = new Frame("Execute-Tags:");
41  this->execBox = new Box('v');
42  this->execFrame->setGroupName("misc");
43 
44  this->start = new Button("Start");
45#ifdef HAVE_GTK2
46  this->start->connectSignal("clicked", this, startOrxonox);
47#endif /* HAVE_GTK2 */
48  this->execBox->fill(start);
49  this->saveSettings = new CheckButton("Save Settings");
50  this->saveSettings->value = 1;
51  this->saveSettings->saveability();
52  this->execBox->fill(this->saveSettings);
53  this->verboseMode = new Menu("verbose mode", "no output", "error", "warning", "info", "lastItem");
54  this->verboseMode->setFlagName("verbose", "v", 0);
55  this->verboseMode->saveability();
56  this->execBox->fill(this->verboseMode);
57  this->alwaysShow = new CheckButton("Always Show this Menu");
58  this->alwaysShow->setFlagName("gui", "g", 0);
59  this->alwaysShow->saveability();
60  this->execBox->fill(this->alwaysShow);
61  this->quit = new Button("Quit");
62#ifdef HAVE_GTK2
63  this->quit->connectSignal("clicked", this, OrxonoxGuiExec::quitOrxonox);
64  //  Window::mainWindow->connectSignal("remove", this, OrxonoxGuiExec::quitOrxonox);
65  Window::mainWindow->connectSignal("destroy", this, OrxonoxGuiExec::quitOrxonox);
66#endif /* HAVE_GTK2 */
67  this->execBox->fill(this->quit);
68
69  this->execFrame->fill(this->execBox);
70}
71
72/**
73   \brief Return the Frame
74   \return Returns the Exec-frame
75*/
76Widget* OrxonoxGuiExec::getWidget(void)
77{
78  return execFrame;
79}
80
81/* FILE HANDLING */
82
83/**
84   \brief Sets the location of the configuration File.
85   \param filename the location of the configFile
86
87   \todo: memory allocation could be better.
88
89   The name will be parsed from ~/ to /home/[username] on unix and c:/Documents and Settings/username/Settings/ on Windows
90*/
91void OrxonoxGuiExec::setFilename(char* filename)
92{
93  char* buffer = (char*)malloc(2048*sizeof(char));
94  sprintf(buffer, "%s", filename);
95  if(!strncmp(buffer, "~/", 2))
96  {
97#ifdef __WIN32__
98    sprintf(configFile, "%s/%s", getenv("USERPROFILE"), buffer+2);
99#else
100    sprintf(configFile, "%s/%s", getenv("HOME"), buffer+2);
101#endif
102  }
103  else if(buffer)
104    strcpy(this->configFile, buffer);
105  free (buffer);
106}
107
108/**
109   \returns The name of the Configuration-File
110*/
111char* OrxonoxGuiExec::getConfigFile(void)
112{
113  return this->configFile;
114}
115
116/**
117   \brief checks if a option should be saved.
118   \return 1 if it should 0 if not/
119*/
120int OrxonoxGuiExec::shouldsave()
121{
122  return(static_cast<Option*>(this->saveSettings)->value);
123}
124
125/**
126    \brief Saves the configuration-file to the Disk.\n
127    \param widget from which Widget on should be saved.
128
129    this Function only opens and closes the file, in between OrxonoxGuiExec::writeFileText(Widget* widget) will execute the real writing process.
130*/
131void OrxonoxGuiExec::writeToFile(Widget* widget)
132{
133  this->CONFIG_FILE = fopen(configFile, "w");
134  if(this->CONFIG_FILE)
135    this->writeFileText(widget, 0);
136  fclose(this->CONFIG_FILE);
137}
138
139/**
140   \brief Actually writes into the configuration file to the disk.
141   \param widget from which Widget on should be saved.
142   \param depth initially "0", and grows higher, while new Groups are bundeled.
143*/
144void OrxonoxGuiExec::writeFileText(Widget* widget, int depth)
145{
146  int counter = 0;
147  while(counter < depth &&((widget->isOption>0
148                              &&(static_cast<Option*>(widget)->isSaveable()) )
149                             ||(widget->isOption<0
150                                 && static_cast<Packer*>(widget)->getGroupName())))
151    {
152      fprintf(this->CONFIG_FILE, "  ", depth);
153      counter++;
154    }
155 
156  // check if it is a Packer, and if it is, check if it has a name and if there is something in it.
157  if(widget->isOption <0)
158    {
159      if(static_cast<Packer*>(widget)->getGroupName())
160        {
161          fprintf(CONFIG_FILE, "[%s]\n", static_cast<Packer*>(widget)->getGroupName());
162          this->writeFileText(static_cast<Packer*>(widget)->down, depth+1);
163          fprintf(CONFIG_FILE, "\n");
164        }
165      else
166        {
167          this->writeFileText(static_cast<Packer*>(widget)->down, depth);
168        }
169    } 
170  //  if(widget->isOption == 0)
171  //    printf("%s\n",widget->title);
172  if(widget->isOption >= 1)
173    if (static_cast<Option*>(widget)->isSaveable())
174      {
175        char Buffer[256];
176        char* space2under;
177        strcpy(Buffer, static_cast<Option*>(widget)->title);
178        if(strchr(Buffer, '_'))
179          cout << "Warning Optionname" << Buffer << " is not Valid for Saving, because it includes an underscore" << endl;
180        while(space2under = strchr(Buffer, ' '))
181          {
182            space2under[0] = '_';
183          }
184        if(widget->isOption <=3)
185          fprintf(CONFIG_FILE, "%s = %d\n", Buffer, static_cast<Option*>(widget)->value);
186        else if(widget->isOption == 5)
187          fprintf(CONFIG_FILE, "%s = %s\n", Buffer, static_cast<OptionLabel*>(widget)->cValue);
188      }
189
190  if(widget->next != NULL)
191    this->writeFileText(widget->next, depth);
192}
193
194/**
195   \brief Reads in Configuration Data.
196   \param widget from which Widget on should be saved.
197*/
198void OrxonoxGuiExec::readFromFile(Widget* widget)
199{
200  this->CONFIG_FILE = fopen(configFile, "r");
201  VarInfo varInfo;
202  if(this->CONFIG_FILE)
203    {
204      Widget* groupWidget = widget;
205      char Buffer[256] = "";
206      char Variable[256]= "";
207      char* Value;
208      while(fscanf(this->CONFIG_FILE, "%s", Buffer) != EOF)
209        {
210          // group-search //
211          if(!strncmp(Buffer, "[", 1))
212            {
213              if((groupWidget = locateGroup(widget, Buffer, 1))==NULL)
214                {
215                  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";
216                  groupWidget = widget;
217                }
218            }
219          // option-setting //
220          if(!strcmp(Buffer, "="))
221            {
222              char* under2space;
223              while(under2space = strchr(Variable, '_'))
224                {
225                  sprintf(under2space, " %s", under2space+1);
226                }
227             
228              fscanf(this->CONFIG_FILE, "%s", Buffer);
229              varInfo.variableName = Variable;
230              varInfo.variableValue = Buffer;
231              groupWidget->walkThrough(this->readFileText, &varInfo, 0);
232              sprintf(Variable, "");
233            }
234          sprintf(Variable, "%s", Buffer);
235        }
236      widget->walkThrough(widget->setOptions, 0);
237    }
238}
239/**
240   \brief Maps Confugurations to the Options.
241   \param widget which widget downwards
242   \param varInfo Information about the Variable to read
243*/
244void OrxonoxGuiExec::readFileText(Widget* widget, void* varInfo)
245{
246  VarInfo* info =(VarInfo*)varInfo;
247
248  if(widget->isOption >= 1 && widget->isOption <= 3)
249    {
250      if(!strcmp(static_cast<Option*>(widget)->title, info->variableName))
251        static_cast<Option*>(widget)->value = atoi(info->variableValue);
252    }
253  else if(widget->isOption == 5)
254    {
255       if(!strcmp(static_cast<Option*>(widget)->title, info->variableName))
256        static_cast<OptionLabel*>(widget)->setValue(info->variableValue);
257    }
258}
259
260/**
261   \brief Locates a Group.
262   \param widget The Widget from where to search from
263   \param groupName The GroupName for which to search.
264   \param depth The Depth of the search seen from the first widget we searched from.
265   \returns The Widget that holds the Group, or the NULL if the Group wasn't found.
266
267   \todo do this in gui-gtk.
268*/
269Widget* OrxonoxGuiExec::locateGroup(Widget* widget, char* groupName, int depth)
270{
271  Widget* tmp;
272
273  // removes the trailing and ending [ ].
274  if(!strncmp(groupName, "[", 1))
275    {
276      groupName = groupName+1;
277      groupName[strlen(groupName)-1] = '\0';
278    }
279
280  if(widget->isOption < 0)
281    {
282      if(static_cast<Packer*>(widget)->getGroupName() && !strcmp(groupName, static_cast<Packer*>(widget)->getGroupName()))
283        {
284          return widget;
285        }
286      else
287        {
288          if((tmp = locateGroup(static_cast<Packer*>(widget)->down, groupName, depth+1)) != NULL)
289            return tmp;
290        }
291    } 
292 
293  if(widget->next != NULL && depth != 0)
294    {
295      if((tmp = locateGroup(widget->next, groupName, depth)) != NULL)
296        return tmp;
297    }
298  return NULL;
299}
300
301#ifdef HAVE_GTK2
302/**
303   \brief Starts ORXONOX.(not really implemented yet, but the function is there.\n
304   \param widget the widget that executed the start command
305   \param data additional data
306
307   This is a Signal and can be executed through Widget::signal_connect
308*/
309int OrxonoxGuiExec::startOrxonox(GtkWidget* widget, void* data)
310{
311  OrxonoxGuiExec* exec =(OrxonoxGuiExec*)data;
312  if(exec->shouldsave())
313    exec->writeToFile(Window::mainWindow);
314  cout << "Starting Orxonox" <<endl;
315  gtk_main_quit();
316  system("cd ..;./orxonox"); //!< \todo fix this. should execute orxonox for real(coded not over the shell)
317}
318
319/**
320   \brief Starts ORXONOX.(not really implemented yet, but the function is there.\n
321   \param widget the widget that executed the start command
322   \param data additional data
323
324   This is a Signal and can be executed through Widget::signal_connect
325*/
326int OrxonoxGuiExec::quitOrxonox(GtkWidget* widget, void* data)
327{
328  OrxonoxGuiExec* exec =(OrxonoxGuiExec*)data;
329  PRINT(3)( "Quitting Orxonox %p\n", exec);
330  if(exec->shouldsave())
331    exec->writeToFile(Window::mainWindow);
332  gtk_main_quit();
333}
334#else /* HAVE_GTK2 */
335/**
336   \brief Starts ORXONOX.(not really implemented yet, but the function is there.\n
337   \param widget the widget that executed the start command
338   \param data additional data
339*/
340int OrxonoxGuiExec::startOrxonox(void* widget, void* data)
341{
342  OrxonoxGuiExec* exec =(OrxonoxGuiExec*)data;
343  PRINT(3)("Starting Orxonox\n");
344  if(exec->shouldsave())
345    exec->writeToFile(Window::mainWindow);
346  system("cd ..;./orxonox"); //!< \todo fix this. should execute orxonox for real(coded not over the shell)
347}
348/**
349   \brief Quits ORXONOX.
350   \param widget the widget that executed the Quit command
351   \param data additional data
352
353   This is a Signal and can be executed through Widget::signal_connect
354*/
355int OrxonoxGuiExec::quitOrxonox(void* widget, void* data)
356{
357  OrxonoxGuiExec* exec =(OrxonoxGuiExec*)data;
358  PRINT(3)("Quiting Orxonox");
359  if(exec->shouldsave())
360    exec->writeToFile(Window::mainWindow);
361}
362
363#endif /* HAVE_GTK2 */
Note: See TracBrowser for help on using the repository browser.