add browse bg btn in titler, prevent a few badwin signals, titler text popup upgrades
[goodguy/history.git] / cinelerra-5.1 / guicast / bcwindowbase.C
index c54685a41070cb335d2c2989badacf69202ce4c3..791aa0c856ccfb84252f3f9dced1afd7c0c70ea5 100644 (file)
@@ -53,6 +53,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <wchar.h>
+#include <typeinfo>
 
 #include <X11/extensions/Xinerama.h>
 #include <X11/extensions/Xvlib.h>
@@ -112,13 +113,19 @@ BC_WindowBase::~BC_WindowBase()
        {
 // stop event input
                XSelectInput(top_level->display, this->win, 0);
-               motion_events = resize_events = translation_events = 0;
+               XSync(top_level->display,0);
 #ifndef SINGLE_THREAD
                top_level->dequeue_events(win);
 #endif
+// drop active window refs to this
+               if(top_level->active_grab == this) top_level->active_grab = 0;
                if(top_level->active_menubar == this) top_level->active_menubar = 0;
                if(top_level->active_popup_menu == this) top_level->active_popup_menu = 0;
                if(top_level->active_subwindow == this) top_level->active_subwindow = 0;
+// drop motion window refs to this
+               if(top_level->motion_events && top_level->last_motion_win == this->win)
+                       top_level->motion_events = 0;
+
 // Remove pointer from parent window to this
                parent_window->subwindows->remove(this);
        }
@@ -249,6 +256,7 @@ int BC_WindowBase::initialize()
        done = 0;
        done_set = 0;
        window_running = 0;
+       display_lock_owner = 0;
        test_keypress = 0;
        keys_return[0] = 0;
        is_deleting = 0;
@@ -282,6 +290,7 @@ int BC_WindowBase::initialize()
        event_win = 0;
        last_motion_win = 0;
        key_pressed = 0;
+       active_grab = 0;
        active_menubar = 0;
        active_popup_menu = 0;
        active_subwindow = 0;
@@ -953,7 +962,10 @@ locking_message = event->xclient.message_type;
 //   event_names[event->type] : "Unknown");
 //}
 
-
+       if( active_grab && active_grab->grab_event(event) ) {
+               unlock_window();
+               return 0;
+       }
 
 
        switch(event->type) {
@@ -2815,7 +2827,7 @@ int BC_WindowBase::get_single_text_width(int font, const char *text, int length)
        if(get_resources()->use_fontset && top_level->get_fontset(font))
                return XmbTextEscapement(top_level->get_fontset(font), text, length);
        else
-       if(get_font_struct(font)) 
+       if(get_font_struct(font))
                return XTextWidth(get_font_struct(font), text, length);
        else
        {
@@ -2884,27 +2896,21 @@ int BC_WindowBase::get_text_width(int font, const char *text, int length)
 
 int BC_WindowBase::get_text_width(int font, const wchar_t *text, int length)
 {
-       int i, j, w = 0, line_w = 0;
-
-       if(length < 0) length = wcslen(text);
+       int i, j, w = 0;
+       if( length < 0 ) length = wcslen(text);
 
-       for(i = 0, j = 0; i <= length; i++)
-       {
-               line_w = 0;
-               if(text[i] == '\n')
-               {
-                       line_w = get_single_text_width(font, &text[j], i - j);
-                       j = i + 1;
+       for( i=j=0; i<length && text[i]; ++i ) {
+               if( text[i] != '\n' ) continue;
+               if( i > j ) {
+                       int lw = get_single_text_width(font, &text[j], i-j);
+                       if( w < lw ) w = lw;
                }
-               else
-               if(text[i] == 0)
-                       line_w = get_single_text_width(font, &text[j], length - j);
-
-               if(line_w > w) w = line_w;
+               j = i + 1;
+       }
+       if( j < length ) {
+               int lw = get_single_text_width(font, &text[j], length-j);
+               if( w < lw ) w = lw;
        }
-
-       if(i > length && w == 0)
-               w = get_single_text_width(font, text, length);
 
        return w;
 }
@@ -3288,9 +3294,9 @@ void BC_WindowBase::set_done(int return_value)
                ptr->message_type = SetDoneXAtom;
                ptr->format = 32;
                this->return_value = return_value;
-// May lock up here because XSendEvent doesn't work too well 
+// May lock up here because XSendEvent doesn't work too well
 // asynchronous with XNextEvent.
-// This causes BC_WindowEvents to forward a copy of the event to run_window where 
+// This causes BC_WindowEvents to forward a copy of the event to run_window where
 // it is deleted.
 // Deletion of event_thread is done at the end of BC_WindowBase::run_window() - by calling the destructor
                put_event(event);
@@ -3304,6 +3310,19 @@ void BC_WindowBase::close(int return_value)
        set_done(return_value);
 }
 
+int BC_WindowBase::grab(BC_WindowBase *window)
+{
+       if( window->active_grab && this != window->active_grab ) return 0;
+       window->active_grab = this;
+       return 1;
+}
+int BC_WindowBase::ungrab(BC_WindowBase *window)
+{
+       if( window->active_grab && this != window->active_grab ) return 0;
+       window->active_grab = 0;
+       return 1;
+}
+
 int BC_WindowBase::get_w()
 {
        return w;
@@ -3634,66 +3653,83 @@ BC_Clipboard* BC_WindowBase::get_clipboard()
 #endif
 }
 
-int BC_WindowBase::get_relative_cursor_x()
+void BC_WindowBase::get_relative_cursor_xy(int &x, int &y, int lock_window)
 {
-       int abs_x, abs_y, x, y, win_x, win_y;
+       int abs_x, abs_y, win_x, win_y;
        unsigned int temp_mask;
        Window temp_win;
 
+       if(lock_window) this->lock_window("BC_WindowBase::get_relative_cursor_xy");
        XQueryPointer(top_level->display, top_level->win,
           &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
           &temp_mask);
 
        XTranslateCoordinates(top_level->display, top_level->rootwin,
           win, abs_x, abs_y, &x, &y, &temp_win);
-
+       if(lock_window) this->unlock_window();
+}
+int BC_WindowBase::get_relative_cursor_x(int lock_window)
+{
+       int x, y;
+       get_relative_cursor_xy(x, y, lock_window);
        return x;
 }
-
-int BC_WindowBase::get_relative_cursor_y()
+int BC_WindowBase::get_relative_cursor_y(int lock_window)
 {
-       int abs_x, abs_y, x, y, win_x, win_y;
-       unsigned int temp_mask;
-       Window temp_win;
-
-       XQueryPointer(top_level->display, top_level->win,
-          &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
-          &temp_mask);
-
-       XTranslateCoordinates(top_level->display,
-          top_level->rootwin, win, abs_x, abs_y, &x, &y, &temp_win);
-
+       int x, y;
+       get_relative_cursor_xy(x, y, lock_window);
        return y;
 }
 
-int BC_WindowBase::get_abs_cursor_x(int lock_window)
+void BC_WindowBase::get_abs_cursor_xy(int &abs_x, int &abs_y, int lock_window)
 {
-       int abs_x, abs_y, win_x, win_y;
+       int win_x, win_y;
        unsigned int temp_mask;
        Window temp_win;
 
-       if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_x");
+       if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_xy");
        XQueryPointer(top_level->display, top_level->win,
                &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
                &temp_mask);
        if(lock_window) this->unlock_window();
+}
+int BC_WindowBase::get_abs_cursor_x(int lock_window)
+{
+       int abs_x, abs_y;
+       get_abs_cursor_xy(abs_x, abs_y, lock_window);
        return abs_x;
 }
-
 int BC_WindowBase::get_abs_cursor_y(int lock_window)
 {
-       int abs_x, abs_y, win_x, win_y;
-       unsigned int temp_mask;
-       Window temp_win;
-
-       if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_y");
-       XQueryPointer(top_level->display, top_level->win,
-               &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
-               &temp_mask);
-       if(lock_window) this->unlock_window();
+       int abs_x, abs_y;
+       get_abs_cursor_xy(abs_x, abs_y, lock_window);
        return abs_y;
 }
 
+void BC_WindowBase::get_pop_cursor_xy(int &px, int &py, int lock_window)
+{
+       int margin = 100;
+       get_abs_cursor_xy(px, py, lock_window);
+       if( px < margin ) px = margin;
+       if( py < margin ) py = margin;
+       int wd = get_screen_w(lock_window,-1) - margin;
+       if( px > wd ) px = wd;
+       int ht = get_screen_h(lock_window,-1) - margin;
+       if( py > ht ) py = ht;
+}
+int BC_WindowBase::get_pop_cursor_x(int lock_window)
+{
+       int px, py;
+       get_pop_cursor_xy(px, py, lock_window);
+       return px;
+}
+int BC_WindowBase::get_pop_cursor_y(int lock_window)
+{
+       int px, py;
+       get_pop_cursor_xy(px, py, lock_window);
+       return py;
+}
+
 int BC_WindowBase::match_window(Window win)
 {
        if (this->win == win) return 1;
@@ -3727,11 +3763,10 @@ int BC_WindowBase::get_cursor_over_window()
 
 int BC_WindowBase::cursor_above()
 {
-       int rx = get_relative_cursor_x();
-       if( rx < 0 || rx >= get_w() ) return 0;
-       int ry = get_relative_cursor_y();
-       if( ry < 0 || ry >= get_h() ) return 0;
-       return 1;
+       int rx, ry;
+       get_relative_cursor_xy(rx, ry);
+       return rx < 0 || rx >= get_w() ||
+               ry < 0 || ry >= get_h() ? 0 : 1;
 }
 
 int BC_WindowBase::get_drag_x()
@@ -3756,13 +3791,15 @@ int BC_WindowBase::get_cursor_y()
 
 int BC_WindowBase::dump_windows()
 {
-       printf("\tBC_WindowBase::dump_windows window=%p win=%p\n",
-               this, (void*)this->win);
+       printf("\tBC_WindowBase::dump_windows window=%p win=%p '%s', %dx%d+%d+%d %s\n",
+               this, (void*)this->win, title, w,h,x,y, typeid(*this).name());
        for(int i = 0; i < subwindows->size(); i++)
                subwindows->get(i)->dump_windows();
-       for(int i = 0; i < popups.size(); i++)
-               printf("\tBC_WindowBase::dump_windows popup=%p win=%p\n",
-                       popups.get(i), (void*)popups.get(i)->win);
+       for(int i = 0; i < popups.size(); i++) {
+               BC_WindowBase *p = popups[i];
+               printf("\tBC_WindowBase::dump_windows popup=%p win=%p '%s', %dx%d+%d+%d %s\n",
+                       p, (void*)p->win, p->title, p->w,p->h,p->x,p->y, typeid(*p).name());
+       }
        return 0;
 }
 
@@ -4356,3 +4393,15 @@ BC_Pixmap *BC_WindowBase::create_pixmap(VFrame *vframe)
 }
 
 
+void BC_WindowBase::flicker(int n, int ms)
+{
+       int color = get_bg_color();
+       for( int i=2*n; --i>=0; ) {
+               set_inverse();          set_bg_color(WHITE);
+               clear_box(0,0, w,h);    flash(1);
+               sync_display();         Timer::delay(ms);
+       }
+       set_bg_color(color);
+       set_opaque();
+}
+