Fix wxTextCtrl size computation in wxGTK

Also improve wxTreeTextCtrl positioning in wxTreeCtrl.

See #23001, #23610, #23623.

(cherry picked from commit 27aa7e90a7882faeb9da42375e95ebba855ae1ca)
This commit is contained in:
Alex Shvartzkop 2023-06-10 17:38:21 +02:00 committed by Vadim Zeitlin
parent ad91e9d35a
commit 8ab4cac7a9
4 changed files with 27 additions and 44 deletions

View File

@ -338,6 +338,7 @@ wxGTK:
- Allow selecting and copying text in wxMessageDialog (Ian McInerney, #23039). - Allow selecting and copying text in wxMessageDialog (Ian McInerney, #23039).
- Fix initial size of top-level window on Wayland (#23041). - Fix initial size of top-level window on Wayland (#23041).
- Improve size and behaviour of in-place editor in wxTreeCtrl (taler21, #23001). - Improve size and behaviour of in-place editor in wxTreeCtrl (taler21, #23001).
- Also improve size compuation of wxTextCtrl (Alex Shvartzkop, #23610).
wxMSW: wxMSW:

View File

@ -438,26 +438,21 @@ wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl *owner,
wxRect rect; wxRect rect;
m_owner->GetBoundingRect(m_itemEdited, rect, true); m_owner->GetBoundingRect(m_itemEdited, rect, true);
// corrects position and size for better appearance
#ifdef __WXMSW__
rect.x -= 5;
rect.width += 10;
#elif defined(__WXGTK__)
rect.x -= 5;
rect.y -= 2;
rect.width += 8;
rect.height += 4;
#endif // platforms
const wxSize textSize = rect.GetSize(); const wxSize textSize = rect.GetSize();
wxSize fullSize = GetSizeFromTextSize(textSize); wxSize fullSize = GetSizeFromTextSize(textSize);
// Ensure that the text field covers tree item.
fullSize.x = wxMax(fullSize.x, textSize.x + 5);
// Correct position for better appearance.
#ifdef __WXMSW__
rect.x -= 5;
#else
rect.x -= (fullSize.x - textSize.x) / 2;
#endif
if ( fullSize.y > textSize.y ) if ( fullSize.y > textSize.y )
{ rect.y -= (fullSize.y - textSize.y) / 2;
// It's ok to extend the rect to the right horizontally, which happens
// when we just change its size without changing its position below,
// but when extending it vertically, we need to keep it centered.
rect.y -= (fullSize.y - textSize.y + 1) / 2;
}
// Also check that the control fits into the parent window. // Also check that the control fits into the parent window.
const int totalWidth = m_owner->GetClientSize().x; const int totalWidth = m_owner->GetClientSize().x;

View File

@ -366,12 +366,17 @@ wxSize wxControl::GTKGetPreferredSize(GtkWidget* widget) const
wxSize wxControl::GTKGetEntryMargins(GtkEntry* entry) const wxSize wxControl::GTKGetEntryMargins(GtkEntry* entry) const
{ {
wxSize size; wxSize size;
gtk_entry_get_layout_offsets(entry, &size.x, &size.y);
#ifdef __WXGTK3__ #ifdef __WXGTK3__
GtkBorder border;
GtkStyleContext* sc = gtk_widget_get_style_context(GTK_WIDGET(entry)); GtkStyleContext* sc = gtk_widget_get_style_context(GTK_WIDGET(entry));
gtk_style_context_get_padding(sc, gtk_style_context_get_state(sc), &border); GtkStateFlags state = gtk_style_context_get_state(sc);
GtkBorder padding, border;
gtk_style_context_get_padding(sc, state, &padding);
gtk_style_context_get_border(sc, state, &border);
size.x += padding.left + padding.right + border.left + border.right;
size.y += padding.top + padding.bottom + border.top + border.bottom;
#else #else
if (gtk_entry_get_has_frame(entry)) if (gtk_entry_get_has_frame(entry))
{ {
@ -402,10 +407,10 @@ wxSize wxControl::GTKGetEntryMargins(GtkEntry* entry) const
} }
} }
#endif // GTK+ 2.10+ #endif // GTK+ 2.10+
#endif
size.x += border.left + border.right; size.x += border.left + border.right;
size.y += border.top + border.bottom; size.y += border.top + border.bottom;
#endif
return size; return size;
} }

View File

@ -2161,29 +2161,11 @@ wxSize wxTextCtrl::DoGetSizeFromTextSize(int xlen, int ylen) const
if ( IsSingleLine() ) if ( IsSingleLine() )
{ {
if ( HasFlag(wxBORDER_NONE) ) // Default height
{
#ifdef __WXGTK3__
tsize.IncBy(9, 0);
#else
tsize.IncBy(4, 0);
#endif // GTK3
}
else
{
// default height
tsize.y = GTKGetPreferredSize(m_widget).y; tsize.y = GTKGetPreferredSize(m_widget).y;
#ifdef __WXGTK3__
// Add the margins we have previously set. // Add padding + border size
tsize.IncBy( GTKGetEntryMargins(GetEntry()) ); tsize.x += GTKGetEntryMargins(GetEntry()).x;
#else
// For GTK 2 these margins are too big, so hard code something more
// reasonable, this is not great but should be fine considering
// that it's very unlikely that GTK 2 is going to evolve, making
// this inappropriate.
tsize.IncBy(20, 0);
#endif
}
} }
//multiline //multiline