gtk/glade Program Development (Part 1)
Reprinted 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 cuigf I have installed GTK 2.0/Glade 2.10.0, system environment RH9. I plan to provide an introduction to GTK programming over the next month, and I hope for your support.
Develop on Windows, then transfer the completed program to Linux. (No modifications to the source code are needed; just recompile and link once.)
Required Development Tools:
Dev-C++ 4.9.9.2 (for compiling and linking C programs)
Glade (for graphical interface design)
GTK Development Components
Only two software packages are needed:
devcpp-4.9.9.2_setup
gtk-win32-devel-2.6.8-rc1 (includes Glade and GTK development components)
Download URLs:
www.bloodshed.net
gladewin32.sourceforge.net,
Please download the versions specified above; other versions may have bugs.
Download and install the software (first install devcpp-4.9.9.2_setup, then install gtk-win32-devel-2.6.8-rc1; select the Dev-C++ option when it appears).
Next, let's run our first program.
(Comment: The author developed on Windows and then ported to Linux. I do not recommend this method.)
Section 2: Understanding Glade-Generated Code (Part 1)
In the src directory of the Glade source files, there are 7 code files, which are:
1> callbacks.c The file containing most of the empty callback functions generated by Glade; you add your code here.
2> callbacks.h (Header file) Definition file for Glade-generated callback functions.
4> interface.c Glade-generated UI source code file.
5> interface.h (Header file) Definition file for Glade-generated UI.
6> main.c Main function.
7> support.c Support file.
8> support.h (Header file) Support file.
In the devcpp directory generated by Glade, there are 2 files:
1> config.h Glade-generated configuration file.
2> gtk1.dev Glade-generated Dev-C++ project file (the name you used when saving).
Don't be intimidated by so many files. You only need to write code in three files (do not touch the others):
main.c (usually only requires minor modifications)
callbacks.h (usually only requires minor modifications, or even none)
callbacks.c (the specific implementation part of functions, where you need to write a certain amount of code)
Among these, callbacks.c is the main file where you write your code.
Below, let's briefly understand the Glade-generated UI file interface.c.
GtkWidget*
create_window1 (void)
{
GtkWidget *window1; /* Defines a window widget */
GtkWidget *fixed1; /* Defines a fixed container widget, used to place other widgets */
GtkWidget *label1; /* Defines a label widget */
window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL); /* Creates a new window */
gtk_window_set_title (GTK_WINDOW (window1), _("window1")); /* Sets the window title */
gtk_window_set_default_size (GTK_WINDOW (window1), 400, 300); /* Sets the default window size to 400,300 */
fixed1 = gtk_fixed_new (); /* Creates a new fixed container */
gtk_widget_show (fixed1); /* Shows the fixed container */
gtk_container_add (GTK_CONTAINER (window1), fixed1); /* Adds the fixed container to the window */
label1 = gtk_label_new (_("/346/254/242/350/277/216/350/265/260/350/277/233GTK/347/232/204/344/270/226/347/225/214")); /* Creates a new label */
gtk_widget_show (label1); /* Shows the label */
gtk_fixed_put (GTK_FIXED (fixed1), label1, 120, 72); /* Adds the label to the fixed container */
gtk_widget_set_size_request (label1, 136, 40); /* Sets the label 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 code inside may differ depending on the version.)
(Understand as much of this section as you can; it's okay if you don't understand everything.)
File: main.c
Only a partial explanation is provided; you can read it more thoroughly once you've gained more experience!
The following code is excerpted from main.c
int main (int argc, char *argv[])
{
GtkWidget *window1; /* Defines the window1 widget */
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