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 :

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''.
  1. Basic design.
  2. Generating the first ocaml program.
  3. Reacting to events. Now we want to close the application when the ''Quit'' button is clicked.
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 a 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.

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.