Debian Codegraph @

gdraw/gtextinfo.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "gdraw.h"
31 #include "gfile.h"
32 #include "ggadgetP.h"
33 #include "gkeysym.h"
34 #include "gresourceP.h"
35 #include "hotkeys.h"
36 #include "ustring.h"
37 #include "utype.h"
38
39 /////////////////////////////////////////////////////////////////
40 // The below keys are from this file, when/if we move to GTK+
41 // then perhaps we should include this file instead of the inline
42 // definitions
43 //#include <gdk/gdkkeysyms.h>
44 #define GDK_KEY_Tab 0xff09
45 #define GDK_KEY_ISO_Left_Tab 0xfe20
46 /////////////////////////////////////////////////////////////////
47
48 int GTextInfoGetWidth(GWindow base,GTextInfo *ti,FontInstance *font) {
49 int width=0;
50 int iwidth=0;
51 int skip = 0;
52
53 if ( ti->text!=NULL ) {
54 if ( ti->font!=NULL )
55 font = ti->font;
56
57 if ( font!=NULL )
58 GDrawSetFont(base,font);
59 width = GDrawGetTextWidth(base,ti->text, -1);
60 }
61 if ( ti->image!=NULL ) {
62 iwidth = GImageGetScaledWidth(base,ti->image);
63 if ( ti->text!=NULL )
64 skip = GDrawPointsToPixels(base,6);
65 }
66 return( width+iwidth+skip );
67 }
68
69 int GTextInfoGetMaxWidth(GWindow base,GTextInfo **ti,FontInstance *font) {
70 int width=0, temp;
71 int i;
72
73 for ( i=0; ti[i]->text!=NULL || ti[i]->image!=NULL; ++i ) {
74 if (( temp= GTextInfoGetWidth(base,ti[i],font))>width )
75 width = temp;
76 }
77 return( width );
78 }
79
80 int GTextInfoGetHeight(GWindow base,GTextInfo *ti,FontInstance *font) {
81 int fh=0, as=0, ds=0, ld;
82 int iheight=0;
83 int height;
84 GTextBounds bounds;
85
86 if ( ti->font!=NULL )
87 font = ti->font;
88 GDrawWindowFontMetrics(base,font,&as, &ds, &ld);
89 if ( ti->text!=NULL ) {
90 GDrawSetFont(base,font);
91 GDrawGetTextBounds(base,ti->text, -1, &bounds);
92 if ( as<bounds.as ) as = bounds.as;
93 if ( ds<bounds.ds ) ds = bounds.ds;
94 }
95 fh = as+ds;
96 if ( ti->image!=NULL ) {
97 iheight = GImageGetScaledHeight(base,ti->image);
98 iheight += 1;
99 }
100 if ( (height = fh)<iheight ) height = iheight;
101 return( height );
102 }
103
104 int GTextInfoGetMaxHeight(GWindow base,GTextInfo **ti,FontInstance *font,int *allsame) {
105 int height=0, temp, same=1;
106 int i;
107
108 for ( i=0; ti[i]->text!=NULL || ti[i]->image!=NULL; ++i ) {
109 temp= GTextInfoGetHeight(base,ti[i],font);
110 if ( height!=0 && height!=temp )
111 same = 0;
112 if ( height<temp )
113 height = temp;
114 }
115 *allsame = same;
116 return( height );
117 }
118
119 int GTextInfoGetAs(GWindow base,GTextInfo *ti, FontInstance *font) {
120 int fh=0, as=0, ds=0, ld;
121 int iheight=0;
122 int height;
123 GTextBounds bounds;
124
125 GDrawWindowFontMetrics(base,font,&as, &ds, &ld);
126 if ( ti->text!=NULL ) {
127 GDrawSetFont(base,font);
128 GDrawGetTextBounds(base,ti->text, -1, &bounds);
129 if ( as<bounds.as ) as = bounds.as;
130 if ( ds<bounds.ds ) ds = bounds.ds;
131 }
132 fh = as+ds;
133 if ( ti->image!=NULL ) {
134 iheight = GImageGetScaledHeight(base,ti->image);
135 }
136 if ( (height = fh)<iheight ) height = iheight;
137
138 if ( ti->text!=NULL )
139 return( as+(height>fh?(height-fh)/2:0) );
140
141 return( iheight );
142 }
143
144 int GTextInfoDraw(GWindow base,int x,int y,GTextInfo *ti,
145 FontInstance *font,Color fg, Color sel, int ymax, int as, int ds) {
146 int fh=0, tas=0, tds=0, ld;
147 int iwidth=0, iheight=0;
148 int height, skip = 0;
149 GTextBounds bounds;
150 GRect r, old;
151
152 GDrawWindowFontMetrics(base, font, &tas, &tds, &ld);
153 if ( ti->text!=NULL ) {
154 if ( ti->font!=NULL )
155 font = ti->font;
156 if ( ti->fg!=COLOR_DEFAULT && ti->fg!=COLOR_UNKNOWN && ti->fg!=COLOR_WARNING )
157 fg = ti->fg;
158
159 GDrawSetFont(base,font);
160 if ( as<0 || ds<0 ) {
161 GDrawGetTextBounds(base,ti->text, -1, &bounds);
162 if ( as<0 )
163 as = (tas<bounds.as) ? bounds.as : tas;
164 if ( ds<0 )
165 ds = (tds<bounds.ds) ? bounds.ds : tds;
166 }
167
168 } else {
169 as = tas;
170 ds = tds;
171 }
172 fh = as+ds;
173 if ( fg == COLOR_DEFAULT )
174 fg = GDrawGetDefaultForeground(GDrawGetDisplayOfWindow(base));
175 else if ( fg == COLOR_WARNING )
176 fg = GDrawGetWarningForeground(GDrawGetDisplayOfWindow(base));
177 if ( ti->image!=NULL ) {
178 iwidth = GImageGetScaledWidth(base,ti->image);
179 iheight = GImageGetScaledHeight(base,ti->image)+1;
180 if ( ti->text!=NULL )
181 skip = GDrawPointsToPixels(base,6);
182 }
183 if ( (height = fh)<iheight ) height = iheight;
184
185 r.y = y; r.height = height;
186 r.x = 0; r.width = 10000;
187
188 if ( ti->line ) {
189 _GGroup_Init();
190 GDrawGetClip(base,&r);
191 r.x += GDrawPointsToPixels(base,2); r.width -= 2*GDrawPointsToPixels(base,2);
192 GDrawPushClip(base,&r,&old);
193 r.y = y; r.height = height;
194 r.x = x; r.width = 10000;
195 GBoxDrawHLine(base,&r,&_GGroup_LineBox);
196 GDrawPopClip(base,&old);
197 } else {
198 if (( ti->selected && sel!=COLOR_DEFAULT ) || ( ti->bg!=COLOR_DEFAULT && ti->bg!=COLOR_UNKNOWN )) {
199 Color bg = ti->bg;
200 if ( ti->selected ) {
201 if ( sel==COLOR_DEFAULT )
202 sel = fg;
203 bg = sel;
204 if ( sel==fg ) {
205 fg = ti->bg;
206 if ( fg==COLOR_DEFAULT || fg==COLOR_UNKNOWN )
207 fg = GDrawGetDefaultBackground(GDrawGetDisplayOfWindow(base));
208 }
209 }
210 GDrawFillRect(base,&r,bg);
211 }
212
213 if ( ti->image!=NULL && ti->image_precedes ) {
214 GDrawDrawScaledImage(base,ti->image,x,y + (iheight>as?0:as-iheight));
215 x += iwidth + skip;
216 }
217 if ( ti->text!=NULL ) {
218 int ypos = y+as+(height>fh?(height-fh)/2:0);
219 int width = GDrawDrawText(base,x,ypos,ti->text,-1,fg);
220 _ggadget_underlineMnemonic(base,x,ypos,ti->text,ti->mnemonic,fg,ymax);
221 x += width + skip;
222 }
223 if ( ti->image!=NULL && !ti->image_precedes )
224 GDrawDrawScaledImage(base,ti->image,x,y + (iheight>as?0:as-iheight));
225 }
226
227 return( height );
228 }
229
230 unichar_t *utf82u_mncopy(const char *utf8buf,unichar_t *mn) {
231 int len = strlen(utf8buf);
232 unichar_t *ubuf = malloc((len+1)*sizeof(unichar_t));
233 unichar_t *upt=ubuf, *uend=ubuf+len;
234 const uint8_t *pt = (const uint8_t *) utf8buf, *end = pt+strlen(utf8buf);
235 int w;
236 int was_mn = false;
237
238 *mn = '\0';
239 while ( pt<end && *pt!='\0' && upt<uend ) {
240 if ( *pt<=127 ) {
241 if ( *pt!='_' )
242 *upt = *pt++;
243 else {
244 was_mn = 2;
245 ++pt;
246 --upt;
247 }
248 } else if ( *pt<=0xdf ) {
249 *upt = ((*pt&0x1f)<<6) | (pt[1]&0x3f);
250 pt += 2;
251 } else if ( *pt<=0xef ) {
252 *upt = ((*pt&0xf)<<12) | ((pt[1]&0x3f)<<6) | (pt[2]&0x3f);
253 pt += 3;
254 } else if ( upt+1<uend ) {
255 /* Um... I don't support surrogates */
256 w = ( ((*pt&0x7)<<2) | ((pt[1]&0x30)>>4) )-1;
257 *upt++ = 0xd800 | (w<<6) | ((pt[1]&0xf)<<2) | ((pt[2]&0x30)>>4);
258 *upt = 0xdc00 | ((pt[2]&0xf)<<6) | (pt[3]&0x3f);
259 pt += 4;
260 } else {
261 /* no space for surrogate */
262 pt += 4;
263 }
264 ++upt;
265 if ( was_mn==1 ) {
266 *mn = upt[-1];
267 if ( islower(*mn) ) *mn = toupper(*mn);
268 }
269 --was_mn;
270 }
271 *upt = '\0';
272 return( ubuf );
273 }
274
275 GTextInfo *GTextInfoCopy(GTextInfo *ti) {
276 GTextInfo *copy;
277
278 copy = malloc(sizeof(GTextInfo));
279 *copy = *ti;
280 if ( copy->fg == 0 && copy->bg == 0 ) {
281 copy->fg = copy->bg = COLOR_UNKNOWN;
282 }
283 if ( ti->text!=NULL ) {
284 if ( ti->text_is_1byte ) {
285 copy->text = utf82u_mncopy((char *) copy->text,&copy->mnemonic);
286 } else
287 copy->text = u_copy(copy->text);
288 }
289 copy->text_in_resource = false;
290 copy->text_is_1byte = false;
291 return( copy);
292 }
293
294 static const char *imagedir_default = "fontforge-pixmaps";
295 static char *imagedir = NULL; /* This is the system pixmap directory */
296 static char **imagepath = NULL; /* May contain user directories too */
297 static size_t imagepathlenmax = 0;
298
299 #define IC_SIZE 127
300 static GImageCacheBucket *imagecache[IC_SIZE];
301
302 void InitImageCache() {
303 memset(imagecache, 0, IC_SIZE * sizeof(GImageCacheBucket *));
304 }
305
306 void ClearImageCache() {
307 for ( int i=0; i<IC_SIZE; ++i ) {
308 GImageCacheBucket * bucket;
309 GImageCacheBucket * nextbucket;
310 for ( bucket = imagecache[i]; bucket!=NULL; bucket=nextbucket ) {
311 nextbucket = bucket->next;
312 if (bucket->filename != NULL) { free(bucket->filename); bucket->filename = NULL; }
313 if (bucket->absname != NULL) { free(bucket->absname); bucket->absname = NULL; }
314 if (bucket->image != NULL) { GImageDestroy(bucket->image); bucket->image = NULL; }
315 free(bucket);
316 }
317 imagecache[i] = NULL;
318 }
319 }
320
321 static int hash_filename(const char *_pt ) {
322 const unsigned char *pt = (const unsigned char *) _pt;
323 int val = 0;
324
325 while ( *pt ) {
326 val <<= 1;
327 if ( val & 0x8000 ) {
328 val &= ~0x8000;
329 val ^= 1;
330 }
331 val ^= *pt++;
332 }
333 return( val%IC_SIZE );
334 }
335
336 static void InitImagePath(void) {
337 if ( imagepath==NULL ) {
338 imagepath = malloc(2*sizeof(void *));
339 imagepath[0] = (imagedir == NULL) ? copy(imagedir_default) : copy(imagedir);
340 imagepath[1] = NULL;
341 imagepathlenmax = strlen(imagepath[0]);
342 }
343 }
344
345 /**
346 * \return The image path. The return value should not be freed or modified.
347 */
348 const char* const* _GGadget_GetImagePath(void) {
349 InitImagePath();
350 return (const char* const*) imagepath;
351 }
352
353 int _GGadget_ImageInCache(GImage *image) {
354 int i;
355 GImageCacheBucket *bucket;
356
357 for ( i=0; i<IC_SIZE; ++i ) {
358 for ( bucket=imagecache[i]; bucket!=NULL; bucket=bucket->next )
359 if ( bucket->image==image )
360 return( true );
361 }
362 return( false );
363 }
364
365 static GImage *GadgetNormalizeImageFilenames(const char *inname, char **relname, char **absname) {
366 int l, k;
367 char *home = GFileGetHomeDir(), *path;
368 GImage *img;
369
370 if ( relname!=NULL )
371 *relname=NULL;
372 if ( absname!=NULL )
373 *absname=NULL;
374
375 if ( inname==NULL )
376 return NULL;
377
378 InitImagePath();
379
380 if ( *inname=='/' ) {
381 img = GImageRead((char *)inname);
382 if ( img==NULL )
383 return NULL;
384 if ( absname!=NULL )
385 *absname = copy(inname);
386 if ( relname!=NULL ) {
387 for ( k=0; imagepath[k]!=NULL; ++k ) {
388 l = strlen(imagepath[k]);
389 if ( strncmp(imagepath[k],inname,l)==0 && inname[l]=='/') {
390 *relname = copy(inname+l+1);
391 return img;
392 }
393 }
394 if ( home!=NULL ) {
395 l = strlen(home);
396 if ( strncmp(home,inname,l)==0 && inname[l]=='/') {
397 *relname = copy(inname+l+1);
398 return img;
399 }
400 }
401 *relname = copy(inname);
402 return img;
403 }
404 } else if ( *inname=='~' && inname[1]=='/' ) {
405 if ( home==NULL )
406 return NULL;
407 path = smprintf("%s/%s", home, inname+1);
408 img = GImageRead(path);
409 if ( img==NULL ) {
410 free(path);
411 return NULL;
412 }
413 if ( relname!=NULL )
414 *relname = copy(inname);
415 if ( absname!=NULL )
416 *absname = path;
417 return img;
418 }
419
420 for ( k=0; imagepath[k]!=NULL; ++k ) {
421 path = smprintf("%s/%s", imagepath[k], inname );
422 img = GImageRead(path);
423 if ( img!=NULL ) {
424 if ( relname!=NULL )
425 *relname = copy(inname);
426 if ( absname!=NULL )
427 *absname = path;
428 return img;
429 }
430 free(path);
431 }
432 return NULL;
433 }
434
435 static void ImageCacheReload(int do_absolute) {
436 int i,k;
437 GImageCacheBucket *bucket;
438 char *path;
439 GImage *temp, hold;
440
441 InitImagePath();
442
443 /* Try to reload the cache from the new directory */
444 /* If a file doesn't exist in the new dir when it did in the old then */
445 /* retain the old copy (people may hold pointers to it) */
446 for ( i=0; i<IC_SIZE; ++i ) {
447 for ( bucket = imagecache[i]; bucket!=NULL; bucket=bucket->next ) {
448 if ( *bucket->filename=='/' || *bucket->filename=='~' ) {
449 if ( !do_absolute )
450 continue;
451 if ( bucket->absname!=NULL ) {
452 temp = GImageRead(bucket->absname);
453 if ( temp!=NULL )
454 path = copy(bucket->absname);
455 } else {
456 // This was a negative cache value which means there are
457 // no references to it. Not much point in attempting to
458 // reload.
459 ;
460 }
461 } else {
462 for ( k=0; imagepath[k]!=NULL; ++k ) {
463 path = smprintf("%s/%s", imagepath[k], bucket->filename );
464 temp = GImageRead(path);
465 if ( temp!=NULL )
466 break;
467 else
468 free(path);
469 }
470 }
471 if ( temp!=NULL ) {
472 if ( bucket->image==NULL )
473 bucket->image = temp;
474 else {
475 /* Need to retain the GImage point, but update its */
476 /* contents, and free the old stuff */
477 hold = *(bucket->image);
478 *bucket->image = *temp;
479 *temp = hold;
480 GImageDestroy(temp);
481 }
482 free( bucket->absname );
483 bucket->absname = path;
484 }
485 }
486 }
487 }
488
489 void GGadgetSetImageDir(const char *dir) {
490 int k;
491 char *ptr = imagedir;
492 //Check if imagedir has been initialised
493 if (ptr == NULL) {
494 //We shall check later if ptr should be freed or not
495 ptr = (char*) imagedir_default;
496 }
497
498 if (dir != NULL && strcmp(ptr,dir) != 0) {
499 imagedir = copy(dir);
500 if (imagepath != NULL) {
501 for (k=0; imagepath[k] != NULL; ++k) {
502 if (strcmp(imagepath[k],ptr) == 0) {
503 break;
504 }
505 }
506
507 if (ptr != imagedir_default) {
508 free(ptr);
509 }
510 if (imagepath[k] != NULL) {
511 free(imagepath[k]);
512 imagepath[k] = copy(imagedir);
513 ImageCacheReload(false);
514 }
515 }
516 }
517 }
518
519 static char *ImagePathFigureElement(const char *start, int len) {
520 const char *homedir = GFileGetHomeDir();
521 if ( *start=='=' && len==1 ) {
522 if (imagedir == NULL) {
523 return copy(imagedir_default);
524 }
525 return copy(imagedir);
526 }
527 else if ( *start=='~' && start[1]=='/' && len>=2 && homedir!=NULL ) {
528 int hlen = strlen(homedir);
529 char *absname = malloc( hlen+len+8 );
530 strcpy(absname,homedir);
531 strncpy(absname+hlen,start+1,len-1);
532 absname[hlen+len-1] = '\0';
533 return( absname );
534 } else
535 return( copyn(start,len));
536 }
537
538 #ifndef PATH_SEPARATOR
539 # define PATH_SEPARATOR ':'
540 #endif
541
542 void GGadgetSetImagePath(const char *path) {
543 int cnt, k;
544 const char *pt, *end;
545 static char *_GGadget_CurrentImagePath=NULL;
546
547 if ( path==NULL )
548 return;
549
550 if ( _GGadget_CurrentImagePath!=NULL &&
551 strcmp(_GGadget_CurrentImagePath, path)==0 )
552 return;
553
554 free(_GGadget_CurrentImagePath);
555
556 if ( imagepath!=NULL ) {
557 for ( k=0; imagepath[k]!=NULL; ++k )
558 free( imagepath[k] );
559 free( imagepath );
560 }
561 for ( cnt=0, pt = path; (end = strchr(pt,PATH_SEPARATOR))!=NULL; ++cnt, pt = end+1 );
562 imagepath = malloc((cnt+2)*sizeof(char *));
563 for ( cnt=0, pt = path; (end = strchr(pt,PATH_SEPARATOR))!=NULL; ++cnt, pt = end+1 )
564 imagepath[cnt] = ImagePathFigureElement(pt,end-pt);
565 imagepath[cnt] = ImagePathFigureElement(pt,strlen(pt));
566 imagepath[cnt+1] = NULL;
567 imagepathlenmax = 0;
568 for ( cnt=0; imagepath[cnt]!=NULL; ++cnt )
569 if ( strlen(imagepath[cnt]) > imagepathlenmax )
570 imagepathlenmax = strlen(imagepath[cnt]);
571 ImageCacheReload(false);
572 _GGadget_CurrentImagePath = copy(path);
573 }
574
575 GImageCacheBucket *_GGadgetImageCache(const char *filename, int keep_empty) {
576 int index;
577 GImageCacheBucket *bucket;
578 char *relname, *absname;
579 GImage *img;
580
581 index = hash_filename(filename);
582 for ( bucket = imagecache[index]; bucket!=NULL; bucket = bucket->next ) {
583 if ( strcmp(bucket->filename, filename)==0 )
584 return bucket;
585 }
586
587 img = GadgetNormalizeImageFilenames(filename, &relname, &absname);
588
589 if ( img==NULL ) {
590 if ( keep_empty ) { // Add negative cache value
591 bucket = calloc(1,sizeof(GImageCacheBucket));
592 bucket->filename = copy(filename);
593 bucket->next = imagecache[index];
594 imagecache[index] = bucket;
595 return bucket;
596 }
597 return NULL;
598 }
599
600 // Recheck for normalized name in cache
601 index = hash_filename(relname);
602 for ( bucket = imagecache[index]; bucket!=NULL; bucket = bucket->next ) {
603 if ( strcmp(bucket->filename, relname)==0 ) {
604 GImageDestroy(img);
605 free(absname);
606 free(relname);
607 return bucket;
608 }
609 }
610
611 /* Play with the clut to make white be transparent */
612 struct _GImage *base = img->u.image;
613 if ( base->image_type==it_mono && base->clut==NULL )
614 base->trans = 1;
615 else if ( base->image_type!=it_true && base->clut!=NULL && base->trans==0xffffffff ) {
616 int i;
617 for ( i=0 ; i<base->clut->clut_len; ++i ) {
618 if ( base->clut->clut[i]==0xffffff ) {
619 base->trans = i;
620 break;
621 }
622 }
623 }
624
625 bucket = calloc(1,sizeof(GImageCacheBucket));
626 bucket->filename = relname;
627 bucket->absname = absname;
628 bucket->image = img;
629 bucket->next = imagecache[index];
630 imagecache[index] = bucket;
631 return bucket;
632 }
633
634 GImage *GGadgetImageCache(const char *filename) {
635 GImageCacheBucket *bucket = _GGadgetImageCache(filename,true);
636 if ( bucket!=NULL )
637 return bucket->image;
638 return NULL;
639 }
640
641 /* Substitutes an image contents with what's found in cache. */
642 /* That is, unless there is nothing found in the cache. */
643 int TryGGadgetImageCache(GImage *image, const char *name) {
644 GImage *loaded = GGadgetImageCache(name);
645 if (loaded != NULL) *image = *loaded;
646 return (loaded != NULL);
647 }
648
649 static void GTextInfoImageLookup(GTextInfo *ti) {
650 char *pt;
651 int any;
652
653 if ( ti->image==NULL )
654 return;
655
656 /* Image might be an image pointer, or it might be a filename we want to */
657 /* read and convert into an image. If it's an image it will begin with */
658 /* a short containing a small number (usually 1), which won't look like */
659 /* a filename */
660 any = 0;
661 for ( pt = (char *) (ti->image); *pt!='\0'; ++pt ) {
662 if ( *pt<' ' || *pt>=0x7f )
663 return;
664 if ( *pt=='.' )
665 any = 1;
666 }
667 if ( !any ) /* Must have an extension */
668 return;
669
670 ti->image = GGadgetImageCache((char *) (ti->image));
671 }
672
673 GTextInfo **GTextInfoArrayFromList(GTextInfo *ti, uint16_t *cnt) {
674 int i;
675 GTextInfo **arr;
676
677 i = 0;
678 if ( ti!=NULL )
679 for ( ; ti[i].text!=NULL || ti[i].image!=NULL || ti[i].line; ++i );
680 if ( i==0 ) {
681 arr = malloc(sizeof(GTextInfo *));
682 i =0;
683 } else {
684 arr = malloc((i+1)*sizeof(GTextInfo *));
685 for ( i=0; ti[i].text!=NULL || ti[i].image!=NULL || ti[i].line; ++i )
686 arr[i] = GTextInfoCopy(&ti[i]);
687 }
688 arr[i] = calloc(1,sizeof(GTextInfo));
689 if ( cnt!=NULL ) *cnt = i;
690 return( arr );
691 }
692
693 GTextInfo **GTextInfoArrayCopy(GTextInfo **ti) {
694 int i;
695 GTextInfo **arr;
696
697 if ( ti==NULL || (ti[0]->image==NULL && ti[0]->text==NULL && !ti[0]->line) ) {
698 arr = malloc(sizeof(GTextInfo *));
699 i =0;
700 } else {
701 for ( i=0; ti[i]->text!=NULL || ti[i]->image!=NULL || ti[i]->line; ++i );
702 arr = malloc((i+1)*sizeof(GTextInfo *));
703 for ( i=0; ti[i]->text!=NULL || ti[i]->image!=NULL || ti[i]->line; ++i )
704 arr[i] = GTextInfoCopy(ti[i]);
705 }
706 arr[i] = calloc(1,sizeof(GTextInfo));
707 return( arr );
708 }
709
710 int GTextInfoArrayCount(GTextInfo **ti) {
711 int i;
712
713 for ( i=0; ti[i]->text || ti[i]->image || ti[i]->line; ++i );
714 return( i );
715 }
716
717 void GTextInfoFree(GTextInfo *ti) {
718 if ( !ti->text_in_resource )
719 free(ti->text);
720 free(ti);
721 }
722
723 void GTextInfoListFree(GTextInfo *ti) {
724 int i;
725
726 if ( ti==NULL )
727 return;
728
729 for ( i=0; ti[i].text!=NULL || ti[i].image!=NULL || ti[i].line; ++i )
730 if ( !ti[i].text_in_resource )
731 free(ti[i].text);
732 free(ti);
733 }
734
735 /* The list is terminated with an empty entry. Not a NULL pointer, but
736 * rather an empty entry terminates lists of GTextInfo entries. (!)
737 */
738 void GTextInfoArrayFree(GTextInfo **ti) {
739 int i;
740
741 if ( ti == NULL )
742 return;
743 for ( i=0; ti[i]->text || ti[i]->image || ti[i]->line; ++i )
744 GTextInfoFree(ti[i]);
745 GTextInfoFree(ti[i]); /* And free the null entry at end */
746 free(ti);
747 }
748
749 int GTextInfoCompare(GTextInfo *ti1, GTextInfo *ti2) {
750 GTextInfo2 *_ti1 = (GTextInfo2 *) ti1, *_ti2 = (GTextInfo2 *) ti2;
751
752 if ( _ti1->sort_me_first_in_list != _ti2->sort_me_first_in_list ) {
753 if ( _ti1->sort_me_first_in_list )
754 return( -1 );
755 else
756 return( 1 );
757 }
758
759 if ( ti1->text == NULL && ti2->text==NULL )
760 return( 0 );
761 else if ( ti1->text==NULL )
762 return( -1 );
763 else if ( ti2->text==NULL )
764 return( 1 );
765 else {
766 char *t1, *t2;
767 int ret;
768 t1 = u2utf8_copy(ti1->text);
769 t2 = u2utf8_copy(ti2->text);
770 ret = strcoll(t1,t2);
771 free(t1); free(t2);
772 return( ret );
773 }
774 }
775
776 GTextInfo **GTextInfoFromChars(char **array, int len) {
777 int i;
778 GTextInfo **ti;
779
780 if ( array==NULL || len==0 )
781 return( NULL );
782 if ( len==-1 ) {
783 for ( len=0; array[len]!=NULL; ++len );
784 } else {
785 for ( i=0; i<len && array[i]!=NULL; ++i );
786 len = i;
787 }
788 ti = malloc((len+1)*sizeof(GTextInfo *));
789 for ( i=0; i<len; ++i ) {
790 ti[i] = calloc(1,sizeof(GTextInfo));
791 ti[i]->text = uc_copy(array[i]);
792 ti[i]->fg = ti[i]->bg = COLOR_DEFAULT;
793 }
794 ti[i] = calloc(1,sizeof(GTextInfo));
795 return( ti );
796 }
797
798 void GMenuItemArrayFree(GMenuItem *mi) {
799 int i;
800
801 if ( mi == NULL )
802 return;
803 for ( i=0; mi[i].ti.text || mi[i].ti.image || mi[i].ti.line; ++i ) {
804 GMenuItemArrayFree(mi[i].sub);
805 free(mi[i].ti.text);
806 }
807 free(mi);
808 }
809
810 GMenuItem *GMenuItemArrayCopy(GMenuItem *mi, uint16_t *cnt) {
811 int i;
812 GMenuItem *arr;
813
814 if ( mi==NULL )
815 return( NULL );
816 for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.image!=NULL || mi[i].ti.line; ++i );
817 if ( i==0 )
818 return( NULL );
819 arr = malloc((i+1)*sizeof(GMenuItem));
820 for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.image!=NULL || mi[i].ti.line; ++i ) {
821 arr[i] = mi[i];
822 GTextInfoImageLookup(&arr[i].ti);
823 if ( mi[i].ti.text!=NULL ) {
824 if ( mi[i].ti.text_is_1byte )
825 arr[i].ti.text = utf82u_mncopy((char *) mi[i].ti.text,&arr[i].ti.mnemonic);
826 else
827 arr[i].ti.text = u_copy(mi[i].ti.text);
828 arr[i].ti.text_in_resource = arr[i].ti.text_is_1byte = false;
829 }
830 if ( islower(arr[i].ti.mnemonic))
831 arr[i].ti.mnemonic = toupper(arr[i].ti.mnemonic);
832 if ( islower(arr[i].shortcut))
833 arr[i].shortcut = toupper(arr[i].shortcut);
834 if ( mi[i].sub!=NULL )
835 arr[i].sub = GMenuItemArrayCopy(mi[i].sub,NULL);
836 }
837 memset(&arr[i],'\0',sizeof(GMenuItem));
838 if ( cnt!=NULL ) *cnt = i;
839 return( arr );
840 }
841
842 int GMenuItemArrayMask(GMenuItem *mi) {
843 int mask = 0;
844 int i;
845
846 for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.image!=NULL || mi[i].ti.line; ++i ) {
847 if ( mi[i].sub!=NULL )
848 mask |= GMenuItemArrayMask(mi[i].sub);
849 else
850 mask |= mi[i].short_mask;
851 }
852 return( mask );
853 }
854
855 int GMenuItemArrayAnyUnmasked(GMenuItem *mi) {
856 int i;
857
858 for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.image!=NULL || mi[i].ti.line; ++i ) {
859 if ( mi[i].sub!=NULL ) {
860 if ( GMenuItemArrayAnyUnmasked(mi[i].sub) )
861 return( true );
862 } else {
863 if ( (mi[i].short_mask&~ksm_shift)==0 && mi[i].shortcut!=0 )
864 return( true );
865 }
866 }
867 return( false );
868 }
869
870 void GMenuItem2ArrayFree(GMenuItem2 *mi) {
871 int i;
872
873 if ( mi == NULL )
874 return;
875 for ( i=0; mi[i].ti.text || mi[i].ti.image || mi[i].ti.line; ++i ) {
876 GMenuItem2ArrayFree(mi[i].sub);
877 free(mi[i].ti.text);
878 }
879 free(mi);
880 }
881
882 static char *shortcut_domain = "shortcuts";
883
884 void GMenuSetShortcutDomain(char *domain) {
885 shortcut_domain = domain;
886 }
887
888 const char *GMenuGetShortcutDomain(void) {
889 return(shortcut_domain);
890 }
891
892 static struct { char *modifier; int mask; char *alt; } modifiers[] = {
893 { "Ctl+", ksm_control, NULL },
894 { "Ctrl+", ksm_control, NULL },
895 { "Control+", ksm_control, NULL },
896 { "Shft+", ksm_shift, NULL },
897 { "Shift+", ksm_shift, NULL },
898 { "CapsLk+", ksm_capslock, NULL },
899 { "CapsLock+", ksm_capslock, NULL },
900 { "Meta+", ksm_meta, NULL },
901 { "Alt+", ksm_meta, NULL },
902 { "Esc+", ksm_meta, NULL },
903 { "Flag0x01+", 0x01, NULL },
904 { "Flag0x02+", 0x02, NULL },
905 { "Flag0x04+", 0x04, NULL },
906 { "Flag0x08+", 0x08, NULL },
907 { "Flag0x10+", 0x10, NULL },
908 { "Flag0x20+", 0x20, NULL },
909 { "Flag0x40+", 0x40, NULL },
910 { "Flag0x80+", 0x80, NULL },
911 { "Opt+", ksm_meta, NULL },
912 { "Option+", ksm_meta, NULL },
913 /* We used to map command to control on the mac, no longer, let it be itself */
914 { "Command+", ksm_cmdmacosx, NULL },
915 { "Cmd+", ksm_cmdmacosx, NULL },
916 { "NumLk+", ksm_cmdmacosx, NULL }, /* This is unfortunate. Numlock should be ignored, Command should not */
917 { "NumLock+", ksm_cmdmacosx, NULL },
918 { "numlock+", ksm_cmdmacosx, NULL },
919 { "numberlock+", ksm_cmdmacosx, NULL },
920 { NULL, 0, NULL }
921 /* Windows flag key=Super (keysym ffeb/ffec) key maps to 0x40 on my machine */
922 };
923
924 static void initmods(void) {
925 if ( modifiers[0].alt==NULL ) {
926 int i;
927 for ( i=0; modifiers[i].modifier!=NULL; ++i )
928 modifiers[i].alt = dgettext(shortcut_domain,modifiers[i].modifier);
929 }
930 }
931
932 int GMenuItemParseMask(char *shortcut) {
933 char *pt, *sh;
934 int mask, temp, i;
935
936 sh = dgettext(shortcut_domain,shortcut);
937 if ( sh==shortcut && strlen(shortcut)>2 && shortcut[2]=='*' ) {
938 sh = dgettext(shortcut_domain,shortcut+3);
939 if ( sh==shortcut+3 )
940 sh = shortcut;
941 }
942 pt = strchr(sh,'|');
943 if ( pt!=NULL )
944 sh = pt+1;
945 if ( *sh=='\0' || strcmp(sh,"No Shortcut")==0 || strcmp(sh,"None")==0 )
946 return(0);
947
948 initmods();
949
950 mask = 0;
951 for (;;) {
952 pt = strchr(sh,'+');
953 if ( pt==sh || *sh=='\0' )
954 return( mask );
955 if ( pt==NULL )
956 pt = sh+strlen(sh);
957 for ( i=0; modifiers[i].modifier!=NULL; ++i ) {
958 if ( strncasecmp(sh,modifiers[i].modifier,pt-sh)==0 )
959 break;
960 }
961 if ( modifiers[i].modifier==NULL ) {
962 for ( i=0; modifiers[i].alt!=NULL; ++i ) {
963 if ( strncasecmp(sh,modifiers[i].alt,pt-sh)==0 )
964 break;
965 }
966 }
967 if ( modifiers[i].modifier!=NULL )
968 mask |= modifiers[i].mask;
969 else if ( sscanf( sh, "0x%x", &temp)==1 )
970 mask |= temp;
971 else {
972 fprintf( stderr, "Could not parse short cut: %s\n", shortcut );
973 return(0);
974 }
975 sh = pt+1;
976 }
977 }
978
979 int HotkeyParse( Hotkey* hk, const char *shortcut ) {
980 char *pt;
981 const char *sh;
982 int mask, temp, i;
983
984 if ( hk!=NULL ) {
985 hk->state = 0;
986 hk->keysym = 0;
987 strncpy( hk->text, shortcut, HOTKEY_TEXT_MAX_SIZE );
988 }
989
990 sh = dgettext(shortcut_domain,shortcut);
991 /* shortcut might be "Open|Ctl+O" meaning the Open menu item is bound to ^O */
992 /* or "CV*Open|Ctl+O" meaning that in the charview the Open menu item ...*/
993 /* but if CV*Open|Ctl+O isn't found then check simple "Open|Ctl+O" as a default */
994 if ( sh==shortcut && strlen(shortcut)>2 && shortcut[2]=='*' ) {
995 sh = dgettext(shortcut_domain,shortcut+3);
996 if ( sh==shortcut+3 )
997 sh = shortcut;
998 }
999 pt = strchr(sh,'|');
1000 if ( pt!=NULL )
1001 sh = pt+1;
1002 if ( *sh=='\0' || strcmp(sh,"No Shortcut")==0 || strcmp(sh,"None")==0 )
1003 return true;
1004
1005 initmods();
1006
1007 mask = 0;
1008 while ( (pt=strchr(sh,'+'))!=NULL && pt!=sh ) { /* A '+' can also occur as the short cut char itself */
1009 for ( i=0; modifiers[i].modifier!=NULL; ++i ) {
1010 if ( strncasecmp(sh,modifiers[i].modifier,pt-sh)==0 )
1011 break;
1012 }
1013 if ( modifiers[i].modifier==NULL ) {
1014 for ( i=0; modifiers[i].alt!=NULL; ++i ) {
1015 if ( strncasecmp(sh,modifiers[i].alt,pt-sh)==0 )
1016 break;
1017 }
1018 }
1019 if ( modifiers[i].modifier!=NULL )
1020 mask |= modifiers[i].mask;
1021 else if ( sscanf( sh, "0x%x", &temp)==1 )
1022 mask |= temp;
1023 else {
1024 fprintf(stderr, "Could not parse short cut: %s\n", shortcut );
1025 return false;
1026 }
1027 sh = pt+1;
1028 }
1029 if ( hk!=NULL )
1030 hk->state = mask;
1031 for ( i=0; i<0x100; ++i ) {
1032 if ( GDrawKeysyms[i]!=NULL && uc_strcmp(GDrawKeysyms[i],sh)==0 ) {
1033 if ( hk!=NULL )
1034 hk->keysym = 0xff00 + i;
1035 break;
1036 }
1037 }
1038 if ( i==0x100 ) {
1039 char tmp = utf8_ildb((const char **) &sh);
1040 if ( hk!=NULL )
1041 hk->keysym = tmp;
1042 if ( *sh!='\0' ) {
1043 fprintf(stderr, "Unexpected characters at end of short cut: %s %c\n", shortcut, *sh );
1044 return false;
1045 }
1046 }
1047
1048 if ( hk==NULL )
1049 return true;
1050 //
1051 // The user really means lower case keys unless they have
1052 // given the "shift" modifier too. Like: Ctl+Shft+L
1053 //
1054 // fprintf(stderr,"HotkeyParse(1) spec:%d hk->keysym:%d shortcut:%s\n", GK_Special, hk->keysym, shortcut );
1055 if( hk->keysym < GK_Special ) {
1056 hk->keysym = tolower(hk->keysym);
1057 if( hk->state & ksm_shift ) {
1058 hk->keysym = toupper(hk->keysym);
1059 }
1060 }
1061 if( hk->keysym == GDK_KEY_Tab ) {
1062 #ifndef __Mac
1063 if( hk->state & ksm_shift ) {
1064 hk->keysym = GDK_KEY_ISO_Left_Tab;
1065 }
1066 #endif
1067 }
1068 return true;
1069 // fprintf(stderr,"HotkeyParse(end) spec:%d hk->state:%d hk->keysym:%d shortcut:%s\n", GK_Special, hk->state, hk->keysym, shortcut );
1070 }
1071
1072 void GMenuItemParseShortCut(GMenuItem *mi,char *shortcut) {
1073 char *pt, *sh;
1074 int mask, temp, i;
1075
1076 mi->short_mask = 0;
1077 mi->shortcut = '\0';
1078
1079 sh = dgettext(shortcut_domain,shortcut);
1080 /* shortcut might be "Open|Ctl+O" meaning the Open menu item is bound to ^O */
1081 /* or "CV*Open|Ctl+O" meaning that in the charview the Open menu item ...*/
1082 /* but if CV*Open|Ctl+O isn't found then check simple "Open|Ctl+O" as a default */
1083 if ( sh==shortcut && strlen(shortcut)>2 && shortcut[2]=='*' ) {
1084 sh = dgettext(shortcut_domain,shortcut+3);
1085 if ( sh==shortcut+3 )
1086 sh = shortcut;
1087 }
1088 pt = strchr(sh,'|');
1089 if ( pt!=NULL )
1090 sh = pt+1;
1091 if ( *sh=='\0' || strcmp(sh,"No Shortcut")==0 || strcmp(sh,"None")==0 )
1092 return;
1093
1094 initmods();
1095
1096 mask = 0;
1097 while ( (pt=strchr(sh,'+'))!=NULL && pt!=sh ) { /* A '+' can also occur as the short cut char itself */
1098 for ( i=0; modifiers[i].modifier!=NULL; ++i ) {
1099 if ( strncasecmp(sh,modifiers[i].modifier,pt-sh)==0 )
1100 break;
1101 }
1102 if ( modifiers[i].modifier==NULL ) {
1103 for ( i=0; modifiers[i].alt!=NULL; ++i ) {
1104 if ( strncasecmp(sh,modifiers[i].alt,pt-sh)==0 )
1105 break;
1106 }
1107 }
1108 if ( modifiers[i].modifier!=NULL )
1109 mask |= modifiers[i].mask;
1110 else if ( sscanf( sh, "0x%x", &temp)==1 )
1111 mask |= temp;
1112 else {
1113 fprintf( stderr, "Could not parse short cut: %s\n", shortcut );
1114 return;
1115 }
1116 sh = pt+1;
1117 }
1118 mi->short_mask = mask;
1119 for ( i=0; i<0x100; ++i ) {
1120 if ( GDrawKeysyms[i]!=NULL && uc_strcmp(GDrawKeysyms[i],sh)==0 ) {
1121 mi->shortcut = 0xff00 + i;
1122 break;
1123 }
1124 }
1125 if ( i==0x100 ) {
1126 if ( mask==0 ) {
1127 static int first = true;
1128 if ( first ) {
1129 fprintf( stderr, "Warning: No modifiers in short cut: %s\n", shortcut );
1130 first = false;
1131 }
1132 }
1133 mi->shortcut = utf8_ildb((const char **) &sh);
1134 if ( *sh!='\0' ) {
1135 fprintf( stderr, "Unexpected characters at end of short cut: %s\n", shortcut );
1136 return;
1137 }
1138 }
1139 }
1140
1141 GMenuItem *GMenuItem2ArrayCopy(GMenuItem2 *mi, uint16_t *cnt) {
1142 int i;
1143 GMenuItem *arr;
1144
1145 if ( mi==NULL )
1146 return( NULL );
1147 for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.image!=NULL || mi[i].ti.line; ++i );
1148 if ( i==0 )
1149 return( NULL );
1150 arr = calloc((i+1),sizeof(GMenuItem));
1151 for ( i=0; mi[i].ti.text!=NULL || mi[i].ti.image!=NULL || mi[i].ti.line; ++i ) {
1152 arr[i].ti = mi[i].ti;
1153 GTextInfoImageLookup(&arr[i].ti);
1154 arr[i].moveto = mi[i].moveto;
1155 arr[i].invoke = mi[i].invoke;
1156 arr[i].mid = mi[i].mid;
1157 if ( mi[i].shortcut!=NULL )
1158 GMenuItemParseShortCut(&arr[i],mi[i].shortcut);
1159 if ( mi[i].ti.text!=NULL ) {
1160 if ( mi[i].ti.text_is_1byte )
1161 arr[i].ti.text = utf82u_mncopy((char *) mi[i].ti.text,&arr[i].ti.mnemonic);
1162 else
1163 arr[i].ti.text = u_copy(mi[i].ti.text);
1164 arr[i].ti.text_in_resource = arr[i].ti.text_is_1byte = false;
1165 }
1166 if ( islower(arr[i].ti.mnemonic))
1167 arr[i].ti.mnemonic = toupper(arr[i].ti.mnemonic);
1168 if ( islower(arr[i].shortcut))
1169 arr[i].shortcut = toupper(arr[i].shortcut);
1170 if ( mi[i].sub!=NULL )
1171 arr[i].sub = GMenuItem2ArrayCopy(mi[i].sub,NULL);
1172 }
1173 memset(&arr[i],'\0',sizeof(GMenuItem));
1174 if ( cnt!=NULL ) *cnt = i;
1175 return( arr );
1176 }
1177
1178 int GIntGetResource(int index) {
1179 if ( index<2 ) {
1180 static int gt_intarray[2];
1181 if ( gt_intarray[0]==0 ) {
1182 char *pt, *end;
1183 /* GT: This is an unusual string. It is used to get around a limitation in */
1184 /* GT: FontForge's widget set. You should put a number here (do NOT translate */
1185 /* GT: "GGadget|ButtonSize|", that's only to provide context. The number should */
1186 /* GT: be the number of points used for a standard sized button. It should be */
1187 /* GT: big enough to contain "OK", "Cancel", "New...", "Edit...", "Delete" */
1188 /* GT: (in their translated forms of course). */
1189 pt = S_("GGadget|ButtonSize|55");
1190 gt_intarray[0] = strtol(pt,&end,10);
1191 if ( pt==end || gt_intarray[0]<20 || gt_intarray[0]>4000 )
1192 gt_intarray[0]=55;
1193 /* GT: This is an unusual string. It is used to get around a limitation in */
1194 /* GT: FontForge's widget set. You should put a number here (do NOT translate */
1195 /* GT: "GGadget|ScaleFactor|", that's only to provide context. The number should */
1196 /* GT: be a percentage and indicates the ratio of the length of a string in */
1197 /* GT: your language to the same string's length in English. */
1198 /* GT: Suppose it takes 116 pixels to say "Ne pas enregistrer" in French but */
1199 /* GT: only 67 pixels to say "Don't Save" in English. Then a value for ScaleFactor */
1200 /* GT: might be 116*100/67 = 173 */
1201 pt = S_("GGadget|ScaleFactor|100");
1202 gt_intarray[1] = strtol(pt,&end,10);
1203 if ( pt==end || gt_intarray[1]<20 || gt_intarray[1]>4000 )
1204 gt_intarray[1]=100;
1205 }
1206 return( gt_intarray[index] );
1207 }
1208
1209 return -1;
1210 }