fixes and upgrades to guicast
[goodguy/history.git] / cinelerra-5.0 / guicast / bcwindowbase.C
index d74cce3753ae6d1fe0827014576b46804862321b..2d43b62bfd6544ddb40cce46f6a2d9c8e4899629 100644 (file)
@@ -136,17 +136,9 @@ BC_WindowBase::~BC_WindowBase()
        delete pixmap;
 
 #ifdef HAVE_GL
-       if( get_resources()->get_synchronous() && top_level->options & GLX_WINDOW ) {
-               if( !glx_win ) {
-// NVIDIA library threading problem, XCloseDisplay SEGVs without this
-                       sync_lock("BC_WindowBase::~BC_WindowBase:XDestroyWindow");
-                       glXMakeContextCurrent(top_level->display, 0, 0, 0);
-                       XDestroyWindow(top_level->display, win);
-                       sync_unlock();
-               }
-               else
-                       get_resources()->get_synchronous()->delete_window(this);
-       }
+       if( get_resources()->get_synchronous() &&
+               (top_level->options & GLX_WINDOW) && glx_win != 0 )
+               get_resources()->get_synchronous()->delete_window(this);
        else
 #endif
                XDestroyWindow(top_level->display, win);
@@ -168,20 +160,31 @@ BC_WindowBase::~BC_WindowBase()
        if(window_type == MAIN_WINDOW)
        {
                XFreeGC(display, gc);
-               XFreeFont(display, smallfont);
-               XFreeFont(display, mediumfont);
-               XFreeFont(display, largefont);
-               XFreeFont(display, bigfont);
-
-#ifdef HAVE_XFT
-               if(bigfont_xft)
-                       XftFontClose (display, (XftFont*)bigfont_xft);
-               if(largefont_xft)
-                       XftFontClose (display, (XftFont*)largefont_xft);
-               if(mediumfont_xft)
-                       XftFontClose (display, (XftFont*)mediumfont_xft);
-               if(smallfont_xft)
-                       XftFontClose (display, (XftFont*)smallfont_xft);
+               static XFontStruct *BC_WindowBase::*xfont[] = {
+                        &BC_WindowBase::smallfont,
+                        &BC_WindowBase::mediumfont,
+                        &BC_WindowBase::largefont,
+                        &BC_WindowBase::bigfont,
+               };
+               for( int i=sizeof(xfont)/sizeof(xfont[0]); --i>=0; )
+                       XFreeFont(display, this->*xfont[i]);
+
+// bug in X causes XRenderExtensionInfo to be damaged if this is done here
+//  left to be done in XCloseDisplay by Xlib.
+#if defined(HAVE_XFT) && 0
+               static void *BC_WindowBase::*xft_font[] = {
+                        &BC_WindowBase::smallfont_xft,
+                        &BC_WindowBase::mediumfont_xft,
+                        &BC_WindowBase::largefont_xft,
+                        &BC_WindowBase::bigfont_xft,
+                        &BC_WindowBase::bold_smallfont_xft,
+                        &BC_WindowBase::bold_mediumfont_xft,
+                        &BC_WindowBase::bold_largefont_xft,
+               };
+               for( int i=sizeof(xft_font)/sizeof(xft_font[0]); --i>=0; ) {
+                       XftFont *xft = (XftFont *)(this->*xft_font[i]);
+                       if( xft ) XftFontClose (display, xft);
+               }
 #endif
                finit_im();
 
@@ -200,8 +203,8 @@ BC_WindowBase::~BC_WindowBase()
 #ifndef SINGLE_THREAD
 #ifdef HAVE_GL
                if( (options & GLX_DISPLAY) != 0 && get_resources()->get_synchronous() ) {
-                       printf("BC_WindowBase::~BC_WindowBase window deleted but opengl deletion is not\n"
-                               "implemented for BC_Pixmap.\n");
+                       printf(_("BC_WindowBase::~BC_WindowBase window deleted but opengl deletion is not\n"
+                               "implemented for BC_Pixmap.\n"));
                        get_resources()->get_synchronous()->delete_display(this);
                }
                else
@@ -406,7 +409,7 @@ int BC_WindowBase::create_window(BC_WindowBase *parent_window,
        else
                this->display_name[0] = 0;
 
-       strcpy(this->title, _(title));
+       put_title(_(title));
        if(bg_pixmap) shared_bg_pixmap = 1;
 
        subwindows = new BC_SubWindowList;
@@ -457,8 +460,8 @@ int BC_WindowBase::create_window(BC_WindowBase *parent_window,
                init_colors();
 // get the resources
                if(resources.use_shm < 0) resources.initialize_display(this);
-               x_correction = get_resources()->get_left_border();
-               y_correction = get_resources()->get_top_border();
+               x_correction = BC_DisplayInfo::get_left_border();
+               y_correction = BC_DisplayInfo::get_top_border();
 
 // clamp window placement
                if(this->x + this->w + x_correction > root_w)
@@ -655,7 +658,7 @@ Display* BC_WindowBase::init_display(const char *display_name)
                printf("BC_WindowBase::init_display: cannot connect to X server %s\n",
                        display_name);
                if(getenv("DISPLAY") == NULL) {
-                       printf("'DISPLAY' environment variable not set.\n");
+                       printf(_("'DISPLAY' environment variable not set.\n"));
                        exit(1);
                }
                else {
@@ -859,20 +862,25 @@ int BC_WindowBase::keysym_lookup(XEvent *event)
                keys_return[0] = keysym;
                return 0;
        }
+       wkey_string_length = 0;
 
-       wkey_string_length = XwcLookupString(input_context,
-               (XKeyEvent*)event, wkey_string, 4, &keysym, 0);
+       if( input_context ) {
+               wkey_string_length = XwcLookupString(input_context,
+                       (XKeyEvent*)event, wkey_string, 4, &keysym, 0);
 //printf("keysym_lookup 1 %d %d %lx %x %x %x %x\n", wkey_string_length, keysym,
 //  wkey_string[0], wkey_string[1], wkey_string[2], wkey_string[3]);
 
-       Status stat;
-       int ret = Xutf8LookupString(input_context, (XKeyEvent*)event,
-                       keys_return, KEYPRESSLEN, &keysym, &stat);
+               Status stat;
+               int ret = Xutf8LookupString(input_context, (XKeyEvent*)event,
+                               keys_return, KEYPRESSLEN, &keysym, &stat);
 //printf("keysym_lookup 2 %d %d %lx %x %x\n", ret, stat, keysym, keys_return[0], keys_return[1]);
-       if( stat == XLookupBoth ) return ret;
-       if( stat == XLookupKeySym ) return 0;
-
-       return XLookupString((XKeyEvent*)event, keys_return, KEYPRESSLEN, &keysym, 0);
+               if( stat == XLookupBoth ) return ret;
+               if( stat == XLookupKeySym ) return 0;
+       }
+       int ret = XLookupString((XKeyEvent*)event, keys_return, KEYPRESSLEN, &keysym, 0);
+       wkey_string_length = ret;
+       for( int i=0; i<ret; ++i ) wkey_string[i] = keys_return[i];
+       return ret;
 }
 
 pthread_t locking_task = -1;
@@ -2253,6 +2261,7 @@ int BC_WindowBase::init_fonts()
 void BC_WindowBase::init_xft()
 {
 #ifdef HAVE_XFT
+       if( !get_resources()->use_xft ) return;
        if(!(smallfont_xft =
                (resources.small_font_xft[0] == '-' ?
                        XftFontOpenXlfd(display, screen, resources.small_font_xft) :
@@ -2270,7 +2279,7 @@ void BC_WindowBase::init_xft()
        if(!(largefont_xft =
                (resources.large_font_xft[0] == '-' ?
                        XftFontOpenXlfd(display, screen, resources.large_font_xft) :
-                       XftFontOpenXlfd(display, screen, resources.large_font_xft))) )
+                       XftFontOpenName(display, screen, resources.large_font_xft))) )
                if(!(largefont_xft =
                        XftFontOpenXlfd(display, screen, resources.large_font_xft2)))
                        largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
@@ -2282,13 +2291,34 @@ void BC_WindowBase::init_xft()
                        XftFontOpenXlfd(display, screen, resources.big_font_xft2)))
                        bigfont_xft = XftFontOpenXlfd(display, screen, "fixed");
 
+       if(!(bold_smallfont_xft =
+               (resources.small_b_font_xft[0] == '-' ?
+                       XftFontOpenXlfd(display, screen, resources.small_b_font_xft) :
+                       XftFontOpenName(display, screen, resources.small_b_font_xft))) )
+               bold_smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
+       if(!(bold_mediumfont_xft =
+               (resources.medium_b_font_xft[0] == '-' ?
+                       XftFontOpenXlfd(display, screen, resources.medium_b_font_xft) :
+                       XftFontOpenName(display, screen, resources.medium_b_font_xft))) )
+               bold_mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
+       if(!(bold_largefont_xft =
+               (resources.large_b_font_xft[0] == '-' ?
+                       XftFontOpenXlfd(display, screen, resources.large_b_font_xft) :
+                       XftFontOpenName(display, screen, resources.large_b_font_xft))) )
+               bold_largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
+
 // Extension failed to locate fonts
-       if( !smallfont_xft || !mediumfont_xft || !largefont_xft || !bigfont_xft) {
-               printf("BC_WindowBase::init_fonts: no xft fonts found %s=%p %s=%p %s=%p %s=%p\n",
+       if( !smallfont_xft || !mediumfont_xft || !largefont_xft || !bigfont_xft ||
+           !bold_largefont_xft || !bold_mediumfont_xft || !bold_largefont_xft ) {
+               printf("BC_WindowBase::init_fonts: no xft fonts found:"
+                       " %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n",
                        resources.small_font_xft, smallfont_xft,
                        resources.medium_font_xft, mediumfont_xft,
                        resources.large_font_xft, largefont_xft,
-                       resources.big_font_xft, bigfont_xft);
+                       resources.big_font_xft, bigfont_xft,
+                       resources.small_b_font_xft, bold_smallfont_xft,
+                       resources.medium_b_font_xft, bold_mediumfont_xft,
+                       resources.large_b_font_xft, bold_largefont_xft);
                get_resources()->use_xft = 0;
                exit(1);
        }
@@ -2787,16 +2817,22 @@ int BC_WindowBase::get_single_text_width(int font, const char *text, int length)
 
 int BC_WindowBase::get_single_text_width(int font, const wchar_t *text, int length)
 {
-//#ifdef HAVE_XFT
-       XGlyphInfo extents;
+#ifdef HAVE_XFT
+       if(get_resources()->use_xft && get_xft_struct(font)) {
+               XGlyphInfo extents;
 
-       XftTextExtents32(top_level->display,
-               get_xft_struct(font),
-               (const FcChar32*)text,
-               length,
-               &extents);
-       return extents.xOff;
-//#endif
+               XftTextExtents32(top_level->display, get_xft_struct(font),
+                       (const FcChar32*)text, length, &extents);
+               return extents.xOff;
+       }
+#endif
+       if(!get_font_struct(font)) return 0;
+       XChar2b xtext[length], *xp = xtext;
+       for( int i=0; i<length; ++i,++xp ) {
+               xp->byte1 = (unsigned char) (text[i] >> 8);
+               xp->byte2 = (unsigned char) (text[i] & 0xff);
+       }
+       return XTextWidth16(get_font_struct(font), xtext, length);
 }
 
 int BC_WindowBase::get_text_width(int font, const char *text, int length)
@@ -3992,15 +4028,30 @@ void BC_WindowBase::set_background(VFrame *bitmap)
        draw_background(0, 0, w, h);
 }
 
+void BC_WindowBase::put_title(const char *text)
+{
+       if( BC_Resources::locale_utf8 ) {
+               char *bp=this->title, *ep = bp+sizeof(this->title)-1;
+               for( const char *cp=text; *cp!=0 && bp<ep; ) {
+                       if( *cp < 0 ) { do { *bp++ = *cp++; } while( *cp < 0 );  continue; }
+                       if( *cp < ' ' ) { *bp++ = ' ';  ++cp;  continue; }
+                       *bp++ = *cp++;
+               }
+               *bp = 0;
+       }
+       else
+               strcpy(this->title, text);
+}
+
 void BC_WindowBase::set_title(const char *text)
 {
-       XTextProperty titleprop;
-       char *txlist[2];
+       put_title(_(text));
 
-       strcpy(this->title, _(text));
+       char *txlist[2];
        txlist[0] = this->title;
        txlist[1] = 0;
 
+       XTextProperty titleprop;
        XmbTextListToTextProperty(top_level->display, txlist, 1,
                XStdICCTextStyle, &titleprop);
        XSetWMName(top_level->display, top_level->win, &titleprop);
@@ -4180,6 +4231,15 @@ void BC_WindowBase::translate_coordinates(Window src_w,
        }
 }
 
+void BC_WindowBase::get_root_coordinates(int x, int y, int *abs_x, int *abs_y)
+{
+       translate_coordinates(win, top_level->rootwin, x, y, abs_x, abs_y);
+}
+
+void BC_WindowBase::get_win_coordinates(int abs_x, int abs_y, int *x, int *y)
+{
+       translate_coordinates(top_level->rootwin, win, abs_x, abs_y, x, y);
+}