merged hv7 mod
[goodguy/history.git] / cinelerra-5.1 / guicast / bcwindowbase.C
index b827113a52a4c55cd42b120767b559c592de21ac..6599369e1b2276e71eac074dcbeb3154e938631c 100644 (file)
@@ -36,7 +36,7 @@
 #include "bcwindowbase.h"
 #include "bcwindowevents.h"
 #include "bccmodels.h"
-#include "colors.h"
+#include "bccolors.h"
 #include "condition.h"
 #include "cursors.h"
 #include "bchash.h"
@@ -181,9 +181,10 @@ BC_WindowBase::~BC_WindowBase()
                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
+// past bug in X caused XRenderExtensionInfo to be damaged
+// if this is done here. left to be done in XCloseDisplay by Xlib.
+// works in more modern systems, and needed for leak testing.
+#if defined(HAVE_XFT) && defined(VALGRIND)
                static void *BC_WindowBase::*xft_font[] = {
                         &BC_WindowBase::smallfont_xft,
                         &BC_WindowBase::mediumfont_xft,
@@ -222,7 +223,12 @@ BC_WindowBase::~BC_WindowBase()
                }
                else
 #endif
+               {
+// _XftDisplayInfo needs a lock.
+                       get_resources()->create_window_lock->lock("BC_WindowBase::~BC_WindowBase");
                        XCloseDisplay(display);
+                       get_resources()->create_window_lock->unlock();
+               }
 // clipboard uses a different display connection
                clipboard->stop_clipboard();
                delete clipboard;
@@ -295,6 +301,7 @@ int BC_WindowBase::initialize()
        active_menubar = 0;
        active_popup_menu = 0;
        active_subwindow = 0;
+       cursor_entered = 0;
        pixmap = 0;
        bg_pixmap = 0;
        _7segment_pixmaps = 0;
@@ -324,16 +331,24 @@ int BC_WindowBase::initialize()
 #ifdef HAVE_LIBXXF86VM
        vm_switched = 0;
 #endif
-       smallfont_xft = 0;
-       bold_largefont_xft = 0;
-       bold_mediumfont_xft = 0;
-       bold_smallfont_xft = 0;
        input_method = 0;
        input_context = 0;
 
+       smallfont = 0;
+       mediumfont = 0;
+       largefont = 0;
+       bigfont = 0;
+       clockfont = 0;
+
+       smallfont_xft = 0;
        mediumfont_xft = 0;
        largefont_xft = 0;
        bigfont_xft = 0;
+       clockfont_xft = 0;
+
+       bold_smallfont_xft = 0;
+       bold_mediumfont_xft = 0;
+       bold_largefont_xft = 0;
 #ifdef SINGLE_THREAD
        completion_lock = new Condition(0, "BC_WindowBase::completion_lock");
 #else
@@ -529,24 +544,11 @@ int BC_WindowBase::create_window(BC_WindowBase *parent_window,
                        size_hints.x = this->x;
                        size_hints.y = this->y;
                }
-
-               char *txlist[2];
-               txlist[0] = this->title;
-               txlist[1] = 0;
-               XTextProperty titleprop;
-               if(options & WINDOW_UTF8)
-                       Xutf8TextListToTextProperty(display, txlist,  1,
-                               XUTF8StringStyle, &titleprop);
-               else
-                       XmbTextListToTextProperty(display, txlist, 1,
-                               XStdICCTextStyle, &titleprop);
-               XSetWMProperties(display, win, &titleprop, &titleprop,
-                       0, 0, &size_hints, 0, 0);
-               XFree(titleprop.value);
+               XSetWMProperties(display, win, 0, 0, 0, 0, &size_hints, 0, 0);
                get_atoms();
-
+               set_title(title);
 #ifndef SINGLE_THREAD
-               clipboard = new BC_Clipboard(display_name);
+               clipboard = new BC_Clipboard(this);
                clipboard->start_clipboard();
 #endif
 
@@ -683,6 +685,15 @@ Display* BC_WindowBase::init_display(const char *display_name)
                        }
                }
        }
+
+       static int xsynch = -1;
+       if( xsynch < 0 ) {
+               const char *cp = getenv("CIN_XSYNCH");
+               xsynch = !cp ? 0 : atoi(cp);
+       }
+       if( xsynch > 0 )
+               XSynchronize(display, True);
+
        return display;
 }
 
@@ -962,12 +973,15 @@ locking_message = event->xclient.message_type;
 //   event_names[event->type] : "Unknown");
 //}
 
-       if( active_grab && active_grab->grab_event(event) ) {
+       if( active_grab ) {
                unlock_window();
-               return 0;
+               active_grab->lock_window("BC_WindowBase::dispatch_event 3");
+               result = active_grab->grab_event(event);
+               active_grab->unlock_window();
+               if( result ) return result;
+               lock_window("BC_WindowBase::dispatch_event 4");
        }
 
-
        switch(event->type) {
        case ClientMessage:
 // Clear the resize buffer
@@ -1325,16 +1339,26 @@ locking_message = event->xclient.message_type;
                break;
 
        case LeaveNotify:
+               if( cursor_entered && event->xcrossing.window == win ) {
+                       cursor_entered = 0;
+               }
                event_win = event->xany.window;
                dispatch_cursor_leave();
                break;
 
        case EnterNotify:
+               if( !cursor_entered && event->xcrossing.window == win ) {
+                       if( !event->xcrossing.focus && get_resources()->grab_input_focus ) {
+                               XSetInputFocus(display, win, RevertToParent, CurrentTime);
+                       }
+                       cursor_entered = 1;
+               }
                event_win = event->xany.window;
                cursor_x = event->xcrossing.x;
                cursor_y = event->xcrossing.y;
                dispatch_cursor_enter();
                break;
+
        default:
                break;
        }
@@ -2287,6 +2311,10 @@ int BC_WindowBase::init_fonts()
                if( !(bigfont = XLoadQueryFont(display, _(resources.big_font2))) )
                        bigfont = XLoadQueryFont(display, "fixed");
 
+       if((clockfont = XLoadQueryFont(display, _(resources.clock_font))) == NULL)
+               if((clockfont = XLoadQueryFont(display, _(resources.clock_font2))) == NULL)
+                       clockfont = XLoadQueryFont(display, "fixed");
+
        init_xft();
        if(get_resources()->use_fontset)
        {
@@ -2305,8 +2333,11 @@ int BC_WindowBase::init_fonts()
                        largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
                bigfontset = XCreateFontSet(display, resources.big_fontset, &m, &n, &d);
                if( !bigfontset )
-                       largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
-               if(bigfontset && largefontset && mediumfontset && smallfontset) {
+                       bigfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
+               clockfontset = XCreateFontSet(display, resources.clock_fontset, &m, &n, &d);
+               if( !clockfontset )
+                       clockfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
+               if(clockfontset && bigfontset && largefontset && mediumfontset && smallfontset) {
                        curr_fontset = mediumfontset;
                        get_resources()->use_fontset = 1;
                }
@@ -2351,6 +2382,12 @@ void BC_WindowBase::init_xft()
                if(!(bigfont_xft =
                        XftFontOpenXlfd(display, screen, resources.big_font_xft2)))
                        bigfont_xft = XftFontOpenXlfd(display, screen, "fixed");
+       if(!(clockfont_xft =
+               (resources.clock_font_xft[0] == '-' ?
+                       XftFontOpenXlfd(display, screen, resources.clock_font_xft) :
+                       XftFontOpenName(display, screen, resources.clock_font_xft))) )
+               clockfont_xft = XftFontOpenXlfd(display, screen, "fixed");
+
 
        if(!(bold_smallfont_xft =
                (resources.small_b_font_xft[0] == '-' ?
@@ -2368,21 +2405,24 @@ void BC_WindowBase::init_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 ||
-           !bold_largefont_xft || !bold_mediumfont_xft || !bold_largefont_xft ) {
+           !bold_largefont_xft || !bold_mediumfont_xft || !bold_largefont_xft ||
+           !clockfont_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",
+                       " %s=%p\n %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.clock_font_xft, clockfont_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);
        }
+// _XftDisplayInfo needs a lock.
+       XftDefaultHasRender(display);
 #endif // HAVE_XFT
 }
 
@@ -2733,6 +2773,7 @@ XFontStruct* BC_WindowBase::get_font_struct(int font)
                case MEDIUMFONT: return top_level->mediumfont; break;
                case LARGEFONT:  return top_level->largefont;  break;
                case BIGFONT:    return top_level->bigfont;    break;
+               case CLOCKFONT:  return top_level->clockfont;  break;
        }
        return 0;
 }
@@ -2748,6 +2789,7 @@ XFontSet BC_WindowBase::get_fontset(int font)
                        case MEDIUMFONT: fs = top_level->mediumfontset; break;
                        case LARGEFONT:  fs = top_level->largefontset; break;
                        case BIGFONT:    fs = top_level->bigfontset;   break;
+                       case CLOCKFONT: fs = top_level->clockfontset; break;
                }
        }
 
@@ -2762,6 +2804,7 @@ XftFont* BC_WindowBase::get_xft_struct(int font)
                case MEDIUMFONT:   return (XftFont*)top_level->mediumfont_xft;
                case LARGEFONT:    return (XftFont*)top_level->largefont_xft;
                case BIGFONT:      return (XftFont*)top_level->bigfont_xft;
+               case CLOCKFONT:    return (XftFont*)top_level->clockfont_xft;
                case MEDIUMFONT_3D: return (XftFont*)top_level->bold_mediumfont_xft;
                case SMALLFONT_3D:  return (XftFont*)top_level->bold_smallfont_xft;
                case LARGEFONT_3D:  return (XftFont*)top_level->bold_largefont_xft;
@@ -2772,13 +2815,6 @@ XftFont* BC_WindowBase::get_xft_struct(int font)
 #endif
 
 
-
-
-
-
-
-
-
 int BC_WindowBase::get_current_font()
 {
        return top_level->current_font;
@@ -2814,6 +2850,7 @@ void BC_WindowBase::set_fontset(int font)
                        case MEDIUMFONT: fs = top_level->mediumfontset; break;
                        case LARGEFONT:  fs = top_level->largefontset; break;
                        case BIGFONT:    fs = top_level->bigfontset;   break;
+                       case CLOCKFONT:  fs = top_level->clockfontset; break;
                }
        }
 
@@ -2877,26 +2914,6 @@ 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
-       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
-       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)
 {
        int i, j, w = 0, line_w = 0;
@@ -3703,6 +3720,38 @@ BC_Clipboard* BC_WindowBase::get_clipboard()
 #endif
 }
 
+Atom BC_WindowBase::to_clipboard(const char *data, long len, int clipboard_num)
+{
+       return get_clipboard()->to_clipboard(this, data, len, clipboard_num);
+}
+
+long BC_WindowBase::from_clipboard(char *data, long maxlen, int clipboard_num)
+{
+       return get_clipboard()->from_clipboard(data, maxlen, clipboard_num);
+}
+
+long BC_WindowBase::clipboard_len(int clipboard_num)
+{
+       return get_clipboard()->clipboard_len(clipboard_num);
+}
+
+int BC_WindowBase::do_selection_clear(Window win)
+{
+       top_level->event_win = win;
+       return dispatch_selection_clear();
+}
+
+int BC_WindowBase::dispatch_selection_clear()
+{
+       int result = 0;
+       for( int i=0; i<subwindows->total && !result; ++i )
+               result = subwindows->values[i]->dispatch_selection_clear();
+       if( !result )
+               result = selection_clear_event();
+       return result;
+}
+
+
 void BC_WindowBase::get_relative_cursor_xy(int &x, int &y, int lock_window)
 {
        int abs_x, abs_y, win_x, win_y;
@@ -4098,39 +4147,30 @@ void BC_WindowBase::put_title(const char *text)
        *cp = 0;
 }
 
-void BC_WindowBase::set_title(const char *text)
+void BC_WindowBase::set_title(const char *text, int utf8)
 {
+// utf8>0: wm + net_wm, utf8=0: wm only,  utf<0: net_wm only
        put_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);
-       XSetWMIconName(top_level->display, top_level->win, &titleprop);
-       XFree(titleprop.value);
-
-       flush();
-}
-
-void BC_WindowBase::set_utf8title(const char *text)
-{
-       XTextProperty titleprop;
-       char *txlist[2];
-
-       strcpy(this->title, text);
-       txlist[0] = this->title;
-       txlist[1] = 0;
-
-       Xutf8TextListToTextProperty(top_level->display, txlist,  1,
-               XUTF8StringStyle, &titleprop);
-       XSetWMName(top_level->display, top_level->win, &titleprop);
-       XSetWMIconName(top_level->display, top_level->win, &titleprop);
-       XFree(titleprop.value);
-
+       const unsigned char *wm_title = (const unsigned char *)title;
+       int title_len = strlen((const char *)title);
+       if( utf8 >= 0 ) {
+               Atom xa_wm_name = XA_WM_NAME;
+               Atom xa_icon_name = XA_WM_ICON_NAME;
+               Atom xa_string = XA_STRING;
+               XChangeProperty(display, win, xa_wm_name, xa_string, 8,
+                               PropModeReplace, wm_title, title_len);
+               XChangeProperty(display, win, xa_icon_name, xa_string, 8,
+                               PropModeReplace, wm_title, title_len);
+       }
+       if( utf8 != 0 ) {
+               Atom xa_net_wm_name = XInternAtom(display, "_NET_WM_NAME", True);
+               Atom xa_net_icon_name = XInternAtom(display, "_NET_WM_ICON_NAME", True);
+               Atom xa_utf8_string = XInternAtom(display, "UTF8_STRING", True);
+               XChangeProperty(display, win, xa_net_wm_name, xa_utf8_string, 8,
+                                       PropModeReplace, wm_title, title_len);
+               XChangeProperty(display, win, xa_net_icon_name, xa_utf8_string, 8,
+                                       PropModeReplace, wm_title, title_len);
+       }
        flush();
 }