| 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 "gdraw.h" |
| 31 |
#include "ggadgetP.h" |
| 32 |
#include "gkeysym.h" |
| 33 |
#include "gwidget.h" |
| 34 |
#include "ustring.h" |
| 35 |
#include "utype.h" |
| 36 |
|
| 37 |
#include <assert.h> |
| 38 |
#include <math.h> |
| 39 |
|
| 40 |
GBox _GGadget_gtextfield_box = GBOX_EMPTY; |
| 41 |
static GBox glistfield_box = GBOX_EMPTY; |
| 42 |
static GBox glistfieldmenu_box = GBOX_EMPTY; |
| 43 |
static GBox gnumericfield_box = GBOX_EMPTY; |
| 44 |
static GBox gnumericfieldspinner_box = GBOX_EMPTY; |
| 45 |
GResFont _gtextfield_font = GRESFONT_INIT("400 10pt " MONO_UI_FAMILIES); |
| 46 |
|
| 47 |
static GResInfo listfield_ri, listfieldmenu_ri, numericfield_ri, numericfieldspinner_ri; |
| 48 |
static GTextInfo text_lab[] = { |
| 49 |
{ (unichar_t *) "Disabled", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' }, |
| 50 |
{ (unichar_t *) "Enabled" , NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' } |
| 51 |
}; |
| 52 |
static GTextInfo list_choices[] = { |
| 53 |
{ (unichar_t *) "1", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' }, |
| 54 |
{ (unichar_t *) "2", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' }, |
| 55 |
{ (unichar_t *) "3", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' }, |
| 56 |
GTEXTINFO_EMPTY |
| 57 |
}; |
| 58 |
static GGadgetCreateData text_gcd[] = { |
| 59 |
{ GTextFieldCreate, { { 0, 0, 70, 0 }, NULL, 0, 0, 0, 0, 0, &text_lab[0], { NULL }, gg_visible, NULL, NULL }, NULL, NULL }, |
| 60 |
{ GTextFieldCreate, { { 0, 0, 70, 0 }, NULL, 0, 0, 0, 0, 0, &text_lab[1], { NULL }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL } |
| 61 |
}; |
| 62 |
static GGadgetCreateData *tarray[] = { GCD_Glue, &text_gcd[0], GCD_Glue, &text_gcd[1], GCD_Glue, NULL, NULL }; |
| 63 |
static GGadgetCreateData textbox = |
| 64 |
{ GHVGroupCreate, { { 2, 2, 0, 0 }, NULL, 0, 0, 0, 0, 0, NULL, { (GTextInfo *) tarray }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL }; |
| 65 |
GResInfo gtextfield_ri = { |
| 66 |
&listfield_ri, &ggadget_ri,NULL, NULL, |
| 67 |
&_GGadget_gtextfield_box, |
| 68 |
&_gtextfield_font, |
| 69 |
&textbox, |
| 70 |
NULL, |
| 71 |
N_("Text Field"), |
| 72 |
N_("Text Field"), |
| 73 |
"GTextField", |
| 74 |
"Gdraw", |
| 75 |
false, |
| 76 |
false, |
| 77 |
omf_padding, |
| 78 |
{ 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 79 |
GBOX_EMPTY, |
| 80 |
NULL, |
| 81 |
NULL, |
| 82 |
NULL |
| 83 |
}; |
| 84 |
static GGadgetCreateData textlist_gcd[] = { |
| 85 |
{ GListFieldCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &text_lab[0], { list_choices }, gg_visible, NULL, NULL }, NULL, NULL }, |
| 86 |
{ GListFieldCreate, { GRECT_EMPTY, NULL, 0, 0, 0, 0, 0, &text_lab[1], { list_choices }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL } |
| 87 |
}; |
| 88 |
static GGadgetCreateData *tlarray[] = { GCD_Glue, &textlist_gcd[0], GCD_Glue, &textlist_gcd[1], GCD_Glue, NULL, NULL }; |
| 89 |
static GGadgetCreateData textlistbox = |
| 90 |
{ GHVGroupCreate, { { 2, 2, 0, 0 }, NULL, 0, 0, 0, 0, 0, NULL, { (GTextInfo *) tlarray }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL }; |
| 91 |
static GResInfo listfield_ri = { |
| 92 |
&listfieldmenu_ri,>extfield_ri,&listfieldmenu_ri, &listmark_ri, |
| 93 |
&glistfield_box, |
| 94 |
NULL, |
| 95 |
&textlistbox, |
| 96 |
NULL, |
| 97 |
N_("List Field"), |
| 98 |
N_("List Field (Combo Box)"), |
| 99 |
"GComboBox", |
| 100 |
"Gdraw", |
| 101 |
false, |
| 102 |
false, |
| 103 |
0, |
| 104 |
GBOX_EMPTY, |
| 105 |
GBOX_EMPTY, |
| 106 |
NULL, |
| 107 |
NULL, |
| 108 |
NULL |
| 109 |
}; |
| 110 |
static GResInfo listfieldmenu_ri = { |
| 111 |
&numericfield_ri, &listfield_ri, &listmark_ri,NULL, |
| 112 |
&glistfieldmenu_box, |
| 113 |
NULL, |
| 114 |
&textlistbox, |
| 115 |
NULL, |
| 116 |
N_("List Field Menu"), |
| 117 |
N_("Box surrounding the ListMark in a list field (combobox)"), |
| 118 |
"GComboBoxMenu", |
| 119 |
"Gdraw", |
| 120 |
false, |
| 121 |
false, |
| 122 |
omf_padding, |
| 123 |
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 124 |
GBOX_EMPTY, |
| 125 |
NULL, |
| 126 |
NULL, |
| 127 |
NULL |
| 128 |
}; |
| 129 |
static GGadgetCreateData num_gcd[] = { |
| 130 |
{ GNumericFieldCreate, { { 0, 0, 50, 0 }, NULL, 0, 0, 0, 0, 0, &list_choices[0], { NULL }, gg_visible, NULL, NULL }, NULL, NULL }, |
| 131 |
{ GNumericFieldCreate, { { 0, 0, 50, 0 }, NULL, 0, 0, 0, 0, 0, &list_choices[0], { NULL }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL } |
| 132 |
}; |
| 133 |
static GGadgetCreateData *narray[] = { GCD_Glue, &num_gcd[0], GCD_Glue, &num_gcd[1], GCD_Glue, NULL, NULL }; |
| 134 |
static GGadgetCreateData numbox = |
| 135 |
{ GHVGroupCreate, { { 2, 2, 0, 0 }, NULL, 0, 0, 0, 0, 0, NULL, { (GTextInfo *) narray }, gg_visible|gg_enabled, NULL, NULL }, NULL, NULL }; |
| 136 |
static GResInfo numericfield_ri = { |
| 137 |
&numericfieldspinner_ri, >extfield_ri,&numericfieldspinner_ri, NULL, |
| 138 |
&gnumericfield_box, |
| 139 |
NULL, |
| 140 |
&numbox, |
| 141 |
NULL, |
| 142 |
N_("Numeric Field"), |
| 143 |
N_("Numeric Field (Spinner)"), |
| 144 |
"GNumericField", |
| 145 |
"Gdraw", |
| 146 |
false, |
| 147 |
false, |
| 148 |
0, |
| 149 |
GBOX_EMPTY, |
| 150 |
GBOX_EMPTY, |
| 151 |
NULL, |
| 152 |
NULL, |
| 153 |
NULL |
| 154 |
}; |
| 155 |
extern GResInfo glist_ri; |
| 156 |
static GResInfo numericfieldspinner_ri = { |
| 157 |
&glist_ri, &numericfield_ri,NULL, NULL, |
| 158 |
&gnumericfieldspinner_box, |
| 159 |
NULL, |
| 160 |
&numbox, |
| 161 |
NULL, |
| 162 |
N_("Numeric Field Sign"), |
| 163 |
N_("The box around the up/down arrows of a numeric field (spinner)"), |
| 164 |
"GNumericFieldSpinner", |
| 165 |
"Gdraw", |
| 166 |
false, |
| 167 |
false, |
| 168 |
omf_border_type|omf_border_width|omf_padding, |
| 169 |
{ bt_none, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 170 |
GBOX_EMPTY, |
| 171 |
NULL, |
| 172 |
NULL, |
| 173 |
NULL |
| 174 |
}; |
| 175 |
|
| 176 |
static unichar_t nullstr[] = { 0 }, nstr[] = { 'n', 0 }, |
| 177 |
newlinestr[] = { '\n', 0 }, tabstr[] = { '\t', 0 }; |
| 178 |
|
| 179 |
static void GListFieldSelected(GGadget *g, int i); |
| 180 |
static int GTextField_Show(GTextField *gt, int pos); |
| 181 |
static void GTPositionGIC(GTextField *gt); |
| 182 |
|
| 183 |
static void GCompletionDestroy(GCompletionField *gc); |
| 184 |
static void GTextFieldComplete(GTextField *gt,int from_tab); |
| 185 |
static int GCompletionHandleKey(GTextField *gt,GEvent *event); |
| 186 |
|
| 187 |
|
| 188 |
static int u2utf8_index(int pos,const char *start) { |
| 189 |
const char *pt = start; |
| 190 |
|
| 191 |
while ( --pos>=0 ) |
| 192 |
utf8_ildb(&pt); |
| 193 |
return( pt-start ); |
| 194 |
} |
| 195 |
|
| 196 |
static int utf82u_index(int pos, const char *start) { |
| 197 |
int uc = 0; |
| 198 |
const char *end = start+pos; |
| 199 |
|
| 200 |
while ( start<end ) { |
| 201 |
utf8_ildb(&start); |
| 202 |
++uc; |
| 203 |
} |
| 204 |
return( uc ); |
| 205 |
} |
| 206 |
|
| 207 |
static void GTextFieldChanged(GTextField *gt,int src) { |
| 208 |
GEvent e; |
| 209 |
|
| 210 |
e.type = et_controlevent; |
| 211 |
e.w = gt->g.base; |
| 212 |
e.u.control.subtype = et_textchanged; |
| 213 |
e.u.control.g = >->g; |
| 214 |
e.u.control.u.tf_changed.from_pulldown = src; |
| 215 |
if ( gt->g.handle_controlevent != NULL ) |
| 216 |
(gt->g.handle_controlevent)(>->g,&e); |
| 217 |
else |
| 218 |
GDrawPostEvent(&e); |
| 219 |
} |
| 220 |
|
| 221 |
static void GTextFieldSaved(GTextField *gt) { |
| 222 |
GEvent e; |
| 223 |
|
| 224 |
e.type = et_controlevent; |
| 225 |
e.w = gt->g.base; |
| 226 |
e.u.control.subtype = et_save; |
| 227 |
e.u.control.g = >->g; |
| 228 |
if ( gt->g.handle_controlevent != NULL ) |
| 229 |
(gt->g.handle_controlevent)(>->g,&e); |
| 230 |
else |
| 231 |
GDrawPostEvent(&e); |
| 232 |
} |
| 233 |
|
| 234 |
static void GTextFieldFocusChanged(GTextField *gt,int gained) { |
| 235 |
GEvent e; |
| 236 |
|
| 237 |
if ( (gt->g.box->flags & box_active_border_inner) && |
| 238 |
( gt->g.state==gs_enabled || gt->g.state==gs_active )) { |
| 239 |
int state = gained?gs_active:gs_enabled; |
| 240 |
if ( state!=gt->g.state ) { |
| 241 |
gt->g.state = state; |
| 242 |
GGadgetRedraw((GGadget *) gt); |
| 243 |
} |
| 244 |
} |
| 245 |
e.type = et_controlevent; |
| 246 |
e.w = gt->g.base; |
| 247 |
e.u.control.subtype = et_textfocuschanged; |
| 248 |
e.u.control.g = >->g; |
| 249 |
e.u.control.u.tf_focus.gained_focus = gained; |
| 250 |
if ( gt->g.handle_controlevent != NULL ) |
| 251 |
(gt->g.handle_controlevent)(>->g,&e); |
| 252 |
else |
| 253 |
GDrawPostEvent(&e); |
| 254 |
} |
| 255 |
|
| 256 |
static void GTextFieldPangoRefigureLines(GTextField *gt, int start_of_change) { |
| 257 |
char *utf8_text, *pt, *ept; |
| 258 |
unichar_t *upt, *uept; |
| 259 |
int i, uc; |
| 260 |
GRect size; |
| 261 |
|
| 262 |
free(gt->utf8_text); |
| 263 |
if ( gt->lines8==NULL ) { |
| 264 |
gt->lines8 = malloc(gt->lmax*sizeof(int32_t)); |
| 265 |
gt->lines8[0] = 0; |
| 266 |
gt->lines8[1] = -1; |
| 267 |
} |
| 268 |
|
| 269 |
if ( gt->password ) { |
| 270 |
int cnt = u_strlen(gt->text); |
| 271 |
utf8_text = malloc(cnt+1); |
| 272 |
memset(utf8_text,'*',cnt); |
| 273 |
utf8_text[cnt] = '\0'; |
| 274 |
} else |
| 275 |
utf8_text = u2utf8_copy(gt->text); |
| 276 |
gt->utf8_text = utf8_text; |
| 277 |
GDrawLayoutInit(gt->g.base,utf8_text,-1,NULL); |
| 278 |
|
| 279 |
if ( !gt->multi_line ) { |
| 280 |
GDrawLayoutExtents(gt->g.base,&size); |
| 281 |
gt->xmax = size.width; |
| 282 |
return; |
| 283 |
} |
| 284 |
|
| 285 |
if ( !gt->wrap ) { |
| 286 |
pt = utf8_text; |
| 287 |
i=0; |
| 288 |
while ( ( ept = strchr(pt,'\n'))!=NULL ) { |
| 289 |
if ( i>=gt->lmax ) { |
| 290 |
gt->lines8 = realloc(gt->lines8,(gt->lmax+=10)*sizeof(int32_t)); |
| 291 |
gt->lines = realloc(gt->lines,gt->lmax*sizeof(int32_t)); |
| 292 |
} |
| 293 |
gt->lines8[i++] = pt-utf8_text; |
| 294 |
pt = ept+1; |
| 295 |
} |
| 296 |
if ( i>=gt->lmax ) { |
| 297 |
gt->lines8 = realloc(gt->lines8,(gt->lmax+=10)*sizeof(int32_t)); |
| 298 |
gt->lines = realloc(gt->lines,gt->lmax*sizeof(int32_t)); |
| 299 |
} |
| 300 |
gt->lines8[i++] = pt-utf8_text; |
| 301 |
|
| 302 |
upt = gt->text; |
| 303 |
i = 0; |
| 304 |
while ( ( uept = u_strchr(upt,'\n'))!=NULL ) { |
| 305 |
gt->lines[i++] = upt-gt->text; |
| 306 |
upt = uept+1; |
| 307 |
} |
| 308 |
gt->lines[i++] = upt-gt->text; |
| 309 |
} else { |
| 310 |
int lcnt; |
| 311 |
GDrawLayoutSetWidth(gt->g.base,gt->g.inner.width); |
| 312 |
lcnt = GDrawLayoutLineCount(gt->g.base); |
| 313 |
if ( lcnt+2>=gt->lmax ) { |
| 314 |
gt->lines8 = realloc(gt->lines8,(gt->lmax=lcnt+10)*sizeof(int32_t)); |
| 315 |
gt->lines = realloc(gt->lines,gt->lmax*sizeof(int32_t)); |
| 316 |
} |
| 317 |
pt = utf8_text; uc=0; |
| 318 |
for ( i=0; i<lcnt; ++i ) { |
| 319 |
gt->lines8[i] = GDrawLayoutLineStart(gt->g.base,i); |
| 320 |
ept = utf8_text + gt->lines8[i]; |
| 321 |
while ( pt<ept ) { |
| 322 |
++uc; |
| 323 |
utf8_ildb((const char **) &pt); |
| 324 |
} |
| 325 |
gt->lines[i] = uc; |
| 326 |
} |
| 327 |
if ( i==0 ) { |
| 328 |
gt->lines8[i] = strlen(utf8_text); |
| 329 |
gt->lines[i] = u_strlen(gt->text); |
| 330 |
} else { |
| 331 |
gt->lines8[i] = gt->lines8[i-1] + strlen( utf8_text + gt->lines8[i-1]); |
| 332 |
gt->lines [i] = gt->lines [i-1] + u_strlen( gt->text + gt->lines [i-1]); |
| 333 |
} |
| 334 |
} |
| 335 |
if ( gt->lcnt!=i ) { |
| 336 |
gt->lcnt = i; |
| 337 |
if ( gt->vsb!=NULL ) |
| 338 |
GScrollBarSetBounds(>->vsb->g,0,gt->lcnt, |
| 339 |
gt->g.inner.height<gt->fh? 1 : gt->g.inner.height/gt->fh); |
| 340 |
if ( gt->loff_top+gt->g.inner.height/gt->fh>gt->lcnt ) { |
| 341 |
gt->loff_top = gt->lcnt-gt->g.inner.height/gt->fh; |
| 342 |
if ( gt->loff_top<0 ) gt->loff_top = 0; |
| 343 |
if ( gt->vsb!=NULL ) |
| 344 |
GScrollBarSetPos(>->vsb->g,gt->loff_top); |
| 345 |
} |
| 346 |
} |
| 347 |
if ( i>=gt->lmax ) |
| 348 |
gt->lines = realloc(gt->lines,(gt->lmax+=10)*sizeof(int32_t)); |
| 349 |
gt->lines8[i] = -1; |
| 350 |
gt->lines[i++] = -1; |
| 351 |
|
| 352 |
GDrawLayoutExtents(gt->g.base,&size); |
| 353 |
gt->xmax = size.width; |
| 354 |
|
| 355 |
if ( gt->hsb!=NULL ) { |
| 356 |
GScrollBarSetBounds(>->hsb->g,0,gt->xmax,gt->g.inner.width); |
| 357 |
} |
| 358 |
GDrawLayoutSetWidth(gt->g.base,-1); |
| 359 |
} |
| 360 |
|
| 361 |
static void GTextFieldRefigureLines(GTextField *gt, int start_of_change) { |
| 362 |
GDrawSetFont(gt->g.base,gt->font); |
| 363 |
if ( gt->lines==NULL ) { |
| 364 |
gt->lines = malloc(10*sizeof(int32_t)); |
| 365 |
gt->lines[0] = 0; |
| 366 |
gt->lines[1] = -1; |
| 367 |
gt->lmax = 10; |
| 368 |
gt->lcnt = 1; |
| 369 |
if ( gt->vsb!=NULL ) |
| 370 |
GScrollBarSetBounds(>->vsb->g,0,gt->lcnt, |
| 371 |
gt->g.inner.height<gt->fh ? 1 : gt->g.inner.height/gt->fh); |
| 372 |
} |
| 373 |
|
| 374 |
GTextFieldPangoRefigureLines(gt,start_of_change); |
| 375 |
return; |
| 376 |
} |
| 377 |
|
| 378 |
static void _GTextFieldReplace(GTextField *gt, const unichar_t *str) { |
| 379 |
unichar_t *old = gt->oldtext; |
| 380 |
unichar_t *new = malloc((u_strlen(gt->text)-(gt->sel_end-gt->sel_start) + u_strlen(str)+1)*sizeof(unichar_t)); |
| 381 |
|
| 382 |
gt->oldtext = gt->text; |
| 383 |
gt->sel_oldstart = gt->sel_start; |
| 384 |
gt->sel_oldend = gt->sel_end; |
| 385 |
gt->sel_oldbase = gt->sel_base; |
| 386 |
|
| 387 |
u_strncpy(new,gt->text,gt->sel_start); |
| 388 |
u_strcpy(new+gt->sel_start,str); |
| 389 |
gt->sel_start = u_strlen(new); |
| 390 |
u_strcpy(new+gt->sel_start,gt->text+gt->sel_end); |
| 391 |
gt->text = new; |
| 392 |
gt->sel_end = gt->sel_base = gt->sel_start; |
| 393 |
free(old); |
| 394 |
|
| 395 |
GTextFieldRefigureLines(gt,gt->sel_oldstart); |
| 396 |
} |
| 397 |
|
| 398 |
static void GTextField_Replace(GTextField *gt, const unichar_t *str) { |
| 399 |
_GTextFieldReplace(gt,str); |
| 400 |
GTextField_Show(gt,gt->sel_start); |
| 401 |
} |
| 402 |
|
| 403 |
static int GTextFieldFindLine(GTextField *gt, int pos) { |
| 404 |
int i; |
| 405 |
for ( i=0; gt->lines[i+1]!=-1; ++i ) |
| 406 |
if ( pos<gt->lines[i+1]) |
| 407 |
break; |
| 408 |
return( i ); |
| 409 |
} |
| 410 |
|
| 411 |
static unichar_t *GTextFieldGetPtFromPos(GTextField *gt,int i,int xpos) { |
| 412 |
unichar_t *end; |
| 413 |
|
| 414 |
int index8, uc; |
| 415 |
if ( gt->lines8[i+1]==-1 ) |
| 416 |
GDrawLayoutInit(gt->g.base,gt->utf8_text + gt->lines8[i],-1,NULL); |
| 417 |
else { |
| 418 |
GDrawLayoutInit(gt->g.base,gt->utf8_text + gt->lines8[i], gt->lines8[i+1]-gt->lines8[i], NULL); |
| 419 |
} |
| 420 |
index8 = GDrawLayoutXYToIndex(gt->g.base,xpos-gt->g.inner.x+gt->xoff_left,0); |
| 421 |
uc = utf82u_index(index8,gt->utf8_text + gt->lines8[i]); |
| 422 |
end = gt->text + gt->lines[i] + uc; |
| 423 |
return( end ); |
| 424 |
} |
| 425 |
|
| 426 |
static int GTextField_Show(GTextField *gt, int pos) { |
| 427 |
int i, ll, xoff, loff; |
| 428 |
int refresh=false; |
| 429 |
GListField *ge = (GListField *) gt; |
| 430 |
int width = gt->g.inner.width; |
| 431 |
|
| 432 |
if ( gt->listfield || gt->numericfield ) |
| 433 |
width = ge->fieldrect.width - 2*(gt->g.inner.x - gt->g.r.x); |
| 434 |
|
| 435 |
if ( pos < 0 ) pos = 0; |
| 436 |
if ( pos > u_strlen(gt->text)) pos = u_strlen(gt->text); |
| 437 |
i = GTextFieldFindLine(gt,pos); |
| 438 |
|
| 439 |
loff = gt->loff_top; |
| 440 |
if ( gt->lcnt<gt->g.inner.height/gt->fh || loff==0 ) |
| 441 |
loff = 0; |
| 442 |
if ( i<loff ) |
| 443 |
loff = i; |
| 444 |
if ( i>=loff+gt->g.inner.height/gt->fh ) { |
| 445 |
loff = i-(gt->g.inner.height/gt->fh); |
| 446 |
if ( gt->g.inner.height/gt->fh>2 ) |
| 447 |
++loff; |
| 448 |
} |
| 449 |
|
| 450 |
xoff = gt->xoff_left; |
| 451 |
if ( gt->lines[i+1]==-1 ) ll = -1; else ll = gt->lines[i+1]-gt->lines[i]-1; |
| 452 |
GRect size; |
| 453 |
if ( gt->lines8[i+1]==-1 ) ll = strlen(gt->utf8_text+gt->lines8[i]); else ll = gt->lines8[i+1]-gt->lines8[i]-1; |
| 454 |
GDrawLayoutInit(gt->g.base,gt->utf8_text+gt->lines8[i],ll,NULL); |
| 455 |
GDrawLayoutExtents(gt->g.base,&size); |
| 456 |
if ( size.width < width ) |
| 457 |
xoff = 0; |
| 458 |
else { |
| 459 |
int index8 = u2utf8_index(pos- gt->lines8[i],gt->utf8_text + gt->lines8[i]); |
| 460 |
GDrawLayoutIndexToPos(gt->g.base,index8,&size); |
| 461 |
if ( size.x + 2*size.width < width ) |
| 462 |
xoff = 0; |
| 463 |
else |
| 464 |
xoff = size.x - (width - size.width)/2; |
| 465 |
if ( xoff<0 ) |
| 466 |
xoff = 0; |
| 467 |
} |
| 468 |
|
| 469 |
if ( xoff!=gt->xoff_left ) { |
| 470 |
gt->xoff_left = xoff; |
| 471 |
if ( gt->hsb!=NULL ) |
| 472 |
GScrollBarSetPos(>->hsb->g,xoff); |
| 473 |
refresh = true; |
| 474 |
} |
| 475 |
if ( loff!=gt->loff_top ) { |
| 476 |
gt->loff_top = loff; |
| 477 |
if ( gt->vsb!=NULL ) |
| 478 |
GScrollBarSetPos(>->vsb->g,loff); |
| 479 |
refresh = true; |
| 480 |
} |
| 481 |
GTPositionGIC(gt); |
| 482 |
return( refresh ); |
| 483 |
} |
| 484 |
|
| 485 |
static void *genunicodedata(void *_gt,int32_t *len) { |
| 486 |
GTextField *gt = _gt; |
| 487 |
unichar_t *temp; |
| 488 |
*len = gt->sel_end-gt->sel_start + 1; |
| 489 |
temp = malloc((*len+2)*sizeof(unichar_t)); |
| 490 |
temp[0] = 0xfeff; |
| 491 |
u_strncpy(temp+1,gt->text+gt->sel_start,gt->sel_end-gt->sel_start); |
| 492 |
temp[*len+1] = 0; |
| 493 |
return( temp ); |
| 494 |
} |
| 495 |
|
| 496 |
static void *genutf8data(void *_gt,int32_t *len) { |
| 497 |
GTextField *gt = _gt; |
| 498 |
unichar_t *temp =u_copyn(gt->text+gt->sel_start,gt->sel_end-gt->sel_start); |
| 499 |
char *ret = u2utf8_copy(temp); |
| 500 |
free(temp); |
| 501 |
*len = strlen(ret); |
| 502 |
return( ret ); |
| 503 |
} |
| 504 |
|
| 505 |
static void *ddgenunicodedata(void *_gt,int32_t *len) { |
| 506 |
void *temp = genunicodedata(_gt,len); |
| 507 |
GTextField *gt = _gt; |
| 508 |
_GTextFieldReplace(gt,nullstr); |
| 509 |
_ggadget_redraw(>->g); |
| 510 |
return( temp ); |
| 511 |
} |
| 512 |
|
| 513 |
static void *genlocaldata(void *_gt,int32_t *len) { |
| 514 |
GTextField *gt = _gt; |
| 515 |
unichar_t *temp =u_copyn(gt->text+gt->sel_start,gt->sel_end-gt->sel_start); |
| 516 |
char *ret = u2def_copy(temp); |
| 517 |
free(temp); |
| 518 |
*len = strlen(ret); |
| 519 |
return( ret ); |
| 520 |
} |
| 521 |
|
| 522 |
static void *ddgenlocaldata(void *_gt,int32_t *len) { |
| 523 |
void *temp = genlocaldata(_gt,len); |
| 524 |
GTextField *gt = _gt; |
| 525 |
_GTextFieldReplace(gt,nullstr); |
| 526 |
_ggadget_redraw(>->g); |
| 527 |
return( temp ); |
| 528 |
} |
| 529 |
|
| 530 |
static void noop(void *_gt) { |
| 531 |
} |
| 532 |
|
| 533 |
static void GTextFieldGrabPrimarySelection(GTextField *gt) { |
| 534 |
int ss = gt->sel_start, se = gt->sel_end; |
| 535 |
|
| 536 |
GDrawGrabSelection(gt->g.base,sn_primary); |
| 537 |
gt->sel_start = ss; gt->sel_end = se; |
| 538 |
GDrawAddSelectionType(gt->g.base,sn_primary,"text/plain;charset=ISO-10646-UCS-4",gt,gt->sel_end-gt->sel_start, |
| 539 |
sizeof(unichar_t), |
| 540 |
genunicodedata,noop); |
| 541 |
GDrawAddSelectionType(gt->g.base,sn_primary,"UTF8_STRING",gt,gt->sel_end-gt->sel_start, |
| 542 |
sizeof(char), |
| 543 |
genutf8data,noop); |
| 544 |
GDrawAddSelectionType(gt->g.base,sn_primary,"text/plain;charset=UTF-8",gt,gt->sel_end-gt->sel_start, |
| 545 |
sizeof(char), |
| 546 |
genutf8data,noop); |
| 547 |
GDrawAddSelectionType(gt->g.base,sn_primary,"STRING",gt,gt->sel_end-gt->sel_start, |
| 548 |
sizeof(char), |
| 549 |
genlocaldata,noop); |
| 550 |
} |
| 551 |
|
| 552 |
static void GTextFieldGrabDDSelection(GTextField *gt) { |
| 553 |
|
| 554 |
GDrawGrabSelection(gt->g.base,sn_drag_and_drop); |
| 555 |
GDrawAddSelectionType(gt->g.base,sn_drag_and_drop,"text/plain;charset=ISO-10646-UCS-4",gt,gt->sel_end-gt->sel_start, |
| 556 |
sizeof(unichar_t), |
| 557 |
ddgenunicodedata,noop); |
| 558 |
GDrawAddSelectionType(gt->g.base,sn_drag_and_drop,"STRING",gt,gt->sel_end-gt->sel_start,sizeof(char), |
| 559 |
ddgenlocaldata,noop); |
| 560 |
} |
| 561 |
|
| 562 |
static void GTextFieldGrabSelection(GTextField *gt, enum selnames sel ) { |
| 563 |
|
| 564 |
if ( gt->sel_start!=gt->sel_end ) { |
| 565 |
unichar_t *temp; |
| 566 |
char *ctemp, *ctemp2; |
| 567 |
int i; |
| 568 |
uint16_t *u2temp; |
| 569 |
|
| 570 |
GDrawGrabSelection(gt->g.base,sel); |
| 571 |
temp = malloc((gt->sel_end-gt->sel_start + 2)*sizeof(unichar_t)); |
| 572 |
temp[0] = 0xfeff; |
| 573 |
u_strncpy(temp+1,gt->text+gt->sel_start,gt->sel_end-gt->sel_start); |
| 574 |
ctemp = u2utf8_copy(temp+1); |
| 575 |
ctemp2 = u2def_copy(temp+1); |
| 576 |
GDrawAddSelectionType(gt->g.base,sel,"text/plain;charset=ISO-10646-UCS-4",temp,u_strlen(temp), |
| 577 |
sizeof(unichar_t), |
| 578 |
NULL,NULL); |
| 579 |
u2temp = malloc((gt->sel_end-gt->sel_start + 2)*sizeof(uint16_t)); |
| 580 |
for ( i=0; temp[i]!=0; ++i ) |
| 581 |
u2temp[i] = temp[i]; |
| 582 |
u2temp[i] = 0; |
| 583 |
GDrawAddSelectionType(gt->g.base,sel,"text/plain;charset=ISO-10646-UCS-2",u2temp,u_strlen(temp), |
| 584 |
2, |
| 585 |
NULL,NULL); |
| 586 |
GDrawAddSelectionType(gt->g.base,sel,"UTF8_STRING",copy(ctemp),strlen(ctemp), |
| 587 |
sizeof(char), |
| 588 |
NULL,NULL); |
| 589 |
GDrawAddSelectionType(gt->g.base,sel,"text/plain;charset=UTF-8",ctemp,strlen(ctemp), |
| 590 |
sizeof(char), |
| 591 |
NULL,NULL); |
| 592 |
|
| 593 |
if ( ctemp2!=NULL && *ctemp2!='\0' ) |
| 594 |
GDrawAddSelectionType(gt->g.base,sel,"STRING",ctemp2,strlen(ctemp2), |
| 595 |
sizeof(char), |
| 596 |
NULL,NULL); |
| 597 |
else |
| 598 |
free(ctemp2); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
static int GTextFieldSelBackword(unichar_t *text,int start) { |
| 603 |
unichar_t ch = text[start-1]; |
| 604 |
|
| 605 |
if ( start==0 ) |
| 606 |
; |
| 607 |
else if ( isalnum(ch) || ch=='_' ) { |
| 608 |
int i; |
| 609 |
for ( i=start-1; i>=0 && (isalnum(text[i]) || text[i]=='_') ; --i ); |
| 610 |
start = i+1; |
| 611 |
} else { |
| 612 |
int i; |
| 613 |
for ( i=start-1; i>=0 && !isalnum(text[i]) && text[i]!='_' ; --i ); |
| 614 |
start = i+1; |
| 615 |
} |
| 616 |
return( start ); |
| 617 |
} |
| 618 |
|
| 619 |
static int GTextFieldSelForeword(unichar_t *text,int end) { |
| 620 |
unichar_t ch = text[end]; |
| 621 |
|
| 622 |
if ( ch=='\0' ) |
| 623 |
; |
| 624 |
else if ( isalnum(ch) || ch=='_' ) { |
| 625 |
int i; |
| 626 |
for ( i=end; isalnum(text[i]) || text[i]=='_' ; ++i ); |
| 627 |
end = i; |
| 628 |
} else { |
| 629 |
int i; |
| 630 |
for ( i=end; !isalnum(text[i]) && text[i]!='_' && text[i]!='\0' ; ++i ); |
| 631 |
end = i; |
| 632 |
} |
| 633 |
return( end ); |
| 634 |
} |
| 635 |
|
| 636 |
static void GTextFieldSelectWord(GTextField *gt,int mid, int32_t *start, int32_t *end) { |
| 637 |
unichar_t *text; |
| 638 |
unichar_t ch = gt->text[mid]; |
| 639 |
|
| 640 |
text = gt->text; |
| 641 |
ch = text[mid]; |
| 642 |
|
| 643 |
if ( ch=='\0' ) |
| 644 |
*start = *end = mid; |
| 645 |
else if ( isspace(ch) ) { |
| 646 |
int i; |
| 647 |
for ( i=mid; isspace(text[i]); ++i ); |
| 648 |
*end = i; |
| 649 |
for ( i=mid-1; i>=0 && isspace(text[i]) ; --i ); |
| 650 |
*start = i+1; |
| 651 |
} else if ( isalnum(ch) || ch=='_' ) { |
| 652 |
int i; |
| 653 |
for ( i=mid; isalnum(text[i]) || text[i]=='_' ; ++i ); |
| 654 |
*end = i; |
| 655 |
for ( i=mid-1; i>=0 && (isalnum(text[i]) || text[i]=='_') ; --i ); |
| 656 |
*start = i+1; |
| 657 |
} else { |
| 658 |
int i; |
| 659 |
for ( i=mid; !isalnum(text[i]) && text[i]!='_' && text[i]!='\0' ; ++i ); |
| 660 |
*end = i; |
| 661 |
for ( i=mid-1; i>=0 && !isalnum(text[i]) && text[i]!='_' ; --i ); |
| 662 |
*start = i+1; |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
static void GTextFieldSelectWords(GTextField *gt,int last) { |
| 667 |
int32_t ss, se; |
| 668 |
GTextFieldSelectWord(gt,gt->sel_base,>->sel_start,>->sel_end); |
| 669 |
if ( last!=gt->sel_base ) { |
| 670 |
GTextFieldSelectWord(gt,last,&ss,&se); |
| 671 |
if ( ss<gt->sel_start ) gt->sel_start = ss; |
| 672 |
if ( se>gt->sel_end ) gt->sel_end = se; |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
static void GTextFieldPaste(GTextField *gt,enum selnames sel) { |
| 677 |
if ( GDrawSelectionHasType(gt->g.base,sel,"UTF8_STRING") || |
| 678 |
GDrawSelectionHasType(gt->g.base,sel,"text/plain;charset=UTF-8")) { |
| 679 |
unichar_t *temp; char *ctemp; |
| 680 |
int32_t len; |
| 681 |
ctemp = GDrawRequestSelection(gt->g.base,sel,"UTF8_STRING",&len); |
| 682 |
if ( ctemp==NULL || len==0 ) |
| 683 |
ctemp = GDrawRequestSelection(gt->g.base,sel,"text/plain;charset=UTF-8",&len); |
| 684 |
if ( ctemp!=NULL ) { |
| 685 |
temp = utf82u_copyn(ctemp,strlen(ctemp)); |
| 686 |
GTextField_Replace(gt,temp); |
| 687 |
free(ctemp); free(temp); |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
} else if ( GDrawSelectionHasType(gt->g.base,sel,"text/plain;charset=ISO-10646-UCS-4")) { |
| 692 |
unichar_t *temp; |
| 693 |
int32_t len; |
| 694 |
temp = GDrawRequestSelection(gt->g.base,sel,"text/plain;charset=ISO-10646-UCS-4",&len); |
| 695 |
|
| 696 |
if ( temp!=NULL ) |
| 697 |
GTextField_Replace(gt,temp[0]==0xfeff?temp+1:temp); |
| 698 |
free(temp); |
| 699 |
} else if ( GDrawSelectionHasType(gt->g.base,sel,"Unicode") || |
| 700 |
GDrawSelectionHasType(gt->g.base,sel,"text/plain;charset=ISO-10646-UCS-2")) { |
| 701 |
unichar_t *temp; |
| 702 |
uint16_t *temp2; |
| 703 |
int32_t len; |
| 704 |
temp2 = GDrawRequestSelection(gt->g.base,sel,"text/plain;charset=ISO-10646-UCS-2",&len); |
| 705 |
if ( temp2==NULL || len==0 ) |
| 706 |
temp2 = GDrawRequestSelection(gt->g.base,sel,"Unicode",&len); |
| 707 |
if ( temp2!=NULL ) { |
| 708 |
int i; |
| 709 |
temp = malloc((len/2+1)*sizeof(unichar_t)); |
| 710 |
for ( i=0; temp2[i]!=0; ++i ) |
| 711 |
temp[i] = temp2[i]; |
| 712 |
temp[i] = 0; |
| 713 |
GTextField_Replace(gt,temp[0]==0xfeff?temp+1:temp); |
| 714 |
free(temp); |
| 715 |
} |
| 716 |
free(temp2); |
| 717 |
} else if ( GDrawSelectionHasType(gt->g.base,sel,"STRING")) { |
| 718 |
unichar_t *temp; char *ctemp; |
| 719 |
int32_t len; |
| 720 |
ctemp = GDrawRequestSelection(gt->g.base,sel,"STRING",&len); |
| 721 |
if ( ctemp==NULL || len==0 ) |
| 722 |
ctemp = GDrawRequestSelection(gt->g.base,sel,"text/plain;charset=UTF-8",&len); |
| 723 |
if ( ctemp!=NULL ) { |
| 724 |
temp = def2u_copy(ctemp); |
| 725 |
GTextField_Replace(gt,temp); |
| 726 |
free(ctemp); free(temp); |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
static int gtextfield_editcmd(GGadget *g,enum editor_commands cmd) { |
| 732 |
GTextField *gt = (GTextField *) g; |
| 733 |
|
| 734 |
switch ( cmd ) { |
| 735 |
case ec_selectall: |
| 736 |
gt->sel_start = 0; |
| 737 |
gt->sel_end = u_strlen(gt->text); |
| 738 |
return( true ); |
| 739 |
case ec_clear: |
| 740 |
GTextField_Replace(gt,nullstr); |
| 741 |
_ggadget_redraw(g); |
| 742 |
return( true ); |
| 743 |
case ec_cut: |
| 744 |
GTextFieldGrabSelection(gt,sn_clipboard); |
| 745 |
GTextField_Replace(gt,nullstr); |
| 746 |
_ggadget_redraw(g); |
| 747 |
break; |
| 748 |
case ec_copy: |
| 749 |
GTextFieldGrabSelection(gt,sn_clipboard); |
| 750 |
return( true ); |
| 751 |
case ec_paste: |
| 752 |
GTextFieldPaste(gt,sn_clipboard); |
| 753 |
GTextField_Show(gt,gt->sel_start); |
| 754 |
_ggadget_redraw(g); |
| 755 |
break; |
| 756 |
case ec_undo: |
| 757 |
if ( gt->oldtext!=NULL ) { |
| 758 |
unichar_t *temp = gt->text; |
| 759 |
int16_t s; |
| 760 |
gt->text = gt->oldtext; gt->oldtext = temp; |
| 761 |
s = gt->sel_start; gt->sel_start = gt->sel_oldstart; gt->sel_oldstart = s; |
| 762 |
s = gt->sel_end; gt->sel_end = gt->sel_oldend; gt->sel_oldend = s; |
| 763 |
s = gt->sel_base; gt->sel_base = gt->sel_oldbase; gt->sel_oldbase = s; |
| 764 |
GTextFieldRefigureLines(gt, 0); |
| 765 |
GTextField_Show(gt,gt->sel_end); |
| 766 |
_ggadget_redraw(g); |
| 767 |
} |
| 768 |
break; |
| 769 |
case ec_redo: |
| 770 |
return( true ); |
| 771 |
case ec_backword: |
| 772 |
if ( gt->sel_start==gt->sel_end && gt->sel_start!=0 ) { |
| 773 |
gt->sel_start = GTextFieldSelBackword(gt->text,gt->sel_start); |
| 774 |
} |
| 775 |
GTextField_Replace(gt,nullstr); |
| 776 |
_ggadget_redraw(g); |
| 777 |
break; |
| 778 |
case ec_deleteword: |
| 779 |
if ( gt->sel_start==gt->sel_end && gt->sel_start!=0 ) |
| 780 |
GTextFieldSelectWord(gt,gt->sel_start,>->sel_start,>->sel_end); |
| 781 |
GTextField_Replace(gt,nullstr); |
| 782 |
_ggadget_redraw(g); |
| 783 |
break; |
| 784 |
default: |
| 785 |
return( false ); |
| 786 |
} |
| 787 |
GTextFieldChanged(gt,-1); |
| 788 |
return( true ); |
| 789 |
} |
| 790 |
|
| 791 |
static int _gtextfield_editcmd(GGadget *g,enum editor_commands cmd) { |
| 792 |
if ( gtextfield_editcmd(g,cmd)) { |
| 793 |
_ggadget_redraw(g); |
| 794 |
GTPositionGIC((GTextField *) g); |
| 795 |
return( true ); |
| 796 |
} |
| 797 |
return( false ); |
| 798 |
} |
| 799 |
|
| 800 |
static int GTBackPos(GTextField *gt,int pos, int ismeta) { |
| 801 |
int newpos; |
| 802 |
|
| 803 |
if ( ismeta ) |
| 804 |
newpos = GTextFieldSelBackword(gt->text,pos); |
| 805 |
else |
| 806 |
newpos = pos-1; |
| 807 |
if ( newpos==-1 ) newpos = pos; |
| 808 |
return( newpos ); |
| 809 |
} |
| 810 |
|
| 811 |
static int GTForePos(GTextField *gt,int pos, int ismeta) { |
| 812 |
int newpos=pos; |
| 813 |
|
| 814 |
if ( ismeta ) |
| 815 |
newpos = GTextFieldSelForeword(gt->text,pos); |
| 816 |
else { |
| 817 |
if ( gt->text[pos]!=0 ) |
| 818 |
newpos = pos+1; |
| 819 |
} |
| 820 |
return( newpos ); |
| 821 |
} |
| 822 |
|
| 823 |
unichar_t *_GGadgetFileToUString(char *filename,int max) { |
| 824 |
FILE *file; |
| 825 |
int ch, ch2, ch3; |
| 826 |
int format=0; |
| 827 |
unichar_t *space, *upt, *end; |
| 828 |
|
| 829 |
file = fopen( filename,"r" ); |
| 830 |
if ( file==NULL ) |
| 831 |
return( NULL ); |
| 832 |
ch = getc(file); ch2 = getc(file); ch3 = getc(file); |
| 833 |
ungetc(ch3,file); |
| 834 |
if ( ch==0xfe && ch2==0xff ) |
| 835 |
format = 1; |
| 836 |
else if ( ch==0xff && ch2==0xfe ) |
| 837 |
format = 2; |
| 838 |
else if ( ch==0xef && ch2==0xbb && ch3==0xbf ) { |
| 839 |
format = 3; |
| 840 |
getc(file); |
| 841 |
} else { |
| 842 |
getc(file); |
| 843 |
rewind(file); |
| 844 |
} |
| 845 |
space = upt = malloc((max+1)*sizeof(unichar_t)); |
| 846 |
end = space+max; |
| 847 |
if ( format==3 ) { |
| 848 |
while ( upt<end ) { |
| 849 |
ch=getc(file); |
| 850 |
if ( ch==EOF ) |
| 851 |
break; |
| 852 |
if ( ch<0x80 ) |
| 853 |
*upt++ = ch; |
| 854 |
else if ( ch<0xe0 ) { |
| 855 |
ch2 = getc(file); |
| 856 |
*upt++ = ((ch&0x1f)<<6)|(ch2&0x3f); |
| 857 |
} else if ( ch<0xf0 ) { |
| 858 |
ch2 = getc(file); ch3 = getc(file); |
| 859 |
*upt++ = ((ch&0xf)<<12)|((ch2&0x3f)<<6)|(ch3&0x3f); |
| 860 |
} else { |
| 861 |
int ch4, w; |
| 862 |
ch2 = getc(file); ch3 = getc(file); ch4=getc(file); |
| 863 |
w = ( ((ch&7)<<2) | ((ch2&0x30)>>4) ) -1; |
| 864 |
*upt++ = 0xd800 | (w<<6) | ((ch2&0xf)<<2) | ((ch3&0x30)>>4); |
| 865 |
if ( upt<end ) |
| 866 |
*upt++ = 0xdc00 | ((ch3&0xf)<<6) | (ch4&0x3f); |
| 867 |
} |
| 868 |
} |
| 869 |
} else if ( format!=0 ) { |
| 870 |
while ( upt<end ) { |
| 871 |
ch = getc(file); ch2 = getc(file); |
| 872 |
if ( ch2==EOF ) |
| 873 |
break; |
| 874 |
if ( format==1 ) |
| 875 |
*upt ++ = (ch<<8)|ch2; |
| 876 |
else |
| 877 |
*upt ++ = (ch2<<8)|ch; |
| 878 |
} |
| 879 |
} else { |
| 880 |
char buffer[400]; |
| 881 |
while ( fgets(buffer,sizeof(buffer),file)!=NULL ) { |
| 882 |
def2u_strncpy(upt,buffer,end-upt); |
| 883 |
upt += u_strlen(upt); |
| 884 |
} |
| 885 |
} |
| 886 |
*upt = '\0'; |
| 887 |
fclose(file); |
| 888 |
return( space ); |
| 889 |
} |
| 890 |
|
| 891 |
bool GTextFieldIsEmpty(GGadget *g) { |
| 892 |
GTextField *gt = (GTextField *) g; |
| 893 |
return gt->text == NULL || *gt->text == '\0'; |
| 894 |
} |
| 895 |
|
| 896 |
static void GTextFieldImport(GTextField *gt) { |
| 897 |
unichar_t *ret; |
| 898 |
char *cret; |
| 899 |
unichar_t *str; |
| 900 |
|
| 901 |
char *temp = GWidgetOpenFile8(_("Open"),NULL,"*.{txt,py}",NULL,NULL); |
| 902 |
ret = utf82u_copy(temp); |
| 903 |
free(temp); |
| 904 |
|
| 905 |
if ( ret==NULL ) |
| 906 |
return; |
| 907 |
cret = u2def_copy(ret); |
| 908 |
free(ret); |
| 909 |
str = _GGadgetFileToUString(cret,65536); |
| 910 |
if ( str==NULL ) { |
| 911 |
GWidgetError8(_("Could not open file"), _("Could not open %.100s"),cret); |
| 912 |
free(cret); |
| 913 |
return; |
| 914 |
} |
| 915 |
free(cret); |
| 916 |
GTextField_Replace(gt,str); |
| 917 |
free(str); |
| 918 |
} |
| 919 |
|
| 920 |
static void GTextFieldSave(GTextField *gt,int utf8) { |
| 921 |
unichar_t *ret; |
| 922 |
char *cret; |
| 923 |
FILE *file; |
| 924 |
unichar_t *pt; |
| 925 |
|
| 926 |
char *temp = GWidgetOpenFile8(_("Save"),NULL,"*.{txt,py}",NULL,NULL); |
| 927 |
ret = utf82u_copy(temp); |
| 928 |
free(temp); |
| 929 |
|
| 930 |
if ( ret==NULL ) |
| 931 |
return; |
| 932 |
cret = u2def_copy(ret); |
| 933 |
free(ret); |
| 934 |
file = fopen(cret,"w"); |
| 935 |
if ( file==NULL ) { |
| 936 |
GWidgetError8(_("Could not open file"), _("Could not open %.100s"),cret); |
| 937 |
free(cret); |
| 938 |
return; |
| 939 |
} |
| 940 |
free(cret); |
| 941 |
|
| 942 |
if ( utf8 ) { |
| 943 |
putc(0xef,file); |
| 944 |
putc(0xbb,file); |
| 945 |
putc(0xbf,file); |
| 946 |
for ( pt = gt->text ; *pt; ++pt ) { |
| 947 |
if ( *pt<0x80 ) |
| 948 |
putc(*pt,file); |
| 949 |
else if ( *pt<0x800 ) { |
| 950 |
putc(0xc0 | (*pt>>6), file); |
| 951 |
putc(0x80 | (*pt&0x3f), file); |
| 952 |
} else if ( *pt>=0xd800 && *pt<0xdc00 && pt[1]>=0xdc00 && pt[1]<0xe000 ) { |
| 953 |
int u = ((*pt>>6)&0xf)+1, y = ((*pt&3)<<4) | ((pt[1]>>6)&0xf); |
| 954 |
putc( 0xf0 | (u>>2),file ); |
| 955 |
putc( 0x80 | ((u&3)<<4) | ((*pt>>2)&0xf),file ); |
| 956 |
putc( 0x80 | y,file ); |
| 957 |
putc( 0x80 | (pt[1]&0x3f),file ); |
| 958 |
} else { |
| 959 |
putc( 0xe0 | (*pt>>12),file ); |
| 960 |
putc( 0x80 | ((*pt>>6)&0x3f),file ); |
| 961 |
putc( 0x80 | (*pt&0x3f),file ); |
| 962 |
} |
| 963 |
} |
| 964 |
} else { |
| 965 |
putc(0xfeff>>8,file); |
| 966 |
putc(0xfeff&0xff,file); |
| 967 |
for ( pt = gt->text ; *pt; ++pt ) { |
| 968 |
putc(*pt>>8,file); |
| 969 |
putc(*pt&0xff,file); |
| 970 |
} |
| 971 |
} |
| 972 |
fclose(file); |
| 973 |
GTextFieldSaved(gt); |
| 974 |
} |
| 975 |
|
| 976 |
#define MID_Cut 1 |
| 977 |
#define MID_Copy 2 |
| 978 |
#define MID_Paste 3 |
| 979 |
|
| 980 |
#define MID_SelectAll 4 |
| 981 |
|
| 982 |
#define MID_Save 5 |
| 983 |
#define MID_SaveUCS2 6 |
| 984 |
#define MID_Import 7 |
| 985 |
|
| 986 |
#define MID_Undo 8 |
| 987 |
|
| 988 |
static GTextField *popup_kludge; |
| 989 |
|
| 990 |
static void GTFPopupInvoked(GWindow v, GMenuItem *mi,GEvent *e) { |
| 991 |
GTextField *gt; |
| 992 |
if ( popup_kludge==NULL ) |
| 993 |
return; |
| 994 |
gt = popup_kludge; |
| 995 |
popup_kludge = NULL; |
| 996 |
switch ( mi->mid ) { |
| 997 |
case MID_Undo: |
| 998 |
gtextfield_editcmd(>->g,ec_undo); |
| 999 |
break; |
| 1000 |
case MID_Cut: |
| 1001 |
gtextfield_editcmd(>->g,ec_cut); |
| 1002 |
break; |
| 1003 |
case MID_Copy: |
| 1004 |
gtextfield_editcmd(>->g,ec_copy); |
| 1005 |
break; |
| 1006 |
case MID_Paste: |
| 1007 |
gtextfield_editcmd(>->g,ec_paste); |
| 1008 |
break; |
| 1009 |
case MID_SelectAll: |
| 1010 |
gtextfield_editcmd(>->g,ec_selectall); |
| 1011 |
break; |
| 1012 |
case MID_Save: |
| 1013 |
GTextFieldSave(gt,true); |
| 1014 |
break; |
| 1015 |
case MID_SaveUCS2: |
| 1016 |
GTextFieldSave(gt,false); |
| 1017 |
break; |
| 1018 |
case MID_Import: |
| 1019 |
GTextFieldImport(gt); |
| 1020 |
break; |
| 1021 |
} |
| 1022 |
_ggadget_redraw(>->g); |
| 1023 |
} |
| 1024 |
|
| 1025 |
static GMenuItem gtf_popuplist[] = { |
| 1026 |
{ { (unichar_t *) "_Undo", NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 1, 0, '\0' }, 'Z', ksm_control, NULL, NULL, GTFPopupInvoked, MID_Undo }, |
| 1027 |
{ { NULL, NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 1, 0, 0, 0, '\0' }, '\0', 0, NULL, NULL, NULL, 0 }, |
| 1028 |
{ { (unichar_t *) "Cu_t", NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 1, 0, '\0' }, 'X', ksm_control, NULL, NULL, GTFPopupInvoked, MID_Cut }, |
| 1029 |
{ { (unichar_t *) "_Copy", NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 1, 0, '\0' }, 'C', ksm_control, NULL, NULL, GTFPopupInvoked, MID_Copy }, |
| 1030 |
{ { (unichar_t *) "_Paste", NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 1, 0, '\0' }, 'V', ksm_control, NULL, NULL, GTFPopupInvoked, MID_Paste }, |
| 1031 |
{ { NULL, NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 1, 0, 0, 0, '\0' }, '\0', 0, NULL, NULL, NULL, 0 }, |
| 1032 |
{ { (unichar_t *) "_Save in UTF8", NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 1, 0, '\0' }, 'S', ksm_control, NULL, NULL, GTFPopupInvoked, MID_Save }, |
| 1033 |
{ { (unichar_t *) "Save in _UCS2", NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 1, 0, '\0' }, '\0', ksm_control, NULL, NULL, GTFPopupInvoked, MID_SaveUCS2 }, |
| 1034 |
{ { (unichar_t *) "_Import", NULL, COLOR_DEFAULT, COLOR_DEFAULT, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 1, 0, '\0' }, 'I', ksm_control, NULL, NULL, GTFPopupInvoked, MID_Import }, |
| 1035 |
GMENUITEM_EMPTY |
| 1036 |
}; |
| 1037 |
static int first = true; |
| 1038 |
|
| 1039 |
static void GTFPopupMenu(GTextField *gt, GEvent *event) { |
| 1040 |
int no_sel = gt->sel_start==gt->sel_end; |
| 1041 |
|
| 1042 |
if ( first ) { |
| 1043 |
gtf_popuplist[0].ti.text = (unichar_t *) _("_Undo"); |
| 1044 |
gtf_popuplist[2].ti.text = (unichar_t *) _("Cu_t"); |
| 1045 |
gtf_popuplist[3].ti.text = (unichar_t *) _("_Copy"); |
| 1046 |
gtf_popuplist[4].ti.text = (unichar_t *) _("_Paste"); |
| 1047 |
gtf_popuplist[6].ti.text = (unichar_t *) _("_Save in UTF8"); |
| 1048 |
gtf_popuplist[7].ti.text = (unichar_t *) _("Save in _UCS2"); |
| 1049 |
gtf_popuplist[8].ti.text = (unichar_t *) _("_Import"); |
| 1050 |
first = false; |
| 1051 |
} |
| 1052 |
|
| 1053 |
gtf_popuplist[0].ti.disabled = gt->oldtext==NULL; |
| 1054 |
gtf_popuplist[2].ti.disabled = no_sel; |
| 1055 |
gtf_popuplist[3].ti.disabled = no_sel; |
| 1056 |
gtf_popuplist[4].ti.disabled = !GDrawSelectionHasType(gt->g.base,sn_clipboard,"text/plain;charset=ISO-10646-UCS-2") && |
| 1057 |
!GDrawSelectionHasType(gt->g.base,sn_clipboard,"UTF8_STRING") && |
| 1058 |
!GDrawSelectionHasType(gt->g.base,sn_clipboard,"STRING"); |
| 1059 |
popup_kludge = gt; |
| 1060 |
GMenuCreatePopupMenu(gt->g.base,event, gtf_popuplist); |
| 1061 |
} |
| 1062 |
|
| 1063 |
static void GTextFieldIncrement(GTextField *gt,int amount) { |
| 1064 |
unichar_t *end; |
| 1065 |
double d = u_strtod(gt->text,&end); |
| 1066 |
char buf[40]; |
| 1067 |
|
| 1068 |
while ( *end==' ' ) ++end; |
| 1069 |
if ( *end!='\0' ) { |
| 1070 |
GDrawBeep(NULL); |
| 1071 |
return; |
| 1072 |
} |
| 1073 |
d = floor(d)+amount; |
| 1074 |
sprintf(buf,"%g", d); |
| 1075 |
free(gt->oldtext); |
| 1076 |
gt->oldtext = gt->text; |
| 1077 |
gt->text = uc_copy(buf); |
| 1078 |
free(gt->utf8_text); |
| 1079 |
gt->utf8_text = copy(buf); |
| 1080 |
_ggadget_redraw(>->g); |
| 1081 |
GTextFieldChanged(gt,-1); |
| 1082 |
} |
| 1083 |
|
| 1084 |
static int GTextFieldDoChange(GTextField *gt, GEvent *event) { |
| 1085 |
int ss = gt->sel_start, se = gt->sel_end; |
| 1086 |
int pos, l, xpos, sel; |
| 1087 |
unichar_t *upt; |
| 1088 |
|
| 1089 |
if ( ( event->u.chr.state&(GMenuMask()&~ksm_shift)) || |
| 1090 |
event->u.chr.chars[0]<' ' || event->u.chr.chars[0]==0x7f ) { |
| 1091 |
switch ( event->u.chr.keysym ) { |
| 1092 |
case GK_BackSpace: |
| 1093 |
if ( gt->sel_start==gt->sel_end ) { |
| 1094 |
if ( gt->sel_start==0 ) |
| 1095 |
return( 2 ); |
| 1096 |
--gt->sel_start; |
| 1097 |
} |
| 1098 |
GTextField_Replace(gt,nullstr); |
| 1099 |
return( true ); |
| 1100 |
break; |
| 1101 |
case GK_Delete: |
| 1102 |
if ( gt->sel_start==gt->sel_end ) { |
| 1103 |
if ( gt->text[gt->sel_start]==0 ) |
| 1104 |
return( 2 ); |
| 1105 |
++gt->sel_end; |
| 1106 |
} |
| 1107 |
GTextField_Replace(gt,nullstr); |
| 1108 |
return( true ); |
| 1109 |
break; |
| 1110 |
case GK_Left: case GK_KP_Left: |
| 1111 |
if ( gt->sel_start==gt->sel_end ) { |
| 1112 |
gt->sel_start = GTBackPos(gt,gt->sel_start,event->u.chr.state&ksm_meta); |
| 1113 |
if ( !(event->u.chr.state&ksm_shift )) |
| 1114 |
gt->sel_end = gt->sel_start; |
| 1115 |
} else if ( event->u.chr.state&ksm_shift ) { |
| 1116 |
if ( gt->sel_end==gt->sel_base ) { |
| 1117 |
gt->sel_start = GTBackPos(gt,gt->sel_start,event->u.chr.state&ksm_meta); |
| 1118 |
} else { |
| 1119 |
gt->sel_end = GTBackPos(gt,gt->sel_end,event->u.chr.state&ksm_meta); |
| 1120 |
} |
| 1121 |
} else { |
| 1122 |
gt->sel_end = gt->sel_base = gt->sel_start; |
| 1123 |
} |
| 1124 |
GTextField_Show(gt,gt->sel_start); |
| 1125 |
return( 2 ); |
| 1126 |
break; |
| 1127 |
case GK_Right: case GK_KP_Right: |
| 1128 |
if ( gt->sel_start==gt->sel_end ) { |
| 1129 |
gt->sel_end = GTForePos(gt,gt->sel_start,event->u.chr.state&ksm_meta); |
| 1130 |
if ( !(event->u.chr.state&ksm_shift )) |
| 1131 |
gt->sel_start = gt->sel_end; |
| 1132 |
} else if ( event->u.chr.state&ksm_shift ) { |
| 1133 |
if ( gt->sel_end==gt->sel_base ) { |
| 1134 |
gt->sel_start = GTForePos(gt,gt->sel_start,event->u.chr.state&ksm_meta); |
| 1135 |
} else { |
| 1136 |
gt->sel_end = GTForePos(gt,gt->sel_end,event->u.chr.state&ksm_meta); |
| 1137 |
} |
| 1138 |
} else { |
| 1139 |
gt->sel_start = gt->sel_base = gt->sel_end; |
| 1140 |
} |
| 1141 |
GTextField_Show(gt,gt->sel_start); |
| 1142 |
return( 2 ); |
| 1143 |
break; |
| 1144 |
case GK_Up: case GK_KP_Up: |
| 1145 |
if ( gt->numericfield ) { |
| 1146 |
GTextFieldIncrement(gt,(event->u.chr.state&(ksm_shift|ksm_control))?10:1); |
| 1147 |
return( 2 ); |
| 1148 |
} |
| 1149 |
if ( !gt->multi_line ) |
| 1150 |
break; |
| 1151 |
if ( !( event->u.chr.state&ksm_shift ) && gt->sel_start!=gt->sel_end ) |
| 1152 |
gt->sel_end = gt->sel_base = gt->sel_start; |
| 1153 |
else { |
| 1154 |
pos = gt->sel_start; |
| 1155 |
if ( ( event->u.chr.state&ksm_shift ) && gt->sel_start==gt->sel_base ) |
| 1156 |
pos = gt->sel_end; |
| 1157 |
l = GTextFieldFindLine(gt,gt->sel_start); |
| 1158 |
GRect pos_rect; |
| 1159 |
int ll = gt->lines8[l+1]==-1 ? -1 : gt->lines8[l+1]-gt->lines8[l]; |
| 1160 |
sel = u2utf8_index(gt->sel_start-gt->lines[l],gt->utf8_text+gt->lines8[l]); |
| 1161 |
GDrawLayoutInit(gt->g.base,gt->utf8_text+gt->lines8[l],ll,NULL); |
| 1162 |
GDrawLayoutIndexToPos(gt->g.base,sel,&pos_rect); |
| 1163 |
xpos = pos_rect.x; |
| 1164 |
if ( l!=0 ) { |
| 1165 |
GDrawLayoutInit(gt->g.base,gt->utf8_text+gt->lines8[l-1],gt->lines8[l]-gt->lines8[l-1],NULL); |
| 1166 |
pos = GDrawLayoutXYToIndex(gt->g.base,xpos,0); |
| 1167 |
pos = utf82u_index(pos+gt->lines8[l-1],gt->utf8_text); |
| 1168 |
} |
| 1169 |
if ( event->u.chr.state&ksm_shift ) { |
| 1170 |
if ( pos<gt->sel_base ) { |
| 1171 |
gt->sel_start = pos; |
| 1172 |
gt->sel_end = gt->sel_base; |
| 1173 |
} else { |
| 1174 |
gt->sel_start = gt->sel_base; |
| 1175 |
gt->sel_end = pos; |
| 1176 |
} |
| 1177 |
} else { |
| 1178 |
gt->sel_start = gt->sel_end = gt->sel_base = pos; |
| 1179 |
} |
| 1180 |
} |
| 1181 |
GTextField_Show(gt,gt->sel_start); |
| 1182 |
return( 2 ); |
| 1183 |
break; |
| 1184 |
case GK_Down: case GK_KP_Down: |
| 1185 |
if ( gt->numericfield ) { |
| 1186 |
GTextFieldIncrement(gt,(event->u.chr.state&(ksm_shift|ksm_control))?-10:-1); |
| 1187 |
return( 2 ); |
| 1188 |
} |
| 1189 |
if ( !gt->multi_line ) |
| 1190 |
break; |
| 1191 |
if ( !( event->u.chr.state&ksm_shift ) && gt->sel_start!=gt->sel_end ) |
| 1192 |
gt->sel_end = gt->sel_base = gt->sel_end; |
| 1193 |
else { |
| 1194 |
pos = gt->sel_start; |
| 1195 |
if ( ( event->u.chr.state&ksm_shift ) && gt->sel_start==gt->sel_base ) |
| 1196 |
pos = gt->sel_end; |
| 1197 |
l = GTextFieldFindLine(gt,gt->sel_start); |
| 1198 |
GRect pos_rect; |
| 1199 |
int ll = gt->lines8[l+1]==-1 ? -1 : gt->lines8[l+1]-gt->lines8[l]; |
| 1200 |
sel = u2utf8_index(gt->sel_start-gt->lines[l],gt->utf8_text+gt->lines8[l]); |
| 1201 |
GDrawLayoutInit(gt->g.base,gt->utf8_text+gt->lines8[l],ll,NULL); |
| 1202 |
GDrawLayoutIndexToPos(gt->g.base,sel,&pos_rect); |
| 1203 |
xpos = pos_rect.x; |
| 1204 |
if ( l<gt->lcnt-1 ) { |
| 1205 |
ll = gt->lines8[l+2]==-1 ? -1 : gt->lines8[l+2]-gt->lines8[l+1]; |
| 1206 |
GDrawLayoutInit(gt->g.base,gt->utf8_text+gt->lines8[l+1],ll,NULL); |
| 1207 |
pos = GDrawLayoutXYToIndex(gt->g.base,xpos,0); |
| 1208 |
pos = utf82u_index(pos+gt->lines8[l+1],gt->utf8_text); |
| 1209 |
} |
| 1210 |
if ( event->u.chr.state&ksm_shift ) { |
| 1211 |
if ( pos<gt->sel_base ) { |
| 1212 |
gt->sel_start = pos; |
| 1213 |
gt->sel_end = gt->sel_base; |
| 1214 |
} else { |
| 1215 |
gt->sel_start = gt->sel_base; |
| 1216 |
gt->sel_end = pos; |
| 1217 |
} |
| 1218 |
} else { |
| 1219 |
gt->sel_start = gt->sel_end = gt->sel_base = pos; |
| 1220 |
} |
| 1221 |
} |
| 1222 |
GTextField_Show(gt,gt->sel_start); |
| 1223 |
return( 2 ); |
| 1224 |
break; |
| 1225 |
case GK_Home: case GK_Begin: case GK_KP_Home: case GK_KP_Begin: |
| 1226 |
if ( !(event->u.chr.state&ksm_shift) ) { |
| 1227 |
gt->sel_start = gt->sel_base = gt->sel_end = 0; |
| 1228 |
} else { |
| 1229 |
gt->sel_start = 0; gt->sel_end = gt->sel_base; |
| 1230 |
} |
| 1231 |
GTextField_Show(gt,gt->sel_start); |
| 1232 |
return( 2 ); |
| 1233 |
break; |
| 1234 |
|
| 1235 |
case 'E': case 'e': |
| 1236 |
if ( !( event->u.chr.state&ksm_control ) ) |
| 1237 |
return( false ); |
| 1238 |
upt = gt->text+gt->sel_base; |
| 1239 |
if ( *upt=='\n' ) |
| 1240 |
++upt; |
| 1241 |
upt = u_strchr(upt,'\n'); |
| 1242 |
if ( upt==NULL ) upt=gt->text+u_strlen(gt->text); |
| 1243 |
if ( !(event->u.chr.state&ksm_shift) ) { |
| 1244 |
gt->sel_start = gt->sel_base = gt->sel_end =upt-gt->text; |
| 1245 |
} else { |
| 1246 |
gt->sel_start = gt->sel_base; gt->sel_end = upt-gt->text; |
| 1247 |
} |
| 1248 |
GTextField_Show(gt,gt->sel_start); |
| 1249 |
return( 2 ); |
| 1250 |
break; |
| 1251 |
case GK_End: case GK_KP_End: |
| 1252 |
if ( !(event->u.chr.state&ksm_shift) ) { |
| 1253 |
gt->sel_start = gt->sel_base = gt->sel_end = u_strlen(gt->text); |
| 1254 |
} else { |
| 1255 |
gt->sel_start = gt->sel_base; gt->sel_end = u_strlen(gt->text); |
| 1256 |
} |
| 1257 |
GTextField_Show(gt,gt->sel_start); |
| 1258 |
return( 2 ); |
| 1259 |
break; |
| 1260 |
case 'D': case 'd': |
| 1261 |
if ( event->u.chr.state&ksm_control ) { |
| 1262 |
gtextfield_editcmd(>->g,ec_deleteword); |
| 1263 |
GTextField_Show(gt,gt->sel_start); |
| 1264 |
return( true ); |
| 1265 |
} |
| 1266 |
break; |
| 1267 |
case 'W': case 'w': |
| 1268 |
if ( event->u.chr.state&ksm_control ) { |
| 1269 |
gtextfield_editcmd(>->g,ec_backword); |
| 1270 |
GTextField_Show(gt,gt->sel_start); |
| 1271 |
return( true ); |
| 1272 |
} |
| 1273 |
break; |
| 1274 |
case 'M': case 'm': case 'J': case 'j': |
| 1275 |
if ( !( event->u.chr.state&ksm_control ) ) |
| 1276 |
return( false ); |
| 1277 |
|
| 1278 |
case GK_Return: case GK_Linefeed: |
| 1279 |
if ( gt->accepts_returns ) { |
| 1280 |
GTextField_Replace(gt,newlinestr); |
| 1281 |
return( true ); |
| 1282 |
} |
| 1283 |
break; |
| 1284 |
case GK_Tab: |
| 1285 |
if ( gt->completionfield && ((GCompletionField *) gt)->completion!=NULL ) { |
| 1286 |
GTextFieldComplete(gt,true); |
| 1287 |
gt->was_completing = true; |
| 1288 |
return( 3 ); |
| 1289 |
} |
| 1290 |
if ( gt->accepts_tabs ) { |
| 1291 |
GTextField_Replace(gt,tabstr); |
| 1292 |
return( true ); |
| 1293 |
} |
| 1294 |
break; |
| 1295 |
default: |
| 1296 |
if ( GMenuIsCommand(event,H_("Select All|Ctl+A")) ) { |
| 1297 |
gtextfield_editcmd(>->g,ec_selectall); |
| 1298 |
return( 2 ); |
| 1299 |
} else if ( GMenuIsCommand(event,H_("Copy|Ctl+C")) ) { |
| 1300 |
gtextfield_editcmd(>->g,ec_copy); |
| 1301 |
} else if ( GMenuIsCommand(event,H_("Paste|Ctl+V")) ) { |
| 1302 |
gtextfield_editcmd(>->g,ec_paste); |
| 1303 |
GTextField_Show(gt,gt->sel_start); |
| 1304 |
return( true ); |
| 1305 |
} else if ( GMenuIsCommand(event,H_("Cut|Ctl+X")) ) { |
| 1306 |
gtextfield_editcmd(>->g,ec_cut); |
| 1307 |
GTextField_Show(gt,gt->sel_start); |
| 1308 |
return( true ); |
| 1309 |
} else if ( GMenuIsCommand(event,H_("Undo|Ctl+Z")) ) { |
| 1310 |
gtextfield_editcmd(>->g,ec_undo); |
| 1311 |
GTextField_Show(gt,gt->sel_start); |
| 1312 |
return( true ); |
| 1313 |
} else if ( GMenuIsCommand(event,H_("Save|Ctl+S")) ) { |
| 1314 |
GTextFieldSave(gt,true); |
| 1315 |
return( 2 ); |
| 1316 |
} else if ( GMenuIsCommand(event,H_("Import...|Ctl+Shft+I")) ) { |
| 1317 |
GTextFieldImport(gt); |
| 1318 |
return( true ); |
| 1319 |
} else |
| 1320 |
return( false ); |
| 1321 |
break; |
| 1322 |
} |
| 1323 |
} else { |
| 1324 |
GTextField_Replace(gt,event->u.chr.chars); |
| 1325 |
return( 4 ); |
| 1326 |
} |
| 1327 |
|
| 1328 |
if ( gt->sel_start == gt->sel_end ) |
| 1329 |
gt->sel_base = gt->sel_start; |
| 1330 |
if ( ss!=gt->sel_start || se!=gt->sel_end ) |
| 1331 |
GTextFieldGrabPrimarySelection(gt); |
| 1332 |
return( false ); |
| 1333 |
} |
| 1334 |
|
| 1335 |
static void _gt_cursor_pos(GTextField *gt, int sel_start, int *x, int *y) { |
| 1336 |
int l, sel; |
| 1337 |
|
| 1338 |
*x = -1; *y= -1; |
| 1339 |
GDrawSetFont(gt->g.base,gt->font); |
| 1340 |
l = GTextFieldFindLine(gt,sel_start); |
| 1341 |
if ( l<gt->loff_top || l>=gt->loff_top + ((gt->g.inner.height+gt->fh/2)/gt->fh)) |
| 1342 |
return; |
| 1343 |
*y = (l-gt->loff_top)*gt->fh; |
| 1344 |
GRect pos_rect; |
| 1345 |
int ll = gt->lines8[l+1]==-1 ? -1 : gt->lines8[l+1]-gt->lines8[l]; |
| 1346 |
sel = u2utf8_index(sel_start-gt->lines[l],gt->utf8_text+gt->lines8[l]); |
| 1347 |
GDrawLayoutInit(gt->g.base,gt->utf8_text+gt->lines8[l],ll,NULL); |
| 1348 |
GDrawLayoutIndexToPos(gt->g.base,sel,&pos_rect); |
| 1349 |
*x = pos_rect.x - gt->xoff_left; |
| 1350 |
} |
| 1351 |
|
| 1352 |
static void gt_cursor_pos(GTextField *gt, int *x, int *y) { |
| 1353 |
_gt_cursor_pos(gt,gt->sel_start,x,y); |
| 1354 |
} |
| 1355 |
|
| 1356 |
static void GTPositionGIC(GTextField *gt) { |
| 1357 |
int x,y; |
| 1358 |
|
| 1359 |
if ( !gt->g.has_focus || gt->gic==NULL ) |
| 1360 |
return; |
| 1361 |
gt_cursor_pos(gt,&x,&y); |
| 1362 |
if ( x<0 ) |
| 1363 |
return; |
| 1364 |
GDrawSetGIC(gt->g.base,gt->gic,gt->g.inner.x+x,gt->g.inner.y+y+gt->as); |
| 1365 |
} |
| 1366 |
|
| 1367 |
static void gt_set_dd_cursor(GTextField *gt, int pos) { |
| 1368 |
gt->has_dd_cursor = true; |
| 1369 |
gt->dd_cursor_pos = pos; |
| 1370 |
} |
| 1371 |
|
| 1372 |
static void gt_request_redraw_cursor(GWindow pixmap, GTextField *gt) { |
| 1373 |
GRect clip; |
| 1374 |
int x, y; |
| 1375 |
|
| 1376 |
gt_cursor_pos(gt,&x,&y); |
| 1377 |
|
| 1378 |
if ( x<0 || x>=gt->g.inner.width ) |
| 1379 |
return; |
| 1380 |
|
| 1381 |
clip.x = gt->g.inner.x + x; |
| 1382 |
clip.y = gt->g.inner.y + y; |
| 1383 |
clip.width = 1; |
| 1384 |
clip.height = gt->fh + 1; |
| 1385 |
|
| 1386 |
GDrawRequestExpose(pixmap, &clip, false); |
| 1387 |
} |
| 1388 |
|
| 1389 |
static void gt_draw_cursor(GWindow pixmap, GTextField *gt) { |
| 1390 |
int x, y, l; |
| 1391 |
|
| 1392 |
if (gt->has_dd_cursor) { |
| 1393 |
gt->has_dd_cursor = false; |
| 1394 |
l = GTextFieldFindLine(gt, gt->dd_cursor_pos); |
| 1395 |
if (l < gt->loff_top || l >= gt->loff_top + (gt->g.inner.height/gt->fh)) |
| 1396 |
return; |
| 1397 |
_gt_cursor_pos(gt,gt->dd_cursor_pos,&x,&y); |
| 1398 |
if (x < 0 || x >= gt->g.inner.width) |
| 1399 |
return; |
| 1400 |
GDrawSetLineWidth(gt->g.base,0); |
| 1401 |
GDrawSetDashedLine(gt->g.base,2,2,0); |
| 1402 |
GDrawDrawLine(gt->g.base,gt->g.inner.x+x,gt->g.inner.y+y, |
| 1403 |
gt->g.inner.x+x,gt->g.inner.y+y+gt->fh,0); |
| 1404 |
GDrawSetDashedLine(gt->g.base,0,0,0); |
| 1405 |
return; |
| 1406 |
} |
| 1407 |
|
| 1408 |
if ( !gt->cursor_on || gt->sel_start != gt->sel_end ) |
| 1409 |
return; |
| 1410 |
gt_cursor_pos(gt,&x,&y); |
| 1411 |
|
| 1412 |
if ( x<0 || x>=gt->g.inner.width ) |
| 1413 |
return; |
| 1414 |
GDrawDrawLine(pixmap,gt->g.inner.x+x,gt->g.inner.y+y, |
| 1415 |
gt->g.inner.x+x,gt->g.inner.y+y+gt->fh, 0); |
| 1416 |
} |
| 1417 |
|
| 1418 |
static void GTextFieldDrawLineSel(GWindow pixmap, GTextField *gt, int line ) { |
| 1419 |
GRect selr, sofar, nextch; |
| 1420 |
int s,e, y,llen,i,j; |
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
y = gt->g.inner.y+(line-gt->loff_top)*gt->fh; |
| 1425 |
selr = gt->g.inner; selr.y = y; selr.height = gt->fh; |
| 1426 |
if ( !gt->g.has_focus ) --selr.height; |
| 1427 |
llen = gt->lines[line+1]==-1? |
| 1428 |
u_strlen(gt->text+gt->lines[line])+gt->lines[line]: |
| 1429 |
gt->lines[line+1]; |
| 1430 |
s = gt->sel_start<gt->lines[line]?gt->lines[line]:gt->sel_start; |
| 1431 |
e = gt->sel_end>gt->lines[line+1] && gt->lines[line+1]!=-1?gt->lines[line+1]-1: |
| 1432 |
gt->sel_end; |
| 1433 |
|
| 1434 |
s = u2utf8_index(s-gt->lines[line],gt->utf8_text+gt->lines8[line]); |
| 1435 |
e = u2utf8_index(e-gt->lines[line],gt->utf8_text+gt->lines8[line]); |
| 1436 |
llen = gt->lines8[line+1]==-1? -1 : gt->lines8[line+1]-gt->lines8[line]; |
| 1437 |
GDrawLayoutInit(pixmap,gt->utf8_text+gt->lines8[line],llen,NULL); |
| 1438 |
for ( i=s; i<e; ) { |
| 1439 |
GDrawLayoutIndexToPos(pixmap,i,&sofar); |
| 1440 |
for ( j=i+1; j<e; ++j ) { |
| 1441 |
GDrawLayoutIndexToPos(pixmap,j,&nextch); |
| 1442 |
if ( nextch.x != sofar.x+sofar.width ) |
| 1443 |
break; |
| 1444 |
sofar.width += nextch.width; |
| 1445 |
} |
| 1446 |
if ( sofar.width<0 ) { |
| 1447 |
selr.x = sofar.x+sofar.width + gt->g.inner.x - gt->xoff_left; |
| 1448 |
selr.width = -sofar.width; |
| 1449 |
} else { |
| 1450 |
selr.x = sofar.x + gt->g.inner.x - gt->xoff_left; |
| 1451 |
selr.width = sofar.width; |
| 1452 |
} |
| 1453 |
GDrawFillRect(pixmap,&selr,gt->g.box->active_border); |
| 1454 |
i = j; |
| 1455 |
} |
| 1456 |
} |
| 1457 |
|
| 1458 |
static void GTextFieldDrawLine(GWindow pixmap, GTextField *gt, int line, Color fg ) { |
| 1459 |
int y = gt->g.inner.y+(line-gt->loff_top)*gt->fh; |
| 1460 |
int ll = gt->lines[line+1]==-1 ? -1 : gt->lines[line+1]-gt->lines[line]; |
| 1461 |
|
| 1462 |
ll = gt->lines8[line+1]==-1? -1 : gt->lines8[line+1]-gt->lines8[line]; |
| 1463 |
GDrawLayoutInit(pixmap,gt->utf8_text+gt->lines8[line],ll,NULL); |
| 1464 |
GDrawLayoutDraw(pixmap,gt->g.inner.x-gt->xoff_left,y+gt->as,fg); |
| 1465 |
} |
| 1466 |
|
| 1467 |
static int gtextfield_expose(GWindow pixmap, GGadget *g, GEvent *event) { |
| 1468 |
GTextField *gt = (GTextField *) g; |
| 1469 |
GListField *ge = (GListField *) g; |
| 1470 |
GRect old1, old2, old3, *r = &g->r; |
| 1471 |
Color fg; |
| 1472 |
int i, last; |
| 1473 |
GRect unpadded_inner; |
| 1474 |
int pad; |
| 1475 |
|
| 1476 |
if ( g->state == gs_invisible || gt->dontdraw ) |
| 1477 |
return( false ); |
| 1478 |
|
| 1479 |
if ( gt->listfield || gt->numericfield ) r = &ge->fieldrect; |
| 1480 |
|
| 1481 |
unpadded_inner = g->inner; |
| 1482 |
pad = GDrawPointsToPixels(g->base,g->box->padding); |
| 1483 |
unpadded_inner.x -= pad; unpadded_inner.y -= pad; |
| 1484 |
unpadded_inner.width += 2*pad; unpadded_inner.height += 2*pad; |
| 1485 |
|
| 1486 |
GDrawPushClip(pixmap,&event->u.expose.rect, &old1); |
| 1487 |
GDrawPushClip(pixmap,r,&old2); |
| 1488 |
if (!GDrawClipContains(pixmap, &unpadded_inner, true)) { |
| 1489 |
GBoxDrawBackground(pixmap,r,g->box, |
| 1490 |
g->state==gs_enabled? gs_pressedactive: g->state,false); |
| 1491 |
GBoxDrawBorder(pixmap,r,g->box,g->state,false); |
| 1492 |
} else { |
| 1493 |
|
| 1494 |
enum border_shape old = g->box->border_shape; |
| 1495 |
g->box->border_shape = bs_rect; |
| 1496 |
GBoxDrawBackground(pixmap,r,g->box, |
| 1497 |
g->state==gs_enabled? gs_pressedactive: g->state,false); |
| 1498 |
g->box->border_shape = old; |
| 1499 |
} |
| 1500 |
|
| 1501 |
GDrawPushClip(pixmap,&unpadded_inner,&old3); |
| 1502 |
GDrawGetClip(pixmap,&unpadded_inner); |
| 1503 |
GDrawSetFont(pixmap,gt->font); |
| 1504 |
|
| 1505 |
fg = g->state==gs_disabled?g->box->disabled_foreground: |
| 1506 |
g->box->main_foreground==COLOR_DEFAULT?GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(pixmap)): |
| 1507 |
g->box->main_foreground; |
| 1508 |
if ( (last = gt->g.inner.height/gt->fh)==0 ) last = 1; |
| 1509 |
if ( gt->sel_start != gt->sel_end ) { |
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
for ( i=gt->loff_top; i<gt->loff_top+last && gt->lines[i]!=-1; ++i ) { |
| 1515 |
if ( gt->sel_end>gt->lines[i] && |
| 1516 |
(gt->lines[i+1]==-1 || gt->sel_start<gt->lines[i+1])) |
| 1517 |
GTextFieldDrawLineSel(pixmap,gt,i); |
| 1518 |
} |
| 1519 |
} |
| 1520 |
for ( i=gt->loff_top; i<gt->loff_top+last && gt->lines[i]!=-1; ++i ) { |
| 1521 |
int y = gt->g.inner.y+(i-gt->loff_top)*gt->fh+1; |
| 1522 |
if (unpadded_inner.y < (y+gt->fh-1) && unpadded_inner.y + unpadded_inner.height > y) { |
| 1523 |
GTextFieldDrawLine(pixmap,gt,i,fg); |
| 1524 |
} |
| 1525 |
} |
| 1526 |
|
| 1527 |
gt_draw_cursor(pixmap, gt); |
| 1528 |
GDrawPopClip(pixmap,&old3); |
| 1529 |
GDrawPopClip(pixmap,&old2); |
| 1530 |
|
| 1531 |
if ( (gt->listfield || gt->numericfield) && !GDrawClipOverlaps(pixmap, &ge->buttonrect)) { |
| 1532 |
GDrawPopClip(pixmap,&old1); |
| 1533 |
return true; |
| 1534 |
} |
| 1535 |
|
| 1536 |
if ( gt->listfield ) { |
| 1537 |
int marklen = GDrawPointsToPixels(pixmap,_GListMarkSize); |
| 1538 |
|
| 1539 |
GDrawPushClip(pixmap,&ge->buttonrect,&old2); |
| 1540 |
|
| 1541 |
GBoxDrawBackground(pixmap,&ge->buttonrect,&glistfieldmenu_box, |
| 1542 |
g->state==gs_enabled? gs_pressedactive: g->state,false); |
| 1543 |
GBoxDrawBorder(pixmap,&ge->buttonrect,&glistfieldmenu_box,g->state,false); |
| 1544 |
|
| 1545 |
GListMarkDraw(pixmap, |
| 1546 |
ge->buttonrect.x + (ge->buttonrect.width - marklen)/2, |
| 1547 |
g->inner.y, |
| 1548 |
g->inner.height, |
| 1549 |
g->state); |
| 1550 |
GDrawPopClip(pixmap,&old2); |
| 1551 |
} else if ( gt->numericfield ) { |
| 1552 |
int y, w; |
| 1553 |
int half; |
| 1554 |
GPoint pts[5]; |
| 1555 |
int bp = GBoxBorderWidth(gt->g.base,&gnumericfieldspinner_box); |
| 1556 |
Color fg = g->state==gs_disabled?gnumericfieldspinner_box.disabled_foreground: |
| 1557 |
gnumericfieldspinner_box.main_foreground==COLOR_DEFAULT?GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(pixmap)): |
| 1558 |
gnumericfieldspinner_box.main_foreground; |
| 1559 |
|
| 1560 |
GBoxDrawBackground(pixmap,&ge->buttonrect,&gnumericfieldspinner_box, |
| 1561 |
g->state==gs_enabled? gs_pressedactive: g->state,false); |
| 1562 |
GBoxDrawBorder(pixmap,&ge->buttonrect,&gnumericfieldspinner_box,g->state,false); |
| 1563 |
|
| 1564 |
|
| 1565 |
y = ge->buttonrect.y + ge->buttonrect.height/2; |
| 1566 |
w = ge->buttonrect.width; |
| 1567 |
w &= ~1; |
| 1568 |
pts[0].x = ge->buttonrect.x+3+bp; |
| 1569 |
pts[1].x = ge->buttonrect.x+w-3-bp; |
| 1570 |
pts[2].x = ge->buttonrect.x + w/2; |
| 1571 |
half = pts[2].x-pts[0].x; |
| 1572 |
GDrawDrawLine(pixmap, pts[0].x-3,y, pts[1].x+3,y, fg ); |
| 1573 |
pts[0].y = pts[1].y = y-2; |
| 1574 |
pts[2].y = pts[1].y-half; |
| 1575 |
pts[3] = pts[0]; |
| 1576 |
GDrawFillPoly(pixmap,pts,3,fg); |
| 1577 |
pts[0].y = pts[1].y = y+2; |
| 1578 |
pts[2].y = pts[1].y+half; |
| 1579 |
pts[3] = pts[0]; |
| 1580 |
GDrawFillPoly(pixmap,pts,3,fg); |
| 1581 |
} |
| 1582 |
GDrawPopClip(pixmap,&old1); |
| 1583 |
return( true ); |
| 1584 |
} |
| 1585 |
|
| 1586 |
static int glistfield_mouse(GListField *ge, GEvent *event) { |
| 1587 |
if ( event->type!=et_mousedown ) |
| 1588 |
return( true ); |
| 1589 |
if ( ge->popup != NULL ) { |
| 1590 |
GDrawDestroyWindow(ge->popup); |
| 1591 |
ge->popup = NULL; |
| 1592 |
return( true ); |
| 1593 |
} |
| 1594 |
ge->popup = GListPopupCreate(&ge->gt.g,GListFieldSelected,ge->ti); |
| 1595 |
return( true ); |
| 1596 |
} |
| 1597 |
|
| 1598 |
static int gnumericfield_mouse(GTextField *gt, GEvent *event) { |
| 1599 |
GListField *ge = (GListField *) gt; |
| 1600 |
if ( event->type==et_mousedown ) { |
| 1601 |
gt->incr_down = event->u.mouse.y > (ge->buttonrect.y + ge->buttonrect.height/2); |
| 1602 |
GTextFieldIncrement(gt,gt->incr_down?-1:1); |
| 1603 |
if ( gt->numeric_scroll==NULL ) |
| 1604 |
gt->numeric_scroll = GDrawRequestTimer(gt->g.base,200,100,NULL); |
| 1605 |
} else if ( gt->numeric_scroll!=NULL ) { |
| 1606 |
GDrawCancelTimer(gt->numeric_scroll); |
| 1607 |
gt->numeric_scroll = NULL; |
| 1608 |
} |
| 1609 |
return( true ); |
| 1610 |
} |
| 1611 |
|
| 1612 |
static int GTextFieldDoDrop(GTextField *gt,GEvent *event,int endpos) { |
| 1613 |
if ( event->type == et_mousemove ) { |
| 1614 |
if ( GGadgetInnerWithin(>->g,event->u.mouse.x,event->u.mouse.y) ) { |
| 1615 |
if ( endpos<gt->sel_start || endpos>=gt->sel_end ) |
| 1616 |
gt_set_dd_cursor(gt, endpos); |
| 1617 |
} else if ( !GGadgetWithin(>->g,event->u.mouse.x,event->u.mouse.y) ) { |
| 1618 |
GDrawPostDragEvent(gt->g.base,event,et_drag); |
| 1619 |
} |
| 1620 |
} else { |
| 1621 |
if ( GGadgetInnerWithin(>->g,event->u.mouse.x,event->u.mouse.y) ) { |
| 1622 |
if ( endpos>=gt->sel_start && endpos<gt->sel_end ) { |
| 1623 |
gt->sel_start = gt->sel_end = endpos; |
| 1624 |
} else { |
| 1625 |
unichar_t *old=gt->oldtext, *temp; |
| 1626 |
int pos=0; |
| 1627 |
if ( event->u.mouse.state&ksm_control ) { |
| 1628 |
temp = malloc((u_strlen(gt->text)+gt->sel_end-gt->sel_start+1)*sizeof(unichar_t)); |
| 1629 |
memcpy(temp,gt->text,endpos*sizeof(unichar_t)); |
| 1630 |
memcpy(temp+endpos,gt->text+gt->sel_start, |
| 1631 |
(gt->sel_end-gt->sel_start)*sizeof(unichar_t)); |
| 1632 |
u_strcpy(temp+endpos+gt->sel_end-gt->sel_start,gt->text+endpos); |
| 1633 |
} else if ( endpos>=gt->sel_end ) { |
| 1634 |
temp = u_copy(gt->text); |
| 1635 |
memcpy(temp+gt->sel_start,temp+gt->sel_end, |
| 1636 |
(endpos-gt->sel_end)*sizeof(unichar_t)); |
| 1637 |
memcpy(temp+endpos-(gt->sel_end-gt->sel_start), |
| 1638 |
gt->text+gt->sel_start,(gt->sel_end-gt->sel_start)*sizeof(unichar_t)); |
| 1639 |
pos = endpos; |
| 1640 |
} else { |
| 1641 |
temp = u_copy(gt->text); |
| 1642 |
memcpy(temp+endpos,gt->text+gt->sel_start, |
| 1643 |
(gt->sel_end-gt->sel_start)*sizeof(unichar_t)); |
| 1644 |
memcpy(temp+endpos+gt->sel_end-gt->sel_start,gt->text+endpos, |
| 1645 |
(gt->sel_start-endpos)*sizeof(unichar_t)); |
| 1646 |
pos = endpos+gt->sel_end-gt->sel_start; |
| 1647 |
} |
| 1648 |
gt->oldtext = gt->text; |
| 1649 |
gt->sel_oldstart = gt->sel_start; |
| 1650 |
gt->sel_oldend = gt->sel_end; |
| 1651 |
gt->sel_oldbase = gt->sel_base; |
| 1652 |
gt->sel_start = gt->sel_end = pos; |
| 1653 |
gt->text = temp; |
| 1654 |
free(old); |
| 1655 |
GTextFieldRefigureLines(gt, endpos<gt->sel_oldstart?endpos:gt->sel_oldstart); |
| 1656 |
} |
| 1657 |
} else if ( !GGadgetWithin(>->g,event->u.mouse.x,event->u.mouse.y) ) { |
| 1658 |
|
| 1659 |
|
| 1660 |
if ( ( event->u.mouse.state&ksm_control ) ) |
| 1661 |
GTextFieldGrabSelection(gt,sn_drag_and_drop); |
| 1662 |
else |
| 1663 |
GTextFieldGrabDDSelection(gt); |
| 1664 |
GDrawPostDragEvent(gt->g.base,event,et_drop); |
| 1665 |
} |
| 1666 |
gt->drag_and_drop = false; |
| 1667 |
GDrawSetCursor(gt->g.base,gt->old_cursor); |
| 1668 |
} |
| 1669 |
_ggadget_redraw(>->g); |
| 1670 |
return( false ); |
| 1671 |
} |
| 1672 |
|
| 1673 |
static int gtextfield_mouse(GGadget *g, GEvent *event) { |
| 1674 |
GTextField *gt = (GTextField *) g; |
| 1675 |
GListField *ge = (GListField *) g; |
| 1676 |
unichar_t *end=NULL, *end1, *end2; |
| 1677 |
int i=0,ll,curlen; |
| 1678 |
|
| 1679 |
if ( gt->hidden_cursor ) { |
| 1680 |
GDrawSetCursor(gt->g.base,gt->old_cursor); |
| 1681 |
gt->hidden_cursor = false; |
| 1682 |
_GWidget_ClearGrabGadget(g); |
| 1683 |
} |
| 1684 |
if ( !g->takes_input || (g->state!=gs_enabled && g->state!=gs_active && g->state!=gs_focused )) |
| 1685 |
return( false ); |
| 1686 |
if ( event->type == et_crossing ) |
| 1687 |
return( false ); |
| 1688 |
if ( gt->completionfield && ((GCompletionField *) gt)->choice_popup!=NULL && |
| 1689 |
event->type==et_mousedown ) |
| 1690 |
GCompletionDestroy((GCompletionField *) gt); |
| 1691 |
if (( gt->listfield && event->u.mouse.x>=ge->buttonrect.x && |
| 1692 |
event->u.mouse.x<ge->buttonrect.x+ge->buttonrect.width && |
| 1693 |
event->u.mouse.y>=ge->buttonrect.y && |
| 1694 |
event->u.mouse.y<ge->buttonrect.y+ge->buttonrect.height ) || |
| 1695 |
( gt->listfield && ge->popup!=NULL )) |
| 1696 |
return( glistfield_mouse(ge,event)); |
| 1697 |
if ( gt->numericfield && event->u.mouse.x>=ge->buttonrect.x && |
| 1698 |
event->u.mouse.x<ge->buttonrect.x+ge->buttonrect.width && |
| 1699 |
event->u.mouse.y>=ge->buttonrect.y && |
| 1700 |
event->u.mouse.y<ge->buttonrect.y+ge->buttonrect.height ) |
| 1701 |
return( gnumericfield_mouse(gt,event)); |
| 1702 |
if (( event->type==et_mouseup || event->type==et_mousedown ) && |
| 1703 |
(event->u.mouse.button>=4 && event->u.mouse.button<=7)) { |
| 1704 |
int isv = event->u.mouse.button<=5; |
| 1705 |
if ( event->u.mouse.state&ksm_shift ) isv = !isv; |
| 1706 |
if ( isv && gt->vsb!=NULL ) |
| 1707 |
return( GGadgetDispatchEvent(>->vsb->g,event)); |
| 1708 |
else if ( !isv && gt->hsb!=NULL ) |
| 1709 |
return( GGadgetDispatchEvent(>->hsb->g,event)); |
| 1710 |
else |
| 1711 |
return( true ); |
| 1712 |
} |
| 1713 |
|
| 1714 |
if ( gt->pressed==NULL && event->type == et_mousemove && g->popup_msg!=NULL && |
| 1715 |
GGadgetWithin(g,event->u.mouse.x,event->u.mouse.y)) |
| 1716 |
GGadgetPreparePopup(g->base,g->popup_msg); |
| 1717 |
|
| 1718 |
curlen = u_strlen(gt->text); |
| 1719 |
|
| 1720 |
if ( event->type == et_mousedown || gt->pressed ) { |
| 1721 |
i = (event->u.mouse.y-g->inner.y)/gt->fh + gt->loff_top; |
| 1722 |
if ( i<0 ) i = 0; |
| 1723 |
if ( !gt->multi_line ) i = 0; |
| 1724 |
if ( i>=gt->lcnt ) { |
| 1725 |
end = gt->text+curlen; |
| 1726 |
i = gt->lcnt - 1; |
| 1727 |
if (i < 0) i = 0; |
| 1728 |
} else |
| 1729 |
end = GTextFieldGetPtFromPos(gt,i,event->u.mouse.x); |
| 1730 |
} |
| 1731 |
|
| 1732 |
if ( event->type == et_mousedown ) { |
| 1733 |
int end8; |
| 1734 |
if ( event->u.mouse.button==3 && |
| 1735 |
GGadgetWithin(g,event->u.mouse.x,event->u.mouse.y)) { |
| 1736 |
GTFPopupMenu(gt,event); |
| 1737 |
return( true ); |
| 1738 |
} |
| 1739 |
|
| 1740 |
ll = gt->lines8[i+1]==-1?-1:gt->lines8[i+1]-gt->lines8[i]-1; |
| 1741 |
GDrawLayoutInit(gt->g.base,gt->utf8_text+gt->lines8[i],ll,NULL); |
| 1742 |
end8 = GDrawLayoutXYToIndex(gt->g.base,event->u.mouse.x-g->inner.x+gt->xoff_left,0); |
| 1743 |
end1 = end2 = gt->text + gt->lines[i] + utf82u_index(end8,gt->utf8_text+gt->lines8[i]); |
| 1744 |
|
| 1745 |
gt->wordsel = gt->linesel = false; |
| 1746 |
if ( event->u.mouse.button==1 && event->u.mouse.clicks>=3 ) { |
| 1747 |
gt->sel_start = gt->lines[i]; gt->sel_end = gt->lines[i+1]; |
| 1748 |
if ( gt->sel_end==-1 ) gt->sel_end = curlen; |
| 1749 |
gt->wordsel = false; gt->linesel = true; |
| 1750 |
} else if ( event->u.mouse.button==1 && event->u.mouse.clicks==2 ) { |
| 1751 |
gt->sel_start = gt->sel_end = gt->sel_base = end-gt->text; |
| 1752 |
gt->wordsel = true; |
| 1753 |
GTextFieldSelectWords(gt,gt->sel_base); |
| 1754 |
} else if ( end1-gt->text>=gt->sel_start && end2-gt->text<gt->sel_end && |
| 1755 |
gt->sel_start!=gt->sel_end && |
| 1756 |
event->u.mouse.button==1 ) { |
| 1757 |
gt->drag_and_drop = true; |
| 1758 |
if ( !gt->hidden_cursor ) |
| 1759 |
gt->old_cursor = GDrawGetCursor(gt->g.base); |
| 1760 |
GDrawSetCursor(gt->g.base,ct_draganddrop); |
| 1761 |
} else if ( !(event->u.mouse.state&ksm_shift) ) { |
| 1762 |
if ( event->u.mouse.button==1 ) |
| 1763 |
GTextFieldGrabPrimarySelection(gt); |
| 1764 |
gt->sel_start = gt->sel_end = gt->sel_base = end-gt->text; |
| 1765 |
} else if ( end-gt->text>gt->sel_base ) { |
| 1766 |
gt->sel_start = gt->sel_base; |
| 1767 |
gt->sel_end = end-gt->text; |
| 1768 |
} else { |
| 1769 |
gt->sel_start = gt->sel_base = gt->sel_end = end-gt->text; |
| 1770 |
} |
| 1771 |
|
| 1772 |
if ( gt->pressed==NULL ) |
| 1773 |
gt->pressed = GDrawRequestTimer(gt->g.base,200,100,NULL); |
| 1774 |
if ( gt->sel_start > curlen ) |
| 1775 |
fprintf( stderr, "About to crash\n" ); |
| 1776 |
_ggadget_redraw(g); |
| 1777 |
return( true ); |
| 1778 |
} else if ( gt->pressed && (event->type == et_mousemove || event->type == et_mouseup )) { |
| 1779 |
int refresh = true; |
| 1780 |
|
| 1781 |
if ( gt->drag_and_drop ) { |
| 1782 |
refresh = GTextFieldDoDrop(gt,event,end-gt->text); |
| 1783 |
|
| 1784 |
} else if ( gt->linesel ) { |
| 1785 |
int j; |
| 1786 |
for ( j=i; j>0 && gt->text[gt->lines[j]-1] != '\n'; --j ) |
| 1787 |
; |
| 1788 |
gt->sel_start = gt->lines[j]; |
| 1789 |
for ( j=i+1; gt->lines[j]!=-1 && gt->text[gt->lines[j]-1] != '\n'; ++j ) |
| 1790 |
; |
| 1791 |
gt->sel_end = gt->lines[j]!=-1 ? gt->lines[j]-1 : curlen; |
| 1792 |
} else if ( gt->wordsel ) |
| 1793 |
GTextFieldSelectWords(gt,end-gt->text); |
| 1794 |
else if ( event->u.mouse.button!=2 ) { |
| 1795 |
int e = end-gt->text; |
| 1796 |
if ( e>gt->sel_base ) { |
| 1797 |
gt->sel_start = gt->sel_base; gt->sel_end = e; |
| 1798 |
} else { |
| 1799 |
gt->sel_start = e; gt->sel_end = gt->sel_base; |
| 1800 |
} |
| 1801 |
} |
| 1802 |
if ( event->type==et_mouseup ) { |
| 1803 |
GDrawCancelTimer(gt->pressed); gt->pressed = NULL; |
| 1804 |
if ( event->u.mouse.button==2 ) |
| 1805 |
GTextFieldPaste(gt,sn_primary); |
| 1806 |
if ( gt->sel_start==gt->sel_end ) |
| 1807 |
GTextField_Show(gt,gt->sel_start); |
| 1808 |
GTextFieldChanged(gt,-1); |
| 1809 |
} |
| 1810 |
if ( gt->sel_end > u_strlen(gt->text) ) |
| 1811 |
fprintf( stderr, "About to crash\n" ); |
| 1812 |
if ( refresh ) |
| 1813 |
_ggadget_redraw(g); |
| 1814 |
return( true ); |
| 1815 |
} |
| 1816 |
return( false ); |
| 1817 |
} |
| 1818 |
|
| 1819 |
static int gtextfield_key(GGadget *g, GEvent *event) { |
| 1820 |
GTextField *gt = (GTextField *) g; |
| 1821 |
|
| 1822 |
if ( !g->takes_input || (g->state!=gs_enabled && g->state!=gs_active && g->state!=gs_focused )) |
| 1823 |
return( false ); |
| 1824 |
if ( gt->listfield && ((GListField *) gt)->popup!=NULL ) { |
| 1825 |
GWindow popup = ((GListField *) gt)->popup; |
| 1826 |
(GDrawGetEH(popup))(popup,event); |
| 1827 |
return( true ); |
| 1828 |
} |
| 1829 |
|
| 1830 |
if ( gt->completionfield && ((GCompletionField *) gt)->choice_popup!=NULL && |
| 1831 |
GCompletionHandleKey(gt,event)) |
| 1832 |
return( true ); |
| 1833 |
|
| 1834 |
if ( event->type == et_charup ) |
| 1835 |
return( false ); |
| 1836 |
if ( event->u.chr.keysym == GK_F1 || event->u.chr.keysym == GK_Help || |
| 1837 |
(event->u.chr.keysym == GK_Return && !gt->accepts_returns ) || |
| 1838 |
( event->u.chr.keysym == GK_Tab && !gt->accepts_tabs ) || |
| 1839 |
event->u.chr.keysym == GK_BackTab || event->u.chr.keysym == GK_Escape ) |
| 1840 |
return( false ); |
| 1841 |
|
| 1842 |
if ( !gt->hidden_cursor ) { |
| 1843 |
if ( !gt->drag_and_drop ) |
| 1844 |
gt->old_cursor = GDrawGetCursor(gt->g.base); |
| 1845 |
GDrawSetCursor(g->base,ct_invisible); |
| 1846 |
gt->hidden_cursor = true; |
| 1847 |
_GWidget_SetGrabGadget(g); |
| 1848 |
} |
| 1849 |
gt->cursor_on = false; |
| 1850 |
|
| 1851 |
switch ( GTextFieldDoChange(gt,event)) { |
| 1852 |
case 4: |
| 1853 |
|
| 1854 |
if ( gt->completionfield && ((GCompletionField *) gt)->completion!=NULL && |
| 1855 |
gt->was_completing && gt->sel_start == u_strlen(gt->text)) |
| 1856 |
GTextFieldComplete(gt,false); |
| 1857 |
else |
| 1858 |
GTextFieldChanged(gt,-1); |
| 1859 |
break; |
| 1860 |
case 3: |
| 1861 |
|
| 1862 |
break; |
| 1863 |
case 2: |
| 1864 |
break; |
| 1865 |
case true: |
| 1866 |
GTextFieldChanged(gt,-1); |
| 1867 |
break; |
| 1868 |
case false: |
| 1869 |
return( false ); |
| 1870 |
} |
| 1871 |
_ggadget_redraw(g); |
| 1872 |
return( true ); |
| 1873 |
} |
| 1874 |
|
| 1875 |
static int gtextfield_focus(GGadget *g, GEvent *event) { |
| 1876 |
GTextField *gt = (GTextField *) g; |
| 1877 |
|
| 1878 |
if ( g->state == gs_invisible || g->state == gs_disabled ) |
| 1879 |
return( false ); |
| 1880 |
|
| 1881 |
if ( gt->cursor!=NULL ) { |
| 1882 |
GDrawCancelTimer(gt->cursor); |
| 1883 |
gt->cursor = NULL; |
| 1884 |
gt->cursor_on = false; |
| 1885 |
} |
| 1886 |
if ( gt->hidden_cursor && !event->u.focus.gained_focus ) { |
| 1887 |
GDrawSetCursor(gt->g.base,gt->old_cursor); |
| 1888 |
gt->hidden_cursor = false; |
| 1889 |
} |
| 1890 |
gt->g.has_focus = event->u.focus.gained_focus; |
| 1891 |
if ( event->u.focus.gained_focus ) { |
| 1892 |
gt->cursor = GDrawRequestTimer(gt->g.base,400,400,NULL); |
| 1893 |
gt->cursor_on = true; |
| 1894 |
if ( event->u.focus.mnemonic_focus != mf_normal ) |
| 1895 |
GTextFieldSelect(>->g,0,-1); |
| 1896 |
if ( gt->gic!=NULL ) |
| 1897 |
GTPositionGIC(gt); |
| 1898 |
else if ( GWidgetGetInputContext(gt->g.base)!=NULL ) |
| 1899 |
GDrawSetGIC(gt->g.base,GWidgetGetInputContext(gt->g.base),10000,10000); |
| 1900 |
} |
| 1901 |
_ggadget_redraw(g); |
| 1902 |
GTextFieldFocusChanged(gt,event->u.focus.gained_focus); |
| 1903 |
return( true ); |
| 1904 |
} |
| 1905 |
|
| 1906 |
static int gtextfield_timer(GGadget *g, GEvent *event) { |
| 1907 |
GTextField *gt = (GTextField *) g; |
| 1908 |
|
| 1909 |
if ( !g->takes_input || (g->state!=gs_enabled && g->state!=gs_active && g->state!=gs_focused )) |
| 1910 |
return(false); |
| 1911 |
if ( gt->cursor == event->u.timer.timer && gt->sel_start == gt->sel_end ) { |
| 1912 |
gt->cursor_on = !gt->cursor_on; |
| 1913 |
gt_request_redraw_cursor(g->base, gt); |
| 1914 |
return( true ); |
| 1915 |
} |
| 1916 |
if ( gt->numeric_scroll == event->u.timer.timer ) { |
| 1917 |
GTextFieldIncrement(gt,gt->incr_down?-1:1); |
| 1918 |
return( true ); |
| 1919 |
} |
| 1920 |
if ( gt->pressed == event->u.timer.timer ) { |
| 1921 |
GEvent e; |
| 1922 |
GDrawSetFont(g->base,gt->font); |
| 1923 |
GDrawGetPointerPosition(g->base,&e); |
| 1924 |
if ( (e.u.mouse.x<g->r.x && gt->xoff_left>0 ) || |
| 1925 |
(gt->multi_line && e.u.mouse.y<g->r.y && gt->loff_top>0 ) || |
| 1926 |
( e.u.mouse.x >= g->r.x + g->r.width && |
| 1927 |
gt->xmax-gt->xoff_left>g->inner.width ) || |
| 1928 |
( e.u.mouse.y >= g->r.y + g->r.height && |
| 1929 |
gt->lcnt-gt->loff_top > g->inner.height/gt->fh )) { |
| 1930 |
int l = gt->loff_top + (e.u.mouse.y-g->inner.y)/gt->fh; |
| 1931 |
int xpos; unichar_t *end; |
| 1932 |
|
| 1933 |
if ( e.u.mouse.y<g->r.y && gt->loff_top>0 ) |
| 1934 |
l = --gt->loff_top; |
| 1935 |
else if ( e.u.mouse.y >= g->r.y + g->r.height && |
| 1936 |
gt->lcnt-gt->loff_top > g->inner.height/gt->fh ) { |
| 1937 |
++gt->loff_top; |
| 1938 |
l = gt->loff_top + g->inner.width/gt->fh; |
| 1939 |
} else if ( l<gt->loff_top ) |
| 1940 |
l = gt->loff_top; |
| 1941 |
else if ( l>=gt->loff_top + g->inner.height/gt->fh ) |
| 1942 |
l = gt->loff_top + g->inner.height/gt->fh-1; |
| 1943 |
if ( l>=gt->lcnt ) l = gt->lcnt-1; |
| 1944 |
|
| 1945 |
xpos = e.u.mouse.x+gt->xoff_left; |
| 1946 |
if ( e.u.mouse.x<g->r.x && gt->xoff_left>0 ) { |
| 1947 |
gt->xoff_left -= gt->nw; |
| 1948 |
xpos = g->inner.x + gt->xoff_left; |
| 1949 |
} else if ( e.u.mouse.x >= g->r.x + g->r.width && |
| 1950 |
gt->xmax-gt->xoff_left>g->inner.width ) { |
| 1951 |
gt->xoff_left += gt->nw; |
| 1952 |
xpos = g->inner.x + gt->xoff_left + g->inner.width; |
| 1953 |
} |
| 1954 |
|
| 1955 |
end = GTextFieldGetPtFromPos(gt,l,xpos); |
| 1956 |
if ( end-gt->text > gt->sel_base ) { |
| 1957 |
gt->sel_start = gt->sel_base; |
| 1958 |
gt->sel_end = end-gt->text; |
| 1959 |
} else { |
| 1960 |
gt->sel_start = end-gt->text; |
| 1961 |
gt->sel_end = gt->sel_base; |
| 1962 |
} |
| 1963 |
_ggadget_redraw(g); |
| 1964 |
if ( gt->vsb!=NULL ) |
| 1965 |
GScrollBarSetPos(>->vsb->g,gt->loff_top); |
| 1966 |
if ( gt->hsb!=NULL ) |
| 1967 |
GScrollBarSetPos(>->hsb->g,gt->xoff_left); |
| 1968 |
} |
| 1969 |
return( true ); |
| 1970 |
} |
| 1971 |
return( false ); |
| 1972 |
} |
| 1973 |
|
| 1974 |
static int gtextfield_sel(GGadget *g, GEvent *event) { |
| 1975 |
GTextField *gt = (GTextField *) g; |
| 1976 |
unichar_t *end; |
| 1977 |
int i; |
| 1978 |
|
| 1979 |
if ( event->type == et_selclear ) { |
| 1980 |
if ( event->u.selclear.sel==sn_primary && gt->sel_start!=gt->sel_end ) { |
| 1981 |
gt->sel_start = gt->sel_end = gt->sel_base; |
| 1982 |
_ggadget_redraw(g); |
| 1983 |
return( true ); |
| 1984 |
} |
| 1985 |
return( false ); |
| 1986 |
} |
| 1987 |
|
| 1988 |
GDrawSetFont(g->base,gt->font); |
| 1989 |
i = (event->u.drag_drop.y-g->inner.y)/gt->fh + gt->loff_top; |
| 1990 |
if ( !gt->multi_line ) i = 0; |
| 1991 |
if ( i>=gt->lcnt ) |
| 1992 |
end = gt->text+u_strlen(gt->text); |
| 1993 |
else |
| 1994 |
end = GTextFieldGetPtFromPos(gt,i,event->u.drag_drop.x); |
| 1995 |
if ( event->type == et_drag ) { |
| 1996 |
gt_set_dd_cursor(gt, end - gt->text); |
| 1997 |
} else if ( event->type == et_dragout ) { |
| 1998 |
|
| 1999 |
|
| 2000 |
} else if ( event->type == et_drop ) { |
| 2001 |
gt->sel_start = gt->sel_end = gt->sel_base = end-gt->text; |
| 2002 |
GTextFieldPaste(gt,sn_drag_and_drop); |
| 2003 |
GTextField_Show(gt,gt->sel_start); |
| 2004 |
GTextFieldChanged(gt,-1); |
| 2005 |
} else |
| 2006 |
return( false ); |
| 2007 |
|
| 2008 |
_ggadget_redraw(>->g); |
| 2009 |
return( true ); |
| 2010 |
} |
| 2011 |
|
| 2012 |
static void gtextfield_destroy(GGadget *g) { |
| 2013 |
GTextField *gt = (GTextField *) g; |
| 2014 |
|
| 2015 |
if ( gt==NULL ) |
| 2016 |
return; |
| 2017 |
if ( gt->listfield ) { |
| 2018 |
GListField *glf = (GListField *) g; |
| 2019 |
if ( glf->popup ) { |
| 2020 |
|
| 2021 |
|
| 2022 |
GEvent die; |
| 2023 |
die.type = et_close; |
| 2024 |
die.w = glf->popup; |
| 2025 |
GDrawPostEvent(&die); |
| 2026 |
} |
| 2027 |
GTextInfoArrayFree(glf->ti); |
| 2028 |
} |
| 2029 |
if ( gt->completionfield ) |
| 2030 |
GCompletionDestroy((GCompletionField *) g); |
| 2031 |
|
| 2032 |
if ( gt->vsb!=NULL ) |
| 2033 |
(gt->vsb->g.funcs->destroy)(>->vsb->g); |
| 2034 |
if ( gt->hsb!=NULL ) |
| 2035 |
(gt->hsb->g.funcs->destroy)(>->hsb->g); |
| 2036 |
GDrawCancelTimer(gt->numeric_scroll); |
| 2037 |
GDrawCancelTimer(gt->pressed); |
| 2038 |
GDrawCancelTimer(gt->cursor); |
| 2039 |
free(gt->lines); |
| 2040 |
free(gt->oldtext); |
| 2041 |
free(gt->text); |
| 2042 |
free(gt->utf8_text); |
| 2043 |
free(gt->lines8); |
| 2044 |
_ggadget_destroy(g); |
| 2045 |
} |
| 2046 |
|
| 2047 |
static void GTextFieldSetTitle(GGadget *g,const unichar_t *tit) { |
| 2048 |
GTextField *gt = (GTextField *) g; |
| 2049 |
unichar_t *old = gt->oldtext; |
| 2050 |
if ( tit==NULL || u_strcmp(tit,gt->text)==0 ) |
| 2051 |
return; |
| 2052 |
gt->oldtext = gt->text; |
| 2053 |
gt->sel_oldstart = gt->sel_start; gt->sel_oldend = gt->sel_end; gt->sel_oldbase = gt->sel_base; |
| 2054 |
gt->text = u_copy(tit); |
| 2055 |
free(old); |
| 2056 |
free(gt->utf8_text); |
| 2057 |
gt->utf8_text = u2utf8_copy(gt->text); |
| 2058 |
gt->sel_start = gt->sel_end = gt->sel_base = u_strlen(tit); |
| 2059 |
GTextFieldRefigureLines(gt,0); |
| 2060 |
GTextField_Show(gt,gt->sel_start); |
| 2061 |
_ggadget_redraw(g); |
| 2062 |
} |
| 2063 |
|
| 2064 |
static const unichar_t *_GTextFieldGetTitle(GGadget *g) { |
| 2065 |
GTextField *gt = (GTextField *) g; |
| 2066 |
return( gt->text ); |
| 2067 |
} |
| 2068 |
|
| 2069 |
static void GTextFieldSetFont(GGadget *g,FontInstance *new) { |
| 2070 |
GTextField *gt = (GTextField *) g; |
| 2071 |
gt->font = new; |
| 2072 |
GTextFieldRefigureLines(gt,0); |
| 2073 |
} |
| 2074 |
|
| 2075 |
static FontInstance *GTextFieldGetFont(GGadget *g) { |
| 2076 |
GTextField *gt = (GTextField *) g; |
| 2077 |
return( gt->font ); |
| 2078 |
} |
| 2079 |
|
| 2080 |
void GTextFieldShow(GGadget *g,int pos) { |
| 2081 |
GTextField *gt = (GTextField *) g; |
| 2082 |
|
| 2083 |
GTextField_Show(gt,pos); |
| 2084 |
_ggadget_redraw(g); |
| 2085 |
} |
| 2086 |
|
| 2087 |
void GTextFieldSelect(GGadget *g,int start, int end) { |
| 2088 |
GTextField *gt = (GTextField *) g; |
| 2089 |
|
| 2090 |
GTextFieldGrabPrimarySelection(gt); |
| 2091 |
if ( end<0 ) { |
| 2092 |
end = u_strlen(gt->text); |
| 2093 |
if ( start<0 ) start = end; |
| 2094 |
} |
| 2095 |
if ( start>end ) { int temp = start; start = end; end = temp; } |
| 2096 |
if ( end>u_strlen(gt->text)) end = u_strlen(gt->text); |
| 2097 |
if ( start>u_strlen(gt->text)) start = end; |
| 2098 |
else if ( start<0 ) start=0; |
| 2099 |
gt->sel_start = gt->sel_base = start; |
| 2100 |
gt->sel_end = end; |
| 2101 |
_ggadget_redraw(g); |
| 2102 |
} |
| 2103 |
|
| 2104 |
void GTextFieldReplace(GGadget *g,const unichar_t *txt) { |
| 2105 |
GTextField *gt = (GTextField *) g; |
| 2106 |
|
| 2107 |
GTextField_Replace(gt,txt); |
| 2108 |
_ggadget_redraw(g); |
| 2109 |
} |
| 2110 |
|
| 2111 |
static void GListFSelectOne(GGadget *g, int32_t pos) { |
| 2112 |
GListField *gl = (GListField *) g; |
| 2113 |
int i; |
| 2114 |
|
| 2115 |
for ( i=0; i<gl->ltot; ++i ) |
| 2116 |
gl->ti[i]->selected = false; |
| 2117 |
if ( pos>=gl->ltot ) pos = gl->ltot-1; |
| 2118 |
if ( pos<0 ) pos = 0; |
| 2119 |
if ( gl->ltot>0 ) { |
| 2120 |
gl->ti[pos]->selected = true; |
| 2121 |
GTextFieldSetTitle(g,gl->ti[pos]->text); |
| 2122 |
} |
| 2123 |
} |
| 2124 |
|
| 2125 |
static int32_t GListFIsSelected(GGadget *g, int32_t pos) { |
| 2126 |
GListField *gl = (GListField *) g; |
| 2127 |
|
| 2128 |
if ( pos>=gl->ltot ) |
| 2129 |
return( false ); |
| 2130 |
if ( pos<0 ) |
| 2131 |
return( false ); |
| 2132 |
if ( gl->ltot>0 ) |
| 2133 |
return( gl->ti[pos]->selected ); |
| 2134 |
|
| 2135 |
return( false ); |
| 2136 |
} |
| 2137 |
|
| 2138 |
static int32_t GListFGetFirst(GGadget *g) { |
| 2139 |
int i; |
| 2140 |
GListField *gl = (GListField *) g; |
| 2141 |
|
| 2142 |
for ( i=0; i<gl->ltot; ++i ) |
| 2143 |
if ( gl->ti[i]->selected ) |
| 2144 |
return( i ); |
| 2145 |
|
| 2146 |
return( -1 ); |
| 2147 |
} |
| 2148 |
|
| 2149 |
static GTextInfo **GListFGet(GGadget *g,int32_t *len) { |
| 2150 |
GListField *gl = (GListField *) g; |
| 2151 |
if ( len!=NULL ) *len = gl->ltot; |
| 2152 |
return( gl->ti ); |
| 2153 |
} |
| 2154 |
|
| 2155 |
static GTextInfo *GListFGetItem(GGadget *g,int32_t pos) { |
| 2156 |
GListField *gl = (GListField *) g; |
| 2157 |
if ( pos<0 || pos>=gl->ltot ) |
| 2158 |
return( NULL ); |
| 2159 |
|
| 2160 |
return(gl->ti[pos]); |
| 2161 |
} |
| 2162 |
|
| 2163 |
static void GListFSet(GGadget *g,GTextInfo **ti,int32_t docopy) { |
| 2164 |
GListField *gl = (GListField *) g; |
| 2165 |
|
| 2166 |
GTextInfoArrayFree(gl->ti); |
| 2167 |
if ( docopy || ti==NULL ) |
| 2168 |
ti = GTextInfoArrayCopy(ti); |
| 2169 |
gl->ti = ti; |
| 2170 |
gl->ltot = GTextInfoArrayCount(ti); |
| 2171 |
} |
| 2172 |
|
| 2173 |
static void GListFClear(GGadget *g) { |
| 2174 |
GListFSet(g,NULL,true); |
| 2175 |
} |
| 2176 |
|
| 2177 |
static void gtextfield_redraw(GGadget *g) { |
| 2178 |
GTextField *gt = (GTextField *) g; |
| 2179 |
if ( gt->vsb!=NULL ) |
| 2180 |
_ggadget_redraw((GGadget *) (gt->vsb)); |
| 2181 |
if ( gt->hsb!=NULL ) |
| 2182 |
_ggadget_redraw((GGadget *) (gt->hsb)); |
| 2183 |
_ggadget_redraw(g); |
| 2184 |
} |
| 2185 |
|
| 2186 |
static void gtextfield_move(GGadget *g, int32_t x, int32_t y ) { |
| 2187 |
GTextField *gt = (GTextField *) g; |
| 2188 |
int fxo=0, fyo=0, bxo=0, byo=0; |
| 2189 |
|
| 2190 |
if ( gt->listfield || gt->numericfield ) { |
| 2191 |
fxo = ((GListField *) gt)->fieldrect.x - g->r.x; |
| 2192 |
fyo = ((GListField *) gt)->fieldrect.y - g->r.y; |
| 2193 |
bxo = ((GListField *) gt)->buttonrect.x - g->r.x; |
| 2194 |
byo = ((GListField *) gt)->buttonrect.y - g->r.y; |
| 2195 |
} |
| 2196 |
if ( gt->vsb!=NULL ) |
| 2197 |
_ggadget_move((GGadget *) (gt->vsb),x+(gt->vsb->g.r.x-g->r.x),y); |
| 2198 |
if ( gt->hsb!=NULL ) |
| 2199 |
_ggadget_move((GGadget *) (gt->hsb),x,y+(gt->hsb->g.r.y-g->r.y)); |
| 2200 |
_ggadget_move(g,x,y); |
| 2201 |
if ( gt->listfield || gt->numericfield ) { |
| 2202 |
((GListField *) gt)->fieldrect.x = g->r.x + fxo; |
| 2203 |
((GListField *) gt)->fieldrect.y = g->r.y + fyo; |
| 2204 |
((GListField *) gt)->buttonrect.x = g->r.x + bxo; |
| 2205 |
((GListField *) gt)->buttonrect.y = g->r.y + byo; |
| 2206 |
} |
| 2207 |
} |
| 2208 |
|
| 2209 |
static void gtextfield_resize(GGadget *g, int32_t width, int32_t height ) { |
| 2210 |
GTextField *gt = (GTextField *) g; |
| 2211 |
int gtwidth=width, gtheight=height, oldheight=0; |
| 2212 |
int fxo=0, fwo=0, fyo=0, bxo=0, byo=0; |
| 2213 |
int l; |
| 2214 |
|
| 2215 |
if ( gt->listfield || gt->numericfield ) { |
| 2216 |
fxo = ((GListField *) gt)->fieldrect.x - g->r.x; |
| 2217 |
fwo = g->r.width - ((GListField *) gt)->fieldrect.width; |
| 2218 |
fyo = ((GListField *) gt)->fieldrect.y - g->r.y; |
| 2219 |
bxo = g->r.x+g->r.width - ((GListField *) gt)->buttonrect.x; |
| 2220 |
byo = ((GListField *) gt)->buttonrect.y - g->r.y; |
| 2221 |
} |
| 2222 |
if ( gt->hsb!=NULL ) { |
| 2223 |
oldheight = gt->hsb->g.r.y+gt->hsb->g.r.height-g->r.y; |
| 2224 |
gtheight = height - (oldheight-g->r.height); |
| 2225 |
} |
| 2226 |
if ( gt->vsb!=NULL ) { |
| 2227 |
int oldwidth = gt->vsb->g.r.x+gt->vsb->g.r.width-g->r.x; |
| 2228 |
gtwidth = width - (oldwidth-g->r.width); |
| 2229 |
_ggadget_move((GGadget *) (gt->vsb),gt->vsb->g.r.x+width-oldwidth,gt->vsb->g.r.y); |
| 2230 |
_ggadget_resize((GGadget *) (gt->vsb),gt->vsb->g.r.width,gtheight); |
| 2231 |
} |
| 2232 |
if ( gt->hsb!=NULL ) { |
| 2233 |
_ggadget_move((GGadget *) (gt->hsb),gt->hsb->g.r.x,gt->hsb->g.r.y+height-oldheight); |
| 2234 |
_ggadget_resize((GGadget *) (gt->hsb),gtwidth,gt->hsb->g.r.height); |
| 2235 |
} |
| 2236 |
_ggadget_resize(g,gtwidth, gtheight); |
| 2237 |
|
| 2238 |
if ( gt->hsb==NULL && gt->xoff_left!=0 && !gt->multi_line && |
| 2239 |
GDrawGetTextWidth(gt->g.base,gt->text,-1)<gt->g.inner.width ) |
| 2240 |
gt->xoff_left = 0; |
| 2241 |
|
| 2242 |
GTextFieldRefigureLines(gt,0); |
| 2243 |
if ( gt->vsb!=NULL ) { |
| 2244 |
GScrollBarSetBounds(>->vsb->g,0,gt->lcnt, |
| 2245 |
gt->g.inner.height<gt->fh ? 1 : gt->g.inner.height/gt->fh); |
| 2246 |
l = gt->loff_top; |
| 2247 |
if ( gt->loff_top>gt->lcnt-gt->g.inner.height/gt->fh ) |
| 2248 |
l = gt->lcnt-gt->g.inner.height/gt->fh; |
| 2249 |
if ( l<0 ) l = 0; |
| 2250 |
if ( l!=gt->loff_top ) { |
| 2251 |
gt->loff_top = l; |
| 2252 |
GScrollBarSetPos(>->vsb->g,l); |
| 2253 |
_ggadget_redraw(>->g); |
| 2254 |
} |
| 2255 |
} |
| 2256 |
if ( gt->listfield || gt->numericfield) { |
| 2257 |
((GListField *) gt)->fieldrect.x = g->r.x + fxo; |
| 2258 |
((GListField *) gt)->fieldrect.width = g->r.width -fwo; |
| 2259 |
((GListField *) gt)->fieldrect.y = g->r.y + fyo; |
| 2260 |
((GListField *) gt)->buttonrect.x = g->r.x+g->r.width - bxo; |
| 2261 |
((GListField *) gt)->buttonrect.y = g->r.y + byo; |
| 2262 |
} |
| 2263 |
} |
| 2264 |
|
| 2265 |
static GRect *gtextfield_getsize(GGadget *g, GRect *r ) { |
| 2266 |
GTextField *gt = (GTextField *) g; |
| 2267 |
_ggadget_getsize(g,r); |
| 2268 |
if ( gt->vsb!=NULL ) |
| 2269 |
r->width = gt->vsb->g.r.x+gt->vsb->g.r.width-g->r.x; |
| 2270 |
if ( gt->hsb!=NULL ) |
| 2271 |
r->height = gt->hsb->g.r.y+gt->hsb->g.r.height-g->r.y; |
| 2272 |
return( r ); |
| 2273 |
} |
| 2274 |
|
| 2275 |
static void gtextfield_setvisible(GGadget *g, int visible ) { |
| 2276 |
GTextField *gt = (GTextField *) g; |
| 2277 |
if ( gt->vsb!=NULL ) _ggadget_setvisible(>->vsb->g,visible); |
| 2278 |
if ( gt->hsb!=NULL ) _ggadget_setvisible(>->hsb->g,visible); |
| 2279 |
_ggadget_setvisible(g,visible); |
| 2280 |
} |
| 2281 |
|
| 2282 |
static void gtextfield_setenabled(GGadget *g, int enabled ) { |
| 2283 |
GTextField *gt = (GTextField *) g; |
| 2284 |
if ( gt->vsb!=NULL ) _ggadget_setenabled(>->vsb->g,enabled); |
| 2285 |
if ( gt->hsb!=NULL ) _ggadget_setenabled(>->hsb->g,enabled); |
| 2286 |
_ggadget_setenabled(g,enabled); |
| 2287 |
} |
| 2288 |
|
| 2289 |
static int gtextfield_vscroll(GGadget *g, GEvent *event) { |
| 2290 |
enum sb sbt = event->u.control.u.sb.type; |
| 2291 |
GTextField *gt = (GTextField *) (g->data); |
| 2292 |
int loff = gt->loff_top; |
| 2293 |
|
| 2294 |
g = (GGadget *) gt; |
| 2295 |
|
| 2296 |
if ( sbt==et_sb_top ) |
| 2297 |
loff = 0; |
| 2298 |
else if ( sbt==et_sb_bottom ) { |
| 2299 |
loff = gt->lcnt - gt->g.inner.height/gt->fh; |
| 2300 |
} else if ( sbt==et_sb_up ) { |
| 2301 |
if ( gt->loff_top!=0 ) loff = gt->loff_top-1; else loff = 0; |
| 2302 |
} else if ( sbt==et_sb_down ) { |
| 2303 |
if ( gt->loff_top + gt->g.inner.height/gt->fh >= gt->lcnt ) |
| 2304 |
loff = gt->lcnt - gt->g.inner.height/gt->fh; |
| 2305 |
else |
| 2306 |
++loff; |
| 2307 |
} else if ( sbt==et_sb_uppage ) { |
| 2308 |
int page = g->inner.height/gt->fh- (g->inner.height/gt->fh>2?1:0); |
| 2309 |
loff = gt->loff_top - page; |
| 2310 |
if ( loff<0 ) loff=0; |
| 2311 |
} else if ( sbt==et_sb_downpage ) { |
| 2312 |
int page = g->inner.height/gt->fh- (g->inner.height/gt->fh>2?1:0); |
| 2313 |
loff = gt->loff_top + page; |
| 2314 |
if ( loff + gt->g.inner.height/gt->fh >= gt->lcnt ) |
| 2315 |
loff = gt->lcnt - gt->g.inner.height/gt->fh; |
| 2316 |
} else { |
| 2317 |
loff = event->u.control.u.sb.pos; |
| 2318 |
} |
| 2319 |
if ( loff + gt->g.inner.height/gt->fh >= gt->lcnt ) |
| 2320 |
loff = gt->lcnt - gt->g.inner.height/gt->fh; |
| 2321 |
if ( loff<0 ) loff = 0; |
| 2322 |
if ( loff!=gt->loff_top ) { |
| 2323 |
gt->loff_top = loff; |
| 2324 |
GScrollBarSetPos(>->vsb->g,loff); |
| 2325 |
_ggadget_redraw(>->g); |
| 2326 |
} |
| 2327 |
return( true ); |
| 2328 |
} |
| 2329 |
|
| 2330 |
static int gtextfield_hscroll(GGadget *g, GEvent *event) { |
| 2331 |
enum sb sbt = event->u.control.u.sb.type; |
| 2332 |
GTextField *gt = (GTextField *) (g->data); |
| 2333 |
int xoff = gt->xoff_left; |
| 2334 |
|
| 2335 |
g = (GGadget *) gt; |
| 2336 |
|
| 2337 |
if ( sbt==et_sb_top ) |
| 2338 |
xoff = 0; |
| 2339 |
else if ( sbt==et_sb_bottom ) { |
| 2340 |
xoff = gt->xmax - gt->g.inner.width; |
| 2341 |
if ( xoff<0 ) xoff = 0; |
| 2342 |
} else if ( sbt==et_sb_up ) { |
| 2343 |
if ( gt->xoff_left>gt->nw ) xoff = gt->xoff_left-gt->nw; else xoff = 0; |
| 2344 |
} else if ( sbt==et_sb_down ) { |
| 2345 |
if ( gt->xoff_left + gt->nw + gt->g.inner.width >= gt->xmax ) |
| 2346 |
xoff = gt->xmax - gt->g.inner.width; |
| 2347 |
else |
| 2348 |
xoff += gt->nw; |
| 2349 |
} else if ( sbt==et_sb_uppage ) { |
| 2350 |
int page = (3*g->inner.width)/4; |
| 2351 |
xoff = gt->xoff_left - page; |
| 2352 |
if ( xoff<0 ) xoff=0; |
| 2353 |
} else if ( sbt==et_sb_downpage ) { |
| 2354 |
int page = (3*g->inner.width)/4; |
| 2355 |
xoff = gt->xoff_left + page; |
| 2356 |
if ( xoff + gt->g.inner.width >= gt->xmax ) |
| 2357 |
xoff = gt->xmax - gt->g.inner.width; |
| 2358 |
} else { |
| 2359 |
xoff = event->u.control.u.sb.pos; |
| 2360 |
} |
| 2361 |
if ( xoff + gt->g.inner.width >= gt->xmax ) |
| 2362 |
xoff = gt->xmax - gt->g.inner.width; |
| 2363 |
if ( xoff<0 ) xoff = 0; |
| 2364 |
if ( gt->xoff_left!=xoff ) { |
| 2365 |
gt->xoff_left = xoff; |
| 2366 |
GScrollBarSetPos(>->hsb->g,xoff); |
| 2367 |
_ggadget_redraw(>->g); |
| 2368 |
} |
| 2369 |
return( true ); |
| 2370 |
} |
| 2371 |
|
| 2372 |
static void GTextFieldSetDesiredSize(GGadget *g,GRect *outer,GRect *inner) { |
| 2373 |
GTextField *gt = (GTextField *) g; |
| 2374 |
|
| 2375 |
if ( outer!=NULL ) { |
| 2376 |
g->desired_width = outer->width; |
| 2377 |
g->desired_height = outer->height; |
| 2378 |
} else if ( inner!=NULL ) { |
| 2379 |
int bp = GBoxBorderWidth(g->base,g->box); |
| 2380 |
int extra=0; |
| 2381 |
|
| 2382 |
if ( gt->listfield ) { |
| 2383 |
extra = GDrawPointsToPixels(gt->g.base,_GListMarkSize) + |
| 2384 |
GDrawPointsToPixels(gt->g.base,_GGadget_TextImageSkip) + |
| 2385 |
2*GBoxBorderWidth(gt->g.base,&_GListMark_Box) + |
| 2386 |
GBoxBorderWidth(gt->g.base,&glistfieldmenu_box); |
| 2387 |
} else if ( gt->numericfield ) { |
| 2388 |
extra = GDrawPointsToPixels(gt->g.base,_GListMarkSize)/2 + |
| 2389 |
GDrawPointsToPixels(gt->g.base,_GGadget_TextImageSkip) + |
| 2390 |
2*GBoxBorderWidth(gt->g.base,&gnumericfieldspinner_box); |
| 2391 |
} |
| 2392 |
g->desired_width = inner->width + 2*bp + extra; |
| 2393 |
g->desired_height = inner->height + 2*bp; |
| 2394 |
if ( gt->multi_line ) { |
| 2395 |
int sbadd = GDrawPointsToPixels(gt->g.base,_GScrollBar_Width) + |
| 2396 |
GDrawPointsToPixels(gt->g.base,1); |
| 2397 |
g->desired_width += sbadd; |
| 2398 |
if ( !gt->wrap ) |
| 2399 |
g->desired_height += sbadd; |
| 2400 |
} |
| 2401 |
} |
| 2402 |
} |
| 2403 |
|
| 2404 |
static void GTextFieldGetDesiredSize(GGadget *g,GRect *outer,GRect *inner) { |
| 2405 |
GTextField *gt = (GTextField *) g; |
| 2406 |
int width=0, height; |
| 2407 |
int extra=0; |
| 2408 |
int bp = GBoxBorderWidth(g->base,g->box); |
| 2409 |
|
| 2410 |
if ( gt->listfield ) { |
| 2411 |
extra = GDrawPointsToPixels(gt->g.base,_GListMarkSize) + |
| 2412 |
GDrawPointsToPixels(gt->g.base,_GGadget_TextImageSkip) + |
| 2413 |
2*GBoxBorderWidth(gt->g.base,&_GListMark_Box) + |
| 2414 |
GBoxBorderWidth(gt->g.base,&glistfieldmenu_box); |
| 2415 |
} else if ( gt->numericfield ) { |
| 2416 |
extra = GDrawPointsToPixels(gt->g.base,_GListMarkSize)/2 + |
| 2417 |
GDrawPointsToPixels(gt->g.base,_GGadget_TextImageSkip) + |
| 2418 |
2*GBoxBorderWidth(gt->g.base,&gnumericfieldspinner_box); |
| 2419 |
} |
| 2420 |
|
| 2421 |
width = GGadgetScale(GDrawPointsToPixels(gt->g.base,80)); |
| 2422 |
height = gt->multi_line? 4*gt->fh:gt->fh; |
| 2423 |
|
| 2424 |
if ( g->desired_width>extra+2*bp ) width = g->desired_width - extra - 2*bp; |
| 2425 |
if ( g->desired_height>2*bp ) height = g->desired_height - 2*bp; |
| 2426 |
|
| 2427 |
if ( gt->multi_line ) { |
| 2428 |
int sbadd = GDrawPointsToPixels(gt->g.base,_GScrollBar_Width) + |
| 2429 |
GDrawPointsToPixels(gt->g.base,1); |
| 2430 |
width += sbadd; |
| 2431 |
if ( !gt->wrap ) |
| 2432 |
height += sbadd; |
| 2433 |
} |
| 2434 |
|
| 2435 |
if ( inner!=NULL ) { |
| 2436 |
inner->x = inner->y = 0; |
| 2437 |
inner->width = width; |
| 2438 |
inner->height = height; |
| 2439 |
} |
| 2440 |
if ( outer!=NULL ) { |
| 2441 |
outer->x = outer->y = 0; |
| 2442 |
outer->width = width + extra + 2*bp; |
| 2443 |
outer->height = height + 2*bp; |
| 2444 |
} |
| 2445 |
} |
| 2446 |
|
| 2447 |
static int gtextfield_FillsWindow(GGadget *g) { |
| 2448 |
return( ((GTextField *) g)->multi_line && g->prev==NULL && |
| 2449 |
(_GWidgetGetGadgets(g->base)==g || |
| 2450 |
_GWidgetGetGadgets(g->base)==(GGadget *) ((GTextField *) g)->vsb || |
| 2451 |
_GWidgetGetGadgets(g->base)==(GGadget *) ((GTextField *) g)->hsb )); |
| 2452 |
} |
| 2453 |
|
| 2454 |
struct gfuncs gtextfield_funcs = { |
| 2455 |
0, |
| 2456 |
sizeof(struct gfuncs), |
| 2457 |
|
| 2458 |
gtextfield_expose, |
| 2459 |
gtextfield_mouse, |
| 2460 |
gtextfield_key, |
| 2461 |
_gtextfield_editcmd, |
| 2462 |
gtextfield_focus, |
| 2463 |
gtextfield_timer, |
| 2464 |
gtextfield_sel, |
| 2465 |
|
| 2466 |
gtextfield_redraw, |
| 2467 |
gtextfield_move, |
| 2468 |
gtextfield_resize, |
| 2469 |
gtextfield_setvisible, |
| 2470 |
gtextfield_setenabled, |
| 2471 |
gtextfield_getsize, |
| 2472 |
_ggadget_getinnersize, |
| 2473 |
|
| 2474 |
gtextfield_destroy, |
| 2475 |
|
| 2476 |
GTextFieldSetTitle, |
| 2477 |
_GTextFieldGetTitle, |
| 2478 |
NULL, |
| 2479 |
NULL, |
| 2480 |
NULL, |
| 2481 |
GTextFieldSetFont, |
| 2482 |
GTextFieldGetFont, |
| 2483 |
|
| 2484 |
NULL, |
| 2485 |
NULL, |
| 2486 |
NULL, |
| 2487 |
NULL, |
| 2488 |
NULL, |
| 2489 |
NULL, |
| 2490 |
NULL, |
| 2491 |
NULL, |
| 2492 |
NULL, |
| 2493 |
NULL, |
| 2494 |
NULL, |
| 2495 |
|
| 2496 |
GTextFieldGetDesiredSize, |
| 2497 |
GTextFieldSetDesiredSize, |
| 2498 |
gtextfield_FillsWindow, |
| 2499 |
NULL |
| 2500 |
}; |
| 2501 |
|
| 2502 |
struct gfuncs glistfield_funcs = { |
| 2503 |
0, |
| 2504 |
sizeof(struct gfuncs), |
| 2505 |
|
| 2506 |
gtextfield_expose, |
| 2507 |
gtextfield_mouse, |
| 2508 |
gtextfield_key, |
| 2509 |
gtextfield_editcmd, |
| 2510 |
gtextfield_focus, |
| 2511 |
gtextfield_timer, |
| 2512 |
gtextfield_sel, |
| 2513 |
|
| 2514 |
gtextfield_redraw, |
| 2515 |
gtextfield_move, |
| 2516 |
gtextfield_resize, |
| 2517 |
gtextfield_setvisible, |
| 2518 |
gtextfield_setenabled, |
| 2519 |
gtextfield_getsize, |
| 2520 |
_ggadget_getinnersize, |
| 2521 |
|
| 2522 |
gtextfield_destroy, |
| 2523 |
|
| 2524 |
GTextFieldSetTitle, |
| 2525 |
_GTextFieldGetTitle, |
| 2526 |
NULL, |
| 2527 |
NULL, |
| 2528 |
NULL, |
| 2529 |
GTextFieldSetFont, |
| 2530 |
GTextFieldGetFont, |
| 2531 |
|
| 2532 |
GListFClear, |
| 2533 |
GListFSet, |
| 2534 |
GListFGet, |
| 2535 |
GListFGetItem, |
| 2536 |
NULL, |
| 2537 |
GListFSelectOne, |
| 2538 |
GListFIsSelected, |
| 2539 |
GListFGetFirst, |
| 2540 |
NULL, |
| 2541 |
NULL, |
| 2542 |
NULL, |
| 2543 |
|
| 2544 |
GTextFieldGetDesiredSize, |
| 2545 |
GTextFieldSetDesiredSize, |
| 2546 |
NULL, |
| 2547 |
NULL |
| 2548 |
}; |
| 2549 |
|
| 2550 |
void GTextFieldInit() { |
| 2551 |
static int gtextfield_inited = false; |
| 2552 |
if ( gtextfield_inited ) |
| 2553 |
return; |
| 2554 |
GResEditDoInit(>extfield_ri); |
| 2555 |
GResEditDoInit(&listfield_ri); |
| 2556 |
GResEditDoInit(&listfieldmenu_ri); |
| 2557 |
GResEditDoInit(&numericfield_ri); |
| 2558 |
GResEditDoInit(&numericfieldspinner_ri); |
| 2559 |
gtextfield_inited = true; |
| 2560 |
} |
| 2561 |
|
| 2562 |
static void GTextFieldAddVSb(GTextField *gt) { |
| 2563 |
GGadgetData gd; |
| 2564 |
|
| 2565 |
memset(&gd,'\0',sizeof(gd)); |
| 2566 |
gd.pos.y = gt->g.r.y; gd.pos.height = gt->g.r.height; |
| 2567 |
gd.pos.width = GDrawPointsToPixels(gt->g.base,_GScrollBar_Width); |
| 2568 |
gd.pos.x = gt->g.r.x+gt->g.r.width - gd.pos.width; |
| 2569 |
gd.flags = (gt->g.state==gs_invisible?0:gg_visible)|gg_enabled|gg_pos_in_pixels|gg_sb_vert; |
| 2570 |
gd.handle_controlevent = gtextfield_vscroll; |
| 2571 |
gt->vsb = (GScrollBar *) GScrollBarCreate(gt->g.base,&gd,gt); |
| 2572 |
gt->vsb->g.contained = true; |
| 2573 |
|
| 2574 |
gd.pos.width += GDrawPointsToPixels(gt->g.base,1); |
| 2575 |
gt->g.r.width -= gd.pos.width; |
| 2576 |
gt->g.inner.width -= gd.pos.width; |
| 2577 |
} |
| 2578 |
|
| 2579 |
static void GTextFieldAddHSb(GTextField *gt) { |
| 2580 |
GGadgetData gd; |
| 2581 |
|
| 2582 |
memset(&gd,'\0',sizeof(gd)); |
| 2583 |
gd.pos.x = gt->g.r.x; gd.pos.width = gt->g.r.width; |
| 2584 |
gd.pos.height = GDrawPointsToPixels(gt->g.base,_GScrollBar_Width); |
| 2585 |
gd.pos.y = gt->g.r.y+gt->g.r.height - gd.pos.height; |
| 2586 |
gd.flags = (gt->g.state==gs_invisible?0:gg_visible)|gg_enabled|gg_pos_in_pixels; |
| 2587 |
gd.handle_controlevent = gtextfield_hscroll; |
| 2588 |
gt->hsb = (GScrollBar *) GScrollBarCreate(gt->g.base,&gd,gt); |
| 2589 |
gt->hsb->g.contained = true; |
| 2590 |
|
| 2591 |
gd.pos.height += GDrawPointsToPixels(gt->g.base,1); |
| 2592 |
gt->g.r.height -= gd.pos.height; |
| 2593 |
gt->g.inner.height -= gd.pos.height; |
| 2594 |
if ( gt->vsb!=NULL ) { |
| 2595 |
gt->vsb->g.r.height -= gd.pos.height; |
| 2596 |
gt->vsb->g.inner.height -= gd.pos.height; |
| 2597 |
} |
| 2598 |
} |
| 2599 |
|
| 2600 |
static void GTextFieldFit(GTextField *gt) { |
| 2601 |
int as=0, ds, ld; |
| 2602 |
GRect inner, outer; |
| 2603 |
int bp = GBoxBorderWidth(gt->g.base,gt->g.box); |
| 2604 |
|
| 2605 |
{ |
| 2606 |
FontInstance *old = GDrawSetFont(gt->g.base,gt->font); |
| 2607 |
FontRequest rq; |
| 2608 |
int tries; |
| 2609 |
for ( tries = 0; tries<2; ++tries ) { |
| 2610 |
GDrawWindowFontMetrics(gt->g.base,gt->font,&as, &ds, &ld); |
| 2611 |
if ( gt->g.r.height==0 || as+ds-3+2*bp<=gt->g.r.height || tries==1 ) |
| 2612 |
break; |
| 2613 |
|
| 2614 |
GDrawDecomposeFont(gt->font,&rq); |
| 2615 |
--rq.point_size; |
| 2616 |
gt->font = GDrawInstanciateFont(gt->g.base,&rq); |
| 2617 |
} |
| 2618 |
gt->fh = as+ds; |
| 2619 |
gt->as = as; |
| 2620 |
gt->nw = GDrawGetTextWidth(gt->g.base,nstr, 1); |
| 2621 |
GDrawSetFont(gt->g.base,old); |
| 2622 |
} |
| 2623 |
|
| 2624 |
GTextFieldGetDesiredSize(>->g,&outer,&inner); |
| 2625 |
if ( gt->g.r.width==0 ) { |
| 2626 |
int extra=0; |
| 2627 |
if ( gt->listfield ) { |
| 2628 |
extra = GDrawPointsToPixels(gt->g.base,_GListMarkSize) + |
| 2629 |
2*GDrawPointsToPixels(gt->g.base,_GGadget_TextImageSkip) + |
| 2630 |
GBoxBorderWidth(gt->g.base,&_GListMark_Box); |
| 2631 |
} else if ( gt->numericfield ) { |
| 2632 |
extra = GDrawPointsToPixels(gt->g.base,_GListMarkSize)/2 + |
| 2633 |
GDrawPointsToPixels(gt->g.base,_GGadget_TextImageSkip) + |
| 2634 |
2*GBoxBorderWidth(gt->g.base,&gnumericfieldspinner_box); |
| 2635 |
} |
| 2636 |
gt->g.r.width = outer.width; |
| 2637 |
gt->g.inner.width = inner.width; |
| 2638 |
gt->g.inner.x = gt->g.r.x + (outer.width-inner.width-extra)/2; |
| 2639 |
} else { |
| 2640 |
gt->g.inner.x = gt->g.r.x + bp; |
| 2641 |
gt->g.inner.width = gt->g.r.width - 2*bp; |
| 2642 |
} |
| 2643 |
if ( gt->g.r.height==0 ) { |
| 2644 |
gt->g.r.height = outer.height; |
| 2645 |
gt->g.inner.height = inner.height; |
| 2646 |
gt->g.inner.y = gt->g.r.y + (outer.height-gt->g.inner.height)/2; |
| 2647 |
} else { |
| 2648 |
gt->g.inner.y = gt->g.r.y + bp; |
| 2649 |
gt->g.inner.height = gt->g.r.height - 2*bp; |
| 2650 |
} |
| 2651 |
|
| 2652 |
if ( gt->multi_line ) { |
| 2653 |
GTextFieldAddVSb(gt); |
| 2654 |
if ( !gt->wrap ) |
| 2655 |
GTextFieldAddHSb(gt); |
| 2656 |
} |
| 2657 |
if ( gt->listfield || gt->numericfield ) { |
| 2658 |
GListField *ge = (GListField *) gt; |
| 2659 |
int extra; |
| 2660 |
if ( gt->listfield ) |
| 2661 |
extra = GDrawPointsToPixels(gt->g.base,_GListMarkSize) + |
| 2662 |
GDrawPointsToPixels(gt->g.base,_GGadget_TextImageSkip) + |
| 2663 |
2*GBoxBorderWidth(gt->g.base,&_GListMark_Box)+ |
| 2664 |
GBoxBorderWidth(gt->g.base,&glistfieldmenu_box); |
| 2665 |
else { |
| 2666 |
extra = GDrawPointsToPixels(gt->g.base,_GListMarkSize)/2 + |
| 2667 |
GDrawPointsToPixels(gt->g.base,_GGadget_TextImageSkip) + |
| 2668 |
2*GBoxBorderWidth(gt->g.base,&gnumericfieldspinner_box); |
| 2669 |
} |
| 2670 |
ge->fieldrect = ge->buttonrect = gt->g.r; |
| 2671 |
ge->fieldrect.width -= extra; |
| 2672 |
extra -= GDrawPointsToPixels(gt->g.base,_GGadget_TextImageSkip)/2; |
| 2673 |
ge->buttonrect.x = ge->buttonrect.x+ge->buttonrect.width-extra; |
| 2674 |
ge->buttonrect.width = extra; |
| 2675 |
if ( gt->numericfield ) |
| 2676 |
++ge->fieldrect.width; |
| 2677 |
} |
| 2678 |
} |
| 2679 |
|
| 2680 |
static GTextField *_GTextFieldCreate(GTextField *gt, struct gwindow *base, GGadgetData *gd,void *data, GBox *def) { |
| 2681 |
|
| 2682 |
GTextFieldInit(); |
| 2683 |
gt->g.funcs = >extfield_funcs; |
| 2684 |
_GGadget_Create(>->g,base,gd,data,def); |
| 2685 |
|
| 2686 |
gt->g.takes_input = true; gt->g.takes_keyboard = true; gt->g.focusable = true; |
| 2687 |
if ( gd->label!=NULL && gd->label->text!=NULL ) { |
| 2688 |
if ( gd->label->text_is_1byte ) |
| 2689 |
gt->text = utf82u_copy((char *) gd->label->text); |
| 2690 |
else |
| 2691 |
gt->text = u_copy(gd->label->text); |
| 2692 |
gt->sel_start = gt->sel_end = gt->sel_base = u_strlen(gt->text); |
| 2693 |
} |
| 2694 |
if ( gt->text==NULL ) |
| 2695 |
gt->text = calloc(1,sizeof(unichar_t)); |
| 2696 |
gt->font = _gtextfield_font.fi; |
| 2697 |
if ( gd->label!=NULL && gd->label->font!=NULL ) |
| 2698 |
gt->font = gd->label->font; |
| 2699 |
if ( (gd->flags & gg_textarea_wrap) && gt->multi_line ) |
| 2700 |
gt->wrap = true; |
| 2701 |
GTextFieldFit(gt); |
| 2702 |
_GGadget_FinalPosition(>->g,base,gd); |
| 2703 |
GTextFieldRefigureLines(gt,0); |
| 2704 |
|
| 2705 |
if ( gd->flags & gg_group_end ) |
| 2706 |
_GGadgetCloseGroup(>->g); |
| 2707 |
GWidgetIndicateFocusGadget(>->g); |
| 2708 |
if ( gd->flags & gg_text_xim ) |
| 2709 |
gt->gic = GWidgetCreateInputContext(base,gic_overspot|gic_orlesser); |
| 2710 |
return( gt ); |
| 2711 |
} |
| 2712 |
|
| 2713 |
GGadget *GTextFieldCreate(struct gwindow *base, GGadgetData *gd,void *data) { |
| 2714 |
GTextField *gt = _GTextFieldCreate(calloc(1,sizeof(GTextField)),base,gd,data,&_GGadget_gtextfield_box); |
| 2715 |
|
| 2716 |
return( >->g ); |
| 2717 |
} |
| 2718 |
|
| 2719 |
GGadget *GPasswordCreate(struct gwindow *base, GGadgetData *gd,void *data) { |
| 2720 |
GTextField *gt = _GTextFieldCreate(calloc(1,sizeof(GTextField)),base,gd,data,&_GGadget_gtextfield_box); |
| 2721 |
gt->password = true; |
| 2722 |
GTextFieldRefigureLines(gt, 0); |
| 2723 |
|
| 2724 |
return( >->g ); |
| 2725 |
} |
| 2726 |
|
| 2727 |
GGadget *GNumericFieldCreate(struct gwindow *base, GGadgetData *gd,void *data) { |
| 2728 |
GTextField *gt = calloc(1,sizeof(GNumericField)); |
| 2729 |
gt->numericfield = true; |
| 2730 |
_GTextFieldCreate(gt,base,gd,data,&gnumericfield_box); |
| 2731 |
|
| 2732 |
return( >->g ); |
| 2733 |
} |
| 2734 |
|
| 2735 |
GGadget *GTextCompletionCreate(struct gwindow *base, GGadgetData *gd,void *data) { |
| 2736 |
GTextField *gt = calloc(1,sizeof(GCompletionField)); |
| 2737 |
gt->accepts_tabs = true; |
| 2738 |
gt->completionfield = true; |
| 2739 |
gt->was_completing = true; |
| 2740 |
((GCompletionField *) gt)->completion = gd->u.completion; |
| 2741 |
_GTextFieldCreate(gt,base,gd,data,&_GGadget_gtextfield_box); |
| 2742 |
gt->accepts_tabs = ((GCompletionField *) gt)->completion != NULL; |
| 2743 |
|
| 2744 |
return( >->g ); |
| 2745 |
} |
| 2746 |
|
| 2747 |
GGadget *GTextAreaCreate(struct gwindow *base, GGadgetData *gd,void *data) { |
| 2748 |
GTextField *gt = calloc(1,sizeof(GTextField)); |
| 2749 |
gt->multi_line = true; |
| 2750 |
gt->accepts_returns = true; |
| 2751 |
_GTextFieldCreate(gt,base,gd,data,&_GGadget_gtextfield_box); |
| 2752 |
|
| 2753 |
return( >->g ); |
| 2754 |
} |
| 2755 |
|
| 2756 |
static void GListFieldSelected(GGadget *g, int i) { |
| 2757 |
GListField *ge = (GListField *) g; |
| 2758 |
|
| 2759 |
ge->popup = NULL; |
| 2760 |
_GWidget_ClearGrabGadget(&ge->gt.g); |
| 2761 |
if ( i<0 || i>=ge->ltot || ge->ti[i]->text==NULL ) |
| 2762 |
return; |
| 2763 |
GTextFieldSetTitle(g,ge->ti[i]->text); |
| 2764 |
_ggadget_redraw(g); |
| 2765 |
|
| 2766 |
GTextFieldChanged(&ge->gt,i); |
| 2767 |
} |
| 2768 |
|
| 2769 |
GGadget *GSimpleListFieldCreate(struct gwindow *base, GGadgetData *gd,void *data) { |
| 2770 |
GListField *ge = calloc(1,sizeof(GListField)); |
| 2771 |
|
| 2772 |
ge->gt.listfield = true; |
| 2773 |
if ( gd->u.list!=NULL ) |
| 2774 |
ge->ti = GTextInfoArrayFromList(gd->u.list,&ge->ltot); |
| 2775 |
_GTextFieldCreate(&ge->gt,base,gd,data,&glistfield_box); |
| 2776 |
ge->gt.g.funcs = &glistfield_funcs; |
| 2777 |
return( &ge->gt.g ); |
| 2778 |
} |
| 2779 |
|
| 2780 |
static unichar_t **GListField_NameCompletion(GGadget *t,int from_tab) { |
| 2781 |
const unichar_t *spt; unichar_t **ret; |
| 2782 |
GTextInfo **ti; |
| 2783 |
int32_t len; |
| 2784 |
int i, cnt, doit, match_len; |
| 2785 |
|
| 2786 |
spt = _GGadgetGetTitle(t); |
| 2787 |
if ( spt==NULL ) |
| 2788 |
return( NULL ); |
| 2789 |
|
| 2790 |
match_len = u_strlen(spt); |
| 2791 |
ti = GGadgetGetList(t,&len); |
| 2792 |
ret = NULL; |
| 2793 |
for ( doit=0; doit<2; ++doit ) { |
| 2794 |
cnt=0; |
| 2795 |
for ( i=0; i<len; ++i ) { |
| 2796 |
if ( ti[i]->text && u_strncmp(ti[i]->text,spt,match_len)==0 ) { |
| 2797 |
if ( doit ) |
| 2798 |
ret[cnt] = u_copy(ti[i]->text); |
| 2799 |
++cnt; |
| 2800 |
} |
| 2801 |
} |
| 2802 |
if ( doit ) |
| 2803 |
ret[cnt] = NULL; |
| 2804 |
else if ( cnt==0 ) |
| 2805 |
return( NULL ); |
| 2806 |
else |
| 2807 |
ret = malloc((cnt+1)*sizeof(unichar_t *)); |
| 2808 |
} |
| 2809 |
return( ret ); |
| 2810 |
} |
| 2811 |
|
| 2812 |
GGadget *GListFieldCreate(struct gwindow *base, GGadgetData *gd,void *data) { |
| 2813 |
GListField *ge = calloc(1,sizeof(GCompletionField)); |
| 2814 |
|
| 2815 |
ge->gt.listfield = true; |
| 2816 |
if ( gd->u.list!=NULL ) |
| 2817 |
ge->ti = GTextInfoArrayFromList(gd->u.list,&ge->ltot); |
| 2818 |
ge->gt.accepts_tabs = true; |
| 2819 |
ge->gt.completionfield = true; |
| 2820 |
|
| 2821 |
((GCompletionField *) ge)->completion = GListField_NameCompletion; |
| 2822 |
_GTextFieldCreate(&ge->gt,base,gd,data,&_GGadget_gtextfield_box); |
| 2823 |
ge->gt.g.funcs = &glistfield_funcs; |
| 2824 |
return( &ge->gt.g ); |
| 2825 |
} |
| 2826 |
|
| 2827 |
|
| 2828 |
|
| 2829 |
|
| 2830 |
|
| 2831 |
static void GCompletionDestroy(GCompletionField *gc) { |
| 2832 |
int i; |
| 2833 |
|
| 2834 |
if ( gc->choice_popup!=NULL ) { |
| 2835 |
GWindow cp = gc->choice_popup; |
| 2836 |
gc->choice_popup = NULL; |
| 2837 |
GDrawSetUserData(cp,NULL); |
| 2838 |
GDrawDestroyWindow(cp); |
| 2839 |
} |
| 2840 |
if ( gc->choices!=NULL ) { |
| 2841 |
for ( i=0; gc->choices[i]!=NULL; ++i ) |
| 2842 |
free(gc->choices[i]); |
| 2843 |
free(gc->choices); |
| 2844 |
gc->choices = NULL; |
| 2845 |
} |
| 2846 |
} |
| 2847 |
|
| 2848 |
static int GTextFieldSetTitleRmDotDotDot(GGadget *g,unichar_t *tit) { |
| 2849 |
unichar_t *pt = uc_strstr(tit," ..."); |
| 2850 |
if ( pt!=NULL ) |
| 2851 |
*pt = '\0'; |
| 2852 |
GTextFieldSetTitle(g,tit); |
| 2853 |
if ( pt!=NULL ) |
| 2854 |
*pt = ' '; |
| 2855 |
return( pt!=NULL ); |
| 2856 |
} |
| 2857 |
|
| 2858 |
static int popup_eh(GWindow popup,GEvent *event) { |
| 2859 |
GGadget *owner = GDrawGetUserData(popup); |
| 2860 |
GTextField *gt = (GTextField *) owner; |
| 2861 |
GCompletionField *gc = (GCompletionField *) owner; |
| 2862 |
GRect old1, r; |
| 2863 |
Color fg; |
| 2864 |
int i, bp; |
| 2865 |
|
| 2866 |
if ( owner==NULL ) |
| 2867 |
return( true ); |
| 2868 |
|
| 2869 |
bp = GBoxBorderWidth(owner->base,owner->box); |
| 2870 |
if ( event->type == et_expose ) { |
| 2871 |
GDrawPushClip(popup,&event->u.expose.rect,&old1); |
| 2872 |
GDrawSetFont(popup,gt->font); |
| 2873 |
GBoxDrawBackground(popup,&event->u.expose.rect,owner->box, |
| 2874 |
owner->state,false); |
| 2875 |
GDrawGetSize(popup,&r); |
| 2876 |
r.x = r.y = 0; |
| 2877 |
GBoxDrawBorder(popup,&r,owner->box,owner->state,false); |
| 2878 |
r.x += bp; r.width -= 2*bp; |
| 2879 |
fg = owner->box->main_foreground==COLOR_DEFAULT?GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(popup)): |
| 2880 |
owner->box->main_foreground; |
| 2881 |
for ( i=0; gc->choices[i]!=NULL; ++i ) { |
| 2882 |
if ( i==gc->selected ) { |
| 2883 |
r.y = i*gt->fh+bp; |
| 2884 |
r.height = gt->fh; |
| 2885 |
GDrawFillRect(popup,&r,owner->box->active_border); |
| 2886 |
} |
| 2887 |
GDrawDrawText(popup,bp,i*gt->fh+gt->as+bp,gc->choices[i],-1,fg); |
| 2888 |
} |
| 2889 |
GDrawPopClip(popup,&old1); |
| 2890 |
} else if ( event->type == et_mouseup ) { |
| 2891 |
gc->selected = (event->u.mouse.y-bp)/gt->fh; |
| 2892 |
if ( gc->selected>=0 && gc->selected<gc->ctot ) { |
| 2893 |
int tryagain = GTextFieldSetTitleRmDotDotDot(owner,gc->choices[gc->selected]); |
| 2894 |
GTextFieldChanged(gt,-1); |
| 2895 |
GCompletionDestroy(gc); |
| 2896 |
if ( tryagain ) |
| 2897 |
GTextFieldComplete(gt,false); |
| 2898 |
} else { |
| 2899 |
gc->selected = -1; |
| 2900 |
GDrawRequestExpose(popup,NULL,false); |
| 2901 |
} |
| 2902 |
} else if ( event->type == et_char ) { |
| 2903 |
return( gtextfield_key(owner,event)); |
| 2904 |
} |
| 2905 |
return( true ); |
| 2906 |
} |
| 2907 |
|
| 2908 |
static void GCompletionCreatePopup(GCompletionField *gc) { |
| 2909 |
int width, maxw, i; |
| 2910 |
GWindowAttrs pattrs; |
| 2911 |
GWindow base = gc->gl.gt.g.base; |
| 2912 |
GDisplay *disp = GDrawGetDisplayOfWindow(base); |
| 2913 |
GWindow root = GDrawGetRoot(disp); |
| 2914 |
int bp = GBoxBorderWidth(base,gc->gl.gt.g.box); |
| 2915 |
GRect pos, screen; |
| 2916 |
GPoint pt; |
| 2917 |
|
| 2918 |
GDrawSetFont(base,gc->gl.gt.font); |
| 2919 |
|
| 2920 |
maxw = 0; |
| 2921 |
for ( i=0; i<gc->ctot; ++i ) { |
| 2922 |
width = GDrawGetTextWidth(base,gc->choices[i],-1); |
| 2923 |
if ( width > maxw ) maxw = width; |
| 2924 |
} |
| 2925 |
maxw += 2*bp; |
| 2926 |
pos.width = maxw; pos.height = gc->gl.gt.fh*gc->ctot+2*bp; |
| 2927 |
if ( pos.width < gc->gl.gt.g.r.width ) |
| 2928 |
pos.width = gc->gl.gt.g.r.width; |
| 2929 |
|
| 2930 |
pattrs.mask = wam_events|wam_nodecor|wam_positioned|wam_cursor| |
| 2931 |
wam_transient|wam_verytransient; |
| 2932 |
pattrs.event_masks = -1; |
| 2933 |
pattrs.nodecoration = true; |
| 2934 |
pattrs.positioned = true; |
| 2935 |
pattrs.cursor = ct_pointer; |
| 2936 |
pattrs.transient = GWidgetGetTopWidget(base); |
| 2937 |
pattrs.border_width = 1; |
| 2938 |
pattrs.border_color = gc->gl.gt.g.box->main_foreground; |
| 2939 |
|
| 2940 |
GDrawGetSize(root,&screen); |
| 2941 |
pt.x = gc->gl.gt.g.r.x; |
| 2942 |
pt.y = gc->gl.gt.g.r.y + gc->gl.gt.g.r.height; |
| 2943 |
GDrawTranslateCoordinates(base,root,&pt); |
| 2944 |
if ( pt.y+pos.height > screen.height ) { |
| 2945 |
if ( pt.y-gc->gl.gt.g.r.height-pos.height>=0 ) { |
| 2946 |
|
| 2947 |
pt.y -= gc->gl.gt.g.r.height; |
| 2948 |
pt.y -= pos.height; |
| 2949 |
} else if ( pt.x + gc->gl.gt.g.r.width + maxw <= screen.width ) { |
| 2950 |
pt.x += gc->gl.gt.g.r.width; |
| 2951 |
pt.y = 0; |
| 2952 |
} else |
| 2953 |
pt.x = pt.y = 0; |
| 2954 |
} |
| 2955 |
pos.x = pt.x; pos.y = pt.y; |
| 2956 |
|
| 2957 |
gc->choice_popup = GWidgetCreateTopWindow(disp,&pos,popup_eh,gc,&pattrs); |
| 2958 |
GDrawSetGIC(gc->choice_popup,GWidgetCreateInputContext(gc->choice_popup,gic_overspot|gic_orlesser), |
| 2959 |
gc->gl.gt.g.inner.x,gc->gl.gt.g.inner.y+gc->gl.gt.as); |
| 2960 |
GDrawSetVisible(gc->choice_popup,true); |
| 2961 |
|
| 2962 |
} |
| 2963 |
|
| 2964 |
static int ucmp(const void *_s1, const void *_s2) { |
| 2965 |
return( u_strcmp(*(const unichar_t **)_s1,*(const unichar_t **)_s2)); |
| 2966 |
} |
| 2967 |
|
| 2968 |
#define MAXLINES 30 |
| 2969 |
#define MAXBRACKETS 30 |
| 2970 |
|
| 2971 |
static void GTextFieldComplete(GTextField *gt,int from_tab) { |
| 2972 |
GCompletionField *gc = (GCompletionField *) gt; |
| 2973 |
unichar_t **ret; |
| 2974 |
int i, len, orig_len; |
| 2975 |
unichar_t *pt1, *pt2, ch; |
| 2976 |
|
| 2977 |
|
| 2978 |
|
| 2979 |
ret = (gc->completion)(>->g,from_tab); |
| 2980 |
if ( ret==NULL || ret[0]==NULL ) { |
| 2981 |
if ( from_tab ) |
| 2982 |
GDrawBeep(NULL); |
| 2983 |
else |
| 2984 |
GTextFieldChanged(gt,-1); |
| 2985 |
free(ret); |
| 2986 |
} else { |
| 2987 |
orig_len = u_strlen(gt->text); |
| 2988 |
len = u_strlen(ret[0]); |
| 2989 |
for ( i=1; ret[i]!=NULL; ++i ) { |
| 2990 |
for ( pt1=ret[0], pt2=ret[i]; *pt1==*pt2 && pt1-ret[0]<len ; ++pt1, ++pt2 ); |
| 2991 |
len = pt1-ret[0]; |
| 2992 |
} |
| 2993 |
if ( orig_len!=len ) { |
| 2994 |
ch = ret[0][len]; ret[0][len] = '\0'; |
| 2995 |
GTextFieldSetTitle(>->g,ret[0]); |
| 2996 |
ret[0][len] = ch; |
| 2997 |
if ( !from_tab ) |
| 2998 |
GTextFieldSelect(>->g,orig_len,len); |
| 2999 |
GTextFieldChanged(gt,-1); |
| 3000 |
} else if ( !from_tab ) |
| 3001 |
GTextFieldChanged(gt,-1); |
| 3002 |
if ( ret[1]!=NULL ) { |
| 3003 |
gc->choices = ret; |
| 3004 |
gc->selected = -1; |
| 3005 |
if ( from_tab ) GDrawBeep(NULL); |
| 3006 |
qsort(ret,i,sizeof(unichar_t *),ucmp); |
| 3007 |
gc->ctot = i; |
| 3008 |
if ( i>=MAXLINES ) { |
| 3009 |
|
| 3010 |
|
| 3011 |
|
| 3012 |
|
| 3013 |
unichar_t **ret2=NULL, last_ch = -1; |
| 3014 |
int cnt, doit, type2=false; |
| 3015 |
for ( doit=0; doit<2; ++doit ) { |
| 3016 |
for ( i=cnt=0; ret[i]!=NULL; ++i ) { |
| 3017 |
if ( last_ch!=ret[i][len] ) { |
| 3018 |
if ( doit && type2 ) { |
| 3019 |
int c2 = cnt/MAXBRACKETS, c3 = cnt%MAXBRACKETS; |
| 3020 |
if ( ret[i][len]=='\0' ) |
| 3021 |
continue; |
| 3022 |
if ( c3==0 ) { |
| 3023 |
ret2[c2] = calloc((len+MAXBRACKETS+2+4+1),sizeof(unichar_t)); |
| 3024 |
memcpy(ret2[c2],ret[i],len*sizeof(unichar_t)); |
| 3025 |
ret2[c2][len] = '['; |
| 3026 |
} |
| 3027 |
ret2[c2][len+1+c3] = ret[i][len]; |
| 3028 |
uc_strcpy(ret2[c2]+len+2+c3,"] ..."); |
| 3029 |
} else if ( doit ) { |
| 3030 |
ret2[cnt] = malloc((u_strlen(ret[i])+5)*sizeof(unichar_t)); |
| 3031 |
u_strcpy(ret2[cnt],ret[i]); |
| 3032 |
} |
| 3033 |
++cnt; |
| 3034 |
last_ch = ret[i][len]; |
| 3035 |
} else if ( doit && !type2 ) { |
| 3036 |
int j; |
| 3037 |
for ( j=len+1; ret[i][j]!='\0' && ret[i][j] == ret2[cnt-1][j]; ++j ); |
| 3038 |
uc_strcpy(ret2[cnt-1]+j," ..."); |
| 3039 |
} |
| 3040 |
} |
| 3041 |
if ( cnt>=MAXLINES*MAXBRACKETS ) |
| 3042 |
break; |
| 3043 |
if ( cnt>=MAXLINES && !doit ) { |
| 3044 |
type2 = (cnt+MAXBRACKETS-1)/MAXBRACKETS; |
| 3045 |
ret2 = malloc((type2+1)*sizeof(unichar_t *)); |
| 3046 |
} else if ( !doit ) |
| 3047 |
ret2 = malloc((cnt+1)*sizeof(unichar_t *)); |
| 3048 |
else { |
| 3049 |
if ( type2 ) { |
| 3050 |
cnt = (cnt+MAXBRACKETS-1)/MAXBRACKETS; |
| 3051 |
assert(cnt <= type2); |
| 3052 |
} |
| 3053 |
ret2[cnt] = NULL; |
| 3054 |
} |
| 3055 |
} |
| 3056 |
if ( ret2!=NULL ) { |
| 3057 |
for ( i=0; ret[i]!=NULL; ++i ) |
| 3058 |
free(ret[i]); |
| 3059 |
free(ret); |
| 3060 |
ret = gc->choices = ret2; |
| 3061 |
i = gc->ctot = cnt; |
| 3062 |
} |
| 3063 |
} |
| 3064 |
if ( gc->ctot>=MAXLINES ) { |
| 3065 |
|
| 3066 |
gc->choices = NULL; |
| 3067 |
for ( i=0; ret[i]!=NULL; ++i ) |
| 3068 |
free(ret[i]); |
| 3069 |
free(ret); |
| 3070 |
} else { |
| 3071 |
gc->ctot = i; |
| 3072 |
GCompletionCreatePopup(gc); |
| 3073 |
} |
| 3074 |
} else { |
| 3075 |
free(ret[1]); |
| 3076 |
free(ret); |
| 3077 |
} |
| 3078 |
} |
| 3079 |
} |
| 3080 |
|
| 3081 |
static int GCompletionHandleKey(GTextField *gt,GEvent *event) { |
| 3082 |
GCompletionField *gc = (GCompletionField *) gt; |
| 3083 |
int dir = 0; |
| 3084 |
|
| 3085 |
if ( gc->choice_popup==NULL || event->type == et_charup ) |
| 3086 |
return( false ); |
| 3087 |
|
| 3088 |
if ( event->u.chr.keysym == GK_Up || event->u.chr.keysym == GK_KP_Up ) |
| 3089 |
dir = -1; |
| 3090 |
else if ( event->u.chr.keysym == GK_Down || event->u.chr.keysym == GK_KP_Down ) |
| 3091 |
dir = 1; |
| 3092 |
|
| 3093 |
if ( dir==0 || event->u.chr.chars[0]!='\0' ) { |
| 3094 |
|
| 3095 |
|
| 3096 |
GCompletionDestroy(gc); |
| 3097 |
if ( event->u.chr.keysym == GK_Escape ) |
| 3098 |
gt->was_completing = false; |
| 3099 |
return( event->u.chr.keysym == GK_Escape || |
| 3100 |
event->u.chr.keysym == GK_Return ); |
| 3101 |
} |
| 3102 |
|
| 3103 |
if (( gc->selected==-1 && dir==-1 ) || ( gc->selected==gc->ctot-1 && dir==1 )) |
| 3104 |
return( true ); |
| 3105 |
gc->selected += dir; |
| 3106 |
if ( gc->selected!=-1 ) |
| 3107 |
GTextFieldSetTitleRmDotDotDot(>->g,gc->choices[gc->selected]); |
| 3108 |
GTextFieldChanged(gt,-1); |
| 3109 |
GDrawRequestExpose(gc->choice_popup,NULL,false); |
| 3110 |
return( true ); |
| 3111 |
} |
| 3112 |
|
| 3113 |
void GCompletionFieldSetCompletion(GGadget *g,GTextCompletionHandler completion) { |
| 3114 |
((GCompletionField *) g)->completion = completion; |
| 3115 |
((GTextField *) g)->accepts_tabs = ((GCompletionField *) g)->completion != NULL; |
| 3116 |
} |
| 3117 |
|
| 3118 |
void GCompletionFieldSetCompletionMode(GGadget *g,int enabled) { |
| 3119 |
((GTextField *) g)->was_completing = enabled; |
| 3120 |
} |