GTK/Glade Program Development (2)

Source: Internet
Author: User
Tags gtk

Spin buttons)

Common functions:

Create a widget:
/* "Adjust object" is used to create the fine-tuning button component. Therefore, you must create an "Adjust object"
The following are the fine-tuning button components automatically created by glade, and the "Adjust object" is used "*/


GtkObject *spinbutton1_adj;

GtkWidget *spinbutton1;



spinbutton1_adj = gtk_adjustment_new (1, 0, 100, 1, 10, 10);

spinbutton1 = gtk_spin_button_new (GTK_ADJUSTMENT (spinbutton1_adj), 1, 0);

The new functions used in this section:
Obtain the current value of the Widget:
Function Name: gtk_spin_button_get_value_as_int
Usage:

            gfloat  spinvalue1;

gint spinvalue2;

spinvalue1=gtk_spin_button_get_value(GTK_SPIN_BUTTON(spinbutton));

spinvalue2=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spinbutton));

Function Name: sprintf ()
Usage: sprintf (BUF, "% d", spinvalue1 );

Function Name: lookup_widget ()
Usage: gtkwidget * label1 = lookup_widget (gtk_widget (spinbutton), "label1 ");

1. Use glade to design the interface
Step: 1. Create a glade Project
2. Place the window (400,300), fixed position, tag, and fine-tune button accordingly.
3. For the fine-tuning button component, add the value_changed signal response, and enter "spinbutton1" after the object.
4. Set attributes of the fine-tuning button: value, minimum value, maximum value, step increment, page increment, and page size. (What are the meanings of these attributes .)
5. Save the project and generate code.

Ii. modify the code

Interface. c/* is not modified. The parts in your file are the same as those in this file. */


G_signal_connect_swapped (gpointer) spinbutton1, "value_changed ",

G_callback (on_spinbutton#value_changed ),

Gtk_object (spinbutton1 ));



Callbacks. h/* does not make any changes. Check that your file is the same as this one. */



Void on_spinbutton#value_changed (gtkspinbutton * spinbutton,

Gpointer user_data );

Callbacks. c



Void

On_spinbutton1_value_changed (gtkspinbutton * spinbutton,

Gpointer user_data)

{

Gchar Buf [10];

Gint spinvalue1;



Gtkwidget * label1 = lookup_widget (gtk_widget (spinbutton), "label1 ");

Spinvalue1 = gtk_spin_button_get_value_as_int (gtk_spin_button (spinbutton ));

Sprintf (BUF, "% d", spinvalue1 );

Gtk_label_set_text (gtk_label (label1), Buf );

}

Drawing area component (drawingarea)

Common functions:

Create component:
/**/
Gtkwidget * drawingarea1;
Drawingarea1 = gtk_drawing_area_new ();

The new functions used in this section:

Function Name: gdk_gc_new/* New paint brush */
Usage:
Gdkgc * gc1;
Gc1 = gdk_gc_new (drawingarea1-> window );

Function Name: gdk_gc_set_rgb_fg_color/* set the foreground color of the paint brush */
Usage:
Gdkcolor color;
Color. Red = 30000;
Gdk_gc_set_rgb_fg_color (gc1, & color );

Function Name: gdk_draw_line/* draw a straight line */
Function Name: gdk_draw_rectangle/* draw a rectangle */

1. Use glade to design the interface
Step: 1. Create a glade Project
2. Place the window (400,300), fixed position, button, and drawing area accordingly.
3. Add the clicked signal response for the button component.
4. Save the project and generate code.

Ii. modify the code

Interface. c/* is not modified. The parts in your file are the same as those in this file. */


g_signal_connect ((gpointer) button1, "clicked",

G_CALLBACK (on_button1_clicked),

NULL);

Callbacks. h/* does not make any changes. Check that your file is the same as this one. */

Callbacks. c


Void

On_button1_clicked (gtkbutton * button,

Gpointer user_data)

{

Gtkwidget * drawingarea1 = lookup_widget (gtk_widget (button), "drawingarea1 ");

Gdkgc gc1;/* define paint brush */

Gdkcolor color;/* define the color variable */

Gc1 = gdk_gc_new (drawingarea1-> window);/* Create a paint brush */



Color. Red = 30000;/* define the color */

Gdk_gc_set_rgb_fg_color (gc1, & color);/* set the foreground color of the paint brush */

Gdk_draw_line (drawingarea1-> window,/* draw a straight line */

Gc1,

0, 0, 0,300 );



Color. Red = 0;

Color. Blue = 30000;/* redefine the color */

Gdk_gc_set_rgb_fg_color (gc1, & color);/* set the foreground color of the paint brush */



Gdk_draw_rectangle (drawingarea1-> window,/* draw a rectangle */

Gc1,

True,

50,

50,

100,

100 );

}

File chooser Dialog)

Common functions:

Create component:
/**/
Gtkwidget window2;
Window2 = create_filechooserdialog1 ();
Gtk_widget_show (window2 );

The new functions used in this section:

Function Name: gtk_file_chooser_get_filename/* get the file name */
Usage:

Gchar * filename;
Filename = gtk_file_chooser_get_filename (gtk_file_chooser (window2 ));

Note: This section involves calling the dialog box in the main window. Therefore, a global pointer variable is defined in callbacks. h to point to the dialog box.

1. Use glade to design the interface
Step: 1. Create a glade Project
2. The placement window (400,300), fixed position, button, and file selection dialog box are as follows.
3. Add a clicked signal response for the "cancel" and "open" buttons in the button component and file selection dialog box.
4. Save the project and generate code.

Ii. modify the code
Before modifying the Code, set the project property to "Win32 console program"


Main. c

Remove the following parts:/* This part is not included in the anjuta ide design in Linux. */

Gtkwidget * fontselectiondialog1;

Fontselectiondialog1 = create_fontselectiondialog1 ();

Gtk_widget_show (fontselectiondialog1 );



Interface. c/* is not modified. The parts in your file are the same as those in this file. */



Create_window1

G_signal_connect (gpointer) button1, "clicked ",

G_callback (on_button1_clicked ),

Null );



Create_filechooserdialog1

G_signal_connect (gpointer) button2, "clicked ",

G_callback (on_button2_clicked ),

Null );

G_signal_connect (gpointer) button3, "clicked ",

G_callback (on_button3_clicked ),

Null );



Callbacks. h. */



Gchar * filename;/* is used to store the obtained file name */(global variable)

Gtkwidget * window2;/* points to the pointer variable (global) in the dialog box and uses it to access the dialog box. */



Void on_button1_clicked (gtkbutton * button,

Gpointer user_data );

Void on_button2_clicked (gtkbutton * button,

Gpointer user_data );

Void on_button3_clicked (gtkbutton * button,

Gpointer user_data );



Callbacks. c



Void on_button1_clicked (gtkbutton,/* Corresponds to the button in the main window, used to create a dialog box */

Gpointer user_data)

{

Window2 = create_filechooserdialog1 ();

Gtk_widget_show (window2 );

}



Void on_button2_clicked (gtkbutton * button,/* "cancel" button */

Gpointer user_data)

{

G_print ("no file is select/N ");

Gtk_widget_destroy (window2);/* remove dialog box */

}



Void on_button3_clicked (gtkbutton,/* "open" button on the dialog box */

Gpointer user_data)

{

Filename = gtk_file_chooser_get_filename (gtk_file_chooser (window2);/* get the file name in the dialog box */

G_print ("the file is:/N ");

G_print (filename );

Gtk_widget_destroy (window2);/* remove dialog box */

}


 

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.