Tool bar in GTK +

Source: Internet
Author: User
Tags gtk

Tool bar in GTK +

| Views: 1184

The toolbar (toolbars) is often used to group components, which simplifies the customization of their appearance and layout. In typical cases, the toolbar consists of buttons with icons, labels, and tooltip. However, other components can also be placed in the toolbar. The toolbar components can be horizontally or vertically arranged, icons or labels can be displayed, or both are displayed.

Use the following function to create a toolbar (some people may have guessed it ):

Gtkwidget * gtk_toolbar_new (void );

After creating a toolbar, You can append, forward, and insert toolbar items (simple text strings) or elements (any component type) to it ). To describe an object on a toolbar, you need a label text, a tooltip text, a private tooltip text, an icon, and a callback function. For example, to insert or append a button in front, use the following function:

Gtkwidget * gtk_toolbar_append_item (gtktoolbar * toolbar,

Const char * text,

Const char * tooltip_text,

Const char * tooltip_private_text,

Gtkwidget * icon,

Gtksignalfunc callback,

Gpointer user_data );

Gtkwidget * gtk_toolbar_prepend_item (gtktoolbar * toolbar,

Const char * text,

Const char * tooltip_text,

Const char * tooltip_private_text,

Gtkwidget * icon,

Gtksignalfunc callback,

Gpointer user_data );

If you want to use gtk_toolbar_insert_item (), in addition to the parameters to be specified in the above function, you must specify the position of the inserted object, as follows:

Gtkwidget * gtk_toolbar_insert_item (gtktoolbar * toolbar,

Const char * text,

Const char * tooltip_text,

Const char * tooltip_private_text,

Gtkwidget * icon,

Gtksignalfunc callback,

Gpointer user_data,

Gint position );

To simply add a blank area between toolbar items, you can use the following function:

Void gtk_toolbar_append_space (gtktoolbar * toolbar );

Void gtk_toolbar_prepend_space (gtktoolbar * toolbar );

Void gtk_toolbar_insert_space (gtktoolbar * toolbar,

Gint position );

If necessary, you can set the placement direction of the toolbar and its style by using the following function at runtime:

Void gtk_toolbar_set_orientation (gtktoolbar * toolbar,

Gtkorientation orientation );

Void gtk_toolbar_set_style (gtktoolbar * toolbar,

Gtktoolbarstyle style );

Void gtk_toolbar_set_tooltips (gtktoolbar * toolbar,

Gint enable );

The preceding orientation parameter is gtk_orientation_horizontal or gtk_orientation_vertical. The style parameter is used to set the appearance of a toolbar item. You can use gtk_toolbar_icons, gtk_toolbar_text, or gtk_toolbar_both.

To understand what the toolbar can do, take a look at the following Program (we have inserted some explanations between the Code ):

# Include

/* This function connects to the close button or closes the window event from the window manager */

Gint delete_event (gtkwidget * widget, gdkevent * event, gpointer data)

{

Gtk_main_quit ();

Return false;

}

The above code is not much different from other GTK applications. The difference is that we include a beautiful XPM image, which is used as an icon for all buttons.

Gtkwidget * close_button;/* This button will trigger a signal

* Close the application */

Gtkwidget * tooltips_button;/* enable/disable tooltip */

Gtkwidget * text_button,

* Icon_button,

* Both_button;/* change the single-choice button in the toolbar style */

Gtkwidget * entry;/* a text input component used to demonstrate that any component can be assembled

* In the toolbar */

In fact, not all the components above are necessary. I put them together to make things clearer.

/* Very simple... when the button changes the status, we can check which button is active and set the style of the toolbar accordingly.

* Note: The toolbar is passed to the callback function as the number of users! */

Void radio_event (gtkwidget * widget, gpointer data)

{

If (gtk_toggle_button (text_button)-> active)

Gtk_toolbar_set_style (gtk_toolbar (data), gtk_toolbar_text );

Else if (gtk_toggle_button (icon_button)-> active)

Gtk_toolbar_set_style (gtk_toolbar (data), gtk_toolbar_icons );

Else if (gtk_toggle_button (both_button)-> active)

Gtk_toolbar_set_style (gtk_toolbar (data), gtk_toolbar_both );

}

/* Simpler. Check the status of the specified switch button. enable or disable the tooltip accordingly */

Void toggle_event (gtkwidget * widget, gpointer data)

{

Gtk_toolbar_set_tooltips (gtk_toolbar (data ),

Gtk_toggle_button (widget)-> active );

}

The above is only the two callback functions to be called when a button on the toolbar is pressed. You should be familiar with these things if you have used the switch button (and single choice button ).

Int main (INT argc, char * argv [])

{

/* The following is the main window (a dialog box) and a handlebox )*/

Gtkwidget * dialog;

Gtkwidget * handlebox;

/* Well, we need a toolbar, an icon with a mask (mask) (all buttons share a mask) and

* A Chart icon component (but we will create a split component for each button )*/

Gtkwidget * toolbar;

Gtkwidget * iconw;

/* This is called in all GTK programs. */

Gtk_init (& argc, & argv );

/* Create a new window with the given title and size */

Dialog = gtk_dialog_new ();

Gtk_window_set_title (gtk_window (DIALOG), "gtktoolbar tutorial ");

Gtk_widget_set_size_request (gtk_widget (DIALOG), 600,300 );

Gtk_window (DIALOG)-> allow_shrink = true;

/* Exit when closing the window */

G_signal_connect (g_object (DIALOG), "delete_event ",

G_callback (delete_event), null );

/* The window needs to be instantiated, because we need to set an image for the toolbar in its content */

Gtk_widget_realize (DIALOG );

/* Place the toolbar on a handle box,

* In this way, it can be removed from the main window */

Handlebox = gtk_handle_box_new ();

Gtk_box_pack_start (gtk_box (gtk_dialog (DIALOG)-> vbox ),

Handlebox, false, false, 5 );

The above code is similar to any other GTK application. They initialize GTK and create the main window. The only thing that needs to be explained is: A handle box ). The handle box is just a box where components can be assembled. The difference between it and a normal box is that it can be removed from a parent window (in fact, the handle box is kept on the parent window, but it is reduced to a very small rectangle, at the same time, all its content is placed on a new freely moving floating window ). Having a floating toolbar makes people feel very good, so these two components are often used at the same time.

/* Set the toolbar to horizontal with icons and text

* The distance between each item is 5 pixels,

* In addition, we also put it on the handle box */

Toolbar = gtk_toolbar_new ();

Gtk_toolbar_set_orientation (gtk_toolbar (toolbar), gtk_orientation_horizontal );

Gtk_toolbar_set_style (gtk_toolbar (toolbar), gtk_toolbar_both );

Gtk_container_set_border_width (gtk_container (toolbar), 5 );

Gtk_toolbar_set_space_size (gtk_toolbar (toolbar), 5 );

Gtk_container_add (gtk_container (handlebox), toolbar );

The code above initializes the toolbar component.

/* The first button in the toolbar */

Iconw = gtk_image_new_from_file ("GTK. XPM");/* icon component */

Close_button =

Gtk_toolbar_append_item (gtk_toolbar (toolbar),/* toolbar */

"Close",/* button label */

"Closes this app",/* button tooltip */

"Private",/* private information prompted by the tool */

Iconw,/* icon component */

Gtk_signal_func (delete_event),/* one signal */

Null );

Gtk_toolbar_append_space (gtk_toolbar (toolbar);/* blank after toolbar entry */

In the code above, you can see the simplest situation: Add a button on the toolbar. Before appending a new toolbar item, you must construct an image component to use as the icon for this item. In this step, repeat Each toolbar item. You also need to increase the interval between toolbar items so that the toolbar items will not be next to each other. As you can see, gtk_toolbar_append_item () returns a pointer to the newly created button component, so we can use it normally.

/* Now, we create a single-choice button group ...*/

Iconw = gtk_image_new_from_file ("GTK. XPM ");

Icon_button = gtk_toolbar_append_element (

Gtk_toolbar (toolbar ),

Gtk_toolbar_child_radiobutton,/* element type */

Null,/* pointer to component */

"Icon",/* tag */

"Only icons in toolbar",/* tooltip */

"Private",/* Private string prompted by the tool */

Iconw,/* icon */

Gtk_signal_func (radio_event),/* signal */

Toolbar);/* signal transmission data */

Gtk_toolbar_append_space (gtk_toolbar (toolbar ));

Here we start to create a single-choice button group. Use gtk_toolbar_append_element. In fact, with this function, we can add simple toolbar items or blank intervals (type: gtk_toolbar_child_space or gtk_toolbar_child_button ). In the preceding example, a single-choice button group is created. To create other radio buttons for this group, you need a pointer to the previous button, so that the list of buttons can be easily organized (see the radio button section in the previous sections of this document ).

/* The Single-choice button behind it references the one created earlier */

Iconw = gtk_image_new_from_file ("GTK. XPM ");

Text_button =

Gtk_toolbar_append_element (gtk_toolbar (toolbar ),

Gtk_toolbar_child_radiobutton,

Icon_button,

"Text ",

"Only texts in toolbar ",

"Private ",

Iconw,

Gtk_signal_func (radio_event ),

Toolbar );

Gtk_toolbar_append_space (gtk_toolbar (toolbar ));

Iconw = gtk_image_new_from_file ("GTK. XPM ");

Both_button =

Gtk_toolbar_append_element (gtk_toolbar (toolbar ),

Gtk_toolbar_child_radiobutton,

Text_button,

"Both ",

"Icons and text in toolbar ",

"Private ",

Iconw,

Gtk_signal_func (radio_event ),

Toolbar );

Gtk_toolbar_append_space (gtk_toolbar (toolbar ));

Gtk_toggle_button_set_active (gtk_toggle_button (both_button), true );

Finally, we must manually set the status of one of the buttons (otherwise they are all active and prevent us from making choices between them ).

/* The following is a simple switch button */

Iconw = gtk_image_new_from_file ("GTK. XPM ");

Tooltips_button =

Gtk_toolbar_append_element (gtk_toolbar (toolbar ),

Gtk_toolbar_child_togglebutton,

Null,

"Tooltips ",

"Toolbar with or without tips ",

"Private ",

Iconw,

Gtk_signal_func (toggle_event ),

Toolbar );

Gtk_toolbar_append_space (gtk_toolbar (toolbar ));

Gtk_toggle_button_set_active (gtk_toggle_button (tooltips_button), true );

The method for creating the switch button is obvious (if you already know how to create a single button ).

/* To assemble a component into the toolbar, you only need to create it and then chase it

* Add the tool to the toolbar and set the appropriate tooltip */

Entry = gtk_entry_new ();

Gtk_toolbar_append_widget (gtk_toolbar (toolbar ),

Entry,

"This is just an entry ",

"Private ");

/* Because it is not created by the toolbar, We need to display it */

Gtk_widget_show (entry );

As you can see, it is very easy to add any component to the toolbar. The only thing to remember is that this component must be displayed manually (on the contrary, the toolbar items created by the toolbar are displayed along with the toolbar ).

/* Okay. Now we can display everything */

Gtk_widget_show (toolbar );

Gtk_widget_show (handlebox );

Gtk_widget_show (DIALOG );

/* Enter the main loop and wait for user operations */

Gtk_main ();

Return 0;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.