| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#include <fontforge-config.h> |
| 29 |
|
| 30 |
#include "fontforgeui.h" |
| 31 |
#include "gkeysym.h" |
| 32 |
#include "langfreq.h" |
| 33 |
#include "sftextfieldP.h" |
| 34 |
#include "splinefill.h" |
| 35 |
#include "splineutil.h" |
| 36 |
#include "tottfgpos.h" |
| 37 |
#include "ustring.h" |
| 38 |
#include "utype.h" |
| 39 |
|
| 40 |
#include <math.h> |
| 41 |
|
| 42 |
static GBox sftextarea_box = GBOX_EMPTY; |
| 43 |
static GResFont sftextarea_font = GRESFONT_INIT("400 10pt " MONO_UI_FAMILIES); |
| 44 |
extern GBox _ggadget_Default_Box; |
| 45 |
#define MAIN_FOREGROUND (_ggadget_Default_Box.main_foreground) |
| 46 |
|
| 47 |
static unichar_t nullstr[] = { 0 }, |
| 48 |
newlinestr[] = { '\n', 0 }, tabstr[] = { '\t', 0 }; |
| 49 |
|
| 50 |
GResInfo sftextarea_ri = { |
| 51 |
NULL, &ggadget_ri,NULL, NULL, |
| 52 |
&sftextarea_box, |
| 53 |
&sftextarea_font, |
| 54 |
NULL, |
| 55 |
NULL, |
| 56 |
N_("SFTextArea"), |
| 57 |
N_("SFTextArea"), |
| 58 |
"SFTextArea", |
| 59 |
"fontforge", |
| 60 |
false, |
| 61 |
false, |
| 62 |
omf_padding|box_active_border_inner, |
| 63 |
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 64 |
GBOX_EMPTY, |
| 65 |
NULL, |
| 66 |
NULL, |
| 67 |
NULL |
| 68 |
}; |
| 69 |
|
| 70 |
static int SFTextArea_Show(SFTextArea *st, int pos); |
| 71 |
static void GTPositionGIC(SFTextArea *st); |
| 72 |
|
| 73 |
static void SFTextAreaRefigureLines(SFTextArea *st, int start_of_change, |
| 74 |
int end_of_change) { |
| 75 |
|
| 76 |
if ( st->vsb!=NULL && st->li.lines==NULL ) |
| 77 |
GScrollBarSetBounds(&st->vsb->g,0,0,st->g.inner.height); |
| 78 |
|
| 79 |
LayoutInfoRefigureLines(&st->li,start_of_change,end_of_change, |
| 80 |
st->g.inner.width); |
| 81 |
|
| 82 |
if ( st->hsb!=NULL ) |
| 83 |
GScrollBarSetBounds(&st->hsb->g,0,st->li.xmax,st->g.inner.width); |
| 84 |
if ( st->vsb!=NULL && st->li.lcnt>0 ) |
| 85 |
GScrollBarSetBounds(&st->vsb->g,0,st->li.lineheights[st->li.lcnt-1].y+st->li.lineheights[st->li.lcnt-1].fh,st->g.inner.height); |
| 86 |
} |
| 87 |
|
| 88 |
static void SFTextAreaChanged(SFTextArea *st,int src) { |
| 89 |
GEvent e; |
| 90 |
|
| 91 |
e.type = et_controlevent; |
| 92 |
e.w = st->g.base; |
| 93 |
e.u.control.subtype = et_textchanged; |
| 94 |
e.u.control.g = &st->g; |
| 95 |
e.u.control.u.tf_changed.from_pulldown = src; |
| 96 |
if ( st->g.handle_controlevent != NULL ) |
| 97 |
(st->g.handle_controlevent)(&st->g,&e); |
| 98 |
else |
| 99 |
GDrawPostEvent(&e); |
| 100 |
} |
| 101 |
|
| 102 |
static void SFTextAreaFocusChanged(SFTextArea *st,int gained) { |
| 103 |
GEvent e; |
| 104 |
|
| 105 |
e.type = et_controlevent; |
| 106 |
e.w = st->g.base; |
| 107 |
e.u.control.subtype = et_textfocuschanged; |
| 108 |
e.u.control.g = &st->g; |
| 109 |
e.u.control.u.tf_focus.gained_focus = gained; |
| 110 |
if ( st->g.handle_controlevent != NULL ) |
| 111 |
(st->g.handle_controlevent)(&st->g,&e); |
| 112 |
else |
| 113 |
GDrawPostEvent(&e); |
| 114 |
} |
| 115 |
|
| 116 |
static void _SFTextAreaReplace(SFTextArea *st, const unichar_t *str) { |
| 117 |
|
| 118 |
st->sel_oldstart = st->sel_start; |
| 119 |
st->sel_oldend = st->sel_end; |
| 120 |
st->sel_oldbase = st->sel_base; |
| 121 |
st->sel_start += LayoutInfoReplace(&st->li,str,st->sel_start,st->sel_end, |
| 122 |
st->g.inner.width); |
| 123 |
st->sel_end = st->sel_start; |
| 124 |
|
| 125 |
if ( st->hsb!=NULL ) |
| 126 |
GScrollBarSetBounds(&st->hsb->g,0,st->li.xmax,st->g.inner.width); |
| 127 |
if ( st->vsb!=NULL && st->li.lcnt>0 ) |
| 128 |
GScrollBarSetBounds(&st->vsb->g,0,st->li.lineheights[st->li.lcnt-1].y+st->li.lineheights[st->li.lcnt-1].fh,st->g.inner.height); |
| 129 |
} |
| 130 |
|
| 131 |
static void SFTextArea_Replace(SFTextArea *st, const unichar_t *str) { |
| 132 |
_SFTextAreaReplace(st,str); |
| 133 |
SFTextArea_Show(st,st->sel_start); |
| 134 |
} |
| 135 |
|
| 136 |
static int SFTextAreaFindLine(SFTextArea *st, int pos) { |
| 137 |
int i; |
| 138 |
|
| 139 |
for ( i=0; i+1<st->li.lcnt; ++i ) |
| 140 |
if ( pos<st->li.lineheights[i+1].start_pos ) |
| 141 |
break; |
| 142 |
|
| 143 |
return( i ); |
| 144 |
} |
| 145 |
|
| 146 |
static int PSTComponentCount(PST *pst) { |
| 147 |
int cnt=0; |
| 148 |
char *pt = pst->u.lig.components; |
| 149 |
|
| 150 |
for (;;) { |
| 151 |
while ( *pt==' ' ) ++pt; |
| 152 |
if ( *pt=='\0' ) |
| 153 |
return( cnt ); |
| 154 |
while ( *pt!=' ' && *pt!='\0' ) ++pt; |
| 155 |
++cnt; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
static int PSTLigComponentCount(SplineChar *sc) { |
| 160 |
PST *pst; |
| 161 |
int lcnt, ltemp; |
| 162 |
|
| 163 |
lcnt = 0; |
| 164 |
for ( pst=sc->possub; pst!=NULL ; pst=pst->next ) { |
| 165 |
|
| 166 |
|
| 167 |
if ( pst->type==pst_ligature ) { |
| 168 |
ltemp = PSTComponentCount(pst); |
| 169 |
if ( ltemp>lcnt ) |
| 170 |
lcnt = ltemp; |
| 171 |
} |
| 172 |
} |
| 173 |
return( lcnt ); |
| 174 |
} |
| 175 |
|
| 176 |
static int SFTextAreaGetOffsetFromXPos(SFTextArea *st,int i,int xpos) { |
| 177 |
int p, x, xend, pos, j, l, r2l, lcnt; |
| 178 |
struct opentype_str **line; |
| 179 |
PST *pst; |
| 180 |
|
| 181 |
if ( i<0 ) |
| 182 |
return( 0 ); |
| 183 |
if ( i>=st->li.lcnt ) |
| 184 |
return( u_strlen(st->li.text)); |
| 185 |
|
| 186 |
p = st->li.lineheights[i].p; |
| 187 |
if ( st->li.paras[p].para[0]!=NULL && |
| 188 |
ScriptIsRightToLeft( ((struct fontlist *) (st->li.paras[p].para[0]->fl))->script )) { |
| 189 |
x = st->li.xmax - st->li.lineheights[i].linelen; |
| 190 |
r2l = true; |
| 191 |
} else { |
| 192 |
x = 0; |
| 193 |
r2l = false; |
| 194 |
} |
| 195 |
|
| 196 |
line = st->li.lines[i]; |
| 197 |
if ( line[0]==NULL ) { |
| 198 |
pos = st->li.lineheights[i].start_pos; |
| 199 |
} else { |
| 200 |
for ( j=0; line[j]!=NULL; ++j ) { |
| 201 |
xend = x + line[j]->advance_width + line[j]->vr.h_adv_off; |
| 202 |
if ( xpos<xend ) { |
| 203 |
double scale; |
| 204 |
xpos -= x; xend -= x; |
| 205 |
|
| 206 |
for ( pst=line[j]->sc->possub; pst!=NULL && pst->type!=pst_lcaret; pst=pst->next ); |
| 207 |
if ( pst!=NULL && pst->u.lcaret.cnt==0 ) |
| 208 |
pst = NULL; |
| 209 |
lcnt = 0; |
| 210 |
if ( pst==NULL ) { |
| 211 |
lcnt = PSTLigComponentCount(line[j]->sc); |
| 212 |
if ( lcnt<=1 ) { |
| 213 |
if ( xpos>xend/2 ) |
| 214 |
++j; |
| 215 |
} else { |
| 216 |
if ( line[j+1]!=NULL && xpos>(2*lcnt-1)*xend/lcnt ) { |
| 217 |
++j; |
| 218 |
lcnt = 0; |
| 219 |
} |
| 220 |
} |
| 221 |
} else { |
| 222 |
FontData *fd = ((struct fontlist *) (line[j]->fl))->fd; |
| 223 |
scale = fd->pointsize*st->li.dpi / (72.0*(fd->sf->ascent+fd->sf->descent)); |
| 224 |
if ( xpos-pst->u.lcaret.carets[ pst->u.lcaret.cnt-1 ]*scale > xend-xpos ) { |
| 225 |
++j; |
| 226 |
pst = NULL; |
| 227 |
} |
| 228 |
} |
| 229 |
if ( line[j]==NULL ) { |
| 230 |
pos = line[j-1]->orig_index + ((struct fontlist *) (line[j-1]->fl))->start +1; |
| 231 |
} else |
| 232 |
pos = line[j]->orig_index + ((struct fontlist *) (line[j]->fl))->start; |
| 233 |
if ( pst!=NULL ) { |
| 234 |
for ( l=0; l<pst->u.lcaret.cnt; ++l ) |
| 235 |
if (( l==0 && xpos < scale*pst->u.lcaret.carets[0]-xpos ) || |
| 236 |
(l!=0 && xpos-scale*pst->u.lcaret.carets[l-1] < scale*pst->u.lcaret.carets[0]-xpos )) |
| 237 |
break; |
| 238 |
if ( r2l ) |
| 239 |
l = pst->u.lcaret.cnt-l; |
| 240 |
pos += l; |
| 241 |
} else if ( lcnt>=2 ) { |
| 242 |
|
| 243 |
|
| 244 |
l = (xpos+xend/(2*lcnt))/(xend/lcnt); |
| 245 |
if ( r2l ) |
| 246 |
l = lcnt-1-l; |
| 247 |
pos += l; |
| 248 |
} |
| 249 |
break; |
| 250 |
} |
| 251 |
x = xend; |
| 252 |
} |
| 253 |
if ( line[j]==NULL ) |
| 254 |
pos = line[j-1]->orig_index + ((struct fontlist *) (line[j-1]->fl))->start +1; |
| 255 |
} |
| 256 |
return( pos ); |
| 257 |
} |
| 258 |
|
| 259 |
static int SFTextAreaGetXPosFromOffset(SFTextArea *st,int l,int pos) { |
| 260 |
int j, scpos, lcnt, x; |
| 261 |
struct opentype_str **line; |
| 262 |
PST *pst; |
| 263 |
|
| 264 |
if ( l<0 || l>= st->li.lcnt ) |
| 265 |
return( 0 ); |
| 266 |
if ( st->li.lines[0]==NULL || pos < st->li.lineheights[l].start_pos ) |
| 267 |
return( 0 ); |
| 268 |
|
| 269 |
line = st->li.lines[l]; |
| 270 |
x = 0; |
| 271 |
for ( j=0; line[j]!=NULL; ++j ) { |
| 272 |
scpos = line[j]->orig_index + ((struct fontlist *) (line[j]->fl))->start; |
| 273 |
if ( scpos==pos ) |
| 274 |
return( x ); |
| 275 |
for ( pst=line[j]->sc->possub; pst!=NULL && pst->type!=pst_lcaret; pst=pst->next ); |
| 276 |
if ( pst!=NULL && pst->u.lcaret.cnt==0 ) |
| 277 |
pst = NULL; |
| 278 |
if ( pst!=NULL && pos>scpos && pos<=scpos+pst->u.lcaret.cnt ) { |
| 279 |
FontData *fd = ((struct fontlist *) (line[j]->fl))->fd; |
| 280 |
double scale = fd->pointsize*st->li.dpi / (72.0*(fd->sf->ascent+fd->sf->descent)); |
| 281 |
return( x + rint(scale*pst->u.lcaret.carets[pos-scpos-1]) ); |
| 282 |
} |
| 283 |
x += line[j]->advance_width + line[j]->vr.h_adv_off; |
| 284 |
} |
| 285 |
|
| 286 |
x=0; |
| 287 |
for ( j=0; line[j]!=NULL; ++j ) { |
| 288 |
scpos = line[j]->orig_index + ((struct fontlist *) (line[j]->fl))->start; |
| 289 |
if ( scpos==pos ) |
| 290 |
return( x ); |
| 291 |
lcnt = PSTLigComponentCount(line[j]->sc); |
| 292 |
if ( pos>scpos && pos<scpos+lcnt ) { |
| 293 |
int wid = line[j]->advance_width + line[j]->vr.h_adv_off; |
| 294 |
return( x + (pos-scpos)*wid/lcnt ); |
| 295 |
} |
| 296 |
x += line[j]->advance_width + line[j]->vr.h_adv_off; |
| 297 |
} |
| 298 |
return( x ); |
| 299 |
} |
| 300 |
|
| 301 |
static int SFTextArea_EndPage(SFTextArea *st) { |
| 302 |
int endpage; |
| 303 |
|
| 304 |
for ( endpage=1; st->li.lcnt-endpage>=0 && |
| 305 |
st->li.lineheights[st->li.lcnt-1].y+st->li.lineheights[st->li.lcnt-1].fh |
| 306 |
-st->li.lineheights[st->li.lcnt-endpage].y <= st->g.inner.height; |
| 307 |
++endpage ); |
| 308 |
if ( (endpage-=2) < 1 ) endpage = 1; |
| 309 |
return( endpage ); |
| 310 |
} |
| 311 |
|
| 312 |
static int SFTextArea_Show(SFTextArea *st, int pos) { |
| 313 |
int i, xoff, loff, x, xlen; |
| 314 |
int refresh=false, endpage, page; |
| 315 |
|
| 316 |
if ( pos < 0 ) pos = 0; |
| 317 |
if ( pos > u_strlen(st->li.text)) pos = u_strlen(st->li.text); |
| 318 |
i = SFTextAreaFindLine(st,pos); |
| 319 |
|
| 320 |
loff = st->loff_top; |
| 321 |
for ( page=1; st->loff_top+page<st->li.lcnt && st->li.lineheights[st->loff_top+page].y-st->li.lineheights[st->loff_top].y<=st->g.inner.height; |
| 322 |
++page ); |
| 323 |
if ( --page < 1 ) page = 1; |
| 324 |
|
| 325 |
|
| 326 |
endpage = SFTextArea_EndPage(st); |
| 327 |
if ( i<loff || i>=st->loff_top+page) |
| 328 |
loff = i-page/4; |
| 329 |
if ( loff > st->li.lcnt-endpage ) |
| 330 |
loff = st->li.lcnt-endpage; |
| 331 |
if ( loff<0 ) loff = 0; |
| 332 |
if ( st->li.lcnt==0 || st->li.lineheights[st->li.lcnt-1].y<st->g.inner.height ) |
| 333 |
loff = 0; |
| 334 |
|
| 335 |
xoff = st->xoff_left; |
| 336 |
x = 0; |
| 337 |
if ( i<st->li.lcnt ) { |
| 338 |
x = SFTextAreaGetXPosFromOffset(st,i,pos); |
| 339 |
xlen = st->li.lineheights[i].linelen; |
| 340 |
if ( xlen< st->g.inner.width ) |
| 341 |
xoff = 0; |
| 342 |
else if ( x<xoff+4 || x>=xoff+st->g.inner.width-4 ) { |
| 343 |
xoff = x - xlen/4; |
| 344 |
if ( xoff<0 ) xoff = 0; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
if ( xoff!=st->xoff_left ) { |
| 349 |
st->xoff_left = xoff; |
| 350 |
if ( st->hsb!=NULL ) |
| 351 |
GScrollBarSetPos(&st->hsb->g,xoff); |
| 352 |
refresh = true; |
| 353 |
} |
| 354 |
if ( loff!=st->loff_top ) { |
| 355 |
st->loff_top = loff; |
| 356 |
if ( st->vsb!=NULL ) |
| 357 |
GScrollBarSetPos(&st->vsb->g,st->li.lineheights[loff].y); |
| 358 |
refresh = true; |
| 359 |
} |
| 360 |
GTPositionGIC(st); |
| 361 |
return( refresh ); |
| 362 |
} |
| 363 |
|
| 364 |
static void *genunicodedata(void *_gt,int32_t *len) { |
| 365 |
SFTextArea *st = _gt; |
| 366 |
unichar_t *temp; |
| 367 |
*len = st->sel_end-st->sel_start + 1; |
| 368 |
temp = malloc((*len+2)*sizeof(unichar_t)); |
| 369 |
temp[0] = 0xfeff; |
| 370 |
u_strncpy(temp+1,st->li.text+st->sel_start,st->sel_end-st->sel_start); |
| 371 |
temp[*len+1] = 0; |
| 372 |
return( temp ); |
| 373 |
} |
| 374 |
|
| 375 |
static void *genutf8data(void *_gt,int32_t *len) { |
| 376 |
SFTextArea *st = _gt; |
| 377 |
unichar_t *temp =u_copyn(st->li.text+st->sel_start,st->sel_end-st->sel_start); |
| 378 |
char *ret = u2utf8_copy(temp); |
| 379 |
free(temp); |
| 380 |
*len = strlen(ret); |
| 381 |
return( ret ); |
| 382 |
} |
| 383 |
|
| 384 |
static void *ddgenunicodedata(void *_gt,int32_t *len) { |
| 385 |
void *temp = genunicodedata(_gt,len); |
| 386 |
SFTextArea *st = _gt; |
| 387 |
_SFTextAreaReplace(st,nullstr); |
| 388 |
_ggadget_redraw(&st->g); |
| 389 |
return( temp ); |
| 390 |
} |
| 391 |
|
| 392 |
static void *genlocaldata(void *_gt,int32_t *len) { |
| 393 |
SFTextArea *st = _gt; |
| 394 |
unichar_t *temp =u_copyn(st->li.text+st->sel_start,st->sel_end-st->sel_start); |
| 395 |
char *ret = u2def_copy(temp); |
| 396 |
free(temp); |
| 397 |
*len = strlen(ret); |
| 398 |
return( ret ); |
| 399 |
} |
| 400 |
|
| 401 |
static void *ddgenlocaldata(void *_gt,int32_t *len) { |
| 402 |
void *temp = genlocaldata(_gt,len); |
| 403 |
SFTextArea *st = _gt; |
| 404 |
_SFTextAreaReplace(st,nullstr); |
| 405 |
_ggadget_redraw(&st->g); |
| 406 |
return( temp ); |
| 407 |
} |
| 408 |
|
| 409 |
static void noop(void *_st) { |
| 410 |
} |
| 411 |
|
| 412 |
static void SFTextAreaGrabPrimarySelection(SFTextArea *st) { |
| 413 |
int ss = st->sel_start, se = st->sel_end; |
| 414 |
|
| 415 |
GDrawGrabSelection(st->g.base,sn_primary); |
| 416 |
st->sel_start = ss; st->sel_end = se; |
| 417 |
GDrawAddSelectionType(st->g.base,sn_primary,"text/plain;charset=ISO-10646-UCS-4",st,st->sel_end-st->sel_start, |
| 418 |
sizeof(unichar_t), |
| 419 |
genunicodedata,noop); |
| 420 |
GDrawAddSelectionType(st->g.base,sn_primary,"UTF8_STRING",st,3*(st->sel_end-st->sel_start), |
| 421 |
sizeof(unichar_t), |
| 422 |
genutf8data,noop); |
| 423 |
GDrawAddSelectionType(st->g.base,sn_primary,"STRING",st,st->sel_end-st->sel_start,sizeof(char), |
| 424 |
genlocaldata,noop); |
| 425 |
} |
| 426 |
|
| 427 |
static void SFTextAreaGrabDDSelection(SFTextArea *st) { |
| 428 |
|
| 429 |
GDrawGrabSelection(st->g.base,sn_drag_and_drop); |
| 430 |
GDrawAddSelectionType(st->g.base,sn_drag_and_drop,"text/plain;charset=ISO-10646-UCS-4",st,st->sel_end-st->sel_start, |
| 431 |
sizeof(unichar_t), |
| 432 |
ddgenunicodedata,noop); |
| 433 |
GDrawAddSelectionType(st->g.base,sn_drag_and_drop,"STRING",st,st->sel_end-st->sel_start,sizeof(char), |
| 434 |
ddgenlocaldata,noop); |
| 435 |
} |
| 436 |
|
| 437 |
static void SFTextAreaGrabSelection(SFTextArea *st, enum selnames sel ) { |
| 438 |
|
| 439 |
if ( st->sel_start!=st->sel_end ) { |
| 440 |
unichar_t *temp; |
| 441 |
char *ctemp; |
| 442 |
int i; |
| 443 |
uint16_t *u2temp; |
| 444 |
|
| 445 |
GDrawGrabSelection(st->g.base,sel); |
| 446 |
temp = malloc((st->sel_end-st->sel_start + 2)*sizeof(unichar_t)); |
| 447 |
temp[0] = 0xfeff; |
| 448 |
u_strncpy(temp+1,st->li.text+st->sel_start,st->sel_end-st->sel_start); |
| 449 |
ctemp = u2utf8_copy(temp); |
| 450 |
GDrawAddSelectionType(st->g.base,sel,"text/plain;charset=ISO-10646-UCS-4",temp,u_strlen(temp), |
| 451 |
sizeof(unichar_t), |
| 452 |
NULL,NULL); |
| 453 |
u2temp = malloc((st->sel_end-st->sel_start + 2)*sizeof(uint16_t)); |
| 454 |
for ( i=0; temp[i]!=0; ++i ) |
| 455 |
u2temp[i] = temp[i]; |
| 456 |
u2temp[i] = 0; |
| 457 |
GDrawAddSelectionType(st->g.base,sel,"text/plain;charset=ISO-10646-UCS-2",u2temp,u_strlen(temp), |
| 458 |
2, |
| 459 |
NULL,NULL); |
| 460 |
GDrawAddSelectionType(st->g.base,sel,"UTF8_STRING",ctemp,strlen(ctemp),sizeof(char), |
| 461 |
NULL,NULL); |
| 462 |
GDrawAddSelectionType(st->g.base,sel,"STRING",u2def_copy(temp),u_strlen(temp),sizeof(char), |
| 463 |
NULL,NULL); |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
static int SFTextAreaSelBackword(unichar_t *text,int start) { |
| 468 |
unichar_t ch; |
| 469 |
|
| 470 |
if ( start==0 ) |
| 471 |
return( 0 ); ; |
| 472 |
|
| 473 |
ch = text[start-1]; |
| 474 |
if ( isalnum(ch) || ch=='_' ) { |
| 475 |
int i; |
| 476 |
for ( i=start-1; i>=0 && (isalnum(text[i]) || text[i]=='_') ; --i ); |
| 477 |
start = i+1; |
| 478 |
} else { |
| 479 |
int i; |
| 480 |
for ( i=start-1; i>=0 && !isalnum(text[i]) && text[i]!='_' ; --i ); |
| 481 |
start = i+1; |
| 482 |
} |
| 483 |
return( start ); |
| 484 |
} |
| 485 |
|
| 486 |
static int SFTextAreaSelForeword(unichar_t *text,int end) { |
| 487 |
unichar_t ch = text[end]; |
| 488 |
|
| 489 |
if ( ch=='\0' ) |
| 490 |
; |
| 491 |
else if ( isalnum(ch) || ch=='_' ) { |
| 492 |
int i; |
| 493 |
for ( i=end; isalnum(text[i]) || text[i]=='_' ; ++i ); |
| 494 |
end = i; |
| 495 |
} else { |
| 496 |
int i; |
| 497 |
for ( i=end; !isalnum(text[i]) && text[i]!='_' && text[i]!='\0' ; ++i ); |
| 498 |
end = i; |
| 499 |
} |
| 500 |
return( end ); |
| 501 |
} |
| 502 |
|
| 503 |
static void SFTextAreaSelectWord(SFTextArea *st,int mid, int16_t *start, int16_t *end) { |
| 504 |
unichar_t *text = st->li.text; |
| 505 |
unichar_t ch = text[mid]; |
| 506 |
|
| 507 |
if ( ch=='\0' ) |
| 508 |
*start = *end = mid; |
| 509 |
else if ( isspace(ch) ) { |
| 510 |
int i; |
| 511 |
for ( i=mid; isspace(text[i]); ++i ); |
| 512 |
*end = i; |
| 513 |
for ( i=mid-1; i>=0 && isspace(text[i]) ; --i ); |
| 514 |
*start = i+1; |
| 515 |
} else if ( isalnum(ch) || ch=='_' ) { |
| 516 |
int i; |
| 517 |
for ( i=mid; isalnum(text[i]) || text[i]=='_' ; ++i ); |
| 518 |
*end = i; |
| 519 |
for ( i=mid-1; i>=0 && (isalnum(text[i]) || text[i]=='_') ; --i ); |
| 520 |
*start = i+1; |
| 521 |
} else { |
| 522 |
int i; |
| 523 |
for ( i=mid; !isalnum(text[i]) && text[i]!='_' && text[i]!='\0' ; ++i ); |
| 524 |
*end = i; |
| 525 |
for ( i=mid-1; i>=0 && !isalnum(text[i]) && text[i]!='_' ; --i ); |
| 526 |
*start = i+1; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
static void SFTextAreaSelectWords(SFTextArea *st,int last) { |
| 531 |
int16_t ss, se; |
| 532 |
SFTextAreaSelectWord(st,st->sel_base,&st->sel_start,&st->sel_end); |
| 533 |
if ( last!=st->sel_base ) { |
| 534 |
SFTextAreaSelectWord(st,last,&ss,&se); |
| 535 |
if ( ss<st->sel_start ) st->sel_start = ss; |
| 536 |
if ( se>st->sel_end ) st->sel_end = se; |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
static void SFTextAreaPaste(SFTextArea *st,enum selnames sel) { |
| 541 |
if ( GDrawSelectionHasType(st->g.base,sel,"UTF8_STRING") || |
| 542 |
GDrawSelectionHasType(st->g.base,sel,"text/plain;charset=UTF-8")) { |
| 543 |
unichar_t *temp; char *ctemp; |
| 544 |
int32_t len; |
| 545 |
if ( GDrawSelectionHasType(st->g.base,sel,"UTF8_STRING") ) |
| 546 |
ctemp = GDrawRequestSelection(st->g.base,sel,"UTF8_STRING",&len); |
| 547 |
else |
| 548 |
ctemp = GDrawRequestSelection(st->g.base,sel,"text/plain;charset=UTF-8",&len); |
| 549 |
if ( ctemp!=NULL ) { |
| 550 |
temp = utf82u_copyn(ctemp,strlen(ctemp)); |
| 551 |
SFTextArea_Replace(st,temp); |
| 552 |
free(ctemp); free(temp); |
| 553 |
} |
| 554 |
} else if ( GDrawSelectionHasType(st->g.base,sel,"text/plain;charset=ISO-10646-UCS-4")) { |
| 555 |
unichar_t *temp; |
| 556 |
int32_t len; |
| 557 |
temp = GDrawRequestSelection(st->g.base,sel,"text/plain;charset=ISO-10646-UCS-4",&len); |
| 558 |
|
| 559 |
if ( temp!=NULL ) |
| 560 |
SFTextArea_Replace(st,temp[0]==0xfeff?temp+1:temp); |
| 561 |
free(temp); |
| 562 |
} else if ( GDrawSelectionHasType(st->g.base,sel,"Unicode") || |
| 563 |
GDrawSelectionHasType(st->g.base,sel,"text/plain;charset=ISO-10646-UCS-2")) { |
| 564 |
unichar_t *temp; |
| 565 |
uint16_t *temp2; |
| 566 |
int32_t len; |
| 567 |
temp2 = GDrawRequestSelection(st->g.base,sel,"text/plain;charset=ISO-10646-UCS-2",&len); |
| 568 |
if ( temp2==NULL || len==0 ) |
| 569 |
temp2 = GDrawRequestSelection(st->g.base,sel,"Unicode",&len); |
| 570 |
if ( temp2!=NULL ) { |
| 571 |
int i; |
| 572 |
temp = malloc((len/2+1)*sizeof(unichar_t)); |
| 573 |
for ( i=0; temp2[i]!=0; ++i ) |
| 574 |
temp[i] = temp2[i]; |
| 575 |
temp[i] = 0; |
| 576 |
SFTextArea_Replace(st,temp[0]==0xfeff?temp+1:temp); |
| 577 |
free(temp); |
| 578 |
} |
| 579 |
free(temp2); |
| 580 |
} else if ( GDrawSelectionHasType(st->g.base,sel,"STRING")) { |
| 581 |
unichar_t *temp; char *ctemp; |
| 582 |
int32_t len; |
| 583 |
ctemp = GDrawRequestSelection(st->g.base,sel,"STRING",&len); |
| 584 |
if ( ctemp!=NULL ) { |
| 585 |
temp = def2u_copy(ctemp); |
| 586 |
SFTextArea_Replace(st,temp); |
| 587 |
free(ctemp); free(temp); |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
static int sftextarea_editcmd(GGadget *g,enum editor_commands cmd) { |
| 593 |
SFTextArea *st = (SFTextArea *) g; |
| 594 |
int i; |
| 595 |
|
| 596 |
switch ( cmd ) { |
| 597 |
case ec_selectall: |
| 598 |
st->sel_start = 0; |
| 599 |
st->sel_end = u_strlen(st->li.text); |
| 600 |
return( true ); |
| 601 |
case ec_clear: |
| 602 |
SFTextArea_Replace(st,nullstr); |
| 603 |
return( true ); |
| 604 |
case ec_cut: |
| 605 |
SFTextAreaGrabSelection(st,sn_clipboard); |
| 606 |
SFTextArea_Replace(st,nullstr); |
| 607 |
return( true ); |
| 608 |
case ec_copy: |
| 609 |
SFTextAreaGrabSelection(st,sn_clipboard); |
| 610 |
return( true ); |
| 611 |
case ec_paste: |
| 612 |
SFTextAreaPaste(st,sn_clipboard); |
| 613 |
SFTextArea_Show(st,st->sel_start); |
| 614 |
return( true ); |
| 615 |
case ec_undo: |
| 616 |
if ( st->li.oldtext!=NULL ) { |
| 617 |
unichar_t *temp = st->li.text; |
| 618 |
struct fontlist *ofl = st->li.fontlist; |
| 619 |
int16_t s; |
| 620 |
st->li.text = st->li.oldtext; st->li.oldtext = temp; |
| 621 |
st->li.fontlist = st->li.oldfontlist; st->li.oldfontlist = ofl; |
| 622 |
s = st->sel_start; st->sel_start = st->sel_oldstart; st->sel_oldstart = s; |
| 623 |
s = st->sel_end; st->sel_end = st->sel_oldend; st->sel_oldend = s; |
| 624 |
s = st->sel_base; st->sel_base = st->sel_oldbase; st->sel_oldbase = s; |
| 625 |
for ( i=0; i<st->li.pcnt; ++i ) |
| 626 |
free( st->li.paras[i].para); |
| 627 |
free(st->li.paras); st->li.paras = NULL; st->li.pcnt = 0; st->li.pmax = 0; |
| 628 |
for ( i=0; i<st->li.lcnt; ++i ) |
| 629 |
free( st->li.lines[i]); |
| 630 |
free( st->li.lines ); |
| 631 |
free( st->li.lineheights ); |
| 632 |
st->li.lines = NULL; st->li.lineheights = NULL; st->li.lcnt = 0; |
| 633 |
SFTextAreaRefigureLines(st, 0, -1); |
| 634 |
SFTextArea_Show(st,st->sel_end); |
| 635 |
} |
| 636 |
return( true ); |
| 637 |
case ec_redo: |
| 638 |
return( true ); |
| 639 |
case ec_backword: |
| 640 |
if ( st->sel_start==st->sel_end && st->sel_start!=0 ) { |
| 641 |
st->sel_start = SFTextAreaSelBackword(st->li.text,st->sel_start); |
| 642 |
} |
| 643 |
SFTextArea_Replace(st,nullstr); |
| 644 |
return( true ); |
| 645 |
case ec_deleteword: |
| 646 |
if ( st->sel_start==st->sel_end && st->sel_start!=0 ) |
| 647 |
SFTextAreaSelectWord(st,st->sel_start,&st->sel_start,&st->sel_end); |
| 648 |
SFTextArea_Replace(st,nullstr); |
| 649 |
return( true ); |
| 650 |
case ec_search: case ec_backsearch: case ec_max: break; |
| 651 |
} |
| 652 |
return( false ); |
| 653 |
} |
| 654 |
|
| 655 |
static int _sftextarea_editcmd(GGadget *g,enum editor_commands cmd) { |
| 656 |
if ( sftextarea_editcmd(g,cmd)) { |
| 657 |
_ggadget_redraw(g); |
| 658 |
GTPositionGIC((SFTextArea *) g); |
| 659 |
return( true ); |
| 660 |
} |
| 661 |
return( false ); |
| 662 |
} |
| 663 |
|
| 664 |
static int GTBackPos(SFTextArea *st,int pos, int ismeta) { |
| 665 |
int newpos; |
| 666 |
|
| 667 |
if ( ismeta ) |
| 668 |
newpos = SFTextAreaSelBackword(st->li.text,pos); |
| 669 |
else |
| 670 |
newpos = pos-1; |
| 671 |
if ( newpos==-1 ) newpos = 0; |
| 672 |
return( newpos ); |
| 673 |
} |
| 674 |
|
| 675 |
static int GTForePos(SFTextArea *st,int pos, int ismeta) { |
| 676 |
int newpos=pos; |
| 677 |
|
| 678 |
if ( ismeta ) |
| 679 |
newpos = SFTextAreaSelForeword(st->li.text,pos); |
| 680 |
else { |
| 681 |
if ( st->li.text[pos]!=0 ) |
| 682 |
newpos = pos+1; |
| 683 |
} |
| 684 |
return( newpos ); |
| 685 |
} |
| 686 |
|
| 687 |
static void SFTextAreaImport(SFTextArea *st) { |
| 688 |
char *cret = gwwv_open_filename(_("Open"),NULL, |
| 689 |
"*.txt",NULL); |
| 690 |
unichar_t *str; |
| 691 |
|
| 692 |
if ( cret==NULL ) |
| 693 |
return; |
| 694 |
str = _GGadgetFileToUString(cret,65536); |
| 695 |
if ( str==NULL ) { |
| 696 |
ff_post_error(_("Could not open"),_("Could not open %.100s"),cret); |
| 697 |
free(cret); |
| 698 |
return; |
| 699 |
} |
| 700 |
free(cret); |
| 701 |
SFTextArea_Replace(st,str); |
| 702 |
free(str); |
| 703 |
} |
| 704 |
|
| 705 |
static void SFTextAreaInsertRandom(SFTextArea *st) { |
| 706 |
LayoutInfo *li = &st->li; |
| 707 |
struct fontlist *fl, *prev; |
| 708 |
char **scriptlangs; |
| 709 |
int i,cnt; |
| 710 |
uint32_t script, lang; |
| 711 |
char *utf8_str; |
| 712 |
unichar_t *str; |
| 713 |
int start, pos; |
| 714 |
struct lang_frequencies **freq; |
| 715 |
|
| 716 |
for ( fl=li->fontlist, prev = NULL; fl!=NULL && fl->start<=st->sel_start ; prev=fl, fl=fl->next ); |
| 717 |
if ( prev==NULL ) |
| 718 |
return; |
| 719 |
scriptlangs = SFScriptLangs(prev->fd->sf,&freq); |
| 720 |
if ( scriptlangs==NULL || scriptlangs[0]==NULL ) { |
| 721 |
ff_post_error(_("No letters in font"), _("No letters in font")); |
| 722 |
free(scriptlangs); |
| 723 |
free(freq); |
| 724 |
return; |
| 725 |
} |
| 726 |
for ( cnt=0; scriptlangs[cnt]!=NULL; ++cnt ); |
| 727 |
i = ff_choose(_("Text from script"),(const char **) scriptlangs,cnt,0,_("Insert random text in the specified script")); |
| 728 |
if ( i==-1 ) |
| 729 |
return; |
| 730 |
pos = strlen(scriptlangs[i])-10; |
| 731 |
script = (scriptlangs[i][pos+0]<<24) | |
| 732 |
(scriptlangs[i][pos+1]<<16) | |
| 733 |
(scriptlangs[i][pos+2]<<8 ) | |
| 734 |
(scriptlangs[i][pos+3] ); |
| 735 |
lang = (scriptlangs[i][pos+5]<<24) | |
| 736 |
(scriptlangs[i][pos+6]<<16) | |
| 737 |
(scriptlangs[i][pos+7]<<8 ) | |
| 738 |
(scriptlangs[i][pos+8] ); |
| 739 |
|
| 740 |
utf8_str = RandomParaFromScriptLang(script,lang,prev->fd->sf,freq[i]); |
| 741 |
str = utf82u_copy(utf8_str); |
| 742 |
|
| 743 |
start = st->sel_start; |
| 744 |
SFTextArea_Replace(st,str); |
| 745 |
SFTFSetScriptLang(&st->g,start,start+u_strlen(str),script,lang); |
| 746 |
|
| 747 |
free(str); |
| 748 |
free(utf8_str); |
| 749 |
for ( i=0; scriptlangs[i]!=NULL; ++i ) |
| 750 |
free(scriptlangs[i]); |
| 751 |
free(scriptlangs); |
| 752 |
free(freq); |
| 753 |
} |
| 754 |
|
| 755 |
static void SFTextAreaSave(SFTextArea *st) { |
| 756 |
char *cret = gwwv_save_filename(_("Save"),NULL, "*.txt"); |
| 757 |
FILE *file; |
| 758 |
unichar_t *pt; |
| 759 |
|
| 760 |
if ( cret==NULL ) |
| 761 |
return; |
| 762 |
file = fopen(cret,"w"); |
| 763 |
if ( file==NULL ) { |
| 764 |
ff_post_error(_("Could not open"),_("Could not open %.100s"),cret); |
| 765 |
free(cret); |
| 766 |
return; |
| 767 |
} |
| 768 |
free(cret); |
| 769 |
|
| 770 |
putc(0xef,file); |
| 771 |
putc(0xbb,file); |
| 772 |
putc(0xbf,file); |
| 773 |
for ( pt = st->li.text ; *pt; ++pt ) { |
| 774 |
if ( *pt<0x80 ) |
| 775 |
putc(*pt,file); |
| 776 |
else if ( *pt<0x800 ) { |
| 777 |
putc(0xc0 | (*pt>>6), file); |
| 778 |
putc(0x80 | (*pt&0x3f), file); |
| 779 |
} else if ( *pt>=0xd800 && *pt<0xdc00 && pt[1]>=0xdc00 && pt[1]<0xe000 ) { |
| 780 |
int u = ((*pt>>6)&0xf)+1, y = ((*pt&3)<<4) | ((pt[1]>>6)&0xf); |
| 781 |
putc( 0xf0 | (u>>2),file ); |
| 782 |
putc( 0x80 | ((u&3)<<4) | ((*pt>>2)&0xf),file ); |
| 783 |
putc( 0x80 | y,file ); |
| 784 |
putc( 0x80 | (pt[1]&0x3f),file ); |
| 785 |
} else { |
| 786 |
putc( 0xe0 | (*pt>>12),file ); |
| 787 |
putc( 0x80 | ((*pt>>6)&0x3f),file ); |
| 788 |
putc( 0x80 | (*pt&0x3f),file ); |
| 789 |
} |
| 790 |
} |
| 791 |
fclose(file); |
| 792 |
} |
| 793 |
|
| 794 |
static void SFTextAreaSaveImage(SFTextArea *st) { |
| 795 |
char *cret; |
| 796 |
GImage *image; |
| 797 |
struct _GImage *base; |
| 798 |
char *basename; |
| 799 |
int i,ret, p, x, j; |
| 800 |
struct opentype_str **line; |
| 801 |
int as; |
| 802 |
|
| 803 |
if ( st->li.lcnt==0 ) |
| 804 |
return; |
| 805 |
|
| 806 |
basename = NULL; |
| 807 |
if ( st->li.fontlist!=NULL ) { |
| 808 |
basename = malloc(strlen(st->li.fontlist->fd->sf->fontname)+8); |
| 809 |
strcpy(basename, st->li.fontlist->fd->sf->fontname); |
| 810 |
#ifdef _NO_LIBPNG |
| 811 |
strcat(basename,".bmp"); |
| 812 |
#else |
| 813 |
strcat(basename,".png"); |
| 814 |
#endif |
| 815 |
} |
| 816 |
#ifdef _NO_LIBPNG |
| 817 |
cret = gwwv_save_filename(_("Save Image"),basename, "*.bmp"); |
| 818 |
#else |
| 819 |
cret = gwwv_save_filename(_("Save Image"),basename, "*.{bmp,png}"); |
| 820 |
#endif |
| 821 |
free(basename); |
| 822 |
if ( cret==NULL ) |
| 823 |
return; |
| 824 |
|
| 825 |
image = GImageCreate(it_index,st->g.inner.width+2, |
| 826 |
st->li.lineheights[st->li.lcnt-1].y+st->li.lineheights[st->li.lcnt-1].fh+2); |
| 827 |
base = image->u.image; |
| 828 |
memset(base->data,0,base->bytes_per_line*base->height); |
| 829 |
for ( i=0; i<256; ++i ) |
| 830 |
base->clut->clut[i] = (255-i)*0x010101; |
| 831 |
base->clut->is_grey = true; |
| 832 |
base->clut->clut_len = 256; |
| 833 |
|
| 834 |
as = 0; |
| 835 |
if ( st->li.lcnt!=0 ) |
| 836 |
as = st->li.lineheights[0].as; |
| 837 |
|
| 838 |
for ( i=0; i<st->li.lcnt; ++i ) { |
| 839 |
|
| 840 |
p = st->li.lineheights[i].p; |
| 841 |
if ( st->li.paras[p].para[0]!=NULL && |
| 842 |
ScriptIsRightToLeft( ((struct fontlist *) (st->li.paras[p].para[0]->fl))->script )) |
| 843 |
x = st->li.xmax - st->li.lineheights[i].linelen; |
| 844 |
else |
| 845 |
x = 0; |
| 846 |
line = st->li.lines[i]; |
| 847 |
for ( j=0; line[j]!=NULL; ++j ) { |
| 848 |
LI_FDDrawChar(image, |
| 849 |
(void (*)(void *,GImage *,GRect *,int, int)) GImageDrawImage, |
| 850 |
(void (*)(void *,GRect *,Color)) GImageDrawRect, |
| 851 |
line[j],x,st->li.lineheights[i].y+as,0x000000); |
| 852 |
x += line[j]->advance_width + line[j]->vr.h_adv_off; |
| 853 |
} |
| 854 |
} |
| 855 |
#ifndef _NO_LIBPNG |
| 856 |
if ( strstrmatch(cret,".png")!=NULL ) |
| 857 |
ret = GImageWritePng(image,cret,false); |
| 858 |
else |
| 859 |
#endif |
| 860 |
if ( strstrmatch(cret,".bmp")!=NULL ) |
| 861 |
ret = GImageWriteBmp(image,cret); |
| 862 |
else |
| 863 |
ff_post_error(_("Unsupported image format"), |
| 864 |
#ifndef _NO_LIBPNG |
| 865 |
_("Unsupported image format must be bmp or png") |
| 866 |
#else |
| 867 |
_("Unsupported image format must be bmp") |
| 868 |
#endif |
| 869 |
); |
| 870 |
if ( !ret ) |
| 871 |
ff_post_error(_("Could not write"),_("Could not write %.100s"),cret); |
| 872 |
free( cret ); |
| 873 |
GImageDestroy(image); |
| 874 |
} |
| 875 |
|
| 876 |
#define MID_Cut 1 |
| 877 |
#define MID_Copy 2 |
| 878 |
#define MID_Paste 3 |
| 879 |
|
| 880 |
#define MID_SelectAll 4 |
| 881 |
|
| 882 |
#define MID_Save 5 |
| 883 |
#define MID_Import 6 |
| 884 |
#define MID_Insert 7 |
| 885 |
|
| 886 |
#define MID_Undo 8 |
| 887 |
|
| 888 |
#define MID_SaveImage 9 |
| 889 |
|
| 890 |
static SFTextArea *popup_kludge; |
| 891 |
|
| 892 |
static void SFTFPopupInvoked(GWindow v, GMenuItem *mi,GEvent *e) { |
| 893 |
SFTextArea *st; |
| 894 |
if ( popup_kludge==NULL ) |
| 895 |
return; |
| 896 |
st = popup_kludge; |
| 897 |
popup_kludge = NULL; |
| 898 |
switch ( mi->mid ) { |
| 899 |
case MID_Undo: |
| 900 |
sftextarea_editcmd(&st->g,ec_undo); |
| 901 |
break; |
| 902 |
case MID_Cut: |
| 903 |
sftextarea_editcmd(&st->g,ec_cut); |
| 904 |
break; |
| 905 |
case MID_Copy: |
| 906 |
sftextarea_editcmd(&st->g,ec_copy); |
| 907 |
break; |
| 908 |
case MID_Paste: |
| 909 |
sftextarea_editcmd(&st->g,ec_paste); |
| 910 |
break; |
| 911 |
case MID_SelectAll: |
| 912 |
sftextarea_editcmd(&st->g,ec_selectall); |
| 913 |
break; |
| 914 |
case MID_Save: |
| 915 |
SFTextAreaSave(st); |
| 916 |
break; |
| 917 |
case MID_Import: |
| 918 |
SFTextAreaImport(st); |
| 919 |
break; |
| 920 |
case MID_Insert: |
| 921 |
SFTextAreaInsertRandom(st); |
| 922 |
break; |
| 923 |
case MID_SaveImage: |
| 924 |
SFTextAreaSaveImage(st); |
| 925 |
break; |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
static GMenuItem sftf_popuplist[] = { |
| 930 |
{ { (unichar_t *) N_("_Undo"), NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 1, 0, 'U' }, 'Z', ksm_control, NULL, NULL, SFTFPopupInvoked, MID_Undo }, |
| 931 |
{ { NULL, NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 1, 0, 0, 0, '\0' }, '\0', 0, NULL, NULL, NULL, 0 }, |
| 932 |
{ { (unichar_t *) N_("Cu_t"), NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 1, 0, 't' }, 'X', ksm_control, NULL, NULL, SFTFPopupInvoked, MID_Cut }, |
| 933 |
{ { (unichar_t *) N_("_Copy"), NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 1, 0, 'C' }, 'C', ksm_control, NULL, NULL, SFTFPopupInvoked, MID_Copy }, |
| 934 |
{ { (unichar_t *) N_("_Paste"), NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 1, 0, 'P' }, 'V', ksm_control, NULL, NULL, SFTFPopupInvoked, MID_Paste }, |
| 935 |
{ { NULL, NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 1, 0, 0, 0, '\0' }, '\0', 0, NULL, NULL, NULL, 0 }, |
| 936 |
{ { (unichar_t *) N_("_Save As..."), NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 1, 0, 'S' }, 'S', ksm_control, NULL, NULL, SFTFPopupInvoked, MID_Save }, |
| 937 |
{ { (unichar_t *) N_("_Import..."), NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 1, 0, 'I' }, 'I', ksm_control, NULL, NULL, SFTFPopupInvoked, MID_Import }, |
| 938 |
{ { (unichar_t *) N_("_Insert Random Text..."), NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 1, 0, 'I' }, 'T', ksm_control, NULL, NULL, SFTFPopupInvoked, MID_Insert }, |
| 939 |
{ { NULL, NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 1, 0, 0, 0, '\0' }, '\0', 0, NULL, NULL, NULL, 0 }, |
| 940 |
{ { (unichar_t *) N_("Save As _Image..."), NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 1, 0, 'S' }, 'S', ksm_control|ksm_shift, NULL, NULL, SFTFPopupInvoked, MID_SaveImage }, |
| 941 |
GMENUITEM_EMPTY |
| 942 |
}; |
| 943 |
|
| 944 |
void SFTFPopupMenu(SFTextArea *st, GEvent *event) { |
| 945 |
int no_sel = st->sel_start==st->sel_end; |
| 946 |
static int done = false; |
| 947 |
|
| 948 |
if ( !done ) { |
| 949 |
int i; |
| 950 |
for ( i=0; sftf_popuplist[i].ti.text!=NULL || sftf_popuplist[i].ti.line; ++i ) |
| 951 |
if ( sftf_popuplist[i].ti.text!=NULL ) |
| 952 |
sftf_popuplist[i].ti.text = (unichar_t *) _( (char *) sftf_popuplist[i].ti.text); |
| 953 |
done = true; |
| 954 |
} |
| 955 |
|
| 956 |
sftf_popuplist[0].ti.disabled = st->li.oldtext==NULL; |
| 957 |
sftf_popuplist[2].ti.disabled = no_sel; |
| 958 |
sftf_popuplist[3].ti.disabled = no_sel; |
| 959 |
sftf_popuplist[4].ti.disabled = !GDrawSelectionHasType(st->g.base,sn_clipboard,"text/plain;charset=ISO-10646-UCS-2") && |
| 960 |
!GDrawSelectionHasType(st->g.base,sn_clipboard,"UTF8_STRING") && |
| 961 |
!GDrawSelectionHasType(st->g.base,sn_clipboard,"STRING"); |
| 962 |
sftf_popuplist[9].ti.disabled = (st->li.lcnt<=0); |
| 963 |
popup_kludge = st; |
| 964 |
GMenuCreatePopupMenu(st->g.base,event, sftf_popuplist); |
| 965 |
} |
| 966 |
|
| 967 |
static int SFTextAreaDoChange(SFTextArea *st, GEvent *event) { |
| 968 |
int ss = st->sel_start, se = st->sel_end; |
| 969 |
int pos, l, xpos; |
| 970 |
unichar_t *upt; |
| 971 |
|
| 972 |
if ( ( event->u.chr.state&(ksm_control|ksm_meta)) || |
| 973 |
event->u.chr.chars[0]<' ' || event->u.chr.chars[0]==0x7f ) { |
| 974 |
switch ( event->u.chr.keysym ) { |
| 975 |
case GK_BackSpace: |
| 976 |
if ( st->sel_start==st->sel_end ) { |
| 977 |
if ( st->sel_start==0 ) |
| 978 |
return( 2 ); |
| 979 |
--st->sel_start; |
| 980 |
} |
| 981 |
SFTextArea_Replace(st,nullstr); |
| 982 |
return( true ); |
| 983 |
break; |
| 984 |
case GK_Delete: |
| 985 |
if ( st->sel_start==st->sel_end ) { |
| 986 |
if ( st->li.text[st->sel_start]==0 ) |
| 987 |
return( 2 ); |
| 988 |
++st->sel_end; |
| 989 |
} |
| 990 |
SFTextArea_Replace(st,nullstr); |
| 991 |
return( true ); |
| 992 |
break; |
| 993 |
case GK_Left: case GK_KP_Left: |
| 994 |
if ( st->sel_start==st->sel_end ) { |
| 995 |
st->sel_start = GTBackPos(st,st->sel_start,event->u.chr.state&ksm_meta); |
| 996 |
if ( !(event->u.chr.state&ksm_shift )) |
| 997 |
st->sel_end = st->sel_start; |
| 998 |
} else if ( event->u.chr.state&ksm_shift ) { |
| 999 |
if ( st->sel_end==st->sel_base ) { |
| 1000 |
st->sel_start = GTBackPos(st,st->sel_start,event->u.chr.state&ksm_meta); |
| 1001 |
} else { |
| 1002 |
st->sel_end = GTBackPos(st,st->sel_end,event->u.chr.state&ksm_meta); |
| 1003 |
} |
| 1004 |
} else { |
| 1005 |
st->sel_end = st->sel_base = st->sel_start; |
| 1006 |
} |
| 1007 |
SFTextArea_Show(st,st->sel_start); |
| 1008 |
return( 2 ); |
| 1009 |
break; |
| 1010 |
case GK_Right: case GK_KP_Right: |
| 1011 |
if ( st->sel_start==st->sel_end ) { |
| 1012 |
st->sel_end = GTForePos(st,st->sel_start,event->u.chr.state&ksm_meta); |
| 1013 |
if ( !(event->u.chr.state&ksm_shift )) |
| 1014 |
st->sel_start = st->sel_end; |
| 1015 |
} else if ( event->u.chr.state&ksm_shift ) { |
| 1016 |
if ( st->sel_end==st->sel_base ) { |
| 1017 |
st->sel_start = GTForePos(st,st->sel_start,event->u.chr.state&ksm_meta); |
| 1018 |
} else { |
| 1019 |
st->sel_end = GTForePos(st,st->sel_end,event->u.chr.state&ksm_meta); |
| 1020 |
} |
| 1021 |
} else { |
| 1022 |
st->sel_start = st->sel_base = st->sel_end; |
| 1023 |
} |
| 1024 |
SFTextArea_Show(st,st->sel_start); |
| 1025 |
return( 2 ); |
| 1026 |
break; |
| 1027 |
case GK_Up: case GK_KP_Up: |
| 1028 |
if ( !st->multi_line ) |
| 1029 |
break; |
| 1030 |
if ( !( event->u.chr.state&ksm_shift ) && st->sel_start!=st->sel_end ) |
| 1031 |
st->sel_end = st->sel_base = st->sel_start; |
| 1032 |
else { |
| 1033 |
pos = st->sel_start; |
| 1034 |
if ( ( event->u.chr.state&ksm_shift ) && st->sel_start==st->sel_base ) |
| 1035 |
pos = st->sel_end; |
| 1036 |
l = SFTextAreaFindLine(st,st->sel_start); |
| 1037 |
xpos = SFTextAreaGetXPosFromOffset(st,l,st->sel_start); |
| 1038 |
if ( l!=0 ) |
| 1039 |
pos = SFTextAreaGetOffsetFromXPos(st,l-1,xpos); |
| 1040 |
if ( event->u.chr.state&ksm_shift ) { |
| 1041 |
if ( pos<st->sel_base ) { |
| 1042 |
st->sel_start = pos; |
| 1043 |
st->sel_end = st->sel_base; |
| 1044 |
} else { |
| 1045 |
st->sel_start = st->sel_base; |
| 1046 |
st->sel_end = pos; |
| 1047 |
} |
| 1048 |
} else { |
| 1049 |
st->sel_start = st->sel_end = st->sel_base = pos; |
| 1050 |
} |
| 1051 |
} |
| 1052 |
SFTextArea_Show(st,st->sel_start); |
| 1053 |
return( 2 ); |
| 1054 |
break; |
| 1055 |
case GK_Down: case GK_KP_Down: |
| 1056 |
if ( !st->multi_line ) |
| 1057 |
break; |
| 1058 |
if ( !( event->u.chr.state&ksm_shift ) && st->sel_start!=st->sel_end ) |
| 1059 |
st->sel_end = st->sel_base = st->sel_end; |
| 1060 |
else { |
| 1061 |
pos = st->sel_start; |
| 1062 |
if ( ( event->u.chr.state&ksm_shift ) && st->sel_start==st->sel_base ) |
| 1063 |
pos = st->sel_end; |
| 1064 |
l = SFTextAreaFindLine(st,st->sel_start); |
| 1065 |
xpos = SFTextAreaGetXPosFromOffset(st,l,st->sel_start); |
| 1066 |
if ( l<st->li.lcnt-1 ) |
| 1067 |
pos = SFTextAreaGetOffsetFromXPos(st,l+1,xpos); |
| 1068 |
if ( event->u.chr.state&ksm_shift ) { |
| 1069 |
if ( pos<st->sel_base ) { |
| 1070 |
st->sel_start = pos; |
| 1071 |
st->sel_end = st->sel_base; |
| 1072 |
} else { |
| 1073 |
st->sel_start = st->sel_base; |
| 1074 |
st->sel_end = pos; |
| 1075 |
} |
| 1076 |
} else { |
| 1077 |
st->sel_start = st->sel_end = st->sel_base = pos; |
| 1078 |
} |
| 1079 |
} |
| 1080 |
SFTextArea_Show(st,st->sel_start); |
| 1081 |
return( 2 ); |
| 1082 |
break; |
| 1083 |
case GK_Home: case GK_Begin: case GK_KP_Home: case GK_KP_Begin: |
| 1084 |
if ( !(event->u.chr.state&ksm_shift) ) { |
| 1085 |
st->sel_start = st->sel_base = st->sel_end = 0; |
| 1086 |
} else { |
| 1087 |
st->sel_start = 0; st->sel_end = st->sel_base; |
| 1088 |
} |
| 1089 |
SFTextArea_Show(st,st->sel_start); |
| 1090 |
return( 2 ); |
| 1091 |
break; |
| 1092 |
|
| 1093 |
case 'E': case 'e': |
| 1094 |
if ( !( event->u.chr.state&ksm_control ) ) |
| 1095 |
return( false ); |
| 1096 |
upt = st->li.text+st->sel_base; |
| 1097 |
if ( *upt=='\n' ) |
| 1098 |
++upt; |
| 1099 |
upt = u_strchr(upt,'\n'); |
| 1100 |
if ( upt==NULL ) upt=st->li.text+u_strlen(st->li.text); |
| 1101 |
if ( !(event->u.chr.state&ksm_shift) ) { |
| 1102 |
st->sel_start = st->sel_base = st->sel_end =upt-st->li.text; |
| 1103 |
} else { |
| 1104 |
st->sel_start = st->sel_base; st->sel_end = upt-st->li.text; |
| 1105 |
} |
| 1106 |
SFTextArea_Show(st,st->sel_start); |
| 1107 |
return( 2 ); |
| 1108 |
break; |
| 1109 |
case GK_End: case GK_KP_End: |
| 1110 |
if ( !(event->u.chr.state&ksm_shift) ) { |
| 1111 |
st->sel_start = st->sel_base = st->sel_end = u_strlen(st->li.text); |
| 1112 |
} else { |
| 1113 |
st->sel_start = st->sel_base; st->sel_end = u_strlen(st->li.text); |
| 1114 |
} |
| 1115 |
SFTextArea_Show(st,st->sel_start); |
| 1116 |
return( 2 ); |
| 1117 |
break; |
| 1118 |
case 'A': case 'a': |
| 1119 |
if ( event->u.chr.state&ksm_control ) { |
| 1120 |
sftextarea_editcmd(&st->g,ec_selectall); |
| 1121 |
return( 2 ); |
| 1122 |
} |
| 1123 |
break; |
| 1124 |
case 'C': case 'c': |
| 1125 |
if ( event->u.chr.state&ksm_control ) { |
| 1126 |
sftextarea_editcmd(&st->g,ec_copy); |
| 1127 |
} |
| 1128 |
break; |
| 1129 |
case 'V': case 'v': |
| 1130 |
if ( event->u.chr.state&ksm_control ) { |
| 1131 |
sftextarea_editcmd(&st->g,ec_paste); |
| 1132 |
SFTextArea_Show(st,st->sel_start); |
| 1133 |
return( true ); |
| 1134 |
} |
| 1135 |
break; |
| 1136 |
case 'X': case 'x': |
| 1137 |
if ( event->u.chr.state&ksm_control ) { |
| 1138 |
sftextarea_editcmd(&st->g,ec_cut); |
| 1139 |
SFTextArea_Show(st,st->sel_start); |
| 1140 |
return( true ); |
| 1141 |
} |
| 1142 |
break; |
| 1143 |
case 'Z': case 'z': |
| 1144 |
if ( event->u.chr.state&ksm_control ) { |
| 1145 |
sftextarea_editcmd(&st->g,ec_undo); |
| 1146 |
SFTextArea_Show(st,st->sel_start); |
| 1147 |
return( true ); |
| 1148 |
} |
| 1149 |
break; |
| 1150 |
case 'D': case 'd': |
| 1151 |
if ( event->u.chr.state&ksm_control ) { |
| 1152 |
sftextarea_editcmd(&st->g,ec_deleteword); |
| 1153 |
SFTextArea_Show(st,st->sel_start); |
| 1154 |
return( true ); |
| 1155 |
} |
| 1156 |
break; |
| 1157 |
case 'W': case 'w': |
| 1158 |
if ( event->u.chr.state&ksm_control ) { |
| 1159 |
sftextarea_editcmd(&st->g,ec_backword); |
| 1160 |
SFTextArea_Show(st,st->sel_start); |
| 1161 |
return( true ); |
| 1162 |
} |
| 1163 |
break; |
| 1164 |
case 'M': case 'm': case 'J': case 'j': |
| 1165 |
if ( !( event->u.chr.state&ksm_control ) ) |
| 1166 |
return( false ); |
| 1167 |
|
| 1168 |
case GK_Return: case GK_Linefeed: |
| 1169 |
if ( st->accepts_returns ) { |
| 1170 |
SFTextArea_Replace(st,newlinestr); |
| 1171 |
return( true ); |
| 1172 |
} |
| 1173 |
break; |
| 1174 |
case GK_Tab: |
| 1175 |
if ( st->accepts_tabs ) { |
| 1176 |
SFTextArea_Replace(st,tabstr); |
| 1177 |
return( true ); |
| 1178 |
} |
| 1179 |
break; |
| 1180 |
case 's': case 'S': |
| 1181 |
if ( !( event->u.chr.state&ksm_control ) ) |
| 1182 |
return( false ); |
| 1183 |
SFTextAreaSave(st); |
| 1184 |
return( 2 ); |
| 1185 |
break; |
| 1186 |
case 'I': case 'i': |
| 1187 |
if ( !( event->u.chr.state&ksm_control ) ) |
| 1188 |
return( false ); |
| 1189 |
SFTextAreaImport(st); |
| 1190 |
return( true ); |
| 1191 |
} |
| 1192 |
} else { |
| 1193 |
SFTextArea_Replace(st,event->u.chr.chars); |
| 1194 |
return( true ); |
| 1195 |
} |
| 1196 |
|
| 1197 |
if ( st->sel_start == st->sel_end ) |
| 1198 |
st->sel_base = st->sel_start; |
| 1199 |
if ( ss!=st->sel_start || se!=st->sel_end ) |
| 1200 |
SFTextAreaGrabPrimarySelection(st); |
| 1201 |
return( false ); |
| 1202 |
} |
| 1203 |
|
| 1204 |
static void gt_cursor_pos(SFTextArea *st, int *x, int *y, int *fh) { |
| 1205 |
int l, ty; |
| 1206 |
|
| 1207 |
*x = 0; *y= 0; *fh = 20; |
| 1208 |
if ( st->li.fontlist!=NULL ) |
| 1209 |
*fh = st->li.fontlist->fd->pointsize*st->li.dpi/72; |
| 1210 |
l = SFTextAreaFindLine(st,st->sel_start); |
| 1211 |
if ( l<0 || l>=st->li.lcnt ) |
| 1212 |
return; |
| 1213 |
ty = st->li.lineheights[l].y - st->li.lineheights[st->loff_top].y; |
| 1214 |
if ( ty<0 || ty>st->g.inner.height ) { |
| 1215 |
*x = *y = -1; |
| 1216 |
return; |
| 1217 |
} |
| 1218 |
*y = ty; |
| 1219 |
*fh = st->li.lineheights[l].fh; |
| 1220 |
*x = SFTextAreaGetXPosFromOffset(st,l,st->sel_start); |
| 1221 |
} |
| 1222 |
|
| 1223 |
static void GTPositionGIC(SFTextArea *st) { |
| 1224 |
int x,y,fh; |
| 1225 |
|
| 1226 |
if ( !st->g.has_focus || st->gic==NULL ) |
| 1227 |
return; |
| 1228 |
gt_cursor_pos(st,&x,&y,&fh); |
| 1229 |
if ( x<0 ) |
| 1230 |
return; |
| 1231 |
GDrawSetGIC(st->g.base,st->gic,st->g.inner.x+x,st->g.inner.y+y+st->as); |
| 1232 |
} |
| 1233 |
|
| 1234 |
static void gt_set_dd_cursor(SFTextArea *st, int pos) { |
| 1235 |
st->has_dd_cursor = true; |
| 1236 |
st->dd_cursor_pos = pos; |
| 1237 |
} |
| 1238 |
|
| 1239 |
static void gt_request_redraw_cursor(GWindow pixmap, SFTextArea *st) { |
| 1240 |
GRect clip; |
| 1241 |
int x, y, fh; |
| 1242 |
|
| 1243 |
gt_cursor_pos(st,&x,&y,&fh); |
| 1244 |
|
| 1245 |
if ( x<0 || x>=st->g.inner.width ) |
| 1246 |
return; |
| 1247 |
|
| 1248 |
clip.x = st->g.inner.x + x; |
| 1249 |
clip.y = st->g.inner.y + y; |
| 1250 |
clip.width = 1; |
| 1251 |
clip.height = fh + 1; |
| 1252 |
|
| 1253 |
GDrawRequestExpose(pixmap, &clip, false); |
| 1254 |
} |
| 1255 |
|
| 1256 |
static void gt_draw_cursor(GWindow pixmap, SFTextArea *st) { |
| 1257 |
int x, y, fh, l; |
| 1258 |
|
| 1259 |
if (st->has_dd_cursor) { |
| 1260 |
st->has_dd_cursor = false; |
| 1261 |
l = SFTextAreaFindLine(st, st->dd_cursor_pos); |
| 1262 |
y = st->li.lineheights[l].y - st->li.lineheights[st->loff_top].y; |
| 1263 |
if (y < 0 || y > st->g.inner.height) |
| 1264 |
return; |
| 1265 |
x = SFTextAreaGetXPosFromOffset(st, l, st->dd_cursor_pos); |
| 1266 |
if (x < 0 || x >= st->g.inner.width) |
| 1267 |
return; |
| 1268 |
|
| 1269 |
GDrawSetLineWidth(st->g.base,0); |
| 1270 |
GDrawSetDashedLine(st->g.base,2,2,0); |
| 1271 |
GDrawDrawLine(st->g.base,st->g.inner.x+x,st->g.inner.y+y, |
| 1272 |
st->g.inner.x+x,st->g.inner.y+y+st->li.lineheights[l].fh,MAIN_FOREGROUND); |
| 1273 |
GDrawSetDashedLine(st->g.base,0,0,0); |
| 1274 |
return; |
| 1275 |
} |
| 1276 |
|
| 1277 |
if ( !st->cursor_on || st->sel_start != st->sel_end ) |
| 1278 |
return; |
| 1279 |
gt_cursor_pos(st,&x,&y,&fh); |
| 1280 |
|
| 1281 |
if ( x<0 || x>=st->g.inner.width ) |
| 1282 |
return; |
| 1283 |
GDrawDrawLine(pixmap,st->g.inner.x+x,st->g.inner.y+y, |
| 1284 |
st->g.inner.x+x,st->g.inner.y+y+fh, MAIN_FOREGROUND); |
| 1285 |
} |
| 1286 |
|
| 1287 |
static int sftextarea_expose(GWindow pixmap, GGadget *g, GEvent *event) { |
| 1288 |
SFTextArea *st = (SFTextArea *) g; |
| 1289 |
GRect old1, old2, old3, *r = &g->r, selr; |
| 1290 |
Color fg,sel; |
| 1291 |
int y,x,p,i,dotext,j,xend; |
| 1292 |
struct opentype_str **line; |
| 1293 |
|
| 1294 |
if ( g->state == gs_invisible || st->dontdraw ) |
| 1295 |
return( false ); |
| 1296 |
|
| 1297 |
GDrawPushClip(pixmap,&event->u.expose.rect, &old1); |
| 1298 |
GDrawPushClip(pixmap,r,&old2); |
| 1299 |
|
| 1300 |
if (!GDrawClipContains(pixmap, &g->inner, true)) { |
| 1301 |
GBoxDrawBackground(pixmap,r,g->box, |
| 1302 |
g->state==gs_enabled? gs_pressedactive: g->state,false); |
| 1303 |
GBoxDrawBorder(pixmap,r,g->box,g->state,false); |
| 1304 |
} else { |
| 1305 |
|
| 1306 |
enum border_shape old = g->box->border_shape; |
| 1307 |
g->box->border_shape = bs_rect; |
| 1308 |
GBoxDrawBackground(pixmap,r,g->box, |
| 1309 |
g->state==gs_enabled? gs_pressedactive: g->state,false); |
| 1310 |
g->box->border_shape = old; |
| 1311 |
} |
| 1312 |
|
| 1313 |
GDrawPushClip(pixmap,&g->inner,&old3); |
| 1314 |
GDrawSetFont(pixmap,st->font); |
| 1315 |
GDrawSetDither(NULL, false); |
| 1316 |
GDrawSetLineWidth(pixmap,0); |
| 1317 |
|
| 1318 |
fg = g->state==gs_disabled?g->box->disabled_foreground: |
| 1319 |
g->box->main_foreground==COLOR_DEFAULT?GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(pixmap)): |
| 1320 |
g->box->main_foreground; |
| 1321 |
sel = g->box->active_border; |
| 1322 |
for ( i=st->loff_top; i<st->li.lcnt; ++i ) { |
| 1323 |
int selstartx, selendx; |
| 1324 |
|
| 1325 |
y = g->inner.y+ st->li.lineheights[i].y-st->li.lineheights[st->loff_top].y+ |
| 1326 |
st->li.lineheights[i].as; |
| 1327 |
if ( y>g->inner.y+g->inner.height || y>event->u.expose.rect.y+event->u.expose.rect.height ) |
| 1328 |
break; |
| 1329 |
if ( y+st->li.lineheights[i].fh<=event->u.expose.rect.y ) |
| 1330 |
continue; |
| 1331 |
selstartx = selendx = -1; |
| 1332 |
for ( dotext=0; dotext<2; ++dotext ) { |
| 1333 |
|
| 1334 |
p = st->li.lineheights[i].p; |
| 1335 |
if ( st->li.paras[p].para[0]!=NULL && |
| 1336 |
st->li.paras[p].para[0]->fl!=NULL && |
| 1337 |
ScriptIsRightToLeft( ((struct fontlist *) (st->li.paras[p].para[0]->fl))->script )) |
| 1338 |
x = st->li.xmax - st->li.lineheights[i].linelen; |
| 1339 |
else |
| 1340 |
x = 0; |
| 1341 |
line = st->li.lines[i]; |
| 1342 |
for ( j=0; line[j]!=NULL; ++j ) { |
| 1343 |
xend = x + line[j]->advance_width + line[j]->vr.h_adv_off; |
| 1344 |
if ( dotext ) { |
| 1345 |
LI_FDDrawChar(pixmap, |
| 1346 |
(void (*)(void *,GImage *,GRect *,int, int)) GDrawDrawGlyph, |
| 1347 |
(void (*)(void *,GRect *,Color)) GDrawDrawRect, |
| 1348 |
line[j],g->inner.x+x-st->xoff_left,y,fg); |
| 1349 |
} else { |
| 1350 |
int pos = line[j]->orig_index + |
| 1351 |
((struct fontlist *) (line[j]->fl))->start; |
| 1352 |
if ( pos>=st->sel_start && pos<st->sel_end ) { |
| 1353 |
if ( selstartx==-1 ) |
| 1354 |
selstartx = x; |
| 1355 |
selendx = xend; |
| 1356 |
} |
| 1357 |
if ( !(pos>=st->sel_start && pos<st->sel_end) || line[j+1]==NULL ) { |
| 1358 |
if ( selstartx!=-1 ) { |
| 1359 |
selr.x = selstartx+g->inner.x-st->xoff_left; |
| 1360 |
selr.width = selendx-selstartx; |
| 1361 |
selr.y = y-st->li.lineheights[i].as; |
| 1362 |
selr.height = st->li.lineheights[i].fh; |
| 1363 |
GDrawFillRect(pixmap,&selr,sel); |
| 1364 |
selstartx = selendx = -1; |
| 1365 |
} |
| 1366 |
} |
| 1367 |
} |
| 1368 |
x = xend; |
| 1369 |
} |
| 1370 |
} |
| 1371 |
} |
| 1372 |
|
| 1373 |
GDrawSetDither(NULL, true); |
| 1374 |
gt_draw_cursor(pixmap, st); |
| 1375 |
GDrawPopClip(pixmap,&old3); |
| 1376 |
GDrawPopClip(pixmap,&old2); |
| 1377 |
GDrawPopClip(pixmap,&old1); |
| 1378 |
return( true ); |
| 1379 |
} |
| 1380 |
|
| 1381 |
static int SFTextAreaDoDrop(SFTextArea *st,GEvent *event,int endpos) { |
| 1382 |
if ( event->type == et_mousemove ) { |
| 1383 |
if ( GGadgetInnerWithin(&st->g,event->u.mouse.x,event->u.mouse.y) ) { |
| 1384 |
if ( endpos<st->sel_start || endpos>=st->sel_end ) |
| 1385 |
gt_set_dd_cursor(st, endpos); |
| 1386 |
} else if ( !GGadgetWithin(&st->g,event->u.mouse.x,event->u.mouse.y) ) { |
| 1387 |
GDrawPostDragEvent(st->g.base,event,et_drag); |
| 1388 |
} |
| 1389 |
} else { |
| 1390 |
if ( GGadgetInnerWithin(&st->g,event->u.mouse.x,event->u.mouse.y) ) { |
| 1391 |
if ( endpos>=st->sel_start && endpos<st->sel_end ) { |
| 1392 |
st->sel_start = st->sel_end = endpos; |
| 1393 |
} else { |
| 1394 |
unichar_t *old=st->li.oldtext, *temp; |
| 1395 |
int pos=0; |
| 1396 |
if ( event->u.mouse.state&ksm_control ) { |
| 1397 |
temp = malloc((u_strlen(st->li.text)+st->sel_end-st->sel_start+1)*sizeof(unichar_t)); |
| 1398 |
memcpy(temp,st->li.text,endpos*sizeof(unichar_t)); |
| 1399 |
memcpy(temp+endpos,st->li.text+st->sel_start, |
| 1400 |
(st->sel_end-st->sel_start)*sizeof(unichar_t)); |
| 1401 |
u_strcpy(temp+endpos+st->sel_end-st->sel_start,st->li.text+endpos); |
| 1402 |
} else if ( endpos>=st->sel_end ) { |
| 1403 |
temp = u_copy(st->li.text); |
| 1404 |
memcpy(temp+st->sel_start,temp+st->sel_end, |
| 1405 |
(endpos-st->sel_end)*sizeof(unichar_t)); |
| 1406 |
memcpy(temp+endpos-(st->sel_end-st->sel_start), |
| 1407 |
st->li.text+st->sel_start,(st->sel_end-st->sel_start)*sizeof(unichar_t)); |
| 1408 |
pos = endpos; |
| 1409 |
} else { |
| 1410 |
temp = u_copy(st->li.text); |
| 1411 |
memcpy(temp+endpos,st->li.text+st->sel_start, |
| 1412 |
(st->sel_end-st->sel_start)*sizeof(unichar_t)); |
| 1413 |
memcpy(temp+endpos+st->sel_end-st->sel_start,st->li.text+endpos, |
| 1414 |
(st->sel_start-endpos)*sizeof(unichar_t)); |
| 1415 |
pos = endpos+st->sel_end-st->sel_start; |
| 1416 |
} |
| 1417 |
st->li.oldtext = st->li.text; |
| 1418 |
st->sel_oldstart = st->sel_start; |
| 1419 |
st->sel_oldend = st->sel_end; |
| 1420 |
st->sel_oldbase = st->sel_base; |
| 1421 |
st->sel_start = st->sel_end = pos; |
| 1422 |
st->li.text = temp; |
| 1423 |
free(old); |
| 1424 |
SFTextAreaRefigureLines(st, endpos<st->sel_oldstart?endpos:st->sel_oldstart,-1); |
| 1425 |
} |
| 1426 |
} else if ( !GGadgetWithin(&st->g,event->u.mouse.x,event->u.mouse.y) ) { |
| 1427 |
|
| 1428 |
|
| 1429 |
if ( ( event->u.mouse.state&ksm_control ) ) |
| 1430 |
SFTextAreaGrabSelection(st,sn_drag_and_drop); |
| 1431 |
else |
| 1432 |
SFTextAreaGrabDDSelection(st); |
| 1433 |
GDrawPostDragEvent(st->g.base,event,et_drop); |
| 1434 |
} |
| 1435 |
st->drag_and_drop = false; |
| 1436 |
GDrawSetCursor(st->g.base,st->old_cursor); |
| 1437 |
} |
| 1438 |
_ggadget_redraw(&st->g); |
| 1439 |
return( false ); |
| 1440 |
} |
| 1441 |
|
| 1442 |
static void STChangeCheck(SFTextArea *st) { |
| 1443 |
struct fontlist *fl; |
| 1444 |
|
| 1445 |
if ( st->changefontcallback==NULL ) |
| 1446 |
return; |
| 1447 |
for ( fl=st->li.fontlist; fl!=NULL && fl->end<st->sel_end; fl=fl->next ); |
| 1448 |
if ( fl!=NULL && fl->next!=NULL && fl->next->end==st->sel_end ) |
| 1449 |
fl = fl->next; |
| 1450 |
if ( fl==NULL ) |
| 1451 |
return; |
| 1452 |
(st->changefontcallback)(st->cbcontext,fl->fd->sf,fl->fd->fonttype, |
| 1453 |
fl->fd->pointsize,fl->fd->antialias,fl->script,fl->lang,fl->feats); |
| 1454 |
} |
| 1455 |
|
| 1456 |
static int sftextarea_mouse(GGadget *g, GEvent *event) { |
| 1457 |
SFTextArea *st = (SFTextArea *) g; |
| 1458 |
int end=-1; |
| 1459 |
int i=0; |
| 1460 |
|
| 1461 |
if ( st->hidden_cursor ) { |
| 1462 |
GDrawSetCursor(st->g.base,st->old_cursor); |
| 1463 |
st->hidden_cursor = false; |
| 1464 |
_GWidget_ClearGrabGadget(g); |
| 1465 |
} |
| 1466 |
if ( !g->takes_input || (g->state!=gs_enabled && g->state!=gs_active && g->state!=gs_focused )) |
| 1467 |
return( false ); |
| 1468 |
if ( event->type == et_crossing ) |
| 1469 |
return( false ); |
| 1470 |
if (( event->type==et_mouseup || event->type==et_mousedown ) && |
| 1471 |
(event->u.mouse.button>=4 && event->u.mouse.button<=7)) { |
| 1472 |
int isv = event->u.mouse.button<=5; |
| 1473 |
if ( event->u.mouse.state&ksm_shift ) isv = !isv; |
| 1474 |
if ( isv && st->vsb!=NULL ) |
| 1475 |
return( GGadgetDispatchEvent(&st->vsb->g,event)); |
| 1476 |
else if ( !isv && st->hsb!=NULL ) |
| 1477 |
return( GGadgetDispatchEvent(&st->hsb->g,event)); |
| 1478 |
else |
| 1479 |
return( true ); |
| 1480 |
} |
| 1481 |
|
| 1482 |
if ( st->pressed==NULL && event->type == et_mousemove && g->popup_msg!=NULL && |
| 1483 |
GGadgetWithin(g,event->u.mouse.x,event->u.mouse.y)) |
| 1484 |
GGadgetPreparePopup(g->base,g->popup_msg); |
| 1485 |
|
| 1486 |
if ( event->type == et_mousedown && event->u.mouse.button==3 && |
| 1487 |
GGadgetWithin(g,event->u.mouse.x,event->u.mouse.y)) { |
| 1488 |
SFTFPopupMenu(st,event); |
| 1489 |
return( true ); |
| 1490 |
} |
| 1491 |
|
| 1492 |
if ( event->type == et_mousedown || st->pressed ) { |
| 1493 |
for ( i=st->loff_top; i<st->li.lcnt-1 && |
| 1494 |
event->u.mouse.y-g->inner.y>=st->li.lineheights[i+1].y-st->li.lineheights[st->loff_top].y; |
| 1495 |
++i ); |
| 1496 |
if ( i<0 ) i = 0; |
| 1497 |
if ( !st->multi_line ) i = 0; |
| 1498 |
end = SFTextAreaGetOffsetFromXPos(st,i,event->u.mouse.x - st->g.inner.x - st->xoff_left); |
| 1499 |
} |
| 1500 |
|
| 1501 |
if ( event->type == et_mousedown ) { |
| 1502 |
st->wordsel = st->linesel = false; |
| 1503 |
if ( event->u.mouse.button==1 && event->u.mouse.clicks>=3 ) { |
| 1504 |
if ( i<st->li.lcnt ) |
| 1505 |
st->sel_start = st->li.lineheights[i].start_pos; |
| 1506 |
else |
| 1507 |
st->sel_start = end; |
| 1508 |
if ( i+1<st->li.lcnt ) |
| 1509 |
st->sel_end = st->li.lineheights[i+1].start_pos; |
| 1510 |
else |
| 1511 |
st->sel_end = u_strlen(st->li.text); |
| 1512 |
st->wordsel = false; st->linesel = true; |
| 1513 |
} else if ( event->u.mouse.button==1 && event->u.mouse.clicks==2 ) { |
| 1514 |
st->sel_start = st->sel_end = st->sel_base = end; |
| 1515 |
st->wordsel = true; |
| 1516 |
SFTextAreaSelectWords(st,st->sel_base); |
| 1517 |
} else if ( end>=st->sel_start && end<st->sel_end && |
| 1518 |
st->sel_start!=st->sel_end && |
| 1519 |
event->u.mouse.button==1 ) { |
| 1520 |
st->drag_and_drop = true; |
| 1521 |
if ( !st->hidden_cursor ) |
| 1522 |
st->old_cursor = GDrawGetCursor(st->g.base); |
| 1523 |
GDrawSetCursor(st->g.base,ct_draganddrop); |
| 1524 |
} else if ( event->u.mouse.button!=3 && !(event->u.mouse.state&ksm_shift) ) { |
| 1525 |
if ( event->u.mouse.button==1 ) |
| 1526 |
SFTextAreaGrabPrimarySelection(st); |
| 1527 |
st->sel_start = st->sel_end = st->sel_base = end; |
| 1528 |
} else if ( end>st->sel_base ) { |
| 1529 |
st->sel_start = st->sel_base; |
| 1530 |
st->sel_end = end; |
| 1531 |
} else { |
| 1532 |
st->sel_start = end; |
| 1533 |
st->sel_end = st->sel_base; |
| 1534 |
} |
| 1535 |
if ( st->pressed==NULL ) |
| 1536 |
st->pressed = GDrawRequestTimer(st->g.base,200,100,NULL); |
| 1537 |
if ( st->sel_start > u_strlen( st->li.text )) |
| 1538 |
fprintf( stderr, "About to crash\n" ); |
| 1539 |
_ggadget_redraw(g); |
| 1540 |
if ( st->changefontcallback ) |
| 1541 |
STChangeCheck(st); |
| 1542 |
return( true ); |
| 1543 |
} else if ( st->pressed && (event->type == et_mousemove || event->type == et_mouseup )) { |
| 1544 |
int refresh = true; |
| 1545 |
|
| 1546 |
if ( st->drag_and_drop ) { |
| 1547 |
refresh = SFTextAreaDoDrop(st,event,end); |
| 1548 |
} else if ( st->linesel ) { |
| 1549 |
int basel, l, spos; |
| 1550 |
basel = SFTextAreaFindLine(st,st->sel_base); |
| 1551 |
l = basel<i ? basel : i; |
| 1552 |
if ( l<st->li.lcnt ) |
| 1553 |
spos = st->li.lineheights[l].start_pos; |
| 1554 |
else |
| 1555 |
spos = basel<i ? st->sel_base : end; |
| 1556 |
st->sel_start = spos; |
| 1557 |
l = basel>i ? basel : i; |
| 1558 |
if ( l+1<st->li.lcnt ) |
| 1559 |
spos = st->li.lineheights[l+1].start_pos; |
| 1560 |
else |
| 1561 |
spos = u_strlen(st->li.text); |
| 1562 |
st->sel_end = spos; |
| 1563 |
} else if ( st->wordsel ) |
| 1564 |
SFTextAreaSelectWords(st,end); |
| 1565 |
else if ( event->u.mouse.button!=2 ) { |
| 1566 |
int e = end; |
| 1567 |
if ( e>st->sel_base ) { |
| 1568 |
st->sel_start = st->sel_base; st->sel_end = e; |
| 1569 |
} else { |
| 1570 |
st->sel_start = e; st->sel_end = st->sel_base; |
| 1571 |
} |
| 1572 |
} |
| 1573 |
if ( event->type==et_mouseup ) { |
| 1574 |
GDrawCancelTimer(st->pressed); st->pressed = NULL; |
| 1575 |
if ( event->u.mouse.button==2 ) |
| 1576 |
SFTextAreaPaste(st,sn_primary); |
| 1577 |
if ( st->sel_start==st->sel_end ) |
| 1578 |
SFTextArea_Show(st,st->sel_start); |
| 1579 |
} |
| 1580 |
if ( st->sel_end > u_strlen( st->li.text )) |
| 1581 |
fprintf( stderr, "About to crash\n" ); |
| 1582 |
if ( refresh ) |
| 1583 |
_ggadget_redraw(g); |
| 1584 |
if ( event->type==et_mouseup && st->changefontcallback ) |
| 1585 |
STChangeCheck(st); |
| 1586 |
return( true ); |
| 1587 |
} |
| 1588 |
return( false ); |
| 1589 |
} |
| 1590 |
|
| 1591 |
static int sftextarea_key(GGadget *g, GEvent *event) { |
| 1592 |
SFTextArea *st = (SFTextArea *) g; |
| 1593 |
int ret; |
| 1594 |
|
| 1595 |
if ( !g->takes_input || (g->state!=gs_enabled && g->state!=gs_active && g->state!=gs_focused )) |
| 1596 |
return( false ); |
| 1597 |
|
| 1598 |
if ( event->type == et_charup ) |
| 1599 |
return( false ); |
| 1600 |
if ( event->u.chr.keysym == GK_F1 || event->u.chr.keysym == GK_Help || |
| 1601 |
(event->u.chr.keysym == GK_Return && !st->accepts_returns ) || |
| 1602 |
( event->u.chr.keysym == GK_Tab && !st->accepts_tabs ) || |
| 1603 |
event->u.chr.keysym == GK_BackTab || event->u.chr.keysym == GK_Escape ) |
| 1604 |
return( false ); |
| 1605 |
|
| 1606 |
if ( !st->hidden_cursor ) { |
| 1607 |
if ( !st->drag_and_drop ) |
| 1608 |
st->old_cursor = GDrawGetCursor(st->g.base); |
| 1609 |
GDrawSetCursor(g->base,ct_invisible); |
| 1610 |
st->hidden_cursor = true; |
| 1611 |
_GWidget_SetGrabGadget(g); |
| 1612 |
} |
| 1613 |
st->cursor_on = false; |
| 1614 |
|
| 1615 |
ret = SFTextAreaDoChange(st,event); |
| 1616 |
if ( st->changefontcallback ) |
| 1617 |
STChangeCheck(st); |
| 1618 |
switch ( ret ) { |
| 1619 |
case 2: |
| 1620 |
break; |
| 1621 |
case true: |
| 1622 |
SFTextAreaChanged(st,-1); |
| 1623 |
break; |
| 1624 |
case false: |
| 1625 |
return( false ); |
| 1626 |
} |
| 1627 |
_ggadget_redraw(g); |
| 1628 |
return( true ); |
| 1629 |
} |
| 1630 |
|
| 1631 |
static int sftextarea_focus(GGadget *g, GEvent *event) { |
| 1632 |
SFTextArea *st = (SFTextArea *) g; |
| 1633 |
if ( st->cursor!=NULL ) { |
| 1634 |
GDrawCancelTimer(st->cursor); |
| 1635 |
st->cursor = NULL; |
| 1636 |
st->cursor_on = false; |
| 1637 |
} |
| 1638 |
if ( st->hidden_cursor && !event->u.focus.gained_focus ) { |
| 1639 |
GDrawSetCursor(st->g.base,st->old_cursor); |
| 1640 |
st->hidden_cursor = false; |
| 1641 |
} |
| 1642 |
st->g.has_focus = event->u.focus.gained_focus; |
| 1643 |
if ( event->u.focus.gained_focus ) { |
| 1644 |
st->cursor = GDrawRequestTimer(st->g.base,400,400,NULL); |
| 1645 |
st->cursor_on = true; |
| 1646 |
if ( event->u.focus.mnemonic_focus != mf_normal ) |
| 1647 |
SFTextAreaSelect(&st->g,0,-1); |
| 1648 |
if ( st->gic!=NULL ) |
| 1649 |
GTPositionGIC(st); |
| 1650 |
} |
| 1651 |
_ggadget_redraw(g); |
| 1652 |
SFTextAreaFocusChanged(st,event->u.focus.gained_focus); |
| 1653 |
return( true ); |
| 1654 |
} |
| 1655 |
|
| 1656 |
static int sftextarea_timer(GGadget *g, GEvent *event) { |
| 1657 |
SFTextArea *st = (SFTextArea *) g; |
| 1658 |
|
| 1659 |
if ( !g->takes_input || (g->state!=gs_enabled && g->state!=gs_active && g->state!=gs_focused )) |
| 1660 |
return(false); |
| 1661 |
if ( st->cursor == event->u.timer.timer && st->sel_start == st->sel_end ) { |
| 1662 |
st->cursor_on = !st->cursor_on; |
| 1663 |
gt_request_redraw_cursor(g->base, st); |
| 1664 |
return( true ); |
| 1665 |
} |
| 1666 |
if ( st->pressed == event->u.timer.timer ) { |
| 1667 |
GEvent e; |
| 1668 |
GDrawSetFont(g->base,st->font); |
| 1669 |
GDrawGetPointerPosition(g->base,&e); |
| 1670 |
if ( (e.u.mouse.x<g->r.x && st->xoff_left>0 ) || |
| 1671 |
(st->multi_line && e.u.mouse.y<g->r.y && st->loff_top>0 ) || |
| 1672 |
( e.u.mouse.x >= g->r.x + g->r.width && |
| 1673 |
st->li.xmax-st->xoff_left>g->inner.width ) || |
| 1674 |
( e.u.mouse.y >= g->r.y + g->r.height && |
| 1675 |
st->li.lineheights[st->li.lcnt-1].y-st->li.lineheights[st->loff_top].y >= g->inner.height )) { |
| 1676 |
int l; |
| 1677 |
int xpos, end; |
| 1678 |
|
| 1679 |
for ( l=st->loff_top; l<st->li.lcnt-1 && e.u.mouse.y-g->inner.y>st->li.lineheights[l+1].y-st->li.lineheights[st->loff_top].y; |
| 1680 |
++l ); |
| 1681 |
if ( e.u.mouse.y<g->r.y && st->loff_top>0 ) |
| 1682 |
l = --st->loff_top; |
| 1683 |
else if ( e.u.mouse.y >= g->r.y + g->r.height && |
| 1684 |
st->li.lineheights[st->li.lcnt-1].y-st->li.lineheights[st->loff_top].y > g->inner.height ) { |
| 1685 |
++st->loff_top; |
| 1686 |
++l; |
| 1687 |
} else if ( l<st->loff_top ) |
| 1688 |
l = st->loff_top; |
| 1689 |
else if ( st->li.lineheights[l].y>=st->li.lineheights[st->loff_top].y + g->inner.height ) { |
| 1690 |
for ( l = st->loff_top+1; st->li.lineheights[l].y<st->li.lineheights[st->loff_top].y+g->inner.height; ++l ); |
| 1691 |
--l; |
| 1692 |
if ( l==st->loff_top ) ++l; |
| 1693 |
} |
| 1694 |
if ( l>=st->li.lcnt ) l = st->li.lcnt-1; |
| 1695 |
|
| 1696 |
xpos = e.u.mouse.x+st->xoff_left; |
| 1697 |
if ( e.u.mouse.x<g->r.x && st->xoff_left>0 ) { |
| 1698 |
st->xoff_left -= st->nw; |
| 1699 |
xpos = g->inner.x + st->xoff_left; |
| 1700 |
} else if ( e.u.mouse.x >= g->r.x + g->r.width && |
| 1701 |
st->li.xmax-st->xoff_left>g->inner.width ) { |
| 1702 |
st->xoff_left += st->nw; |
| 1703 |
xpos = g->inner.x + st->xoff_left + g->inner.width; |
| 1704 |
} |
| 1705 |
|
| 1706 |
end = SFTextAreaGetOffsetFromXPos(st,l,xpos - st->g.inner.x - st->xoff_left); |
| 1707 |
if ( end > st->sel_base ) { |
| 1708 |
st->sel_start = st->sel_base; |
| 1709 |
st->sel_end = end; |
| 1710 |
} else { |
| 1711 |
st->sel_start = end; |
| 1712 |
st->sel_end = st->sel_base; |
| 1713 |
} |
| 1714 |
_ggadget_redraw(g); |
| 1715 |
if ( st->vsb!=NULL ) |
| 1716 |
GScrollBarSetPos(&st->vsb->g,st->li.lineheights[st->loff_top].y); |
| 1717 |
if ( st->hsb!=NULL ) |
| 1718 |
GScrollBarSetPos(&st->hsb->g,st->xoff_left); |
| 1719 |
} |
| 1720 |
return( true ); |
| 1721 |
} |
| 1722 |
return( false ); |
| 1723 |
} |
| 1724 |
|
| 1725 |
static int sftextarea_sel(GGadget *g, GEvent *event) { |
| 1726 |
SFTextArea *st = (SFTextArea *) g; |
| 1727 |
int end; |
| 1728 |
int i; |
| 1729 |
|
| 1730 |
if ( event->type == et_selclear ) { |
| 1731 |
if ( event->u.selclear.sel==sn_primary && st->sel_start!=st->sel_end ) { |
| 1732 |
return( true ); |
| 1733 |
} |
| 1734 |
return( false ); |
| 1735 |
} |
| 1736 |
|
| 1737 |
GDrawSetFont(g->base,st->font); |
| 1738 |
for ( i=st->loff_top ; i<st->li.lcnt-1 && st->li.lineheights[i+1].y-st->li.lineheights[st->loff_top].y< |
| 1739 |
event->u.drag_drop.y-g->inner.y; ++i ); |
| 1740 |
if ( !st->multi_line ) i = 0; |
| 1741 |
if ( i>=st->li.lcnt ) |
| 1742 |
end = u_strlen(st->li.text); |
| 1743 |
else |
| 1744 |
end = SFTextAreaGetOffsetFromXPos(st,i,event->u.drag_drop.x - st->g.inner.x - st->xoff_left); |
| 1745 |
if ( event->type == et_drag ) { |
| 1746 |
gt_set_dd_cursor(st, end); |
| 1747 |
} else if ( event->type == et_dragout ) { |
| 1748 |
|
| 1749 |
|
| 1750 |
} else if ( event->type == et_drop ) { |
| 1751 |
st->sel_start = st->sel_end = st->sel_base = end; |
| 1752 |
SFTextAreaPaste(st,sn_drag_and_drop); |
| 1753 |
SFTextArea_Show(st,st->sel_start); |
| 1754 |
} else |
| 1755 |
return( false ); |
| 1756 |
|
| 1757 |
_ggadget_redraw(&st->g); |
| 1758 |
return( true ); |
| 1759 |
} |
| 1760 |
|
| 1761 |
static void sftextarea_destroy(GGadget *g) { |
| 1762 |
SFTextArea *st = (SFTextArea *) g; |
| 1763 |
|
| 1764 |
if ( st==NULL ) |
| 1765 |
return; |
| 1766 |
|
| 1767 |
if ( st->vsb!=NULL ) |
| 1768 |
(st->vsb->g.funcs->destroy)(&st->vsb->g); |
| 1769 |
if ( st->hsb!=NULL ) |
| 1770 |
(st->hsb->g.funcs->destroy)(&st->hsb->g); |
| 1771 |
GDrawCancelTimer(st->pressed); |
| 1772 |
GDrawCancelTimer(st->cursor); |
| 1773 |
LayoutInfo_Destroy(&st->li); |
| 1774 |
_ggadget_destroy(g); |
| 1775 |
} |
| 1776 |
|
| 1777 |
static void SFTextAreaSetTitle(GGadget *g,const unichar_t *tit) { |
| 1778 |
SFTextArea *st = (SFTextArea *) g; |
| 1779 |
unichar_t *old = st->li.oldtext; |
| 1780 |
if ( u_strcmp(tit,st->li.text)==0 ) |
| 1781 |
return; |
| 1782 |
st->li.oldtext = st->li.text; |
| 1783 |
st->sel_oldstart = st->sel_start; st->sel_oldend = st->sel_end; st->sel_oldbase = st->sel_base; |
| 1784 |
st->li.text = u_copy(tit); |
| 1785 |
free(old); |
| 1786 |
st->sel_start = st->sel_end = st->sel_base = u_strlen(tit); |
| 1787 |
LI_fontlistmergecheck(&st->li); |
| 1788 |
LayoutInfoRefigureLines(&st->li,0,-1,st->g.inner.width); |
| 1789 |
SFTextArea_Show(st,st->sel_start); |
| 1790 |
_ggadget_redraw(g); |
| 1791 |
} |
| 1792 |
|
| 1793 |
static const unichar_t *_SFTextAreaGetTitle(GGadget *g) { |
| 1794 |
SFTextArea *st = (SFTextArea *) g; |
| 1795 |
return( st->li.text ); |
| 1796 |
} |
| 1797 |
|
| 1798 |
static void SFTextAreaSetFont(GGadget *g,FontInstance *new) { |
| 1799 |
SFTextArea *st = (SFTextArea *) g; |
| 1800 |
st->font = new; |
| 1801 |
; |
| 1802 |
} |
| 1803 |
|
| 1804 |
static FontInstance *SFTextAreaGetFont(GGadget *g) { |
| 1805 |
SFTextArea *st = (SFTextArea *) g; |
| 1806 |
return( st->font ); |
| 1807 |
} |
| 1808 |
|
| 1809 |
void SFTextAreaShow(GGadget *g,int pos) { |
| 1810 |
SFTextArea *st = (SFTextArea *) g; |
| 1811 |
|
| 1812 |
SFTextArea_Show(st,pos); |
| 1813 |
_ggadget_redraw(g); |
| 1814 |
} |
| 1815 |
|
| 1816 |
void SFTextAreaSelect(GGadget *g,int start, int end) { |
| 1817 |
SFTextArea *st = (SFTextArea *) g; |
| 1818 |
|
| 1819 |
SFTextAreaGrabPrimarySelection(st); |
| 1820 |
if ( end<0 ) { |
| 1821 |
end = u_strlen(st->li.text); |
| 1822 |
if ( start<0 ) start = end; |
| 1823 |
} |
| 1824 |
if ( start>end ) { int temp = start; start = end; end = temp; } |
| 1825 |
if ( end>u_strlen(st->li.text)) end = u_strlen(st->li.text); |
| 1826 |
if ( start>u_strlen(st->li.text)) start = end; |
| 1827 |
else if ( start<0 ) start=0; |
| 1828 |
st->sel_start = st->sel_base = start; |
| 1829 |
st->sel_end = end; |
| 1830 |
_ggadget_redraw(g); |
| 1831 |
} |
| 1832 |
|
| 1833 |
void SFTextAreaReplace(GGadget *g,const unichar_t *txt) { |
| 1834 |
SFTextArea *st = (SFTextArea *) g; |
| 1835 |
|
| 1836 |
SFTextArea_Replace(st,txt); |
| 1837 |
_ggadget_redraw(g); |
| 1838 |
} |
| 1839 |
|
| 1840 |
static void sftextarea_redraw(GGadget *g) { |
| 1841 |
SFTextArea *st = (SFTextArea *) g; |
| 1842 |
if ( st->vsb!=NULL ) |
| 1843 |
_ggadget_redraw((GGadget *) (st->vsb)); |
| 1844 |
if ( st->hsb!=NULL ) |
| 1845 |
_ggadget_redraw((GGadget *) (st->hsb)); |
| 1846 |
_ggadget_redraw(g); |
| 1847 |
} |
| 1848 |
|
| 1849 |
static void sftextarea_move(GGadget *g, int32_t x, int32_t y ) { |
| 1850 |
SFTextArea *st = (SFTextArea *) g; |
| 1851 |
if ( st->vsb!=NULL ) |
| 1852 |
_ggadget_move((GGadget *) (st->vsb),x+(st->vsb->g.r.x-g->r.x),y); |
| 1853 |
if ( st->hsb!=NULL ) |
| 1854 |
_ggadget_move((GGadget *) (st->hsb),x,y+(st->hsb->g.r.y-g->r.y)); |
| 1855 |
_ggadget_move(g,x,y); |
| 1856 |
} |
| 1857 |
|
| 1858 |
static void sftextarea_resize(GGadget *g, int32_t width, int32_t height ) { |
| 1859 |
SFTextArea *st = (SFTextArea *) g; |
| 1860 |
int gtwidth=width, gtheight=height, oldheight=0; |
| 1861 |
int l; |
| 1862 |
|
| 1863 |
if ( st->hsb!=NULL ) { |
| 1864 |
oldheight = st->hsb->g.r.y+st->hsb->g.r.height-g->r.y; |
| 1865 |
gtheight = height - (oldheight-g->r.height); |
| 1866 |
} |
| 1867 |
if ( st->vsb!=NULL ) { |
| 1868 |
int oldwidth = st->vsb->g.r.x+st->vsb->g.r.width-g->r.x; |
| 1869 |
gtwidth = width - (oldwidth-g->r.width); |
| 1870 |
_ggadget_move((GGadget *) (st->vsb),st->vsb->g.r.x+width-oldwidth,st->vsb->g.r.y); |
| 1871 |
_ggadget_resize((GGadget *) (st->vsb),st->vsb->g.r.width,gtheight); |
| 1872 |
} |
| 1873 |
if ( st->hsb!=NULL ) { |
| 1874 |
_ggadget_move((GGadget *) (st->hsb),st->hsb->g.r.y,st->hsb->g.r.y+height-oldheight); |
| 1875 |
_ggadget_resize((GGadget *) (st->hsb),gtwidth,st->hsb->g.r.height); |
| 1876 |
} |
| 1877 |
_ggadget_resize(g,gtwidth, gtheight); |
| 1878 |
SFTextAreaRefigureLines(st,0,-1); |
| 1879 |
if ( st->vsb!=NULL ) { |
| 1880 |
GScrollBarSetBounds(&st->vsb->g,0,st->li.lineheights[st->li.lcnt-1].y+st->li.lineheights[st->li.lcnt-1].fh,st->g.inner.height); |
| 1881 |
if ( st->loff_top>=st->li.lcnt ) |
| 1882 |
st->loff_top = st->li.lcnt-1; |
| 1883 |
l = st->li.lcnt - SFTextArea_EndPage(st); |
| 1884 |
if ( l<0 ) l = 0; |
| 1885 |
if ( l!=st->loff_top ) { |
| 1886 |
st->loff_top = l; |
| 1887 |
GScrollBarSetPos(&st->vsb->g,st->li.lineheights[l].y); |
| 1888 |
_ggadget_redraw(&st->g); |
| 1889 |
} |
| 1890 |
} |
| 1891 |
SFTextAreaShow(&st->g,st->sel_start); |
| 1892 |
} |
| 1893 |
|
| 1894 |
static GRect *sftextarea_getsize(GGadget *g, GRect *r ) { |
| 1895 |
SFTextArea *st = (SFTextArea *) g; |
| 1896 |
_ggadget_getsize(g,r); |
| 1897 |
if ( st->vsb!=NULL ) |
| 1898 |
r->width = st->vsb->g.r.x+st->vsb->g.r.width-g->r.x; |
| 1899 |
if ( st->hsb!=NULL ) |
| 1900 |
r->height = st->hsb->g.r.y+st->hsb->g.r.height-g->r.y; |
| 1901 |
return( r ); |
| 1902 |
} |
| 1903 |
|
| 1904 |
static void sftextarea_setvisible(GGadget *g, int visible ) { |
| 1905 |
SFTextArea *st = (SFTextArea *) g; |
| 1906 |
if ( st->vsb!=NULL ) _ggadget_setvisible(&st->vsb->g,visible); |
| 1907 |
if ( st->hsb!=NULL ) _ggadget_setvisible(&st->hsb->g,visible); |
| 1908 |
_ggadget_setvisible(g,visible); |
| 1909 |
} |
| 1910 |
|
| 1911 |
static void sftextarea_setenabled(GGadget *g, int enabled ) { |
| 1912 |
SFTextArea *st = (SFTextArea *) g; |
| 1913 |
if ( st->vsb!=NULL ) _ggadget_setenabled(&st->vsb->g,enabled); |
| 1914 |
if ( st->hsb!=NULL ) _ggadget_setenabled(&st->hsb->g,enabled); |
| 1915 |
_ggadget_setenabled(g,enabled); |
| 1916 |
} |
| 1917 |
|
| 1918 |
static int sftextarea_vscroll(GGadget *g, GEvent *event) { |
| 1919 |
enum sb sbt = event->u.control.u.sb.type; |
| 1920 |
SFTextArea *st = (SFTextArea *) (g->data); |
| 1921 |
int loff = st->loff_top; |
| 1922 |
int page; |
| 1923 |
|
| 1924 |
g = (GGadget *) st; |
| 1925 |
|
| 1926 |
if ( sbt==et_sb_top ) |
| 1927 |
loff = 0; |
| 1928 |
else if ( sbt==et_sb_bottom ) { |
| 1929 |
loff = st->li.lcnt; |
| 1930 |
} else if ( sbt==et_sb_up ) { |
| 1931 |
if ( st->loff_top!=0 ) loff = st->loff_top-1; else loff = 0; |
| 1932 |
} else if ( sbt==et_sb_down ) { |
| 1933 |
++loff; |
| 1934 |
} else if ( sbt==et_sb_uppage ) { |
| 1935 |
for ( page=0; st->loff_top-page>=0 && st->li.lineheights[st->loff_top].y-st->li.lineheights[st->loff_top-page].y<=g->inner.height; |
| 1936 |
++page ); |
| 1937 |
if ( --page < 1 ) page = 1; |
| 1938 |
else if ( page>2 ) page-=1; |
| 1939 |
loff = st->loff_top - page; |
| 1940 |
} else if ( sbt==et_sb_downpage ) { |
| 1941 |
for ( page=0; st->loff_top+page<st->li.lcnt && st->li.lineheights[st->loff_top+page].y-st->li.lineheights[st->loff_top].y<=g->inner.height; |
| 1942 |
++page ); |
| 1943 |
if ( --page < 1 ) page = 1; |
| 1944 |
else if ( page>2 ) page-=1; |
| 1945 |
loff = st->loff_top + page; |
| 1946 |
} else { |
| 1947 |
for ( loff = 0; loff<st->li.lcnt && st->li.lineheights[loff].y<event->u.control.u.sb.pos; ++loff ); |
| 1948 |
} |
| 1949 |
for ( page=1; st->li.lcnt-page>=0 && st->li.lineheights[st->li.lcnt-1].y+st->li.lineheights[st->li.lcnt-1].fh-st->li.lineheights[st->li.lcnt-page].y<=g->inner.height; |
| 1950 |
++page ); |
| 1951 |
--page; |
| 1952 |
if ( loff > st->li.lcnt-page ) |
| 1953 |
loff = st->li.lcnt - page; |
| 1954 |
if ( loff<0 ) loff = 0; |
| 1955 |
if ( loff!=st->loff_top ) { |
| 1956 |
st->loff_top = loff; |
| 1957 |
GScrollBarSetPos(&st->vsb->g,st->li.lineheights[loff].y); |
| 1958 |
_ggadget_redraw(&st->g); |
| 1959 |
} |
| 1960 |
return( true ); |
| 1961 |
} |
| 1962 |
|
| 1963 |
static int sftextarea_hscroll(GGadget *g, GEvent *event) { |
| 1964 |
enum sb sbt = event->u.control.u.sb.type; |
| 1965 |
SFTextArea *st = (SFTextArea *) (g->data); |
| 1966 |
int xoff = st->xoff_left; |
| 1967 |
|
| 1968 |
g = (GGadget *) st; |
| 1969 |
|
| 1970 |
if ( sbt==et_sb_top ) |
| 1971 |
xoff = 0; |
| 1972 |
else if ( sbt==et_sb_bottom ) { |
| 1973 |
xoff = st->li.xmax - st->g.inner.width; |
| 1974 |
if ( xoff<0 ) xoff = 0; |
| 1975 |
} else if ( sbt==et_sb_up ) { |
| 1976 |
if ( st->xoff_left>st->nw ) xoff = st->xoff_left-st->nw; else xoff = 0; |
| 1977 |
} else if ( sbt==et_sb_down ) { |
| 1978 |
if ( st->xoff_left + st->nw + st->g.inner.width >= st->li.xmax ) |
| 1979 |
xoff = st->li.xmax - st->g.inner.width; |
| 1980 |
else |
| 1981 |
xoff += st->nw; |
| 1982 |
} else if ( sbt==et_sb_uppage ) { |
| 1983 |
int page = (3*g->inner.width)/4; |
| 1984 |
xoff = st->xoff_left - page; |
| 1985 |
if ( xoff<0 ) xoff=0; |
| 1986 |
} else if ( sbt==et_sb_downpage ) { |
| 1987 |
int page = (3*g->inner.width)/4; |
| 1988 |
xoff = st->xoff_left + page; |
| 1989 |
if ( xoff + st->g.inner.width >= st->li.xmax ) |
| 1990 |
xoff = st->li.xmax - st->g.inner.width; |
| 1991 |
} else { |
| 1992 |
xoff = event->u.control.u.sb.pos; |
| 1993 |
} |
| 1994 |
if ( xoff + st->g.inner.width >= st->li.xmax ) |
| 1995 |
xoff = st->li.xmax - st->g.inner.width; |
| 1996 |
if ( xoff<0 ) xoff = 0; |
| 1997 |
if ( st->xoff_left!=xoff ) { |
| 1998 |
st->xoff_left = xoff; |
| 1999 |
GScrollBarSetPos(&st->hsb->g,xoff); |
| 2000 |
_ggadget_redraw(&st->g); |
| 2001 |
} |
| 2002 |
return( true ); |
| 2003 |
} |
| 2004 |
|
| 2005 |
static void SFTextFieldSetDesiredSize(GGadget *g,GRect *outer,GRect *inner) { |
| 2006 |
SFTextArea *gt = (SFTextArea *) g; |
| 2007 |
|
| 2008 |
if ( outer!=NULL ) { |
| 2009 |
g->desired_width = outer->width; |
| 2010 |
g->desired_height = outer->height; |
| 2011 |
} else if ( inner!=NULL ) { |
| 2012 |
int bp = GBoxBorderWidth(g->base,g->box); |
| 2013 |
int extra=0; |
| 2014 |
g->desired_width = inner->width + 2*bp + extra; |
| 2015 |
g->desired_height = inner->height + 2*bp; |
| 2016 |
if ( gt->multi_line ) { |
| 2017 |
int sbadd = GDrawPointsToPixels(gt->g.base,_GScrollBar_Width) + |
| 2018 |
GDrawPointsToPixels(gt->g.base,1); |
| 2019 |
g->desired_width += sbadd; |
| 2020 |
if ( !gt->li.wrap ) |
| 2021 |
g->desired_height += sbadd; |
| 2022 |
} |
| 2023 |
} |
| 2024 |
} |
| 2025 |
|
| 2026 |
static void SFTextFieldGetDesiredSize(GGadget *g,GRect *outer,GRect *inner) { |
| 2027 |
SFTextArea *gt = (SFTextArea *) g; |
| 2028 |
int width=0, height; |
| 2029 |
int extra=0; |
| 2030 |
int bp = GBoxBorderWidth(g->base,g->box); |
| 2031 |
|
| 2032 |
width = GGadgetScale(GDrawPointsToPixels(gt->g.base,80)); |
| 2033 |
height = gt->multi_line? 4*gt->fh:gt->fh; |
| 2034 |
|
| 2035 |
if ( g->desired_width>extra+2*bp ) width = g->desired_width - extra - 2*bp; |
| 2036 |
if ( g->desired_height>2*bp ) height = g->desired_height - 2*bp; |
| 2037 |
|
| 2038 |
if ( gt->multi_line ) { |
| 2039 |
int sbadd = GDrawPointsToPixels(gt->g.base,_GScrollBar_Width) + |
| 2040 |
GDrawPointsToPixels(gt->g.base,1); |
| 2041 |
width += sbadd; |
| 2042 |
if ( !gt->li.wrap ) |
| 2043 |
height += sbadd; |
| 2044 |
} |
| 2045 |
|
| 2046 |
if ( inner!=NULL ) { |
| 2047 |
inner->x = inner->y = 0; |
| 2048 |
inner->width = width; |
| 2049 |
inner->height = height; |
| 2050 |
} |
| 2051 |
if ( outer!=NULL ) { |
| 2052 |
outer->x = outer->y = 0; |
| 2053 |
outer->width = width + extra + 2*bp; |
| 2054 |
outer->height = height + 2*bp; |
| 2055 |
} |
| 2056 |
} |
| 2057 |
|
| 2058 |
static int SFtextfield_FillsWindow(GGadget *g) { |
| 2059 |
return( ((SFTextArea *) g)->multi_line && g->prev==NULL && |
| 2060 |
(_GWidgetGetGadgets(g->base)==g || |
| 2061 |
_GWidgetGetGadgets(g->base)==(GGadget *) ((SFTextArea *) g)->vsb || |
| 2062 |
_GWidgetGetGadgets(g->base)==(GGadget *) ((SFTextArea *) g)->hsb )); |
| 2063 |
} |
| 2064 |
|
| 2065 |
struct gfuncs sftextarea_funcs = { |
| 2066 |
0, |
| 2067 |
sizeof(struct gfuncs), |
| 2068 |
|
| 2069 |
sftextarea_expose, |
| 2070 |
sftextarea_mouse, |
| 2071 |
sftextarea_key, |
| 2072 |
_sftextarea_editcmd, |
| 2073 |
sftextarea_focus, |
| 2074 |
sftextarea_timer, |
| 2075 |
sftextarea_sel, |
| 2076 |
|
| 2077 |
sftextarea_redraw, |
| 2078 |
sftextarea_move, |
| 2079 |
sftextarea_resize, |
| 2080 |
sftextarea_setvisible, |
| 2081 |
sftextarea_setenabled, |
| 2082 |
sftextarea_getsize, |
| 2083 |
_ggadget_getinnersize, |
| 2084 |
|
| 2085 |
sftextarea_destroy, |
| 2086 |
|
| 2087 |
SFTextAreaSetTitle, |
| 2088 |
_SFTextAreaGetTitle, |
| 2089 |
NULL, |
| 2090 |
NULL, |
| 2091 |
NULL, |
| 2092 |
SFTextAreaSetFont, |
| 2093 |
SFTextAreaGetFont, |
| 2094 |
|
| 2095 |
NULL, |
| 2096 |
NULL, |
| 2097 |
NULL, |
| 2098 |
NULL, |
| 2099 |
NULL, |
| 2100 |
NULL, |
| 2101 |
NULL, |
| 2102 |
NULL, |
| 2103 |
NULL, |
| 2104 |
NULL, |
| 2105 |
NULL, |
| 2106 |
|
| 2107 |
SFTextFieldGetDesiredSize, |
| 2108 |
SFTextFieldSetDesiredSize, |
| 2109 |
SFtextfield_FillsWindow, |
| 2110 |
NULL |
| 2111 |
}; |
| 2112 |
|
| 2113 |
static void SFTextAreaAddVSb(SFTextArea *st) { |
| 2114 |
GGadgetData gd; |
| 2115 |
|
| 2116 |
memset(&gd,'\0',sizeof(gd)); |
| 2117 |
gd.pos.y = st->g.r.y; gd.pos.height = st->g.r.height; |
| 2118 |
gd.pos.width = GDrawPointsToPixels(st->g.base,_GScrollBar_Width); |
| 2119 |
gd.pos.x = st->g.r.x+st->g.r.width - gd.pos.width; |
| 2120 |
gd.flags = gg_visible|gg_enabled|gg_pos_in_pixels|gg_sb_vert; |
| 2121 |
gd.handle_controlevent = sftextarea_vscroll; |
| 2122 |
st->vsb = (GScrollBar *) GScrollBarCreate(st->g.base,&gd,st); |
| 2123 |
st->vsb->g.contained = true; |
| 2124 |
|
| 2125 |
gd.pos.width += GDrawPointsToPixels(st->g.base,1); |
| 2126 |
st->g.r.width -= gd.pos.width; |
| 2127 |
st->g.inner.width -= gd.pos.width; |
| 2128 |
} |
| 2129 |
|
| 2130 |
static void SFTextAreaAddHSb(SFTextArea *st) { |
| 2131 |
GGadgetData gd; |
| 2132 |
|
| 2133 |
memset(&gd,'\0',sizeof(gd)); |
| 2134 |
gd.pos.x = st->g.r.x; gd.pos.width = st->g.r.width; |
| 2135 |
gd.pos.height = GDrawPointsToPixels(st->g.base,_GScrollBar_Width); |
| 2136 |
gd.pos.y = st->g.r.y+st->g.r.height - gd.pos.height; |
| 2137 |
gd.flags = gg_visible|gg_enabled|gg_pos_in_pixels; |
| 2138 |
gd.handle_controlevent = sftextarea_hscroll; |
| 2139 |
st->hsb = (GScrollBar *) GScrollBarCreate(st->g.base,&gd,st); |
| 2140 |
st->hsb->g.contained = true; |
| 2141 |
|
| 2142 |
gd.pos.height += GDrawPointsToPixels(st->g.base,1); |
| 2143 |
st->g.r.height -= gd.pos.height; |
| 2144 |
st->g.inner.height -= gd.pos.height; |
| 2145 |
if ( st->vsb!=NULL ) { |
| 2146 |
st->vsb->g.r.height -= gd.pos.height; |
| 2147 |
st->vsb->g.inner.height -= gd.pos.height; |
| 2148 |
} |
| 2149 |
} |
| 2150 |
|
| 2151 |
static void SFTextAreaFit(SFTextArea *st) { |
| 2152 |
GTextBounds bounds; |
| 2153 |
int as=0, ds, ld, fh=0, temp; |
| 2154 |
GRect needed; |
| 2155 |
int extra=0; |
| 2156 |
|
| 2157 |
needed.x = needed.y = 0; |
| 2158 |
needed.width = needed.height = 1; |
| 2159 |
|
| 2160 |
{ |
| 2161 |
FontInstance *old = GDrawSetFont(st->g.base,st->font); |
| 2162 |
(void) GDrawGetTextBounds(st->g.base,st->li.text, -1, &bounds); |
| 2163 |
GDrawWindowFontMetrics(st->g.base,st->font,&as, &ds, &ld); |
| 2164 |
if ( as<bounds.as ) as = bounds.as; |
| 2165 |
if ( ds<bounds.ds ) ds = bounds.ds; |
| 2166 |
st->fh = fh = as+ds; |
| 2167 |
st->as = as; |
| 2168 |
st->nw = 6; |
| 2169 |
GDrawSetFont(st->g.base,old); |
| 2170 |
} |
| 2171 |
|
| 2172 |
temp = GGadgetScale(GDrawPointsToPixels(st->g.base,80))+extra; |
| 2173 |
|
| 2174 |
if ( st->g.r.width==0 || st->g.r.height==0 ) { |
| 2175 |
int bp = GBoxBorderWidth(st->g.base,st->g.box); |
| 2176 |
needed.x = needed.y = 0; |
| 2177 |
needed.width = temp; |
| 2178 |
needed.height = st->multi_line? 4*fh:fh; |
| 2179 |
_ggadgetFigureSize(st->g.base,st->g.box,&needed,false); |
| 2180 |
if ( st->g.r.width==0 ) { |
| 2181 |
st->g.r.width = needed.width; |
| 2182 |
st->g.inner.width = temp-extra; |
| 2183 |
st->g.inner.x = st->g.r.x + (needed.width-temp)/2; |
| 2184 |
} else { |
| 2185 |
st->g.inner.x = st->g.r.x + bp; |
| 2186 |
st->g.inner.width = st->g.r.width - 2*bp; |
| 2187 |
} |
| 2188 |
if ( st->g.r.height==0 ) { |
| 2189 |
st->g.r.height = needed.height; |
| 2190 |
st->g.inner.height = st->multi_line? 4*fh:fh; |
| 2191 |
st->g.inner.y = st->g.r.y + (needed.height-st->g.inner.height)/2; |
| 2192 |
} else { |
| 2193 |
st->g.inner.y = st->g.r.y + bp; |
| 2194 |
st->g.inner.height = st->g.r.height - 2*bp; |
| 2195 |
} |
| 2196 |
if ( st->multi_line ) { |
| 2197 |
int sbadd = GDrawPointsToPixels(st->g.base,_GScrollBar_Width) + |
| 2198 |
GDrawPointsToPixels(st->g.base,1); |
| 2199 |
{ |
| 2200 |
st->g.r.width += sbadd; |
| 2201 |
st->g.inner.width += sbadd; |
| 2202 |
} |
| 2203 |
if ( !st->li.wrap ) { |
| 2204 |
st->g.r.height += sbadd; |
| 2205 |
st->g.inner.height += sbadd; |
| 2206 |
} |
| 2207 |
} |
| 2208 |
} else { |
| 2209 |
int bp = GBoxBorderWidth(st->g.base,st->g.box); |
| 2210 |
st->g.inner = st->g.r; |
| 2211 |
st->g.inner.x += bp; st->g.inner.y += bp; |
| 2212 |
st->g.inner.width -= 2*bp-extra; st->g.inner.height -= 2*bp; |
| 2213 |
} |
| 2214 |
if ( st->multi_line ) { |
| 2215 |
SFTextAreaAddVSb(st); |
| 2216 |
if ( !st->li.wrap ) |
| 2217 |
SFTextAreaAddHSb(st); |
| 2218 |
} |
| 2219 |
} |
| 2220 |
|
| 2221 |
static SFTextArea *_SFTextAreaCreate(SFTextArea *st, struct gwindow *base, GGadgetData *gd,void *data, GBox *def) { |
| 2222 |
|
| 2223 |
GResEditDoInit(&sftextarea_ri); |
| 2224 |
st->g.funcs = &sftextarea_funcs; |
| 2225 |
_GGadget_Create(&st->g,base,gd,data,def); |
| 2226 |
|
| 2227 |
st->g.takes_input = true; st->g.takes_keyboard = true; st->g.focusable = true; |
| 2228 |
if ( gd->label!=NULL ) { |
| 2229 |
if ( gd->label->text_is_1byte ) |
| 2230 |
st->li.text = utf82u_copy((char *) gd->label->text); |
| 2231 |
else |
| 2232 |
st->li.text = u_copy(gd->label->text); |
| 2233 |
st->sel_start = st->sel_end = st->sel_base = u_strlen(st->li.text); |
| 2234 |
} |
| 2235 |
if ( st->li.text==NULL ) |
| 2236 |
st->li.text = calloc(1,sizeof(unichar_t)); |
| 2237 |
st->font = sftextarea_font.fi; |
| 2238 |
if ( gd->label!=NULL && gd->label->font!=NULL ) |
| 2239 |
st->font = gd->label->font; |
| 2240 |
SFTextAreaFit(st); |
| 2241 |
_GGadget_FinalPosition(&st->g,base,gd); |
| 2242 |
SFTextAreaRefigureLines(st,0,-1); |
| 2243 |
|
| 2244 |
if ( gd->flags & gg_group_end ) |
| 2245 |
_GGadgetCloseGroup(&st->g); |
| 2246 |
GWidgetIndicateFocusGadget(&st->g); |
| 2247 |
if ( gd->flags & gg_text_xim ) |
| 2248 |
st->gic = GDrawCreateInputContext(base,gic_overspot|gic_orlesser); |
| 2249 |
return( st ); |
| 2250 |
} |
| 2251 |
|
| 2252 |
GGadget *SFTextAreaCreate(struct gwindow *base, GGadgetData *gd,void *data) { |
| 2253 |
SFTextArea *st = calloc(1,sizeof(SFTextArea)); |
| 2254 |
st->multi_line = true; |
| 2255 |
st->accepts_returns = true; |
| 2256 |
st->li.wrap = true; |
| 2257 |
_SFTextAreaCreate(st,base,gd,data,&sftextarea_box); |
| 2258 |
st->li.dpi = 100; |
| 2259 |
|
| 2260 |
return( &st->g ); |
| 2261 |
} |
| 2262 |
|
| 2263 |
static int SFTF_NormalizeStartEnd(SFTextArea *st, int start, int *_end) { |
| 2264 |
int end = *_end; |
| 2265 |
int len = u_strlen(st->li.text); |
| 2266 |
|
| 2267 |
if ( st->li.generated==NULL ) { |
| 2268 |
start = 0; |
| 2269 |
end = len; |
| 2270 |
} else if ( start==-1 ) { |
| 2271 |
start = st->sel_start; |
| 2272 |
end = st->sel_end; |
| 2273 |
} else if ( end==-1 ) |
| 2274 |
end = len; |
| 2275 |
if ( end>len ) end = len; |
| 2276 |
if ( start<0 ) start = 0; |
| 2277 |
if ( start>end ) start = end; |
| 2278 |
*_end = end; |
| 2279 |
return( start ); |
| 2280 |
} |
| 2281 |
|
| 2282 |
static void SFTFMetaChangeCleanup(SFTextArea *st,int start, int end) { |
| 2283 |
LI_fontlistmergecheck(&st->li); |
| 2284 |
SFTextAreaRefigureLines(st, start,end); |
| 2285 |
GDrawRequestExpose(st->g.base,&st->g.inner,false); |
| 2286 |
if ( st->changefontcallback != NULL ) |
| 2287 |
STChangeCheck(st); |
| 2288 |
} |
| 2289 |
|
| 2290 |
int SFTFSetFont(GGadget *g, int start, int end, SplineFont *sf) { |
| 2291 |
SFTextArea *st = (SFTextArea *) g; |
| 2292 |
FontData *cur; |
| 2293 |
struct fontlist *fl; |
| 2294 |
|
| 2295 |
start = SFTF_NormalizeStartEnd(st, start, &end); |
| 2296 |
fl = LI_BreakFontList(&st->li,start,end); |
| 2297 |
while ( fl!=NULL && fl->end<=end ) { |
| 2298 |
if ( fl->fd->sf!=sf ) { |
| 2299 |
cur = LI_FindFontData(&st->li, sf, fl->fd->layer, fl->fd->fonttype, fl->fd->pointsize, fl->fd->antialias); |
| 2300 |
if ( cur!=NULL ) |
| 2301 |
fl->fd = cur; |
| 2302 |
} |
| 2303 |
fl = fl->next; |
| 2304 |
} |
| 2305 |
|
| 2306 |
SFTFMetaChangeCleanup(st,start,end); |
| 2307 |
return( true ); |
| 2308 |
} |
| 2309 |
|
| 2310 |
int SFTFSetFontType(GGadget *g, int start, int end, enum sftf_fonttype fonttype) { |
| 2311 |
SFTextArea *st = (SFTextArea *) g; |
| 2312 |
FontData *cur; |
| 2313 |
struct fontlist *fl; |
| 2314 |
|
| 2315 |
start = SFTF_NormalizeStartEnd(st, start, &end); |
| 2316 |
fl = LI_BreakFontList(&st->li,start,end); |
| 2317 |
while ( fl!=NULL && fl->end<=end ) { |
| 2318 |
if ( fl->fd->fonttype!=fonttype ) { |
| 2319 |
cur = LI_FindFontData(&st->li, fl->fd->sf, fl->fd->layer, fonttype, fl->fd->pointsize, fl->fd->antialias); |
| 2320 |
if ( cur!=NULL ) |
| 2321 |
fl->fd = cur; |
| 2322 |
} |
| 2323 |
fl = fl->next; |
| 2324 |
} |
| 2325 |
|
| 2326 |
SFTFMetaChangeCleanup(st,start,end); |
| 2327 |
return( true ); |
| 2328 |
} |
| 2329 |
|
| 2330 |
int SFTFSetSize(GGadget *g, int start, int end, int pointsize) { |
| 2331 |
SFTextArea *st = (SFTextArea *) g; |
| 2332 |
FontData *cur; |
| 2333 |
struct fontlist *fl; |
| 2334 |
|
| 2335 |
if ( st->li.generated==NULL ) |
| 2336 |
return( false ); |
| 2337 |
start = SFTF_NormalizeStartEnd(st, start, &end); |
| 2338 |
fl = LI_BreakFontList(&st->li,start,end); |
| 2339 |
while ( fl!=NULL && fl->end<=end ) { |
| 2340 |
if ( fl->fd->pointsize!=pointsize ) { |
| 2341 |
cur = LI_FindFontData(&st->li, fl->fd->sf, fl->fd->layer, fl->fd->fonttype, pointsize, fl->fd->antialias); |
| 2342 |
if ( cur!=NULL ) |
| 2343 |
fl->fd = cur; |
| 2344 |
} |
| 2345 |
fl = fl->next; |
| 2346 |
} |
| 2347 |
|
| 2348 |
SFTFMetaChangeCleanup(st,start,end); |
| 2349 |
return( true ); |
| 2350 |
} |
| 2351 |
|
| 2352 |
int SFTFSetAntiAlias(GGadget *g, int start, int end, int antialias) { |
| 2353 |
SFTextArea *st = (SFTextArea *) g; |
| 2354 |
FontData *cur; |
| 2355 |
struct fontlist *fl; |
| 2356 |
|
| 2357 |
start = SFTF_NormalizeStartEnd(st, start, &end); |
| 2358 |
fl = LI_BreakFontList(&st->li,start,end); |
| 2359 |
while ( fl!=NULL && fl->end<=end ) { |
| 2360 |
if ( fl->fd->antialias!=antialias ) { |
| 2361 |
cur = LI_FindFontData(&st->li, fl->fd->sf, fl->fd->layer, fl->fd->fonttype, fl->fd->pointsize, antialias); |
| 2362 |
if ( cur!=NULL ) |
| 2363 |
fl->fd = cur; |
| 2364 |
} |
| 2365 |
fl = fl->next; |
| 2366 |
} |
| 2367 |
|
| 2368 |
SFTFMetaChangeCleanup(st,start,end); |
| 2369 |
return( true ); |
| 2370 |
} |
| 2371 |
|
| 2372 |
int SFTFSetScriptLang(GGadget *g, int start, int end, uint32_t script, uint32_t lang) { |
| 2373 |
SFTextArea *st = (SFTextArea *) g; |
| 2374 |
struct fontlist *fl; |
| 2375 |
|
| 2376 |
start = SFTF_NormalizeStartEnd(st, start, &end); |
| 2377 |
fl = LI_BreakFontList(&st->li,start,end); |
| 2378 |
while ( fl!=NULL && fl->end<=end ) { |
| 2379 |
if ( fl->script != script ) { |
| 2380 |
free(fl->feats); |
| 2381 |
fl->feats = LI_TagsCopy(StdFeaturesOfScript(script)); |
| 2382 |
} |
| 2383 |
fl->script = script; |
| 2384 |
fl->lang = lang; |
| 2385 |
fl = fl->next; |
| 2386 |
} |
| 2387 |
|
| 2388 |
SFTFMetaChangeCleanup(st,start,end); |
| 2389 |
return( true ); |
| 2390 |
} |
| 2391 |
|
| 2392 |
int SFTFSetFeatures(GGadget *g, int start, int end, uint32_t *features) { |
| 2393 |
SFTextArea *st = (SFTextArea *) g; |
| 2394 |
struct fontlist *fl; |
| 2395 |
|
| 2396 |
start = SFTF_NormalizeStartEnd(st, start, &end); |
| 2397 |
fl = LI_BreakFontList(&st->li,start,end); |
| 2398 |
while ( fl!=NULL && fl->end<=end ) { |
| 2399 |
free(fl->feats); |
| 2400 |
fl->feats = LI_TagsCopy(features); |
| 2401 |
fl = fl->next; |
| 2402 |
} |
| 2403 |
|
| 2404 |
SFTFMetaChangeCleanup(st,start,end); |
| 2405 |
return( true ); |
| 2406 |
} |
| 2407 |
|
| 2408 |
void SFTFRegisterCallback(GGadget *g, void *cbcontext, |
| 2409 |
void (*changefontcallback)(void *,SplineFont *,enum sftf_fonttype,int size,int aa, uint32_t script, uint32_t lang, uint32_t *feats)) { |
| 2410 |
SFTextArea *st = (SFTextArea *) g; |
| 2411 |
|
| 2412 |
st->cbcontext = cbcontext; |
| 2413 |
st->changefontcallback = changefontcallback; |
| 2414 |
} |
| 2415 |
|
| 2416 |
void SFTFProvokeCallback(GGadget *g) { |
| 2417 |
SFTextArea *st = (SFTextArea *) g; |
| 2418 |
STChangeCheck(st); |
| 2419 |
} |
| 2420 |
|
| 2421 |
void SFTFSetDPI(GGadget *g, float dpi) { |
| 2422 |
SFTextArea *st = (SFTextArea *) g; |
| 2423 |
FontData *fd; |
| 2424 |
|
| 2425 |
if ( st->li.dpi == dpi ) |
| 2426 |
return; |
| 2427 |
st->li.dpi = dpi; |
| 2428 |
for ( fd = st->li.generated; fd!=NULL; fd=fd->next ) { |
| 2429 |
LI_RegenFontData(&st->li,fd); |
| 2430 |
} |
| 2431 |
SFTextAreaRefigureLines(st,0,-1); |
| 2432 |
SFTextAreaShow(&st->g,st->sel_start); |
| 2433 |
|
| 2434 |
} |
| 2435 |
|
| 2436 |
float SFTFGetDPI(GGadget *g) { |
| 2437 |
SFTextArea *st = (SFTextArea *) g; |
| 2438 |
|
| 2439 |
return( st->li.dpi ); |
| 2440 |
} |
| 2441 |
|
| 2442 |
void SFTFRefreshFonts(GGadget *g) { |
| 2443 |
SFTextArea *st = (SFTextArea *) g; |
| 2444 |
FontData *fd; |
| 2445 |
struct sfmaps *sfmaps; |
| 2446 |
|
| 2447 |
|
| 2448 |
for ( sfmaps = st->li.sfmaps; sfmaps!=NULL; sfmaps = sfmaps->next ) { |
| 2449 |
EncMapFree(sfmaps->map); |
| 2450 |
SplineCharFree(sfmaps->fake_notdef); |
| 2451 |
sfmaps->fake_notdef = NULL; |
| 2452 |
SFMapFill(sfmaps,sfmaps->sf); |
| 2453 |
} |
| 2454 |
|
| 2455 |
|
| 2456 |
|
| 2457 |
for ( fd = st->li.generated; fd!=NULL; fd=fd->next ) { |
| 2458 |
if ( fd->depends_on ) |
| 2459 |
fd->bdf->freetype_context = NULL; |
| 2460 |
if ( fd->fonttype!=sftf_bitmap ) { |
| 2461 |
BDFFontFree(fd->bdf); |
| 2462 |
fd->bdf = NULL; |
| 2463 |
} |
| 2464 |
} |
| 2465 |
for ( fd = st->li.generated; fd!=NULL; fd=fd->next ) { |
| 2466 |
LI_RegenFontData(&st->li,fd); |
| 2467 |
} |
| 2468 |
LayoutInfoRefigureLines(&st->li,0,-1,st->g.inner.width); |
| 2469 |
SFTextAreaShow(&st->g,st->sel_start); |
| 2470 |
|
| 2471 |
} |