Using the previous section, basically mastering how to add a menu bar to a window and add a shortcut key for Menu_item, let's create a selection menu option from an instance. Gtkcheckmenuitem is a menu option that can be generated with a selection.
Here's a look at the code:
#include <gtk/gtk.h>void Toggle_statusbar (Gtkwidget*widget, Gpointer StatusBar) {if(Gtk_check_menu_item_get_active (Gtk_check_menu_item (widget))) {gtk_widget_show (StatusBar); }Else{gtk_widget_hide (StatusBar); }}intMainintArgc,char*ARGV[]) {Gtkwidget*window; Gtkwidget*vbox; Gtkwidget*menubar; Gtkwidget*viewmenu; Gtkwidget*view; Gtkwidget*tog_stat; Gtkwidget*statusbar; Gtk_init (&ARGC,&ARGV); window = Gtk_window_new (gtk_window_toplevel); Gtk_window_set_position (Gtk_window (window), gtk_win_pos_center); Gtk_window_set_default_size (Gtk_window (window), -, $); Gtk_window_set_title (Gtk_window (window),"View status bar"); VBox = Gtk_vbox_new (FALSE,0); Gtk_container_add (Gtk_container (window), vbox); menubar = Gtk_menu_bar_new (); Viewmenu = Gtk_menu_new (); View = Gtk_menu_item_new_with_label ("View"); Tog_stat = Gtk_check_menu_item_new_with_label ("View Statusbar"); Gtk_check_menu_item_set_active (Gtk_check_menu_item (Tog_stat), TRUE); Gtk_menu_item_set_submenu (Gtk_menu_item (view), Viewmenu); Gtk_menu_shell_append (Gtk_menu_shell (Viewmenu), tog_stat); Gtk_menu_shell_append (Gtk_menu_shell (menubar), view); Gtk_box_pack_start (Gtk_box (VBox), Menubar,false,false,3); StatusBar = Gtk_statusbar_new (); Gtk_box_pack_end (Gtk_box (VBox), Statusbar,false,true,1); g_signal_connect_swapped (G_object (window),"Destroy", G_callback (Gtk_main_quit), NULL); G_signal_connect (G_object (Tog_stat),"Activate", G_callback (Toggle_statusbar), statusbar); Gtk_widget_show_all (window); Gtk_main ();return 0;}
The makefile file is the same as the above section. First, let's look at the effect after the run:
This is the menu item with the option box.
Our example means that when the menu item is selected, the status bar is displayed, and if unchecked, the status bar is hidden.
The function Gtk_check_menu_item_new_with_label () can generate a new menu with a selection box
Single option.
Well, the use of the menu bar is roughly the same, let's look at how the toolbar is used.
#include <gtk/gtk.h>intMainintArgc,char*ARGV[]) {Gtkwidget*window; Gtkwidget*vbox; Gtkwidget*toolbar; Gtktoolitem*new; Gtktoolitem*open; Gtktoolitem*save; Gtktoolitem*sep; Gtktoolitem*exit; Gtk_init (&ARGC,&ARGV); window = Gtk_window_new (gtk_window_toplevel); Gtk_window_set_position (Gtk_window (window), gtk_win_pos_center); Gtk_window_set_default_size (Gtk_window (window), -, $); Gtk_window_set_title (Gtk_window (window),"Toolbar"); VBox = Gtk_vbox_new (FALSE,0); Gtk_container_add (Gtk_container (window), vbox); toolbar = gtk_toolbar_new (); Gtk_toolbar_set_style (Gtk_toolbar (toolbar), gtk_toolbar_icons); Gtk_container_set_border_width (Gtk_container (toolbar),2); New = Gtk_tool_button_new_from_stock (gtk_stock_new); Gtk_toolbar_insert (Gtk_toolbar (toolbar), new,-1);Open= Gtk_tool_button_new_from_stock (Gtk_stock_open); Gtk_toolbar_insert (Gtk_toolbar (toolbar),Open,-1); Save = Gtk_tool_button_new_from_stock (gtk_stock_new); Gtk_toolbar_insert (Gtk_toolbar (toolbar), save,-1); Sep = gtk_separator_tool_item_new (); Gtk_toolbar_insert (Gtk_toolbar (toolbar), sep,-1);Exit= Gtk_tool_button_new_from_stock (gtk_stock_quit); Gtk_toolbar_insert (Gtk_toolbar (toolbar),Exit,-1); Gtk_box_pack_start (Gtk_box (VBox), Toolbar,false,false,5); G_signal_connect (G_object (Exit),"clicked", G_callback (Gtk_main_quit), NULL); g_signal_connect_swapped (G_object (window),"Destroy", G_callback (Gtk_main_quit), NULL); Gtk_widget_show_all (window); Gtk_main ();return 0;}
Here's how the toolbar works after it's run:
toolbar = gtk_toolbar_new ();
Gtk_toolbar_set_style (Gtk_toolbar (toolbar), gtk_toolbar_icons);
As you can see from the above two lines of code, a toolbar with a picture display is generated.
New = Gtk_tool_button_new_from_stock (gtk_stock_new);
Gtk_toolbar_insert (Gtk_toolbar (toolbar), new,-1);
A new tool button is generated from the stock to be added to the toolbar, only the function Gtk_toolbar_insert () is required.
Sep = gtk_separator_tool_item_new ();
Gtk_toolbar_insert (Gtk_toolbar (toolbar), sep,-1);
The code generates a split line separating the toolbars.
This creates the toolbars that we often see.
Below we learn a function failure, that is, a button in the toolbar to invalidate, that is, the item cannot be manipulated.
Take a look at the code below:
#include <gtk/gtk.h>#include <string.h>void Undo_redo (Gtkwidget*widget, Gpointer item) {StaticintCount =2; const CHAR*name= Gtk_widget_get_name (widget);if(strcmp (Name,"undo")) {count++; }Else{count--; }if(Count <0) {gtk_widget_set_sensitive (widget,false); Gtk_widget_set_sensitive (item,true); }if(Count >5) {gtk_widget_set_sensitive (widget,false); Gtk_widget_set_sensitive (item,true); }}intMainintArgc,char*ARGV[]) {Gtkwidget*window; Gtkwidget*vbox; Gtkwidget*toolbar; Gtkwidget*undo; Gtkwidget*redo; Gtkwidget*sep; Gtkwidget*exit; Gtk_init (&ARGC,&ARGV); window = Gtk_window_new (gtk_window_toplevel); Gtk_window_set_position (Gtk_window (window), gtk_win_pos_center); Gtk_window_set_default_size (Gtk_window (window), -, $); Gtk_window_set_title (Gtk_window (window),"Undoredo"); VBox = Gtk_vbox_new (FALSE,0); Gtk_container_add (Gtk_container (window), vbox); toolbar = gtk_toolbar_new (); Gtk_toolbar_set_style (Gtk_toolbar (toolbar), gtk_toolbar_icons); Gtk_container_set_border_width (Gtk_container (toolbar),2); Undo = Gtk_tool_button_new_from_stock (Gtk_stock_undo); Gtk_widget_set_name (Gtk_widget (undo),"undo"); Gtk_toolbar_insert (Gtk_toolbar (toolbar), undo,-1);Redo= Gtk_tool_button_new_from_stock (Gtk_stock_redo); Gtk_toolbar_insert (Gtk_toolbar (toolbar),Redo,-1); Sep = gtk_separator_tool_item_new (); Gtk_toolbar_insert (Gtk_toolbar (toolbar), sep,-1);Exit= Gtk_tool_button_new_from_stock (gtk_stock_quit); Gtk_toolbar_insert (Gtk_toolbar (toolbar),Exit,-1); Gtk_box_pack_start (Gtk_box (VBox), Toolbar,false,false,5); G_signal_connect (G_object (undo),"clicked", G_callback (Undo_redo),Redo); G_signal_connect (G_object (Redo),"clicked", G_callback (Undo_redo), undo); G_signal_connect (G_object (Exit),"clicked", G_callback (Gtk_main_quit), NULL); g_signal_connect_swapped (G_object (window),"Destroy", G_callback (Gtk_main_quit), NULL); Gtk_widget_show_all (window); Gtk_main ();return 0;}
Here is the result after the run
Gtk_widget_set_sensitive () is a toolbar button that is used to tell the computer whether to hit live.
Let's take a look at an official API for this toolbar:
1): Gtk_container_set_border_width (Gtk_container (Toolbar), 2);
Function Prototypes:
voidgtk_container_set_border_width (GtkContainer *container, guint border_width);
According to the official document introduction:
Sets the border width of the container.
The border width of a container is the amount of space to leave around the outside of the container. The only exception to this is Gtkwindow; Because toplevel windows can ' t leave space outside, they leave the space inside. The border is added on all sides of the container. To add space to only one side, use a specific "margin" in the child widget, for example "Margin-top".
In other words, the function is used to set the container boundary width.
The function is to set the width of all boundaries, and if you want to just set a boundary, you can use margin, such as "Margin-top".
Attention:
Since I was studying GTK, I looked at other people's books, based on 2.0, but now the GTK version has been upgraded to 3.0, many methods have changed, but these methods are still available.
As an example:
Function:
gtk_tool_button_new_from_stock ()
That's what the official says:
Gtk_tool_button_new_from_stock have been deprecated since version 3.10 and should not being used in Newly-written code.
Use Gtk_tool_button_new () instead.
Let's take a look at the use of function gtk_tool_button_new ()
Its function prototypes are:
*icon_widget, *label);
Create a new Gtktoolbutton using Icon_widget as the content and using label as a label
Parameters:
? label a string that is a label or null
? Icon_widget a widget that will be used as the content of the button, or null
Return
? A new Gtktoolbutton.
Starting with version 2.4
GTK Learning 3--menu bar and toolbars