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