1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "GUI/TextEdit.h"
25 #include "Interface.h"
29 TextEdit::TextEdit(unsigned short maxLength
, unsigned short px
, unsigned short py
)
34 Buffer
= ( unsigned char * ) malloc( max
+ 1 );
40 ResetEventHandler( EditOnChange
);
41 ResetEventHandler( EditOnDone
);
42 ResetEventHandler( EditOnCancel
);
43 Color white
= {0xff, 0xff, 0xff, 0x00}, black
= {0x00, 0x00, 0x00, 0x00};
44 palette
= core
->CreatePalette( white
, black
);
47 TextEdit::~TextEdit(void)
49 Video
*video
= core
->GetVideoDriver();
50 gamedata
->FreePalette( palette
);
52 video
->FreeSprite( Back
);
53 video
->FreeSprite( Cursor
);
56 /** Draws the Control on the Output Display */
57 void TextEdit::Draw(unsigned short x
, unsigned short y
)
59 if (!Changed
&& !(Owner
->Flags
&WF_FLOAT
)) {
64 core
->GetVideoDriver()->BlitSprite( Back
, x
+ XPos
, y
+ YPos
, true );
70 //The aligning of textedit fields is done by absolute positioning (FontPosX, FontPosY)
72 font
->Print( Region( x
+ XPos
+ FontPosX
, y
+ YPos
+ FontPosY
, Width
, Height
), Buffer
,
73 palette
, IE_FONT_ALIGN_LEFT
| IE_FONT_ALIGN_TOP
,
74 true, NULL
, Cursor
, CurPos
);
76 font
->Print( Region( x
+ XPos
+ FontPosX
, y
+ YPos
+ FontPosY
, Width
, Height
), Buffer
,
77 palette
, IE_FONT_ALIGN_LEFT
| IE_FONT_ALIGN_TOP
, true );
82 void TextEdit::SetFont(Font
* f
)
89 printMessage("TextEdit","Invalid font set!\n", LIGHT_RED
);
92 Font
*TextEdit::GetFont() { return font
; }
95 void TextEdit::SetCursor(Sprite2D
* cur
)
97 core
->GetVideoDriver()->FreeSprite( Cursor
);
104 /** Set BackGround */
105 void TextEdit::SetBackGround(Sprite2D
* back
)
107 //if 'back' is NULL then no BackGround will be drawn
109 core
->GetVideoDriver()->FreeSprite(Back
);
114 /** Key Press Event */
115 void TextEdit::OnKeyPress(unsigned char Key
, unsigned short /*Mod*/)
118 if (Value
&& ( (Key
<'0') || (Key
>'9') ) )
122 int len
= ( int ) strlen( ( char* ) Buffer
);
124 for (int i
= len
; i
> CurPos
; i
--) {
125 Buffer
[i
] = Buffer
[i
- 1];
127 Buffer
[CurPos
] = Key
;
131 RunEventHandler( EditOnChange
);
134 /** Special Key Press */
135 void TextEdit::OnSpecialKeyPress(unsigned char Key
)
146 CurPos
= (ieWord
) strlen( (char * ) Buffer
);
153 len
= ( int ) strlen( ( char * ) Buffer
);
159 len
= ( int ) strlen( ( char * ) Buffer
);
161 for (int i
= CurPos
; i
< len
; i
++) {
162 Buffer
[i
] = Buffer
[i
+ 1];
168 int len
= ( int ) strlen( ( char* ) Buffer
);
169 for (int i
= CurPos
; i
< len
; i
++) {
170 Buffer
[i
- 1] = Buffer
[i
];
177 RunEventHandler( EditOnDone
);
181 RunEventHandler( EditOnChange
);
184 /** Sets the Text of the current control */
185 int TextEdit::SetText(const char* string
, int /*pos*/)
187 strncpy( ( char * ) Buffer
, string
, max
);
189 CurPos
= (ieWord
) strlen((char *) Buffer
);
196 void TextEdit::SetBufferLength(ieWord buflen
)
200 Buffer
= (unsigned char *) realloc(Buffer
, buflen
+1);
206 /** Simply returns the pointer to the text, don't modify it! */
207 const char* TextEdit::QueryText()
209 return ( const char * ) Buffer
;
212 bool TextEdit::SetEvent(int eventType
, EventHandler handler
)
217 case IE_GUI_EDIT_ON_CHANGE
:
218 EditOnChange
= handler
;
220 case IE_GUI_EDIT_ON_DONE
:
221 EditOnDone
= handler
;
223 case IE_GUI_EDIT_ON_CANCEL
:
224 EditOnCancel
= handler
;