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