Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/updater: implemented some destructors

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