Module Gctx

module Gctx: sig .. end

The "Graphics Context" component of the GUI library.


A graphics context represents a portion of the window to which widgets will be drawn.

The drawing primitives in this module are all relative to the graphics context. This means that when a widget needs to draw on the screen, it need not know its absolute position. The graphics context is responsible for translating the relative positions passed into the drawing routines into absolute positions on the screen.

The graphics context also includes other information for basic drawing (such as the current pen color.)

Note that this module defines a persistent (immutable) data structure. The operations here use a given graphics context to create a new one with the specified characteristics. They do not modify their arguments.

type gctx 

The main (abstract) type of graphics contexts.

Colors

type color = {
   r : int;
   g : int;
   b : int;
}

A type for colors, specified by red, green and blue values in the range 0 .. 255 inclusive

val black : color
val white : color
val red : color
val green : color
val blue : color
val yellow : color
val cyan : color
val magenta : color

Basic Gctx operations

val open_graphics : unit -> unit

Open the graphics window

val clear_graph : unit -> unit

Clear the graphics window

val top_level : gctx

The top-level graphics context

val translate : gctx -> int * int -> gctx

Produce a new gctx shifted by (dx,dy)

val with_color : gctx -> color -> gctx

Produce a new gctx with a different pen color

Drawing

type position = int * int 

A widget-relative position

type dimension = int * int 

A width and height paired together.

Various primitive drawing routines. Arguments are widget-local coordinates.

val draw_line : gctx -> position -> position -> unit

Draw a line between the two specified positions

val draw_string : gctx -> position -> string -> unit

Display text at the given position

val draw_rect : gctx -> position -> dimension -> unit

Draw a rectangle, with lower-left corner at position of the specified dimension.

val fill_rect : gctx -> position -> dimension -> unit

Display a filled rectangle with lower-left corner at position with the specified dimension.

val draw_ellipse : gctx -> position -> int -> int -> unit

Draw an elipse, centered at position with given x and y radii.

val text_size : string -> dimension

Calculates the size of text when rendered.

Event Handling

type event_type = 
| KeyPress of char
| MouseDown
| MouseUp
| MouseMove
| MouseDrag

Types of events that could occur

Events produced by the user-interface. Each event contains a type and a position.

type event = event_type * position 

An event records its type and the widget-local position of the mouse when the event occurred.

val event_type : event -> event_type

Accessor for the type of an event.

val event_pos : event -> gctx -> position

Accessor for the widget-local position of an event.

val make_test_event : event_type -> position -> event

Make an event by hand for testing.