Hacking GTK+
-Muthiah A, Octave-Gtk+ team

Hacker Crackdown-2005 GLUG-T

Muthiah Annamalai [gnumuthu@users.sf.net]

2005 January 21,22,23

NIT-Trichy, India

These slides are licensed under GNU FDL

Hacking GTK+
-Muthiah A, Octave-Gtk+ team

To show the power and utilityof GTK toolkit

Design & Architecture of GTK

Tools of trade & writing custom GTK widgets

Hacking GTK. Tips and tricks, demos

Objectives

Dole not fish, Teach them fishing.

Hacking GTK+, -Muthiah A, Octave-Gtk+ team

GTK is GIMP ToolKit

GTK library is used to create GUI's.

Industrial strength applications written using GTK like GNOME, XFCE, GIMP, ANJUTA.

Tools for Image processing, Word processing, Games, Front ends, terminal emulators, simulation program are written using GTK+

Desktop environments, POS, Installation programs, Wizards, Setup/Configuration programs: real software is written with GTK+.

Utility of GTK+

Uses

Features

GTK library is used to create GUI's.

GTK is cross platform. On GNU/Linux, *nix, and M$ windows, MAC OS X.

API is available in many languages. C#,C,C++,Perl,Python,Ruby,Lisp, Scheme,PHP language bindings are available.

Written in C, works for X window system on *NIX, and LGPL'ed.

Simple API, good documentation, and user support, mailing lists, example codes, books.

Easy installation, maintenance, coding.

Hacking GTK+, -Muthiah A, Octave-Gtk+ team

Power of GTK+

Light weight layer over X window system.

GTK library supports a type system, and run time introspection.

Internationalization: PANGO

Themeable , customizeable

Layered architecture makes development easy.

Third party widgets

Automatic library scripts

Transparency, animation

GL extensions

Non-Rectangular shaped widgets.

Support for custom widgets.

Drag and Drop

Language bindings are easily written for GTK.

Hacking GTK+, -Muthiah A, Octave-Gtk+ team

History of GTK+

GIMP Tool Kit was written by peter mattis, and spencer kimball at UCB.

GIMP [ GNU Image Manipulation Program ], an Image Processing tool , gave birth to GTK.

GTK was adopted as basis for GNOME project.

GTK used in major applications like Dia, AbiWord, XMMS.

GTK is a populay toolkit with several language bindings like C++,Perl,Python.

1997

1998

1999

1999

2001

Hacking GTK+, -Muthiah A, Octave-Gtk+ team

Developer Platform for GTK+

Standard GNU/Linux system with X window system

GCC

Pkg-config & GNU Auto tools

Glib, Gdk , GTK libraries { /usr/lib/gtk-2.0/ }

GTK+ developer header files, and documents { /usr/include/gtk-2.0/[gtk|gdk|gdk-pixbuf]}

Editors, IDE's

Extra languages , language bindings

GUI Builder GLADE, and libGlade library.

GNOME, GIMP, DIA

Additional GtkExtra libraries.

Hacking GTK+, -Muthiah A, Octave-Gtk+ team

Tools for GTK+

GNU Emacs, Anjuta [IDE/Editors]

Doxygen [Code reader]

GNU Auto tools [Configure ,Make etc]

Mozilla, [Help browser]

GDB [ Debugger ]

GLADE [ UI Builder ]

LibGLADE [Interface loader]

Hacking GTK+, -Muthiah A, Octave-Gtk+ team

Hello World: GTK+

int main(int argc, char *argv[])

{

GtkWidget *w,*btn;

gtk_init();

w=gtk_window_new(GTK_WINDOW_TOPLEVEL);

btn=gtk_button_new_with_label(“Hello World”);

gtk_window_set_title(GTK_WINDOW(w),”Hello World”);

gtk_container_add(GTK_CONTAINER(w),btn);

g_signal_connect(G_OBJECT(btn),”clicked”, G_CALLBACK(gtk_main_quit),NULL);

gtk_widget_show_all(w);

gtk_main();

}

Declaration + Initialization

Widget Instantiation

Packing

Entering Main Loop

Event Callbacks

Hacking GTK+, -Muthiah A, Octave-Gtk+ team

Events & Signals in GTK+

While(events pending)

Lookup callback

for event

Callback

Main Loop

exit(0)

Re Painting

Events

Expose

KeyPress

ButtonPress

Enter

Leave

Focus

Map

UnMap

Drag

Drop

Delete

Destroy

Hacking GTK+, -Muthiah A, Octave-Gtk+ team

Standard GTK+ Widgets

Standard Widgets

Button

Label

Entry

Radio

Check

Menubar

Menu

Scrollbar

Progressbar

Ruler

Scale

Dialogs

Layout Widgets

Window

Container

Box

Table

Scrolled Window

Drawing Widgets

Drawing Area

Window

Curve

MVC Widgets

TreeView

TextView

ListView

Misc Widgets

Pane

Separators

Tearoff menu items

Tooltips

Preview

Notebook

Hacking GTK+, -Muthiah A, Octave-Gtk+ team

gtk_[type]_[function](Gtk[Type] *ptr, ...)

Eg: gtk_entry_get_text(GtkEntry *)

gtk_button_set_label(GtkButton *,char *)

Properties have get/set accessors for each object.

Member functions must be passed their parent instances as the first argument. gtk_widget_set_name(GtkWidget *,char *)

Conventions in GTK+

Member Functions

Variable Names

typedef struct _Gtk[Type] Gtk[Type];

Enumerations / Macros

GTK_[TYPE]_[OPTION1]

GTK+ Architecture
-Muthiah A, Octave-Gtk+ team

GTK+ is written in C

It uses OO programming paradigm in C

GTK+ is built on top of GLIB,GObject and the GDK libraries.

GTK+

Gobject

GDK+

Glib

GTK+ layers

GTK+ http://www.gtk.org

GTK+ Type system
-Muthiah A, Octave-Gtk+ team

Glib & Gobject implement type system

Static & dynamic type registration

Classes & Objects can be created

Inheritance,polymorphism & encapsulation

GTK+ is GIMP Tool Kit, used in making GIMP.

G_TYPE_

FUNDAMENTAL

G_TYPE_OBJECT

GDK_TYPE_*

GTK_TYPE_*

GTK+ C OOP Vs C++
-Muthiah A, Octave-Gtk+ team

GTK+ C code, makes write easy wrappers. GTK+ bindings exist for more than 10 languages.

C++ code:

class usa: private hitler

{

dictator president;

timespan fouryears;

decmocracies UN[198];

vector<villains> terrorists;

america(string name);

private greencard(void);

void vote(citizen kane);

};

C code:

typedef struct _usa usa;

typedef struct _usaclass usaclass;

struct _usa

{

dictator president;

timespan fouryears;

democracies UN[198];

GSList terrorists;

}

GTK+ C OOP Vs C++
-Muthiah A, Octave-Gtk+ team

GTK+ C code has canonical code, for making standard

types and classes, which g++ makes for our C++ programs

C++ code:

class usa: private hitler

{

dictator president;

timespan fouryears;

decmocracies UN[198];

vector<villains> terrorists;

america(string name);

private greencard(void);

void vote(citizen kane);

};

C code: [contd]

struct _usaclass

{

GObject parent_class;

void (*greencard)(void);

}

usa_get_type(void);

usa_new(char *name);

usa_greencard(void);

usa_vote(citizen *kane);

Guts of GTK+ Type system
-Muthiah A, Octave-Gtk+ team

usa_get_type(void)

{

static int x=0;

if(!x)

{ static struct Gtype info

={

// base class init/clean

// our class init/clean

// other things Vtables

};

x=g_type_register_static()

}

return x;

}

usa_class_init(usaclass *klass)

{

hitler *class=(hitler *)klass;

class->vote=usa_vote;

klass->greencard=usa_greencard;

};

usa_init(usa *obj)

{

obj->president=”Abe Lincoln”;

obj->fouryears=4;

obj->terrorists=NULL;
}

GTK+ types are inited when we call gtk_init(), before creating any widgets.

GTK+ Inheritance
-Muthiah A, Octave-Gtk+ team

GTK+ is very fast because of the type system it implements, instead of depending on compilers.

On calling gtk_init() we setup the fundamental types

Other types are initialized when we first create them.

The gtk_window_new() class of functions register types.

Type system allows us to create user defined types.

New objects, inherited from Gobject, GTKObject or derived ones.

GTK classes are inited once.

gtk_windowclass_init registers the member functions and overrides the parent class types/member-functions.

Those function's of the parent we fail to override, will fall through to the parent. That is parents functions will be called if our handler [function pointer] is NULL.

So GTK+ is OOP get it!

GTK+ overview
-Muthiah A, Octave-Gtk+ team

GTK+ is glues well to scripting languages due to its

C code base; this makes GTK bindings popular

GTK system

GDK system

Drawing Functions

Xlib

Native Drawing

Signal & Event system

GObject system

Glib system

Threads & Main Loop

User Code

Enter main loop

Callbacks

GTK+ examples
-Muthiah A, Octave-Gtk+ team

GTK+ system is event driven!

We can extend GTK+ widgets to create our OWN. One such is the GTK dial widget.

GIMP main widget.

GTK+ examples
-Muthiah A, Octave-Gtk+ team

Access GTK+ demo programs by

[muthu@EarthSimulator]$ gtk-demo & #enjoy gtk

The GTK demo suite of programs

GTK+ examples
-Muthiah A, Octave-Gtk+ team

GTK+ system is event driven!

GTK Application program

GTK programs are interactive!

GTK+ Reference
-Muthiah A, Octave-Gtk+ team

Im writing a book on GTK+ along with Ishan Chattopadyay, who maintains GTK+ port for windows.

www.gtk.org , is the primary location for documents & source code.

www.gnome.org ,is the biggest project using GTK+

Gnome desktop is written fully on GTK+

Lots of mailing lists at http://mail.gnome.org

The gtk-app-devel@lists.gnome.org is a good place to start

Online Books by Havoc Pennington, GGAD ,free from http://developer.gnome.org , check it out

GTK+ Resources
-Muthiah A, Octave-Gtk+ team

Click on http://cybernetics.freewebspace.com/gtk

and get this whole presentation as a tarball.

Language Bindings for:

C API is automatically what the GTK+2.4 releases come with. Get it from www.gtk.org .

C++: gtkmm, a superb uptodate in touch with the latest GTK+2.4 sources http://gtkmm.sf.net

Python: http://www.pygtk.org ,is also hot on the trails of GTK+2.4

Perl : http://perl-gtk.sf.net is another uptodate binding.

Java: Java GNOME binding is also uptodate.

GTK#: The binding for C# is pushed by MONO project. See http://www.mono.com and http://gtksharp.sf.net .

Haskell, C# , Clisp , R and many more languages have GTK+ bindings.

Octave GTK bindings are released as on 8-02-2005 http://octave-gtk.sourceforge.net

Thanks & Credits

GTK+ project, mailing lists, free software projects in general

GNU/Linux User Group Trichy

GNU

Friends at NIT-T

To friends who know, who they are.

These slides are licensed under GNU FDL

2004-07-21T20:56:07