From http://www.linuxforum.net/forum/gshowflat.php? Cat = & board = kylix & number = 566950 & page = 0 & view = collapsed & SB = 4 & O = All & fpart = All & Vc = 1
By cugf
I have installed gtk2.0/glade2.10.0 and rh9 in the system environment.
I am going to introduce GTK programming in one month. I hope you can support it.
Develop the program in windows and transfer the program to Linux. (the source code is not modified. You only need to re-compile and connect it once)
Required development tools:
Dev-CPP 4.9.9.2 (compile and Connect C Programs)
Glade (graphic interface design)
GTK development component
Only two software packages are required:
Devcpp-4.9.9.2_setup
Gtk-win32-devel-2.6.8-rc1 (including glade and GTK development components)
Download URL:
Www.bloodshed.net
Gladewin32.sourceforge.net,
Download the above version. Other versions have bugs.
Download the software and install it (first install the devcpp-4.9.9.2_setup, then install the gtk-win32-devel-2.6.8-rc1, and when the dev-CPP option appears, select it ),
Next, run our first program.
(Comment: I developed and transplanted it to Linux in windows. I do not recommend this method)
Section 2 read the code generated by Glade (1)
There are 7 code files under the source file directory SRC of glade, which are:
1> Callbacks. c Glade generates the file where most of the empty callback functions are located. You can add code to it by yourself.
2> Callbacks. H (header file) The definition file of the callback function generated by glade
4> interface. c Glade-generated Interface source code file
5> interface. H (header file) interface definition file generated by glade
6> main. C Main Function
7> support. c Support file
8> support. H (header files) support files
There are 2 files in the devcpp directory generated by galde
1> configuration file generated by config. h glade
2> the devcpp project file generated by gtk1.dev Glade (that is, the name that you started when saving ).
Don't be scared by so many files. You need to write only three files (other files are not moved) by yourself. They are:
Main. C (generally, only a few modifications are required)
Callbacks. h)
Callbacks. C (the specific implementation part of the function, you need to write a certain amount of code here)
Callbacks. c is the main file of your code.
Next, let's take a look at the interface. c file generated by glade.
Gtkwidget *
Create_window1 (void)
{
Gtkwidget * window1;/* define a window component */
Gtkwidget * fixed1;/* defines a fixed container component for storing other components */
Gtkwidget * label1;/* define a tag component */
Window1 = gtk_window_new (gtk_window_toplevel);/* create a window */
Gtk_window_set_title (gtk_window (window1), _ ("window1");/* set the window title */
Gtk_window_set_default_size (gtk_window (window1), 400,300);/* set the default window size to 400,300 */
Fixed1 = gtk_fixed_new ();/* Create a fixed container */
Gtk_widget_show (fixed1);/* display fixed containers */
Gtk_container_add (gtk_container (window1), fixed1);/* Add a fixed container to the window */
Label1 = gtk_label_new (_ ("/346/254/242/350/277/216/350/265/260/350/277/233 GTK/347/232/204/344/270/226/347/225 ")); /* Create a tag */
Gtk_widget_show (label1);/* display tag */
Gtk_fixed_put (gtk_fixed (fixed1), label1, 120, 72);/* Add a tag to a fixed container */
Gtk_widget_set_size_request (label1, 136, 40);/* set the tag size */
/* Store pointers to all widgets, for use by lookup_widget ().*/
Glade_hookup_object_no_ref (window1, window1, "window1 ");
Glade_hookup_object (window1, fixed1, "fixed1 ");
Glade_hookup_object (window1, label1, "label1 ");
Return window1;
}
(Comment: The version may be different, and the code may be different)
(It doesn't matter how much you can understand or understand .)
File: Main. c
I will only explain it in part. You will be at a high level in the future. Let's take a look!
The following code is excerpted from Main. C.
Int main (INT argc, char * argv [])
{
Gtkwidget * window1;/* define the window1 component */
Gchar * pixmap_dir;
# Ifdef g_ OS _win32
Package_prefix = g_win32_get_package_installation_directory (null, null );
Package_data_dir = g_build_filename (package_prefix, "share", null );
Package_locale_dir = g_build_filename (package_prefix, "share", "locale", null );
# Endif
# Ifdef enable_nls
Bindtextdomain (gettext_package, package_locale_dir );
Bind_textdomain_codeset (gettext_package, "UTF-8 ");
Textdomain (gettext_package );
# Endif
Gtk_set_locale ();/* you can call this function to display Chinese characters */
Gtk_init (& argc, & argv);/* GTK initialization */
Pixmap_dir = g_build_filename (package_data_dir, package, "pixmaps", null );
Add_pixmap_directory (pixmap_dir );
G_free (pixmap_dir );
/*
* The following code was added by glade to create one of each component
* (Response t popup menus), just so that you see something after building
* The project. delete any components that you don't want shown initially.
*/
Window1 = create_window1 ();/* Create a window1 component */
Gtk_widget_show (window1);/** display the window1 component/
G_signal_connect (gpointer) window1, "Destroy", g_callback (gtk_main_quit ),
Null );
/* It is the "Destroy" event of the window1 component, associated function,
That is, when you click the close button of window1, close window1 and exit GTK */
Gtk_main ();/* GTK main program starts execution */
# Ifdef g_ OS _win32
G_free (package_prefix );
G_free (package_data_dir );
G_free (package_locale_dir );
# Endif
Return 0;
}
Tag component
Functions used by TAG Components
Create a new tag:
Gtk_label_new ("label1 ");
Gtk_label_new_with_mnemonic ("label1 ");
Place a line break in a string to create a multiline tag.
Change the label text:
Gtk_label_set_text (gtk_label (label1), "label2 ");
Get the current text of the tag:
Gtk_label_get_text (gtk_label (label1 ));
Set the alignment of label text:
Gtk_label_set_justify (gtk_label (label1), gtk_justify_left );
Gtk_justify_left/* left alignment */
Gtk_justify_right/* Right-aligned */
Gtk_justify_center/* center alignment */
Gtk_justify_fill/* Full */
Text wrap in the label:
Gtk_label_set_line_wrap (gtk_label (label1), true );
Note: The tag cannot trigger a signal. to trigger a signal, place the tag in the event box.
Common functions of text input components:
Create a text input component:
Gtkwidget * entry1;
Entry1 = gtk_entry_new ();
Change the current text content of the text input component:
Gtk_entry_set_text (gtk_entry (entry1), "entry2 ");
Obtain the current text content of the text input component:
Const gchar * text1;
Text1 = gtk_entry_get_text (gtk_entry (entry1 ));
In the next section, we use an example to describe the use of text entry components.
Text input component (2)
1. Use glade to design the interface
1. Create a glade Project
2. Place various components: create a window component (window1), place fixed container components (fixed positions) on window1, and place the text input component (entry1) on the fixed container component ).
3. Add the changed signal response for the text input component entry1 (Event Response in other programming languages) on_entry1_changed,Be sure to set the object to entry1 on the signals tab..
4. Click build to generate the source code.
Ii. Add code
You only need to add three lines of code in Callbacks. C.
Callbacks. c
Void on_entrytransferchanged (gtkeditable * editable,
Gpointer user_data)
{
Const gchar * text1;
Text1 = gtk_entry_get_text (gtk_entry (editable ));
Printf ("text entry: % s/n", text1 );
}
3. Run the program
Before compiling the program, set the project property to "Win32 console program" in devcpp"
Step: 1. Click the project properties sub-menu under the project menu.
2. Click "Win32 console program" in "type" on the "General" tab ".
3. Click OK ".
Compile and run the program. Enter several English letters in the text input box to check the running effect.
Note: in Linux, the operation is very simple. Click build of glade to automatically generate code, and then run autogen. Sh in the directory to run make.
The src directory generates executable programs.
Common functions of the combox component:
Create a widget:
Comboboxentry1 = gtk_combo_box_entry_new_text ();
Add a new project to the drop-down list of the combo box component:
Gtk_combo_box_append_text (gtk_combo_box (comboboxentry1), _ ("one "));
Read the text in the component combination:
Text1 = gtk_combo_box_get_active_text (ComboBox );
Example:
1. Use glade to design the interface
Step: 1. Create a glade Project
2. Place the window component (size: 400,300), fixed positions (fixed container) component, and comboboxentry (comboboxentry) component in sequence.
3. On the attribute panel, set the item attribute of comboboxentry1 to one (Press ENTER), two (Press press Enter), three (Press ENTER ),
4. Add the changed signal response for the comboboxentry1 component (Note: Enter comboboxentry1 in the text box after the object ).
5. Save the project and generate code.
2. write code
Before writing the code, refer to the content in the previous section and set the project property to "Win32 console program ".
Callbacks. h
Void on_comboboxentrycmdchanged (gtkcombobox * ComboBox,
Gpointer user_data );
Callbacks. c
Void on_comboboxentrycmdchanged (gtkcombobox * ComboBox,
Gpointer user_data)
{
Gchar * text1;
Text1 = gtk_combo_box_get_active_text (ComboBox );
Printf ("the combo text is: % s/n", text1 );
}
3. Run the program to view the Running Effect
Toggle button component
Common functions:
Create a new switch button component
Gtkwidget * togglebutton1;
Togglebutton1 = gtk_toggle_button_new ();
Togglebutton1 = gtk_toggle_button_new_with_label ("toggle button ");
Togglebutton1 = gtk_toggle_button_new_with_mnemonic ("toggle button ");
Status of the read switch button
Gtk_toggle_button_get_active (gtk_toggle_button (button ))
Set the status of the switch button
Gtk_toggle_button_set_active (gtk_toggle_button (button), true)
Gtk_toggle_button_set_active (gtk_toggle_button (button), false)
Example:
I. Design Interface
Step: 1. Create a glade Project
2. Place the window component (size: 300,200), fixed positions (fixed container) component, and toggle button component in sequence.
3. On the attribute panel, set togglebutton1's label attribute to "switch button ".
4. Add a clicked signal response (Note: Fill in togglebutton1 in the text box after the object).
5. Save the project and generate code.
2. write code
Before running, set the project property to "Win32 console program ".
Callbacks. c
Void on_togglebuttonw.clicked (gtkbutton * button,
Gpointer user_data)
{
If (gtk_toggle_button_get_active (gtk_toggle_button (button )))
{Printf ("toggle button is on/N ");}
Else
{Printf ("toggle button is off/N ");}
}
Iii. Run debugging
Radio button radio buttons component
Common functions of radio button components:
Code for creating a radio button component:
/* The following code is excerpted from interface. C */
/* Define component */]
Gtkwidget * radiobutton1;
Gslist * radiobutton1_group = NULL;
Gtkwidget * radiobutton2;
Gtkwidget * radiobutton3;
/*To create a component, first define a radiobutton component,
Add the radiobutton component to the group,
In this way, all radiobutton components can be associated into a whole.*/
(Note: This text is not detailed. It should be: 1. Create a radiobutton1.
2. There is a grouping item at the bottom of the radiobutton1 attribute bar. Select radiobutton1.
3. Create a New radiobutton and add radiobutton1.
)
Radiobutton1 = gtk_radio_button_new_with_mnemonic (null, _ ("radiobutton1 "));
Gtk_radio_button_set_group (gtk_radio_button (radiobutton1), radiobutton1_group );
Radiobutton1_group = gtk_radio_button_get_group (gtk_radio_button (radiobutton1 ));
Radiobutton2 = gtk_radio_button_new_with_mnemonic (null, _ ("radiobutton2 "));
Gtk_radio_button_set_group (gtk_radio_button (radiobutton2), radiobutton1_group );
Radiobutton1_group = gtk_radio_button_get_group (gtk_radio_button (radiobutton2 ));
Radiobutton3 = gtk_radio_button_new_with_mnemonic (null, _ ("radiobutton3 "));
Gtk_radio_button_set_group (gtk_radio_button (radiobutton3), radiobutton1_group );
Radiobutton1_group = gtk_radio_button_get_group (gtk_radio_button (radiobutton3 ));
Function used to determine the component Status of a Single-choice button (same as the switch button ):
Gtk_toggle_button_get_active (gtk_toggle_button (radiobutton1 ))
1. Use glade to design the interface
Step: 1. Create a glade Project
2. Place the window (400,300), fixed position, and three radio button components accordingly.
3. Add toggled signal responses for the three radio button components, and enter radiobutton1, radiobutton2, and radiobutton3.
4. Save the project and generate code.
Ii. modify the code
Interface. c
G_signal_connect_swapped (gpointer) radiobutton1, "toggled ",
G_callback (on_radiobutton1_toggled ),
Gtk_object (radiobutton1 ));
G_signal_connect_swapped (gpointer) radiobutton2, "toggled ",
G_callback (on_radiobutton2_toggled ),
Gtk_object (radiobutton2 ));
G_signal_connect_swapped (gpointer) radiobutton3, "toggled ",
G_callback (on_radiobutton3_toggled ),
Gtk_object (radiobutton3 ));
Callbacks. h
Void on_radiobutton1_toggled (gtktogglebutton * radiobutton1,
Gpointer user_data );
Void on_radiobutton2_toggled (gtktogglebutton * radiobutton2,
Gpointer user_data );
Void on_radiobutton3_toggled (gtktogglebutton * radiobutton3,
Gpointer user_data );
Callbacks. c
Void on_radiobutton1_toggled (gtktogglebutton * radiobutton1,
Gpointer user_data)
{
If (gtk_toggle_button_get_active (gtk_toggle_button (radiobutton1 )))
{
Printf ("radiobutton1 is true/N ");
}
}
Void on_radiobutton2_toggled (gtktogglebutton * radiobutton2,
Gpointer user_data)
{
If (gtk_toggle_button_get_active (gtk_toggle_button (radiobutton2 )))
{
Printf ("radiobutton2 is true/N ");
}
}
Void on_radiobutton3_toggled (gtktogglebutton * radiobutton3,
Gpointer user_data)
{
If (gtk_toggle_button_get_active (gtk_toggle_button (radiobutton3 )))
{
Printf ("radiobutton3 is true/N ");
}
}