From df9f4af3570b06b70e4da278a75c604e0e45b700 Mon Sep 17 00:00:00 2001 From: Paul Cornett Date: Mon, 11 Nov 2019 08:29:28 -0800 Subject: [PATCH] Improve our estimate of GtkEntry margins For GTK2, get the "inner border" the same way GTK does it. And for GTK3, provide an actual implementation. Also, don't return a wxPoint for a size. See #18567 --- include/wx/gtk/control.h | 2 +- src/gtk/control.cpp | 51 +++++++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/include/wx/gtk/control.h b/include/wx/gtk/control.h index 1171f55919..2890f6ccaa 100644 --- a/include/wx/gtk/control.h +++ b/include/wx/gtk/control.h @@ -84,7 +84,7 @@ protected: wxSize GTKGetPreferredSize(GtkWidget* widget) const; // Inner margins in a GtkEntry - wxPoint GTKGetEntryMargins(GtkEntry* entry) const; + wxSize GTKGetEntryMargins(GtkEntry* entry) const; private: wxDECLARE_DYNAMIC_CLASS(wxControl); diff --git a/src/gtk/control.cpp b/src/gtk/control.cpp index 009f65333f..7aab1a03e5 100644 --- a/src/gtk/control.cpp +++ b/src/gtk/control.cpp @@ -363,37 +363,44 @@ wxSize wxControl::GTKGetPreferredSize(GtkWidget* widget) const return wxSize(req.width, req.height); } -wxPoint wxControl::GTKGetEntryMargins(GtkEntry* entry) const +wxSize wxControl::GTKGetEntryMargins(GtkEntry* entry) const { - wxPoint marg(0, 0); + wxSize size; + gtk_entry_get_layout_offsets(entry, &size.x, &size.y); + +#ifdef __WXGTK3__ + GtkBorder border; + GtkStyleContext* sc = gtk_widget_get_style_context(GTK_WIDGET(entry)); + gtk_style_context_get_padding(sc, gtk_style_context_get_state(sc), &border); +#else + // Equivalent to the GTK2 private function _gtk_entry_effective_inner_border() + + GtkBorder border = { 2, 2, 2, 2 }; -#ifndef __WXGTK3__ #if GTK_CHECK_VERSION(2,10,0) - // The margins we have previously set - const GtkBorder* border = NULL; if (wx_is_at_least_gtk2(10)) - border = gtk_entry_get_inner_border(entry); - - if ( border ) { - marg.x = border->left + border->right; - marg.y = border->top + border->bottom; + const GtkBorder* innerBorder1 = gtk_entry_get_inner_border(entry); + if (innerBorder1) + border = *innerBorder1; + else + { + GtkBorder* innerBorder2; + gtk_widget_style_get(GTK_WIDGET(entry), "inner-border", &innerBorder2, NULL); + if (innerBorder2) + { + border = *innerBorder2; + gtk_border_free(innerBorder2); + } + } } #endif // GTK+ 2.10+ -#else // GTK+ 3 - // Gtk3 does not use inner border, but StyleContext and CSS - // TODO: implement it, starting with wxTextEntry::DoSetMargins() -#endif // GTK+ 2/3 +#endif - int x, y; - gtk_entry_get_layout_offsets(entry, &x, &y); - // inner borders are included. Substract them so we can get other margins - x -= marg.x; - y -= marg.y; - marg.x += 2 * x + 2; - marg.y += 2 * y + 2; + size.x += border.left + border.right; + size.y += border.top + border.bottom; - return marg; + return size; }