5e1e4e6265928b13673231c11cbf5c831f0acc02
[goodguy/history.git] / cinelerra-5.0 / guicast / bcresources.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009-2015 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcdisplayinfo.h"
23 #include "bcipc.h"
24 #include "bclistbox.inc"
25 #include "bcfontentry.h"
26 #include "bcresources.h"
27 #include "bcsignals.h"
28 #include "bcsynchronous.h"
29 #include "bcwindowbase.h"
30 #include "colors.h"
31 #include "bccmodels.h"
32 #include "cstrdup.h"
33 #include "fonts.h"
34 #include "language.h"
35 #include "vframe.h"
36
37 #include <string.h>
38 #include <iconv.h>
39 #include <sys/ipc.h>
40 #include <sys/shm.h>
41 #include <X11/extensions/XShm.h>
42 #include <fontconfig/fontconfig.h>
43 #include <fontconfig/fcfreetype.h>
44 #include <unistd.h>
45
46
47
48
49
50 int BC_Resources::error = 0;
51
52 VFrame* BC_Resources::bg_image = 0;
53 VFrame* BC_Resources::menu_bg = 0;
54
55 int BC_Resources::locale_utf8 = 0;
56 int BC_Resources::little_endian = 0;
57 char BC_Resources::language[LEN_LANG] = {0};
58 char BC_Resources::region[LEN_LANG] = {0};
59 char BC_Resources::encoding[LEN_ENCOD] = {0};
60 const char *BC_Resources::wide_encoding = 0;
61 ArrayList<BC_FontEntry*> *BC_Resources::fontlist = 0;
62 const char *BC_Resources::fc_properties[] = { FC_SLANT, FC_WEIGHT, FC_WIDTH };
63 #define LEN_FCPROP (sizeof(BC_Resources::fc_properties) / sizeof(const char*))
64
65 static const char *def_small_font = "-*-helvetica-medium-r-normal-*-%d-*";  // 10
66 static const char *def_small_font2 = "-*-helvetica-medium-r-normal-*-%d-*"; // 11
67 static const char *def_medium_font = "-*-helvetica-bold-r-normal-*-%d-*";   // 14
68 static const char *def_medium_font2 = "-*-helvetica-bold-r-normal-*-%d-*";  // 14
69 static const char *def_large_font = "-*-helvetica-bold-r-normal-*-%d-*";    // 18
70 static const char *def_large_font2 = "-*-helvetica-bold-r-normal-*-%d-*";   // 20
71 static const char *def_big_font =
72   "-*-bitstream charter-bold-r-normal-*-*-0-%d-%d-p-0-iso8859-1"; // 160
73 static const char *def_big_font2 =
74   "-*-nimbus sans l-bold-r-normal-*-*-0-%d-%d-p-0-iso8859-1";     // 160
75 static const char *def_small_fontset =  "-*-helvetica-medium-r-normal-*-%d-*";// 10
76 static const char *def_medium_fontset = "-*-helvetica-bold-r-normal-*-%d-*";  // 14
77 static const char *def_large_fontset =  "-*-helvetica-bold-r-normal-*-%d-*";  // 18
78 static const char *def_big_fontset =    "-*-helvetica-bold-r-normal-*-%d-*";  // 24
79 static const char *def_small_font_xft = "Sans:pixelsize=%.4f";           // 10.6667
80 static const char *def_small_b_font_xft = "Sans:bold:pixelsize=%.4f";    // 10.6667
81 static const char *def_medium_font_xft = "Sans:pixelsize=%.4f";          // 13.3333
82 static const char *def_medium_b_font_xft = "Sans:bold:pixelsize=%.4f";   // 13.3333
83 static const char *def_large_font_xft = "Sans:pixelsize=%.4f";           // 21.3333
84 static const char *def_large_b_font_xft = "Sans:bold:pixelsize=%.4f";    // 21.3333
85 static const char *def_big_font_xft = "Sans:pixelsize=37.3333";          // 37.3333
86 static const char *def_big_b_font_xft = "Sans:bold:pixelsize=37.33333";  // 37.3333
87
88 #define default_font_xft2 "-microsoft-verdana-*-*-*-*-*-*-*-*-*-*-*-*"
89
90 const char* BC_Resources::small_font = 0;
91 const char* BC_Resources::small_font2 = 0;
92 const char* BC_Resources::medium_font = 0;
93 const char* BC_Resources::medium_font2 = 0;
94 const char* BC_Resources::large_font = 0;
95 const char* BC_Resources::large_font2 = 0;
96 const char* BC_Resources::big_font = 0;
97 const char* BC_Resources::big_font2 = 0;
98 const char* BC_Resources::small_fontset = 0;
99 const char* BC_Resources::medium_fontset = 0;
100 const char* BC_Resources::large_fontset = 0;
101 const char* BC_Resources::big_fontset = 0;
102 const char* BC_Resources::small_font_xft = 0;
103 const char* BC_Resources::small_font_xft2 = 0;
104 const char* BC_Resources::small_b_font_xft = 0;
105 const char* BC_Resources::medium_font_xft = 0;
106 const char* BC_Resources::medium_font_xft2 = 0;
107 const char* BC_Resources::medium_b_font_xft = 0;
108 const char* BC_Resources::large_font_xft = 0;
109 const char* BC_Resources::large_font_xft2 = 0;
110 const char* BC_Resources::large_b_font_xft = 0;
111 const char* BC_Resources::big_font_xft = 0;
112 const char* BC_Resources::big_font_xft2 = 0;
113 const char* BC_Resources::big_b_font_xft = 0;
114
115 #define def_font(v, s...) do { sprintf(string,def_##v,s); v = cstrdup(string); } while(0)
116 #define set_font(v, s) do { sprintf(string, "%s", s); v = cstrdup(string); } while(0)
117 #define iround(v) ((int)(v+0.5))
118 void BC_Resources::init_font_defs(double scale)
119 {
120         char string[BCTEXTLEN];
121         def_font(small_font,       iround(scale*11));
122         def_font(small_font2,      iround(scale*11));
123         def_font(medium_font,      iround(scale*14));
124         def_font(medium_font2,     iround(scale*14));
125         def_font(large_font,       iround(scale*18));
126         def_font(large_font2,      iround(scale*20));
127         def_font(big_font,         iround(scale*160), iround(scale*160));
128         def_font(big_font2,        iround(scale*160), iround(scale*160));
129         def_font(small_fontset,    iround(scale*10));
130         def_font(medium_fontset,   iround(scale*14));
131         def_font(large_fontset,    iround(scale*18));
132         def_font(big_fontset,      iround(scale*24));
133         def_font(small_font_xft,   (scale*10.6667));
134         def_font(small_b_font_xft, (scale*10.6667));
135         def_font(medium_font_xft,  (scale*13.3333));
136         def_font(medium_b_font_xft,(scale*13.3333));
137         def_font(large_font_xft,   (scale*21.3333));
138         def_font(large_b_font_xft, (scale*21.3333));
139         def_font(big_font_xft,     (scale*37.3333));
140         def_font(big_b_font_xft,   (scale*37.3333));
141
142         set_font(small_font_xft2,  default_font_xft2);
143         set_font(medium_font_xft2, default_font_xft2);
144         set_font(large_font_xft2,  default_font_xft2);
145         set_font(big_font_xft2,    default_font_xft2);
146 }
147
148 suffix_to_type_t BC_Resources::suffix_to_type[] =
149 {
150         { "m2v", ICON_FILM },
151         { "mov", ICON_FILM },
152         { "mp2", ICON_SOUND },
153         { "mp3", ICON_SOUND },
154         { "ac3", ICON_SOUND },
155         { "mpg", ICON_FILM },
156         { "vob", ICON_FILM },
157         { "ifo", ICON_FILM },
158         { "ts",  ICON_FILM },
159         { "vts", ICON_FILM },
160         { "wav", ICON_SOUND }
161 };
162
163 BC_Signals* BC_Resources::signal_handler = 0;
164 Mutex BC_Resources::fontconfig_lock("BC_Resources::fonconfig_lock");
165
166 int BC_Resources::x_error_handler(Display *display, XErrorEvent *event)
167 {
168 #if defined(OUTPUT_X_ERROR)
169         char string[1024];
170         XGetErrorText(event->display, event->error_code, string, 1024);
171         fprintf(stderr, "BC_Resources::x_error_handler: error_code=%d opcode=%d,%d %s\n",
172                 event->error_code,
173                 event->request_code,
174                 event->minor_code,
175                 string);
176 #endif
177
178         BC_Resources::error = 1;
179 // This bug only happens in 32 bit mode.
180 //      if(sizeof(long) == 4)
181 //              BC_WindowBase::get_resources()->use_xft = 0;
182         return 0;
183 }
184
185 int BC_Resources::machine_cpus = 1;
186
187 int BC_Resources::get_machine_cpus()
188 {
189         int cpus = 1;
190         FILE *proc = fopen("/proc/cpuinfo", "r");
191         if( proc ) {
192                 char string[BCTEXTLEN], *cp;
193                 while(!feof(proc) && fgets(string, sizeof(string), proc) ) {
194                         if( !strncasecmp(string, "processor", 9) &&
195                             (cp = strchr(string, ':')) != 0 ) {
196                                 int n = atol(cp+1) + 1;
197                                 if( n > cpus ) cpus = n;
198                         }
199                         else if( !strncasecmp(string, "cpus detected", 13) &&
200                             (cp = strchr(string, ':')) != 0 )
201                                 cpus = atol(cp+1);
202                 }
203                 fclose(proc);
204         }
205         return cpus;
206 }
207
208 BC_Resources::BC_Resources()
209 {
210         synchronous = 0;
211         vframe_shm = 0;
212
213         display_info = new BC_DisplayInfo("", 0);
214         int display_w = display_info->get_root_w();
215         int display_h = display_info->get_root_h();
216         int display_size = display_h < display_w ? display_h : display_w;
217         double default_scale = display_size/1000.;
218         char *env = getenv("BC_FONT_SCALE");
219         font_scale = env ? atof(env) : default_scale;
220         if( font_scale <= 0 ) font_scale = 1;
221         init_font_defs(font_scale);
222         env = getenv("BC_ICON_SCALE");
223         icon_scale = env ? atof(env) : default_scale;
224         if( icon_scale <= 0 ) icon_scale = 1;
225
226         id_lock = new Mutex("BC_Resources::id_lock");
227         create_window_lock = new Mutex("BC_Resources::create_window_lock", 1);
228         id = 0;
229         machine_cpus = get_machine_cpus();
230
231         for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
232                 filebox_history[i].path[0] = 0;
233
234 #ifdef HAVE_XFT
235         XftInitFtLibrary();
236 #endif
237
238         little_endian = (*(const u_int32_t*)"\01\0\0\0") & 1;
239         wide_encoding = little_endian ?  "UTF32LE" : "UTF32BE";
240         use_xvideo = 1;
241
242 #include "images/file_film_png.h"
243 #include "images/file_folder_png.h"
244 #include "images/file_sound_png.h"
245 #include "images/file_unknown_png.h"
246 #include "images/file_column_png.h"
247         static VFrame* default_type_to_icon[] =
248         {
249                 new VFramePng(file_folder_png),
250                 new VFramePng(file_unknown_png),
251                 new VFramePng(file_film_png),
252                 new VFramePng(file_sound_png),
253                 new VFramePng(file_column_png)
254         };
255         type_to_icon = default_type_to_icon;
256
257
258 #include "images/bar_png.h"
259         static VFrame* default_bar = new VFramePng(bar_png);
260         bar_data = default_bar;
261
262
263 #include "images/cancel_up_png.h"
264 #include "images/cancel_hi_png.h"
265 #include "images/cancel_dn_png.h"
266         static VFrame* default_cancel_images[] =
267         {
268                 new VFramePng(cancel_up_png),
269                 new VFramePng(cancel_hi_png),
270                 new VFramePng(cancel_dn_png)
271         };
272
273 #include "images/ok_up_png.h"
274 #include "images/ok_hi_png.h"
275 #include "images/ok_dn_png.h"
276         static VFrame* default_ok_images[] =
277         {
278                 new VFramePng(ok_up_png),
279                 new VFramePng(ok_hi_png),
280                 new VFramePng(ok_dn_png)
281         };
282
283 #include "images/usethis_up_png.h"
284 #include "images/usethis_uphi_png.h"
285 #include "images/usethis_dn_png.h"
286         static VFrame* default_usethis_images[] =
287         {
288                 new VFramePng(usethis_up_png),
289                 new VFramePng(usethis_uphi_png),
290                 new VFramePng(usethis_dn_png)
291         };
292
293 #if 0
294 #include "images/checkbox_checked_png.h"
295 #include "images/checkbox_dn_png.h"
296 #include "images/checkbox_checkedhi_png.h"
297 #include "images/checkbox_up_png.h"
298 #include "images/checkbox_hi_png.h"
299         static VFrame* default_checkbox_images[] =
300         {
301                 new VFramePng(checkbox_up_png),
302                 new VFramePng(checkbox_hi_png),
303                 new VFramePng(checkbox_checked_png),
304                 new VFramePng(checkbox_dn_png),
305                 new VFramePng(checkbox_checkedhi_png)
306         };
307
308 #include "images/radial_checked_png.h"
309 #include "images/radial_dn_png.h"
310 #include "images/radial_checkedhi_png.h"
311 #include "images/radial_up_png.h"
312 #include "images/radial_hi_png.h"
313         static VFrame* default_radial_images[] =
314         {
315                 new VFramePng(radial_up_png),
316                 new VFramePng(radial_hi_png),
317                 new VFramePng(radial_checked_png),
318                 new VFramePng(radial_dn_png),
319                 new VFramePng(radial_checkedhi_png)
320         };
321
322         static VFrame* default_label_images[] =
323         {
324                 new VFramePng(radial_up_png),
325                 new VFramePng(radial_hi_png),
326                 new VFramePng(radial_checked_png),
327                 new VFramePng(radial_dn_png),
328                 new VFramePng(radial_checkedhi_png)
329         };
330 #endif
331
332 #include "images/check_png.h"
333         static VFrame* default_check_image = new VFramePng(check_png);
334         check = default_check_image;
335
336 #include "images/file_text_up_png.h"
337 #include "images/file_text_hi_png.h"
338 #include "images/file_text_dn_png.h"
339 #include "images/file_icons_up_png.h"
340 #include "images/file_icons_hi_png.h"
341 #include "images/file_icons_dn_png.h"
342 #include "images/file_newfolder_up_png.h"
343 #include "images/file_newfolder_hi_png.h"
344 #include "images/file_newfolder_dn_png.h"
345 #include "images/file_rename_up_png.h"
346 #include "images/file_rename_hi_png.h"
347 #include "images/file_rename_dn_png.h"
348 #include "images/file_updir_up_png.h"
349 #include "images/file_updir_hi_png.h"
350 #include "images/file_updir_dn_png.h"
351 #include "images/file_delete_up_png.h"
352 #include "images/file_delete_hi_png.h"
353 #include "images/file_delete_dn_png.h"
354 #include "images/file_reload_up_png.h"
355 #include "images/file_reload_hi_png.h"
356 #include "images/file_reload_dn_png.h"
357         static VFrame* default_filebox_text_images[] =
358         {
359                 new VFramePng(file_text_up_png),
360                 new VFramePng(file_text_hi_png),
361                 new VFramePng(file_text_dn_png)
362         };
363
364         static VFrame* default_filebox_icons_images[] =
365         {
366                 new VFramePng(file_icons_up_png),
367                 new VFramePng(file_icons_hi_png),
368                 new VFramePng(file_icons_dn_png)
369         };
370
371         static VFrame* default_filebox_updir_images[] =
372         {
373                 new VFramePng(file_updir_up_png),
374                 new VFramePng(file_updir_hi_png),
375                 new VFramePng(file_updir_dn_png)
376         };
377
378         static VFrame* default_filebox_newfolder_images[] =
379         {
380                 new VFramePng(file_newfolder_up_png),
381                 new VFramePng(file_newfolder_hi_png),
382                 new VFramePng(file_newfolder_dn_png)
383         };
384
385
386         static VFrame* default_filebox_rename_images[] =
387         {
388                 new VFramePng(file_rename_up_png),
389                 new VFramePng(file_rename_hi_png),
390                 new VFramePng(file_rename_dn_png)
391         };
392
393         static VFrame* default_filebox_delete_images[] =
394         {
395                 new VFramePng(file_delete_up_png),
396                 new VFramePng(file_delete_hi_png),
397                 new VFramePng(file_delete_dn_png)
398         };
399
400         static VFrame* default_filebox_reload_images[] =
401         {
402                 new VFramePng(file_reload_up_png),
403                 new VFramePng(file_reload_hi_png),
404                 new VFramePng(file_reload_dn_png)
405         };
406
407 #include "images/listbox_button_dn_png.h"
408 #include "images/listbox_button_hi_png.h"
409 #include "images/listbox_button_up_png.h"
410 #include "images/listbox_button_disabled_png.h"
411         static VFrame* default_listbox_button[] =
412         {
413                 new VFramePng(listbox_button_up_png),
414                 new VFramePng(listbox_button_hi_png),
415                 new VFramePng(listbox_button_dn_png),
416                 new VFramePng(listbox_button_disabled_png)
417         };
418         listbox_button = default_listbox_button;
419
420 #include "images/menu_popup_bg_png.h"
421         static VFrame* default_listbox_bg = 0;
422         listbox_bg = default_listbox_bg;
423
424 #include "images/listbox_expandchecked_png.h"
425 #include "images/listbox_expandcheckedhi_png.h"
426 #include "images/listbox_expanddn_png.h"
427 #include "images/listbox_expandup_png.h"
428 #include "images/listbox_expanduphi_png.h"
429         static VFrame* default_listbox_expand[] =
430         {
431                 new VFramePng(listbox_expandup_png),
432                 new VFramePng(listbox_expanduphi_png),
433                 new VFramePng(listbox_expandchecked_png),
434                 new VFramePng(listbox_expanddn_png),
435                 new VFramePng(listbox_expandcheckedhi_png),
436         };
437         listbox_expand = default_listbox_expand;
438
439 #include "images/listbox_columnup_png.h"
440 #include "images/listbox_columnhi_png.h"
441 #include "images/listbox_columndn_png.h"
442         static VFrame* default_listbox_column[] =
443         {
444                 new VFramePng(listbox_columnup_png),
445                 new VFramePng(listbox_columnhi_png),
446                 new VFramePng(listbox_columndn_png)
447         };
448         listbox_column = default_listbox_column;
449
450
451 #include "images/listbox_up_png.h"
452 #include "images/listbox_dn_png.h"
453         listbox_up = new VFramePng(listbox_up_png);
454         listbox_dn = new VFramePng(listbox_dn_png);
455         listbox_title_overlap = 0;
456         listbox_title_margin = 0;
457         listbox_title_color = BLACK;
458         listbox_title_hotspot = 5;
459
460         listbox_border1 = DKGREY;
461         listbox_border2_hi = RED;
462         listbox_border2 = BLACK;
463         listbox_border3_hi = RED;
464         listbox_border3 = MEGREY;
465         listbox_border4 = WHITE;
466         listbox_selected = BLUE;
467         listbox_highlighted = LTGREY;
468         listbox_inactive = WHITE;
469         listbox_text = BLACK;
470
471 #include "images/pot_hi_png.h"
472 #include "images/pot_up_png.h"
473 #include "images/pot_dn_png.h"
474         static VFrame *default_pot_images[] =
475         {
476                 new VFramePng(pot_up_png),
477                 new VFramePng(pot_hi_png),
478                 new VFramePng(pot_dn_png)
479         };
480
481 #include "images/progress_up_png.h"
482 #include "images/progress_hi_png.h"
483         static VFrame* default_progress_images[] =
484         {
485                 new VFramePng(progress_up_png),
486                 new VFramePng(progress_hi_png)
487         };
488
489         pan_data = 0;
490         pan_text_color = YELLOW;
491
492 #include "images/7seg_small/0_png.h"
493 #include "images/7seg_small/1_png.h"
494 #include "images/7seg_small/2_png.h"
495 #include "images/7seg_small/3_png.h"
496 #include "images/7seg_small/4_png.h"
497 #include "images/7seg_small/5_png.h"
498 #include "images/7seg_small/6_png.h"
499 #include "images/7seg_small/7_png.h"
500 #include "images/7seg_small/8_png.h"
501 #include "images/7seg_small/9_png.h"
502 #include "images/7seg_small/colon_png.h"
503 #include "images/7seg_small/period_png.h"
504 #include "images/7seg_small/a_png.h"
505 #include "images/7seg_small/b_png.h"
506 #include "images/7seg_small/c_png.h"
507 #include "images/7seg_small/d_png.h"
508 #include "images/7seg_small/e_png.h"
509 #include "images/7seg_small/f_png.h"
510 #include "images/7seg_small/space_png.h"
511 #include "images/7seg_small/dash_png.h"
512         static VFrame* default_medium_7segment[] =
513         {
514                 new VFramePng(_0_png),
515                 new VFramePng(_1_png),
516                 new VFramePng(_2_png),
517                 new VFramePng(_3_png),
518                 new VFramePng(_4_png),
519                 new VFramePng(_5_png),
520                 new VFramePng(_6_png),
521                 new VFramePng(_7_png),
522                 new VFramePng(_8_png),
523                 new VFramePng(_9_png),
524                 new VFramePng(colon_png),
525                 new VFramePng(period_png),
526                 new VFramePng(a_png),
527                 new VFramePng(b_png),
528                 new VFramePng(c_png),
529                 new VFramePng(d_png),
530                 new VFramePng(e_png),
531                 new VFramePng(f_png),
532                 new VFramePng(space_png),
533                 new VFramePng(dash_png)
534         };
535
536         generic_button_margin = 15;
537         draw_clock_background=1;
538
539         use_shm = -1;
540         shm_reply = 1;
541
542 // Initialize
543         bg_color = ORANGE;
544         bg_shadow1 = DKGREY;
545         bg_shadow2 = BLACK;
546         bg_light1 = WHITE;
547         bg_light2 = bg_color;
548
549
550         border_light1 = bg_color;
551         border_light2 = MEGREY;
552         border_shadow1 = BLACK;
553         border_shadow2 = bg_color;
554
555         default_text_color = BLACK;
556         disabled_text_color = DMGREY;
557
558         button_light = MEGREY;           // bright corner
559 //      button_highlighted = LTGREY;  // face when highlighted
560         button_highlighted = 0xffe000;  // face when highlighted
561         button_down = MDGREY;         // face when down
562 //      button_up = MEGREY;           // face when up
563         button_up = 0xffc000;           // face when up
564         button_shadow = BLACK;       // dark corner
565         button_uphighlighted = RED;   // upper side when highlighted
566
567         tumble_data = 0;
568         tumble_duration = 150;
569
570         ok_images = default_ok_images;
571         cancel_images = default_cancel_images;
572         usethis_button_images = default_usethis_images;
573         filebox_descend_images = default_ok_images;
574
575         menu_light = LTCYAN;
576         menu_highlighted = LTBLUE;
577         menu_down = MDCYAN;
578         menu_up = MECYAN;
579         menu_shadow = DKCYAN;
580
581
582 #include "images/menuitem_up_png.h"
583 #include "images/menuitem_hi_png.h"
584 #include "images/menuitem_dn_png.h"
585 #include "images/menubar_up_png.h"
586 #include "images/menubar_hi_png.h"
587 #include "images/menubar_dn_png.h"
588 #include "images/menubar_bg_png.h"
589
590         static VFrame *default_menuitem_data[] =
591         {
592                 new VFramePng(menuitem_up_png),
593                 new VFramePng(menuitem_hi_png),
594                 new VFramePng(menuitem_dn_png),
595         };
596         menu_item_bg = default_menuitem_data;
597
598
599         static VFrame *default_menubar_data[] =
600         {
601                 new VFramePng(menubar_up_png),
602                 new VFramePng(menubar_hi_png),
603                 new VFramePng(menubar_dn_png),
604         };
605         menu_title_bg = default_menubar_data;
606
607         menu_popup_bg = new VFramePng(menu_popup_bg_png);
608
609         menu_bar_bg = new VFramePng(menubar_bg_png);
610
611         popupmenu_images = 0;
612
613
614         popupmenu_margin = 10;
615         popupmenu_triangle_margin = 10;
616
617         min_menu_w = 0;
618         menu_title_text = BLACK;
619         popup_title_text = BLACK;
620         menu_item_text = BLACK;
621         menu_highlighted_fontcolor = BLACK;
622         progress_text = BLACK;
623
624
625
626         text_default = BLACK;
627         highlight_inverse = WHITE ^ BLUE;
628         text_background = WHITE;
629         text_background_hi = LTYELLOW;
630         text_background_noborder_hi = LTGREY;
631         text_background_noborder = -1;
632         text_border1 = DKGREY;
633         text_border2 = BLACK;
634         text_border2_hi = RED;
635         text_border3 = MEGREY;
636         text_border3_hi = LTPINK;
637         text_border4 = WHITE;
638         text_highlight = BLUE;
639         text_inactive_highlight = MEGREY;
640
641         toggle_highlight_bg = 0;
642         toggle_text_margin = 0;
643
644 // Delays must all be different for repeaters
645         double_click = 300;
646         blink_rate = 250;
647         scroll_repeat = 150;
648         tooltip_delay = 1000;
649         tooltip_bg_color = YELLOW;
650         tooltips_enabled = 1;
651
652         filebox_margin = 110;
653         dirbox_margin = 90;
654         filebox_mode = LISTBOX_TEXT;
655         sprintf(filebox_filter, "*");
656         filebox_w = 640;
657         filebox_h = 480;
658         filebox_columntype[0] = FILEBOX_NAME;
659         filebox_columntype[1] = FILEBOX_SIZE;
660         filebox_columntype[2] = FILEBOX_DATE;
661         filebox_columntype[3] = FILEBOX_EXTENSION;
662         filebox_columnwidth[0] = 200;
663         filebox_columnwidth[1] = 100;
664         filebox_columnwidth[2] = 100;
665         filebox_columnwidth[3] = 100;
666         dirbox_columntype[0] = FILEBOX_NAME;
667         dirbox_columntype[1] = FILEBOX_DATE;
668         dirbox_columnwidth[0] = 200;
669         dirbox_columnwidth[1] = 100;
670
671         filebox_text_images = default_filebox_text_images;
672         filebox_icons_images = default_filebox_icons_images;
673         filebox_updir_images = default_filebox_updir_images;
674         filebox_newfolder_images = default_filebox_newfolder_images;
675         filebox_rename_images = default_filebox_rename_images;
676         filebox_delete_images = default_filebox_delete_images;
677         filebox_reload_images = default_filebox_reload_images;
678         directory_color = BLUE;
679         file_color = BLACK;
680
681         filebox_sortcolumn = 0;
682         filebox_sortorder = BC_ListBox::SORT_ASCENDING;
683         dirbox_sortcolumn = 0;
684         dirbox_sortorder = BC_ListBox::SORT_ASCENDING;
685
686
687         pot_images = default_pot_images;
688         pot_offset = 2;
689         pot_x1 = pot_images[0]->get_w() / 2 - pot_offset;
690         pot_y1 = pot_images[0]->get_h() / 2 - pot_offset;
691         pot_r = pot_x1;
692         pot_needle_color = BLACK;
693
694         progress_images = default_progress_images;
695
696         xmeter_images = 0;
697         ymeter_images = 0;
698         meter_font = SMALLFONT_3D;
699         meter_font_color = RED;
700         meter_title_w = 20;
701         meter_3d = 1;
702         medium_7segment = default_medium_7segment;
703
704         audiovideo_color = RED;
705
706         use_fontset = 0;
707
708 // Xft has priority over font set
709 #ifdef HAVE_XFT
710 // But Xft dies in 32 bit mode after some amount of drawing.
711         use_xft = 1;
712 #else
713         use_xft = 0;
714 #endif
715
716
717         drag_radius = 10;
718         recursive_resizing = 1;
719
720
721 }
722
723 BC_Resources::~BC_Resources()
724 {
725 }
726
727 int BC_Resources::initialize_display(BC_WindowBase *window)
728 {
729 // Set up IPC cleanup handlers
730 //      bc_init_ipc();
731
732 // Test for shm.  Must come before yuv test
733         init_shm(window);
734         return 0;
735 }
736
737
738 void BC_Resources::init_shm(BC_WindowBase *window)
739 {
740         use_shm = 0;
741         int (*old_handler)(Display *, XErrorEvent *) =
742                 XSetErrorHandler(BC_Resources::x_error_handler);
743
744         if(XShmQueryExtension(window->display))
745         {
746                 XShmSegmentInfo test_shm;
747                 memset(&test_shm,0,sizeof(test_shm));
748                 XImage *test_image = XShmCreateImage(window->display, window->vis,
749                         window->default_depth, ZPixmap, (char*)NULL, &test_shm, 5, 5);
750                 BC_Resources::error = 0;
751                 test_shm.shmid = shmget(IPC_PRIVATE, 5 * test_image->bytes_per_line, (IPC_CREAT | 0600));
752                 if(test_shm.shmid != -1) {
753                         char *data = (char *)shmat(test_shm.shmid, NULL, 0);
754                         if(data != (void *)-1) {
755                                 shmctl(test_shm.shmid, IPC_RMID, 0);
756                                 test_shm.shmaddr = data;
757                                 test_shm.readOnly = 0;
758
759                                 if(XShmAttach(window->display, &test_shm)) {
760                                         XSync(window->display, False);
761                                         use_shm = 1;
762                                 }
763                                 shmdt(data);
764                         }
765                 }
766
767                 XDestroyImage(test_image);
768                 if(BC_Resources::error) use_shm = 0;
769         }
770         XSetErrorHandler(old_handler);
771 }
772
773
774
775
776 BC_Synchronous* BC_Resources::get_synchronous()
777 {
778         return synchronous;
779 }
780
781 void BC_Resources::set_synchronous(BC_Synchronous *synchronous)
782 {
783         this->synchronous = synchronous;
784 }
785
786
787
788
789
790
791
792 int BC_Resources::get_top_border()
793 {
794         return display_info->get_top_border();
795 }
796
797 int BC_Resources::get_left_border()
798 {
799         return display_info->get_left_border();
800 }
801
802 int BC_Resources::get_right_border()
803 {
804         return display_info->get_right_border();
805 }
806
807 int BC_Resources::get_bottom_border()
808 {
809         return display_info->get_bottom_border();
810 }
811
812
813 int BC_Resources::get_bg_color() { return bg_color; }
814
815 int BC_Resources::get_bg_shadow1() { return bg_shadow1; }
816
817 int BC_Resources::get_bg_shadow2() { return bg_shadow2; }
818
819 int BC_Resources::get_bg_light1() { return bg_light1; }
820
821 int BC_Resources::get_bg_light2() { return bg_light2; }
822
823
824 int BC_Resources::get_id()
825 {
826         id_lock->lock("BC_Resources::get_id");
827         int result = id++;
828         id_lock->unlock();
829         return result;
830 }
831
832 int BC_Resources::get_filebox_id()
833 {
834         id_lock->lock("BC_Resources::get_filebox_id");
835         int result = filebox_id++;
836         id_lock->unlock();
837         return result;
838 }
839
840
841 void BC_Resources::set_signals(BC_Signals *signal_handler)
842 {
843         BC_Resources::signal_handler = signal_handler;
844 }
845
846 int BC_Resources::init_fontconfig(const char *search_path)
847 {
848         if( fontlist ) return 0;
849         fontlist = new ArrayList<BC_FontEntry*>;
850
851 #define get_str(str,sep,ptr,cond) do { char *out = str; \
852   while( *ptr && !strchr(sep,*ptr) && (cond) ) *out++ = *ptr++; \
853   *out = 0; \
854 } while(0)
855
856 #define skip_str(sep, ptr) do { \
857   while( *ptr && strchr(sep,*ptr) ) ++ptr; \
858 } while(0)
859
860         char find_command[BCTEXTLEN];
861         sprintf(find_command,
862                 "find %s -name 'fonts.dir' -print -exec cat {} \\;",
863                 search_path);
864         FILE *in = popen(find_command, "r");
865
866         FT_Library freetype_library = 0;
867 //      FT_Face freetype_face = 0;
868 //      FT_Init_FreeType(&freetype_library);
869
870         char line[BCTEXTLEN], current_dir[BCTEXTLEN];
871         current_dir[0] = 0;
872
873         while( !feof(in) && fgets(line, BCTEXTLEN, in) ) {
874                 if(!strlen(line)) break;
875
876                 char *in_ptr = line;
877
878 // Get current directory
879                 if(line[0] == '/') {
880                         get_str(current_dir, "\n", in_ptr,1);
881                         for( int i=strlen(current_dir); --i>=0 && current_dir[i]!='/'; )
882                                 current_dir[i] = 0;
883                         continue;
884                 }
885
886 //printf("TitleMain::build_fonts %s\n", line);
887                 BC_FontEntry *entry = new BC_FontEntry;
888                 char string[BCTEXTLEN];
889 // Path
890                 get_str(string, "\n", in_ptr, in_ptr[0]!=' ' || in_ptr[1]!='-');
891                 entry->path = cstrcat(2, current_dir, string);
892 // Foundary
893                 skip_str(" -", in_ptr);
894                 get_str(string, " -\n", in_ptr, 1);
895                 if( !string[0] ) { delete entry;  continue; }
896                 entry->foundry = cstrdup(string);
897                 if(*in_ptr == '-') in_ptr++;
898 // Family
899                 get_str(string, "-\n", in_ptr, 1);
900                 if( !string[0] ) { delete entry;  continue; }
901                 entry->family = cstrdup(string);
902                 if(*in_ptr == '-') in_ptr++;
903 // Weight
904                 get_str(string, "-\n", in_ptr, 1);
905                 entry->weight = cstrdup(string);
906                 if(*in_ptr == '-') in_ptr++;
907 // Slant
908                 get_str(string, "-\n", in_ptr, 1);
909                 entry->slant = cstrdup(string);
910                 if(*in_ptr == '-') in_ptr++;
911 // SWidth
912                 get_str(string, "-\n", in_ptr, 1);
913                 entry->swidth = cstrdup(string);
914                 if(*in_ptr == '-') in_ptr++;
915 // Adstyle
916                 get_str(string, "-\n", in_ptr, 1);
917                 entry->adstyle = cstrdup(string);
918                 if(*in_ptr == '-') in_ptr++;
919 // pixelsize
920                 get_str(string, "-\n", in_ptr, 1);
921                 entry->pixelsize = atol(string);
922                 if(*in_ptr == '-') in_ptr++;
923 // pointsize
924                 get_str(string, "-\n", in_ptr, 1);
925                 entry->pointsize = atol(string);
926                 if(*in_ptr == '-') in_ptr++;
927 // xres
928                 get_str(string, "-\n", in_ptr, 1);
929                 entry->xres = atol(string);
930                 if(*in_ptr == '-') in_ptr++;
931 // yres
932                 get_str(string, "-\n", in_ptr, 1);
933                 entry->yres = atol(string);
934                 if(*in_ptr == '-') in_ptr++;
935 // spacing
936                 get_str(string, "-\n", in_ptr, 1);
937                 entry->spacing = cstrdup(string);
938                 if(*in_ptr == '-') in_ptr++;
939 // avg_width
940                 get_str(string, "-\n", in_ptr, 1);
941                 entry->avg_width = atol(string);
942                 if(*in_ptr == '-') in_ptr++;
943 // registry
944                 get_str(string, "-\n", in_ptr, 1);
945                 entry->registry = cstrdup(string);
946                 if(*in_ptr == '-') in_ptr++;
947 // encoding
948                 get_str(string, "-\n", in_ptr, 1);
949                 entry->encoding = cstrdup(string);
950                 if(*in_ptr == '-') in_ptr++;
951
952 // Add to list
953 //printf("TitleMain::build_fonts 1 %s\n", entry->path);
954 // This takes a real long time to do.  Instead just take all fonts
955 //              if(!load_freetype_face(freetype_library,
956 //                      freetype_face, entry->path) )
957 // Fix parameters
958                 sprintf(line, "%s (%s)", entry->family, entry->foundry);
959                 entry->displayname = cstrdup(line);
960
961                 if(!strcasecmp(entry->weight, "demibold")) {
962                         entry->fixed_style |= BC_FONT_BOLD;
963                         entry->style |= FL_WEIGHT_DEMIBOLD;
964                 }
965                 else if(!strcasecmp(entry->weight, "bold")) {
966                         entry->fixed_style |= BC_FONT_BOLD;
967                         entry->style |= FL_WEIGHT_BOLD;
968                 }
969                 else {
970                         entry->style |= FL_WEIGHT_NORMAL;
971                 }
972
973                 if(!strcasecmp(entry->slant, "r")) {
974                         entry->style |= FL_SLANT_ROMAN;
975                 }
976                 else if(!strcasecmp(entry->slant, "i")) {
977                         entry->style |= FL_SLANT_ITALIC;
978                         entry->fixed_style |= BC_FONT_ITALIC;
979                 }
980                 else if(!strcasecmp(entry->slant, "o")) {
981                         entry->style |= FL_SLANT_OBLIQUE;
982                         entry->fixed_style |= BC_FONT_ITALIC;
983                 }
984
985                 if(!strcasecmp(entry->swidth, "normal"))
986                         entry->style = FL_WIDTH_NORMAL;
987                 else if(!strcasecmp(entry->swidth, "ultracondensed"))
988                         entry->style = FL_WIDTH_ULTRACONDENSED;
989                 else if(!strcasecmp(entry->swidth, "extracondensed"))
990                         entry->style = FL_WIDTH_EXTRACONDENSED;
991                 else if(!strcasecmp(entry->swidth, "condensed"))
992                         entry->style = FL_WIDTH_CONDENSED;
993                 else if(!strcasecmp(entry->swidth, "semicondensed"))
994                         entry->style = FL_WIDTH_SEMICONDENSED;
995                 else if(!strcasecmp(entry->swidth, "semiexpanded"))
996                         entry->style = FL_WIDTH_SEMIEXPANDED;
997                 else if(!strcasecmp(entry->swidth, "expanded"))
998                         entry->style = FL_WIDTH_EXPANDED;
999                 else if(!strcasecmp(entry->swidth, "extraexpanded"))
1000                         entry->style = FL_WIDTH_EXTRAEXPANDED;
1001                 else if(!strcasecmp(entry->swidth, "ultraexpanded"))
1002                         entry->style = FL_WIDTH_ULTRAEXPANDED;
1003                 else
1004                         entry->style = FL_WIDTH_NORMAL;
1005
1006                 fontlist->append(entry);
1007 //              printf("TitleMain::build_fonts %s: success\n",  entry->path);
1008 //printf("TitleMain::build_fonts 2\n");
1009         }
1010         pclose(in);
1011
1012
1013 // Load all the fonts from fontconfig
1014         FcPattern *pat;
1015         FcFontSet *fs;
1016         FcObjectSet *os;
1017         FcChar8 *family, *file, *foundry, *style, *format;
1018         int slant, spacing, width, weight;
1019         int force_style = 0;
1020 // if you want limit search to TrueType put 1
1021         int limit_to_trutype = 1;
1022         FcConfig *config;
1023         int i;
1024         char tmpstring[BCTEXTLEN];
1025         if(!FcInit())
1026                 return 1;
1027         config = FcConfigGetCurrent();
1028         FcConfigSetRescanInterval(config, 0);
1029
1030         pat = FcPatternCreate();
1031         os = FcObjectSetBuild ( FC_FAMILY, FC_FILE, FC_FOUNDRY, FC_WEIGHT,
1032                 FC_WIDTH, FC_SLANT, FC_FONTFORMAT, FC_SPACING, FC_STYLE, (char *) 0);
1033         FcPatternAddBool(pat, FC_SCALABLE, true);
1034
1035         if(language[0]) {
1036                 char langstr[LEN_LANG * 3];
1037                 strcpy(langstr, language);
1038
1039                 if(region[0]) {
1040                         strcat(langstr, "-");
1041                         strcat(langstr, region);
1042                 }
1043
1044                 FcLangSet *ls =  FcLangSetCreate();
1045                 if(FcLangSetAdd(ls, (const FcChar8*)langstr))
1046                 if(FcPatternAddLangSet(pat, FC_LANG, ls))
1047                 FcLangSetDestroy(ls);
1048         }
1049
1050         fs = FcFontList(config, pat, os);
1051         FcPatternDestroy(pat);
1052         FcObjectSetDestroy(os);
1053
1054         for (i = 0; fs && i < fs->nfont; i++) {
1055                 FcPattern *font = fs->fonts[i];
1056                 force_style = 0;
1057                 FcPatternGetString(font, FC_FONTFORMAT, 0, &format);
1058                 //on this point you can limit font search
1059                 if(limit_to_trutype && strcmp((char *)format, "TrueType"))
1060                         continue;
1061
1062                 sprintf(tmpstring, "%s", format);
1063                 BC_FontEntry *entry = new BC_FontEntry;
1064                 if(FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
1065                         entry->path = cstrdup((char*)file);
1066                 }
1067
1068                 if(FcPatternGetString(font, FC_FOUNDRY, 0, &foundry) == FcResultMatch) {
1069                         entry->foundry = cstrdup((char*)foundry);
1070                 }
1071
1072                 if(FcPatternGetInteger(font, FC_WEIGHT, 0, &weight) == FcResultMatch) {
1073                         switch(weight) {
1074                         case FC_WEIGHT_THIN:
1075                         case FC_WEIGHT_EXTRALIGHT:
1076                         case FC_WEIGHT_LIGHT:
1077                         case FC_WEIGHT_BOOK:
1078                                 force_style = 1;
1079                                 entry->weight = cstrdup("medium");
1080                                 break;
1081
1082                         case FC_WEIGHT_NORMAL:
1083                         case FC_WEIGHT_MEDIUM:
1084                         default:
1085                                 entry->weight = cstrdup("medium");
1086                                 break;
1087
1088                         case FC_WEIGHT_BLACK:
1089                         case FC_WEIGHT_SEMIBOLD:
1090                         case FC_WEIGHT_BOLD:
1091                                 entry->weight = cstrdup("bold");
1092                                 entry->fixed_style |= BC_FONT_BOLD;
1093                                 break;
1094
1095                         case FC_WEIGHT_EXTRABOLD:
1096                         case FC_WEIGHT_EXTRABLACK:
1097                                 force_style = 1;
1098                                 entry->weight = cstrdup("bold");
1099                                 entry->fixed_style |= BC_FONT_BOLD;
1100                                 break;
1101                         }
1102                 }
1103
1104                 if(FcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch)
1105                         entry->family = cstrdup((char*)family);
1106
1107                 if(FcPatternGetInteger(font, FC_SLANT, 0, &slant) == FcResultMatch) {
1108                         switch(slant) {
1109                         case FC_SLANT_ROMAN:
1110                         default:
1111                                 entry->slant = cstrdup("r");
1112                                 entry->style |= FL_SLANT_ROMAN;
1113                                 break;
1114                         case FC_SLANT_ITALIC:
1115                                 entry->slant = cstrdup("i");
1116                                 entry->style |= FL_SLANT_ITALIC;
1117                                 entry->fixed_style |= BC_FONT_ITALIC;
1118                                 break;
1119                         case FC_SLANT_OBLIQUE:
1120                                 entry->slant = cstrdup("o");
1121                                 entry->style |= FL_SLANT_OBLIQUE;
1122                                 entry->fixed_style |= BC_FONT_ITALIC;
1123                                 break;
1124                         }
1125                 }
1126
1127                 if(FcPatternGetInteger(font, FC_WIDTH, 0, &width) == FcResultMatch) {
1128                         switch(width) {
1129                         case FC_WIDTH_ULTRACONDENSED:
1130                                 entry->swidth = cstrdup("ultracondensed");
1131                                 break;
1132
1133                         case FC_WIDTH_EXTRACONDENSED:
1134                                 entry->swidth = cstrdup("extracondensed");
1135                                 break;
1136
1137                         case FC_WIDTH_CONDENSED:
1138                                 entry->swidth = cstrdup("condensed");
1139                                 break;
1140                         case FC_WIDTH_SEMICONDENSED:
1141                                 entry->swidth = cstrdup("semicondensed");
1142                                 break;
1143
1144                         case FC_WIDTH_NORMAL:
1145                         default:
1146                                 entry->swidth = cstrdup("normal");
1147                                 break;
1148
1149                         case FC_WIDTH_SEMIEXPANDED:
1150                                 entry->swidth = cstrdup("semiexpanded");
1151                                 break;
1152
1153                         case FC_WIDTH_EXPANDED:
1154                                 entry->swidth = cstrdup("expanded");
1155                                 break;
1156
1157                         case FC_WIDTH_EXTRAEXPANDED:
1158                                 entry->swidth = cstrdup("extraexpanded");
1159                                 break;
1160
1161                         case FC_WIDTH_ULTRAEXPANDED:
1162                                 entry->swidth = cstrdup("ultraexpanded");
1163                                 break;
1164                         }
1165                 }
1166
1167                 if(FcPatternGetInteger(font, FC_SPACING, 0, &spacing) == FcResultMatch) {
1168                         switch(spacing) {
1169                         case 0:
1170                         default:
1171                                 entry->spacing = cstrdup("p");
1172                                 break;
1173
1174                         case 90:
1175                                 entry->spacing = cstrdup("d");
1176                                 break;
1177
1178                         case 100:
1179                                 entry->spacing = cstrdup("m");
1180                                 break;
1181
1182                         case 110:
1183                                 entry->spacing = cstrdup("c");
1184                                 break;
1185                         }
1186                 }
1187
1188                 // Add fake stuff for compatibility
1189                 entry->adstyle = cstrdup(" ");
1190                 entry->pixelsize = 0;
1191                 entry->pointsize = 0;
1192                 entry->xres = 0;
1193                 entry->yres = 0;
1194                 entry->avg_width = 0;
1195                 entry->registry = cstrdup("utf");
1196                 entry->encoding = cstrdup("8");
1197
1198                 if(!FcPatternGetString(font, FC_STYLE, 0, &style) == FcResultMatch)
1199                         force_style = 0;
1200
1201                 // If font has a style unmanaged by titler plugin, force style to be displayed on name
1202                 // in this way we can shown all available fonts styles.
1203                 if(force_style) {
1204                         sprintf(tmpstring, "%s (%s)", entry->family, style);
1205                         entry->displayname = cstrdup(tmpstring);
1206                 }
1207                 else {
1208                         if(strcmp(entry->foundry, "unknown")) {
1209                                 sprintf(tmpstring, "%s (%s)", entry->family, entry->foundry);
1210                                 entry->displayname = cstrdup(tmpstring);
1211                         }
1212                         else {
1213                                 sprintf(tmpstring, "%s", entry->family);
1214                                 entry->displayname = cstrdup(tmpstring);
1215                         }
1216
1217                 }
1218                 fontlist->append(entry);
1219         }
1220
1221         FcFontSetDestroy(fs);
1222         if(freetype_library)
1223                 FT_Done_FreeType(freetype_library);
1224 // for(int i = 0; i < fonts->total; i++)
1225 //      fonts->values[i]->dump();
1226
1227         FcConfigAppFontAddDir(0, (const FcChar8*)search_path);
1228         FcConfigSetRescanInterval(0, 0);
1229
1230         os = FcObjectSetBuild(FC_FAMILY, FC_FILE, FC_FOUNDRY, FC_WEIGHT,
1231                 FC_WIDTH, FC_SLANT, FC_SPACING, FC_STYLE, (char *)0);
1232         pat = FcPatternCreate();
1233         FcPatternAddBool(pat, FC_SCALABLE, true);
1234
1235         if(language[0])
1236         {
1237                 char langstr[LEN_LANG * 3];
1238                 strcpy(langstr, language);
1239
1240                 if(region[0])
1241                 {
1242                         strcat(langstr, "-");
1243                         strcat(langstr, region);
1244                 }
1245
1246                 FcLangSet *ls =  FcLangSetCreate();
1247                 if(FcLangSetAdd(ls, (const FcChar8*)langstr))
1248                         if(FcPatternAddLangSet(pat, FC_LANG, ls))
1249                 FcLangSetDestroy(ls);
1250         }
1251
1252         fs = FcFontList(0, pat, os);
1253         FcPatternDestroy(pat);
1254         FcObjectSetDestroy(os);
1255
1256         for(int i = 0; i < fs->nfont; i++)
1257         {
1258                 FcPattern *font = fs->fonts[i];
1259                 BC_FontEntry *entry = new BC_FontEntry;
1260
1261                 FcChar8 *strvalue;
1262                 if(FcPatternGetString(font, FC_FILE, 0, &strvalue) == FcResultMatch)
1263                 {
1264                         entry->path = new char[strlen((char*)strvalue) + 1];
1265                         strcpy(entry->path, (char*)strvalue);
1266                 }
1267
1268                 if(FcPatternGetString(font, FC_FOUNDRY, 0, &strvalue) == FcResultMatch)
1269                 {
1270                         entry->foundry = new char[strlen((char*)strvalue) + 1];
1271                         strcpy(entry->foundry, (char *)strvalue);
1272                 }
1273
1274                 if(FcPatternGetString(font, FC_FAMILY, 0, &strvalue) == FcResultMatch)
1275                 {
1276                         entry->family = new char[strlen((char*)strvalue) + 2];
1277                         strcpy(entry->family, (char*)strvalue);
1278                 }
1279
1280                 int intvalue;
1281                 if(FcPatternGetInteger(font, FC_SLANT, 0, &intvalue) == FcResultMatch)
1282                 {
1283                         switch(intvalue)
1284                         {
1285                         case FC_SLANT_ROMAN:
1286                         default:
1287                                 entry->style |= FL_SLANT_ROMAN;
1288                                 break;
1289
1290                         case FC_SLANT_ITALIC:
1291                                 entry->style |= FL_SLANT_ITALIC;
1292                                 break;
1293
1294                         case FC_SLANT_OBLIQUE:
1295                                 entry->style |= FL_SLANT_OBLIQUE;
1296                                 break;
1297                         }
1298                 }
1299
1300                 if(FcPatternGetInteger(font, FC_WEIGHT, 0, &intvalue) == FcResultMatch)
1301                 {
1302                         switch(intvalue)
1303                         {
1304                         case FC_WEIGHT_THIN:
1305                                 entry->style |= FL_WEIGHT_THIN;
1306                                 break;
1307
1308                         case FC_WEIGHT_EXTRALIGHT:
1309                                 entry->style |= FL_WEIGHT_EXTRALIGHT;
1310                                 break;
1311
1312                         case FC_WEIGHT_LIGHT:
1313                                 entry->style |= FL_WEIGHT_LIGHT;
1314                                 break;
1315
1316                         case FC_WEIGHT_BOOK:
1317                                 entry->style |= FL_WEIGHT_BOOK;
1318                                 break;
1319
1320                         case FC_WEIGHT_NORMAL:
1321                         default:
1322                                 entry->style |= FL_WEIGHT_NORMAL;
1323                                 break;
1324
1325                         case FC_WEIGHT_MEDIUM:
1326                                 entry->style |= FL_WEIGHT_MEDIUM;
1327                                 break;
1328
1329                         case FC_WEIGHT_DEMIBOLD:
1330                                 entry->style |= FL_WEIGHT_DEMIBOLD;
1331                                 break;
1332
1333                         case FC_WEIGHT_BOLD:
1334                                 entry->style |= FL_WEIGHT_BOLD;
1335                                 break;
1336
1337                         case FC_WEIGHT_EXTRABOLD:
1338                                 entry->style |= FL_WEIGHT_EXTRABOLD;
1339                                 break;
1340
1341                         case FC_WEIGHT_BLACK:
1342                                 entry->style |= FL_WEIGHT_BLACK;
1343                                 break;
1344
1345                         case FC_WEIGHT_EXTRABLACK:
1346                                 entry->style |= FL_WEIGHT_EXTRABLACK;
1347                                 break;
1348                         }
1349                 }
1350
1351                 if(FcPatternGetInteger(font, FC_WIDTH, 0, &intvalue) == FcResultMatch)
1352                 {
1353                         switch(intvalue)
1354                         {
1355                         case FC_WIDTH_ULTRACONDENSED:
1356                                 entry->style |= FL_WIDTH_ULTRACONDENSED;
1357                                 break;
1358
1359                         case FC_WIDTH_EXTRACONDENSED:
1360                                 entry->style |= FL_WIDTH_EXTRACONDENSED;
1361                                 break;
1362
1363                         case FC_WIDTH_CONDENSED:
1364                                 entry->style |= FL_WIDTH_CONDENSED;
1365                                 break;
1366
1367                         case FC_WIDTH_SEMICONDENSED:
1368                                 entry->style = FL_WIDTH_SEMICONDENSED;
1369                                 break;
1370
1371                         case FC_WIDTH_NORMAL:
1372                         default:
1373                                 entry->style |= FL_WIDTH_NORMAL;
1374                                 break;
1375
1376                         case FC_WIDTH_SEMIEXPANDED:
1377                                 entry->style |= FL_WIDTH_SEMIEXPANDED;
1378                                 break;
1379
1380                         case FC_WIDTH_EXPANDED:
1381                                 entry->style |= FL_WIDTH_EXPANDED;
1382                                 break;
1383
1384                         case FC_WIDTH_EXTRAEXPANDED:
1385                                 entry->style |= FL_WIDTH_EXTRAEXPANDED;
1386                                 break;
1387
1388                         case FC_WIDTH_ULTRAEXPANDED:
1389                                 entry->style |= FL_WIDTH_ULTRAEXPANDED;
1390                                 break;
1391                         }
1392                 }
1393                 if(FcPatternGetInteger(font, FC_SPACING, 0, &intvalue) == FcResultMatch)
1394                 {
1395                         switch(intvalue)
1396                         {
1397                         case FC_PROPORTIONAL:
1398                         default:
1399                                 entry->style |= FL_PROPORTIONAL;
1400                                 break;
1401
1402                         case FC_DUAL:
1403                                 entry->style |= FL_DUAL;
1404                                 break;
1405
1406                         case FC_MONO:
1407                                 entry->style |= FL_MONO;
1408                                 break;
1409
1410                         case FC_CHARCELL:
1411                                 entry->style |= FL_CHARCELL;
1412                                 break;
1413                         }
1414                 }
1415                 if(entry->foundry && strcmp(entry->foundry, "unknown"))
1416                 {
1417                         char tempstr[BCTEXTLEN];
1418                         sprintf(tempstr, "%s (%s)", entry->family, entry->foundry);
1419                         entry->displayname = new char[strlen(tempstr) + 1];
1420                         strcpy(entry->displayname, tempstr);
1421                 }
1422                 else
1423                 {
1424                         entry->displayname = new char[strlen(entry->family) + 1];
1425                         strcpy(entry->displayname, entry->family);
1426                 }
1427                 fontlist->append(entry);
1428         }
1429         FcFontSetDestroy(fs);
1430         return 0;
1431 }
1432
1433 BC_FontEntry *BC_Resources::find_fontentry(const char *displayname, int style, int mask)
1434 {
1435         BC_FontEntry *entry, *style_match, *displayname_match;
1436
1437         if(!fontlist)
1438                 return 0;
1439
1440         if(displayname)
1441         {
1442                 for(int i = 0; i < fontlist->total; i++)
1443                 {
1444                         entry = fontlist->values[i];
1445
1446                         if(strcmp(entry->displayname, displayname) == 0 &&
1447                                         (entry->style & mask) == style)
1448                                 return entry;
1449                 }
1450         }
1451 // No exact match - assume normal width font
1452         style |= FL_WIDTH_NORMAL;
1453         mask |= FL_WIDTH_MASK;
1454         style_match = 0;  displayname_match = 0;
1455         for(int i = 0; i < fontlist->total; i++)
1456         {
1457                 entry = fontlist->values[i];
1458
1459                 if(!strncasecmp(displayname, entry->family,
1460                                 strlen(entry->family)))
1461                 {
1462                         if((entry->style & mask) == style)
1463                                 return entry;
1464                         if(!displayname_match)
1465                                 displayname_match = entry;
1466                 }
1467
1468                 if((entry->style & mask) == style)
1469                 {
1470                         if(!style_match)
1471                                 style_match = entry;
1472                 }
1473         }
1474         return displayname_match ? displayname_match : style_match;
1475 }
1476
1477 size_t BC_Resources::encode(const char *from_enc, const char *to_enc,
1478         char *input, int input_length, char *output, int output_length)
1479 {
1480         size_t inbytes, outbytes = 0;
1481         iconv_t cd;
1482         char *outbase = output;
1483
1484         if(!from_enc || *from_enc == 0)
1485                 from_enc = "UTF-8";
1486
1487         if(!to_enc || *to_enc == 0)
1488                 to_enc = "UTF-8";
1489
1490         if(input_length < 0)
1491                 inbytes = strlen(input);
1492         else
1493                 inbytes = input_length;
1494
1495         if(strcmp(from_enc, to_enc) && inbytes)
1496         {
1497                 if((cd = iconv_open(to_enc, from_enc)) == (iconv_t)-1)
1498                 {
1499                         printf(_("Conversion from %s to %s is not available"),
1500                                 from_enc, to_enc);
1501                         return 0;
1502                 }
1503
1504                 outbytes = output_length - 1;
1505
1506                 iconv(cd, &input, &inbytes, &output, &outbytes);
1507
1508                 iconv_close(cd);
1509                 inbytes = output - outbase;
1510         }
1511         else if(inbytes)
1512         {
1513                 memcpy(output,  input, inbytes);
1514                 outbytes -= inbytes;
1515         }
1516         for(int i = 0; i < 4; i++)
1517         {
1518                 output[i] = 0;
1519                 if(outbytes-- == 0)
1520                         break;
1521         }
1522         return inbytes;
1523 }
1524
1525 int BC_Resources::find_font_by_char(FT_ULong char_code, char *path_new, const FT_Face oldface)
1526 {
1527         FcPattern *font, *ofont;
1528         FcChar8 *file;
1529         int result = 0;
1530
1531         *path_new = 0;
1532
1533         // Do not search control codes
1534         if(char_code < ' ')
1535                 return 0;
1536
1537         if( (ofont = FcFreeTypeQueryFace(oldface, (const FcChar8*)"", 4097, 0)) != 0 ) {
1538                 if( (font = find_similar_font(char_code, ofont)) != 0 ) {
1539                         if(FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
1540                                 strcpy(path_new, (char*)file);
1541                                 result = 1;
1542                         }
1543                         FcPatternDestroy(font);
1544                 }
1545                 FcPatternDestroy(ofont);
1546         }
1547         return result;
1548 }
1549
1550 FcPattern* BC_Resources::find_similar_font(FT_ULong char_code, FcPattern *oldfont)
1551 {
1552         FcPattern *pat, *font;
1553         FcFontSet *fs;
1554         FcObjectSet *os;
1555         FcCharSet *fcs;
1556         int ival;
1557
1558         // Do not search control codes
1559         if(char_code < ' ')
1560                 return 0;
1561
1562         fontconfig_lock.lock("BC_Resources::find_similar_font");
1563         pat = FcPatternCreate();
1564         os = FcObjectSetBuild(FC_FILE, FC_CHARSET, FC_SCALABLE, FC_FAMILY,
1565                 FC_SLANT, FC_WEIGHT, FC_WIDTH, (char *)0);
1566
1567         FcPatternAddBool(pat, FC_SCALABLE, true);
1568         fcs = FcCharSetCreate();
1569         if(FcCharSetAddChar(fcs, char_code))
1570                 FcPatternAddCharSet(pat, FC_CHARSET, fcs);
1571         FcCharSetDestroy(fcs);
1572         for( int i=0; i<(int)LEN_FCPROP; ++i ) {
1573                 if(FcPatternGetInteger(oldfont, fc_properties[i], 0, &ival) == FcResultMatch)
1574                         FcPatternAddInteger(pat, fc_properties[i], ival);
1575         }
1576         fs = FcFontList(0, pat, os);
1577         for( int i=LEN_FCPROP; --i>=0 && !fs->nfont; ) {
1578                 FcFontSetDestroy(fs);
1579                 FcPatternDel(pat, fc_properties[i]);
1580                 fs = FcFontList(0, pat, os);
1581         }
1582         FcPatternDestroy(pat);
1583         FcObjectSetDestroy(os);
1584
1585         pat = 0;
1586
1587         for (int i = 0; i < fs->nfont; i++)
1588         {
1589                 font = fs->fonts[i];
1590                 if(FcPatternGetCharSet(font, FC_CHARSET, 0, &fcs) == FcResultMatch)
1591                 {
1592                         if(FcCharSetHasChar(fcs, char_code))
1593                         {
1594                                 pat =  FcPatternDuplicate(font);
1595                                 break;
1596                         }
1597                 }
1598         }
1599         FcFontSetDestroy(fs);
1600         fontconfig_lock.unlock();
1601
1602         return pat;
1603 }
1604
1605 void BC_Resources::dump_fonts(FILE *fp)
1606 {
1607         for( int i=0; i<fontlist->total; ++i ) {
1608                 BC_FontEntry *ep = fontlist->values[i];
1609                 fprintf(fp,"%s = %s\n",ep->displayname,ep->path);
1610                 fprintf(fp,"  %s:%s:%s:%s:%s:%s:%d:%d:%d:%d:%d:%s:%d:%s:%s:%d\n",
1611                         ep->foundry, ep->family, ep->weight, ep->slant, ep->swidth, ep->adstyle,
1612                         ep->pixelsize, ep->pointsize, ep->xres, ep->yres, ep->style, ep->spacing,
1613                         ep->avg_width, ep->registry, ep->encoding, ep->fixed_style);
1614         }
1615 }
1616