Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/gui/gui/orxonox_gui_exec.cc @ 4024

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

orxonox/trunk: gui: more addaptive structure

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