int TE_UpdateTextEdition(TextEdition *te, int i) { int c, l, x, y, done=0; i = WordHead(te->text, i-1); GetPositionInEdition(*te, i, &l, &c); x = te->tab[l][c].x; y = te->tab[l][c].y; te->xmax = 0; for(; te->text[i] && !done ; i++) { if (!te->tab[l][c].c) NewLetter(te->tab, l, c); te->tab[l][c].c = te->text[i]; te->tab[l][c].x = x; te->tab[l][c].y = y; x += WidthChar(*te,te->text[i]); c++; if (te->xmax < x) te->xmax = x; if (HasMultilineStyle(te->style)) { if ( (te->text[i] == '\n' || (c>0 && HasAutoJumpStyle(te->style) && (te->text[i] == '-' || te->text[i] == ' ') && WidthWord(*te, i+1) >= te->pos.w-x-VSBWidth(*te))) || (WidthChar(*te,te->text[i+1]) >= te->pos.w-x && HasAutoJumpStyle(te->style)) ) { te->tab[l][c].x = x; te->tab[l][c].y = y; for (; te->tab[l][c].c ; c++) te->tab[l][c].c = INVALID_CHAR; if (++l >= te->numLines) NewLine(te, l); x = 0; y = l*te->hSpace; c = 0; } } } te->tab[l][c].x = x; te->tab[l][c].y = y; te->lastLine = l; for (; l<te->numLines ; l++) { for (; te->tab[l][c].c ; c++) te->tab[l][c].c = INVALID_CHAR; c=0; } SetHSBFromOffset(te); SetVSBFromOffset(te); return 1; }
int WidthWord(TextEdition te, int i) { int j, w=0; for (j=i ; IsCharOK(te.text[j]) && te.text[j] != ' ' && te.text[j] != '.' && te.text[j] != ',' && te.text[j] != '-' && te.text[j] != '\n' ; j++) w += WidthChar(te,te.text[j]); for (; te.text[j] == ' ' || te.text[j] == '.' || te.text[j] == ',' || te.text[j] == '-' ; j++) w += WidthChar(te,te.text[j]); return w; }
XYPOSITION SurfaceImpl::AverageCharWidth(Font &font_) { #if QT_VERSION >= 0x040200 return metrics(font_).averageCharWidth(); #else return WidthChar(font_, 'n'); #endif }
int GetCharFromXPosition(TextEdition te, int l, int x) { int i; if (!te.tab[l]) return -1; x -= te.pos.x + te.offsetX + te.blitSurfPos.x; for (i=0 ; IsCharOK(te.tab[l][i].c) && (te.tab[l][i].x + WidthChar(te,te.tab[l][i].c)/2) < x ; i++); if (!IsCharOK(te.tab[l][i].c) && IsLineOK(te,l+1)) i--; return i; }
int AverageCharWidth(Font &font_) {return WidthChar(font_, 'n');}
int TE_NewTextEdition(TextEdition *te, int length, SDL_Rect pos, TTF_Font *font, SDL_Color colorFG, int style) { int w, h, i; SDL_Rect pos2; if (!TE_initialized) TE_Init(); if (!te || !font || length <= 0) return 0; te->textLength = length; te->text = malloc(sizeof(char)*(length+1)); if (!te->text) return 0; te->text[0] = '\0'; te->pos = pos; te->font = font; te->colorFG = colorFG; te->style = style; te->HScrollBar = NULL; te->VScrollBar = NULL; SetHSBLength(te, 5); SetVSBLength(te, 5); if (te->pos.x < 0) te->pos.x = 0; if (te->pos.y < 0) te->pos.y = 0; if (te->pos.w < 20) te->pos.w = 20; if (te->pos.h < (te->fontHeight=TTF_FontHeight(font))) te->pos.h = te->fontHeight; if (te->blitStyle > 3 || te->blitStyle < 1) te->blitStyle = TE_BLITSTYLE_BLENDED; if (te->hSpace <= 0) te->hSpace = TTF_FontLineSkip(font); if (!HasMultilineStyle(style)) te->pos.h = te->hSpace + HSBHeight(*te); if (AreSameColor(te->colorBG,te->colorFG)) { te->colorBG = ColorInverse(te->colorFG); te->colorBGSelect = te->colorFG; te->colorFGSelect = te->colorBG; } if (!te->blitSurf) te->blitSurf = SDL_GetVideoSurface(); /*te->blitSurfPos.x = 0; te->blitSurfPos.y = 0;*/ te->tab = malloc(sizeof(CharInfo*)); te->tab[0] = malloc(sizeof(CharInfo)); memset(&(te->tab[0][0]),0,sizeof(CharInfo)); te->numLines = 1; te->lastLine = 0; te->offsetX = 0; te->offsetY = 0; te->focus = 0; te->prevCursorPos = -1; te->mouseStatus = 0; te->HSBCaught = 0; te->VSBCaught = 0; for (i=1 ; i<256 ; i++) { DimLetter(*te, (char)(i-128), &w, &h); te->dimTab[i].w = w; te->dimTab[i].h = h; } WidthChar(*te, '\0') = 0; HeigthChar(*te, '\0') = te->fontHeight; WidthChar(*te, '\n') = WidthChar(*te, ' '); HeigthChar(*te, '\n') = HeigthChar(*te, ' '); WidthChar(*te, INVALID_CHAR) = 0; HeigthChar(*te, INVALID_CHAR) = te->fontHeight; pos.x = 0; pos.y = 0; pos2 = te->pos; te->tmpSurf = SDL_CreateRGBSurface(SDL_HWSURFACE, pos2.w, pos2.h, 32, 0,0,0,0); SDL_BlitSurface(te->blitSurf, &pos2, te->tmpSurf, &pos); te->tmpSurfSave = SDL_CreateRGBSurface(SDL_HWSURFACE, pos2.w, pos2.h, 32, 0,0,0,0); SDL_BlitSurface(te->blitSurf, &pos2, te->tmpSurfSave, &pos); return 1; }
int TE_DisplayTextEdition(TextEdition *te) { int l, c, state, i=0, upY = -1, downY = -1; char buf[2] = ""; SDL_Rect pos, rect = te->pos; SDL_Surface *surf; CharInfo *ci, *fci, *lci; rect.x = 0; rect.y = 0; rect.w -= VSBWidth(*te); rect.h -= HSBHeight(*te); for (l=0 ; IsLineOK(*te,l) ; l++) { fci = NULL; lci = NULL; for (c=0 ; IsCharOK(te->tab[l][c].c) ; c++) { ci = &(te->tab[l][c]); buf[0] = ci->c == '\n' ? ' ' : ci->c; pos.x = ci->x + te->offsetX; pos.y = ci->y + te->offsetY; pos.w = WidthChar(*te,buf[0]); pos.h = HeigthChar(*te,buf[0]); state = te->focus && IsInSelection(i,te->selection) && !HasJustDisplayStyle(te->style); if (IsRectInRect(pos, rect)) { if (i == te->prevCursorPos || i == te->cursorPos || state != ci->prevState || pos.x != ci->prevX || pos.y != ci->prevY || buf[0] != ci->prevC) { surf = TE_RenderText(buf, *te, state); if (te->blitStyle != TE_BLITSTYLE_SHADED) SDL_BlitSurface(te->tmpSurfSave, &pos, te->tmpSurf, &pos); SDL_BlitSurface(surf, NULL, te->tmpSurf, &pos); SDL_FreeSurface(surf); } lci = ci; if (!fci) fci = ci; } ci->prevState = state; ci->prevX = pos.x; ci->prevY = pos.y; ci->prevC = buf[0]; i++; } if (!lci) lci = &(te->tab[l][c]); else { downY = pos.y; if (upY < 0) upY = pos.y; } pos.x = lci->x + te->offsetX + WidthChar(*te,lci->c); pos.y = lci->y + te->offsetY; pos.h = te->hSpace; if ((pos.w = te->pos.w - pos.x)>0) SDL_BlitSurface(te->tmpSurfSave, &pos, te->tmpSurf, &pos); if (fci) { pos.x = 0; pos.y = fci->y + te->offsetY; pos.h = te->hSpace; if ((pos.w = fci->x + te->offsetX)>0) SDL_BlitSurface(te->tmpSurfSave, &pos, te->tmpSurf, &pos); } for (; te->tab[l][c].c ; c++) te->tab[l][c].prevC = INVALID_CHAR; } pos.y = downY + te->hSpace; pos.x = 0; pos.w = te->pos.w; if ((pos.h = te->pos.h - pos.y)>0) SDL_BlitSurface(te->tmpSurfSave, &pos, te->tmpSurf, &pos); pos.y = 0; pos.x = 0; pos.w = te->pos.w; if ((pos.h = upY)>0) SDL_BlitSurface(te->tmpSurfSave, &pos, te->tmpSurf, &pos); for (; l<te->numLines ; l++) { for (c=0 ; te->tab[l][c].c ; c++) te->tab[l][c].prevC = INVALID_CHAR; } if (!HasJustDisplayStyle(te->style)) DisplayCursor(*te); te->prevCursorPos = te->cursorPos; DisplayScrollBars(te); SDL_BlitSurface(te->tmpSurf, NULL, te->blitSurf, &(te->pos)); return 1; }