13 #include "../../gfx.h"
15 #if GFX_USE_GWIN && GWIN_NEED_TEXTEDIT
17 #include "gwin_class.h"
21 #define TEXT_PADDING_LEFT 4
22 #define CURSOR_PADDING_LEFT 0
23 #define CURSOR_EXTRA_HEIGHT 1
26 #define gh2obj ((GTexteditObject *)gh)
27 #define gw2obj ((GTexteditObject *)gw)
29 static void TextEditRemoveChar(
GHandle gh) {
35 sz = strlen(gh2obj->w.text);
36 pos = gh2obj->cursorPos;
38 pos = gh2obj->cursorPos = sz;
39 q = gh2obj->w.text+pos;
41 if (!(gh->
flags & GWIN_FLG_ALLOCTXT)) {
46 memcpy(p, gh2obj->w.text, pos);
47 memcpy(p+pos, q+1, sz-pos);
48 gh->
flags |= GWIN_FLG_ALLOCTXT;
51 memcpy((
char *)q, q+1, sz-pos);
52 if (!(p =
gfxRealloc((
char *)gh2obj->w.text, sz+1, sz)))
58 static gBool TextEditAddChars(
GHandle gh,
unsigned cnt) {
65 sz = strlen(gh2obj->w.text)+1;
66 pos = gh2obj->cursorPos;
68 pos = gh2obj->cursorPos = sz-1;
70 if (!(gh->
flags & GWIN_FLG_ALLOCTXT)) {
73 memcpy(p, gh2obj->w.text, pos);
74 memcpy(p+pos+cnt, gh2obj->w.text+pos, sz-pos);
75 gh->
flags |= GWIN_FLG_ALLOCTXT;
78 if (!(p =
gfxRealloc((
char *)gh2obj->w.text, sz, sz+cnt)))
101 gw2obj->cursorPos = strlen(gw->
text);
110 gw2obj->cursorPos = i-1;
116 #if (GFX_USE_GINPUT && GINPUT_NEED_KEYBOARD) || GWIN_NEED_KEYBOARD
117 static void TextEditKeyboard(
GWidgetObject* gw, GEventKeyboard* pke) {
119 if ((pke->keystate & GKEYSTATE_KEYUP) || !pke->bytecount)
123 if (pke->keystate & GKEYSTATE_SPECIAL) {
137 sizeof(GTexteditObject),
143 #if GINPUT_NEED_MOUSE
150 #if (GFX_USE_GINPUT && GINPUT_NEED_KEYBOARD) || GWIN_NEED_KEYBOARD
155 #if GINPUT_NEED_TOGGLE
177 if (!(wt = (GTexteditObject*)_gwidgetCreate(g, &wt->w, pInit, &texteditVMT)))
180 wt->maxSize = maxSize;
183 wt->cursorPos = strlen(wt->w.text);
190 #if (GFX_USE_GINPUT && GINPUT_NEED_KEYBOARD) || GWIN_NEED_KEYBOARD
199 sz = strlen(gh2obj->w.text);
200 if (gh2obj->cursorPos > sz)
201 gh2obj->cursorPos = sz;
206 if (!gh2obj->cursorPos)
211 if (!gh2obj->w.text[gh2obj->cursorPos])
216 if (!gh2obj->cursorPos)
218 gh2obj->cursorPos = 0;
221 if (!gh2obj->w.text[gh2obj->cursorPos])
223 gh2obj->cursorPos = sz;
234 if (gh->
vmt != (
gwinVMT*)&texteditVMT || !key || !len)
238 switch((gU8)key[0]) {
241 if (!gh2obj->cursorPos)
244 TextEditRemoveChar(gh);
254 if (!gh2obj->w.text[gh2obj->cursorPos])
256 TextEditRemoveChar(gh);
260 if ((gU8)key[0] < GKEY_SPACE)
264 if (gh2obj->maxSize && strlen(gh2obj->w.text)+len > gh2obj->maxSize)
268 if (TextEditAddChars(gh, len)) {
270 memcpy((
char *)gh2obj->w.text+gh2obj->cursorPos, key, len);
271 gh2obj->cursorPos += len;
293 if ((gw->
g.
flags & GWIN_FLG_SYSENABLED))
300 if (!gw2obj->cursorPos)
303 for(cpos = gw2obj->cursorPos; ; p++, cpos--) {
305 if (tpos < gw->g.width-(TEXT_PADDING_LEFT+CURSOR_PADDING_LEFT))
311 #if TEXT_PADDING_LEFT
330 _gwidgetDrawFocusRect(gw, 0, 0, gw->g.
width, gw->g.
height);
gCoord gdispGetFontMetric(gFont font, gFontmetric metric)
Get a metric of a font.
void gdispGFillArea(GDisplay *g, gCoord x, gCoord y, gCoord cx, gCoord cy, gColor color)
Fill an area with a color.
void gdispGDrawLine(GDisplay *g, gCoord x0, gCoord y0, gCoord x1, gCoord y1, gColor color)
Draw a line.
void gdispGFillStringBox(GDisplay *g, gCoord x, gCoord y, gCoord cx, gCoord cy, const char *str, gFont font, gColor color, gColor bgColor, gJustify justify)
Draw a text string vertically centered within the specified box. The box background is filled with th...
gCoord gdispGetStringWidthCount(const char *str, gFont font, gU16 count)
Get the pixel width of a string of a given character length.
gI16 gCoord
The type for a coordinate or length on the screen.
gCoord gdispGetStringWidth(const char *str, gFont font)
Get the pixel width of an entire string.
void gdispGDrawBox(GDisplay *g, gCoord x, gCoord y, gCoord cx, gCoord cy, gColor color)
Draw a rectangular box.
void * gfxAlloc(gMemSize sz)
Allocate memory.
void * gfxRealloc(void *ptr, gMemSize oldsz, gMemSize newsz)
Re-allocate memory.
void gwinTexteditDefaultDraw(GWidgetObject *gw, void *param)
The default rendering function for the textedit widget.
void gwinTextEditSendKey(GHandle gh, char *pkey, unsigned len)
Send a normal utf8 character to the textedit.
void gwinTextEditSendSpecialKey(GHandle gh, gU8 key)
Send a special key to the textedit such as GKEY_LEFT, GKEY_RIGHT, GKEY_HOME, GKEY_END.
GHandle gwinGTexteditCreate(GDisplay *g, GTexteditObject *wt, GWidgetInit *pInit, gMemSize maxSize)
Create a TextEdit widget.
void gwinSetVisible(GHandle gh, gBool visible)
Sets whether a window is visible or not.
A window object structure.
const struct gwinVMT * vmt
The Virtual Method Table for a GWIN window.