Main Page | Namespaces | Classes | Compounds | Files | Compound Members | Related TextBox.cpp00001 // 00002 // SYNTOPIA. See http://Syntopia.sourceforge.net for details and documentation. 00003 // 00004 // Author of this file: Mikael Hvidtfeldt Christensen (mikaelc@users.sourceforge.net) 00005 // 00006 00007 00008 00009 #include "TextBox.h" 00010 #include <iomanip> 00011 #include <iostream> 00012 00013 namespace GUIToolkit { 00014 00015 void TextBox::PutInWinForm(WinForm * WF, int x0, int x1, int y0, int y1) { 00016 ID = (HMENU)GUIControl::GUIInstance->Register(this); 00017 topWF = WF; 00018 00019 00020 if (myTextBoxType == singleLine) { 00021 00022 hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", 00023 WS_CHILD | WS_VISIBLE , 00024 x0, x1, y0, y1, WF->hwnd, ID, GetModuleHandle(NULL), NULL); 00025 } else { 00026 hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", 00027 WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 00028 x0, x1, y0, y1, WF->hwnd, ID, GetModuleHandle(NULL), NULL); 00029 } 00030 00031 00032 if(hEdit == NULL) { 00033 MessageBox(WF->hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR); 00034 // Todo: Should Throw Error 00035 } 00036 00037 hfDefault = (HFONT) GetStockObject(DEFAULT_GUI_FONT); 00038 SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0)); 00039 } 00040 00041 void TextBox::setText(std::string s) { 00042 SetDlgItemText(topWF->hwnd, (int) ID, s.c_str()); 00043 UpdateWindow(hEdit); 00044 } 00045 00046 void TextBox::setText(float f) { 00047 char Answer[20]; 00048 sprintf(Answer, "%5.4f", f); 00049 setText(std::string(Answer)); 00050 } 00051 00052 std::string* TextBox::getText() { 00053 std::string * s; 00054 int len = GetWindowTextLength(GetDlgItem(topWF->hwnd, (int) ID)); 00055 if(len > 0) 00056 { 00057 //int i; 00058 char* buf; 00059 00060 buf = (char*)GlobalAlloc(GPTR, len + 1); 00061 GetDlgItemText(topWF->hwnd, (int) ID, buf, len + 1); 00062 00063 //... do stuff with text ... 00064 s = new std::string(buf); 00065 00066 GlobalFree((HANDLE)buf); 00067 } else 00068 { s=new std::string(""); } 00069 return s; 00070 } 00071 00072 TextBox::TextBox() { 00073 myTextBoxType = multiLine; 00074 floatVal = false; fmax = fmin = 0; checkLimits = false; 00075 } 00076 00077 TextBox::TextBox(TextBoxType myType) { 00078 myTextBoxType = myType; 00079 floatVal = false; fmax = fmin = 0; checkLimits = false; 00080 } 00081 00082 void TextBox::setFloatLimit(float from, float to) { 00083 fmax = from; 00084 fmin = to; 00085 floatVal = true; 00086 checkLimits = true; 00087 } 00088 00089 bool TextBox::validateFloat() { 00090 // check if we got a floating point number 00091 std::string data; 00092 data=*getText(); // DANGEROUS 00093 std::istringstream iss(data); 00094 float val = 0; 00095 00096 if(iss >> val) { 00097 std::cout << std::fixed 00098 << std::setprecision(2) 00099 << val; 00100 return true; 00101 } 00102 else 00103 { 00104 std::cout << "(invalid data)"; 00105 return false; 00106 } 00107 } 00108 00109 float TextBox::getFloatVal() { 00110 // check if we got a floating point number 00111 std::string data; 00112 data=*getText(); // DANGEROUS 00113 std::istringstream iss(data); 00114 float val = 0; 00115 00116 if(iss >> val) 00117 return val; 00118 else 00119 return fmin; 00120 } 00121 00122 void TextBox::HandleEvents(event e) 00123 { 00124 if (e.msg == WM_COMMAND) 00125 { 00126 if (HIWORD(e.wParam)==EN_KILLFOCUS) { 00127 // text changed 00128 if (floatVal) { 00129 if (validateFloat()) { 00130 if (myEventHandler !=0) myEventHandler->HandleEvents(e); 00131 } else { 00132 setText(fmin); 00133 } 00134 } else 00135 if (myEventHandler !=0) myEventHandler->HandleEvents(e); 00136 00137 } 00138 } 00139 }; 00140 00141 TextBox::~TextBox() { 00142 } 00143 00144 void TextBox::show() { 00145 ShowWindow( hEdit,SW_SHOW ); 00146 }; 00147 void TextBox::hide() { 00148 ShowWindow( hEdit,SW_HIDE ); 00149 }; 00150 00151 }; // end of namespace Docs made by Doxygen. Email: Mikael Christensen |