A simple starting guide for mlglade
Benjamin Monate
1 Introduction
Glade is a freely available graphical
interface builder. It is very
powerful and enables one to quickly visually design an interface.
It can export source code for various languages like C, C++, Perl,
Eiffel. Mlglade adds support for the OCaml language. It helps you to
build ocaml applications with a gtk user interface without using
libglade.
For a more in-depth view of Glade see the homepage of glade.
Before you continue, you have to install these tools :
On a debian system, this is achieved by having the following packages
installed:
-
ocaml, version 3.04 or higher,
- liblablgtk-ocaml-dev,
- One of glade, glade-gnome, or
glade-gnome-db, of version 0.6 or higher.
2 Hello World
Let's build your first application with mlglade. At the end of this
tutorial, you will have an application looking like this one :

If you have already used glade just skip the first part and design a
glade hello.glade project containing a window named ''my_window''
with a two rows vertical box. Put a label with ''Hello World'' inside
and a button labeled ''Quit''.
-
Basic design.
-
Launch glade. It opens three windows : the main window, the
palette window and the properties window.
- In the palette window click on the top leftmost icon whose
tooltip is ''Window''. This creates a new window called ''window1''
: this is your application's main window.
- In the properties window, change the ''Name'' from ''window1''
to ''my_window''. This is the ocaml identifier for your window.
- Change the ''Title'' into ''Hello World''. Note that your
application's window is updated automatically.
- In the Palette window click on the ''Vertical Box'' icon.
- Click in your application's window and choose 2 rows.
- In the Palette window click on the ''Label'' icon.
- Click in the upper part of your application's window.
- In the properties window change the ''Label'' field into ''Hello
world''
- In the Palette window click on the ''Button'' icon and the in
the lower part of your application's window.
- Change the ''Label'' from ''button1'' to ''Quit''.
- Save you project with the ''Project Name'' hello in a new
directory.
- You don't need to close glade as you will use it again later.
- Generating the first ocaml program.
-
Go in the project's directory and run ''mlglade
hello.glade''. No output should be produced.
- Type ''make''.
- Run ''./hello''
- Kill your application. Closing the window is not enough !
- Reacting to events. Now we want to close the application when the
''Quit'' button is clicked.
-
In Glade, select your ''Quit'' button and open the ''Signal''
panel of the properties window.
- Click on the ''...'' icon in front of ''Signal''.
- Select ''clicked'' then click ''ok''.
- Change the ''Handler'' in to ''quit''. This is the name of the
function called by your application when the button is clicked.
- Click on ''Add''.
- Save your project and regenerate with ''mlglade hello.glade''.
You should read two warnings that you can ignore.
- Now recompile with ''make'' and run your application
''./hello''.
- Notice what happens when you click on the ''Quit''
button. This is the default handler for the event.
- Edit with your favorite editor the file
''hello_glade_main.ml''. This file is created only once by
mlglade and will never be overwritten.
It looks like :
(* THIS FILE WILL NEVER BE OVERWRITTEN.
Use it as a template for your own main module.*)
open GMain
class customized_callbacks = object(self)
inherit Hello_glade_callbacks.default_callbacks
end
let main () =
let callbacks = new customized_callbacks in
let my_window = new Hello_glade_interface.top_my_window callbacks in
let _ = GtkBase.Widget.add_events my_window#my_window#as_widget [`ALL_EVENTS] in
let _ = my_window#my_window#show() in
Main.main ()
let _ = Printexc.print main ()
- To define your own callback for ''Quit'' button you have to
override the default one in the class customized_callbacks. Modify
the class like this :
class customized_callbacks = object(self)
inherit Hello_glade_callbacks.default_callbacks
method quit () = exit 0
end
- Recompile and click on your ''Quit'' button to test.
Now you can add other callbacks to your application using glade and
then implement them in your application.
3 Interacting with widgets
You can access all the widgets from the callbacks methods. You just need
to know the name you gave to them in glade.
For example, in the ''hello'' example, if you want to change the text of the
label when the button is clicked, you need to modify the method
quit like this :
method quit () =
let the_label= self#top_my_window#label1 in
the_label#set_text "Another Label"
All widgets are available as methods of self#top_my_window.
There is no tree structure of the widgets. Therefore you should never
give the same name for two widgets in glade.
4 Building real world applications
The first thing you will change is the ''makefile''. The generated one
is very basic and stupid but explains how to compile an application
with gtk support. It can be (and should be) modified at will to fit
your needs.
The second file you want to edit is the hello_glade_main.ml. You
can use it as a guide for your application, but you can ignore it
completely if you want and know how to initialize the lablgtk
library. You just need to define in one of your modules class
inheriting from Hello_glade_callbacks.default_callbacks
which defines the skeleton of your interface.
In any case contact me for help, suggestions, comments and/or
congratulations.
5 Supported widgets
This is the list of widgets you can use in glade without problems.
They are mainly on the GTK+ Basic page of the palette window.
-
GtkButton (no stock buttons)
- GtkCheckb
- GtkColorSelectionDialog
(you cannot change the labels of the buttons inside glade)
- GtkCombo (no method connect available in GEdit.combo : lablgtk pb ???)
- GtkDrawingArea
- GtkEntry
- GtkEventBox
- GtkFileSelection (you cannot change the labels of the buttons inside glade)
- GtkFontSelectionDialog
(you cannot change the labels of the buttons inside glade)
- GtkHBox
- GtkHButtonBox
- GtkHPaned
- GtkHSeparator
- GtkHandleBox
- GtkLabel
- GtkList
- GtkMenu
- GtkMenuBar
- GtkMenuItem
- GtkNoteBook
- GtkOptionMenu
- GtkPixmap
- GtkProgressBar
- GtkScrolledWindow
- GtkSpinButton
- GtkStatusBar
- GtkTable
- GtkText
- GtkToolbar
- GtkToggleButton
- GtkTree
- GtkVBox
- GtkVButtonBox
- GtkVPaned
- GtkVSeparator
- GtkViewPort
- GtkWindow
6 Contact information
The latest version of this file is available from
http://www.lri.fr/~monate/mlglade.
You can contact the author :
Benjamin Monate
Benjamin.Monate@lri.fr
This document was translated from LATEX by
HEVEA.