[Git][wxwidgets/wxwidgets][3.2-2025-10-backports] Fix wxSearchCtrl best size with GTK

2 views
Skip to first unread message

Vadim Zeitlin (@_VZ_)

unread,
Oct 11, 2025, 4:59:14 PM (10 days ago) Oct 11
to wx-commi...@googlegroups.com

Vadim Zeitlin pushed to branch 3.2-2025-10-backports at wxWidgets / wxWidgets

Commits:

  • 8e0dbf7f
    by Paul Cornett at 2025-10-11T22:57:54+02:00
    Fix wxSearchCtrl best size with GTK
    
    Implement GetSizeFromTextSize() and use that to get a reasonable width.
    See #25210
    
    (cherry picked from commit f2faedcb96b2ff6be7304370065803dcd0d2a980)
    

3 changed files:

Changes:

  • docs/changes.txt
    ... ... @@ -283,6 +283,7 @@ wxMSW
    283 283
     - Fix crash when copying wxBitmaps if GDI resources are exhausted (#24703).
    
    284 284
     - Fix MSVS debug visualizer for wxString (#25684).
    
    285 285
     - Fix TAB behaviour in a panel with notebook as only active child (#25443).
    
    286
    +- Fix wxSearchCtrl best size determination (#25210).
    
    286 287
     - Reduce GDI resource usage in wxStaticBitmap (Alex Shvartzkop, #25698).
    
    287 288
     
    
    288 289
     wxOSX:
    

  • include/wx/gtk/srchctrl.h
    ... ... @@ -96,6 +96,10 @@ private:
    96 96
     #endif // wxUSE_MENUS/!wxUSE_MENUS
    
    97 97
         }
    
    98 98
     
    
    99
    +protected:
    
    100
    +    virtual wxSize DoGetBestSize() const override;
    
    101
    +    virtual wxSize DoGetSizeFromTextSize(int xlen, int ylen = -1) const override;
    
    102
    +
    
    99 103
     private:
    
    100 104
         virtual GtkEntry *GetEntry() const wxOVERRIDE
    
    101 105
             { return m_entry; }
    

  • src/gtk/srchctrl.cpp
    ... ... @@ -16,12 +16,14 @@
    16 16
     
    
    17 17
     #ifndef WX_PRECOMP
    
    18 18
         #include "wx/menu.h"
    
    19
    +    #include "wx/utils.h"
    
    19 20
     #endif //WX_PRECOMP
    
    20 21
     
    
    21
    -#include "wx/utils.h"
    
    22
    +#include "wx/gtk/private/wrapgtk.h"
    
    22 23
     #include "wx/gtk/private.h"
    
    23 24
     #include "wx/gtk/private/event.h"
    
    24 25
     #include "wx/gtk/private/gtk3-compat.h"
    
    26
    +#include "wx/gtk/private/stylecontext.h"
    
    25 27
     
    
    26 28
     
    
    27 29
     #if GTK_CHECK_VERSION(3,6,0)
    
    ... ... @@ -198,10 +200,10 @@ bool wxSearchCtrl::Create(wxWindow *parent, wxWindowID id,
    198 200
     
    
    199 201
         m_focusWidget = GTK_WIDGET(entry);
    
    200 202
     
    
    201
    -    PostCreation(size);
    
    202
    -
    
    203 203
         gtk_entry_set_text(entry, wxGTK_CONV(value));
    
    204 204
     
    
    205
    +    PostCreation(size);
    
    206
    +
    
    205 207
         SetHint(_("Search"));
    
    206 208
     
    
    207 209
         GTKConnectChangedSignal();
    
    ... ... @@ -384,4 +386,39 @@ void wxSearchCtrl::PopupSearchMenu()
    384 386
     
    
    385 387
     #endif // wxUSE_MENUS
    
    386 388
     
    
    389
    +wxSize wxSearchCtrl::DoGetBestSize() const
    
    390
    +{
    
    391
    +    return DoGetSizeFromTextSize(GetCharWidth() * 8);
    
    392
    +}
    
    393
    +
    
    394
    +wxSize wxSearchCtrl::DoGetSizeFromTextSize(int xlen, int ylen) const
    
    395
    +{
    
    396
    +    wxSize size(GTKGetPreferredSize(m_widget));
    
    397
    +    size.x += xlen;
    
    398
    +    if (size.y < ylen)
    
    399
    +        size.y = ylen;
    
    400
    +
    
    401
    +#ifdef wxHAS_GTK_SEARCH_ENTRY
    
    402
    +    if (HasGtkSearchEntry() &&
    
    403
    +        gtk_entry_get_icon_storage_type(m_entry, GTK_ENTRY_ICON_SECONDARY) == GTK_IMAGE_EMPTY)
    
    404
    +    {
    
    405
    +        // If text is empty, there is no "clear" icon, and GtkEntry preferred size
    
    406
    +        // does not account for it. So add in size of primary icon as a substitute.
    
    407
    +        GdkPixbuf* pixbuf = gtk_entry_get_icon_pixbuf(m_entry, GTK_ENTRY_ICON_PRIMARY);
    
    408
    +        if (pixbuf)
    
    409
    +            size.x += gdk_pixbuf_get_width(pixbuf);
    
    410
    +
    
    411
    +        // Also account for secondary icon margin
    
    412
    +        wxGtkStyleContext sc(GetContentScaleFactor());
    
    413
    +        sc.Add(GTK_TYPE_ENTRY, "entry", "entry", nullptr);
    
    414
    +        sc.Add(GTK_TYPE_IMAGE, "image", "right", nullptr);
    
    415
    +        GtkBorder margin;
    
    416
    +        gtk_style_context_get_margin(sc, GTK_STATE_FLAG_NORMAL, &margin);
    
    417
    +        size.x += margin.left + margin.right;
    
    418
    +    }
    
    419
    +#endif // wxHAS_GTK_SEARCH_ENTRY
    
    420
    +
    
    421
    +    return size;
    
    422
    +}
    
    423
    +
    
    387 424
     #endif // wxUSE_SEARCHCTRL


View it on GitLab.
You're receiving this email because of your account on gitlab.com. Manage all notifications · Help Notification message regarding https://gitlab.com/wxwidgets/wxwidgets/-/commit/8e0dbf7fee7ab073a45702f2478c04fc94d840bb at 1760216351

Reply all
Reply to author
Forward
0 new messages