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