From dedf7e17fe055ebb329d070f9f44530e4f474e20 Mon Sep 17 00:00:00 2001 From: gambas Date: Fri, 25 Jun 2021 12:21:33 +0200 Subject: [PATCH] Style.BackgroundOf() method is now reliable. [GB.GTK] * BUG: Style.BackgroundOf() method is now reliable. [GB.GTK3] * BUG: Style.BackgroundOf() method is now reliable. [GB.QT4] * BUG: Style.BackgroundOf() method is now reliable. [GB.QT5] * BUG: Style.BackgroundOf() method is now reliable. --- gb.gtk/src/gbutton.cpp | 8 ++++++ gb.gtk/src/gbutton.h | 2 ++ gb.gtk/src/gcontrol.cpp | 14 ++++----- gb.gtk/src/gcontrol.h | 1 + gb.gtk/src/gdesktop.cpp | 61 +++++++++++++++++++++++----------------- gb.gtk/src/gdesktop.h | 5 ++-- gb.gtk/src/gmenu.cpp | 4 +-- gb.gtk/src/gtextarea.cpp | 5 ++++ gb.gtk/src/gtextarea.h | 1 + gb.gtk/src/gtextbox.cpp | 6 ++++ gb.gtk/src/gtextbox.h | 2 ++ gb.qt4/src/CWidget.cpp | 2 ++ 12 files changed, 74 insertions(+), 37 deletions(-) diff --git a/gb.gtk/src/gbutton.cpp b/gb.gtk/src/gbutton.cpp index 44e904b50..ace4f326c 100644 --- a/gb.gtk/src/gbutton.cpp +++ b/gb.gtk/src/gbutton.cpp @@ -748,3 +748,11 @@ bool gButton::setInverted(bool v) gt_widget_set_inverted(_label, false); return false; } + +gColor gButton::defaultBackground() const +{ + if (type == Check || type == Radio) + return gControl::defaultBackground(); + else + return gDesktop::getColor(gDesktop::BUTTON_BACKGROUND, !isEnabled()); +} diff --git a/gb.gtk/src/gbutton.h b/gb.gtk/src/gbutton.h index 5c558cefb..ce7289758 100644 --- a/gb.gtk/src/gbutton.h +++ b/gb.gtk/src/gbutton.h @@ -67,6 +67,8 @@ public: virtual bool setInverted(bool v); virtual void setRealForeground(gColor color); + gColor defaultBackground() const; + //virtual void setRealBackground(gColor color); //"Method" diff --git a/gb.gtk/src/gcontrol.cpp b/gb.gtk/src/gcontrol.cpp index 9b7b205b7..c4bae7c5a 100644 --- a/gb.gtk/src/gcontrol.cpp +++ b/gb.gtk/src/gcontrol.cpp @@ -2128,6 +2128,11 @@ void gControl::setName(char *name) if (name) _name = g_strdup(name); } +gColor gControl::defaultBackground() const +{ + return gDesktop::getColor(gDesktop::BACKGROUND, !isEnabled()); +} + #ifdef GTK3 GtkWidget *gControl::getStyleSheetWidget() @@ -2196,7 +2201,6 @@ void gControl::updateStyleSheet(bool dirty) } } - if (!isReallyVisible() || !_style_dirty) return; @@ -2238,10 +2242,8 @@ gColor gControl::realBackground(bool no_default) { if (_bg != COLOR_DEFAULT) return _bg; - else if (pr) - return pr->realBackground(no_default); else - return no_default ? gDesktop::getColor(gDesktop::BACKGROUND) : COLOR_DEFAULT; + return no_default ? defaultBackground() : COLOR_DEFAULT; } void gControl::setRealBackground(gColor color) @@ -2294,10 +2296,8 @@ gColor gControl::realBackground(bool no_default) { if (_bg_set) return use_base ? get_gdk_base_color(widget, isEnabled()) : get_gdk_bg_color(widget, isEnabled()); - else if (pr) - return pr->realBackground(no_default); else - return no_default ? gDesktop::getColor(gDesktop::BACKGROUND) : COLOR_DEFAULT; + return no_default ? defaultBackground() : COLOR_DEFAULT; } static void set_background(GtkWidget *widget, gColor color, bool use_base) diff --git a/gb.gtk/src/gcontrol.h b/gb.gtk/src/gcontrol.h index c737e39d7..5ac9b0d42 100644 --- a/gb.gtk/src/gcontrol.h +++ b/gb.gtk/src/gcontrol.h @@ -139,6 +139,7 @@ public: gColor foreground() const { return _fg; } virtual void setBackground(gColor color = COLOR_DEFAULT); virtual void setForeground(gColor color = COLOR_DEFAULT); + virtual gColor defaultBackground() const; gColor realBackground(bool no_default = false); gColor realForeground(bool no_default = false); virtual void setRealBackground(gColor color); diff --git a/gb.gtk/src/gdesktop.cpp b/gb.gtk/src/gdesktop.cpp index 194c82ee0..850190572 100644 --- a/gb.gtk/src/gdesktop.cpp +++ b/gb.gtk/src/gdesktop.cpp @@ -40,6 +40,7 @@ Desktop bool gDesktop::_colors_valid = false; gColor gDesktop::_colors[NUM_COLORS]; +gColor gDesktop::_colors_disabled[NUM_COLORS]; gFont *gDesktop::_desktop_font = NULL; int gDesktop::_desktop_scale = 0; #ifdef GTK3 @@ -204,10 +205,13 @@ void gDesktop::onThemeChange() #ifdef GTK3 -static gColor get_color(GType type, bool fg, GtkStateFlags state) +static gColor get_color(GType type, bool fg, GtkStateFlags state, bool disabled) { GtkStyleContext *style = gt_get_style(type, state == STATE_SELECTED ? "selection" : NULL, (type == GTK_TYPE_TOOLTIP && !fg) ? GTK_STYLE_CLASS_BACKGROUND : NULL); + if (disabled) + state = (GtkStateFlags)(state | STATE_INSENSITIVE); + gtk_style_context_set_state(style, state); if (!fg) @@ -237,11 +241,14 @@ static gColor get_color(GType type, bool fg, GtkStateFlags state) #else -static gColor get_color(GType type, bool fg, GtkStateType state) +static gColor get_color(GType type, bool fg, GtkStateType state, bool disabled) { GtkStyle *st = gt_get_style(type); GdkColor *color; + if (disabled) + state = STATE_INSENSITIVE; + if (type == GTK_TYPE_ENTRY) { if (fg) @@ -262,42 +269,44 @@ static gColor get_color(GType type, bool fg, GtkStateType state) #endif -void gDesktop::calcColors() +void gDesktop::calc_colors(gColor colors[], bool disabled) { - _colors[BACKGROUND] = get_color(GTK_TYPE_WINDOW, false, STATE_NORMAL); - _colors[FOREGROUND] = get_color(GTK_TYPE_WINDOW, true, STATE_NORMAL); - _colors[TEXT_BACKGROUND] = get_color(GTK_TYPE_ENTRY, false, STATE_NORMAL); - _colors[TEXT_FOREGROUND] = get_color(GTK_TYPE_ENTRY, true, STATE_NORMAL); - _colors[SELECTED_BACKGROUND] = get_color(GTK_TYPE_ENTRY, false, STATE_SELECTED); - _colors[SELECTED_FOREGROUND] = get_color(GTK_TYPE_ENTRY, true, STATE_SELECTED); - _colors[BUTTON_BACKGROUND] = get_color(GTK_TYPE_BUTTON, false, STATE_NORMAL); - _colors[BUTTON_FOREGROUND] = get_color(GTK_TYPE_BUTTON, true, STATE_NORMAL); - _colors[TOOLTIP_BACKGROUND] = get_color(GTK_TYPE_TOOLTIP, false, STATE_NORMAL); - _colors[TOOLTIP_FOREGROUND] = get_color(GTK_TYPE_TOOLTIP, true, STATE_NORMAL); + colors[BACKGROUND] = get_color(GTK_TYPE_WINDOW, false, STATE_NORMAL, disabled); + colors[FOREGROUND] = get_color(GTK_TYPE_WINDOW, true, STATE_NORMAL, disabled); + colors[TEXT_BACKGROUND] = get_color(GTK_TYPE_ENTRY, false, STATE_NORMAL, disabled); + colors[TEXT_FOREGROUND] = get_color(GTK_TYPE_ENTRY, true, STATE_NORMAL, disabled); + colors[SELECTED_BACKGROUND] = get_color(GTK_TYPE_ENTRY, false, STATE_SELECTED, disabled); + colors[SELECTED_FOREGROUND] = get_color(GTK_TYPE_ENTRY, true, STATE_SELECTED, disabled); + colors[BUTTON_BACKGROUND] = get_color(GTK_TYPE_BUTTON, false, STATE_NORMAL, disabled); + colors[BUTTON_FOREGROUND] = get_color(GTK_TYPE_BUTTON, true, STATE_NORMAL, disabled); + colors[TOOLTIP_BACKGROUND] = get_color(GTK_TYPE_TOOLTIP, false, STATE_NORMAL, disabled); + colors[TOOLTIP_FOREGROUND] = get_color(GTK_TYPE_TOOLTIP, true, STATE_NORMAL, disabled); #ifdef GTK3 #if GTK_CHECK_VERSION(3, 12, 0) - _colors[LINK_FOREGROUND] = get_color(GTK_TYPE_LINK_BUTTON, true, STATE_LINK); - _colors[VISITED_FOREGROUND] = get_color(GTK_TYPE_LINK_BUTTON, true, (STATE_T)((int)STATE_LINK + (int)STATE_VISITED)); + colors[LINK_FOREGROUND] = get_color(GTK_TYPE_LINK_BUTTON, true, STATE_LINK, disabled); + colors[VISITED_FOREGROUND] = get_color(GTK_TYPE_LINK_BUTTON, true, (STATE_T)((int)STATE_LINK + (int)STATE_VISITED), disabled); #else - _colors[LINK_FOREGROUND] = get_color(GTK_TYPE_LINK_BUTTON, true, STATE_NORMAL); - _colors[VISITED_FOREGROUND] = IMAGE.DarkerColor(_colors[LINK_FOREGROUND]); + colors[LINK_FOREGROUND] = get_color(GTK_TYPE_LINK_BUTTON, true, STATE_NORMAL, disabled); + colors[VISITED_FOREGROUND] = IMAGE.DarkerColor(_colors[LINK_FOREGROUND]); #endif #else - _colors[LINK_FOREGROUND] = IMAGE.LighterColor(_colors[SELECTED_BACKGROUND]); - _colors[VISITED_FOREGROUND] = IMAGE.DarkerColor(_colors[LINK_FOREGROUND]); + colors[LINK_FOREGROUND] = IMAGE.LighterColor(_colors[SELECTED_BACKGROUND]); + colors[VISITED_FOREGROUND] = IMAGE.DarkerColor(_colors[LINK_FOREGROUND]); #endif - _colors[LIGHT_BACKGROUND] = IMAGE.MergeColor(_colors[SELECTED_BACKGROUND], _colors[SELECTED_FOREGROUND], 0.3); - _colors[LIGHT_FOREGROUND] = IMAGE.MergeColor(_colors[BACKGROUND], _colors[FOREGROUND], 0.3); - - _colors_valid = true; + colors[LIGHT_BACKGROUND] = IMAGE.MergeColor(_colors[SELECTED_BACKGROUND], _colors[SELECTED_FOREGROUND], 0.3); + colors[LIGHT_FOREGROUND] = IMAGE.MergeColor(_colors[BACKGROUND], _colors[FOREGROUND], 0.3); } -gColor gDesktop::getColor(int color) +gColor gDesktop::getColor(int color, bool disabled) { if (!_colors_valid) - calcColors(); + { + calc_colors(_colors, false); + calc_colors(_colors_disabled, true); + _colors_valid = true; + } - return _colors[color]; + return disabled ? _colors_disabled[color] : _colors[color]; } void gDesktop::screenResolution(int screen, double *x, double *y) diff --git a/gb.gtk/src/gdesktop.h b/gb.gtk/src/gdesktop.h index 47e2bc8f6..8bde9b5c2 100644 --- a/gb.gtk/src/gdesktop.h +++ b/gb.gtk/src/gdesktop.h @@ -74,7 +74,7 @@ public: static void screenResolution(int screen, double *x, double *y); - static gColor getColor(int color); + static gColor getColor(int color, bool disabled = false); static void onThemeChange(); @@ -84,8 +84,9 @@ private: static gFont *_desktop_font; static bool _colors_valid; static gColor _colors[NUM_COLORS]; + static gColor _colors_disabled[NUM_COLORS]; - static void calcColors(); + static void calc_colors(gColor colors[], bool disabled); #ifdef GTK3 static GtkStyleProvider *_css; diff --git a/gb.gtk/src/gmenu.cpp b/gb.gtk/src/gmenu.cpp index a160ae5f6..66924441a 100644 --- a/gb.gtk/src/gmenu.cpp +++ b/gb.gtk/src/gmenu.cpp @@ -149,8 +149,8 @@ static gboolean cb_map(GtkWidget *menu, gMenu *data) data->_mapping = true; data->hideSeparators(); - gtk_widget_hide(menu); - gtk_widget_show(menu); + /*gtk_widget_hide(menu); + gtk_widget_show(menu);*/ //gtk_menu_reposition(GTK_MENU(menu)); data->_mapping = false; diff --git a/gb.gtk/src/gtextarea.cpp b/gb.gtk/src/gtextarea.cpp index ade9a4894..f6a7b8636 100644 --- a/gb.gtk/src/gtextarea.cpp +++ b/gb.gtk/src/gtextarea.cpp @@ -1156,3 +1156,8 @@ void gTextArea::setBorder(bool b) gControl::setBorder(b); setFramePadding(hasBorder() ? gDesktop::scale() * 3 / 4 : 0); } + +gColor gTextArea::defaultBackground() const +{ + return gDesktop::getColor(gDesktop::TEXT_BACKGROUND, !isEnabled()); +} diff --git a/gb.gtk/src/gtextarea.h b/gb.gtk/src/gtextarea.h index 439cb3461..3f18f912f 100644 --- a/gb.gtk/src/gtextarea.h +++ b/gb.gtk/src/gtextarea.h @@ -98,6 +98,7 @@ public: virtual void setMinimumSize(); virtual void setFont(gFont *ft); virtual void setBorder(bool b); + virtual gColor defaultBackground() const; #ifdef GTK3 virtual GtkWidget *getStyleSheetWidget(); virtual const char *getStyleSheetColorNode(); diff --git a/gb.gtk/src/gtextbox.cpp b/gb.gtk/src/gtextbox.cpp index 52ef7044b..f3f33e061 100644 --- a/gb.gtk/src/gtextbox.cpp +++ b/gb.gtk/src/gtextbox.cpp @@ -24,6 +24,7 @@ #include "widgets.h" #include "gapplication.h" #include "gkey.h" +#include "gdesktop.h" #include "gtextbox.h" #ifdef GTK3 @@ -559,3 +560,8 @@ void gTextBox::onLeaveEvent() gdk_window_hide(TEXT_AREA(entry)); } #endif + +gColor gTextBox::defaultBackground() const +{ + return gDesktop::getColor(gDesktop::TEXT_BACKGROUND, !isEnabled()); +} diff --git a/gb.gtk/src/gtextbox.h b/gb.gtk/src/gtextbox.h index 5940d2c0a..c7e54e8df 100644 --- a/gb.gtk/src/gtextbox.h +++ b/gb.gtk/src/gtextbox.h @@ -82,6 +82,8 @@ public: virtual void setMinimumSize(); virtual void setFont(gFont *ft); + + virtual gColor defaultBackground() const; #ifdef GTK3 virtual void onEnterEvent(); diff --git a/gb.qt4/src/CWidget.cpp b/gb.qt4/src/CWidget.cpp index 7106e1303..52e5194dc 100644 --- a/gb.qt4/src/CWidget.cpp +++ b/gb.qt4/src/CWidget.cpp @@ -1509,6 +1509,8 @@ GB_COLOR CWIDGET_get_real_background(CWIDGET *_object) if (bg != COLOR_DEFAULT) return bg; + return WIDGET->palette().color(WIDGET->backgroundRole()).rgb() & 0xFFFFFF; + CWIDGET *parent = (CWIDGET *)CWIDGET_get_parent(THIS); if (parent)