Author Topic: GTK Glade - Hello World  (Read 19156 times)

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
GTK Glade - Hello World
« on: December 02, 2007, 11:42:42 PM »
This example is using the GTK Glade designer to create a fixed size window, apply a fixed position grid and putting a label in the center of the screen. The ScriptBasic event handler is looking for the close window system button event to exit the REPEAT loop.



hello.bas
Code: [Select]

' Glade Hello World

' GTK Interface
include gtk.bas

' GTK Interface Function
FUNCTION GUI(g0, g1, g2, g3, g4, g5, g6, g7, g8, g9)
LOCAL gtk_cmd
gtk_cmd = STR(g0) & " " & STR(g1) & " " & STR(g2) & " " & STR(g3) & " " & STR(g4) & " " & STR(g5) & " " & STR(g6) & " " & STR(g7) & " " & STR(g8) & " " & STR(g9)
GUI = GTK::gtk(gtk_cmd)
END FUNCTION

' GTK Function DEF's
GUI("/etc/gtk-server.cfg")

' Initialize GTK & Glade
GUI("gtk_init")
GUI("glade_init")

' Load and show Glade defined window
xml = GUI("glade_xml_new", "hello.glade")
GUI("glade_xml_signal_autoconnect", xml)

' Get window ID and define window close event
win = GUI("glade_xml_get_widget", xml, "window1")
GUI("gtk_server_connect", win, "delete-event", "window")

' Event Handler
REPEAT
this_event = GUI("gtk_server_callback", "WAIT")
UNTIL this_event = "window"

END


hello.glade - Glade project XML file
Code: [Select]

<?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="visible">True</property>
  <property name="title" translatable="yes">Hello</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="default_width">400</property>
  <property name="default_height">350</property>
  <property name="resizable">True</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>

  <child>
    <widget class="GtkFixed" id="fixed1">
      <property name="visible">True</property>

      <child>
<widget class="GtkLabel" id="label1">
 <property name="width_request">122</property>
 <property name="height_request">17</property>
 <property name="visible">True</property>
 <property name="label" translatable="yes">Hello World !</property>
 <property name="use_underline">False</property>
 <property name="use_markup">False</property>
 <property name="justify">GTK_JUSTIFY_LEFT</property>
 <property name="wrap">False</property>
 <property name="selectable">False</property>
 <property name="xalign">0.5</property>
 <property name="yalign">0.5</property>
 <property name="xpad">0</property>
 <property name="ypad">0</property>
 <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
 <property name="width_chars">-1</property>
 <property name="single_line_mode">False</property>
 <property name="angle">0</property>
</widget>
<packing>
 <property name="x">128</property>
 <property name="y">128</property>
</packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>


To get an idea of the types of widgets you can use in your screen design, check out the following link.

GNOME 2.20 Release Notes

GTK+ Reference Manual

Pjot

  • Guest
GTK Glade - Hello World
« Reply #1 on: February 08, 2008, 01:22:06 AM »
The code can be improved as follows:
Code: [Select]

' Glade Hello World

' GTK Interface
include gtk.bas

' Initialize GTK & Glade
gtk_init("NULL", "NULL")
glade_init

' Load and show Glade defined window
xml = glade_xml_new("hello.glade")
glade_xml_signal_autoconnect(xml)

' Get window ID and define window close event
win = glade_xml_get_widget(xml, "window1")
gtk_server_connect(win, "delete-event", "window")

' Event Handler
REPEAT
    this_event = gtk_server_callback("WAIT")
UNTIL this_event = "window"

END


The 'gtk.bas' file in the Scriptbasic include directory already contains individual wrappers for the GTK and GLADE functions mentioned in the GTK-server configfile. This way you can program GTK as if you are using the original API.

Regards

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
GTK Glade - Hello World
« Reply #2 on: February 09, 2008, 02:05:35 AM »
Peter,

Thanks for the code clean-up !

John

P.S.

Here is a link to the GTK Widget Gallery.