3) The Camlwin library.

All the variables needed to define a graphics object are packed into a structure which is dependent on the object. Each graphics object has a draw function and a management function. For example, a Button object, of type gr_button, is drawn with gr_draw_button Button. The management function reads the input events and modify the state of the object in consequence. The management function takes an object corresponding to the function and an event and return a boolean to say if the event is treated or not by the function.

The set of graphics objects is:

The objects can take numerous forms, depending on the state of a global variable that can be changed with gr_set_look and read with gr_get_look. This variable is of type gr_look_list. It can take the following values:
type gr_look_list =
    Std_color
  | Std_mono
  | Window_color
  | Os2_color
  | Motif_color
  | Open_look_color
  | Open_look_mono
  | Next_color
  | Mac_mono
  | Mac_color
;;
the look can be set with the system variable CAMLLOOK. It can take the following values:
type window =
  { mutable win_def : gr_window
  ; mutable win_objects : graph_obj list
  }
;;
A window is defined by its characteristics (in win_defwin_def, there is the size, the coordinates, the identifier number and the state of the window) and the list of all the graphical objects present in the window (win_objectswin_objects).

A program starts by opening the window and Camlwin prototypes (#open "windows";; #open "camlwin";;). Then the windows characteristics, the objects, and the windows themselves are defined. The program ends with the call to the main loop, the loop that gets and deals with all the events and manages the objects.

A simple example is shown in the next chapter.

3.1) An example.

#open "windows";;
#open "camlwin";;


  (* defines the window characteristics *)
let proj_win_def =
{
  win_left     = 4;
  win_top      = 461;
  win_width    = 200;
  win_height   = 150;
  win_id       = 0;
  win_state    = Destroyed
};;

  (* defines a centered prompt in the top of the window *)
let proj_title =
{
  pt_window   = proj_win_def;
  pt_left     = 49;
  pt_top      = 145;
  pt_name     = "Demo List"
};;

  (* defines a centered button in the bottom of the window. This
button closes the window *)
let proj_close =
{
  bt_window   = proj_win_def;
  bt_left     = 70;
  bt_top      = 32;
  bt_width    = 50;
  bt_height   = 25;
  bt_state    = Up;
  bt_name     = "Close";
  bt_callback = gr_do_nothing
};;

  (* defines an empty list *)
let proj_list =
{
  li_window   = proj_win_def;
  li_left     = 4;
  li_top      = 130;
  li_width    = 190;
  li_height   = 85;
  li_nu_item  = 0;
  li_1st_item = 0;
  li_scroll   = 0;
  li_items    = [| |];
  li_callback = gr_do_nothing
};;
  (* defines the window itself *)
let proj_win =
{
  win_def      = proj_win_def;
  win_objects  = [
                  g_prompt    proj_title;
                  g_button    proj_close;
                  g_list      proj_list
                 ]
};;

  (* changes the callback of the button in order to end the
program when pressed *)
proj_close.bt_callback < - gr_quit_callback;;

  (* Calls the main loop *)
gr_main_loopgr_main_loop [ proj_win ];;