Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the branches/updater back to the trunk.
merged with command
svn merge -r 3423:HEAD branches/updater/src/gui trunk/src/lib/graphics/gui/gui.

I do not wish to make such jokes again, because it is even worse than copy-pasting. All Files had to be eddited manually, and many diffs where a little bit strange (artifacts).

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