Dear InterWeb...

Mock The Week is about to start, so I'll be quick. Can I ellipsize a GtkMenuItem? It's easy to make Tasks have very long menu items, which end up being too big for the screen. I'd like to get the menu items truncated so that the menu will fit on the screen, or something. I guess I'll have to truncate the string manually if there is no magic way of doing it directly in the menu, but that really is a fallback plan.

21:55 Tuesday, 19 Feb 2008 [#] [computers] (4 comments)

Posted by Stefan Stuhr at Tue Feb 19 22:52:27 2008:
Maybe this will help:

$ python
>>> import gtk
>>> menuitem = gtk.MenuItem("This is a test")
>>> menuitem.child
<gtk.AccelLabel object at 0xb6ba9144 (GtkAccelLabel at 0x82da900)>
>>> help(menuitem.child)
Help on AccelLabel in module gtk object:

class AccelLabel(Label)
|  Object GtkAccelLabel
[...]
Posted by Emmanuele Bassi at Tue Feb 19 23:56:40 2008:
child = gtk_bin_get_child (GTK_BIN (menu_item));

if (GTK_IS_LABEL (child))
  gtk_label_set_ellipsize (GTK_LABEL (child), PANGO_ELLIPSIZE_END);

---

there's code that does the same in GtkRecentChooserMenu, for the same reason.
Posted by Ross at Wed Feb 20 07:56:46 2008:
Ah, yes, I was going to attempt pretty much exactly what ebassi suggested but was wondering how it would know where to ellipsize.  I'm guessing it only ellipsizes when the menu is the width of the screen?
Posted by Emmanuele Bassi at Wed Feb 20 11:05:51 2008:
just for reference: yes, the label will use the current screen size to clamp its maximum width.

a solution is to set the max-width-chars property to an arbitrary value. 30 chars are usually deemed enough in most menus.

Name:


E-mail:


URL:


Add 4 and 7 (required):


Comment: