Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9110 was 8145, checked in by bensch, 18 years ago

trunk: merged the gui back
merged with command:
svn merge -r8114:HEAD https://svn.orxonox.net/orxonox/branches/gui .
→ no conflicts

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