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