| 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 |   saveSettings->saveable = true; | 
|---|
| 48 |   execBox->fill (saveSettings); | 
|---|
| 49 |   verboseMode = new Menu ("verbose mode", "no output", "verbose", "debug", "lastItem"); | 
|---|
| 50 |   verboseMode->setFlagName ("verbose", "v", 0); | 
|---|
| 51 |   verboseMode->saveable = true; | 
|---|
| 52 |   execBox->fill (verboseMode); | 
|---|
| 53 |   alwaysShow = new CheckButton ("Always Show this Menu"); | 
|---|
| 54 |   alwaysShow->setFlagName ("gui", "g", 0); | 
|---|
| 55 |   alwaysShow->saveable = true; | 
|---|
| 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 |  | 
|---|
| 64 | /** | 
|---|
| 65 |    \brief Return the Frame | 
|---|
| 66 |    \return Returns the Exec-frame | 
|---|
| 67 | */ | 
|---|
| 68 | Widget* OrxonoxGuiExec::getWidget () | 
|---|
| 69 | { | 
|---|
| 70 |   return execFrame; | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | /* FILE HANDLING */ | 
|---|
| 74 |  | 
|---|
| 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 | */ | 
|---|
| 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 |  | 
|---|
| 97 | /** | 
|---|
| 98 |    \brief checks if a option should be saved. | 
|---|
| 99 |    \return 1 if it should 0 if not/ | 
|---|
| 100 | */ | 
|---|
| 101 | int OrxonoxGuiExec::shouldsave () | 
|---|
| 102 | { | 
|---|
| 103 |   return (static_cast<Option*>(saveSettings)->value); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 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 | */ | 
|---|
| 111 | void OrxonoxGuiExec::writeToFile (Widget* widget) | 
|---|
| 112 | { | 
|---|
| 113 |   CONFIG_FILE = fopen (configFile, "w"); | 
|---|
| 114 |   if (CONFIG_FILE) | 
|---|
| 115 |     writeFileText (widget, 0); | 
|---|
| 116 |   fclose (CONFIG_FILE); | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | /** | 
|---|
| 120 |    \brief Actually writes into the configuration file to the disk. | 
|---|
| 121 |    \param widget from which Widget on should be saved. | 
|---|
| 122 |    \param depth initially "0", and grows higher, while new Groups are bundeled. | 
|---|
| 123 | */ | 
|---|
| 124 | void OrxonoxGuiExec::writeFileText (Widget* widget, int depth) | 
|---|
| 125 | { | 
|---|
| 126 |   int counter = 0; | 
|---|
| 127 |   while (counter < depth && ((widget->is_option>0 | 
|---|
| 128 |                               && (static_cast<Option*>(widget)->saveable) ) | 
|---|
| 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. | 
|---|
| 137 |   if (widget->is_option <0) | 
|---|
| 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); | 
|---|
| 143 |           fprintf(CONFIG_FILE, "\n"); | 
|---|
| 144 |         } | 
|---|
| 145 |       else | 
|---|
| 146 |         { | 
|---|
| 147 |           writeFileText (static_cast<Packer*>(widget)->down, depth); | 
|---|
| 148 |         } | 
|---|
| 149 |     }  | 
|---|
| 150 |   //  if (widget->is_option == 0) | 
|---|
| 151 |   //    printf ("%s\n",widget->label); | 
|---|
| 152 |   if (widget->is_option >= 1) | 
|---|
| 153 |     if  (static_cast<Option*>(widget)->saveable) | 
|---|
| 154 |       { | 
|---|
| 155 |         char Buffer[256]; | 
|---|
| 156 |         char* space2under; | 
|---|
| 157 |         sprintf (Buffer, "%s", static_cast<Option*>(widget)->label); | 
|---|
| 158 |         if (strchr (Buffer, '_')) | 
|---|
| 159 |           cout << "Warning Optionname" << Buffer << " is not Valid for Saving, because it includes an underscore" << endl; | 
|---|
| 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 |       } | 
|---|
| 166 |  | 
|---|
| 167 |   if (widget->next != NULL) | 
|---|
| 168 |     writeFileText (widget->next, depth); | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | /** | 
|---|
| 172 |    \brief Reads in Configuration Data. | 
|---|
| 173 |    \param widget from which Widget on should be saved. | 
|---|
| 174 | */ | 
|---|
| 175 | void OrxonoxGuiExec::readFromFile (Widget* widget) | 
|---|
| 176 | { | 
|---|
| 177 |   CONFIG_FILE = fopen (configFile, "r"); | 
|---|
| 178 |   if (CONFIG_FILE) | 
|---|
| 179 |     { | 
|---|
| 180 |       Widget* groupWidget = widget; | 
|---|
| 181 |       char Buffer[256] = ""; | 
|---|
| 182 |       char Variable[256]= ""; | 
|---|
| 183 |       int Value; | 
|---|
| 184 |       while (fscanf (CONFIG_FILE, "%s", Buffer) != EOF) | 
|---|
| 185 |         { | 
|---|
| 186 |           if (!strncmp (Buffer, "[", 1)) | 
|---|
| 187 |             { | 
|---|
| 188 |               if ((groupWidget = locateGroup (widget, Buffer, 1))==NULL) | 
|---|
| 189 |                 { | 
|---|
| 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"; | 
|---|
| 191 |                   groupWidget = widget; | 
|---|
| 192 |                 } | 
|---|
| 193 |             } | 
|---|
| 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); | 
|---|
| 204 |               readFileText (groupWidget, Variable, Value, 0); | 
|---|
| 205 |               sprintf (Variable, ""); | 
|---|
| 206 |             } | 
|---|
| 207 |           sprintf (Variable, "%s", Buffer); | 
|---|
| 208 |         } | 
|---|
| 209 |       widget->walkThrough(widget->setOptions); | 
|---|
| 210 |     } | 
|---|
| 211 | } | 
|---|
| 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 | 
|---|
| 217 |    \param depth the depth of the local Widget | 
|---|
| 218 | */ | 
|---|
| 219 | void OrxonoxGuiExec::readFileText (Widget* widget, char* variableName, int variableValue, int depth) | 
|---|
| 220 | { | 
|---|
| 221 |   if (widget->is_option >= 1) | 
|---|
| 222 |     if (!strcmp (static_cast<Option*>(widget)->label, variableName)) | 
|---|
| 223 |         static_cast<Option*>(widget)->value = variableValue; | 
|---|
| 224 |  | 
|---|
| 225 |   if (widget->is_option < 0) | 
|---|
| 226 |     { | 
|---|
| 227 |       readFileText (static_cast<Packer*>(widget)->down, variableName, variableValue, depth+1); | 
|---|
| 228 |     }  | 
|---|
| 229 |  | 
|---|
| 230 |   if (widget->next != NULL && depth !=0) | 
|---|
| 231 |     readFileText (widget->next, variableName, variableValue, depth); | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | /** | 
|---|
| 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 | /** | 
|---|
| 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 | */ | 
|---|
| 275 | gint startOrxonox (GtkWidget *widget, Widget* data) | 
|---|
| 276 | { | 
|---|
| 277 |   cout << "Starting Orxonox" <<endl; | 
|---|
| 278 | } | 
|---|