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
30 using namespace std; // For STL stuff
33 // Text edit class implementation
36 TextEdit::TextEdit(uint32 x/*= 0*/, uint32 y/*= 0*/, uint32 w/*= 0*/, uint32 h/*= 0*/,
37 string s/*= ""*/, Element * parent/*= NULL*/):
38 Element(x, y, w, h, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x40, 0x40, 0xFF, parent),
39 activated(false), clicked(false), inside(false),
40 img(NULL), text(s), caretPos(0), scrollPos(0),
41 activatedSave(false), clickedSave(false), insideSave(false),
42 caretPosSave(0), scrollPosSave(0)
45 extents.h = GetFontHeight();
47 // Setup hardwired colors...
49 uint8 * c = (uint8 *)&hiliteColor;
50 c[0] = 0xFF, c[1] = 0x80, c[2] = 0x00, c[3] = 0xFF;
51 c = (uint8 *)&cursorColor;
52 c[0] = 0x40, c[1] = 0xFF, c[2] = 0x60, c[3] = 0xFF;
54 // Create the text edit surface here...
56 img = SDL_CreateRGBSurface(SDL_SWSURFACE, extents.w, extents.h, 32,
57 MASK_R, MASK_G, MASK_B, MASK_A);
59 Draw(); // Can we do this in the constructor??? Mebbe.
68 //Set different filters depending on type passed in on construction, e.g., filename, amount, etc...?
69 void TextEdit::HandleKey(SDLKey key)
75 SDLMod keyMod = SDL_GetModState();
77 if ((key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_0 && key <= SDLK_9)
78 || key == SDLK_PERIOD || key == SDLK_SLASH || key == SDLK_SPACE)
80 uint8 chr = (uint8)key;
82 // Handle shift key as well...
83 if (keyMod & KMOD_SHIFT)
85 if (key >= SDLK_a && key <= SDLK_z)
86 chr &= 0xDF; // Set to upper case
89 text.insert(scrollPos + caretPos, 1, chr);
91 // If we hit the edge, then scroll; else advance the caret
92 if ((GetFontWidth() * caretPos) > (extents.w - GetFontWidth()))
97 else if (key == SDLK_BACKSPACE)
99 // If there's something to delete, go ahead and delete it
100 if ((scrollPos + caretPos) > 0)
102 text.erase(scrollPos + caretPos - 1, 1);
104 // Scroll the cursor to the left if possible, otherwise move the scroll position
111 else if (key == SDLK_DELETE)
113 if ((scrollPos + caretPos) < text.length())
114 text.erase(scrollPos + caretPos, 1);
116 else if (key == SDLK_LEFT)
120 else if (scrollPos > 0)
123 else if (key == SDLK_RIGHT)
125 if ((scrollPos + caretPos) < text.length())
127 if ((GetFontWidth() * caretPos) > (extents.w - GetFontWidth()))
133 else if (key == SDLK_RETURN)
135 clicked = activated = false;
138 CheckStateAndRedrawIfNeeded();
141 void TextEdit::HandleMouseMove(uint32 x, uint32 y)
143 SaveStateVariables();
144 inside = Inside(x, y);
145 CheckStateAndRedrawIfNeeded();
148 void TextEdit::HandleMouseButton(uint32 x, uint32 y, bool mouseDown)
150 SaveStateVariables();
152 //Not sure that this is right way to handle this...
153 //Should set the cursor position based on where in the text box it was clicked...
159 if (clicked && !mouseDown)
161 clicked = false, activated = true;
163 // Send a message to our parent widget (if any) that we're activated
165 parent->Notify(this);
169 clicked = activated = false;
171 CheckStateAndRedrawIfNeeded();
174 void TextEdit::Draw(void)
177 return; // Bail out if no surface was created...
179 SDL_Rect rect = GetScreenCoords();
181 // Now, draw the appropriate text state!
190 rect2.w = extents.w - 2;
191 rect2.h = extents.h - 2;
193 SDL_FillRect(img, NULL, hiliteColor);
194 SDL_FillRect(img, &rect2, bgColor);
197 SDL_FillRect(img, NULL, bgColor);
200 SDL_FillRect(img, NULL, bgColor);//Make a different color here, so we're clear we're editing...
202 //Should also draw different text color depending on whether or not we're activated...
204 DrawStringTrans(img, 0, 0, fgColor, text.c_str() + scrollPos);
206 DrawStringTrans(img, 0, 0, fgColor, text.c_str());
208 // Draw the cursor, if any
213 rectCursor.x = caretPos * GetFontWidth();
216 rectCursor.h = GetFontHeight();
218 SDL_FillRect(img, &rectCursor, cursorColor);
221 SDL_BlitSurface(img, NULL, screen, &rect); // This handles alpha blending too! :-D
223 needToRefreshScreen = true;
226 void TextEdit::Notify(Element *)
230 string TextEdit::GetText(void)
235 void TextEdit::SaveStateVariables(void)
237 activatedSave = activated;
238 clickedSave = clicked;
240 caretPosSave = caretPos;
241 scrollPosSave = scrollPos;
242 lengthSave = text.length();
245 void TextEdit::CheckStateAndRedrawIfNeeded(void)
247 // Check to see if any of our state variables changed since we last saved them...
248 if (activated != activatedSave || clicked != clickedSave || inside != insideSave
249 || caretPos != caretPosSave || scrollPos != scrollPosSave
250 || text.length() != lengthSave)