Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/gui/gui/gui_exec.cc @ 4133

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

orxonox/trunk: cleanup in gui: less useless output

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