00001
00002
00003
00004 #ifndef OglGui_TextField_h
00005 #define OglGui_TextField_h
00006
00007 #ifndef OglGui_StaticText_h
00008 #include "OglGui/StaticText.h"
00009 #endif
00010
00011 #ifndef OglGui_TextFieldListener_h
00012 #include "OglGui/TextFieldListener.h"
00013 #endif
00014
00015
00016 namespace OglGui
00017 {
00018
00045 class TextField : public StaticText
00046 {
00047 public:
00048
00049 TextField(Window* parent, int w, int h, strconst text) :
00050 StaticText(parent, w, h, text)
00051 {
00052 Init(text);
00053 }
00054
00055 TextField(Window* parent, int x, int y, int w, int h, strconst text) :
00056 StaticText(parent, x, y, w, h, text)
00057 {
00058 Init(text);
00059 }
00060
00061 void SetTextFieldListener(TextFieldListener* listener, void* userData=0)
00062 {
00063 mListener = listener;
00064 mListenerData = userData;
00065 }
00066
00067 virtual void DisplayFunc()
00068 {
00069 Window::DisplayFunc();
00070
00071 OGC oldOGC;
00072 OGCSave(&oldOGC);
00073
00074 DrawTextWithCursor();
00075
00076 OGCRestore(&oldOGC);
00077 }
00078
00079 virtual void DrawTextWithCursor()
00080 {
00081 glColor3d(0,0,0);
00082
00083
00084 int left,right;
00085 GetSelectionLeftRight(left, right);
00086 double posstart[4];
00087 double pos[4];
00088 glRasterPos2d(0, 0);
00089 double xLeft,xRight;
00090 glGetDoublev(GL_CURRENT_RASTER_POSITION, posstart);
00091
00092 glRasterPos2d(4, 6);
00093
00094 oglSys.PrintFont(1, (char*)mText.substr(0,left).c_str());
00095 glGetDoublev(GL_CURRENT_RASTER_POSITION, pos);
00096 xLeft = pos[0] - posstart[0];
00097
00098 oglSys.PrintFont(1, (char*)mText.substr(left,right-left).c_str());
00099 glGetDoublev(GL_CURRENT_RASTER_POSITION, pos);
00100 xRight = pos[0] - posstart[0];
00101
00102 oglSys.PrintFont(1, (char*) mText.substr(right).c_str());
00103
00104
00105 if (xLeft==xRight)
00106 {
00107 xLeft--;
00108 xRight++;
00109 }
00110 glColor3d(1,1,1);
00111 glEnable(GL_BLEND);
00112 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
00113 glBegin(GL_QUADS);
00114 glVertex2d(xLeft, 1);
00115 glVertex2d(xLeft, mOglWnd->height - 1);
00116 glVertex2d(xRight, mOglWnd->height - 1);
00117 glVertex2d(xRight, 1);
00118 glEnd();
00119 glDisable(GL_BLEND);
00120 }
00121
00122 virtual void KeyboardFunc(int c, int state)
00123 {
00124
00125
00126
00127
00128
00129
00130 Window::KeyboardFunc(c, state);
00131 std::string oldText = mText;
00132 bool shift = (state & oglShift) != 0;
00133
00134 if (c == oglLEFT)
00135 {
00136 if (mCaretPosition > 0)
00137 mCaretPosition--;
00138 if (!shift)
00139 mMarkerPosition = mCaretPosition;
00140 }
00141 if (c == oglRIGHT)
00142 {
00143 if (mCaretPosition < mText.size())
00144 mCaretPosition++;
00145 if (!shift)
00146 mMarkerPosition = mCaretPosition;
00147 }
00148 if (c == oglHOME)
00149 {
00150 mCaretPosition = 0;
00151 if (!shift)
00152 mMarkerPosition = mCaretPosition;
00153 }
00154 if (c == oglEND)
00155 {
00156 mCaretPosition = mText.size();
00157 if (!shift)
00158 mMarkerPosition = mCaretPosition;
00159 }
00160
00161 if (c == 0x08)
00162 {
00163 if (mCaretPosition != mMarkerPosition)
00164 {
00165 DeleteSelection();
00166 }
00167 else
00168 if (mCaretPosition > 0)
00169 {
00170 mCaretPosition--;
00171 mMarkerPosition = mCaretPosition;
00172 mText = mText.erase(mCaretPosition, 1);
00173 }
00174
00175 }
00176 if (c == oglDELETE)
00177 {
00178 if (mCaretPosition != mMarkerPosition)
00179 {
00180 DeleteSelection();
00181 }
00182 else
00183 if (mCaretPosition < mText.size()-1)
00184 {
00185 mText = mText.erase(mCaretPosition, 1);
00186 }
00187
00188 }
00189 unsigned char ch = (unsigned char) c;
00190 if (isalnum(ch) || ch == ' ' || ispunct(ch))
00191 {
00192 if (mCaretPosition != mMarkerPosition)
00193 DeleteSelection();
00194 mText.insert(mCaretPosition, 1, ch);
00195 mCaretPosition++;
00196 mMarkerPosition = mCaretPosition;
00197 }
00198 if (oldText.compare(mText) && mListener)
00199 mListener->TextFieldChangedEvent(this, mListenerData);
00200
00201 }
00202
00203 void ResetCaret()
00204 {
00205 mCaretPosition = mMarkerPosition = mText.size();
00206 }
00207 void SetText(strconst text)
00208 {
00209 StaticText::SetText(text);
00210 ResetCaret();
00211 }
00212
00213 protected:
00214
00215 void DeleteSelection()
00216 {
00217 int left,right;
00218 GetSelectionLeftRight(left, right);
00219 mText = mText.erase(left, right - left);
00220 mCaretPosition = mMarkerPosition = left;
00221 }
00222
00223 void GetSelectionLeftRight(int& left, int& right)
00224 {
00225 if (mMarkerPosition < mCaretPosition)
00226 {
00227 left = mMarkerPosition;
00228 right = mCaretPosition;
00229 }
00230 else
00231 {
00232 left = mCaretPosition;
00233 right = mMarkerPosition;
00234 }
00235 }
00236
00237 void Init(strconst text)
00238 {
00239 SetBorderType( BEV_RIDGE );
00240
00241 SetDisableGlobalKeyListener( true );
00242 SetDisableOGLViewKeys( true );
00243 SetDisableOGLViewMouse( true );
00244
00245 mText = text;
00246 mListener = 0;
00247 mListenerData = 0;
00248
00249 mOglWnd->bgCol = oglWHITE;
00250 mCaretPosition = mMarkerPosition = text.size();
00251 }
00252
00253 TextFieldListener* mListener;
00254 void* mListenerData;
00255
00256 int mCaretPosition;
00257 int mMarkerPosition;
00258 };
00259
00260 }
00261
00262 #endif