GTK gossip: gtkuimanager

Source: Internet
Author: User
Tags gtk xml example
Create a user interface, such as a structure selection or a tool column, by using a programming program, you can use gtkuimanager to define an interface definition from one or more user interfaces and automatically create corresponding GTK components, the user interface defines an XML example.

Let's take a look at some practical examples.
Gtkmenubar, gtkmenu, and gtkmenuitem


In the example, gtkuimanager and XML are used to define the same effect. If the XML definition is as follows:

  • Gtk_ui_manager.xml
<ui>
<menubar name="MenuBar">
<menu action="File">
<menuitem action="Open"/>
<menuitem action="Save"/>
<separator/>
<menuitem action="Close"/>
</menu>
<menu action="Edit">
<menuitem action="Cut"/>
<menuitem action="Copy"/>
<menuitem action="Paste"/>
</menu>
<menu action="Help">
<menuitem action="About"/>
</menu>
</menubar>
</ui>

"Name" features allow you to obtain the corresponding GTK component based on the name when creating the program, and "action" will apply to gtkaction, you can use gtkactionentry to create the gtkaction. The definition of gtkactionentry is as follows:

Typedef struct {

Const gchar * Name;

Const gchar * stock_id;

Const gchar * label;

Const gchar * accelerator;

Const gchar * tooltip;

Gcallback callback;

} Gtkactionentry;

The first name operator defines the "name" encoding in the response. The others are the representation, text, quick response, prompt, and callback function, a preset is as follows:

Static gtkactionentry entries [] = {

{"File", null, "_ file "},

{"Open", gtk_stock_open, "open ",

"<Control> O", "Open File", g_callback (itempressed )},

{"Save", gtk_stock_save, "save ",

"<Control> S", "save file", g_callback (itempressed )},

{"Close", gtk_stock_quit, "close ",

"<Control> q", "Close file", g_callback (gtk_main_quit )},

{"Edit", null, "_ edit "},

{"Cut", null, "copy "},

{"Copy", null, "copy "},

{"Paste", null, "Paste "},

{"Help", null, "_ Help "},

{"About", null, "about "}

};

Gtkaction is grouped into gtkactiongrouop. After defining the gtkactionentry, you can use the gtk_action_group_add_actions () function to add it to the gtkactiongroup:

Gtk_action_group_add_actions (Actiongroup, entries, 10, null );


Create the gtkuimanager and use
Add gtk_ui_manager_insert_action_group () to the gtkactiongroup, and then use
Gtk_ui_manager_add_ui_from_file () define the definition of the user interface:
Gtkuimanager * UI = gtk_ui_manager_new ();

Gtk_ui_manager_insert_action_group (ui, Actiongroup, 0 );

Gtk_ui_manager_add_ui_from_file (ui, "gtk_ui_manager.xml", null );

Gtkuimanager automatically creates the GTK component corresponding to the structure and creates the corresponding gtkaction according to the action.

To retrieve components from gtkuimanager, you can use gtk_ui_manager_get_widget () and set it according to the "name" identifier. For example, you can get "menubar" and add it to gtkvbox:

Gtkwidget * vbox = gtk_vbox_new (false, 5 );


Gtk_box_pack_start (gtk_box (vbox ),


Gtk_ui_manager_get_widget (ui, "/menubar"), false, false, 2 );

The following program is a complete example:

  • Gtk_ui_manager_demo.c
#include <gtk/gtk.h>

void itemPressed(GtkMenuItem *menuItem, gpointer data) {
g_print("%s/n", gtk_action_get_name(GTK_ACTION(menuItem)));
}

static GtkActionEntry entries[] = {
{ "File", NULL, "_File" },
{ "Open", GTK_STOCK_OPEN, "Open",
"<control>O", "Open File", G_CALLBACK(itemPressed)},
{ "Save", GTK_STOCK_SAVE, "Save",
"<control>S", "Save File", G_CALLBACK(itemPressed)},
{ "Close", GTK_STOCK_QUIT, "Close",
"<control>Q", "Close File", G_CALLBACK(gtk_main_quit)},
{ "Edit", NULL, "_Edit" },
{ "Cut", NULL, "Copy"},
{ "Copy", NULL, "Copy"},
{ "Paste", NULL, "Paste"},
{ "Help", NULL, "_Help" },
{ "About", NULL, "About" }
};

int main(int argc, char *argv[]) {
GtkWidget *window;
GtkActionGroup *actionGroup;
GtkUIManager *ui;
GtkWidget *vbox;

gtk_init(&argc, &argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "GtkUIManager");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);

actionGroup = gtk_action_group_new("Actions");
gtk_action_group_add_actions(actionGroup, entries, 10, NULL);

ui = gtk_ui_manager_new();
gtk_ui_manager_insert_action_group(ui, actionGroup, 0);
gtk_ui_manager_add_ui_from_file(ui, "gtk_ui_manager.xml", NULL);

vbox = gtk_vbox_new(FALSE, 5);
gtk_box_pack_start(GTK_BOX(vbox),
gtk_ui_manager_get_widget(ui, "/MenuBar"), FALSE, FALSE, 2);

gtk_container_add(GTK_CONTAINER(window), vbox);

g_signal_connect(GTK_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);

gtk_widget_show_all(window);

gtk_main();

return 0;
}

The following figure shows the details of a line:


For more information, refer to the gtkuimanager documentation.
Or the UI manager example in GTK-demo.


 



Bloggerads warning
Genuine iPhone msn
Finally
The official edition features a complete set of functions. It is safe to use and free of trial!
More messages ..
Will it dry in winter, and I am afraid to wipe the lotion?
When a man's baby falls into the insurance market, the man's "Oil Control and oil control" oil control and maintenance are done once! You can select a full range of products immediately!
More messages ..
Impr


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.