From ab307322bf7f85253aa59d5c524184bdcc04d7e3 Mon Sep 17 00:00:00 2001 From: gambas Date: Mon, 18 Jan 2021 22:27:33 +0100 Subject: [PATCH] Do not crash if expanded widgets change during arrangement. Fix GTK+ window centering method. [GB.GTK] * BUG: Do not crash if expanded widgets change during arrangement. [GB.GTK3] * BUG: Remove platform-specific window centering method. * BUG: Do not crash if expanded widgets change during arrangement. [GB.QT4] * BUG: Do not crash if expanded widgets change during arrangement. [GB.QT5] * BUG: Do not crash if expanded widgets change during arrangement. --- gb.gtk/src/gmainwindow.cpp | 27 +++++++++++++++------------ gb.gtk3/src/gb.gtk.platform.h | 1 - gb.gtk3/src/wayland/main.c | 6 ------ gb.gtk3/src/x11/main.c | 21 --------------------- gb.qt4/share/gb.form.arrangement.h | 26 ++++++++++++++++++++------ 5 files changed, 35 insertions(+), 46 deletions(-) diff --git a/gb.gtk/src/gmainwindow.cpp b/gb.gtk/src/gmainwindow.cpp index ca0bae5a2..8b90f6205 100644 --- a/gb.gtk/src/gmainwindow.cpp +++ b/gb.gtk/src/gmainwindow.cpp @@ -958,19 +958,22 @@ void gMainWindow::center() { if (!isTopLevel()) return; + GdkRectangle rect; + int x, y; + + if (!isTopLevel()) return; + #ifdef GTK3 - PLATFORM.Window.Center(GTK_WINDOW(border)); - #else - GdkRectangle rect; - int x, y; - - gDesktop::availableGeometry(screen(), &rect); - - x = rect.x + (rect.width - width()) / 2; - y = rect.y + (rect.height - height()) / 2; - - move(x, y); + if (MAIN_platform_is_wayland) + gtk_window_set_position(GTK_WINDOW(border), GTK_WIN_POS_CENTER_ON_PARENT); #endif + + gDesktop::availableGeometry(screen(), &rect); + + x = rect.x + (rect.width - width()) / 2; + y = rect.y + (rect.height - height()) / 2; + + move(x, y); } bool gMainWindow::isModal() const @@ -991,13 +994,13 @@ void gMainWindow::showModal() setType(GTK_WINDOW_TOPLEVEL); gtk_window_set_modal(GTK_WINDOW(border), true); - center(); setTransientFor(); save = _current; _current = this; show(); + center(); gtk_grab_add(border); gApplication::enterLoop(this); diff --git a/gb.gtk3/src/gb.gtk.platform.h b/gb.gtk3/src/gb.gtk.platform.h index 4ec4d62cb..1adef1faa 100644 --- a/gb.gtk3/src/gb.gtk.platform.h +++ b/gb.gtk3/src/gb.gtk.platform.h @@ -42,7 +42,6 @@ typedef struct { long (*GetId)(GdkWindow *); - void (*Center)(GtkWindow *); } Window; struct { diff --git a/gb.gtk3/src/wayland/main.c b/gb.gtk3/src/wayland/main.c index 8f04b762e..301e5f224 100644 --- a/gb.gtk3/src/wayland/main.c +++ b/gb.gtk3/src/wayland/main.c @@ -47,12 +47,6 @@ static long window_get_id(GdkWindow *window) return 0; } -static void window_center(GtkWindow *window) -{ - fprintf(stderr, "window_center\n"); - gtk_window_set_position(window, GTK_WIN_POS_CENTER_ON_PARENT); -} - //------------------------------------------------------------------------- bool desktop_has_system_tray() diff --git a/gb.gtk3/src/x11/main.c b/gb.gtk3/src/x11/main.c index 6e5cafd9c..801dc1b6c 100644 --- a/gb.gtk3/src/x11/main.c +++ b/gb.gtk3/src/x11/main.c @@ -58,26 +58,6 @@ static long window_get_id(GdkWindow *window) return window ? GDK_WINDOW_XID(window) : 0; } -static void window_center(GtkWindow *window) -{ - GdkRectangle rect; - int x, y; - int screen; - - screen = gdk_screen_get_number(gtk_window_get_screen(window)); - -#if GTK_CHECK_VERSION(3, 22, 0) - gdk_monitor_get_workarea(gdk_display_get_monitor(gdk_display_get_default(), screen), &rect); -#else - gdk_screen_get_monitor_workarea(gdk_screen_get_default(), screen, &rect); -#endif - - x = rect.x + (rect.width - width()) / 2; - y = rect.y + (rect.height - height()) / 2; - - gtk_window_move(window, x, y); -} - //------------------------------------------------------------------------- static bool desktop_has_system_tray() @@ -134,7 +114,6 @@ void *GB_GTK3_X11_1[] EXPORT = { (void *)platform_create_plug, (void *)window_get_id, - (void *)window_center, (void *)desktop_has_system_tray, (void *)desktop_show_tray_icon, diff --git a/gb.qt4/share/gb.form.arrangement.h b/gb.qt4/share/gb.form.arrangement.h index 2c6070f89..8c7979e01 100644 --- a/gb.qt4/share/gb.form.arrangement.h +++ b/gb.qt4/share/gb.form.arrangement.h @@ -393,9 +393,15 @@ void FUNCTION_NAME(void *_object) //(QFrame *cont) { if (IS_EXPAND(ob)) // && !autoresize) { - h = sexp / nexp; - sexp -= h; - nexp--; + if (nexp == 0) // the list of expanded widget may change + h = 0; + else + { + h = sexp / nexp; + sexp -= h; + nexp--; + } + if (h <= 0) MOVE_WIDGET(ob, wid, -GET_WIDGET_W(wid), GET_WIDGET_Y(wid)); } @@ -426,9 +432,17 @@ void FUNCTION_NAME(void *_object) //(QFrame *cont) { if (IS_EXPAND(ob)) // && !autoresize) { - w = sexp / nexp; - sexp -= w; - nexp--; + if (nexp == 0) // the list of expanded widget may change + { + w = 0; + } + else + { + w = sexp / nexp; + sexp -= w; + nexp--; + } + if (w <= 0) MOVE_WIDGET(ob, wid, GET_WIDGET_X(wid), -GET_WIDGET_H(wid)); }