GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Nov 22, 2014 12:15 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: [SOLVED] Use markup on a menuitem?
PostPosted: Mon Dec 24, 2012 6:40 pm 
Offline
Familiar Face

Joined: Thu Sep 13, 2012 10:49 pm
Posts: 6
Happy Christmas Eve.

Me again, trying to do more weird things with menus. Now I'm trying to get a menuitem to use markup. I dug around this forum for a few hours and pestered google to no avail.

I tried this and many variants of this, but it seems menuitems do not like their labels set this way:
Code:
      gchar *text = "Label with <b>bold</b>";
      menuitem1 = gtk_image_menu_item_new (  );
      GtkWidget *label1 = gtk_label_new ( NULL );
      gtk_label_set_markup_with_mnemonic ( GTK_LABEL(label1), text );
      gtk_widget_add_mnemonic_label ( GTK_WIDGET(menuitem1), label1 );

It compiles fine, but segfaults with:
Code:
(deskmenu:10550): Gtk-CRITICAL **: gtk_widget_get_pango_context: assertion `GTK_IS_WIDGET (widget)' failed
(deskmenu:10550): Pango-CRITICAL **: pango_context_get_language: assertion `context != NULL' failed
Segmentation fault

Any notions?


Last edited by Miven on Wed Dec 26, 2012 6:08 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Use markup on a menuitem?
PostPosted: Mon Dec 24, 2012 10:35 pm 
Offline
Familiar Face

Joined: Thu Sep 13, 2012 10:49 pm
Posts: 6
I figured it out and the solution is a bit grotty:
Code:
        menuitem1 = gtk_image_menu_item_new (  );
        GtkWidget *label_m;
        label_m = gtk_label_new ( label );
        gtk_label_set_markup_with_mnemonic ( GTK_LABEL(label_m), label );
        /*
         * gtk_widget_add_mnemonic_label() fails,
         * so you have to do this:
         */
        gtk_container_add ( GTK_CONTAINER(menuitem1), label_m );
        /*
         * Now the labels are all centered and look stupid.
         * gtk_label_set_justify ( GTK_LABEL(label1), GTK_JUSTIFY_LEFT );
         * fails to do anything, so you have to do this:
         */
        gtk_misc_set_alignment ( GTK_MISC(label_m), 0.0, 0.5 );
        /*
         * And all is good: markup, mnemonics, and left justified.
         */

gtk_container and gtk_misc? I dunno, but it works.


Top
 Profile  
 
 Post subject: Re: Use markup on a menuitem?
PostPosted: Tue Dec 25, 2012 11:13 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
I haven't tried anything too adventurous with Pango. I've seen the sort of work around you did in other places, though I see a few inconsistencies. In your first post there is no actual mnemonic included in the text (an underscore identifies the mnemonic character). Could this be a cause of your problems or did you just type in a shortened version where the mnemonic has been cut off?
gtk_widget_add_mnemonic_label and gtk_label_set_mnemonic_widget work on the label being external to the widget (otherwise this is set automatically). I don't see anywhere in your first bit of code where the label is placed in your app.
In your second example you are using gtkimagemenuitem and attaching the label as the single bin child widget. This is the image of the gtkimagemenuitem so if you wanted an image associated with this menu item, unfortunately the label is taking its place. If that's no problem then it'll do as a workaround (though you may as well just use the standard gtkmenuitem for simplicity).
Not too sure how to go fixing your core problem of marking up the gchar* text directly. Maybe when people with pango expertise get off holidays you might get an answer.


Top
 Profile  
 
 Post subject: Re: Use markup on a menuitem?
PostPosted: Wed Dec 26, 2012 1:35 am 
Offline
Familiar Face

Joined: Thu Sep 13, 2012 10:49 pm
Posts: 6
Merry Christmas Paul.

Yeah, the quoted bit I put in is very much paraphrased from the acual code. I didn't want to get too long winded, and I just wanted to show the problem bits. The actual code is much different, but I reduce to simplify.

Yeah, mnemonic works whether there is an actual mnemonic or not, and because it's an image_menu_item, you can poke the image in whenever you want, first, last, whatever, it doesn't have to be like packing an hbox. Of course, I'm using compiled GTK-2.8.19 et al on an 8 year old Mandrake10 system that came with gtk-1.2. I don't know if things have changed since then. Pango is deep magic to me.

The real issue was that markup works so easily in a button, but getting it to work in a menuitem is a bit odd. You have to create the label, then add it to the container, not the menuitem.

Then, for some strange reason, the label text in the menuitems are all centered, instead of left justified like normal. It looks very ugly in a menu with icons on the left. I figure it is a bit of a bug in pango-1.10.4 which is the one I have. Can't decide RTL or LTR? Go centered? I dunno. Pango is deep magic to me. Someone may have fixed this bug, I didn't look into it too far.

The real oddity was having to cast the label to a gtk_misc in order to set the alignment properly. I found that tidbit in an obscure forum post I got from google after many hours of searching. Before this, I'd never cast anything to a gtk_misc. Apparently the only thing it's there for is to set alignment. To quote the gtk reference:

GtkMisc - Base class for widgets with alignments and padding

Hey, what else to do on a rainy Christmas Eve morning when rent is paid, eveything is done, and I'm just waiting until Miller Time? :)

Anyway, it all works now, and I've learned something in the process. Maybe others aren't as demanding of their menus as I am, but this is a good post for the googlebots to save for eternity. Surely someone else is going to try the same thing.

Any notion how to mark this thread as [SOLVED]? I have another one that needs the same treatment.

Peace and Cheer.


Top
 Profile  
 
 Post subject: Re: Use markup on a menuitem?
PostPosted: Wed Dec 26, 2012 4:16 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Just press on edit for your first post and change the subject line to have "[SOLVED]: " at the beginning.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group