Now an C example translated to FreeBASIC it use the curent "x11.bi" file and it works so far. :rotfl:
Code:
' xtest7.bas test for c translatet XLib programm
' demonstrate handling of X events using an events loop.
option explicit
#include once "x11.bi"
dim shared Pixels(2600,2000) as short
' function: create_simple_window. Creates a window with a white background
' in the given size.
' input: display, size of the window (in pixels), and location of the window
' (in pixels).
' output: the window's ID.
' notes: window is created with a black border, 2 pixels wide.
' the window is automatically mapped after its creation.
function create_simple_window(byval display as XDISPLAY ptr, _
byval w as integer,byval h as integer, _
byval x as integer,byval y as integer) as windowid
dim screen_num as integer=XDefaultScreen(display)
dim win_border_width as integer= 2
dim as windowid win
' create a simple window, as a direct child of the screen's
' root window. Use the screen's black and white colors as
' the foreground and background colors of the window,
' respectively. Place the new window's top-left corner at
' the given 'x,y' coordinates.
win = XCreateSimpleWindow(display, XDefaultRootWindow(display), _
x, y, w, h, win_border_width, _
XBlackPixel(display, screen_num), _
XWhitePixel(display, screen_num))
' make the window actually appear on the screen.
XMapWindow display, win
'Flussh all pending requests to the X server.
XFlush display
return win
end function
function create_gc(byval display as XDISPLAY ptr,byval win as windowid,byval reverse_video as integer) as GContext
dim gc as GContext ' handle of newly created GC.
dim valuemask as uinteger ' which values in 'values' to
' check when creating the GC.
dim as XGCValues values ' initial values for the GC.
dim as uinteger line_width = 2 ' line width for the GC.
dim as integer line_style = LineSolid ' style for lines drawing and
dim as integer cap_style = CapButt ' style of the line's edje and
dim as integer join_style = JoinBevel ' joined lines.
dim as integer screen_num = XDefaultScreen(display)
'debug
print "create_gc";reverse_video
gc = XCreateGC(display, win, valuemask, values)
if (gc < 0) then
print "ERROR: XCreateGC"
end if
' allocate foreground and background colors for this GC.
if (reverse_video) then
XSetForeground display, gc, XWhitePixel(display, screen_num)
XSetBackground display, gc, XBlackPixel(display, screen_num)
else
XSetForeground display, gc, XBlackPixel(display, screen_num)
XSetBackground display, gc, XWhitePixel(display, screen_num)
end if
' define the style of lines that will be drawn using this GC.
XSetLineAttributes display, gc,line_width, line_style, cap_style, join_style
' define the fill style for the GC. to be 'solid filling'.
XSetFillStyle display, gc, FillSolid
return gc
end function
'
' function: handle_expose. handles an Expose event by redrawing the window.
' input: display, 2 GCs, XExposeEvent event structure, dimensions of
' the window, pixels array.
' output: none.
'
sub handle_expose(byval display as XDISPLAY ptr,byval gc as GContext,byval rev_gc as GContext, _
byref expose_event as XExposeEvent, _
byval w as integer,byval h as integer)
'debug
print "handel_expose",w,h
' if this is the first in a set of expose events - ignore this event.
if (expose_event.count > 1) then return
' draw the contents of our window.
' draw one pixel near each corner of the window
XDrawPoint display, expose_event.window, gc, 5 , 5
XDrawPoint display, expose_event.window, gc, 5 , h-5
XDrawPoint display, expose_event.window, gc, w-5, 5
XDrawPoint display, expose_event.window, gc, w-5, h-5
' draw two intersecting lines, one horizontal and one vertical,
XDrawLine display, expose_event.window, gc, 0, h\2, w, h\2
XDrawLine display, expose_event.window, gc, w\2, 0, w\2, h
' now use the XDrawArc() function to draw a circle
XDrawArc display, expose_event.window, gc,w\2-25, h\2-25, 250, 25, 0, 360*64
dim points(4) as Xpoint = {(0, 0), (50, 50), (0, 50), (0, 0) }
dim as integer npoints = 3
' draw a small triangle at the top-left corner of the window.
' the triangle is made of a set of consecutive lines, whose
' end-point pixels are specified in the 'points' array.
XDrawLines display, expose_event.window, gc, points(0), npoints, CoordModeOrigin
' draw a rectangle whose top-left corner is at '120,150', its width is
' 50 pixels, and height is 60 pixels.
XDrawRectangle display, expose_event.window, gc, 120, 150, 50, 60
' draw a filled rectangle of the same size as above, to the left of the
' previous rectangle.
XFillRectangle display, expose_event.window, gc, 60, 150, 50, 60
' finally, draw all the pixels in the 'pixels' array.
dim as integer x, y
for x=0 to w
for y=0 to h
select case pixels(x,y)
case 1 'draw point
XDrawPoint display, expose_event.window, gc, x, y
case -1 ' erase point.
XDrawPoint display, expose_event.window, rev_gc, x, y
end select
next
next
XFlush display
end sub
'
' function: handle_drag. handles a Mouse drag event - if the left button
' is depressed - draws the pixel below the mouse pointer. if the
' middle button is depressed - erases the pixel below the mouse
' pointer.
' input: display, 2 GCs, XButtonEvent event structure, dimensions of
' the window, pixels array.
' output: none.
sub handle_drag(byval display as XDISPLAY ptr,byval gc as GContext,byval rev_gc as GContext, _
byref drag_event as XButtonEvent,byval w as uinteger,byval h as uinteger)
dim as integer x, y
' invert the pixel under the mouse.
x = drag_event.x
y = drag_event.y
print "handel_drag ",x,y,drag_event.state
select case (drag_event.state)
case Button1Mask ' draw the given pixel in black color.
XDrawPoint display, drag_event.window, gc, x, y
pixels(x,y) = 1
case Button2Mask ' draw the given pixel in white color.
XDrawPoint display, drag_event.window, rev_gc, x, y
pixels(x,y) = -1
end select
end sub
' function: handle_button_down. handles a Mouse press event - if the left
' button is depressed - draws the pixel below the mouse pointer.
' if the middle button is depressed - erases the pixel below the
' mouse pointer.
' input: display, 2 GCs, XButtonEvent event structure, dimensions of
' the window, pixels array.
' output: none.
sub handle_button_down(byval display as XDISPLAY ptr,byval gc as GContext,byval rev_gc as GContext, _
byref button_event as XButtonEvent ,byval w as uinteger,byval h as uinteger)
dim as integer x, y
' invert the pixel under the mouse.
x = button_event.x
y = button_event.y
print "handle_button_down";x,y,button_event.button
select case (button_event.button)
case Button1 ' draw the given pixel in black color.
XDrawPoint display, button_event.window, gc, x, y
pixels(x,y) = 1
case Button3 ' draw the given pixel in white color.
XDrawPoint display, button_event.window, rev_gc, x, y
pixels(x,y) = -1
end select
end sub
'
' main
'
dim display as XDISPLAY ptr ' pointer to X Display structure.
dim as integer screen_num ' number of screen to place the window on.
dim as windowid win ' pointer to the newly created window.
dim as uinteger display_width ' height and width of the X display.
dim as uinteger display_height
dim as uinteger w ' height and width for the new window.
dim as uinteger h
dim as GContext gc ' GC (graphics context) used for drawing
dim as GContext rev_gc ' in our window.
' open connection with the X server.
display = XOpenDisplay("")
if (display = NULL) then
print "cannot connect to X server"
end 1
end if
' get the geometry of the default screen for our display.
screen_num = XDefaultScreen(display)
display_width = XDisplayWidth (display, screen_num)
display_height = XDisplayHeight(display, screen_num)
' make the new window occupy 1/9 of the screen's size.
w = (display_width / 3)
h = (display_height / 3)
' create a simple window, as a direct child of the screen's
' root window. Use the screen's white color as the background
' color of the window. Place the new window's top-left corner
' at the given 'x,y' coordinates.
win = create_simple_window(display, w, h, 0, 0)
' allocate two new GCs (graphics contexts) for drawing in the window.
' the first is used for drawing black over white, the second is used
' for drawing white over black.
gc = create_gc(display, win, 0)
rev_gc = create_gc(display, win, 1)
' subscribe to the given set of event types.
XSelectInput display, win, ExposureMask or KeyPressMask or ButtonPressMask or _
Button1MotionMask or Button2MotionMask or StructureNotifyMask
' perform an events loop
dim as integer done
dim as XANYEVENT an_event
while done=0
XNextEvent display,an_event
select case (an_event.xevent.eventtype)
case Expose
' redraw our window.
handle_expose display, gc, rev_gc, an_event.xexpose, w, h
case ConfigureNotify
' update the size of our window, for expose events.
w = an_event.xconfigure.width
h = an_event.xconfigure.height
case ButtonPress
' invert the pixel under the mouse pointer.
handle_button_down display, gc, rev_gc,an_event.xbutton, w, h
case MotionNotify
' invert the pixel under the mouse pointer.
handle_drag display, gc, rev_gc,an_event.xbutton, w, h
case KeyPress
' exit the application by braking out of the events loop.
done = 1
case else
' ignore any other event types.
end select
wend
' free the GCs.
XFreeGC display, gc
XFreeGC display, rev_gc
' close the connection to the X server.
XCloseDisplay display
end