[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 | |
---|
[4047] | 26 | #include "gui_exec.h" |
---|
[3625] | 27 | |
---|
[4051] | 28 | #include "resource_manager.h" |
---|
[4049] | 29 | |
---|
[4036] | 30 | #include <string.h> |
---|
[4049] | 31 | #include <stdlib.h> |
---|
[3625] | 32 | #include <sys/stat.h> |
---|
| 33 | #include <sys/types.h> |
---|
| 34 | |
---|
[3423] | 35 | HashTable* orxonoxFlagHash; |
---|
| 36 | |
---|
[2588] | 37 | /** |
---|
| 38 | \brief Creates the Exec-Frame |
---|
| 39 | */ |
---|
[4056] | 40 | GuiExec::GuiExec(void) |
---|
[2018] | 41 | { |
---|
[4051] | 42 | Frame* execFrame; //!< The Frame that holds the ExecutionOptions. |
---|
[2018] | 43 | |
---|
[4051] | 44 | this->confFile = NULL; |
---|
| 45 | this->confDir = NULL; |
---|
| 46 | |
---|
| 47 | execFrame = new Frame("Execute-Tags:"); |
---|
| 48 | { |
---|
| 49 | Box* execBox; //!< The Box that holds the ExecutionOptions. |
---|
| 50 | |
---|
| 51 | execBox = new Box('v'); |
---|
| 52 | execFrame->setGroupName("misc"); |
---|
| 53 | { |
---|
| 54 | Button* start; //!< The start Button of orxonox. |
---|
| 55 | Menu* verboseMode; //!< A Menu for setting the verbose-Mode. \todo setting up a verbose-class. |
---|
| 56 | CheckButton* alwaysShow; //!< A CheckButton, for if orxonox should start with or without gui. |
---|
| 57 | Button* quit; //!< A Button to quit the Gui without starting orxonox. |
---|
| 58 | |
---|
| 59 | start = new Button("Start"); |
---|
[3165] | 60 | #ifdef HAVE_GTK2 |
---|
[4051] | 61 | start->connectSignal("clicked", this, startOrxonox); |
---|
[3165] | 62 | #endif /* HAVE_GTK2 */ |
---|
[4051] | 63 | execBox->fill(start); |
---|
| 64 | this->saveSettings = new CheckButton("Save Settings"); |
---|
| 65 | this->saveSettings->value = 1; |
---|
| 66 | this->saveSettings->saveability(); |
---|
| 67 | execBox->fill(this->saveSettings); |
---|
| 68 | verboseMode = new Menu("verbose mode", "nothing", "error", "warning", "info", "lastItem"); |
---|
[4058] | 69 | verboseMode->setFlagName("verbose", "v", 2); |
---|
[4051] | 70 | verboseMode->saveability(); |
---|
| 71 | execBox->fill(verboseMode); |
---|
| 72 | alwaysShow = new CheckButton("Always Show this Menu"); |
---|
| 73 | alwaysShow->setFlagName("gui", "g", 0); |
---|
| 74 | alwaysShow->saveability(); |
---|
| 75 | execBox->fill(alwaysShow); |
---|
| 76 | quit = new Button("Quit"); |
---|
[3165] | 77 | #ifdef HAVE_GTK2 |
---|
[4056] | 78 | quit->connectSignal("clicked", this, GuiExec::quitGui); |
---|
| 79 | // Window::mainWindow->connectSignal("remove", this, GuiExec::quitGui); |
---|
| 80 | Window::mainWindow->connectSignal("destroy", this, GuiExec::quitGui); |
---|
[3165] | 81 | #endif /* HAVE_GTK2 */ |
---|
[4051] | 82 | execBox->fill(quit); |
---|
| 83 | } |
---|
| 84 | execFrame->fill(execBox); |
---|
| 85 | } |
---|
| 86 | setMainWidget(execFrame); |
---|
[2018] | 87 | } |
---|
| 88 | |
---|
[2588] | 89 | /** |
---|
[3423] | 90 | \brief Destructs the Execution-stuff |
---|
| 91 | */ |
---|
[4056] | 92 | GuiExec::~GuiExec(void) |
---|
[3423] | 93 | { |
---|
[4051] | 94 | if(this->confFile) |
---|
| 95 | delete []this->confFile; |
---|
| 96 | if(this->confDir) |
---|
| 97 | delete []this->confDir; |
---|
[3423] | 98 | } |
---|
| 99 | |
---|
[2018] | 100 | /* FILE HANDLING */ |
---|
| 101 | |
---|
[2588] | 102 | /** |
---|
[3625] | 103 | \brief sets the Directory of the configuration files |
---|
| 104 | \param confDir the Directory for the configuration files |
---|
| 105 | */ |
---|
[4056] | 106 | void GuiExec::setConfDir(const char* confDir) |
---|
[3625] | 107 | { |
---|
[4051] | 108 | this->confDir = ResourceManager::homeDirCheck(confDir); |
---|
| 109 | |
---|
[3625] | 110 | PRINTF(3)("Config Directory is: %s.\n", this->confDir); |
---|
[4051] | 111 | //! \todo F** Windows-support |
---|
[4052] | 112 | #ifndef __WIN32__ |
---|
[3625] | 113 | mkdir(this->confDir, 0755); |
---|
[4052] | 114 | #else /* __WiN32__ */ |
---|
| 115 | mkdir(this->confDir); |
---|
| 116 | #endif /* __WIN32__ */ |
---|
[3625] | 117 | } |
---|
| 118 | |
---|
| 119 | /** |
---|
[3423] | 120 | \brief Sets the location of the configuration File. |
---|
[3625] | 121 | \param fileName the location of the configFile |
---|
[3423] | 122 | |
---|
| 123 | The name will be parsed from ~/ to /home/[username] on unix and c:/Documents and Settings/username/Settings/ on Windows |
---|
[2588] | 124 | */ |
---|
[4056] | 125 | void GuiExec::setConfFile(const char* fileName) |
---|
[2018] | 126 | { |
---|
[3625] | 127 | if (!this->confDir) |
---|
| 128 | this->setConfDir("~/"); |
---|
[4051] | 129 | this->confFile = new char[strlen(this->confDir)+strlen(fileName)+2]; |
---|
| 130 | sprintf(this->confFile, "%s/%s", this->confDir, fileName); |
---|
| 131 | PRINTF(3)("ConfigurationFile is %s.\n", this->confFile); |
---|
[2018] | 132 | } |
---|
| 133 | |
---|
[2588] | 134 | /** |
---|
[3423] | 135 | \returns The name of the Configuration-File |
---|
| 136 | */ |
---|
[4056] | 137 | const char* GuiExec::getConfigFile(void) const |
---|
[3423] | 138 | { |
---|
[4051] | 139 | return this->confFile; |
---|
[3423] | 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
[2588] | 143 | \brief checks if a option should be saved. |
---|
| 144 | \return 1 if it should 0 if not/ |
---|
| 145 | */ |
---|
[4056] | 146 | int GuiExec::shouldsave() |
---|
[2018] | 147 | { |
---|
[3423] | 148 | return(static_cast<Option*>(this->saveSettings)->value); |
---|
[2018] | 149 | } |
---|
| 150 | |
---|
[2588] | 151 | /** |
---|
| 152 | \brief Saves the configuration-file to the Disk.\n |
---|
[3187] | 153 | \param widget from which Widget on should be saved. |
---|
| 154 | |
---|
[4056] | 155 | this Function only opens and closes the file, in between GuiExec::writeFileText(Widget* widget) will execute the real writing process. |
---|
[2588] | 156 | */ |
---|
[4056] | 157 | void GuiExec::writeToFile(Widget* widget) |
---|
[2018] | 158 | { |
---|
[4051] | 159 | this->CONFIG_FILE = fopen(this->confFile, "w"); |
---|
[3423] | 160 | if(this->CONFIG_FILE) |
---|
| 161 | this->writeFileText(widget, 0); |
---|
| 162 | fclose(this->CONFIG_FILE); |
---|
[2018] | 163 | } |
---|
| 164 | |
---|
[2588] | 165 | /** |
---|
| 166 | \brief Actually writes into the configuration file to the disk. |
---|
| 167 | \param widget from which Widget on should be saved. |
---|
[2615] | 168 | \param depth initially "0", and grows higher, while new Groups are bundeled. |
---|
[2588] | 169 | */ |
---|
[4056] | 170 | void GuiExec::writeFileText(Widget* widget, int depth) |
---|
[2018] | 171 | { |
---|
[2615] | 172 | int counter = 0; |
---|
[3423] | 173 | while(counter < depth &&((widget->isOption>0 |
---|
| 174 | &&(static_cast<Option*>(widget)->isSaveable())) |
---|
| 175 | ||(widget->isOption<0 |
---|
| 176 | && static_cast<Packer*>(widget)->getGroupName()))) |
---|
[2615] | 177 | { |
---|
[3423] | 178 | fprintf(this->CONFIG_FILE, " ", depth); |
---|
[2615] | 179 | counter++; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | // check if it is a Packer, and if it is, check if it has a name and if there is something in it. |
---|
[3423] | 183 | if(widget->isOption <0) |
---|
[2615] | 184 | { |
---|
[3423] | 185 | if(static_cast<Packer*>(widget)->getGroupName()) |
---|
[2615] | 186 | { |
---|
[3423] | 187 | fprintf(CONFIG_FILE, "[%s]\n", static_cast<Packer*>(widget)->getGroupName()); |
---|
| 188 | this->writeFileText(static_cast<Packer*>(widget)->down, depth+1); |
---|
[2625] | 189 | fprintf(CONFIG_FILE, "\n"); |
---|
[2615] | 190 | } |
---|
| 191 | else |
---|
| 192 | { |
---|
[3423] | 193 | this->writeFileText(static_cast<Packer*>(widget)->down, depth); |
---|
[2615] | 194 | } |
---|
| 195 | } |
---|
[3423] | 196 | // if(widget->isOption == 0) |
---|
| 197 | // printf("%s\n",widget->title); |
---|
| 198 | if(widget->isOption >= 1) |
---|
| 199 | if (static_cast<Option*>(widget)->isSaveable()) |
---|
[2018] | 200 | { |
---|
| 201 | char Buffer[256]; |
---|
| 202 | char* space2under; |
---|
[3423] | 203 | strcpy(Buffer, static_cast<Option*>(widget)->title); |
---|
| 204 | if(strchr(Buffer, '_')) |
---|
[4026] | 205 | PRINTF(2)("Optionname %s is not Valid for Saving, because it includes an underscore\n", Buffer); |
---|
[3423] | 206 | while(space2under = strchr(Buffer, ' ')) |
---|
[2018] | 207 | { |
---|
[3161] | 208 | space2under[0] = '_'; |
---|
[2018] | 209 | } |
---|
[3625] | 210 | fprintf(CONFIG_FILE, "%s = %s\n", Buffer, static_cast<Option*>(widget)->save()); |
---|
[2018] | 211 | } |
---|
[2615] | 212 | |
---|
[3423] | 213 | if(widget->next != NULL) |
---|
| 214 | this->writeFileText(widget->next, depth); |
---|
[2018] | 215 | } |
---|
| 216 | |
---|
[2588] | 217 | /** |
---|
| 218 | \brief Reads in Configuration Data. |
---|
| 219 | \param widget from which Widget on should be saved. |
---|
| 220 | */ |
---|
[4056] | 221 | void GuiExec::readFromFile(Widget* widget) |
---|
[2018] | 222 | { |
---|
[4051] | 223 | this->CONFIG_FILE = fopen(this->confFile, "r"); |
---|
[3423] | 224 | VarInfo varInfo; |
---|
| 225 | if(this->CONFIG_FILE) |
---|
[2018] | 226 | { |
---|
[2634] | 227 | Widget* groupWidget = widget; |
---|
[2018] | 228 | char Buffer[256] = ""; |
---|
| 229 | char Variable[256]= ""; |
---|
[3162] | 230 | char* Value; |
---|
[3423] | 231 | while(fscanf(this->CONFIG_FILE, "%s", Buffer) != EOF) |
---|
[2018] | 232 | { |
---|
[3161] | 233 | // group-search // |
---|
[3423] | 234 | if(!strncmp(Buffer, "[", 1)) |
---|
[2634] | 235 | { |
---|
[3423] | 236 | if((groupWidget = locateGroup(widget, Buffer, 1))==NULL) |
---|
[2634] | 237 | { |
---|
[4026] | 238 | PRINTF(2)("!!There is no group called %s in this GUI.\n First best Widget will get the Infos assigned.\n Config-File will be updated in next Save\n", Buffer); |
---|
[2634] | 239 | groupWidget = widget; |
---|
| 240 | } |
---|
[3423] | 241 | else |
---|
[4026] | 242 | PRINT(5)("Group %s located.\n", static_cast<Packer*>(groupWidget)->groupName); |
---|
[2634] | 243 | } |
---|
[3161] | 244 | // option-setting // |
---|
[3423] | 245 | if(!strcmp(Buffer, "=")) |
---|
[2018] | 246 | { |
---|
| 247 | char* under2space; |
---|
[3423] | 248 | while(under2space = strchr(Variable, '_')) |
---|
[2018] | 249 | { |
---|
[3423] | 250 | sprintf(under2space, " %s", under2space+1); |
---|
[2018] | 251 | } |
---|
| 252 | |
---|
[3423] | 253 | fscanf(this->CONFIG_FILE, "%s", Buffer); |
---|
| 254 | varInfo.variableName = Variable; |
---|
| 255 | varInfo.variableValue = Buffer; |
---|
| 256 | groupWidget->walkThrough(this->readFileText, &varInfo, 0); |
---|
| 257 | sprintf(Variable, ""); |
---|
[2018] | 258 | } |
---|
[3423] | 259 | sprintf(Variable, "%s", Buffer); |
---|
[2018] | 260 | } |
---|
[3423] | 261 | widget->walkThrough(widget->setOptions, 0); |
---|
[2018] | 262 | } |
---|
| 263 | } |
---|
[3423] | 264 | |
---|
[2588] | 265 | /** |
---|
| 266 | \brief Maps Confugurations to the Options. |
---|
| 267 | \param widget which widget downwards |
---|
[3423] | 268 | \param varInfo Information about the Variable to read |
---|
[2588] | 269 | */ |
---|
[4056] | 270 | void GuiExec::readFileText(Widget* widget, void* varInfo) |
---|
[2018] | 271 | { |
---|
[3423] | 272 | VarInfo* info =(VarInfo*)varInfo; |
---|
| 273 | if(widget->title && !strcmp(widget->title, info->variableName)) |
---|
[3162] | 274 | { |
---|
[4026] | 275 | PRINT(5)("Located Option %s.\n", widget->title); |
---|
[3625] | 276 | if(widget->isOption >= 1) |
---|
[4051] | 277 | static_cast<Option*>(widget)->load(info->variableValue); |
---|
[3162] | 278 | } |
---|
[2053] | 279 | } |
---|
[2018] | 280 | |
---|
[2588] | 281 | /** |
---|
[3187] | 282 | \brief Locates a Group. |
---|
| 283 | \param widget The Widget from where to search from |
---|
| 284 | \param groupName The GroupName for which to search. |
---|
| 285 | \param depth The Depth of the search seen from the first widget we searched from. |
---|
| 286 | \returns The Widget that holds the Group, or the NULL if the Group wasn't found. |
---|
[3423] | 287 | |
---|
| 288 | \todo do this in gui-gtk. |
---|
[2634] | 289 | */ |
---|
[4056] | 290 | Widget* GuiExec::locateGroup(Widget* widget, char* groupName, int depth) |
---|
[2634] | 291 | { |
---|
| 292 | Widget* tmp; |
---|
| 293 | |
---|
| 294 | // removes the trailing and ending [ ]. |
---|
[3423] | 295 | if(!strncmp(groupName, "[", 1)) |
---|
[2634] | 296 | { |
---|
| 297 | groupName = groupName+1; |
---|
| 298 | groupName[strlen(groupName)-1] = '\0'; |
---|
| 299 | } |
---|
| 300 | |
---|
[3423] | 301 | if(widget->isOption < 0) |
---|
[2634] | 302 | { |
---|
[4051] | 303 | if(static_cast<Packer*>(widget)->getGroupName() && |
---|
| 304 | !strcmp(groupName, static_cast<Packer*>(widget)->getGroupName())) |
---|
[2634] | 305 | { |
---|
| 306 | return widget; |
---|
| 307 | } |
---|
| 308 | else |
---|
| 309 | { |
---|
[3423] | 310 | if((tmp = locateGroup(static_cast<Packer*>(widget)->down, groupName, depth+1)) != NULL) |
---|
[2634] | 311 | return tmp; |
---|
| 312 | } |
---|
| 313 | } |
---|
| 314 | |
---|
[3423] | 315 | if(widget->next != NULL && depth != 0) |
---|
[2634] | 316 | { |
---|
[3423] | 317 | if((tmp = locateGroup(widget->next, groupName, depth)) != NULL) |
---|
[2634] | 318 | return tmp; |
---|
| 319 | } |
---|
| 320 | return NULL; |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | /** |
---|
[3423] | 324 | \brief Starts ORXONOX.(not really implemented yet, but the function is there.\n |
---|
[2588] | 325 | \param widget the widget that executed the start command |
---|
| 326 | \param data additional data |
---|
[3423] | 327 | |
---|
| 328 | This is a Signal and can be executed through Widget::signal_connect |
---|
[2588] | 329 | */ |
---|
[4039] | 330 | #ifdef HAVE_GTK2 |
---|
[4056] | 331 | int GuiExec::startOrxonox(GtkWidget* widget, void* data) |
---|
[4039] | 332 | #else /* HAVE_GTK2 */ |
---|
[4056] | 333 | int GuiExec::startOrxonox(void* widget, void* data) |
---|
[4039] | 334 | #endif /* HAVE_GTK2 */ |
---|
[2053] | 335 | { |
---|
[4041] | 336 | Window::mainWindow->hide(); |
---|
| 337 | |
---|
| 338 | #ifdef HAVE_GTK2 |
---|
| 339 | gtk_widget_destroy(Window::mainWindow->widget); |
---|
| 340 | #else |
---|
[4042] | 341 | quitGui(widget, data); |
---|
[4041] | 342 | #endif /* HAVE_GTK2 */ |
---|
| 343 | |
---|
[4034] | 344 | PRINT(3)("Starting Orxonox\n"); |
---|
[4056] | 345 | Gui::startOrxonox = true; |
---|
[2018] | 346 | } |
---|
[3423] | 347 | |
---|
| 348 | /** |
---|
| 349 | \brief Starts ORXONOX.(not really implemented yet, but the function is there.\n |
---|
| 350 | \param widget the widget that executed the start command |
---|
| 351 | \param data additional data |
---|
| 352 | |
---|
| 353 | This is a Signal and can be executed through Widget::signal_connect |
---|
| 354 | */ |
---|
[4039] | 355 | #ifdef HAVE_GTK2 |
---|
[4056] | 356 | int GuiExec::quitGui(GtkWidget* widget, void* data) |
---|
[4039] | 357 | #else /* HAVE_GTK2 */ |
---|
[4056] | 358 | int GuiExec::quitGui(void* widget, void* data) |
---|
[4039] | 359 | #endif /* HAVE_GTK2 */ |
---|
[3423] | 360 | { |
---|
[4056] | 361 | GuiExec* exec =(GuiExec*)data; |
---|
[3423] | 362 | if(exec->shouldsave()) |
---|
| 363 | exec->writeToFile(Window::mainWindow); |
---|
[4041] | 364 | #ifdef HAVE_GTK2 |
---|
[3423] | 365 | gtk_main_quit(); |
---|
[4042] | 366 | while(gtk_events_pending()) gtk_main_iteration(); |
---|
[4041] | 367 | #endif /* HAVE_GTK2 */ |
---|
[3423] | 368 | } |
---|