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