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