Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/gui/gtk_gui/gui_exec.cc @ 5968

Last change on this file since 5968 was 5968, checked in by patrick, 18 years ago

network: merged the trunk into the network with the command svn merge -r5824:HEAD ../trunk network, changes changed… bla bla..

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