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 #ifndef __LISTBOX__
00035 #define __LISTBOX__
00036
00037 #include <vector>
00038
00039 #ifdef __BORLANDC__
00040 #pragma option push -b
00041 #pragma option push -a4
00042 #endif
00043
00044 #ifdef FG_NAMESPACE
00045 namespace fgl {
00046 #endif
00047
00052 class listbox : public FGSlider
00053 {
00054 static void lbCall(FGControl *T);
00055 void corecture(void);
00056
00057 protected:
00058 int cursor;
00059 int slider;
00060 int drop_down;
00061 unsigned color_bg0, color_bg1, color_fg0, color_fg1;
00062 int xx, yy,
00063 curr,
00064 count,
00065 fpol,
00066 line,
00067 onew, oneh;
00068
00069 void Cursor(int);
00071 int AdjustCursor(int item);
00073 int Enabled(void) { if (curr<0 || count<=0) return 0; return 1;}
00074 listbox(int xs, int ys, int w, int h, int DropDown, FGWindow *wind);
00075
00076 virtual void draw(void);
00077 virtual void DrawItem(int , int , int , int ) {}
00078 virtual void WheelSpin(int direction)
00079 {
00080 if (direction<0) Up();
00081 else if (direction>0) Down();
00082 }
00083
00084 public:
00092 void SetColors(int a, int b, int c, int d)
00093 {
00094 color_bg0 = a;
00095 color_fg0 = b;
00096 color_bg1 = c;
00097 color_fg1 = d;
00098 }
00101 void Draw(int i=1);
00103 void ShowCursor(void);
00105 void HideCursor(void);
00107 void Up(void);
00109 void Down(void);
00111 void Begin(void);
00113 void End(void);
00115 void SetSize(int);
00117 int GetSize(void) const { return count; };
00119 void SetIndex(int);
00121 int GetIndex(void) const { return curr; };
00123 void SetIndexRel(int);
00126 int GetCurrent(void) const { return GetIndex(); };
00129 void SetToItemRel(int a) { SetIndexRel(a); }
00132 void Resize(int a) { SetSize(GetSize()+a); }
00135 void SetToItem(int a) { SetIndex(a); }
00137 void RedrawItem(void)
00138 {
00139 ShowCursor();
00140 }
00141 int Test(int, int);
00142 int DoListBox(int key);
00143 virtual const int ToInteger() const { return GetIndex(); }
00144 };
00145
00151 template <typename T> class FGListBoxTemplate : public listbox
00152 {
00153 protected:
00154
00155 typedef typename std::vector<T>::iterator Iter;
00156 std::vector<T> items;
00157 int update;
00158 FGListBoxCallBack show;
00159 void* user_data;
00160
00161 virtual void DrawItem(int x, int y, int index, int flag) { }
00162 public:
00166 FGListBoxTemplate(
00168 int xs,
00170 int ys,
00172 int w,
00174 int h,
00176 int DropDown,
00178 FGWindow *wind,
00180 FGListBoxCallBack show_cb,
00182 void* user_ptr = 0)
00183 : listbox(xs,ys,w,h,DropDown,wind)
00184 {
00185 update = 0;
00186 show = show_cb;
00187 user_data = user_ptr;
00188 }
00190 void DisableUpdate(void) { update = 0; }
00192 void Update(void)
00193 {
00194 update = 1;
00195 Draw(1);
00196 }
00198 void erase(int i)
00199 {
00200 items.erase(items.begin()+i);
00201 count--;
00202 if (update) Draw(0);
00203 }
00205 void clear(void)
00206 {
00207 count = fpol = line = curr = 0;
00208 Draw(0);
00209 update = 0;
00210 items.clear();
00211 }
00213 void insert(T s)
00214 {
00215 items.push_back(s);
00216 count++;
00217 if (update) Draw(0);
00218 }
00220 void insert(int i, T s)
00221 {
00222 items.insert(items.begin()+i,s);
00223 count++;
00224 if (update) Draw(0);
00225 }
00227 void replace(int i, T s)
00228 {
00229 Iter ti = items.begin();
00230 ti[i] = T(s);
00231 }
00232 const T& item(int i) const { return items[i]; }
00233 };
00234
00239 struct id_string
00240 {
00241 char str[64];
00242 id_string()
00243 {
00244 *str=0;
00245 }
00246 id_string(const char *s)
00247 {
00248 strncpy(str,s,63);
00249 str[63]=0;
00250 }
00251 };
00252
00257 class FGListBox : public FGListBoxTemplate<struct id_string>
00258 {
00259 typedef std::vector<id_string>::iterator Iter;
00260 friend class FWindow;
00265 virtual void DrawItem(int x, int y, int index, int flag)
00266 {
00267 if (index>=count) return;
00268 Iter i=items.begin();
00269
00270 if (user_data)
00271 {
00272 if (show)
00273 show(flag, owner, x, y, user_data, index);
00274 }
00275 else
00276 {
00277 if (show==0)
00278 owner->WindowText(x+4, y, i[index].str, flag?color_fg1:color_fg0,flag?color_bg1:color_bg0);
00279 else
00280 show(flag, owner, x, y, i[index].str, index);
00281 }
00282 }
00283 ~FGListBox() {}
00284 public:
00296 FGListBox(int xs, int ys, int w, int h, int DropDown, FGWindow *wind, FGListBoxCallBack show_cb=0, void* user_data = 0)
00297 : FGListBoxTemplate<id_string>(xs, ys, w, h, DropDown, wind, show_cb, user_data)
00298 {
00299 }
00300 const char *GetString(int i) const { return item(i).str; }
00301 virtual const char* ToString() const { return GetString(GetIndex()); }
00302 };
00303
00304 #ifdef __BORLANDC__
00305 #pragma option pop
00306 #pragma option pop
00307 #endif
00308
00309 #ifdef FG_NAMESPACE
00310 }
00311 #endif
00312
00313 #endif
00314