| 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 | |
|---|
| 26 | #include "orxonox_gui_exec.h" |
|---|
| 27 | #include <iostream> |
|---|
| 28 | #include <string> |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | \brief Creates the Exec-Frame |
|---|
| 32 | \param orxonoxGUI ExecFrame needs to know where to get the Options from |
|---|
| 33 | */ |
|---|
| 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'); |
|---|
| 40 | execFrame->setGroupName ("misc"); |
|---|
| 41 | |
|---|
| 42 | start = new Button ("Start"); |
|---|
| 43 | #ifdef HAVE_GTK2 |
|---|
| 44 | start->connectSignal ("clicked", startOrxonox); |
|---|
| 45 | #endif /* HAVE_GTK2 */ |
|---|
| 46 | execBox->fill (start); |
|---|
| 47 | saveSettings = new CheckButton ("Save Settings"); |
|---|
| 48 | saveSettings->value = 1; |
|---|
| 49 | saveSettings->saveable = true; |
|---|
| 50 | execBox->fill (saveSettings); |
|---|
| 51 | verboseMode = new Menu ("verbose mode", "no output", "verbose", "debug", "lastItem"); |
|---|
| 52 | verboseMode->setFlagName ("verbose", "v", 0); |
|---|
| 53 | verboseMode->saveable = true; |
|---|
| 54 | execBox->fill (verboseMode); |
|---|
| 55 | alwaysShow = new CheckButton ("Always Show this Menu"); |
|---|
| 56 | alwaysShow->setFlagName ("gui", "g", 0); |
|---|
| 57 | alwaysShow->saveable = true; |
|---|
| 58 | execBox->fill (alwaysShow); |
|---|
| 59 | quit = new Button ("Quit"); |
|---|
| 60 | #ifdef HAVE_GTK2 |
|---|
| 61 | quit->connectSignal ("clicked", orxonoxGUI->orxonox_gui_quit); |
|---|
| 62 | #endif /* HAVE_GTK2 */ |
|---|
| 63 | execBox->fill (quit); |
|---|
| 64 | |
|---|
| 65 | execFrame->fill (execBox); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | \brief Return the Frame |
|---|
| 70 | \return Returns the Exec-frame |
|---|
| 71 | */ |
|---|
| 72 | Widget* OrxonoxGuiExec::getWidget () |
|---|
| 73 | { |
|---|
| 74 | return execFrame; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /* FILE HANDLING */ |
|---|
| 78 | |
|---|
| 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 | */ |
|---|
| 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 | |
|---|
| 101 | /** |
|---|
| 102 | \brief checks if a option should be saved. |
|---|
| 103 | \return 1 if it should 0 if not/ |
|---|
| 104 | */ |
|---|
| 105 | int OrxonoxGuiExec::shouldsave () |
|---|
| 106 | { |
|---|
| 107 | return (static_cast<Option*>(saveSettings)->value); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | \brief Saves the configuration-file to the Disk.\n |
|---|
| 112 | \param widget from which Widget on should be saved. |
|---|
| 113 | |
|---|
| 114 | this Function only opens and closes the file, in between OrxonoxGuiExec::writeFileText (Widget* widget) will execute the real writing process. |
|---|
| 115 | */ |
|---|
| 116 | void OrxonoxGuiExec::writeToFile (Widget* widget) |
|---|
| 117 | { |
|---|
| 118 | CONFIG_FILE = fopen (configFile, "w"); |
|---|
| 119 | if (CONFIG_FILE) |
|---|
| 120 | writeFileText (widget, 0); |
|---|
| 121 | fclose (CONFIG_FILE); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | /** |
|---|
| 125 | \brief Actually writes into the configuration file to the disk. |
|---|
| 126 | \param widget from which Widget on should be saved. |
|---|
| 127 | \param depth initially "0", and grows higher, while new Groups are bundeled. |
|---|
| 128 | */ |
|---|
| 129 | void OrxonoxGuiExec::writeFileText (Widget* widget, int depth) |
|---|
| 130 | { |
|---|
| 131 | int counter = 0; |
|---|
| 132 | while (counter < depth && ((widget->isOption>0 |
|---|
| 133 | && (static_cast<Option*>(widget)->saveable) ) |
|---|
| 134 | || (widget->isOption<0 |
|---|
| 135 | && static_cast<Packer*>(widget)->getGroupName()))) |
|---|
| 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. |
|---|
| 142 | if (widget->isOption <0) |
|---|
| 143 | { |
|---|
| 144 | if (static_cast<Packer*>(widget)->getGroupName()) |
|---|
| 145 | { |
|---|
| 146 | fprintf (CONFIG_FILE, "[%s]\n", static_cast<Packer*>(widget)->getGroupName()); |
|---|
| 147 | writeFileText (static_cast<Packer*>(widget)->down, depth+1); |
|---|
| 148 | fprintf(CONFIG_FILE, "\n"); |
|---|
| 149 | } |
|---|
| 150 | else |
|---|
| 151 | { |
|---|
| 152 | writeFileText (static_cast<Packer*>(widget)->down, depth); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | // if (widget->isOption == 0) |
|---|
| 156 | // printf ("%s\n",widget->label); |
|---|
| 157 | if (widget->isOption >= 1) |
|---|
| 158 | if (static_cast<Option*>(widget)->saveable) |
|---|
| 159 | { |
|---|
| 160 | char Buffer[256]; |
|---|
| 161 | char* space2under; |
|---|
| 162 | strcpy (Buffer, static_cast<Option*>(widget)->label); |
|---|
| 163 | if (strchr (Buffer, '_')) |
|---|
| 164 | cout << "Warning Optionname" << Buffer << " is not Valid for Saving, because it includes an underscore" << endl; |
|---|
| 165 | while (space2under = strchr(Buffer, ' ')) |
|---|
| 166 | { |
|---|
| 167 | space2under[0] = '_'; |
|---|
| 168 | } |
|---|
| 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); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | if (widget->next != NULL) |
|---|
| 176 | writeFileText (widget->next, depth); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | /** |
|---|
| 180 | \brief Reads in Configuration Data. |
|---|
| 181 | \param widget from which Widget on should be saved. |
|---|
| 182 | */ |
|---|
| 183 | void OrxonoxGuiExec::readFromFile (Widget* widget) |
|---|
| 184 | { |
|---|
| 185 | CONFIG_FILE = fopen (configFile, "r"); |
|---|
| 186 | if (CONFIG_FILE) |
|---|
| 187 | { |
|---|
| 188 | Widget* groupWidget = widget; |
|---|
| 189 | char Buffer[256] = ""; |
|---|
| 190 | char Variable[256]= ""; |
|---|
| 191 | char* Value; |
|---|
| 192 | while (fscanf (CONFIG_FILE, "%s", Buffer) != EOF) |
|---|
| 193 | { |
|---|
| 194 | // group-search // |
|---|
| 195 | if (!strncmp (Buffer, "[", 1)) |
|---|
| 196 | { |
|---|
| 197 | if ((groupWidget = locateGroup (widget, Buffer, 1))==NULL) |
|---|
| 198 | { |
|---|
| 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"; |
|---|
| 200 | groupWidget = widget; |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | // option-setting // |
|---|
| 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); |
|---|
| 213 | Value = Buffer; |
|---|
| 214 | readFileText (groupWidget, Variable, Value, 0); |
|---|
| 215 | sprintf (Variable, ""); |
|---|
| 216 | } |
|---|
| 217 | sprintf (Variable, "%s", Buffer); |
|---|
| 218 | } |
|---|
| 219 | widget->walkThrough(widget->setOptions); |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 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 |
|---|
| 227 | \param depth the depth of the local Widget |
|---|
| 228 | */ |
|---|
| 229 | void OrxonoxGuiExec::readFileText (Widget* widget, char* variableName, char* variableValue, int depth) |
|---|
| 230 | { |
|---|
| 231 | if (widget->isOption >= 1 && widget->isOption <= 3) |
|---|
| 232 | { |
|---|
| 233 | if (!strcmp (static_cast<Option*>(widget)->label, variableName)) |
|---|
| 234 | static_cast<Option*>(widget)->value = atoi(variableValue); |
|---|
| 235 | } |
|---|
| 236 | else if (widget->isOption == 5) |
|---|
| 237 | { |
|---|
| 238 | if (!strcmp (static_cast<Option*>(widget)->label, variableName)) |
|---|
| 239 | static_cast<OptionLabel*>(widget)->setValue(variableValue); |
|---|
| 240 | } |
|---|
| 241 | if (widget->isOption < 0) |
|---|
| 242 | { |
|---|
| 243 | readFileText (static_cast<Packer*>(widget)->down, variableName, variableValue, depth+1); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | if (widget->next != NULL && depth !=0) |
|---|
| 247 | readFileText (widget->next, variableName, variableValue, depth); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | /** |
|---|
| 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. |
|---|
| 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 | |
|---|
| 268 | if (widget->isOption < 0) |
|---|
| 269 | { |
|---|
| 270 | if (static_cast<Packer*>(widget)->getGroupName() && !strcmp(groupName, static_cast<Packer*>(widget)->getGroupName())) |
|---|
| 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 | |
|---|
| 289 | #ifdef HAVE_GTK2 |
|---|
| 290 | /** |
|---|
| 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 | */ |
|---|
| 296 | gint startOrxonox (GtkWidget *widget, Widget* data) |
|---|
| 297 | { |
|---|
| 298 | cout << "Starting Orxonox" <<endl; |
|---|
| 299 | } |
|---|
| 300 | #endif /* HAVE_GTK2 */ |
|---|