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