GTK Development Using Glade 3
Total Page:16
File Type:pdf, Size:1020Kb
GTK Development Using Glade 3 GTK+ is a toolkit, or a collection of libraries, which developers can use to develop GUI applications for Linux, OSX, Windows, and any other platform on which GTK+ is available. Original Article by Micah Carrick Table of Contents Part 1 - Designing a User Interface Using Glade 3 ___________________________________________1 Quick Overview of GTK+ Concepts ______________________________________________________________1 Introduction to Glade3 _________________________________________________________________________3 Getting Familiar with the Glade Interface_______________________________________________________3 Manipulating Widget Properties________________________________________________________________5 Specifying Callback Functions for Signals _______________________________________________________6 Adding Widgets to the GtkWindow _____________________________________________________________9 How Packing Effects the Layout ______________________________________________________________ 13 Editing the Menu (or Toolbar) ________________________________________________________________ 15 Final Touches to the Main Window ___________________________________________________________ 18 Part 2 - Choosing a Programming Language for GT K+ Developm ent _____________ 19 Which is the BEST Language? ________________________________________________________________ 19 Language Choice Considerations _____________________________________________________________ 19 A Look at Python vs. C________________________________________________________________________ 20 Part 3 __________________________________________________________________________________________ 22 Setting Up Your Development Environment___________________________________________________ 22 GtkBuilder and LibGlade _____________________________________________________________________ 24 The Minimal Application _____________________________________________________________________ 25 Compiling and Running the Application ______________________________________________________ 27 Stepping Through the Code ___________________________________________________________________ 29 Including the GTK+ Library _______________________________________________________________ 29 Initializing the GTK+ Library ______________________________________________________________ 29 Building the Interface with GtkBuilder ____________________________________________________ 30 Getting References to Widgets From GtkBuilder _________________________________________ 32 Connecting Callback Functions to Signals _______________________________________________ 33 Showing the Application Window _________________________________________________________ 36 In Summary _______________________________________________________________________________ 37 Part 1| Designing a User Interface Using Glade 3 In part 1 of the GTK+ and Glade3 GUI Programming Tutorial series, we will be designing the graphical user interface (GUI) for a GTK+ text editor application (shown left) which will be used throughout these tutorials. This GUI design will be created using the Glade Interface Designer and is completely independent of the programming language used to implement the design, which will come in subsequent tutorials. Quick Overview of GTK+ Concepts If you have no experience with GTK+, you may struggle with some of the concepts I am going to cover. Although I am going to attempt to teach some of these concepts on the fly, it would serve you well to read up on these ideas further, perhaps after working through part 1 of this tutorial. Understanding the fundamental concepts of GTK+ will be instrumental in your ability to effectively use Glade. First of all, GTK+ is not a programming language. GTK+ is a toolkit, or a collection of libraries, which developers can use to develop GUI applications for Linux, OSX, Windows, and any other platform on which GTK+ is available. It can be thought of in the same terms as MFC or the Win32 API on Windows, Swing and SWT in Java, or Qt (the "other" Linux GUI toolkit used by KDE). Although GTK+ itself is written in C, there are a multitude of language "bindings" allowing programmers to develop GTK+ applications in the language of their choice including C++, Python, Perl, PHP, Ruby, and many others. GTK+ is based on 3 main libraries: Glib, Pango, and ATK, however, we primarily work with GTK+ and let GTK+ do it's magic with those 3 libraries. GLib wraps most of the standard C library functions for portability (allowing your code to run on Windows and Linux if desired). We use GLib a lot when working in C or C++, which I will explain more thoroughly when implementing our design using C. Higher-level languages such as Python and Ruby won't have to worry about GLib as they have their own standard libraries which provide similar functionality. GTK+ and associated libraries implement an object oriented approach through GObject. How this works isn't important just yet, and different programming languages will reveal this to you differently, however, it's important to understand that GTK+ uses object orientation (yes, even in C). Every piece of a GTK+ GUI is comprised of one or more "widgets" which are objects. All widgets will be derived from a base widget called GtkWidget. For example, an application's window is a widget called GtkWindow. The toolbar within that window 1 is a widget called GtkToolbar. Although a GtkWindow is also a GtkWidget, a GtkWidget is not neccesarily a GtkWindow. Child widgets are derived from their parent objects to extend the functionality of that object. These are standard OOP (object oriented programming) concepts (hint: Google search "object oriented programming" if this is a new concept). We can look at any widget in the GTK+ reference documentation to see which objects it is derived from. In the case of GtkWindow, it looks something like this: GObject +----GInitiallyUnowned +----GtkObject +----GtkWidget +----GtkContainer +----GtkBin +----GtkWindow As you can see, a GtkWindow is derived from GtkBin which is derived from GtkContainer, and so on. For your first application, you don't need to worry about anything above the GtkWidget object. The reason this heirarchy is so important is because when you're looking for functions, properties, and signals for any particular widget, you need to realize that the functions, properties, and signals of it's parent objects apply to it as well. In part 2, this will become even more apparent when writing code for this example application. We also begin to see a naming convention emerge. This is pretty handy. We can easily tell what library an object or function is from. All objects beginning with Gtk are from GTK+. Later, we'll see things like GladeXML which is part of Libglade or GError which is part of GLib. All objects (and thus Widgets) are incamel case. The functions which manipulate these objects are in lower-case with underscores for spaces. For example, gtk_window_set_title() is a function to set the title property of a GtkWindow object. All the reference documentation you will need is available online from library.gnome.org/devel/references, however, it is much easier to use Devhelp which is likely available as a package for your distribution. Devhelp allows you to browse and search the API documentation for the libraries you have installed on your system (assuming you install that libraries documentation as well). More information on GTK+ and Glib: Foundations of GTK+ Development (book) GTK+ 2.0 Tutorial GTK+ - The GIMP Toolkit 2 GLib Reference Manual GTK+ Reference Manual GTK+ 2.0 Tutorial (C Programming) PyGTK+ 2.0 Tutorial (Python Programming) Introduction to Glade3 Glade is a RAD (Rapid Application Development) tool for designing GTK+ applications. Glade is a GTK+ application itself. It is simply a piece of software developers use to simplify the process of laying out an application's interface. Glade creates what will hereforth be refered to a s a "glade file". A glade file is actually an XML file which describes the heirachy of the widgets comprising the interface. Glade originally generated C code to build the GUI (and you'll still find examples and tutorials doing this). This was later discouraged in favor of using a library, Libglade, to build the interface at run time. And finally, as of Glade3, the old method has become deprecated. That means the ONLY thing glade does is allow you to generate a glade file which describes how the GUI is going to be built. This allows more flexibility with the developer, prevents having to re-compile applications when a minor interface change is needed, and allows more programming languages to be used with Glade. Glade3 has had significant changes since previous versions such as Glade2. Glade3 has been available for some time and you shouldn't have any problems obtaining it. The package manager for your distribution (yum, aptitude, etc.) should have Glade3 available. You should note however, that the package will have 3 in it. Where 'glade' may be the name for the old package, 'glade-3' or 'glade3' will be the package name for the new version on which this tutorial is based. Glade is also available from source at glade.gnome.org. Getting Familiar with the Glade Interface Start up Glade and let's get familiar with it's interface. I will be referring to various aspects of Glade by the names described here. On the left hand side is the "Palette". The Palette is like that of a graphics editing application. It is a palette of GtkWidgets which you can use to