next up previous index
Next: Drawing Text Up: Using Xlib Primitives Previous: Initialisation

Predefined Predicates

To make the use of Xlib possible even for non-experts, ProTcXl provides a number of predefined predicates that interface the most frequently used Xlib functions. They are described in Appendix B. For instance,

xlib_foreground(XID, Colour)
sets the foreground colour of a given XID,
xlib_fill_rectangle(XID, X, Y, Width, Height)
draws a rectangle (without outline) with the current foreground colour, whereas xlib_rectangle/5   draws only the outline. A very simple Xlib program to display a purple square with a black outline would be:
:- lib(tk).
:- lib('tk/xlib').

top :-
    tk([]),
    tcl update,
    xlib_init(., XID),
    xlib_foreground(XID, purple),
    xlib_fill_rectangle(XID, 10, 10, 100, 100),
    xlib_foreground(XID, black),
    xlib_rectangle(XID, 10, 10, 100, 100).


Now displaying a square does not seem to be particularly slow in Tk and thus not much faster with Xlib. Let us display a table of small squares, flushing after each row to see the speed (file te.pl ):

:- lib(tk).

draw_squares(Canvas, Color, Size, N) :-
    draw_squares(Canvas, Color, 0, N, Size).

draw_squares(_, _, N, N, _) :-
    !.
draw_squares(Canvas, Color, J, N, Size) :-
    draw_row(Canvas, Color, J, 0, N, Size),
    tcl update,
    J1 is J + 1,
    draw_squares(Canvas, Color, J1, N, Size).

draw_row(_, _, _, N, N, _) :-
    !.
draw_row(Canvas, Color, J, I, N, Size) :-
    X1 is I*(Size + 2) + 2,
    Y1 is J*(Size + 2) + 2,
    X2 is X1 + Size,
    Y2 is Y1 + Size,
    tcl('## create rectangle ## ## ## ## -outline ##',
            [Canvas, X1, Y1, X2, Y2, Color]),
    I1 is I + 1,
    draw_row(Canvas, Color, J, I1, N, Size).
draw_squares/4 draws a table of NxN squares of size Size into canvas Canvas:
[eclipse 8]: tk([]), tcl 'canvas .c -width 180 -height 180; pack .c'.

yes.
[eclipse 9]: draw_squares('.c', blue, 5, 25).

yes.


The display is quite slow, because Tk does a lot more than just display small squares, it creates objects and their data structures and is able to assign events to objects etc. If we only want to display the squares, we would take the same program with Xlib, file xe.pl. xlib_flush/1   flushes the X server queue and forces   the execution of all pending operations, similarly to the Tk update   command:

:- lib(tk).
:- lib('tk/xlib').

draw_squares(Path, Color, Size, N) :-
    xlib_init(Path, XID),
    xlib_foreground(XID, Color),
    draw_squares1(XID, 0, N, Size).

draw_squares1(_, N, N, _) :-
    !.
draw_squares1(XID, J, N, Size) :-
    draw_row(XID, J, 0, N, Size),
    xlib_flush(XID),
    J1 is J + 1,
    draw_squares1(XID, J1, N, Size).

draw_row(_, _, N, N, _) :-
    !.
draw_row(XID, J, I, N, Size) :-
    X1 is I*(Size + 2) + 2,
    Y1 is J*(Size + 2) + 2,
    xlib_rectangle(XID, X1, Y1, Size, Size),
    I1 is I + 1,
    draw_row(XID, J, I1, N, Size).
When we now execute the same query, we see the same picture, but the display is much faster:
[eclipse 12]: tk([]), tcl 'canvas .c -width 180 -height 180; pack .c'.

yes.
[eclipse 13]: draw_squares('.c', blue, 5, 25).

yes.


next up previous index
Next: Drawing Text Up: Using Xlib Primitives Previous: Initialisation



Micha Meier
Tue Jul 2 09:49:39 MET DST 1996