Use gtk for Programming 5 (window layout)

Source: Internet
Author: User
Tags call back gtk
Use gtk for Programming 5 (window layout) Reprinted please indicate the source: http://lvjinhua.cublog.cn
Author: lvjinhua at gmail dot com
2006.09.24

  • 7. Window Layout

The last time I talked about how to use gdb to debug our program, this time I will briefly introduce how to effectively layout the window, which will be the last part of the basics of gtk programming; in the following days, I will divide all the content in gtk into topics and describe each part in detail and in depth. Well, I will enter the topic of today.

Run:


Hello_box.c
/* The main purpose of this example is to rotate a box control in the main window, and then in the box Control
* Place a single row edit box and a button
* Click the button to exit the program.
*/
# Include <gtk/gtk. h>

// Callback function of the button "button"
Void
Cb_button (GtkWidget * widget, gpointer data)
{// Cb = call back
G_print ("You clicked the button/n ");
Gtk_main_quit ();
}

Int
Main (int argc, char * argv [])
{
GtkWidget * main_window; // Main Window object
Gtkwidget * hbox, * button, * Editor;

Gtk_init (& argc, & argv );

Main_window = gtk_window_new (gtk_window_toplevel );
G_signal_connect (g_object (main_window), "Destroy", g_callback (gtk_main_quit), null );
Gtk_window_set_title (gtk_window (main_window), "Hello, dubuntu2! ");
Gtk_window_set_default_size (gtk_window (main_window );

Button = gtk_button_new_with_label ("Exit program ");
G_signal_connect (g_object (button), "clicked", g_callback (cb_button), null );

Editor = gtk_entry_new ();

Hbox = gtk_hbox_new (FALSE, 10 );

Gtk_box_pack_start (GTK_BOX (hbox), editor, TRUE, TRUE, 10 );
Gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 10 );
Gtk_container_add (GTK_CONTAINER (main_window), hbox );

Gtk_widget_show_all (main_window );

Gtk_main ();
Return 0;
 

Program: gcc-o hello_box hello_box.c 'pkg-config -- cflags gtk +-1000'-g-Wall
Run the program:./hello_box

Relationship between components:

Program notes:
1) several new functions:

  • G_print ("You clicked the button/N");: This is the console-oriented output function provided by the glib library. Its function is similar to that of the standard C printf, however, using g_print () in GTK programs is more portable.
  • Gtk_entry_new (): constructs a single-line text input box object and returns a pointer to this object.
  • Gtk_hbox_new (): constructs a "horizontal" box to hold the single-line input box and button"
  • Gtk_box_pack_start (): "pack" into the box (here are enter and button)
  • Gtk_widget_show_all (main_window): displays all objects in main_window.

Gossip: In gtk, all functions for creating objects are in the form of "gtk _ OBJECT name_new ()".

2) What is box?
Box is an invisible (or invisible) object. Why is there a box?
Gtk treats the control size, which is different from VC, VB, Delphi, PB, and Qt. In front of these languages, after the interface is designed, the size of all its objects is fixed, that is, it will not automatically adapt to these changes because of screen resolution or window size changes (it is also difficult to implement adaptive functions ). In gtk or Java, the position of the control in the window is a relative value, and the program will automatically adjust the new position of the control according to the current environment. The specific adjustment method can be preset through the program.

A box is a "long" box, which has two types: "vertical" and "horizontal". They are created using gtk_hbox_new () and gtk_vbox_new () respectively, you can add one or more other controls to the "box", which is implemented using the gtk_box_pack_start () function. Of course, you can also add other "Boxes" to the "box ", this is the key to gtk interface layout. Generally, gtk programs set boxes and then set boxes, and put buttons, input boxes, menus, and other controls in each box, this forms a complex interface.

Of course, the table can also be used for the interface layout, but the most commonly used is box.

3) gtk_hbox_new ()

GtkWidget * gtk_hbox_new (gboolean homogeneous,
Gint spacing );
Function: Creates a new horizontal box control.
Parameter: homogeneous: If the value is "TRUE", all the controls that are put in are allocated the same size.
Spacing: the spacing between adjacent controls, in pixels)
Return Value: pointer of the newly created control. ,

The usage of the corresponding gtk_vbox_new () is exactly the same.

4) gtk_box_pack_start ()

Void gtk_box_pack_start (GtkBox * box,
GtkWidget * child,
Gboolean expand,
Gboolean fill,
Guint padding );
Function: Add a control to "box ".

Parameter: box: Add a new control to the "box ".
Child: Put this control
Expand and fill
Padding: distance between adjacent controls of the same child control (unit: pixel)

5) gtk_box (object) Macro
This macro is used to forcibly convert the object parameter to a gtkbox object (of course, type check is also performed). All objects in GTK have similar macros. For example, the gtkwindow object has a gtk_window () Macro, gtkbutton objects include gtk_button () macros.

Now, you can modify the values of parameters in "gtk_hbox_new and gtk_box_pack_start" and re-compile and run the program, see the relationship between parameters based on the running effect.

Recommendation: devhelp help, which contains unclear GTK functions or Macros in programming, can be queried at any time.

The entry part is over, and I am not sure how the content in front is written. I hope you can understand it. In the days to come, we will introduce each GTK control in detail (both visible and invisible), and try to write some small examples to demonstrate the functions of some controls or interface functions. After introducing the control, there are also glib topics, "Internationalization/local China" topics, Glade topics, analysis of hypersrc source code browsing software.

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.