[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 | |
---|
[2018] | 26 | #include "orxonox_gui_exec.h" |
---|
[3625] | 27 | |
---|
[2018] | 28 | #include <iostream> |
---|
| 29 | #include <string> |
---|
| 30 | |
---|
[3625] | 31 | #include <sys/stat.h> |
---|
| 32 | #include <sys/types.h> |
---|
| 33 | |
---|
| 34 | |
---|
[3423] | 35 | HashTable* orxonoxFlagHash; |
---|
| 36 | |
---|
[2588] | 37 | /** |
---|
| 38 | \brief Creates the Exec-Frame |
---|
| 39 | */ |
---|
[3423] | 40 | OrxonoxGuiExec::OrxonoxGuiExec(void) |
---|
[2018] | 41 | { |
---|
[3423] | 42 | configFile =(char*)malloc(512*sizeof(char)); |
---|
[2018] | 43 | |
---|
[3423] | 44 | this->execFrame = new Frame("Execute-Tags:"); |
---|
| 45 | this->execBox = new Box('v'); |
---|
| 46 | this->execFrame->setGroupName("misc"); |
---|
[2018] | 47 | |
---|
[3423] | 48 | this->start = new Button("Start"); |
---|
[3165] | 49 | #ifdef HAVE_GTK2 |
---|
[3423] | 50 | this->start->connectSignal("clicked", this, startOrxonox); |
---|
[3165] | 51 | #endif /* HAVE_GTK2 */ |
---|
[3423] | 52 | this->execBox->fill(start); |
---|
| 53 | this->saveSettings = new CheckButton("Save Settings"); |
---|
| 54 | this->saveSettings->value = 1; |
---|
| 55 | this->saveSettings->saveability(); |
---|
| 56 | this->execBox->fill(this->saveSettings); |
---|
[3625] | 57 | this->verboseMode = new Menu("verbose mode", "nothing", "error", "warning", "info", "lastItem"); |
---|
[3423] | 58 | this->verboseMode->setFlagName("verbose", "v", 0); |
---|
| 59 | this->verboseMode->saveability(); |
---|
| 60 | this->execBox->fill(this->verboseMode); |
---|
| 61 | this->alwaysShow = new CheckButton("Always Show this Menu"); |
---|
| 62 | this->alwaysShow->setFlagName("gui", "g", 0); |
---|
| 63 | this->alwaysShow->saveability(); |
---|
| 64 | this->execBox->fill(this->alwaysShow); |
---|
| 65 | this->quit = new Button("Quit"); |
---|
[3165] | 66 | #ifdef HAVE_GTK2 |
---|
[3423] | 67 | this->quit->connectSignal("clicked", this, OrxonoxGuiExec::quitOrxonox); |
---|
| 68 | // Window::mainWindow->connectSignal("remove", this, OrxonoxGuiExec::quitOrxonox); |
---|
| 69 | Window::mainWindow->connectSignal("destroy", this, OrxonoxGuiExec::quitOrxonox); |
---|
[3165] | 70 | #endif /* HAVE_GTK2 */ |
---|
[3423] | 71 | this->execBox->fill(this->quit); |
---|
[2018] | 72 | |
---|
[3423] | 73 | this->execFrame->fill(this->execBox); |
---|
[2018] | 74 | } |
---|
| 75 | |
---|
[2588] | 76 | /** |
---|
[3423] | 77 | \brief Destructs the Execution-stuff |
---|
| 78 | */ |
---|
| 79 | OrxonoxGuiExec::~OrxonoxGuiExec(void) |
---|
| 80 | { |
---|
| 81 | if(this->configFile) |
---|
| 82 | delete []this->configFile; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /** |
---|
[2588] | 86 | \brief Return the Frame |
---|
| 87 | \return Returns the Exec-frame |
---|
| 88 | */ |
---|
[3423] | 89 | Widget* OrxonoxGuiExec::getWidget(void) |
---|
[2018] | 90 | { |
---|
| 91 | return execFrame; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /* FILE HANDLING */ |
---|
| 95 | |
---|
[2588] | 96 | /** |
---|
[3625] | 97 | \brief sets the confDir and File-name out of an input-string |
---|
| 98 | */ |
---|
| 99 | void OrxonoxGuiExec::setConfFile(char* confFile) |
---|
| 100 | { |
---|
| 101 | char splitter = |
---|
| 102 | #ifdef __WIN32__ |
---|
| 103 | '\\'; |
---|
| 104 | #else |
---|
| 105 | '/'; |
---|
| 106 | #endif |
---|
| 107 | char* tmpConfFile = new char[strlen(confFile)+1]; |
---|
| 108 | strcpy(tmpConfFile, confFile); |
---|
| 109 | char* tmp = strrchr(tmpConfFile, splitter); |
---|
| 110 | if (tmp) |
---|
| 111 | { |
---|
| 112 | tmp[0] = '\0'; |
---|
| 113 | this->setConfDir(tmpConfFile); |
---|
| 114 | this->setFileName(tmp+1); |
---|
| 115 | } |
---|
| 116 | else |
---|
| 117 | { |
---|
| 118 | this->setConfDir("~/"); |
---|
| 119 | this->setFileName(tmpConfFile); |
---|
| 120 | } |
---|
| 121 | delete []tmp; |
---|
| 122 | delete []tmpConfFile; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | \brief sets the Directory of the configuration files |
---|
| 127 | \param confDir the Directory for the configuration files |
---|
| 128 | */ |
---|
| 129 | void OrxonoxGuiExec::setConfDir(char* confDir) |
---|
| 130 | { |
---|
| 131 | if (!strncmp(confDir, "~/", 2)) |
---|
| 132 | { |
---|
| 133 | char tmp[500]; |
---|
| 134 | #ifdef __WIN32__ |
---|
| 135 | strcpy(tmp, getenv("USERPROFILE")); |
---|
| 136 | #else |
---|
| 137 | strcpy(tmp, getenv("HOME")); |
---|
| 138 | #endif |
---|
| 139 | this->confDir = new char[strlen(tmp)+strlen(confDir)]; |
---|
| 140 | sprintf(this->confDir, "%s%s", tmp, confDir+1); |
---|
| 141 | } |
---|
| 142 | else |
---|
| 143 | { |
---|
| 144 | this->confDir = new char[strlen(confDir)+1]; |
---|
| 145 | strcpy(this->confDir, confDir); |
---|
| 146 | } |
---|
| 147 | PRINTF(3)("Config Directory is: %s.\n", this->confDir); |
---|
| 148 | mkdir(this->confDir, 0755); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /** |
---|
[3423] | 152 | \brief Sets the location of the configuration File. |
---|
[3625] | 153 | \param fileName the location of the configFile |
---|
[3423] | 154 | |
---|
| 155 | \todo: memory allocation could be better. |
---|
| 156 | |
---|
| 157 | The name will be parsed from ~/ to /home/[username] on unix and c:/Documents and Settings/username/Settings/ on Windows |
---|
[2588] | 158 | */ |
---|
[3625] | 159 | void OrxonoxGuiExec::setFileName(char* fileName) |
---|
[2018] | 160 | { |
---|
[3625] | 161 | if (!this->confDir) |
---|
| 162 | this->setConfDir("~/"); |
---|
| 163 | this->configFile = new char[strlen(this->confDir)+strlen(fileName)+2]; |
---|
| 164 | sprintf(this->configFile, "%s/%s", this->confDir, fileName); |
---|
| 165 | PRINTF(3)("ConfigurationFile is %s.\n", this->configFile); |
---|
[2018] | 166 | } |
---|
| 167 | |
---|
[2588] | 168 | /** |
---|
[3423] | 169 | \returns The name of the Configuration-File |
---|
| 170 | */ |
---|
[3625] | 171 | char* OrxonoxGuiExec::getConfigFile(void) const |
---|
[3423] | 172 | { |
---|
| 173 | return this->configFile; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | /** |
---|
[2588] | 177 | \brief checks if a option should be saved. |
---|
| 178 | \return 1 if it should 0 if not/ |
---|
| 179 | */ |
---|
[3423] | 180 | int OrxonoxGuiExec::shouldsave() |
---|
[2018] | 181 | { |
---|
[3423] | 182 | return(static_cast<Option*>(this->saveSettings)->value); |
---|
[2018] | 183 | } |
---|
| 184 | |
---|
[2588] | 185 | /** |
---|
| 186 | \brief Saves the configuration-file to the Disk.\n |
---|
[3187] | 187 | \param widget from which Widget on should be saved. |
---|
| 188 | |
---|
[3423] | 189 | this Function only opens and closes the file, in between OrxonoxGuiExec::writeFileText(Widget* widget) will execute the real writing process. |
---|
[2588] | 190 | */ |
---|
[3423] | 191 | void OrxonoxGuiExec::writeToFile(Widget* widget) |
---|
[2018] | 192 | { |
---|
[3423] | 193 | this->CONFIG_FILE = fopen(configFile, "w"); |
---|
| 194 | if(this->CONFIG_FILE) |
---|
| 195 | this->writeFileText(widget, 0); |
---|
| 196 | fclose(this->CONFIG_FILE); |
---|
[2018] | 197 | } |
---|
| 198 | |
---|
[2588] | 199 | /** |
---|
| 200 | \brief Actually writes into the configuration file to the disk. |
---|
| 201 | \param widget from which Widget on should be saved. |
---|
[2615] | 202 | \param depth initially "0", and grows higher, while new Groups are bundeled. |
---|
[2588] | 203 | */ |
---|
[3423] | 204 | void OrxonoxGuiExec::writeFileText(Widget* widget, int depth) |
---|
[2018] | 205 | { |
---|
[2615] | 206 | int counter = 0; |
---|
[3423] | 207 | while(counter < depth &&((widget->isOption>0 |
---|
| 208 | &&(static_cast<Option*>(widget)->isSaveable())) |
---|
| 209 | ||(widget->isOption<0 |
---|
| 210 | && static_cast<Packer*>(widget)->getGroupName()))) |
---|
[2615] | 211 | { |
---|
[3423] | 212 | fprintf(this->CONFIG_FILE, " ", depth); |
---|
[2615] | 213 | counter++; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | // check if it is a Packer, and if it is, check if it has a name and if there is something in it. |
---|
[3423] | 217 | if(widget->isOption <0) |
---|
[2615] | 218 | { |
---|
[3423] | 219 | if(static_cast<Packer*>(widget)->getGroupName()) |
---|
[2615] | 220 | { |
---|
[3423] | 221 | fprintf(CONFIG_FILE, "[%s]\n", static_cast<Packer*>(widget)->getGroupName()); |
---|
| 222 | this->writeFileText(static_cast<Packer*>(widget)->down, depth+1); |
---|
[2625] | 223 | fprintf(CONFIG_FILE, "\n"); |
---|
[2615] | 224 | } |
---|
| 225 | else |
---|
| 226 | { |
---|
[3423] | 227 | this->writeFileText(static_cast<Packer*>(widget)->down, depth); |
---|
[2615] | 228 | } |
---|
| 229 | } |
---|
[3423] | 230 | // if(widget->isOption == 0) |
---|
| 231 | // printf("%s\n",widget->title); |
---|
| 232 | if(widget->isOption >= 1) |
---|
| 233 | if (static_cast<Option*>(widget)->isSaveable()) |
---|
[2018] | 234 | { |
---|
| 235 | char Buffer[256]; |
---|
| 236 | char* space2under; |
---|
[3423] | 237 | strcpy(Buffer, static_cast<Option*>(widget)->title); |
---|
| 238 | if(strchr(Buffer, '_')) |
---|
[2614] | 239 | cout << "Warning Optionname" << Buffer << " is not Valid for Saving, because it includes an underscore" << endl; |
---|
[3423] | 240 | while(space2under = strchr(Buffer, ' ')) |
---|
[2018] | 241 | { |
---|
[3161] | 242 | space2under[0] = '_'; |
---|
[2018] | 243 | } |
---|
[3625] | 244 | fprintf(CONFIG_FILE, "%s = %s\n", Buffer, static_cast<Option*>(widget)->save()); |
---|
[2018] | 245 | } |
---|
[2615] | 246 | |
---|
[3423] | 247 | if(widget->next != NULL) |
---|
| 248 | this->writeFileText(widget->next, depth); |
---|
[2018] | 249 | } |
---|
| 250 | |
---|
[2588] | 251 | /** |
---|
| 252 | \brief Reads in Configuration Data. |
---|
| 253 | \param widget from which Widget on should be saved. |
---|
| 254 | */ |
---|
[3423] | 255 | void OrxonoxGuiExec::readFromFile(Widget* widget) |
---|
[2018] | 256 | { |
---|
[3423] | 257 | this->CONFIG_FILE = fopen(configFile, "r"); |
---|
| 258 | VarInfo varInfo; |
---|
| 259 | if(this->CONFIG_FILE) |
---|
[2018] | 260 | { |
---|
[2634] | 261 | Widget* groupWidget = widget; |
---|
[2018] | 262 | char Buffer[256] = ""; |
---|
| 263 | char Variable[256]= ""; |
---|
[3162] | 264 | char* Value; |
---|
[3423] | 265 | while(fscanf(this->CONFIG_FILE, "%s", Buffer) != EOF) |
---|
[2018] | 266 | { |
---|
[3161] | 267 | // group-search // |
---|
[3423] | 268 | if(!strncmp(Buffer, "[", 1)) |
---|
[2634] | 269 | { |
---|
[3423] | 270 | if((groupWidget = locateGroup(widget, Buffer, 1))==NULL) |
---|
[2634] | 271 | { |
---|
[2635] | 272 | cout << "!!There is no group called " << Buffer << " in this GUI.\n First best Widget will get the Infos assigned.\n Config-File will be updated in next Save\n"; |
---|
[2634] | 273 | groupWidget = widget; |
---|
| 274 | } |
---|
[3423] | 275 | else |
---|
| 276 | PRINT(3)("Group %s located.\n", static_cast<Packer*>(groupWidget)->groupName); |
---|
[2634] | 277 | } |
---|
[3161] | 278 | // option-setting // |
---|
[3423] | 279 | if(!strcmp(Buffer, "=")) |
---|
[2018] | 280 | { |
---|
| 281 | char* under2space; |
---|
[3423] | 282 | while(under2space = strchr(Variable, '_')) |
---|
[2018] | 283 | { |
---|
[3423] | 284 | sprintf(under2space, " %s", under2space+1); |
---|
[2018] | 285 | } |
---|
| 286 | |
---|
[3423] | 287 | fscanf(this->CONFIG_FILE, "%s", Buffer); |
---|
| 288 | varInfo.variableName = Variable; |
---|
| 289 | varInfo.variableValue = Buffer; |
---|
| 290 | groupWidget->walkThrough(this->readFileText, &varInfo, 0); |
---|
| 291 | sprintf(Variable, ""); |
---|
[2018] | 292 | } |
---|
[3423] | 293 | sprintf(Variable, "%s", Buffer); |
---|
[2018] | 294 | } |
---|
[3423] | 295 | widget->walkThrough(widget->setOptions, 0); |
---|
[2018] | 296 | } |
---|
| 297 | } |
---|
[3423] | 298 | |
---|
[2588] | 299 | /** |
---|
| 300 | \brief Maps Confugurations to the Options. |
---|
| 301 | \param widget which widget downwards |
---|
[3423] | 302 | \param varInfo Information about the Variable to read |
---|
[2588] | 303 | */ |
---|
[3423] | 304 | void OrxonoxGuiExec::readFileText(Widget* widget, void* varInfo) |
---|
[2018] | 305 | { |
---|
[3423] | 306 | VarInfo* info =(VarInfo*)varInfo; |
---|
| 307 | if(widget->title && !strcmp(widget->title, info->variableName)) |
---|
[3162] | 308 | { |
---|
[3423] | 309 | PRINT(3)("Located Option %s.\n", widget->title); |
---|
[3625] | 310 | if(widget->isOption >= 1) |
---|
| 311 | static_cast<Option*>(widget)->load(info->variableValue); |
---|
[3162] | 312 | } |
---|
[2053] | 313 | } |
---|
[2018] | 314 | |
---|
[2588] | 315 | /** |
---|
[3187] | 316 | \brief Locates a Group. |
---|
| 317 | \param widget The Widget from where to search from |
---|
| 318 | \param groupName The GroupName for which to search. |
---|
| 319 | \param depth The Depth of the search seen from the first widget we searched from. |
---|
| 320 | \returns The Widget that holds the Group, or the NULL if the Group wasn't found. |
---|
[3423] | 321 | |
---|
| 322 | \todo do this in gui-gtk. |
---|
[2634] | 323 | */ |
---|
| 324 | Widget* OrxonoxGuiExec::locateGroup(Widget* widget, char* groupName, int depth) |
---|
| 325 | { |
---|
| 326 | Widget* tmp; |
---|
| 327 | |
---|
| 328 | // removes the trailing and ending [ ]. |
---|
[3423] | 329 | if(!strncmp(groupName, "[", 1)) |
---|
[2634] | 330 | { |
---|
| 331 | groupName = groupName+1; |
---|
| 332 | groupName[strlen(groupName)-1] = '\0'; |
---|
| 333 | } |
---|
| 334 | |
---|
[3423] | 335 | if(widget->isOption < 0) |
---|
[2634] | 336 | { |
---|
[3423] | 337 | if(static_cast<Packer*>(widget)->getGroupName() && !strcmp(groupName, static_cast<Packer*>(widget)->getGroupName())) |
---|
[2634] | 338 | { |
---|
| 339 | return widget; |
---|
| 340 | } |
---|
| 341 | else |
---|
| 342 | { |
---|
[3423] | 343 | if((tmp = locateGroup(static_cast<Packer*>(widget)->down, groupName, depth+1)) != NULL) |
---|
[2634] | 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) |
---|
[2634] | 351 | return tmp; |
---|
| 352 | } |
---|
| 353 | return NULL; |
---|
| 354 | } |
---|
| 355 | |
---|
[3165] | 356 | #ifdef HAVE_GTK2 |
---|
[2634] | 357 | /** |
---|
[3423] | 358 | \brief Starts ORXONOX.(not really implemented yet, but the function is there.\n |
---|
[2588] | 359 | \param widget the widget that executed the start command |
---|
| 360 | \param data additional data |
---|
[3423] | 361 | |
---|
| 362 | This is a Signal and can be executed through Widget::signal_connect |
---|
[2588] | 363 | */ |
---|
[3423] | 364 | int OrxonoxGuiExec::startOrxonox(GtkWidget* widget, void* data) |
---|
[2053] | 365 | { |
---|
[3423] | 366 | OrxonoxGuiExec* exec =(OrxonoxGuiExec*)data; |
---|
| 367 | if(exec->shouldsave()) |
---|
| 368 | exec->writeToFile(Window::mainWindow); |
---|
[2053] | 369 | cout << "Starting Orxonox" <<endl; |
---|
[3423] | 370 | gtk_main_quit(); |
---|
| 371 | system("cd ..;./orxonox"); //!< \todo fix this. should execute orxonox for real(coded not over the shell) |
---|
[2018] | 372 | } |
---|
[3423] | 373 | |
---|
| 374 | /** |
---|
| 375 | \brief 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 | int OrxonoxGuiExec::quitOrxonox(GtkWidget* widget, void* data) |
---|
| 382 | { |
---|
| 383 | OrxonoxGuiExec* exec =(OrxonoxGuiExec*)data; |
---|
| 384 | PRINT(3)( "Quitting Orxonox %p\n", exec); |
---|
| 385 | if(exec->shouldsave()) |
---|
| 386 | exec->writeToFile(Window::mainWindow); |
---|
| 387 | gtk_main_quit(); |
---|
| 388 | } |
---|
| 389 | #else /* HAVE_GTK2 */ |
---|
| 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 | int OrxonoxGuiExec::startOrxonox(void* widget, void* data) |
---|
| 396 | { |
---|
| 397 | OrxonoxGuiExec* exec =(OrxonoxGuiExec*)data; |
---|
| 398 | PRINT(3)("Starting Orxonox\n"); |
---|
| 399 | if(exec->shouldsave()) |
---|
| 400 | exec->writeToFile(Window::mainWindow); |
---|
| 401 | system("cd ..;./orxonox"); //!< \todo fix this. should execute orxonox for real(coded not over the shell) |
---|
| 402 | } |
---|
| 403 | /** |
---|
| 404 | \brief Quits ORXONOX. |
---|
| 405 | \param widget the widget that executed the Quit command |
---|
| 406 | \param data additional data |
---|
| 407 | |
---|
| 408 | This is a Signal and can be executed through Widget::signal_connect |
---|
| 409 | */ |
---|
| 410 | int OrxonoxGuiExec::quitOrxonox(void* widget, void* data) |
---|
| 411 | { |
---|
| 412 | OrxonoxGuiExec* exec =(OrxonoxGuiExec*)data; |
---|
| 413 | PRINT(3)("Quiting Orxonox"); |
---|
| 414 | if(exec->shouldsave()) |
---|
| 415 | exec->writeToFile(Window::mainWindow); |
---|
| 416 | } |
---|
| 417 | |
---|
[3165] | 418 | #endif /* HAVE_GTK2 */ |
---|