00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00021
00022 #ifndef WIN_CONTROLS_H
00023 #define WIN_CONTROLS_H
00024
00025 #include <windows.h>
00026 #include <commctrl.h>
00027
00028 namespace FemViewer {
00029 namespace Win
00030 {
00031
00032 enum { MAX_INDEX = 30000 };
00033
00034
00036
00038 class ControlBase
00039 {
00040 public:
00041
00042 ControlBase() : handle(0), parent(0), id(0), fontHandle(0) {}
00043 ControlBase(HWND parent, int id, bool visible=true) : handle(GetDlgItem(parent, id)) { if(!visible) disable(); }
00044 ~ControlBase() { if(fontHandle) DeleteObject(fontHandle);
00045 fontHandle = 0; }
00046
00047
00048 HWND getHandle() const { return handle; }
00049 HWND getParentHandle() const { return parent; }
00050
00051
00052 void set(HWND parent, int id, bool visible=true) { this->parent = parent;
00053 this->id = id;
00054 handle = GetDlgItem(parent, id);
00055 if(!visible) disable(); }
00056
00057
00058 void show() { ShowWindow(handle, SW_SHOW); }
00059 void hide() { ShowWindow(handle, SW_HIDE); }
00060
00061
00062 void setFocus() { SetFocus(handle); }
00063
00064
00065 void enable() { EnableWindow(handle, true); }
00066 void disable() { EnableWindow(handle, false); }
00067 bool isVisible() const { return (IsWindowVisible(handle) != 0); }
00068
00069
00070 void setFont(char* fontName, int size, bool bold=false, bool italic=false, bool underline=false, unsigned char charSet=DEFAULT_CHARSET)
00071 { HDC hdc = GetDC(handle);
00072 LOGFONT logFont;
00073 HFONT oldFont = (HFONT)SendMessage(handle, WM_GETFONT, 0 , 0);
00074 GetObject(oldFont, sizeof(LOGFONT), &logFont);
00075 strncpy(logFont.lfFaceName, fontName, LF_FACESIZE);
00076 logFont.lfHeight = -MulDiv(size, GetDeviceCaps(hdc, LOGPIXELSY), 72);
00077 ReleaseDC(handle, hdc);
00078 if(bold) logFont.lfWeight = FW_BOLD;
00079 else logFont.lfWeight = FW_NORMAL;
00080 logFont.lfItalic = italic;
00081 logFont.lfUnderline = underline;
00082 logFont.lfCharSet = charSet;
00083
00084 if(fontHandle) DeleteObject(fontHandle);
00085 fontHandle = CreateFontIndirect(&logFont);
00086 SendMessage(handle, WM_SETFONT, (WPARAM)fontHandle, MAKELPARAM(1, 0));
00087 }
00088
00089
00090 protected:
00091 HWND handle;
00092 HWND parent;
00093 int id;
00094 HFONT fontHandle;
00095 };
00096
00097
00098
00100
00102 class Button : public ControlBase
00103 {
00104 public:
00105
00106 Button() : ControlBase() {}
00107 Button(HWND parent, int id, bool visible=true) : ControlBase(parent, id, visible) {}
00108 ~Button() {}
00109
00110
00111 void setText(const wchar_t* text) { SendMessage(handle, WM_SETTEXT, 0, (LPARAM)text); }
00112 };
00113
00114
00115
00117
00119 class CheckBox : public Button
00120 {
00121 public:
00122 CheckBox() : Button() {}
00123 CheckBox(HWND parent, int id, bool visible=true) : Button(parent, id, visible) {}
00124 ~CheckBox() {}
00125
00126 void check() { SendMessage(handle, BM_SETCHECK, (WPARAM)BST_CHECKED, 0); }
00127 void uncheck() { SendMessage(handle, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0); }
00128 bool isChecked() const { return (SendMessage(handle, BM_GETCHECK, 0, 0) == BST_CHECKED); }
00129 };
00130
00131
00132
00134
00136 class RadioButton : public Button
00137 {
00138 public:
00139 RadioButton() : Button() {}
00140 RadioButton(HWND parent, int id, bool visible=true) : Button(parent, id, visible) {}
00141 ~RadioButton() {}
00142
00143 void check() { SendMessage(handle, BM_SETCHECK, (WPARAM)BST_CHECKED, 0); }
00144 void uncheck() { SendMessage(handle, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0); }
00145 bool isChecked() const { return (SendMessage(handle, BM_GETCHECK, 0, 0) == BST_CHECKED); }
00146 };
00147
00148
00149
00151
00153 class TextBox: public ControlBase
00154 {
00155 public:
00156 TextBox() : ControlBase() {}
00157 TextBox(HWND parent, int id, bool visible=true) : ControlBase(parent, id, visible) {}
00158 ~TextBox() {}
00159
00160 void setText(const wchar_t* buf) { SendMessage(handle, WM_SETTEXT, 0, (LPARAM)buf); }
00161 void getText(wchar_t* buf, int len) const { SendMessage(handle, WM_GETTEXT, (WPARAM)len, (LPARAM)buf); }
00162 int getTextLength() const { return (int)SendMessage(handle, WM_GETTEXTLENGTH, 0, 0); }
00163 };
00164
00165
00166
00168
00170 class EditBox: public TextBox
00171 {
00172 public:
00173 EditBox() : TextBox(), maxLength(0) {}
00174 EditBox(HWND parent, int id, bool visible=true) : TextBox(parent, id, visible), maxLength((int)SendMessage(handle, EM_GETLIMITTEXT, 0, 0)) {}
00175 ~EditBox() {}
00176
00177
00178 void set(HWND parent, int id, bool visible=true) { ControlBase::set(parent, id, visible);
00179 maxLength = (int)SendMessage(handle, EM_GETLIMITTEXT, 0, 0); }
00180
00181 void selectText() { SendMessage(handle, EM_SETSEL, 0, -1); }
00182 void unselectText() { SendMessage(handle, EM_SETSEL, -1, 0); }
00183
00184 int getLimitText() const { return maxLength; }
00185
00186 static bool isChanged(int code) { return (code==EN_CHANGE); }
00187
00188 protected:
00189 int maxLength;
00190 };
00191
00192
00193
00195
00197 class ListBox: public ControlBase
00198 {
00199 public:
00200 ListBox() : ControlBase(), listCount(0) {}
00201 ListBox(HWND parent, int id, bool visible=true) : ControlBase(parent, id, visible), listCount(0) {}
00202 ~ListBox() {}
00203
00204 int getCount() const { return listCount; }
00205
00206 void resetContent() { SendMessage(handle, LB_RESETCONTENT, 0, 0); }
00207
00208
00209
00210
00211 void addString(const wchar_t* str) { if(listCount >= MAX_INDEX) deleteString(0);
00212 int index = (int)SendMessage(handle, LB_ADDSTRING, 0, (LPARAM)str);
00213 if(index != LB_ERR) ++listCount;
00214 SendMessage(handle, LB_SETTOPINDEX, index, 0); }
00215
00216
00217
00218
00219 void insertString(const wchar_t* str, int index) { if(listCount >= MAX_INDEX) deleteString(listCount-1);
00220 index = (int)SendMessage(handle, LB_INSERTSTRING, index, (LPARAM)str);
00221 if(index != LB_ERR) ++listCount;
00222 SendMessage(handle, LB_SETTOPINDEX, index, 0); }
00223
00224 void deleteString(int index) { if(SendMessage(handle, LB_DELETESTRING, index, 0) != LB_ERR) --listCount; }
00225
00226 private:
00227 int listCount;
00228 };
00229
00230
00231
00233
00234
00235
00236
00237
00238
00240 class Trackbar: public ControlBase
00241 {
00242 public:
00243 Trackbar() : ControlBase() {}
00244 Trackbar(HWND parent, int id, bool visible=true) : ControlBase(parent, id, visible) {}
00245 ~Trackbar() {}
00246
00247
00248 void setRange(int first, int last) { SendMessage(handle, TBM_SETRANGE, (WPARAM)true, (LPARAM)MAKELONG(first, last)); }
00249 int getRangeMin() const { return (int)SendMessage(handle, TBM_GETRANGEMIN, 0, 0); }
00250 int getRangeMax() const { return (int)SendMessage(handle, TBM_GETRANGEMAX, 0, 0); }
00251
00252
00253
00254 void setTic(int pos) { if(pos <= getRangeMin()) return;
00255 if(pos >= getRangeMax()) return;
00256 SendMessage(handle, TBM_SETTIC, 0, (LPARAM)pos); }
00257
00258
00259 void setTicFreq(int freq) { SendMessage(handle, TBM_SETTICFREQ, (WPARAM)freq, 0); }
00260
00261
00262 int getPos() const { SendMessage(handle, TBM_GETPOS, 0, 0); }
00263 void setPos(int pos) { SendMessage(handle, TBM_SETPOS, (WPARAM)true, (LPARAM)pos); }
00264 };
00265
00266
00267
00269
00271 class ComboBox: public ControlBase
00272 {
00273 public:
00274 ComboBox() : ControlBase() {}
00275 ComboBox(HWND parent, int id, bool visible=true) : ControlBase(parent, id, visible) {}
00276 ~ComboBox() {}
00277
00278 int getCount() const { return (int)SendMessage(handle, CB_GETCOUNT, 0, 0); }
00279
00280 void resetContent() { SendMessage(handle, CB_RESETCONTENT, 0, 0); }
00281
00282
00283 void addString(const wchar_t* str) { SendMessage(handle, CB_ADDSTRING, 0, (LPARAM)str); }
00284
00285
00286 void insertString(const wchar_t* str, int index) { SendMessage(handle, CB_INSERTSTRING, index, (LPARAM)str); }
00287
00288
00289 void deleteString(int index) { SendMessage(handle, CB_DELETESTRING, index, 0); }
00290
00291
00292 int getCurrentSelection() { return (int)SendMessage(handle, CB_GETCURSEL, 0, 0); }
00293 void setCurrentSelection(int index) { SendMessage(handle, CB_SETCURSEL, index, 0); }
00294 };
00295
00296
00297
00298
00299
00300
00302
00303
00304
00305
00306
00308 class UpDownBox: public ControlBase
00309 {
00310 public:
00311 UpDownBox() : ControlBase() {}
00312 UpDownBox(HWND parent, int id, bool visible=true) : ControlBase(parent, id, visible) {}
00313 ~UpDownBox() {}
00314
00315
00316
00317 void setBuddy(HWND buddy) { SendMessage(handle, UDM_SETBUDDY, (WPARAM)buddy, 0); }
00318 HWND getBuddy() { return (HWND)SendMessage(handle, UDM_GETBUDDY, 0, 0); }
00319
00320
00321
00322 void setBase(int base) { SendMessage(handle, UDM_SETBASE, (WPARAM)base, 0); }
00323 int getBase() { return (int)SendMessage(handle, UDM_GETBASE, 0, 0); }
00324
00325
00326 void setRange(int low, int high) { SendMessage(handle, UDM_SETRANGE32, (WPARAM)low, (LPARAM)high); }
00327 void getRange(int* low, int* high) { SendMessage(handle, UDM_GETRANGE32, (WPARAM)low, (LPARAM)high); }
00328
00329
00330 void setAccel(unsigned int second, unsigned int increment) { UDACCEL accels[2];
00331 accels[0].nSec = 0; accels[0].nInc = 1;
00332 accels[1].nSec = second; accels[1].nInc = increment;
00333 SendMessage(handle, UDM_SETACCEL, (WPARAM)2, (LPARAM)&accels); }
00334 void getAccel(unsigned int* second, unsigned int* increment) { UDACCEL accels[2];
00335 SendMessage(handle, UDM_GETACCEL, (WPARAM)2, (LPARAM)&accels);
00336 *second = accels[1].nSec; *increment = accels[1].nInc; }
00337
00338
00339 void setPos(int position) { SendMessage(handle, UDM_SETPOS32, 0, (LPARAM)position); }
00340 int getPos() { return (int)SendMessage(handle, UDM_GETPOS32, 0, 0); }
00341 };
00342
00343 }
00344 }
00345
00346 #endif