GTK + re-picking components in--09 GTK + (i)

Source: Internet
Author: User
Tags gtk

(a): written in front

In this article, we mainly introduce the various artifacts in the GTK + program, which is the first part of the tutorial, and the other part will be discussed in the next section. A component is the basis for building a GUI program. In the long-term development of GTK +, a number of specific artifacts are becoming common standards for most programming toolsets and almost all operating systems. For example, a button, a selection box, or a slider bar. For GTK +, we have built in a lot of common dedicated artifacts for our use.

(b): Gtkbutton

Gtkbutton is an easy-to-use widget that is often used to trigger an action.

Let's take a look at how Gtkbutton is used:

#include <gtk/gtk.h>intMainintArgc,char*ARGV[]) {Gtkwidget*window; Gtkwidget*fixed; Gtkwidget*button; Gtk_init (&AMP;ARGC,&AMP;ARGV);window= Gtk_window_new (Gtk_window_toplevel); Gtk_window_set_title (Gtk_widget (window),"Gtkbutton"); Gtk_window_set_default_size (Gtk_window (window), the, Max); Gtk_window_set_position (Gtk_window (window), Gtk_win_pos_center);    Fixed = Gtk_fixed_new (); Gtk_container_add (Gtk_container (window), fixed);Button= Gtk_button_new_with_label ("Quit"); Gtk_fixed_put (gtk_fixed (fixed),Button, -, -); Gtk_widget_set_size_request (Button, the, *); G_signal_connect (G_object (Button),"clicked", G_callback (Gtk_main_quit), G_object (window)); G_signal_connect_swapped (G_object (window),"Destroy", G_callback (Gtk_main_quit), NULL); Gtk_widget_show_all (window); Gtk_main ();return 0;}

In the example above, the display of a button is placed in a fixed container, and when we press the button, the program exits.

button = gtk_button_new_with_label("Quit");

This code generates a Gtkbutton component with a label.

Together with the events we learned in the previous section, add the corresponding events to the button, and you can:

g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(gtk_main_quit),G_OBJECT(window));

Let's look at the effect of the operation:

(c): Gtkcheckbutton

Gtkcheckbutton is also a component, he has two states, "on" and "off", open to indicate a visible floating check mark.

Let's take a look at his usage:

#include <gtk/gtk.h>voidToggle_title (Gtkwidget *widget,gpointerwindow){if(Gtk_toggle_button_get_active (Gtk_toggle_button (widget))) {Gtk_window_set_title (window,"Gtkcheckbutton"); }Else{Gtk_window_set_title (window,""); }}int Main (int Argc,char *argv[]) {Gtkwidget *window;    Gtkwidget *frame;    Gtkwidget *check; Gtk_init (&AMP;ARGC,&AMP;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), the, Max); Gtk_window_set_title (Gtk_window (window),"Gtkcheckbutton");    frame = Gtk_fixed_new (); Gtk_container_add (Gtk_container (window), frame); Check = Gtk_check_button_new_with_label ("Show title");    Gtk_toggle_button_set_active (Gtk_toggle_button (check), TRUE);    Gtk_widget_unset_flags (Check,gtk_can_focus); Gtk_fixed_put (Gtk_fixed (frame), check, -, -); G_signal_connect_swapped (G_object (window),"Destroy", G_callback (Gtk_main_quit), NULL); G_signal_connect (G_object (check),"clicked", G_callback (Toggle_title), (Gpointer)window); Gtk_widget_show_all (window); Gtk_main ();return 0;}

The function we want to show is that the display state of the title bar changes depending on the state of the component Gtkcheckbutton.

    check = gtk_check_button_new_with_label("Show title");    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check),TRUE);

A GtkCheckButton component is generated and is marked by default (that is, the state is open).

    GTK_WIDGET_UNSET_FLAGS(check,GTK_CAN_FOCUS);

This line of code cancels the default lock on the check box, so it's easy to do so because we can arbitrarily set the check box to "hood" the previous box.

The top listener function is the key to implement the function: In this way, the display state of the title bar varies according to the state of the component Gtkcheckbutton.

Let's look at the effect of the operation:

(d): Gtkframe

Gtkframe is a decorative frame and can also be used to set a label (optional).

Let's take a look at how gtkframe is used, and how it works after it's used:

#include <gtk/gtk.h>int main (int argc,char *argv[]) {Gtkwidget *window;    Gtkwidget *table;    Gtkwidget *frame1;    Gtkwidget *frame2;    Gtkwidget *frame3;    Gtkwidget *frame4;    Gtk_init (&AMP;ARGC,&AMP;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),"Gtkframe"); Gtk_container_Set_border_width (Gtk_container (window),Ten); Table = Gtk_table_new (2,2, TRUE); Gtk_table_Set_row_spacings (Gtk_table (TABLE),Ten); Gtk_table_Set_col_spacings (Gtk_table (TABLE),Ten);    Gtk_container_add (Gtk_container (window), table); Frame1 = Gtk_frame_new ("Shadow in"); Gtk_frame_Set_shadow_type(Gtk_frame (frame1), gtk_shadow_in); Frame2 = Gtk_frame_new ("Shadow out"); Gtk_frame_Set_shadow_type(Gtk_frame (frame2), gtk_shadow_out); Frame3 = Gtk_frame_new ("Shadow etched in"); Gtk_frame_Set_shadow_type(Gtk_frame (Frame3), gtk_shadow_etched_in); Frame4 = Gtk_frame_new ("Shadow etched out"); Gtk_frame_Set_shadow_type(Gtk_frame (FRAME4), gtk_shadow_etched_out); Gtk_table_attach_defaults (gtk_table (table), Frame1,0,1,0,1); Gtk_table_attach_defaults (gtk_table (table), Frame2,0,1,1,2); Gtk_table_attach_defaults (gtk_table (table), Frame3,1,2,0,1); Gtk_table_attach_defaults (gtk_table (table), Frame4,1,2,1,2); g_signal_connect_swapped (G_object (window),"Destroy", G_callback (Gtk_main_quit), g_object (window));    Gtk_widget_show_all (window); Gtk_main ();return 0;}

This example shows four different styles of frame frames. These framework artifacts are not laid out using tables.

 frame1 = gtk_frame_new("Shadow In"); gtk_frame_set_shadow_type(GTK_FRAME(frame1),GTK_SHADOW_IN);

We created a GtkFrame widget and also set a shadow type for him.

(v): Gtklabel

Gtklabel's function is obvious, is used to display the text, of course, he will also support the markup syntax, we show here two examples:

1: Use of ordinary Gtklabel

#Include <gtk/gtk.h>int main (int Argc,char *argv[]){Gtkwidget *window;    Gtkwidget *label; 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_title (Gtk_window (window), "Nymohetamine");    Gtk_window_set_default_size (Gtk_window (window), 350,400); Label = Gtk_label_new ("Cold was my soul\ nUntold was the pain\ nI faced when you left me\ nA rose in the rain ....\ nSo I swore to the razor\ nThat never,enchained\ nWould your dark nails of faith\ nBe pushed through my veins again\ n    \ nBared on your tomb\ nI ' m A Prayer for your loneliness\ nAnd would you ever soon\ nCome above onto me?\ nOn the binds of your lowliness\ nI could always find the slots for your Sacred key ");    Gtk_label_set_justify (Gtk_label (label), Gtk_justify_center);    Gtk_container_add (Gtk_container (window), label);    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 a lyric, centered, let's see how it works:

2: Label that supports markup syntax

Let's show how Gtklabel supports markup syntax:

#include <gtk/gtk.h>int main (int argc,char *argv[]) {Gtkwidget *window;    Gtkwidget *label; Gtk_init (&AMP;ARGC,&AMP;ARGV);window= Gtk_window_new (Gtk_window_toplevel); Gtk_window_set_position (Gtk_window (window), Gtk_win_pos_center); Gtk_window_set_title (Gtk_window (window),"Markup Label"); Char *str ="<b>zetcode</b>,knowledge Only Matters";    Label = Gtk_label_new (NULL);    Gtk_label_set_markup (Gtk_label (label), str);    Gtk_label_set_justify (Gtk_label (label), Gtk_justify_center); Gtk_container_add (Gtk_container (window), label);    Gtk_widget_show (label); Gtk_window_set_default_size (Gtk_window (window), -, -); G_signal_connect (window,"Destroy", G_callback (Gtk_main_quit), NULL); Gtk_widget_show (window); Gtk_main ();return 0;}

The following is a label that supports the markup syntax effect:

(vi): written in the back

Here, we'll start with a few of the common GTK + artifacts, and we'll go through the other useful artifacts in GTK + later in the next section.

Note: Download the code

GTK + re-picking components in--09 GTK + (i)

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.