[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 | |
---|
[7193] | 28 | #include "util/loading/resource_manager.h" |
---|
[5944] | 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] | 40 | HashTable* orxonoxFlagHash; |
---|
| 41 | |
---|
[4830] | 42 | /** |
---|
[4836] | 43 | * Creates the Exec-Frame |
---|
[2588] | 44 | */ |
---|
[4746] | 45 | GuiExec::GuiExec() |
---|
[2018] | 46 | { |
---|
[4051] | 47 | Frame* execFrame; //!< The Frame that holds the ExecutionOptions. |
---|
[2018] | 48 | |
---|
[7203] | 49 | this->confFile = ""; |
---|
| 50 | this->confDir = ""; |
---|
[4051] | 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] | 137 | GuiExec::~GuiExec() |
---|
[3423] | 138 | { |
---|
| 139 | } |
---|
| 140 | |
---|
[2018] | 141 | /* FILE HANDLING */ |
---|
| 142 | |
---|
[2588] | 143 | /** |
---|
[4836] | 144 | * sets the Directory of the configuration files |
---|
| 145 | * @param confDir the Directory for the configuration files |
---|
[3625] | 146 | */ |
---|
[4056] | 147 | void GuiExec::setConfDir(const char* confDir) |
---|
[3625] | 148 | { |
---|
[4051] | 149 | this->confDir = ResourceManager::homeDirCheck(confDir); |
---|
| 150 | |
---|
[4133] | 151 | PRINTF(5)("Config Directory is: %s.\n", this->confDir); |
---|
[4830] | 152 | //! @todo F** Windows-support |
---|
[4052] | 153 | #ifndef __WIN32__ |
---|
[7203] | 154 | mkdir(this->confDir.c_str(), 0755); |
---|
[4052] | 155 | #else /* __WiN32__ */ |
---|
[7203] | 156 | mkdir(this->confDir.c_str()); |
---|
[4052] | 157 | #endif /* __WIN32__ */ |
---|
[3625] | 158 | } |
---|
| 159 | |
---|
| 160 | /** |
---|
[4836] | 161 | * Sets the location of the configuration File. |
---|
| 162 | * @param fileName the location of the configFile |
---|
[3423] | 163 | |
---|
| 164 | The name will be parsed from ~/ to /home/[username] on unix and c:/Documents and Settings/username/Settings/ on Windows |
---|
[2588] | 165 | */ |
---|
[4056] | 166 | void GuiExec::setConfFile(const char* fileName) |
---|
[2018] | 167 | { |
---|
[7203] | 168 | if (!this->confDir.empty()) |
---|
[3625] | 169 | this->setConfDir("~/"); |
---|
[7203] | 170 | this->confFile = this->confDir + "/" + fileName; |
---|
[4133] | 171 | PRINTF(5)("ConfigurationFile is %s.\n", this->confFile); |
---|
[2018] | 172 | } |
---|
| 173 | |
---|
[2588] | 174 | /** |
---|
[4836] | 175 | * @returns The name of the Configuration-File |
---|
[3423] | 176 | */ |
---|
[4746] | 177 | const char* GuiExec::getConfigFile() const |
---|
[3423] | 178 | { |
---|
[7203] | 179 | return this->confFile.c_str(); |
---|
[3423] | 180 | } |
---|
| 181 | |
---|
| 182 | /** |
---|
[4836] | 183 | * checks if a option should be saved. |
---|
| 184 | * @return 1 if it should 0 if not/ |
---|
[2588] | 185 | */ |
---|
[4056] | 186 | int GuiExec::shouldsave() |
---|
[2018] | 187 | { |
---|
[3423] | 188 | return(static_cast<Option*>(this->saveSettings)->value); |
---|
[2018] | 189 | } |
---|
| 190 | |
---|
[4830] | 191 | /** |
---|
[4836] | 192 | * Saves the configuration-file to the Disk.\n |
---|
| 193 | * @param widget from which Widget on should be saved. |
---|
[3187] | 194 | |
---|
[4056] | 195 | this Function only opens and closes the file, in between GuiExec::writeFileText(Widget* widget) will execute the real writing process. |
---|
[2588] | 196 | */ |
---|
[4056] | 197 | void GuiExec::writeToFile(Widget* widget) |
---|
[2018] | 198 | { |
---|
[5021] | 199 | IniParser iniParser; |
---|
| 200 | this->writeFileText(widget, &iniParser, 0); |
---|
[7203] | 201 | std::string fileName = ResourceManager::homeDirCheck(confFile); |
---|
[5021] | 202 | iniParser.writeFile(fileName); |
---|
[2018] | 203 | } |
---|
| 204 | |
---|
[2588] | 205 | /** |
---|
[4836] | 206 | * Actually writes into the configuration file to the disk. |
---|
| 207 | * @param widget from which Widget on should be saved. |
---|
[5021] | 208 | * @param parser the IniParser to write to. |
---|
[4836] | 209 | * @param depth initially "0", and grows higher, while new Groups are bundeled. |
---|
[2588] | 210 | */ |
---|
[5021] | 211 | void GuiExec::writeFileText(Widget* widget, IniParser* parser, int depth) |
---|
[2018] | 212 | { |
---|
[5021] | 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 | // } |
---|
[4830] | 222 | |
---|
[2615] | 223 | // check if it is a Packer, and if it is, check if it has a name and if there is something in it. |
---|
[4071] | 224 | if(widget->optionType < GUI_NOTHING) |
---|
[2615] | 225 | { |
---|
[3423] | 226 | if(static_cast<Packer*>(widget)->getGroupName()) |
---|
[4830] | 227 | { |
---|
[5021] | 228 | parser->addSection(static_cast<Packer*>(widget)->getGroupName()); |
---|
| 229 | this->writeFileText(static_cast<Packer*>(widget)->down, parser, depth+1); |
---|
[4830] | 230 | } |
---|
[2615] | 231 | else |
---|
[4830] | 232 | { |
---|
[5021] | 233 | this->writeFileText(static_cast<Packer*>(widget)->down, parser, depth); |
---|
[4830] | 234 | } |
---|
| 235 | } |
---|
[4071] | 236 | |
---|
| 237 | if(widget->optionType > GUI_NOTHING) |
---|
[3423] | 238 | if (static_cast<Option*>(widget)->isSaveable()) |
---|
[5241] | 239 | { |
---|
| 240 | char* saveName = static_cast<Option*>(widget)->save(); |
---|
| 241 | parser->addVar(static_cast<Option*>(widget)->title, saveName); |
---|
| 242 | delete[] saveName; |
---|
| 243 | } |
---|
[2615] | 244 | |
---|
[3423] | 245 | if(widget->next != NULL) |
---|
[5021] | 246 | this->writeFileText(widget->next, parser, depth); |
---|
[2018] | 247 | } |
---|
| 248 | |
---|
[2588] | 249 | /** |
---|
[5936] | 250 | * @brief Reads in Configuration Data. |
---|
[4836] | 251 | * @param widget from which Widget on should be saved. |
---|
[2588] | 252 | */ |
---|
[4056] | 253 | void GuiExec::readFromFile(Widget* widget) |
---|
[2018] | 254 | { |
---|
[7203] | 255 | std::string fileName = ResourceManager::homeDirCheck(confFile); |
---|
[5015] | 256 | IniParser iniParser(fileName); |
---|
[5014] | 257 | if (!iniParser.isOpen()) |
---|
| 258 | return; |
---|
[4830] | 259 | |
---|
[5936] | 260 | iniParser.firstSection(); |
---|
[5015] | 261 | Widget* groupWidget = widget; |
---|
[7203] | 262 | std::string groupName; |
---|
| 263 | std::string widgetName; |
---|
[5015] | 264 | VarInfo varInfo; |
---|
[7203] | 265 | while ((groupName = iniParser.getCurrentSection()) != "") |
---|
[5015] | 266 | { |
---|
[7203] | 267 | PRINTF(4)("GROUP:::%s\n", groupName.c_str()); |
---|
| 268 | if((groupWidget = locateGroup(widget, groupName.c_str(), 1))==NULL) |
---|
[5938] | 269 | { |
---|
[7203] | 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()); |
---|
[5938] | 271 | groupWidget = widget; |
---|
| 272 | continue; |
---|
| 273 | } |
---|
[5015] | 274 | else |
---|
[5938] | 275 | PRINT(4)("Group %s located.\n", static_cast<Packer*>(groupWidget)->groupName); |
---|
[5015] | 276 | |
---|
[7203] | 277 | std::string entryName; |
---|
[5936] | 278 | iniParser.firstVar(); |
---|
[7203] | 279 | while((entryName = iniParser.getCurrentName()) != "") |
---|
[5015] | 280 | { |
---|
[7203] | 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(); |
---|
[5015] | 284 | groupWidget->walkThrough(this->readFileText, &varInfo, 0); |
---|
| 285 | iniParser.nextVar(); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | iniParser.nextSection(); |
---|
| 289 | } |
---|
| 290 | widget->walkThrough(widget->setOptions, 0); |
---|
[2018] | 291 | } |
---|
[3423] | 292 | |
---|
[2588] | 293 | /** |
---|
[4836] | 294 | * Maps Confugurations to the Options. |
---|
| 295 | * @param widget which widget downwards |
---|
| 296 | * @param varInfo Information about the Variable to read |
---|
[2588] | 297 | */ |
---|
[4056] | 298 | void GuiExec::readFileText(Widget* widget, void* varInfo) |
---|
[2018] | 299 | { |
---|
[3423] | 300 | VarInfo* info =(VarInfo*)varInfo; |
---|
[5938] | 301 | if (info == NULL || info->variableName == NULL) |
---|
| 302 | return; |
---|
| 303 | |
---|
[3423] | 304 | if(widget->title && !strcmp(widget->title, info->variableName)) |
---|
[3162] | 305 | { |
---|
[4026] | 306 | PRINT(5)("Located Option %s.\n", widget->title); |
---|
[4071] | 307 | if(widget->optionType > GUI_NOTHING) |
---|
[5938] | 308 | if (info->variableValue != NULL) |
---|
| 309 | static_cast<Option*>(widget)->load(info->variableValue); |
---|
[3162] | 310 | } |
---|
[2053] | 311 | } |
---|
[2018] | 312 | |
---|
[2588] | 313 | /** |
---|
[4836] | 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. |
---|
[3423] | 319 | |
---|
[4836] | 320 | @todo do this in gui-gtk. |
---|
[2634] | 321 | */ |
---|
[5015] | 322 | Widget* GuiExec::locateGroup(Widget* widget, const char* groupName, int depth) |
---|
[2634] | 323 | { |
---|
| 324 | Widget* tmp; |
---|
[5938] | 325 | if (widget == NULL || groupName == NULL) |
---|
| 326 | return NULL; |
---|
[2634] | 327 | |
---|
[4071] | 328 | if(widget->optionType < GUI_NOTHING) |
---|
[2634] | 329 | { |
---|
[5938] | 330 | if(static_cast<Packer*>(widget)->getGroupName() != NULL && |
---|
[4830] | 331 | !strcmp(groupName, static_cast<Packer*>(widget)->getGroupName())) |
---|
[5938] | 332 | return widget; |
---|
[2634] | 333 | else |
---|
[4830] | 334 | { |
---|
[5938] | 335 | if((tmp = locateGroup(static_cast<Packer*>(widget)->down, |
---|
| 336 | groupName, depth+1)) != NULL) |
---|
[4830] | 337 | return tmp; |
---|
| 338 | } |
---|
| 339 | } |
---|
| 340 | |
---|
[3423] | 341 | if(widget->next != NULL && depth != 0) |
---|
[2634] | 342 | { |
---|
[3423] | 343 | if((tmp = locateGroup(widget->next, groupName, depth)) != NULL) |
---|
[4830] | 344 | return tmp; |
---|
[2634] | 345 | } |
---|
| 346 | return NULL; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | /** |
---|
[4836] | 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 |
---|
[3423] | 353 | |
---|
| 354 | This is a Signal and can be executed through Widget::signal_connect |
---|
[2588] | 355 | */ |
---|
[4039] | 356 | #ifdef HAVE_GTK2 |
---|
[4056] | 357 | int GuiExec::startOrxonox(GtkWidget* widget, void* data) |
---|
[4039] | 358 | #else /* HAVE_GTK2 */ |
---|
[4056] | 359 | int GuiExec::startOrxonox(void* widget, void* data) |
---|
[4039] | 360 | #endif /* HAVE_GTK2 */ |
---|
[2053] | 361 | { |
---|
[4041] | 362 | Window::mainWindow->hide(); |
---|
| 363 | |
---|
| 364 | #ifdef HAVE_GTK2 |
---|
| 365 | gtk_widget_destroy(Window::mainWindow->widget); |
---|
| 366 | #else |
---|
[4042] | 367 | quitGui(widget, data); |
---|
[4041] | 368 | #endif /* HAVE_GTK2 */ |
---|
| 369 | |
---|
[4034] | 370 | PRINT(3)("Starting Orxonox\n"); |
---|
[4056] | 371 | Gui::startOrxonox = true; |
---|
[2018] | 372 | } |
---|
[3423] | 373 | |
---|
| 374 | /** |
---|
[4836] | 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 |
---|
[3423] | 378 | |
---|
| 379 | This is a Signal and can be executed through Widget::signal_connect |
---|
| 380 | */ |
---|
[4039] | 381 | #ifdef HAVE_GTK2 |
---|
[4056] | 382 | int GuiExec::quitGui(GtkWidget* widget, void* data) |
---|
[4039] | 383 | #else /* HAVE_GTK2 */ |
---|
[4056] | 384 | int GuiExec::quitGui(void* widget, void* data) |
---|
[4039] | 385 | #endif /* HAVE_GTK2 */ |
---|
[3423] | 386 | { |
---|
[4062] | 387 | GuiExec* exec = (GuiExec*)data; |
---|
[3423] | 388 | if(exec->shouldsave()) |
---|
| 389 | exec->writeToFile(Window::mainWindow); |
---|
[4041] | 390 | #ifdef HAVE_GTK2 |
---|
[3423] | 391 | gtk_main_quit(); |
---|
[4042] | 392 | while(gtk_events_pending()) gtk_main_iteration(); |
---|
[4041] | 393 | #endif /* HAVE_GTK2 */ |
---|
[3423] | 394 | } |
---|