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