wxMenu::GtkAppend() (src/gtk/menu.cpp) decides whether to build a
GtkImageMenuItem or a plain GtkMenuItem based on
mitem->GetBitmap().IsOk() at Append() time:
case wxITEM_NORMAL: if (mitem->GetBitmap().IsOk()) menuItem = gtk_image_menu_item_new_with_label(""); else { const char* stockid = wxGetStockGtkID(mitem->GetId()); if (stockid) menuItem = gtk_image_menu_item_new_from_stock(stockid, nullptr); else menuItem = gtk_menu_item_new_with_label(""); // <-- plain item }
But wxMenu::Append() returns the new wxMenuItem* precisely to
support the common idiom:
menu->Append(id, label)->SetBitmap(bmp);Here SetBitmap() runs after Append() already built the
underlying GTK widget. For any non-stock id, that widget ends up
being a plain GtkMenuItem. Later, whenever the menu is actually
shown (wxWindowGTK::DoPopupMenu() → wxMenu::SetupBitmaps() →
wxMenuItem::SetupBitmaps()), wx calls:
gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(m_menuItem), image);
on that plain GtkMenuItem, which trips the GTK_IS_IMAGE_MENU_ITEM
assertion:
Gtk-CRITICAL **: gtk_image_menu_item_set_image: assertion 'GTK_IS_IMAGE_MENU_ITEM (image_menu_item)' failed
and the bitmap is silently never attached.
Minimal standalone repro (not included in this PR, happy to attach if
useful):
wxMenu menu; menu.Append(wxID_HIGHEST + 1000, "Test Item")->SetBitmap(wxBitmap(16, 16)); PopupMenu(&menu, wxPoint(20, 20));
Confirmed present on both the 3.2 branch and master, built from
source (not just the Fedora-packaged wx), with --with-gtk=3 and
system third-party libs.
Always build a GtkImageMenuItem for wxITEM_NORMAL (unless a stock
GTK id already provides one via gtk_image_menu_item_new_from_stock).
An empty GtkImageMenuItem behaves identically to a plain
GtkMenuItem, so this is a no-op for items that never get a bitmap,
and fixes the assertion (and the missing icon) for items that get
SetBitmap() called after Append().
Verified: rebuilt both 3.2 and master from source with the fix
applied, reran the repro above — no assertion, clean stderr.
A separate PR with the same fix against the 3.2 branch will follow
if useful; let me know your preference on which branch should take
this first.
🤖 Generated with Claude Code
https://github.com/wxWidgets/wxWidgets/pull/26771
(1 file)
—
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!
You are receiving this because you are subscribed to this thread.![]()
Thanks, I agree that using SetBitmap() should work, of course, but OTOH it seems a bit wrong to unconditionally create image menu items when many (most?) of them are not going to use images. I think replacing the existing item with an image item in SetBitmap(), if necessary, should work too, but I'm not sure if it's worth it.
I believe that the really correct thing to do would be stop using GtkImageMenuItem entirely and always create a box ourselves and then pack whatever we need into it, just as the documentation for this class suggests. But this would require many more changes...
@paulcor Do you have any thoughts about this?
—
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!
You are receiving this because you are subscribed to this thread.![]()
This is fine.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!
You are receiving this because you are subscribed to this thread.![]()
—
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!
You are receiving this because you are subscribed to this thread.![]()