GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Sep 17, 2014 9:34 pm

All times are UTC




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Very, very simple example using libglade
PostPosted: Tue Jan 16, 2007 12:47 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
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 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:
#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:
gcc -Wall -g `pkg-config --cflags --libs gtk+-2.0` `pkg-config --cflags --libs libglade-2.0` -o hello hello.c

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Last edited by Micah Carrick on Sat Dec 01, 2007 6:14 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 1:01 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
You might want to add -export-dynamic to the compile line so that signals can be autoconnected ...

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 1:05 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
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.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 1:48 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
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.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 7:30 pm 
Offline
Familiar Face

Joined: Fri May 18, 2007 2:05 pm
Posts: 7
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?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 12:06 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
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.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 08, 2008 1:15 pm 
Offline

Joined: Wed Apr 02, 2008 1:30 pm
Posts: 1
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 2:25 pm 
Offline
GTK+ Geek

Joined: Wed May 28, 2008 5:49 pm
Posts: 66
I think you forgot gtk_widget_show(window); in there


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 6:49 am 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
Vadi wrote:
I think you forgot gtk_widget_show(window); in there


Shouldn't need it
Code:
<widget class="GtkWindow" id="window1">
    <property name="visible">True</property>


I didn't know about the -export-dynamic


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 1:19 pm 
Offline
GTK+ Geek

Joined: Wed May 28, 2008 5:49 pm
Posts: 66
visible != show


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 7:27 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
but it is. the widget flag set by gtk_widget_show() is GTK_VISIBLE,
http://library.gnome.org/devel/gtk/stab ... idgetFlags
see the section for GTK_VISIBLE in the above link


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 05, 2008 5:27 pm 
Offline
GTK+ Geek

Joined: Wed May 28, 2008 5:49 pm
Posts: 66
Well, I needed to call that when using the example to get the window to show up. :/


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 05, 2008 11:56 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
With older versions of Glade using Python you would need to explicitly show set the visibility... I think.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group