Home > C > Glade3 gtk c first simple application

Glade3 gtk c first simple application

Start with very basic example:

Part 1: main.c

#include <gtk/gtk.h>

void on_window1_destroy (GtkObject       *object, gpointer         user_data)
{
gtk_main_quit();
}

void on_button2_clicked (GtkButton       *button,gpointer         user_data)
{
gtk_main_quit();
}

void on_button1_clicked (GtkButton       *button,gpointer         user_data)
{
printf(“Hello”);
}

int main( int    argc,
char **argv )
{
GtkBuilder *builder;
GtkWidget  *window;
GError     *error = NULL;

/* Init GTK+ */
gtk_init( &argc, &argv );

/* Create new GtkBuilder object */
builder = gtk_builder_new();
/* Load UI from file. If error occurs, report it and quit application.
* Replace “tut.glade” with your saved project. */
if( ! gtk_builder_add_from_file( builder, “window.glade”, &error ) )
{
g_warning( “%s”, error->message );
g_free( error );
return( 1 );
}

/* Get main window pointer from UI */
window = GTK_WIDGET( gtk_builder_get_object( builder, “window1” ) );

/* Connect signals */
gtk_builder_connect_signals( builder, NULL );

/* Destroy builder, since we don’t need it anymore */
g_object_unref( G_OBJECT( builder ) );

/* Show window. All other widgets are automatically shown by GtkBuilder */
gtk_widget_show( window );

/* Start main loop */
gtk_main();

return( 0 );
}

 

Part 2: prepared by Glade 3

<?xml version=”1.0″?>
<interface>
<requires lib=”gtk+” version=”2.14″/>
<!– interface-naming-policy project-wide –>
<object id=”window1″>
<signal name=”destroy” handler=”on_window1_destroy”/>
<child>
<object id=”vbox1″>
<property name=”visible”>True</property>
<property name=”orientation”>vertical</property>
<child>
<object id=”hbox1″>
<property name=”visible”>True</property>
<child>
<object id=”label1″>
<property name=”visible”>True</property>
<property name=”label” translatable=”yes”>label</property>
</object>
<packing>
<property name=”position”>0</property>
</packing>
</child>
<child>
<object id=”entry1″>
<property name=”visible”>True</property>
<property name=”can_focus”>True</property>
<property name=”invisible_char”>●</property>
</object>
<packing>
<property name=”position”>1</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name=”position”>0</property>
</packing>
</child>
<child>
<object id=”button2″>
<property name=”label” translatable=”yes”>button</property>
<property name=”visible”>True</property>
<property name=”can_focus”>True</property>
<property name=”receives_default”>True</property>
</object>
<packing>
<property name=”position”>1</property>
</packing>
</child>
<child>
<object id=”button1″>
<property name=”label” translatable=”yes”>button</property>
<property name=”visible”>True</property>
<property name=”can_focus”>True</property>
<property name=”receives_default”>True</property>
<signal name=”clicked” handler=”on_button1_clicked”/>
</object>
<packing>
<property name=”position”>2</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

Part 3: compiled as gcc main.c -o main -Wall `pkg-config –cflags –libs gtk+-2.0 gmodule-2.0`

Advertisements
Categories: C
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: