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