First let me say that creating a different color for one label in a menu should be very very limited. Maybe your developing the application for work and your boss or the client just wants it that way. Maybe you have accidentally went to a web page that's trying to run some very destructive code on your computer. You could have an application that's a "emergency Internet stop" A menu item that when activated kills the process that is your browser, locks all Internet access and maybe even shuts down the computer. I have seen similar applications in anti-virus software.

The point is you should have a good reason for doing it. So don't create a menu where the background colors and font colors of each menuitem are different for no reason! A little styling goes a very very long way, don't abuse it!

To style just the first menu item label you may think we could add: gtk_widget_set_name(GTK_WIDGET(item1),"mitem1");Then use: GtkLabel #mitem1You'd be wrong!First lets look at the function used to create a stand-alone label. That is, one that's not going in a menu, It's:GtkWidget *gtk_label_new (const gchar *str);

str: The text of the labelReturns: the new GtkLabel

It returns a pointer to a GtkLabel object. We could then name it just like we did in our C code using:

Then use the name in the CSS to style it using:#somelabel {color: red;}

But #mitem1 is not a GtkLabel, it's a GtkMenuItem. Remember The function we used to create the label for the menu was:gtk_menu_item_new_with_label ()

It creates a new GtkMenuItem whose child is a GtkLabel. It returns a pointer to the newly created GtkMenuItem. NOT the label, the label object is internal, we don't have access to it. The definition of gtk_menu_item_new_with_label () is in gtkmenuitem.c on lines 514 thru 520.

And we can see in the definition of gtk_menu_item_new_with_label() that g_object_new was given: GTK_TYPE_MENU_ITEM. So that's the return type, a GtkMenuItem NOT a GtkLabel. That's why we can't access the label through CSS. But there is a way...

We need access to the GtkMenuItem's child which is the GtkLabel we're looking to style. Here's the object hierarchy for GtkMenuItem.

Code:

+----GtkBin +----GtkMenuItem

In the callback function cb_right_click() we add the following: Create pointers to both labels:GtkWidget *itemlabel1, *itemlabel2;

Use the function: gtk_bin_get_child () It gets the child of the GtkBin, or NULL if the bin contains no child widget. itemlabel1 = gtk_bin_get_child (GTK_BIN (item1)); itemlabel2 = gtk_bin_get_child (GTK_BIN (item2));

Save the context states, so all modifications done through gtk_style_context_add_class() can be reverted using gtk_style_context_restore(). gtk_style_context_save (context1); gtk_style_context_save (context2);

All the built in types have predefined CSS selectors. For example when styling a button, we can can use: GtkButton or .button The . is called a class selector and can be used to style any class which it precedes. Now we can use label1 and label2 just like the built in type .button in our CSS to style the labels individually. We use:

.label1 {color: black;}

.label1:hover {color: forestgreen;}

.label2 {color: cornflowerblue;}

.label2:hover {color: yellow;}

The full code is listed below. I've added two more style contexts' for the GtkMenuItem's. This let's us use different background colors for the hover state of each menu item.

This is a example of creating and styling a GtkProgressBar with GTK+3 and CSS.Thanks to Pablo Farias Navarro, (Youtube user thecplusplusguy). The code is his but I've updated it for GTK+3, added CSS, did some slight modifications and made it work without C++ code.

Suppose you have two or more buttons and want different actions to take place depending on which button is pressed.You can write two different callbacks, one for each button but this isn't necessary and it's poor code. Instead you can do this by attaching different labels to your buttons and using those labels to identify which button was pressed in your callback.

This method is shown in the "Guitar tuner" tutorial on the Gnome site but it uses the Anjuta IDE (integrated development enviroment), it's UI designer and Gstreamer. Using a designer is overkill for a interface with just six buttons and I wanted to show a minimal example without Gstreamer or an IDEhttp://developer.gnome.org/gnome-devel-demos/stable/guitar-tuner.c.html.en

I've created two buttons in this program and set their labels as "Right" and "Left".When you create a standalone label using gtk_label_new() you have access to the text used for the label with gtk_label_get_label().But when you create a button widget with a label using gtk_button_new_with_label() a label widget is created for you and packed into the button widget. The label widget becomes a child of the button widget.

A button is a type GtkBin — A container with just one child. The child in this case is the label widget. To get access to that label widget you can use gtk_bin_get_child() in your callback. Then use gtk_label_get_label() to get the text used for the button's label and g_str_equal() to compare the text returned by gtk_label_get_label(). If the text is "Left" then the left button was pressed and "You pressed the Left button" is printed to the screen. If the text is "Right" then the right button was pressed and "You pressed Right button" is printed.

You could also use: gtk_button_get_label() to get the label directly but this was a good opportunity to introduce gtk_bin_get_child() which works on all type GtkBin. Some GtkBin don't have labels such as GtkOverlay or GtkViewport.

Note for GtkAlignment, the manual says:

Quote:

Note that the desired effect can in most cases be achieved by using the "halign", "valign" and "margin" properties on the child widget, so GtkAlignment should not be used in new code.

In the first signals example I showed how to detect which button was pressed by using the labels on the buttons, but what if your buttons don't have any labels? What if they only have images as shown in the image above?You can use g_object_set_data() from the GObject library to set a unique integer ID for each button. Then use GPOINTER_TO_INT() in your callback to get the ID of the button that was pressed and take the appropriate action.

This example changes the menubar based on the currently selected notebook tab. When the notebook tab called "FirstTab" is selected, the menu that's displayed is "File" When you select the notebook tab called "SecondTab" the "File" menu is replaced with a menu called "Search"

Who is online

Users browsing this forum: No registered users and 2 guests

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