next up previous
Next: Defining External Functions in Up: Standard Libraries Previous: UDP Datagrams (udp.ccl)


Graphics (windows.ccl)

The library lib/windows.ccl provides a simple interface to the GDK/GTK X Windows toolkit (see http://www.gtk.org/). To create a window, use one of the following

int new_window ( string )
int new_window_custom ( string, real, real, real )
The return value is an opaque window identifer, needed for future references to the window - so assign a variable to it. The string argument to both functions will be the window title. In the second function, the last three arguments are the scale factor, and the width and height of the window (in pixels). Thus
w := new_window_custom ( "data", 10.0, 100, 100 );
creates a new window 100 $\times$ 100 pixels big, named ``data''. The scale factor of 10.0 means that if you draw a line of length 1, with will be approximately 10 pixels long. The origin of the window is the center, x coordinates go from left to right, y coordinates fo from bottom to top. To draw something in a window, use one of the following functions
//          w    x1    y1    x2    y2
unit line ( int, real, real, real, real )

//          w    points          filled
unit poly ( int, real list list, bool )

//            w    x     y     r     filled
unit circle ( int, real, real, real, bool )

//         w    x     y     wid   hi    a1    a2    filled
unit arc ( int, real, real, real, real, real, real, bool ) 

//          w    x     y     text
unit text ( int, real, real, string )
The first argument to each of these functions is a window identifier created with one of the new window functions. The rest of the arguments are unique to the functions and should be apparent from the annotations above.

After drawing lines or polygons, you need to refresh the window before you will see anything. Do this using the function

unit refresh ( int )
which takes a window identifier as an argument.

If you want to draw in some color other than black (the default), use

unit setcolor ( int, string )
which takes a window identifier and a string that represents the color (currently, ``black'', ``white'', ``red'', ``blue'', ``green'' or ``yellow''). To erase a window, use
unit erase ( int )
and then refresh(). To process events in a window (like exposures, mouse clicks, etc), use
unit update_windows ()
You usually don't use this function, but instead compose your windows progams with the following program, which is in lib/windows.ccl:
program window_manager() := {
  true : {
    update_windows()
  };
};

For example, here is a program that makes a spinning red square with a black border:

include windows.ccl
include math.ccl

fun make_square r theta . 
  let c := r * cos ( theta ),
      s := r * sin ( theta ) in
    { { c, -s }, { s, c }, { -c, s }, { -s, -c } }
  end;

program spin ( r, delta ) := {

  theta := 0.0;
  w := new_window_custom ( "spin", 10.0, 30 * r, 30 * r );

  true : {
    erase ( w ),
    setcolor ( w, "red" ),
    poly ( w, make_square r theta, true ),
    setcolor ( w, "black" ),
    poly ( w, make_square r theta, false ),
    refresh ( w ),
    theta := theta + delta
  };
  
};

program p ( r, delta ) := spin ( r, delta ) + window_manager();
program main := p ( 10, 0.05 );

Another function that might be useful, if you want to save a graphic or make an animation for example, is the function

//                     w    filename
external unit tofile ( int, string )
  "libcclwindow.so" 
  "ccl_tofile";

which saves the image in the window identified by w into a file named filename. The file name should end with the graphics format you want: .jpg, .png, .ps or .ppm.


next up previous
Next: Defining External Functions in Up: Standard Libraries Previous: UDP Datagrams (udp.ccl)
Eric Klavins 2003-12-03