4 // Graphical User Interface button class
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 02/17/2006 Created this file
12 // JLH 03/01/2006 Added basic editing functionality
16 #include "guimisc.h" // Various support functions
18 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
19 #define MASK_R 0xFF000000
20 #define MASK_G 0x00FF0000
21 #define MASK_B 0x0000FF00
22 #define MASK_A 0x000000FF
24 #define MASK_R 0x000000FF
25 #define MASK_G 0x0000FF00
26 #define MASK_B 0x00FF0000
27 #define MASK_A 0xFF000000
31 // Text edit class implementation
34 TextEdit::TextEdit(uint32 x/*= 0*/, uint32 y/*= 0*/, uint32 w/*= 0*/, uint32 h/*= 0*/,
35 std::string s/*= ""*/, Element * parent/*= NULL*/):
36 Element(x, y, w, h, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x40, 0x40, 0xFF, parent),
37 activated(false), clicked(false), inside(false),
38 img(NULL), text(s), caretPos(0), scrollPos(0),
39 activatedSave(false), clickedSave(false), insideSave(false),
40 caretPosSave(0), scrollPosSave(0)
43 extents.h = GetFontHeight();
45 // Setup hardwired colors...
47 uint8 * c = (uint8 *)&hiliteColor;
48 c[0] = 0xFF, c[1] = 0x80, c[2] = 0x00, c[3] = 0xFF;
49 c = (uint8 *)&cursorColor;
50 c[0] = 0x40, c[1] = 0xFF, c[2] = 0x60, c[3] = 0xFF;
52 // Create the text edit surface here...
54 img = SDL_CreateRGBSurface(SDL_SWSURFACE, extents.w, extents.h, 32,
55 MASK_R, MASK_G, MASK_B, MASK_A);
57 Draw(); // Can we do this in the constructor??? Mebbe.
66 //Set different filters depending on type passed in on construction, e.g., filename, amount, etc...?
67 void TextEdit::HandleKey(SDLKey key)
73 SDLMod keyMod = SDL_GetModState();
75 if ((key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_0 && key <= SDLK_9)
76 || key == SDLK_PERIOD || key == SDLK_SLASH || key == SDLK_SPACE)
78 uint8 chr = (uint8)key;
80 // Handle shift key as well...
81 if (keyMod & KMOD_SHIFT)
83 if (key >= SDLK_a && key <= SDLK_z)
84 chr &= 0xDF; // Set to upper case
87 text.insert(scrollPos + caretPos, 1, chr);
89 // If we hit the edge, then scroll; else advance the caret
90 if ((GetFontWidth() * caretPos) > (extents.w - GetFontWidth()))
95 else if (key == SDLK_BACKSPACE)
97 // If there's something to delete, go ahead and delete it
98 if ((scrollPos + caretPos) > 0)
100 text.erase(scrollPos + caretPos - 1, 1);
102 // Scroll the cursor to the left if possible, otherwise move the scroll position
109 else if (key == SDLK_DELETE)
111 if ((scrollPos + caretPos) < text.length())
112 text.erase(scrollPos + caretPos, 1);
114 else if (key == SDLK_LEFT)
118 else if (scrollPos > 0)
121 else if (key == SDLK_RIGHT)
123 if ((scrollPos + caretPos) < text.length())
125 if ((GetFontWidth() * caretPos) > (extents.w - GetFontWidth()))
131 else if (key == SDLK_RETURN)
133 clicked = activated = false;
136 CheckStateAndRedrawIfNeeded();
139 void TextEdit::HandleMouseMove(uint32 x, uint32 y)
141 SaveStateVariables();
142 inside = Inside(x, y);
143 CheckStateAndRedrawIfNeeded();
146 void TextEdit::HandleMouseButton(uint32 x, uint32 y, bool mouseDown)
148 SaveStateVariables();
150 //Not sure that this is right way to handle this...
151 //Should set the cursor position based on where in the text box it was clicked...
157 if (clicked && !mouseDown)
159 clicked = false, activated = true;
161 // Send a message to our parent widget (if any) that we're activated
163 parent->Notify(this);
167 clicked = activated = false;
169 CheckStateAndRedrawIfNeeded();
172 void TextEdit::Draw(void)
175 return; // Bail out if no surface was created...
177 SDL_Rect rect = GetScreenCoords();
179 // Now, draw the appropriate text state!
188 rect2.w = extents.w - 2;
189 rect2.h = extents.h - 2;
191 SDL_FillRect(img, NULL, hiliteColor);
192 SDL_FillRect(img, &rect2, bgColor);
195 SDL_FillRect(img, NULL, bgColor);
198 SDL_FillRect(img, NULL, bgColor);//Make a different color here, so we're clear we're editing...
200 //Should also draw different text color depending on whether or not we're activated...
202 DrawStringTrans(img, 0, 0, fgColor, text.c_str() + scrollPos);
204 DrawStringTrans(img, 0, 0, fgColor, text.c_str());
206 // Draw the cursor, if any
211 rectCursor.x = caretPos * GetFontWidth();
214 rectCursor.h = GetFontHeight();
216 SDL_FillRect(img, &rectCursor, cursorColor);
219 SDL_BlitSurface(img, NULL, screen, &rect); // This handles alpha blending too! :-D
221 needToRefreshScreen = true;
224 void TextEdit::Notify(Element *)
228 std::string TextEdit::GetText(void)
233 void TextEdit::SaveStateVariables(void)
235 activatedSave = activated;
236 clickedSave = clicked;
238 caretPosSave = caretPos;
239 scrollPosSave = scrollPos;
240 lengthSave = text.length();
243 void TextEdit::CheckStateAndRedrawIfNeeded(void)
245 // Check to see if any of our state variables changed since we last saved them...
246 if (activated != activatedSave || clicked != clickedSave || inside != insideSave
247 || caretPos != caretPosSave || scrollPos != scrollPosSave
248 || text.length() != lengthSave)