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