Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: minor

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