| 1 | #include "orxonox_gui_exec.h" |
|---|
| 2 | #include <iostream> |
|---|
| 3 | #include <string> |
|---|
| 4 | |
|---|
| 5 | OrxonoxGuiExec::OrxonoxGuiExec (Window* orxonoxGUI) |
|---|
| 6 | { |
|---|
| 7 | configFile = (char*)malloc (512*sizeof (char)); |
|---|
| 8 | |
|---|
| 9 | execFrame = new Frame ("Execute-Tags:"); |
|---|
| 10 | execBox = new Box ('v'); |
|---|
| 11 | |
|---|
| 12 | start = new Button ("Start"); |
|---|
| 13 | execBox->fill (start); |
|---|
| 14 | saveSettings = new CheckButton ("Save Settings"); |
|---|
| 15 | saveSettings->value = 1; |
|---|
| 16 | execBox->fill (saveSettings); |
|---|
| 17 | verboseMode = new Menu ("verbose mode", "no output", "verbose", "debug", "lastItem"); |
|---|
| 18 | verboseMode->setFlagName ("verbose", "v", 0); |
|---|
| 19 | execBox->fill (verboseMode); |
|---|
| 20 | alwaysShow = new CheckButton ("Always Show this Menu"); |
|---|
| 21 | alwaysShow->setFlagName ("gui", "g", 0); |
|---|
| 22 | execBox->fill (alwaysShow); |
|---|
| 23 | quit = new Button ("Quit"); |
|---|
| 24 | quit->connectSignal ("clicked", orxonoxGUI->orxonox_gui_quit); |
|---|
| 25 | execBox->fill (quit); |
|---|
| 26 | |
|---|
| 27 | execFrame->fill (execBox); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | Frame* OrxonoxGuiExec::getFrame () |
|---|
| 31 | { |
|---|
| 32 | return execFrame; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | /* FILE HANDLING */ |
|---|
| 36 | |
|---|
| 37 | void OrxonoxGuiExec::setFilename (char* filename) |
|---|
| 38 | { |
|---|
| 39 | char* buffer = (char*) malloc (512*sizeof(buffer)); |
|---|
| 40 | sprintf (buffer, "%s", filename); |
|---|
| 41 | if (!strncmp (buffer, "~/", 2)) |
|---|
| 42 | { |
|---|
| 43 | #ifdef __WIN32__ |
|---|
| 44 | sprintf (configFile, "%s/%s", getenv ("USERPROFILE"), buffer+2); |
|---|
| 45 | #else |
|---|
| 46 | sprintf (configFile, "%s/%s", getenv ("HOME"), buffer+2); |
|---|
| 47 | #endif |
|---|
| 48 | } |
|---|
| 49 | else if (buffer) |
|---|
| 50 | sprintf(configFile, "%s", buffer); |
|---|
| 51 | delete buffer; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | int OrxonoxGuiExec::shouldsave () |
|---|
| 55 | { |
|---|
| 56 | return ( static_cast<Option*>(saveSettings)->value); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | void OrxonoxGuiExec::writeToFile (Widget* widget) |
|---|
| 60 | { |
|---|
| 61 | CONFIG_FILE = fopen (configFile, "w"); |
|---|
| 62 | if (CONFIG_FILE) |
|---|
| 63 | writeFileText (widget); |
|---|
| 64 | fclose (CONFIG_FILE); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void OrxonoxGuiExec::writeFileText (Widget* widget) |
|---|
| 68 | { |
|---|
| 69 | if (widget->is_option >= 1) |
|---|
| 70 | if (strcmp (static_cast<Option*>(widget)->flag_name, "") || strcmp (static_cast<Option*>(widget)->flag_name_short, "")) |
|---|
| 71 | { |
|---|
| 72 | char Buffer[256]; |
|---|
| 73 | char* space2under; |
|---|
| 74 | sprintf (Buffer, "%s", static_cast<Option*>(widget)->option_name); |
|---|
| 75 | if (strchr (Buffer, '_')) |
|---|
| 76 | cout << "Warning Optionname" << Buffer << " is not Valid for Saving, because it includes an underscore" << endl; |
|---|
| 77 | while (space2under = strchr(Buffer, ' ')) |
|---|
| 78 | { |
|---|
| 79 | sprintf (space2under, "_%s", space2under+1); |
|---|
| 80 | } |
|---|
| 81 | fprintf (CONFIG_FILE, "%s = %i\n", Buffer, static_cast<Option*>(widget)->value); |
|---|
| 82 | } |
|---|
| 83 | switch (widget->is_option) |
|---|
| 84 | { |
|---|
| 85 | case -1: |
|---|
| 86 | writeFileText (static_cast<Container*>(widget)->down); |
|---|
| 87 | break; |
|---|
| 88 | case -2: |
|---|
| 89 | writeFileText (static_cast<Box*>(widget)->down); |
|---|
| 90 | break; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | if (widget->next != NULL) |
|---|
| 94 | writeFileText (widget->next); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | void OrxonoxGuiExec::readFromFile (Widget* widget) |
|---|
| 98 | { |
|---|
| 99 | CONFIG_FILE = fopen (configFile, "r"); |
|---|
| 100 | if (CONFIG_FILE) |
|---|
| 101 | { |
|---|
| 102 | char Buffer[256] = ""; |
|---|
| 103 | char Variable[256]= ""; |
|---|
| 104 | int Value; |
|---|
| 105 | while (fscanf (CONFIG_FILE, "%s", Buffer) != EOF) |
|---|
| 106 | { |
|---|
| 107 | if (!strcmp (Buffer, "=")) |
|---|
| 108 | { |
|---|
| 109 | char* under2space; |
|---|
| 110 | while (under2space = strchr(Variable, '_')) |
|---|
| 111 | { |
|---|
| 112 | sprintf (under2space, " %s", under2space+1); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | fscanf (CONFIG_FILE, "%s", Buffer); |
|---|
| 116 | Value = atoi(Buffer); |
|---|
| 117 | readFileText (widget, Variable, Value); |
|---|
| 118 | sprintf (Variable, ""); |
|---|
| 119 | } |
|---|
| 120 | sprintf (Variable, "%s", Buffer); |
|---|
| 121 | } |
|---|
| 122 | widget->setOptions(); |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | void OrxonoxGuiExec::readFileText (Widget* widget, char* variableName, int variableValue) |
|---|
| 127 | { |
|---|
| 128 | if (widget->is_option >= 1) |
|---|
| 129 | if (!strcmp (static_cast<Option*>(widget)->option_name, variableName)) |
|---|
| 130 | static_cast<Option*>(widget)->value = variableValue; |
|---|
| 131 | |
|---|
| 132 | switch (widget->is_option) |
|---|
| 133 | { |
|---|
| 134 | case -1: |
|---|
| 135 | readFileText (static_cast<Container*>(widget)->down, variableName, variableValue); |
|---|
| 136 | break; |
|---|
| 137 | case -2: |
|---|
| 138 | readFileText (static_cast<Box*>(widget)->down, variableName, variableValue); |
|---|
| 139 | break; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | if (widget->next != NULL) |
|---|
| 143 | readFileText (widget->next, variableName, variableValue); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | } |
|---|