/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ### File Specific: main-programmer: Benjamin Grauer */ #include "orxonox_gui_exec.h" #include #include /** \brief Creates the Exec-Frame \param orxonoxGUI ExecFrame needs to know where to get the Options from */ OrxonoxGuiExec::OrxonoxGuiExec (Window* orxonoxGUI) { configFile = (char*)malloc (512*sizeof (char)); execFrame = new Frame ("Execute-Tags:"); execBox = new Box ('v'); execFrame->setGroupName ("misc"); start = new Button ("Start"); #ifdef HAVE_GTK2 start->connectSignal ("clicked", startOrxonox); #endif /* HAVE_GTK2 */ execBox->fill (start); saveSettings = new CheckButton ("Save Settings"); saveSettings->value = 1; saveSettings->saveable = true; execBox->fill (saveSettings); verboseMode = new Menu ("verbose mode", "no output", "verbose", "debug", "lastItem"); verboseMode->setFlagName ("verbose", "v", 0); verboseMode->saveable = true; execBox->fill (verboseMode); alwaysShow = new CheckButton ("Always Show this Menu"); alwaysShow->setFlagName ("gui", "g", 0); alwaysShow->saveable = true; execBox->fill (alwaysShow); quit = new Button ("Quit"); #ifdef HAVE_GTK2 quit->connectSignal ("clicked", orxonoxGUI->orxonox_gui_quit); #endif /* HAVE_GTK2 */ execBox->fill (quit); execFrame->fill (execBox); } /** \brief Return the Frame \return Returns the Exec-frame */ Widget* OrxonoxGuiExec::getWidget () { return execFrame; } /* FILE HANDLING */ /** \brief Sets the location of the configuration File.\n * The name will be parsed from ~/ to /home/[username] on unix and c:/Documents and Settings/username/Settings/ on Windows \param filename the location of the configFile */ void OrxonoxGuiExec::setFilename (char* filename) { char* buffer = (char*) malloc (512*sizeof(buffer)); sprintf (buffer, "%s", filename); if (!strncmp (buffer, "~/", 2)) { #ifdef __WIN32__ sprintf (configFile, "%s/%s", getenv ("USERPROFILE"), buffer+2); #else sprintf (configFile, "%s/%s", getenv ("HOME"), buffer+2); #endif } else if (buffer) sprintf(configFile, "%s", buffer); delete buffer; } /** \brief checks if a option should be saved. \return 1 if it should 0 if not/ */ int OrxonoxGuiExec::shouldsave () { return (static_cast(saveSettings)->value); } /** \brief Saves the configuration-file to the Disk.\n this Function only opens and closes the file, in between OrxonoxGuiExec::writeFileText (Widget* widget) will execute the real writing process. \param widget from which Widget on should be saved. */ void OrxonoxGuiExec::writeToFile (Widget* widget) { CONFIG_FILE = fopen (configFile, "w"); if (CONFIG_FILE) writeFileText (widget, 0); fclose (CONFIG_FILE); } /** \brief Actually writes into the configuration file to the disk. \param widget from which Widget on should be saved. \param depth initially "0", and grows higher, while new Groups are bundeled. */ void OrxonoxGuiExec::writeFileText (Widget* widget, int depth) { int counter = 0; while (counter < depth && ((widget->isOption>0 && (static_cast(widget)->saveable) ) || (widget->isOption<0 && static_cast(widget)->getGroupName()))) { fprintf (CONFIG_FILE, " ", depth); counter++; } // check if it is a Packer, and if it is, check if it has a name and if there is something in it. if (widget->isOption <0) { if (static_cast(widget)->getGroupName()) { fprintf (CONFIG_FILE, "[%s]\n", static_cast(widget)->getGroupName()); writeFileText (static_cast(widget)->down, depth+1); fprintf(CONFIG_FILE, "\n"); } else { writeFileText (static_cast(widget)->down, depth); } } // if (widget->isOption == 0) // printf ("%s\n",widget->label); if (widget->isOption >= 1) if (static_cast(widget)->saveable) { char Buffer[256]; char* space2under; strcpy (Buffer, static_cast(widget)->label); if (strchr (Buffer, '_')) cout << "Warning Optionname" << Buffer << " is not Valid for Saving, because it includes an underscore" << endl; while (space2under = strchr(Buffer, ' ')) { space2under[0] = '_'; } if (widget->isOption <=3) fprintf (CONFIG_FILE, "%s = %d\n", Buffer, static_cast(widget)->value); else if (widget->isOption == 5) fprintf (CONFIG_FILE, "%s = %s\n", Buffer, static_cast(widget)->cValue); } if (widget->next != NULL) writeFileText (widget->next, depth); } /** \brief Reads in Configuration Data. \param widget from which Widget on should be saved. */ void OrxonoxGuiExec::readFromFile (Widget* widget) { CONFIG_FILE = fopen (configFile, "r"); if (CONFIG_FILE) { Widget* groupWidget = widget; char Buffer[256] = ""; char Variable[256]= ""; char* Value; while (fscanf (CONFIG_FILE, "%s", Buffer) != EOF) { // group-search // if (!strncmp (Buffer, "[", 1)) { if ((groupWidget = locateGroup (widget, Buffer, 1))==NULL) { 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"; groupWidget = widget; } } // option-setting // if (!strcmp (Buffer, "=")) { char* under2space; while (under2space = strchr(Variable, '_')) { sprintf (under2space, " %s", under2space+1); } fscanf (CONFIG_FILE, "%s", Buffer); Value = Buffer; readFileText (groupWidget, Variable, Value, 0); sprintf (Variable, ""); } sprintf (Variable, "%s", Buffer); } widget->walkThrough(widget->setOptions); } } /** \brief Maps Confugurations to the Options. \param widget which widget downwards \param variableName the name of the Variable that should be set up. \param variableValue the Value of the Variable that should be set up \param depth the depth of the local Widget */ void OrxonoxGuiExec::readFileText (Widget* widget, char* variableName, char* variableValue, int depth) { if (widget->isOption >= 1 && widget->isOption <= 3) { if (!strcmp (static_cast(widget)->label, variableName)) static_cast(widget)->value = atoi(variableValue); } else if (widget->isOption == 5) { if (!strcmp (static_cast(widget)->label, variableName)) static_cast(widget)->setValue(variableValue); } if (widget->isOption < 0) { readFileText (static_cast(widget)->down, variableName, variableValue, depth+1); } if (widget->next != NULL && depth !=0) readFileText (widget->next, variableName, variableValue, depth); } /** \brief locates a Group member */ Widget* OrxonoxGuiExec::locateGroup(Widget* widget, char* groupName, int depth) { Widget* tmp; // removes the trailing and ending [ ]. if (!strncmp (groupName, "[", 1)) { groupName = groupName+1; groupName[strlen(groupName)-1] = '\0'; } if (widget->isOption < 0) { if (static_cast(widget)->getGroupName() && !strcmp(groupName, static_cast(widget)->getGroupName())) { return widget; } else { if ((tmp = locateGroup (static_cast(widget)->down, groupName, depth+1)) != NULL) return tmp; } } if (widget->next != NULL && depth != 0) { if ((tmp = locateGroup (widget->next, groupName, depth)) != NULL) return tmp; } return NULL; } #ifdef HAVE_GTK2 /** \brief Starts ORXONOX. (not really implemented yet, but the function is there.\n This is a Signal and can be executed through Widget::signal_connect \param widget the widget that executed the start command \param data additional data */ gint startOrxonox (GtkWidget *widget, Widget* data) { cout << "Starting Orxonox" <