|
|
| Author |
Message |
|
|
Micah Carrick Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 514 Location: Portland, OR USA
|
Posted: Tue Jan 16, 2007 12:47 am Post subject: Very, very simple example using libglade |
|
|
Here's a very simple example of using libglade which I created for a different forum recently. Paste the listing for "hello.glade" into a text file and save it as "hello.glade" and then you can open it up with Glade to see how it's layed out.
hello.glade
| Code: (XML/HTML) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| <?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">
<glade-interface>
<widget class="GtkWindow" id="window1">
<property name="border_width">10</property>
<property name="visible">True</property>
<property name="title" translatable="yes">window1</property>
<property name="type">GTK_WINDOW_TOPLEVEL</property>
<property name="window_position">GTK_WIN_POS_NONE</property>
<property name="modal">False</property>
<property name="resizable">False</property>
<property name="destroy_with_parent">False</property>
<property name="decorated">True</property>
<property name="skip_taskbar_hint">False</property>
<property name="skip_pager_hint">False</property>
<property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
<property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
<property name="focus_on_map">True</property>
<property name="urgency_hint">False</property>
<signal name="destroy" handler="on_window1_destroy" last_modification_time="Sun, 14 Jan 2007 01:56:55 GMT"/>
<child>
<widget class="GtkVBox" id="vbox2">
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">5</property>
<child>
<widget class="GtkEntry" id="entry1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="editable">True</property>
<property name="visibility">True</property>
<property name="max_length">0</property>
<property name="text" translatable="yes"></property>
<property name="has_frame">True</property>
<property name="invisible_char">•</property>
<property name="activates_default">False</property>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="button1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="label" translatable="yes">Hello</property>
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_button1_clicked" last_modification_time="Sun, 14 Jan 2007 01:57:15 GMT"/>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
</widget>
</child>
</widget>
</glade-interface> |
hello.c
| Code: (C) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #include <gtk/gtk.h>
#include <glade/glade.h>
#define GLADE_FILE "hello.glade"
void on_button1_clicked (GtkButton *button, GtkWidget *entry);
int
main (int argc, char *argv[])
{
GladeXML *gxml = NULL;
GtkWidget *window = NULL;
GtkWidget *entry = NULL;
/* initialize GTK+ libraries */
gtk_init (&argc, &argv);
/* build from Glade XML file */
gxml = glade_xml_new (GLADE_FILE, NULL, NULL);
g_assert (gxml);
/* get widgets from Glade XML file */
window = glade_xml_get_widget (gxml, "window1");
entry = glade_xml_get_widget (gxml, "entry1");
/* call gtk_main_quit() when the window's "x" is clicked */
glade_xml_signal_connect (gxml, "on_window1_destroy",
G_CALLBACK (gtk_main_quit));
/* call on_button1_clicked() when button1 is clicked, passing the text
entry widget to the function as the user_data */
glade_xml_signal_connect_data (gxml, "on_button1_clicked",
G_CALLBACK (on_button1_clicked), entry);
/* free Glade XML object */
g_object_unref (G_OBJECT (gxml));
/* begin GTK+ main loop */
gtk_main ();
return 0;
}
void
on_button1_clicked (GtkButton *button, GtkWidget *entry)
{
/* set the entry's text */
gtk_entry_set_text (GTK_ENTRY(entry), "Hello World!");
}
|
Compile:
| Code: (Plaintext) | 1
| gcc -Wall -g `pkg-config --cflags --libs gtk+-2.0` `pkg-config --cflags --libs libglade-2.0` -o hello hello.c |
Last edited by Micah Carrick on Sat Dec 01, 2007 6:14 pm; edited 1 time in total |
|
| Back to top |
|
 |
openldev Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 385 Location: Fairfax, Virginia
|
Posted: Tue Jan 16, 2007 1:01 am Post subject: |
|
|
| You might want to add -export-dynamic to the compile line so that signals can be autoconnected ... |
|
| Back to top |
|
 |
Micah Carrick Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 514 Location: Portland, OR USA
|
Posted: Tue Jan 16, 2007 1:05 am Post subject: |
|
|
Good point.
I don't use the autoconnect because I like to define them all in the glade file but only connect them as I write the code for 'em one at a time. Helps me remember what's what. |
|
| Back to top |
|
 |
openldev Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 385 Location: Fairfax, Virginia
|
Posted: Tue Jan 16, 2007 1:48 am Post subject: |
|
|
| I don't use the default autoconnect feature either because it doesn't allow you to pass whatever parameters you want (user data). Personally, I just create my own autoconnect function that introspects the binary with GModule and allows me to specify user data parameters. Although, that does require -export-dynamic as well. |
|
| Back to top |
|
 |
broeisi Familiar Face
Joined: 18 May 2007 Posts: 8
|
Posted: Thu Mar 13, 2008 7:30 pm Post subject: |
|
|
hello,
I keep getting the following error when I try to run the example:
==============================================
(hello:5841): XML-CRITICAL **: Input is not proper UTF-8, indicate encoding !
Bytes: 0xBF 0x3C 0x2F 0x70
(hello:5841): libglade-WARNING **: widget_depth != 0 (3)
(hello:5841): libglade-WARNING **: did not finish in PARSER_FINISH state
** ERROR **: file hello.c: line 20 (main): assertion failed: (gxml)
aborting...
Aborted
===============================================
Do you know what's wrong with the example? |
|
| Back to top |
|
 |
Micah Carrick Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 514 Location: Portland, OR USA
|
Posted: Fri Mar 14, 2008 12:06 am Post subject: |
|
|
| It sounds like when you copied and pasted the glade file you did so in an encoding other than UTF. If you're in GNOME, gedit saves as UTF. If you're in Windows, you'll need to use a programming editor (such as note tab) and specify UTF8 encoding. |
|
| Back to top |
|
 |
Thomas
Joined: 02 Apr 2008 Posts: 1
|
Posted: Tue Apr 08, 2008 1:15 pm Post subject: |
|
|
| I just wanted to say 'Thank you' for your example. I just started programming with Glade/C/GTK+ less than a week ago and your example has helped me out a lot. I am now able to test my Glade interface (a CD player) by changing the properties to see how they affect the UI. What made your example so good is that I now know how to run my UI and how signals (i.e. VB events?) are handled. The two tutorials I used prior to your example did not help at all because the first one was written using code only and the second one has not been completed so I am left hanging. |
|
| Back to top |
|
 |
Vadi GTK+ Geek
Joined: 28 May 2008 Posts: 68
|
Posted: Fri May 30, 2008 2:25 pm Post subject: |
|
|
| I think you forgot gtk_widget_show(window); in there |
|
| Back to top |
|
 |
caracal GTK+ Geek
Joined: 21 Jun 2007 Posts: 91 Location: Wilkes Barre Pa
|
Posted: Fri Jul 04, 2008 6:49 am Post subject: |
|
|
| Vadi wrote: | | I think you forgot gtk_widget_show(window); in there |
Shouldn't need it | Code: (XML/HTML) | 1 2
| <widget class="GtkWindow" id="window1">
<property name="visible">True</property> |
I didn't know about the -export-dynamic |
|
| Back to top |
|
 |
Vadi GTK+ Geek
Joined: 28 May 2008 Posts: 68
|
Posted: Fri Jul 04, 2008 1:19 pm Post subject: |
|
|
| visible != show |
|
| Back to top |
|
 |
dreblen Never Seen the Sunlight
Joined: 14 Jun 2007 Posts: 643 Location: Falun, WI USA
|
|
| Back to top |
|
 |
Vadi GTK+ Geek
Joined: 28 May 2008 Posts: 68
|
Posted: Sat Jul 05, 2008 5:27 pm Post subject: |
|
|
| Well, I needed to call that when using the example to get the window to show up. :/ |
|
| Back to top |
|
 |
Micah Carrick Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 514 Location: Portland, OR USA
|
Posted: Sat Jul 05, 2008 11:56 pm Post subject: |
|
|
| With older versions of Glade using Python you would need to explicitly show set the visibility... I think. |
|
| Back to top |
|
 |
|