PrefsWindow.H
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef GUI_PREFSWINDOW_H_DEFINED
00039 #define GUI_PREFSWINDOW_H_DEFINED
00040
00041 #include "Image/SimpleFont.H"
00042 #include "Util/StringConversions.H"
00043
00044 #include <deque>
00045 #include <vector>
00046 #include <X11/Xlib.h>
00047
00048 class ModelComponent;
00049 class ModelParamBase;
00050 class XWinManaged;
00051
00052 class PrefItem;
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 class PrefsWindow
00075 {
00076 public:
00077
00078
00079 PrefsWindow(const std::string& wintitle = "preferences",
00080 const SimpleFont& font = SimpleFont::FIXED(10));
00081
00082 ~PrefsWindow();
00083
00084
00085 void setValueNumChars(int n);
00086
00087
00088 void setFont(const SimpleFont& font);
00089
00090
00091 void update();
00092
00093
00094 void addPrefForParam(ModelParamBase* mp, ModelComponent* comp);
00095
00096
00097 void addPrefsForComponent(ModelComponent* comp, bool recurse = false);
00098
00099 private:
00100 PrefsWindow(const PrefsWindow&);
00101 PrefsWindow& operator=(const PrefsWindow&);
00102
00103 friend class PrefItem;
00104
00105 void addItem(PrefItem* item, bool takeOwnership);
00106
00107 bool handleKeysym(KeySym ks, const std::string& s);
00108 bool handleButtonPress(XButtonEvent* ev);
00109
00110 void redraw();
00111
00112 enum State { EDITING, SCROLLING };
00113
00114 std::string itsWinTitle;
00115 XWinManaged* itsWin;
00116 SimpleFont itsFont;
00117 std::vector<PrefItem*> itsItems;
00118 std::vector<PrefItem*> itsOwnedItems;
00119 std::string itsEditBuffer;
00120 size_t itsCurrentItem;
00121 State itsState;
00122 int itsNameWidth;
00123 int itsNumWidth;
00124 bool itsDirty;
00125 };
00126
00127
00128 class PrefItem
00129 {
00130 public:
00131 PrefItem(PrefsWindow* pwin, const std::string& nm,
00132 bool pwinTakesOwnership = false);
00133
00134 virtual ~PrefItem();
00135
00136 virtual std::string getName() const;
00137
00138 virtual void fromString(const std::string& s) = 0;
00139
00140 virtual std::string toString() const = 0;
00141
00142 virtual void adjust(int val) = 0;
00143
00144 bool isValueChanged();
00145
00146 virtual bool isDisabled() const = 0;
00147
00148 protected:
00149 std::string name;
00150 bool isChanged;
00151 bool wasDisabled;
00152 };
00153
00154
00155 template <class T>
00156 class PrefItemNum : public PrefItem
00157 {
00158 private:
00159 T val;
00160 bool disabled;
00161
00162 template <class U>
00163 static void adjustHelper(U* val, int v)
00164 { *val += v; }
00165
00166 static void adjustHelper(bool* val, int v)
00167 { *val = (int(*val) + v) % 2; }
00168
00169 public:
00170 PrefItemNum(PrefsWindow* pwin, const std::string& nm, T v,
00171 bool pwinTakesOwnership = false)
00172 : PrefItem(pwin, nm, pwinTakesOwnership), val(v), disabled(false) {}
00173
00174 void set(T v) { if (this->val != v) { this->val = v; this->isChanged = true; } }
00175 T get() const { return this->val; }
00176
00177 virtual void fromString(const std::string& s)
00178 {
00179 if (s.size() == 0) return;
00180 try { this->set(fromStr<T>(s)); }
00181 catch (conversion_error& e) {}
00182 }
00183
00184 virtual std::string toString() const
00185 { return toStr(this->val); }
00186
00187 virtual void adjust(int v)
00188 { adjustHelper(&this->val, v); this->isChanged = true; }
00189
00190 virtual bool isDisabled() const { return disabled; }
00191
00192 void setDisabled(bool v) { if (v != disabled) { disabled = v; isChanged = true; } }
00193 };
00194
00195 typedef PrefItemNum<double> PrefItemDbl;
00196 typedef PrefItemNum<int> PrefItemInt;
00197 typedef PrefItemNum<uint> PrefItemUin;
00198 typedef PrefItemNum<byte> PrefItemByt;
00199 typedef PrefItemNum<bool> PrefItemBln;
00200
00201
00202 class PrefItemStr : public PrefItem
00203 {
00204 private:
00205 std::deque<std::string> itsHist;
00206 const size_t itsMaxSize;
00207 size_t itsPos;
00208 bool disabled;
00209
00210 public:
00211 PrefItemStr(PrefsWindow* pwin, const std::string& nm, const std::string& v,
00212 bool pwinTakesOwnership = false);
00213
00214 void set(const std::string& v);
00215
00216 std::string get() const;
00217
00218 virtual std::string getName() const;
00219
00220 virtual void fromString(const std::string& s);
00221
00222 virtual std::string toString() const;
00223
00224
00225 virtual void adjust(int val);
00226
00227 virtual bool isDisabled() const { return disabled; }
00228
00229 void setDisabled(bool v) { if (v != disabled) { disabled = v; isChanged = true; } }
00230 };
00231
00232
00233
00234
00235
00236
00237
00238
00239 #endif // GUI_PREFSWINDOW_H_DEFINED