Differenze tra le versioni di "Creare una finestra mediante le funzioni del API di X11"

Da Gambas-it.org - Wikipedia.
 
(27 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
 
Con le risorse del API di X11 è possibile creare una finestra ed interagire con essa.
 
Con le risorse del API di X11 è possibile creare una finestra ed interagire con essa.
  
Sarà necessario richiamare la libreria di X attualmente: "''libX11.so.6.3.0''"
+
Sarà necessario richiamare nell'applicazione Gambas la libreria dinamica condivisa di X11: "''libX11.so.6.4.0'' ".
  
 +
===Gli Eventi nel sistema X11===
 +
Ogni tipo di Evento ha una propria ''Struttura'', che in realtà è una "Union" tipica del linguaggio C. Nella libreria di X11 tutte le ''Strutture'' degli Eventi hanno in comune questi primi 5 membri: <SUP>&#091;[[#Note|Nota 1]]&#093;</sup>
 +
typedef struct {
 +
        int type;
 +
        unsigned long serial;
 +
        Bool send_event;
 +
        Display *display;
 +
        Window window;
 +
} XAnyEvent;
 +
Pertanto, nella ricostruzione in Gambas della ''Struttura'' tipica (la "Union") di un determinato Evento bisognerà tenerne conto.
 +
<BR>A questi primi 5 membri comuni seguiranno i restanti caratteristici dell'Evento specifico.
  
===Creare una semplicemente finestra colorata===
+
Poiché la dimensione totale della ''Union'' è pari a 192 byte, per evitare un errore alla chiusura della finestra creata, è necessario colmare la ''Struttura'', ricostruita in Gambas, con un numero di byte sino a raggiungere la dimensione totale della ''Union'' medesima (192 byte).
Mostriamo di seguito un breve codice con le risorse essenziali della libreria X11 per creare una semplice finestra di colore rosso. Se si cliccherà al suo interno con un qualsiasi tasto del mouse, la finestra verrà distrutta:
+
 
  Library "libX11:6.3.0"
+
 
 +
==Esempi pratici==
 +
Mostriamo di seguito alcuni esempi pratici, nei quali ssaranno create delle finestre, sulle quali si potrà agire attraverso la gestione di uno specifico Evento.
 +
 
 +
===Creare semplicemente una finestra colorata===
 +
Mostriamo di seguito un breve codice con le risorse essenziali della libreria X11 per creare una semplice finestra di colore rosso. Se si cliccherà al suo interno con un qualsiasi tasto del mouse, la finestra verrà distrutta.
 +
  Library "libX11:6.4.0"
 
   
 
   
  Private Const ButtonPressMask As Byte = 4
+
  Private Const ButtonPressMask As Long = 4
  Private Enum KeyPress = 2, KeyRelease, ButtonPress, ButtonRelease, MotionNotify, EnterNotify, LeaveNotify,
+
  Private Enum KeyPress = 2, KeyRelease, ButtonPress, ButtonRelease, MotionNotify, EnterNotify,
                            FocusIn, FocusOut, KeymapNotify, Expose, GraphicsExpose, NoExpose
+
              LeaveNotify, FocusIn, FocusOut, KeymapNotify, Expose, GraphicsExpose, NoExpose
 
   
 
   
 
  <FONT color=gray>' ''Display *XOpenDisplay(char *display_name)''
 
  <FONT color=gray>' ''Display *XOpenDisplay(char *display_name)''
 
  ' ''Opens a connection to the X server that controls a display.''</font>
 
  ' ''Opens a connection to the X server that controls a display.''</font>
  Private Extern XOpenDisplay(display$ As String) As Pointer
+
  Private Extern XOpenDisplay(display As Pointer) As Pointer
 
    
 
    
 
  <FONT color=gray>' ''Window XDefaultRootWindow(Display *display)''
 
  <FONT color=gray>' ''Window XDefaultRootWindow(Display *display)''
 
  ' ''Return the root window for the default screen.''</font>
 
  ' ''Return the root window for the default screen.''</font>
  Private Extern XDefaultRootWindow(displayP As Pointer) As Integer
+
  Private Extern XDefaultRootWindow(display As Pointer) As Integer
 
   
 
   
 
  <FONT color=gray>' ''Window XCreateSimpleWindow(Display *display, Window parent, int x, int y, unsigned int width, unsigned int height, unsigned int border_width, unsigned long border, unsigned long background)''
 
  <FONT color=gray>' ''Window XCreateSimpleWindow(Display *display, Window parent, int x, int y, unsigned int width, unsigned int height, unsigned int border_width, unsigned long border, unsigned long background)''
  ' ''creates an unmapped InputOutput subwindow for a specified parent window, returns the window ID of the created window.''</font>
+
  ' ''Creates an unmapped InputOutput subwindow for a specified parent window, returns the window ID of the created window.''</font>
  Private Extern XCreateSimpleWindow(displayP As Pointer, parent As Long, x As Integer, y As Integer, width As Integer, height As Integer, border_width As Integer, border As Integer, background As Long) As Integer
+
  Private Extern XCreateSimpleWindow(display As Pointer, parent As Long, x As Integer, y As Integer, width As Integer, height As Integer, border_width As Integer, border As Integer, background As Long) As Integer
 
   
 
   
 
  <FONT color=gray>' ''XSelectInput (Display *display, Window w, long event_mask)''
 
  <FONT color=gray>' ''XSelectInput (Display *display, Window w, long event_mask)''
  ' ''requests that the X server report the events associated with the specified Event mask.''</font>
+
  ' ''Requests that the X server report the events associated with the specified Event mask.''</font>
  Private Extern XSelectInput(displayP As Pointer, w As Long, event_mask As Long)
+
  Private Extern XSelectInput(display As Pointer, w As Long, event_mask As Long)
 
   
 
   
 
  <FONT color=gray>' ''XMapRaised (Display *display, Window w)''
 
  <FONT color=gray>' ''XMapRaised (Display *display, Window w)''
  ' ''raises the specified window to the top of the stack.''</font>
+
  ' ''Raises the specified window to the top of the stack.''</font>
  Private Extern XMapRaised(displayP As Pointer, w As Long)
+
  Private Extern XMapRaised(display As Pointer, w As Long)
 
   
 
   
 
  <FONT color=gray>' ''XNextEvent (Display *display, XEvent *event_return)''
 
  <FONT color=gray>' ''XNextEvent (Display *display, XEvent *event_return)''
  ' ''gets the next event and remove it from the queue''</font>
+
  ' ''Gets the next event and remove it from the queue''</font>
  Private Extern XNextEvent(displayP As Pointer, event_return As Pointer)
+
  Private Extern XNextEvent(display As Pointer, event_return As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''XDestroyWindow(Display *display, Window w)''
 
  <FONT color=gray>' ''XDestroyWindow(Display *display, Window w)''
  ' ''destroys the specified window as well as all of its subwindows''</font>
+
  ' ''Destroys the specified window as well as all of its subwindows''</font>
  Private Extern XDestroyWindow(displayP As Pointer, w As Long)
+
  Private Extern XDestroyWindow(display As Pointer, w As Long)
 
   
 
   
 
  <FONT color=gray>' ''XCloseDisplay(Display *display)''
 
  <FONT color=gray>' ''XCloseDisplay(Display *display)''
 
  ' ''Closes the connection to the X server for the display specified in the Display structure and destroys all windows.''</font>
 
  ' ''Closes the connection to the X server for the display specified in the Display structure and destroys all windows.''</font>
  Private Extern XCloseDisplay(displayP As Pointer)
+
  Private Extern XCloseDisplay(display As Pointer)
 
+
 
<FONT color=gray>' ''void exit(int status)''
 
' ''Termina il programma. I buffer dei file vengono svuotati, i flussi sono chiusi.''</font>
 
Private Extern exitus(status As Integer) In "libc:6" Exec "exit"
 
 
 
   
 
   
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
Riga 56: Riga 69:
 
   
 
   
 
  <FONT color=gray>' ''Apre la connessione con il server display del sistema grafico X:''</font>
 
  <FONT color=gray>' ''Apre la connessione con il server display del sistema grafico X:''</font>
  disp = XOpenDisplay(Null)
+
  disp = XOpenDisplay(0)
  If IsNull(disp) Then Error.Raise("Impossibile aprire il server display !")
+
  If disp == 0 Then Error.Raise("Impossibile aprire il server X !")
 
      
 
      
 
  <FONT color=gray>' ''Crea la finestra secondo i parametri previsti dalla funzione. L'ultimo parametro imposta il colore di fondo della finestra:''</font>
 
  <FONT color=gray>' ''Crea la finestra secondo i parametri previsti dalla funzione. L'ultimo parametro imposta il colore di fondo della finestra:''</font>
  id = XCreateSimpleWindow(disp, XDefaultRootWindow(disp), 0, 0, 300, 200, 0, 0, &FF0000)
+
  id = XCreateSimpleWindow(disp, XDefaultRootWindow(disp), 0, 0, 300, 200, 0, 0, &FF0000)
  Print "ID della finestra creata: "; Hex(id, 6)
+
  Print "ID della finestra creata: "; Hex(id, 6)
 
   
 
   
 
  <FONT color=gray>' ''Dice al server display quali eventi deve vedere:''</font>
 
  <FONT color=gray>' ''Dice al server display quali eventi deve vedere:''</font>
  XSelectInput(disp, id, ButtonPressMask)
+
  XSelectInput(disp, id, ButtonPressMask)
 
    
 
    
 
  <FONT color=gray>' ''Apre la finestra sullo schermo. Si può utilizzare anche la funzione "XMapWindow(display, w)":''</font>
 
  <FONT color=gray>' ''Apre la finestra sullo schermo. Si può utilizzare anche la funzione "XMapWindow(display, w)":''</font>
  XMapRaised(disp, id)
+
  XMapRaised(disp, id)
 
    
 
    
  <FONT color=gray>' ''Alloca un'area di memoria pari (208 byte) o superiore alla Struttura esterna.
+
  <FONT color=gray>' ''Alloca un'area di memoria pari alla Struttura esterna (192 byte).''
  ' ''Poiché sarà utilizzato soltanto il valore del primo valore di detta Struttura esterna,
+
  ' ''Poiché sarà utilizzato soltanto il valore del primo valore di detta Struttura esterna, si utilizzerà quest'area di memoria riservata al posto della Struttura predetta:''</font>
' ''si utilizzerà quest'area di memoria riservata al posto della Struttura predetta:''</font>
+
  ev = Alloc(SizeOf(gb.Byte), 192)
  ev = Alloc(256)
+
 
 +
<FONT color=gray>' ''Inizia il ciclo, restando in attesa di un evento stabilito con la precedente funzione XSelectInput():''</font>
 +
  Repeat
 +
    XNextEvent(disp, ev)
 +
<FONT color=gray>' ''Se si pone il puntatore del mouse all'interno della finestra e viene premuto un qualsiasi tasto del mouse, chiude la finestra:''</font>
 +
  Until Int@(ev) == ButtonPress
 +
 
 +
<FONT color=gray>' ''Chiude la finestra e libera la memoria:''</font>
 +
  Free(ev)
 +
  XDestroyWindow(disp, id)
 +
  XCloseDisplay(disp)
 +
   
 +
'''End'''
 +
 
 +
 
 +
In quest'altro esempio, molto simile al precedente, la chiusura della finestra avverrà solo se si premerà con il mouse sulla consueta X posta nell'angolo in alto a destra della finestra medesima:
 +
Library "libX11:6.4.0"
 +
 +
Public Struct XButtonEvent
 +
  type As Integer
 +
  serial As Long
 +
  send_event As Boolean
 +
  display As Pointer
 +
  window_ As Long
 +
  root As Long
 +
  subwindow As Long
 +
  time_ As Long
 +
  x As Integer
 +
  y As Integer
 +
  x_root As Integer
 +
  y_root As Integer
 +
  state As Integer
 +
  keycode As Integer
 +
  same_screen As Boolean
 +
  pad[12] As Long        <FONT Color=gray>' ''Completa la "Struttura" sino a 192 byte''</font>
 +
End Struct
 +
 +
Private Const ButtonPressMask As Long = 4
 +
Private Enum KeyPress = 2, KeyRelease, ButtonPress, ButtonRelease, MotionNotify, EnterNotify,
 +
              LeaveNotify, FocusIn, FocusOut, KeymapNotify, Expose, GraphicsExpose, NoExpose
 +
 +
<FONT color=gray>' ''Display *XOpenDisplay(char *display_name)''
 +
' ''Opens a connection to the X server that controls a display.''</font>
 +
Private Extern XOpenDisplay(display As Pointer) As Pointer
 +
 
 +
<FONT color=gray>' ''Window XRootWindow(Display *display, int screen_number)''
 +
' ''Return the root window.''</font>
 +
Private Extern XDefaultRootWindow(display As Pointer) As Integer
 
   
 
   
 +
<FONT color=gray>' ''Window XCreateSimpleWindow(Display *display, Window parent, int x, int y, unsigned int width, unsigned int height, unsigned int border_width, unsigned long border, unsigned long background)''
 +
' ''Creates an unmapped InputOutput subwindow for a specified parent window, returns the window ID of the created window.''</font>
 +
Private Extern XCreateSimpleWindow(display As Pointer, parent As Long, x As Integer, y As Integer, width As Integer, height As Integer, border_width As Integer, border As Integer, background As Long) As Integer
 
   
 
   
  <FONT color=gray>' ''Inizia il ciclo, restando in attesa di un evento stabilito con la precedente funzione XSelectInput():''</font>
+
  <FONT color=gray>' ''XSelectInput (Display *display, Window w, long event_mask)''
  While True
+
' ''Requests that the X server report the events associated with the specified Event mask.''</font>
 +
Private Extern XSelectInput(display As Pointer, w As Long, event_mask As Long)
 
   
 
   
    XNextEvent(disp, ev)
+
  <FONT color=gray>' ''XMapRaised (Display *display, Window w)''
  <FONT color=gray>' ''Se viene premuto un qualsiasi tasto del mouse, chiude la finestra:''</font>
+
' ''Raises the specified window to the top of the stack.''</font>
      If Int@(ev) = ButtonPress Then chiude_X(disp, id, ev)
+
Private Extern XMapRaised(display As Pointer, w As Long)
 
   
 
   
  Wend
+
<FONT color=gray>' ''Atom XInternAtom(Display *display, char *atom_name, Bool only_if_exists)''
 +
' ''Returns the atom identifier associated with the specified atom_name string.''</font>
 +
Private Extern XInternAtom(display As Pointer, atom_name As String, only_if_exists As Boolean) As Long
 
   
 
   
  '''End'''
+
  <FONT color=gray>' ''Status XSetWMProtocols(Display *display, Window w, Atom *protocols, int count)''
 +
' ''Replaces the WM_PROTOCOLS property on the specified window with the list of atoms specified by the protocols argument.''</font>
 +
Private Extern XSetWMProtocols(display As Pointer, w As Long, protocols As Pointer, count As Integer) As Integer
 
   
 
   
 +
<FONT color=gray>' ''XNextEvent (Display *display, XEvent *event_return)''
 +
' ''Gets the next event and remove it from the queue''</font>
 +
Private Extern XNextEvent(display As Pointer, event_return As XButtonEvent)
 
   
 
   
  '''Private''' Procedure chiude_X(disp_ch As Pointer, id As Integer, pev As Pointer)
+
  <FONT color=gray>' ''XDestroyWindow(Display *display, Window w)''
 +
' ''Destroys the specified window as well as all of its subwindows''</font>
 +
Private Extern XDestroyWindow(display As Pointer, w As Long)
 
   
 
   
  Free(pev)
+
<FONT color=gray>' ''XCloseDisplay(Display *display)''
 +
' ''Closes the connection to the X server for the display specified in the Display structure and destroys all windows.''</font>
 +
Private Extern XCloseDisplay(display As Pointer)
 +
 
 
   
 
   
  <FONT color=gray>' ''Chiude la finestra:''</font>
+
  '''Public''' Sub Main()
  XDestroyWindow(disp_ch, id)
 
 
   
 
   
  XCloseDisplay(disp_ch)
+
  Dim disp As Pointer
 +
  Dim ev As New XButtonEvent
 +
  Dim id, atom As Long
 
   
 
   
  exitus(0)
+
<FONT color=gray>' ''Apre la connessione con il server display del sistema grafico X:''</font>
 +
  disp = XOpenDisplay(0)
 +
  If disp == 0 Then Error.Raise("Impossibile aprire il server X !")
 +
   
 +
<FONT color=gray>' ''Crea la finestra secondo i parametri previsti dalla funzione. L'ultimo parametro imposta il colore di fondo della finestra:''</font>
 +
  id = XCreateSimpleWindow(disp, XDefaultRootWindow(disp), 0, 0, 300, 200, 0, 0, &FF0000)
 +
  Print "ID della finestra creata: "; Hex(id, 6)
 
   
 
   
 +
<FONT color=gray>' ''Dice al server display quali eventi deve vedere:''</font>
 +
  XSelectInput(disp, id, ButtonPressMask)
 +
 
 +
<FONT color=gray>' ''Apre la finestra sullo schermo. Si può utilizzare anche la funzione "XMapWindow(display, w)":''</font>
 +
  XMapRaised(disp, id)
 +
 
 +
<FONT color=gray>' ''Ottiene il numero identificativo dell'evento della chiusura della finestra, quando si clicca sulla X in alto a destra:''</font>
 +
  atom = XInternAtom(disp, "WM_DELETE_WINDOW", False)
 +
 
 +
  XSetWMProtocols(disp, id, VarPtr(atom), 1)
 +
   
 +
<FONT color=gray>' ''Inizia il ciclo, restando in attesa di un evento stabilito con la precedente funzione XSelectInput():''</font>
 +
  Repeat
 +
    XNextEvent(disp, ev)
 +
<FONT color=gray>' ''Si esce dal ciclo, solo se si clicca con il mouse sulla X in alto a destra della finestra:''</font>
 +
  Until ev.time_ == atom
 +
   
 +
<FONT color=gray>' ''Chiude la finestra e libera la memoria:''</font>
 +
  XDestroyWindow(disp, id)
 +
  XCloseDisplay(disp)
 +
 
 
  '''End'''
 
  '''End'''
 
  
  
 
===Creare una finestra colorata contenente disegni geometrici e caratteri alfabetici, con impostazione del puntatore del mouse===
 
===Creare una finestra colorata contenente disegni geometrici e caratteri alfabetici, con impostazione del puntatore del mouse===
In quest'altro esempio, più complesso, verrà creata una finestra di colore rosso, nella quale saranno disegnate figure geometriche e caratteri testuali. Sarà, inoltre, possibile intercettare alcuni eventi provenienti dalla tastiera e dal mouse. Verrà anche impostato un diverso aspetto del puntatore del mouse, impostato fra uno dei disegni previsti nel file header "''x11/cursorfont.h''":
+
In quest'altro esempio, più complesso, verrà creata una finestra di colore rosso, nella quale saranno disegnate figure geometriche e caratteri testuali. Sarà, inoltre, possibile intercettare alcuni eventi provenienti dalla tastiera e dal mouse. Verrà anche impostato un diverso aspetto del puntatore del mouse, impostato fra uno dei disegni previsti nel file header "''x11/cursorfont.h''".
  Public Struct XEventStruct
+
<BR>Per chiudere la finestra, bisognerà premere il tasto "q" della tastiera.
 +
Private disp As Pointer
 +
Private wId As Long
 +
Private gc As Pointer
 +
 +
 +
Library "libX11:6.4.0"
 +
 +
  Public Struct XKeyEvent
 
   type As Integer
 
   type As Integer
 
   serial As Long
 
   serial As Long
 
   send_event As Boolean
 
   send_event As Boolean
 
   display As Pointer
 
   display As Pointer
   windowL As Long
+
   window_ As Long
 
   root As Long
 
   root As Long
 
   subwindow As Long
 
   subwindow As Long
   timeL As Long
+
   time_ As Long
 
   x As Integer
 
   x As Integer
 
   y As Integer
 
   y As Integer
Riga 120: Riga 231:
 
   keycode As Integer
 
   keycode As Integer
 
   same_screen As Boolean
 
   same_screen As Boolean
 +
  pad[12] As Long        <FONT Color=gray>' ''Completa la "Struttura" sino a 192 byte''</font>
 
  End Struct
 
  End Struct
 
   
 
   
  Public Struct XFontStruct
+
  Public Struct XFont
 
   ext_data As Long
 
   ext_data As Long
 
   fid As Pointer
 
   fid As Pointer
Riga 141: Riga 253:
 
  End Struct
 
  End Struct
 
   
 
   
+
  Private Const ExposureMask As Long = 32768
  Private Const ExposureMask As Integer = 32768
+
  Private Const KeyPressMask As Long = 1
  Private Const KeyPressMask As Byte = 1
+
  Private Const ButtonPressMask As Long = 4
  Private Const ButtonPressMask As Byte = 4
 
 
 
  Private Enum KeyPress = 2, KeyRelease, ButtonPress, ButtonRelease, MotionNotify, EnterNotify, LeaveNotify, FocusIn, FocusOut, KeymapNotify, Expose, GraphicsExpose, NoExpose
 
  Private Enum KeyPress = 2, KeyRelease, ButtonPress, ButtonRelease, MotionNotify, EnterNotify, LeaveNotify, FocusIn, FocusOut, KeymapNotify, Expose, GraphicsExpose, NoExpose
 
  Private Enum FillSolid = 0, FillTiled, FillStippled, FillOpaqueStippled  <FONT color=gray>' ''fillStyle''</font>
 
  Private Enum FillSolid = 0, FillTiled, FillStippled, FillOpaqueStippled  <FONT color=gray>' ''fillStyle''</font>
 
Private disp As Pointer
 
Private wId As Long
 
Private gc As Pointer
 
 
 
Library "libX11:6.3.0"
 
 
   
 
   
 
  <FONT color=gray>' ''Display *XOpenDisplay(char *display_name)''
 
  <FONT color=gray>' ''Display *XOpenDisplay(char *display_name)''
 
  ' ''Opens a connection to the X server that controls a display.''</font>
 
  ' ''Opens a connection to the X server that controls a display.''</font>
  Private Extern XOpenDisplay(display$ As String) As Pointer
+
  Private Extern XOpenDisplay(display As Pointer) As Pointer
 
   
 
   
 
  <FONT color=gray>' ''XCloseDisplay(Display *display)''
 
  <FONT color=gray>' ''XCloseDisplay(Display *display)''
 
  ' ''Closes the connection to the X server for the display specified in the Display structure and destroys all windows.''</font>
 
  ' ''Closes the connection to the X server for the display specified in the Display structure and destroys all windows.''</font>
  Private Extern XCloseDisplay(displayP As Pointer)
+
  Private Extern XCloseDisplay(display As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''int XDefaultScreen (Display *display)''
 
  <FONT color=gray>' ''int XDefaultScreen (Display *display)''
 
  ' ''returns the default screen number referenced by the XOpenDisplay function.''</font>
 
  ' ''returns the default screen number referenced by the XOpenDisplay function.''</font>
  Private Extern XDefaultScreen(displayP As Pointer) As Integer
+
  Private Extern XDefaultScreen(display As Pointer) As Integer
 
   
 
   
 
  <FONT color=gray>' ''unsigned long XWhitePixel (Display *display, int screen_number)''
 
  <FONT color=gray>' ''unsigned long XWhitePixel (Display *display, int screen_number)''
 
  ' ''returns the white pixel value for the specified screen.''</font>
 
  ' ''returns the white pixel value for the specified screen.''</font>
  Private Extern XWhitePixel(displayP As Pointer, screen_number As Integer) As Long
+
  Private Extern XWhitePixel(display As Pointer, screen_number As Integer) As Long
 
   
 
   
 
  <FONT color=gray>' ''unsigned long XBlackPixel (Display *display, int screen_number)''
 
  <FONT color=gray>' ''unsigned long XBlackPixel (Display *display, int screen_number)''
 
  ' ''returns the black pixel value for the specified screen.''</font>
 
  ' ''returns the black pixel value for the specified screen.''</font>
  Private Extern XBlackPixel(displayP As Pointer, screen_number As Integer) As Long
+
  Private Extern XBlackPixel(display As Pointer, screen_number As Integer) As Long
 
   
 
   
 
  <FONT color=gray>' ''Window XDefaultRootWindow(Display *display)''
 
  <FONT color=gray>' ''Window XDefaultRootWindow(Display *display)''
 
  ' ''Return the root window for the default screen.''</font>
 
  ' ''Return the root window for the default screen.''</font>
  Private Extern XDefaultRootWindow(displayP As Pointer) As Integer
+
  Private Extern XDefaultRootWindow(display As Pointer) As Integer
 
   
 
   
 
  <FONT color=gray>' ''Window XCreateSimpleWindow(Display *display,  Window parent, int x, int y, unsigned int width, unsigned int height, unsigned int border_width, unsigned long border, unsigned long background)''
 
  <FONT color=gray>' ''Window XCreateSimpleWindow(Display *display,  Window parent, int x, int y, unsigned int width, unsigned int height, unsigned int border_width, unsigned long border, unsigned long background)''
 
  ' ''creates an unmapped InputOutput subwindow for a specified parent window, returns the window ID of the created window.''</font>
 
  ' ''creates an unmapped InputOutput subwindow for a specified parent window, returns the window ID of the created window.''</font>
  Private Extern XCreateSimpleWindow(displayP As Pointer, parent As Long, x As Integer, y As Integer, width As Integer, height As Integer, border_width As Integer, border As Integer, background As Long) As Integer  
+
  Private Extern XCreateSimpleWindow(display As Pointer, parent As Long, x As Integer, y As Integer, width As Integer, height As Integer, border_width As Integer, border As Integer, background As Long) As Integer  
 
   
 
   
 
  <FONT color=gray>' ''XSetStandardProperties (Display *display, Window w, char *window_name, char *icon_name, Pixmap icon_pixmap, char **argv, int argc, XSizeHints *hints)''
 
  <FONT color=gray>' ''XSetStandardProperties (Display *display, Window w, char *window_name, char *icon_name, Pixmap icon_pixmap, char **argv, int argc, XSizeHints *hints)''
 
  ' ''specifies a minimum set of properties describing the simplest application.''</font>
 
  ' ''specifies a minimum set of properties describing the simplest application.''</font>
  Private Extern XSetStandardProperties(displayP As Pointer, w As Long, window_name As String, icon_name As String, icon_pixmap As Integer, argv As String, argc As Integer, hints As Pointer)
+
  Private Extern XSetStandardProperties(display As Pointer, w As Long, window_name As String, icon_name As String, icon_pixmap As Integer, argv As String, argc As Integer, hints As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''XSelectInput (Display *display, Window w, long event_mask)''
 
  <FONT color=gray>' ''XSelectInput (Display *display, Window w, long event_mask)''
 
  ' ''requests that the X server report the events associated with the specified Event mask.''</font>
 
  ' ''requests that the X server report the events associated with the specified Event mask.''</font>
  Private Extern XSelectInput(displayP As Pointer, w As Integer, event_mask As Long)
+
  Private Extern XSelectInput(display As Pointer, w As Integer, event_mask As Long)
 
   
 
   
 
  <FONT color=gray>' ''GC XCreateGC(Display *display, Drawable d, unsigned long valuemask, XGCValues *values)''
 
  <FONT color=gray>' ''GC XCreateGC(Display *display, Drawable d, unsigned long valuemask, XGCValues *values)''
 
  ' ''creates a graphics context and returns a GC.''</font>
 
  ' ''creates a graphics context and returns a GC.''</font>
  Private Extern XCreateGC(displayP As Pointer, w As Integer, valuemask As Long, values As Pointer) As Pointer
+
  Private Extern XCreateGC(display As Pointer, w As Integer, valuemask As Long, values As Pointer) As Pointer
 
   
 
   
 
  <FONT color=gray>' ''XSetForeground (Display *display, GC gc, unsigned long foreground)''
 
  <FONT color=gray>' ''XSetForeground (Display *display, GC gc, unsigned long foreground)''
 
  ' ''sets the foreground.''</font>
 
  ' ''sets the foreground.''</font>
  Private Extern XSetForeground(displayP As Pointer, gc As Pointer, foreground As Long)
+
  Private Extern XSetForeground(display As Pointer, gc As Pointer, foreground As Long)
 
   
 
   
 
  <FONT color=gray>' ''char ** XListFonts(Display *display, char *pattern, int maxnames, int *actual_count_return)''
 
  <FONT color=gray>' ''char ** XListFonts(Display *display, char *pattern, int maxnames, int *actual_count_return)''
 
  ' ''returns an array of available font names.''</font>
 
  ' ''returns an array of available font names.''</font>
  Private Extern XListFonts(displayP As Pointer, pattern As String, maxnames As Integer, actual_count_return As Pointer) As Pointer
+
  Private Extern XListFonts(display As Pointer, pattern As String, maxnames As Integer, actual_count_return As Pointer) As Pointer
 
   
 
   
 
  <FONT color=gray>' ''XFontStruct * XLoadQueryFont(Display *display, char *name)''
 
  <FONT color=gray>' ''XFontStruct * XLoadQueryFont(Display *display, char *name)''
 
  ' ''opens(loads)the specified font.''</font>
 
  ' ''opens(loads)the specified font.''</font>
  Private Extern XLoadQueryFont(displayP As Pointer, nameFont As String) As XFontStruct
+
  Private Extern XLoadQueryFont(display As Pointer, nameFont As String) As XFont
 
   
 
   
 
  <FONT color=gray>' ''XSetFont(Display *display, GC gc, Font font)''
 
  <FONT color=gray>' ''XSetFont(Display *display, GC gc, Font font)''
 
  ' ''Assigns a Font to a Graphics Context.''</font>
 
  ' ''Assigns a Font to a Graphics Context.''</font>
  Private Extern XSetFont(displayP As Pointer, gcP As Pointer, fontP As Pointer)
+
  Private Extern XSetFont(display As Pointer, gcP As Pointer, fontP As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''XSetFillStyle(Display *display, GC gc, int fill_style)''
 
  <FONT color=gray>' ''XSetFillStyle(Display *display, GC gc, int fill_style)''
 
  ' ''changes the fill style of GC.''</font>
 
  ' ''changes the fill style of GC.''</font>
  Private Extern XSetFillStyle(displayP As Pointer, gc As Pointer, fill_style As Integer)
+
  Private Extern XSetFillStyle(display As Pointer, gc As Pointer, fill_style As Integer)
 
   
 
   
 
  <FONT color=gray>' ''XClearWindow(Display *display, Window w)''
 
  <FONT color=gray>' ''XClearWindow(Display *display, Window w)''
 
  ' ''clears the entire area in the specified window.''</font>
 
  ' ''clears the entire area in the specified window.''</font>
  Private Extern XClearWindow(displayP As Pointer, w As Long)
+
  Private Extern XClearWindow(display As Pointer, w As Long)
 
   
 
   
 
  <FONT color=gray>' ''XMapRaised (Display *display, Window w)''
 
  <FONT color=gray>' ''XMapRaised (Display *display, Window w)''
 
  ' ''raises the specified window to the top of the stack.''</font>
 
  ' ''raises the specified window to the top of the stack.''</font>
  Private Extern XMapRaised(displayP As Pointer, w As Long)
+
  Private Extern XMapRaised(display As Pointer, w As Long)
 
   
 
   
 
  <FONT color=gray>' ''Cursor XCreateFontCursor(Display *display, unsigned int shape)''
 
  <FONT color=gray>' ''Cursor XCreateFontCursor(Display *display, unsigned int shape)''
 
  ' ''Provides a set of standard cursor shapes in a special font named cursor.''</font>
 
  ' ''Provides a set of standard cursor shapes in a special font named cursor.''</font>
  Private Extern XCreateFontCursor(displayP As Pointer, shape As Integer) As Integer
+
  Private Extern XCreateFontCursor(display As Pointer, shape As Integer) As Integer
 
   
 
   
 
  <FONT color=gray>' ''XDefineCursor(Display *display, Window w, Cursor cursor)''
 
  <FONT color=gray>' ''XDefineCursor(Display *display, Window w, Cursor cursor)''
 
  ' ''If a cursor is set, it will be used when the pointer is in the window.''</font>
 
  ' ''If a cursor is set, it will be used when the pointer is in the window.''</font>
  Private Extern XDefineCursor(displayP As Pointer, w As Long, cursorI As Integer) As Integer
+
  Private Extern XDefineCursor(display As Pointer, w As Long, cursorI As Integer) As Integer
 
   
 
   
 
  <FONT color=gray>' ''XNextEvent (Display *display, XEvent *event_return)''
 
  <FONT color=gray>' ''XNextEvent (Display *display, XEvent *event_return)''
 
  ' ''gets the next event and remove it from the queue''</font>
 
  ' ''gets the next event and remove it from the queue''</font>
  Private Extern XNextEvent(displayP As Pointer, event_return As XEventStruct)
+
  Private Extern XNextEvent(display As Pointer, event_return As XKeyEvent)
 
   
 
   
 
  <FONT color=gray>' ''int XLookupString(XKeyEvent *event_struct, char *buffer_return, int bytes_buffer, KeySym *keysym_return, XComposeStatus *status_in_out)''
 
  <FONT color=gray>' ''int XLookupString(XKeyEvent *event_struct, char *buffer_return, int bytes_buffer, KeySym *keysym_return, XComposeStatus *status_in_out)''
 
  ' ''translates a key event to a KeySym and a string.''</font>
 
  ' ''translates a key event to a KeySym and a string.''</font>
  Private Extern XLookupString(event_struct As XEventStruct, buffer_return As Byte[], bytes_buffer As Integer, keysym_return As Pointer, status_in_out As Pointer) As Integer
+
  Private Extern XLookupString(event_struct As XKeyEvent, buffer_return As Byte[], bytes_buffer As Integer, keysym_return As Pointer, status_in_out As Pointer) As Integer
 
   
 
   
 
  <FONT color=gray>' ''XDrawPoint(Display *display, Drawable d, GC gc, int x, int y)''
 
  <FONT color=gray>' ''XDrawPoint(Display *display, Drawable d, GC gc, int x, int y)''
 
  ' ''draws a single point into the specified drawable''</font>
 
  ' ''draws a single point into the specified drawable''</font>
  Private Extern XDrawPoint(displayP As Pointer, w As Long, gcP As Pointer, x1 As Integer, y1 As Integer)
+
  Private Extern XDrawPoint(display As Pointer, w As Long, gcP As Pointer, x1 As Integer, y1 As Integer)
 
   
 
   
 
  <FONT color=gray>' ''XDrawLine(Display *display, Drawable d, GC gc, int x1, int y1, int x2, int y2)''
 
  <FONT color=gray>' ''XDrawLine(Display *display, Drawable d, GC gc, int x1, int y1, int x2, int y2)''
 
  ' ''draws a line between the specified set of points (x1, y1) and (x2, y2).''</font>
 
  ' ''draws a line between the specified set of points (x1, y1) and (x2, y2).''</font>
  Private Extern XDrawLine(displayP As Pointer, w As Long, gcP As Pointer, x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer)
+
  Private Extern XDrawLine(display As Pointer, w As Long, gc As Pointer, x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer)
 
   
 
   
 
  <FONT color=gray>' ''XDrawRectangle(Display *display, Drawable d, GC gc, int x, int y, unsigned int width, unsigned int height)''
 
  <FONT color=gray>' ''XDrawRectangle(Display *display, Drawable d, GC gc, int x, int y, unsigned int width, unsigned int height)''
 
  ' ''draws the outlines of the specified rectangle.''</font>
 
  ' ''draws the outlines of the specified rectangle.''</font>
  Private Extern XDrawRectangle(displayP As Pointer, w As Integer, gcP As Pointer, xI As Integer, yI As Integer, widthI As Integer, heightI As Integer)
+
  Private Extern XDrawRectangle(display As Pointer, w As Integer, gc As Pointer, xI As Integer, yI As Integer, widthI As Integer, heightI As Integer)
 
   
 
   
 
  <FONT color=gray>' ''XFillRectangle(Display *display, Drawable d, GC gc, int x, int y, unsigned int width, unsigned int height)''
 
  <FONT color=gray>' ''XFillRectangle(Display *display, Drawable d, GC gc, int x, int y, unsigned int width, unsigned int height)''
 
  ' ''fills the specified rectangle''</font>
 
  ' ''fills the specified rectangle''</font>
  Private Extern XFillRectangle(displayP As Pointer, w As Long, gcP As Pointer, xI As Integer, yI As Integer, widthI As Integer, heightI As Integer)
+
  Private Extern XFillRectangle(display As Pointer, w As Long, gc As Pointer, xI As Integer, yI As Integer, widthI As Integer, heightI As Integer)
 
   
 
   
 
  <FONT color=gray>' ''XDrawArc(Display *display, Drawable d, GC gc, int x, int y, unsigned int width, unsigned int height, int angle1, int angle2)''
 
  <FONT color=gray>' ''XDrawArc(Display *display, Drawable d, GC gc, int x, int y, unsigned int width, unsigned int height, int angle1, int angle2)''
 
  ' ''draws an arc.''</font>
 
  ' ''draws an arc.''</font>
  Private Extern XDrawArc(displayP As Pointer, w As Long, gcP As Pointer, xI As Integer, yI As Integer, widthI As Integer, heightI As Integer, angle1 As Integer, angle2 As Integer)
+
  Private Extern XDrawArc(display As Pointer, w As Long, gc As Pointer, xI As Integer, yI As Integer, widthI As Integer, heightI As Integer, angle1 As Integer, angle2 As Integer)
 
   
 
   
 
  <FONT color=gray>' ''XFillArc(Display *display, Drawable d, GC gc, int x, int y, unsigned int width, unsigned int height, int angle1, int angle2)''
 
  <FONT color=gray>' ''XFillArc(Display *display, Drawable d, GC gc, int x, int y, unsigned int width, unsigned int height, int angle1, int angle2)''
 
  ' ''fills the specified arc.''</font>
 
  ' ''fills the specified arc.''</font>
  Private Extern XFillArc(displayP As Pointer, w As Long, gcP As Pointer, xI As Integer, yI As Integer, widthI As Integer, heightI As Integer, angle1 As Integer, angle2 As Integer)
+
  Private Extern XFillArc(display As Pointer, w As Long, gc As Pointer, xI As Integer, yI As Integer, widthI As Integer, heightI As Integer, angle1 As Integer, angle2 As Integer)
 
   
 
   
  <FONT color=gray>' ''XDrawString(Display *display, Drawable d, GC gc, int x, int y, char *string, intlength)''
+
  <FONT color=gray>' ''XDrawString(Display *display, Drawable d, GC gc, int x, int y, char *string, int length)''
 
  ' ''draws a text.''</font>
 
  ' ''draws a text.''</font>
  Private Extern XDrawString(displayP As Pointer, w As Long, gcP As Pointer, x As Integer, y As Integer, test$ As String, length As Integer)
+
  Private Extern XDrawString(display As Pointer, w As Long, gc As Pointer, x As Integer, y As Integer, string_ As Pointer, length As Integer)
 
   
 
   
 
  <FONT color=gray>' ''XFreeGC(Display *display, GC gc)''
 
  <FONT color=gray>' ''XFreeGC(Display *display, GC gc)''
 
  ' ''destroys the specified GC as well as all the associated storage.''</font>
 
  ' ''destroys the specified GC as well as all the associated storage.''</font>
  Private Extern XFreeGC(displayP As Pointer, gcP As Pointer)
+
  Private Extern XFreeGC(display As Pointer, gc As Pointer)
 
   
 
   
 
  <FONT color=gray>' ''XDestroyWindow(Display *display, Window w)''
 
  <FONT color=gray>' ''XDestroyWindow(Display *display, Window w)''
 
  ' ''destroys the specified window as well as all of its subwindows''</font>
 
  ' ''destroys the specified window as well as all of its subwindows''</font>
  Private Extern XDestroyWindow(displayP As Pointer, w As Long)
+
  Private Extern XDestroyWindow(display As Pointer, w As Long)
+
 
 
<FONT color=gray>' ''void exit(int status)''
 
' ''Termina il programma. I buffer dei file vengono svuotati, i flussi sono chiusi.''</font>
 
Private Extern exitus(status As Integer) In "libc:6" Exec "exit"
 
 
 
   
 
   
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
Riga 287: Riga 385:
 
   Dim pxW, pxB As Long
 
   Dim pxW, pxB As Long
 
   Dim testo As New Byte[255]
 
   Dim testo As New Byte[255]
   Dim ev As New XEventStruct
+
   Dim ev As New XKeyEvent
   Dim xfon As New XFontStruct
+
   Dim xfon As New XFont
 
   Dim scriptum As String = "testo qualsiasi"
 
   Dim scriptum As String = "testo qualsiasi"
 
 
   
 
   
 
  <FONT color=gray>' ''Apre la connessione con il server display del sistema grafico X:''</font>
 
  <FONT color=gray>' ''Apre la connessione con il server display del sistema grafico X:''</font>
    disp = XOpenDisplay(Null)
+
  disp = XOpenDisplay(0)
    If IsNull(disp) Then Error.Raise("Impossibile aprire il server display !")
+
  If disp == 0 Then Error.Raise("Impossibile aprire il server X !")
+
 
    screen = XDefaultScreen(disp)
+
  screen = XDefaultScreen(disp)
+
  pxW = XWhitePixel(disp, screen)
    pxW = XWhitePixel(disp, screen)
+
  pxB = XBlackPixel(disp, screen)
   
 
    pxB = XBlackPixel(disp, screen)
 
 
      
 
      
 
  <FONT color=gray>' ''Crea la finestra secondo i parametri previsti dalla funzione.''
 
  <FONT color=gray>' ''Crea la finestra secondo i parametri previsti dalla funzione.''
 
  ' ''L'ultimo parametro imposta il colore di fondo della finestra:''</font>
 
  ' ''L'ultimo parametro imposta il colore di fondo della finestra:''</font>
    wId = XCreateSimpleWindow(disp, XDefaultRootWindow(disp), 0, 0, 300, 200, 5, pxW, &FF0000)
+
  wId = XCreateSimpleWindow(disp, XDefaultRootWindow(disp), 0, 0, 300, 200, 5, pxW, &FF0000)
    Print "ID della finestra creata: "; Hex(wId, 6)
+
  Print "ID della finestra creata: "; Hex(wId, 6)
 
      
 
      
    XSetStandardProperties(disp, wId, "Prova creazione finestra", "Nome icona finestra", 0, Null, 0, Null)
+
  XSetStandardProperties(disp, wId, "Prova creazione finestra", "Nome icona finestra", 0, Null, 0, Null)
 
      
 
      
 
  <FONT color=gray>' ''Dice al server display quali eventi deve vedere:''</font>
 
  <FONT color=gray>' ''Dice al server display quali eventi deve vedere:''</font>
    XSelectInput(disp, wId, ExposureMask Or KeyPressMask Or ButtonPressMask)
+
  XSelectInput(disp, wId, ExposureMask Or KeyPressMask Or ButtonPressMask)
 
      
 
      
 
  <FONT color=gray>' ''Crea un nuovo contesto grafico:''</font>
 
  <FONT color=gray>' ''Crea un nuovo contesto grafico:''</font>
    gc = XCreateGC(disp, wId, 0, Null)
+
  gc = XCreateGC(disp, wId, 0, 0)
    If IsNull(gc) Then Error.Raise("Impossibile creare un nuovo contesto grafico !")
+
  If gc == 0 Then Error.Raise("Impossibile creare un nuovo contesto grafico !")
 
   
 
   
 
  <FONT color=gray>' ''Imposta il colore degli elementi grafici all'interno della finestra:''</font>
 
  <FONT color=gray>' ''Imposta il colore degli elementi grafici all'interno della finestra:''</font>
    XSetForeground(disp, gc, &00FF00)
+
  XSetForeground(disp, gc, &00FF00)
 
   
 
   
    elencoFont()
+
  elencoFont()
 
   
 
   
 
  <FONT color=gray>' ''Imposta il font per i caratteri del testo grafico:''</font>
 
  <FONT color=gray>' ''Imposta il font per i caratteri del testo grafico:''</font>
    xfon = XLoadQueryFont(disp, "*bitstream*")
+
  xfon = XLoadQueryFont(disp, "*bitstream*")
    If IsNull(xFon) Then Error.Raise("Impossibile caricare il font !")
+
  If IsNull(xFon) Then Error.Raise("Impossibile caricare il font !")
 
    
 
    
    XSetFont(disp, gc, xfon.fid)
+
  XSetFont(disp, gc, xfon.fid)
+
  XSetFillStyle(disp, gc, FillSolid)
    XSetFillStyle(disp, gc, FillSolid)
 
 
      
 
      
    redraw()
+
  redraw()
 
      
 
      
 
  <FONT color=gray>' ''Apre la finestra sullo schermo. Si può utilizzare anche la funzione "XMapWindow(display, w)":''</font>
 
  <FONT color=gray>' ''Apre la finestra sullo schermo. Si può utilizzare anche la funzione "XMapWindow(display, w)":''</font>
    XMapRaised(disp, wId)
+
  XMapRaised(disp, wId)
 
   
 
   
  <FONT color=gray>' ''Entrando il cursore del mouse nella finestra, esso cambia aspetto, impostando quello''
+
  <FONT color=gray>' ''Entrando il cursore del mouse nella finestra, esso cambia aspetto, impostando quello che nel file header ".../X11/cursorfont.h" ha il valore 10 tra quelli visibili nella pagina "http://tronche.com/gui/x/xlib/appendix/b/":''</font>
' ''che nel file header "x11/cursorfont.h" ha il valore 10 tra quelli visibili nella pagina "http://tronche.com/gui/x/xlib/appendix/b/":''</font>
+
  cursor = XCreateFontCursor(disp, 10)
    cursor = XCreateFontCursor(disp, 10)
 
 
   
 
   
    XDefineCursor(disp, wId, cursor)
+
  XDefineCursor(disp, wId, cursor)
+
 
+
  While True
    While True
+
    XNextEvent(disp, ev)
+
    Select Case ev.type
      XNextEvent(disp, ev)
+
      Case Expose
 
      Select Case ev.type
 
          Case Expose
 
 
  <FONT color=gray>' ''La finestra mostrata deve essere ripulita:''</font>
 
  <FONT color=gray>' ''La finestra mostrata deve essere ripulita:''</font>
            redraw()
+
        redraw()
 
  <FONT color=gray>' ''Disegna un punto all'interno del quadrato:''</font>
 
  <FONT color=gray>' ''Disegna un punto all'interno del quadrato:''</font>
            XDrawPoint(disp, wId, gc, 70, 120)
+
        XDrawPoint(disp, wId, gc, 70, 120)
 
  <FONT color=gray>' ''Disegna una linea:''</font>
 
  <FONT color=gray>' ''Disegna una linea:''</font>
            XDrawLine(disp, wId, gc, 10, 10, 40, 60)
+
        XDrawLine(disp, wId, gc, 10, 10, 40, 60)
 
  <FONT color=gray>' ''In questo caso disegna un quadrato:''</font>
 
  <FONT color=gray>' ''In questo caso disegna un quadrato:''</font>
            XDrawRectangle(disp, wId, gc, 50, 100, 40, 40)
+
        XDrawRectangle(disp, wId, gc, 50, 100, 40, 40)
 
  <FONT color=gray>' ''Disegna un rettangolo colorato di bianco:''</font>
 
  <FONT color=gray>' ''Disegna un rettangolo colorato di bianco:''</font>
            XFillRectangle(disp, wId, gc, 200, 30, 60, 40)
+
        XFillRectangle(disp, wId, gc, 200, 30, 60, 40)
 
  <FONT color=gray>' ''Disegna un arco:''</font>
 
  <FONT color=gray>' ''Disegna un arco:''</font>
            XDrawArc(disp, wId, gc, 100, 150, 60, 40, 0, 360 * 20)
+
        XDrawArc(disp, wId, gc, 100, 150, 60, 40, 0, 360 * 20)
 
  <FONT color=gray>' ''Disegna un arco riempito di colore bianco:''</font>
 
  <FONT color=gray>' ''Disegna un arco riempito di colore bianco:''</font>
            XFillArc(disp, wId, gc, 40, 20, 60, 40, 0, 360 * 20)
+
        XFillArc(disp, wId, gc, 40, 20, 60, 40, 0, 360 * 20)
 
  <FONT color=gray>' ''Disegna un cerchio:''</font>
 
  <FONT color=gray>' ''Disegna un cerchio:''</font>
            XDrawArc(disp, wId, gc, 200, 100, 50, 50, 0, 360 * 64)
+
        XDrawArc(disp, wId, gc, 200, 100, 50, 50, 0, 360 * 64)
 
  <FONT color=gray>' ''Disegna un cerchio riempito di colore bianco:''</font>
 
  <FONT color=gray>' ''Disegna un cerchio riempito di colore bianco:''</font>
            XFillArc(disp, wId, gc, 110, 40, 50, 50, 0, 360 * 64)
+
        XFillArc(disp, wId, gc, 110, 40, 50, 50, 0, 360 * 64)
 
  <FONT color=gray>' ''Disegna una stringa di caratteri:''</font>
 
  <FONT color=gray>' ''Disegna una stringa di caratteri:''</font>
            XDrawString(disp, wId, gc, 20, 170, scriptum, Len(scriptum))
+
        XDrawString(disp, wId, gc, 20, 170, scriptum, Len(scriptum))
+
      Case KeyPress
          Case KeyPress
+
        XLookupString(ev, testo, 255, VarPtr(key), Null)
            XLookupString(ev, testo, 255, VarPtr(key), Null)
+
        If testo[0] == Asc("q") Then
            If testo[0] = Asc("q") Then
+
          chiude_X()
              chiude_X()
+
          Print "E' stato premuto il tasto: "; Chr(testo[0]), key
              Print "E' stato premuto il tasto: "; Chr(testo[0]), key
+
        Else
              Else
 
 
  <FONT color=gray>' ''Disegna il carattere del tasto premuto alle coordinate del puntatore del mouse:''</font>
 
  <FONT color=gray>' ''Disegna il carattere del tasto premuto alle coordinate del puntatore del mouse:''</font>
                XDrawString(disp, wId, gc, ev.x, ev.y, Chr(key), 1)
+
          XDrawString(disp, wId, gc, ev.x, ev.y, Chr(key), 1)
            Endif
+
        Endif
+
      Case ButtonPress    <FONT color=gray>' ''Indica le coordinate ove è stato premuto il tasto del mouse''</font>
          Case ButtonPress    <FONT color=gray>' ''Indica le coordinate ove è stato premuto il tasto del mouse''</font>
+
        With ev
            With ev
+
          Print "E' stato premuto il tasto del mouse alle seguenti coordinate:\n"; .x; " pixel dal margine sinistro della finestra;"
              Print "E' stato premuto il tasto del mouse alle seguenti coordinate:\n"; .x; " pixel dal margine sinistro della finestra;"
+
          Print .y; " pixel dal margine superiore della finestra;"
              Print .y; " pixel dal margine superiore della finestra;"
+
          Print .x_root; " pixel dal margine sinistro dello schermo;"
              Print .x_root; " pixel dal margine sinistro dello schermo;"
+
          Print .y_root; " pixel dal margine superiore dello schermo."  
              Print .y_root; " pixel dal margine superiore dello schermo."  
+
        End With
            End With
+
        redraw()
            redraw()
+
    End Select
        End Select
+
  Wend
 
    Wend
 
 
      
 
      
  '''End'''
+
  '''End'''  
 
 
   
 
   
 
  '''Private''' Procedure elencoFont()
 
  '''Private''' Procedure elencoFont()
 
   
 
   
 
   Dim po As Pointer
 
   Dim po As Pointer
   Dim num_fonts As Integer
+
   Dim num_fonts, i As Integer
  Dim st As Stream
 
  Dim j As Short
 
 
   Dim b As Byte
 
   Dim b As Byte
 
 
 
    
 
    
  po = XListFonts(disp, "*itstr*", 8196, VarPtr(num_fonts))
+
  po = XListFonts(disp, "*itstr*", 8196, VarPtr(num_fonts))
  If IsNull(po) Then Error.Raise("Impossibile caricare i font !")
+
  If po == 0 Then Error.Raise("Impossibile caricare i font !")
 
  po = Pointer@(po)
 
 
<FONT color=gray>' ''Dereferenziamo il "puntatore" mediante i "Memory Stream":''</font>
 
  st = Memory po For Read
 
 
    
 
    
   For j = 0 To 8196
+
  po = Pointer@(po)
    Seek #st, j
+
    
    Read #st, b
+
<FONT color=gray>' ''Dereferenziamo il "puntatore":''</font>
    If b = 0 Then
+
  For i = 0 To 8196
      Print
+
    b = Byte@(po + i)
    Else
+
    If b == 0 Then
      Print Chr(b);
+
      Print
    Endif
+
    Else
  Next
+
      Print Chr(b);
+
    Endif
  st.Close
+
  Next
  Free(po)
+
   
+
  '''End'''  
  '''End'''
 
 
 
    
 
    
 
  '''Private''' Procedure redraw()
 
  '''Private''' Procedure redraw()
Riga 436: Riga 512:
 
  '''Private''' Procedure chiude_X()
 
  '''Private''' Procedure chiude_X()
 
   
 
   
    XFreeGC(disp, gc)
+
  XFreeGC(disp, gc)
 
 
  <FONT color=gray>' ''Chiude la finestra:''</font>
 
  <FONT color=gray>' ''Chiude la finestra:''</font>
    XDestroyWindow(disp, wId)
+
  XDestroyWindow(disp, wId)
+
  XCloseDisplay(disp)
    XCloseDisplay(disp)
+
  <FONT color=gray>' ''"Quit" consente di terminare anche il programma medesimo:''</font>
   
+
  Quit
    exitus(0)
+
 
 
 
  '''End'''
 
  '''End'''
  
Riga 450: Riga 524:
  
 
===Creare una semplice finestra colorata, con il puntatore del mouse avente un disegno impostato liberamente da noi===
 
===Creare una semplice finestra colorata, con il puntatore del mouse avente un disegno impostato liberamente da noi===
 +
In questo esempio quando si entrerà con il puntatore del mouse all'interno della finestra, l'aspetto del puntatore cambierà in quello di una matita, secondo un disegno da noi impostato.
 +
<BR>Per chiudere la finestra, si dovrà cliccare al suo interno.
 +
Library "libX11:6.4.0"
 +
 
  Public Struct XColor
 
  Public Struct XColor
  pixel As Long
+
  pixel As Long
  red As Short
+
  red As Short
  green As Short
+
  green As Short
  blue As Short
+
  blue As Short
  flags As Byte
+
  flags As Byte
  pad As Byte
+
  pad As Byte
 
  End Struct
 
  End Struct
 
   
 
   
  Public Struct XEventStruct
+
  Public Struct XButtonEvent
 
   type As Integer
 
   type As Integer
 
   serial As Long
 
   serial As Long
 
   send_event As Boolean
 
   send_event As Boolean
 
   display As Pointer
 
   display As Pointer
   windowL As Long
+
   window_ As Long
 
   root As Long
 
   root As Long
 
   subwindow As Long
 
   subwindow As Long
   timeL As Long
+
   time_ As Long
 
   x As Integer
 
   x As Integer
 
   y As Integer
 
   y As Integer
Riga 475: Riga 553:
 
   keycode As Integer
 
   keycode As Integer
 
   same_screen As Boolean
 
   same_screen As Boolean
 +
  pad[12] As Long        <FONT Color=gray>' ''Completa la "Struttura" sino a 192 byte''</font>
 
  End Struct
 
  End Struct
 
   
 
   
+
  Private Const ButtonPressMask As Long = 4
Library "libX11:6.3.0"
+
  Private Enum KeyPress = 2, KeyRelease, ButtonPress, ButtonRelease, MotionNotify, EnterNotify,
+
              LeaveNotify, FocusIn, FocusOut, KeymapNotify, Expose, GraphicsExpose, NoExpose
  Private Const ButtonPressMask As Byte = 4
 
  Private Enum KeyPress = 2, KeyRelease, ButtonPress, ButtonRelease, MotionNotify, EnterNotify, LeaveNotify,
 
                            FocusIn, FocusOut, KeymapNotify, Expose, GraphicsExpose, NoExpose
 
 
   
 
   
 
  <FONT color=gray>' ''Display *XOpenDisplay(char *display_name)''
 
  <FONT color=gray>' ''Display *XOpenDisplay(char *display_name)''
 
  ' ''Opens a connection to the X server that controls a display.''</font>
 
  ' ''Opens a connection to the X server that controls a display.''</font>
  Private Extern XOpenDisplay(display$ As String) As Pointer
+
  Private Extern XOpenDisplay(display As Pointer) As Pointer
 
   
 
   
 
  <FONT color=gray>' ''Screen *XDefaultScreenOfDisplay(Display *display)
 
  <FONT color=gray>' ''Screen *XDefaultScreenOfDisplay(Display *display)
 
  ' ''Returns a pointer to the default screen.''</font>
 
  ' ''Returns a pointer to the default screen.''</font>
  Private Extern XDefaultScreenOfDisplay(displayP As Pointer) As Pointer
+
  Private Extern XDefaultScreenOfDisplay(display As Pointer) As Pointer
 
    
 
    
 
  <FONT color=gray>' ''Window XDefaultRootWindow(Display *display)''
 
  <FONT color=gray>' ''Window XDefaultRootWindow(Display *display)''
 
  ' ''Return the root window for the default screen.''</font>
 
  ' ''Return the root window for the default screen.''</font>
  Private Extern XDefaultRootWindow(displayP As Pointer) As Integer
+
  Private Extern XDefaultRootWindow(display As Pointer) As Integer
 
   
 
   
 
  <FONT color=gray>' ''Window XCreateSimpleWindow(Display *display, Window parent, int x, int y, unsigned int width, unsigned int height, unsigned int border_width, unsigned long border, unsigned long background)''
 
  <FONT color=gray>' ''Window XCreateSimpleWindow(Display *display, Window parent, int x, int y, unsigned int width, unsigned int height, unsigned int border_width, unsigned long border, unsigned long background)''
 
  ' ''creates an unmapped InputOutput subwindow for a specified parent window, returns the window ID of the created window.''</font>
 
  ' ''creates an unmapped InputOutput subwindow for a specified parent window, returns the window ID of the created window.''</font>
  Private Extern XCreateSimpleWindow(displayP As Pointer, parent As Long, x As Integer, y As Integer, width As Integer, height As Integer, border_width As Integer, border As Integer, background As Long) As Integer
+
  Private Extern XCreateSimpleWindow(display As Pointer, parent As Long, x As Integer, y As Integer, width As Integer, height As Integer, border_width As Integer, border As Integer, background As Long) As Integer
 
    
 
    
 
  <FONT color=gray>' ''XMapRaised (Display *display, Window w)''
 
  <FONT color=gray>' ''XMapRaised (Display *display, Window w)''
 
  ' ''raises the specified window to the top of the stack.''</font>
 
  ' ''raises the specified window to the top of the stack.''</font>
  Private Extern XMapRaised(displayP As Pointer, w As Long)
+
  Private Extern XMapRaised(display As Pointer, w As Long)
 
   
 
   
 
  <FONT color=gray>' ''Pixmap XCreatePixmap(display *display, Drawable d, unsigned int width, unsigned int height, unsigned int depth)''
 
  <FONT color=gray>' ''Pixmap XCreatePixmap(display *display, Drawable d, unsigned int width, unsigned int height, unsigned int depth)''
 
  ' ''Creates a pixmap of the width, height, and depth you specified and returns a pixmap ID that identifies it.''</font>
 
  ' ''Creates a pixmap of the width, height, and depth you specified and returns a pixmap ID that identifies it.''</font>
  Private Extern XCreatePixmap(displayP As Pointer, w As Long, width As Integer, height As Integer, depth As Integer) As Pointer
+
  Private Extern XCreatePixmap(display As Pointer, w As Long, width As Integer, height As Integer, depth As Integer) As Pointer
 
   
 
   
 
  <FONT color=gray>' ''Status XLookupColor(Display *display, Colormap colormap, char *color_name, XColor *exact_def_return, XColor *screen_def_return)''
 
  <FONT color=gray>' ''Status XLookupColor(Display *display, Colormap colormap, char *color_name, XColor *exact_def_return, XColor *screen_def_return)''
 
  ' ''Looks up the string name of a color with respect to the screen associated with the specified colormap.''</font>
 
  ' ''Looks up the string name of a color with respect to the screen associated with the specified colormap.''</font>
  Private Extern XLookupColor(displayP As Pointer, colormap As Pointer, color_name As String, exact_def_return As XColor, screen_def_return As XColor) As Integer
+
  Private Extern XLookupColor(display As Pointer, colormap As Pointer, color_name As String, exact_def_return As XColor, screen_def_return As XColor) As Integer
 
   
 
   
 
  <FONT color=gray>' ''Colormap XDefaultColormapOfScreen(Screen *screen)''
 
  <FONT color=gray>' ''Colormap XDefaultColormapOfScreen(Screen *screen)''
Riga 518: Riga 594:
 
  <FONT color=gray>' ''Pixmap XCreatePixmapFromBitmapData(display *display, Drawable d, char *data, unsigned int width, unsigned int height, unsigned long fg, unsigned long bg, unsigned int depth)''
 
  <FONT color=gray>' ''Pixmap XCreatePixmapFromBitmapData(display *display, Drawable d, char *data, unsigned int width, unsigned int height, unsigned long fg, unsigned long bg, unsigned int depth)''
 
  ' ''Creates a pixmap of the given depth and then does a bitmap-format XPutImage() of the data into it.''</font>
 
  ' ''Creates a pixmap of the given depth and then does a bitmap-format XPutImage() of the data into it.''</font>
  Private Extern XCreatePixmapFromBitmapData(displayP As Pointer, w As Pointer, data As Byte[], width As Integer, height As Integer, fg As Long, bg As Long, depth As Integer) As Pointer
+
  Private Extern XCreatePixmapFromBitmapData(display As Pointer, w As Pointer, data As Byte[], width As Integer, height As Integer, fg As Long, bg As Long, depth As Integer) As Pointer
 
   
 
   
 
  <FONT color=gray>' ''Cursor XCreatePixmapCursor(Display *display, Pixmap source, Pixmap mask, XColor *foreground_color, XColor *background_color, unsigned int x, unsigned int y)''
 
  <FONT color=gray>' ''Cursor XCreatePixmapCursor(Display *display, Pixmap source, Pixmap mask, XColor *foreground_color, XColor *background_color, unsigned int x, unsigned int y)''
 
  ' ''Creates a cursor and returns the cursor ID associated with it.''</font>
 
  ' ''Creates a cursor and returns the cursor ID associated with it.''</font>
  Private Extern XCreatePixmapCursor(displayP As Pointer, source As Pointer, mask As Pointer, foreground_color As XColor, background_color As XColor, x As Integer, y As Integer) As Integer
+
  Private Extern XCreatePixmapCursor(display As Pointer, source As Pointer, mask As Pointer, foreground_color As XColor, background_color As XColor, x As Integer, y As Integer) As Integer
 
   
 
   
 
  <FONT color=gray>' ''XDefineCursor(Display *display, Window w, Cursor cursor)''
 
  <FONT color=gray>' ''XDefineCursor(Display *display, Window w, Cursor cursor)''
 
  ' ''If a cursor is set, it will be used when the pointer is in the window.''</font>
 
  ' ''If a cursor is set, it will be used when the pointer is in the window.''</font>
  Private Extern XDefineCursor(displayP As Pointer, w As Long, cursorI As Integer) As Integer
+
  Private Extern XDefineCursor(display As Pointer, w As Long, cursorI As Integer) As Integer
 
    
 
    
 
  <FONT color=gray>' ''XSelectInput (Display *display, Window w, long event_mask)''
 
  <FONT color=gray>' ''XSelectInput (Display *display, Window w, long event_mask)''
 
  ' ''requests that the X server report the events associated with the specified Event mask.''</font>
 
  ' ''requests that the X server report the events associated with the specified Event mask.''</font>
  Private Extern XSelectInput(displayP As Pointer, w As Long, event_mask As Long)
+
  Private Extern XSelectInput(display As Pointer, w As Long, event_mask As Long)
 
    
 
    
 
  <FONT color=gray>' ''XNextEvent (Display *display, XEvent *event_return)''
 
  <FONT color=gray>' ''XNextEvent (Display *display, XEvent *event_return)''
 
  ' ''gets the next event and remove it from the queue''</font>
 
  ' ''gets the next event and remove it from the queue''</font>
  Private Extern XNextEvent(displayP As Pointer, event_return As XEventStruct)
+
  Private Extern XNextEvent(display As Pointer, event_return As XButtonEvent)
 
   
 
   
 
  <FONT color=gray>' ''XDestroyWindow(Display *display, Window w)''
 
  <FONT color=gray>' ''XDestroyWindow(Display *display, Window w)''
 
  ' ''destroys the specified window as well as all of its subwindows''</font>
 
  ' ''destroys the specified window as well as all of its subwindows''</font>
  Private Extern XDestroyWindow(displayP As Pointer, w As Long)
+
  Private Extern XDestroyWindow(display As Pointer, w As Long)
 
   
 
   
 
  <FONT color=gray>' ''XCloseDisplay(Display *display)''
 
  <FONT color=gray>' ''XCloseDisplay(Display *display)''
 
  ' ''Closes the connection to the X server for the display specified in the Display structure and destroys all windows.''</font>
 
  ' ''Closes the connection to the X server for the display specified in the Display structure and destroys all windows.''</font>
  Private Extern XCloseDisplay(displayP As Pointer)
+
  Private Extern XCloseDisplay(display As Pointer)
 
    
 
    
<FONT color=gray>' ''void exit(int status)''
 
' ''Terminates the calling process immediately. Any open file descriptors belonging to the process are closed.''</font>
 
Private Extern exit_C(status As Integer) In "libc:6" Exec "exit"
 
 
 
   
 
   
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
Riga 554: Riga 626:
 
   Dim id, cursore_matita As Integer
 
   Dim id, cursore_matita As Integer
 
   Dim inut, cursore_primopiano, sfondo_cursore As New XColor
 
   Dim inut, cursore_primopiano, sfondo_cursore As New XColor
   Dim ev As New XEventStruct
+
   Dim ev As New XButtonEvent
 
   Dim matita_bits As Byte[] = [&00, &70, &00, &88, &00, &8C, &00, &96, &00, &69, &80, &30, &40, &10, &20, &08,
 
   Dim matita_bits As Byte[] = [&00, &70, &00, &88, &00, &8C, &00, &96, &00, &69, &80, &30, &40, &10, &20, &08,
 
                               &10, &04, &08, &02, &08, &01, &94, &00, &64, &00, &1E, &00, &06, &00, &00, &00]
 
                               &10, &04, &08, &02, &08, &01, &94, &00, &64, &00, &1E, &00, &06, &00, &00, &00]
Riga 564: Riga 636:
 
   Dim matita_Y As Integer = 15
 
   Dim matita_Y As Integer = 15
 
   
 
   
  dsp = XOpenDisplay(Null)
+
  dsp = XOpenDisplay(0)
 +
  If dsp == 0 Then Error.Raise("Impossibile connettersi al server X !")
 +
 
 +
  screen = XDefaultScreenOfDisplay(dsp)
 +
 
 +
  id = XCreateSimpleWindow(dsp, XDefaultRootWindow(dsp), 0, 0, 300, 200, 0, 0, &FF0000)
 +
  Print "ID della finestra creata: "; Hex(id, 6)
 +
 
 +
  XMapRaised(dsp, id)
 
    
 
    
  screen = XDefaultScreenOfDisplay(dsp)
 
 
  id = XCreateSimpleWindow(dsp, XDefaultRootWindow(dsp), 0, 0, 300, 200, 0, 0, &FF0000)
 
  Print "ID della finestra creata: "; Hex(id, 6)
 
 
  XMapRaised(dsp, id)
 
 
 
  <FONT color=gray>' ''Crea le pixmap per il cursore del mouse:''</font>
 
  <FONT color=gray>' ''Crea le pixmap per il cursore del mouse:''</font>
  pixmap = XCreatePixmap(dsp, XDefaultRootWindow(dsp), 1, 1, 1)
+
  pixmap = XCreatePixmap(dsp, XDefaultRootWindow(dsp), 1, 1, 1)
 
    
 
    
  XLookupColor(dsp, XDefaultColormapOfScreen(screen), "yellow", inut, cursore_primopiano)
+
  XLookupColor(dsp, XDefaultColormapOfScreen(screen), "yellow", inut, cursore_primopiano)
  XLookupColor(dsp, XDefaultColormapOfScreen(screen), "blue", inut, sfondo_cursore)
+
  XLookupColor(dsp, XDefaultColormapOfScreen(screen), "blue", inut, sfondo_cursore)
+
 
  matita = XCreatePixmapFromBitmapData(dsp, pixmap, matita_bits, matita_width, matita_height, 1, 0, 1)
+
  matita = XCreatePixmapFromBitmapData(dsp, pixmap, matita_bits, matita_width, matita_height, 1, 0, 1)
  matita_mask = XCreatePixmapFromBitmapData(dsp, pixmap, matita_mask_bits, matita_width, matita_height, 1, 0, 1)
+
  matita_mask = XCreatePixmapFromBitmapData(dsp, pixmap, matita_mask_bits, matita_width, matita_height, 1, 0, 1)
 
      
 
      
  cursore_matita = XCreatePixmapCursor(dsp, matita, matita_mask, cursore_primopiano, sfondo_cursore, matita_X, matita_Y)
+
  cursore_matita = XCreatePixmapCursor(dsp, matita, matita_mask, cursore_primopiano, sfondo_cursore, matita_X, matita_Y)
+
 
  XDefineCursor(dsp, id, cursore_matita)
+
  XDefineCursor(dsp, id, cursore_matita)
+
 
+
  XSelectInput(dsp, id, ButtonPressMask)
  XSelectInput(dsp, id, ButtonPressMask)
+
 
+
  Repeat
  While True
+
    XNextEvent(dsp, ev)
    XNextEvent(dsp, ev)
+
  Until ev.type == ButtonPress
    If ev.type = ButtonPress Then chiude_X(dsp, id)
+
   
  Wend
 
 
'''End'''
 
 
 
'''Private''' Procedure chiude_X(disp_ch As Pointer, chiudId As Integer)
 
 
 
  <FONT color=gray>' ''Chiude la finestra:''</font>
 
  <FONT color=gray>' ''Chiude la finestra:''</font>
  XDestroyWindow(disp_ch, chiudId)
+
  XDestroyWindow(dsp, id)
+
  XCloseDisplay(dsp)
  XCloseDisplay(disp_ch)
 
 
  exit_C(0)
 
 
   
 
   
 
  '''End'''
 
  '''End'''
  
  
 +
 +
=Note=
 +
[1] Vedere questa pagina: https://tronche.com/gui/x/xlib/events/structures.html
  
  
Riga 615: Riga 681:
 
* http://it.wikipedia.org/wiki/X_Window_System
 
* http://it.wikipedia.org/wiki/X_Window_System
 
* [http://www.x.org/wiki/ X.Org project]
 
* [http://www.x.org/wiki/ X.Org project]
 +
* [http://tronche.com/gui/x/xlib/ Manuale della libreria X]
 +
* [http://tronche.com/gui/x/xlib-tutorial/2nd-program-anatomy.html Anatomy of the most basic Xlib program]
 +
* http://www.sbin.org/doc/Xlib/
 
* [http://www.df.unipi.it/~moruzzi/xlib-programming.html http://www.df.unipi.it/~moruzzi/xlib-programming.html]
 
* [http://www.df.unipi.it/~moruzzi/xlib-programming.html http://www.df.unipi.it/~moruzzi/xlib-programming.html]
 
* [http://math.msu.su/~vvb/2course/Borisenko/CppProjects/GWindow/xintro.html#over A Brief intro to X11 Programming]
 
* [http://math.msu.su/~vvb/2course/Borisenko/CppProjects/GWindow/xintro.html#over A Brief intro to X11 Programming]
 
* [http://h71000.www7.hp.com/doc/73final/5642/5642pro_index.html Guide to Xlib]
 
* [http://h71000.www7.hp.com/doc/73final/5642/5642pro_index.html Guide to Xlib]
* [http://tronche.com/gui/x/xlib/ Manuale della libreria X]
 
* [http://tronche.com/gui/x/xlib-tutorial/2nd-program-anatomy.html Anatomy of the most basic Xlib program]
 
 
* [http://buffa.developpez.com/xwindow/?page=page_1 Étude et programmation du système X Window]
 
* [http://buffa.developpez.com/xwindow/?page=page_1 Étude et programmation du système X Window]
 
* [http://xopendisplay.hilltopia.ca/index.html Programming Xlib Tutorial]
 
* [http://xopendisplay.hilltopia.ca/index.html Programming Xlib Tutorial]

Versione attuale delle 15:52, 1 mag 2023

Con le risorse del API di X11 è possibile creare una finestra ed interagire con essa.

Sarà necessario richiamare nell'applicazione Gambas la libreria dinamica condivisa di X11: "libX11.so.6.4.0 ".

Gli Eventi nel sistema X11

Ogni tipo di Evento ha una propria Struttura, che in realtà è una "Union" tipica del linguaggio C. Nella libreria di X11 tutte le Strutture degli Eventi hanno in comune questi primi 5 membri: [Nota 1]

typedef struct {
        int type;
        unsigned long serial;
        Bool send_event;
        Display *display;
        Window window;
} XAnyEvent;

Pertanto, nella ricostruzione in Gambas della Struttura tipica (la "Union") di un determinato Evento bisognerà tenerne conto.
A questi primi 5 membri comuni seguiranno i restanti caratteristici dell'Evento specifico.

Poiché la dimensione totale della Union è pari a 192 byte, per evitare un errore alla chiusura della finestra creata, è necessario colmare la Struttura, ricostruita in Gambas, con un numero di byte sino a raggiungere la dimensione totale della Union medesima (192 byte).


Esempi pratici

Mostriamo di seguito alcuni esempi pratici, nei quali ssaranno create delle finestre, sulle quali si potrà agire attraverso la gestione di uno specifico Evento.

Creare semplicemente una finestra colorata

Mostriamo di seguito un breve codice con le risorse essenziali della libreria X11 per creare una semplice finestra di colore rosso. Se si cliccherà al suo interno con un qualsiasi tasto del mouse, la finestra verrà distrutta.

Library "libX11:6.4.0"

Private Const ButtonPressMask As Long = 4
Private Enum KeyPress = 2, KeyRelease, ButtonPress, ButtonRelease, MotionNotify, EnterNotify,
              LeaveNotify, FocusIn, FocusOut, KeymapNotify, Expose, GraphicsExpose, NoExpose

' Display *XOpenDisplay(char *display_name)
' Opens a connection to the X server that controls a display.
Private Extern XOpenDisplay(display As Pointer) As Pointer
 
' Window XDefaultRootWindow(Display *display)
' Return the root window for the default screen.
Private Extern XDefaultRootWindow(display As Pointer) As Integer

' Window XCreateSimpleWindow(Display *display, Window parent, int x, int y, unsigned int width, unsigned int height, unsigned int border_width, unsigned long border, unsigned long background)
' Creates an unmapped InputOutput subwindow for a specified parent window, returns the window ID of the created window.
Private Extern XCreateSimpleWindow(display As Pointer, parent As Long, x As Integer, y As Integer, width As Integer, height As Integer, border_width As Integer, border As Integer, background As Long) As Integer

' XSelectInput (Display *display, Window w, long event_mask)
' Requests that the X server report the events associated with the specified Event mask.
Private Extern XSelectInput(display As Pointer, w As Long, event_mask As Long)

' XMapRaised (Display *display, Window w)
' Raises the specified window to the top of the stack.
Private Extern XMapRaised(display As Pointer, w As Long)

' XNextEvent (Display *display, XEvent *event_return)
' Gets the next event and remove it from the queue
Private Extern XNextEvent(display As Pointer, event_return As Pointer)

' XDestroyWindow(Display *display, Window w)
' Destroys the specified window as well as all of its subwindows
Private Extern XDestroyWindow(display As Pointer, w As Long)

' XCloseDisplay(Display *display)
' Closes the connection to the X server for the display specified in the Display structure and destroys all windows.
Private Extern XCloseDisplay(display As Pointer)
  

Public Sub Main()

 Dim disp, ev As Pointer
 Dim screen As Integer
 Dim id As Long

' Apre la connessione con il server display del sistema grafico X:
 disp = XOpenDisplay(0)
 If disp == 0 Then Error.Raise("Impossibile aprire il server X !")
   
' Crea la finestra secondo i parametri previsti dalla funzione. L'ultimo parametro imposta il colore di fondo della finestra:
 id = XCreateSimpleWindow(disp, XDefaultRootWindow(disp), 0, 0, 300, 200, 0, 0, &FF0000)
 Print "ID della finestra creata: "; Hex(id, 6)

' Dice al server display quali eventi deve vedere:
 XSelectInput(disp, id, ButtonPressMask)
  
' Apre la finestra sullo schermo. Si può utilizzare anche la funzione "XMapWindow(display, w)":
 XMapRaised(disp, id)
  
' Alloca un'area di memoria pari alla Struttura esterna (192 byte).
' Poiché sarà utilizzato soltanto il valore del primo valore di detta Struttura esterna, si utilizzerà quest'area di memoria riservata al posto della Struttura predetta:
 ev = Alloc(SizeOf(gb.Byte), 192)
 
' Inizia il ciclo, restando in attesa di un evento stabilito con la precedente funzione XSelectInput():
 Repeat
   XNextEvent(disp, ev)
' Se si pone il puntatore del mouse all'interno della finestra e viene premuto un qualsiasi tasto del mouse, chiude la finestra:
 Until Int@(ev) == ButtonPress
 
' Chiude la finestra e libera la memoria:
 Free(ev)
 XDestroyWindow(disp, id)
 XCloseDisplay(disp)
   
End


In quest'altro esempio, molto simile al precedente, la chiusura della finestra avverrà solo se si premerà con il mouse sulla consueta X posta nell'angolo in alto a destra della finestra medesima:

Library "libX11:6.4.0"

Public Struct XButtonEvent
  type As Integer
  serial As Long
  send_event As Boolean
  display As Pointer
  window_ As Long
  root As Long
  subwindow As Long
  time_ As Long
  x As Integer
  y As Integer
  x_root As Integer
  y_root As Integer
  state As Integer
  keycode As Integer
  same_screen As Boolean
  pad[12] As Long         ' Completa la "Struttura" sino a 192 byte
End Struct

Private Const ButtonPressMask As Long = 4
Private Enum KeyPress = 2, KeyRelease, ButtonPress, ButtonRelease, MotionNotify, EnterNotify,
             LeaveNotify, FocusIn, FocusOut, KeymapNotify, Expose, GraphicsExpose, NoExpose

' Display *XOpenDisplay(char *display_name)
' Opens a connection to the X server that controls a display.
Private Extern XOpenDisplay(display As Pointer) As Pointer
 
' Window XRootWindow(Display *display, int screen_number)
' Return the root window.
Private Extern XDefaultRootWindow(display As Pointer) As Integer

' Window XCreateSimpleWindow(Display *display, Window parent, int x, int y, unsigned int width, unsigned int height, unsigned int border_width, unsigned long border, unsigned long background)
' Creates an unmapped InputOutput subwindow for a specified parent window, returns the window ID of the created window.
Private Extern XCreateSimpleWindow(display As Pointer, parent As Long, x As Integer, y As Integer, width As Integer, height As Integer, border_width As Integer, border As Integer, background As Long) As Integer

' XSelectInput (Display *display, Window w, long event_mask)
' Requests that the X server report the events associated with the specified Event mask.
Private Extern XSelectInput(display As Pointer, w As Long, event_mask As Long)

' XMapRaised (Display *display, Window w)
' Raises the specified window to the top of the stack.
Private Extern XMapRaised(display As Pointer, w As Long)

' Atom XInternAtom(Display *display, char *atom_name, Bool only_if_exists)
' Returns the atom identifier associated with the specified atom_name string.
Private Extern XInternAtom(display As Pointer, atom_name As String, only_if_exists As Boolean) As Long

' Status XSetWMProtocols(Display *display, Window w, Atom *protocols, int count)
' Replaces the WM_PROTOCOLS property on the specified window with the list of atoms specified by the protocols argument.
Private Extern XSetWMProtocols(display As Pointer, w As Long, protocols As Pointer, count As Integer) As Integer

' XNextEvent (Display *display, XEvent *event_return)
' Gets the next event and remove it from the queue
Private Extern XNextEvent(display As Pointer, event_return As XButtonEvent)

' XDestroyWindow(Display *display, Window w)
' Destroys the specified window as well as all of its subwindows
Private Extern XDestroyWindow(display As Pointer, w As Long)

' XCloseDisplay(Display *display)
' Closes the connection to the X server for the display specified in the Display structure and destroys all windows.
Private Extern XCloseDisplay(display As Pointer)
  

Public Sub Main()

 Dim disp As Pointer
 Dim ev As New XButtonEvent
 Dim id, atom As Long

' Apre la connessione con il server display del sistema grafico X:
 disp = XOpenDisplay(0)
 If disp == 0 Then Error.Raise("Impossibile aprire il server X !")
   
' Crea la finestra secondo i parametri previsti dalla funzione. L'ultimo parametro imposta il colore di fondo della finestra:
 id = XCreateSimpleWindow(disp, XDefaultRootWindow(disp), 0, 0, 300, 200, 0, 0, &FF0000)
 Print "ID della finestra creata: "; Hex(id, 6)

' Dice al server display quali eventi deve vedere:
 XSelectInput(disp, id, ButtonPressMask)
  
' Apre la finestra sullo schermo. Si può utilizzare anche la funzione "XMapWindow(display, w)":
 XMapRaised(disp, id)
  
' Ottiene il numero identificativo dell'evento della chiusura della finestra, quando si clicca sulla X in alto a destra:
 atom = XInternAtom(disp, "WM_DELETE_WINDOW", False)
  
 XSetWMProtocols(disp, id, VarPtr(atom), 1)
    
' Inizia il ciclo, restando in attesa di un evento stabilito con la precedente funzione XSelectInput():
 Repeat
   XNextEvent(disp, ev)
' Si esce dal ciclo, solo se si clicca con il mouse sulla X in alto a destra della finestra:
 Until ev.time_ == atom
   
' Chiude la finestra e libera la memoria:
 XDestroyWindow(disp, id)
 XCloseDisplay(disp)
  
End


Creare una finestra colorata contenente disegni geometrici e caratteri alfabetici, con impostazione del puntatore del mouse

In quest'altro esempio, più complesso, verrà creata una finestra di colore rosso, nella quale saranno disegnate figure geometriche e caratteri testuali. Sarà, inoltre, possibile intercettare alcuni eventi provenienti dalla tastiera e dal mouse. Verrà anche impostato un diverso aspetto del puntatore del mouse, impostato fra uno dei disegni previsti nel file header "x11/cursorfont.h".
Per chiudere la finestra, bisognerà premere il tasto "q" della tastiera.

Private disp As Pointer
Private wId As Long
Private gc As Pointer


Library "libX11:6.4.0"

Public Struct XKeyEvent
  type As Integer
  serial As Long
  send_event As Boolean
  display As Pointer
  window_ As Long
  root As Long
  subwindow As Long
  time_ As Long
  x As Integer
  y As Integer
  x_root As Integer
  y_root As Integer
  state As Integer
  keycode As Integer
  same_screen As Boolean
  pad[12] As Long         ' Completa la "Struttura" sino a 192 byte
End Struct

Public Struct XFont
  ext_data As Long
  fid As Pointer
  direction As Byte
  min_char_or_byte2 As Byte
  max_char_or_byte2 As Byte
  min_byte1 As Byte
  max_byte1 As Byte
  all_chars_exist As Boolean
  default_char As Byte
  n_properties As Byte
  properties As Pointer
  min_bounds As Pointer
  max_bounds As Pointer
  per_char As Pointer
  ascent As Integer
  descent As Integer
End Struct

Private Const ExposureMask As Long = 32768
Private Const KeyPressMask As Long = 1
Private Const ButtonPressMask As Long = 4
Private Enum KeyPress = 2, KeyRelease, ButtonPress, ButtonRelease, MotionNotify, EnterNotify, LeaveNotify, FocusIn, FocusOut, KeymapNotify, Expose, GraphicsExpose, NoExpose
Private Enum FillSolid = 0, FillTiled, FillStippled, FillOpaqueStippled   ' fillStyle

' Display *XOpenDisplay(char *display_name)
' Opens a connection to the X server that controls a display.
Private Extern XOpenDisplay(display As Pointer) As Pointer

' XCloseDisplay(Display *display)
' Closes the connection to the X server for the display specified in the Display structure and destroys all windows.
Private Extern XCloseDisplay(display As Pointer)

' int XDefaultScreen (Display *display)
' returns the default screen number referenced by the XOpenDisplay function.
Private Extern XDefaultScreen(display As Pointer) As Integer

' unsigned long XWhitePixel (Display *display, int screen_number)
' returns the white pixel value for the specified screen.
Private Extern XWhitePixel(display As Pointer, screen_number As Integer) As Long

' unsigned long XBlackPixel (Display *display, int screen_number)
' returns the black pixel value for the specified screen.
Private Extern XBlackPixel(display As Pointer, screen_number As Integer) As Long

' Window XDefaultRootWindow(Display *display)
' Return the root window for the default screen.
Private Extern XDefaultRootWindow(display As Pointer) As Integer

' Window XCreateSimpleWindow(Display *display,  Window parent, int x, int y, unsigned int width, unsigned int height, unsigned int border_width, unsigned long border, unsigned long background)
' creates an unmapped InputOutput subwindow for a specified parent window, returns the window ID of the created window.
Private Extern XCreateSimpleWindow(display As Pointer, parent As Long, x As Integer, y As Integer, width As Integer, height As Integer, border_width As Integer, border As Integer, background As Long) As Integer 

' XSetStandardProperties (Display *display, Window w, char *window_name, char *icon_name, Pixmap icon_pixmap, char **argv, int argc, XSizeHints *hints)
' specifies a minimum set of properties describing the simplest application.
Private Extern XSetStandardProperties(display As Pointer, w As Long, window_name As String, icon_name As String, icon_pixmap As Integer, argv As String, argc As Integer, hints As Pointer)

' XSelectInput (Display *display, Window w, long event_mask)
' requests that the X server report the events associated with the specified Event mask.
Private Extern XSelectInput(display As Pointer, w As Integer, event_mask As Long)

' GC XCreateGC(Display *display, Drawable d, unsigned long valuemask, XGCValues *values)
' creates a graphics context and returns a GC.
Private Extern XCreateGC(display As Pointer, w As Integer, valuemask As Long, values As Pointer) As Pointer

' XSetForeground (Display *display, GC gc, unsigned long foreground)
' sets the foreground.
Private Extern XSetForeground(display As Pointer, gc As Pointer, foreground As Long)

' char ** XListFonts(Display *display, char *pattern, int maxnames, int *actual_count_return)
' returns an array of available font names.
Private Extern XListFonts(display As Pointer, pattern As String, maxnames As Integer, actual_count_return As Pointer) As Pointer

' XFontStruct * XLoadQueryFont(Display *display, char *name)
' opens(loads)the specified font.
Private Extern XLoadQueryFont(display As Pointer, nameFont As String) As XFont

' XSetFont(Display *display, GC gc, Font font)
' Assigns a Font to a Graphics Context.
Private Extern XSetFont(display As Pointer, gcP As Pointer, fontP As Pointer)

' XSetFillStyle(Display *display, GC gc, int fill_style)
' changes the fill style of GC.
Private Extern XSetFillStyle(display As Pointer, gc As Pointer, fill_style As Integer)

' XClearWindow(Display *display, Window w)
' clears the entire area in the specified window.
Private Extern XClearWindow(display As Pointer, w As Long)

' XMapRaised (Display *display, Window w)
' raises the specified window to the top of the stack.
Private Extern XMapRaised(display As Pointer, w As Long)

' Cursor XCreateFontCursor(Display *display, unsigned int shape)
' Provides a set of standard cursor shapes in a special font named cursor.
Private Extern XCreateFontCursor(display As Pointer, shape As Integer) As Integer

' XDefineCursor(Display *display, Window w, Cursor cursor)
' If a cursor is set, it will be used when the pointer is in the window.
Private Extern XDefineCursor(display As Pointer, w As Long, cursorI As Integer) As Integer

' XNextEvent (Display *display, XEvent *event_return)
' gets the next event and remove it from the queue
Private Extern XNextEvent(display As Pointer, event_return As XKeyEvent)

' int XLookupString(XKeyEvent *event_struct, char *buffer_return, int bytes_buffer, KeySym *keysym_return, XComposeStatus *status_in_out)
' translates a key event to a KeySym and a string.
Private Extern XLookupString(event_struct As XKeyEvent, buffer_return As Byte[], bytes_buffer As Integer, keysym_return As Pointer, status_in_out As Pointer) As Integer

' XDrawPoint(Display *display, Drawable d, GC gc, int x, int y)
' draws a single point into the specified drawable
Private Extern XDrawPoint(display As Pointer, w As Long, gcP As Pointer, x1 As Integer, y1 As Integer)

' XDrawLine(Display *display, Drawable d, GC gc, int x1, int y1, int x2, int y2)
' draws a line between the specified set of points (x1, y1) and (x2, y2).
Private Extern XDrawLine(display As Pointer, w As Long, gc As Pointer, x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer)

' XDrawRectangle(Display *display, Drawable d, GC gc, int x, int y, unsigned int width, unsigned int height)
' draws the outlines of the specified rectangle.
Private Extern XDrawRectangle(display As Pointer, w As Integer, gc As Pointer, xI As Integer, yI As Integer, widthI As Integer, heightI As Integer)

' XFillRectangle(Display *display, Drawable d, GC gc, int x, int y, unsigned int width, unsigned int height)
' fills the specified rectangle
Private Extern XFillRectangle(display As Pointer, w As Long, gc As Pointer, xI As Integer, yI As Integer, widthI As Integer, heightI As Integer)

' XDrawArc(Display *display, Drawable d, GC gc, int x, int y, unsigned int width, unsigned int height, int angle1, int angle2)
' draws an arc.
Private Extern XDrawArc(display As Pointer, w As Long, gc As Pointer, xI As Integer, yI As Integer, widthI As Integer, heightI As Integer, angle1 As Integer, angle2 As Integer)

' XFillArc(Display *display, Drawable d, GC gc, int x, int y, unsigned int width, unsigned int height, int angle1, int angle2)
' fills the specified arc.
Private Extern XFillArc(display As Pointer, w As Long, gc As Pointer, xI As Integer, yI As Integer, widthI As Integer, heightI As Integer, angle1 As Integer, angle2 As Integer)

' XDrawString(Display *display, Drawable d, GC gc, int x, int y, char *string, int length)
' draws a text.
Private Extern XDrawString(display As Pointer, w As Long, gc As Pointer, x As Integer, y As Integer, string_ As Pointer, length As Integer)

' XFreeGC(Display *display, GC gc)
' destroys the specified GC as well as all the associated storage.
Private Extern XFreeGC(display As Pointer, gc As Pointer)

' XDestroyWindow(Display *display, Window w)
' destroys the specified window as well as all of its subwindows
Private Extern XDestroyWindow(display As Pointer, w As Long)
 

Public Sub Main()

 Dim screen, key, cursor As Integer
 Dim pxW, pxB As Long
 Dim testo As New Byte[255]
 Dim ev As New XKeyEvent
 Dim xfon As New XFont
 Dim scriptum As String = "testo qualsiasi"

' Apre la connessione con il server display del sistema grafico X:
 disp = XOpenDisplay(0)
 If disp == 0 Then Error.Raise("Impossibile aprire il server X !")
 
 screen = XDefaultScreen(disp)
 pxW = XWhitePixel(disp, screen)
 pxB = XBlackPixel(disp, screen)
    
' Crea la finestra secondo i parametri previsti dalla funzione.
' L'ultimo parametro imposta il colore di fondo della finestra:
 wId = XCreateSimpleWindow(disp, XDefaultRootWindow(disp), 0, 0, 300, 200, 5, pxW, &FF0000)
 Print "ID della finestra creata: "; Hex(wId, 6)
   
 XSetStandardProperties(disp, wId, "Prova creazione finestra", "Nome icona finestra", 0, Null, 0, Null)
   
' Dice al server display quali eventi deve vedere:
 XSelectInput(disp, wId, ExposureMask Or KeyPressMask Or ButtonPressMask)
   
' Crea un nuovo contesto grafico:
 gc = XCreateGC(disp, wId, 0, 0)
 If gc == 0 Then Error.Raise("Impossibile creare un nuovo contesto grafico !")

' Imposta il colore degli elementi grafici all'interno della finestra:
 XSetForeground(disp, gc, &00FF00)

 elencoFont()

' Imposta il font per i caratteri del testo grafico:
 xfon = XLoadQueryFont(disp, "*bitstream*")
 If IsNull(xFon) Then Error.Raise("Impossibile caricare il font !")
 
 XSetFont(disp, gc, xfon.fid)
 XSetFillStyle(disp, gc, FillSolid)
   
 redraw()
   
' Apre la finestra sullo schermo. Si può utilizzare anche la funzione "XMapWindow(display, w)":
 XMapRaised(disp, wId)

' Entrando il cursore del mouse nella finestra, esso cambia aspetto, impostando quello che nel file header ".../X11/cursorfont.h" ha il valore 10 tra quelli visibili nella pagina "http://tronche.com/gui/x/xlib/appendix/b/":
 cursor = XCreateFontCursor(disp, 10)

 XDefineCursor(disp, wId, cursor)
 
 While True
   XNextEvent(disp, ev)
   Select Case ev.type
     Case Expose
' La finestra mostrata deve essere ripulita:
       redraw()
' Disegna un punto all'interno del quadrato:
       XDrawPoint(disp, wId, gc, 70, 120)
' Disegna una linea:
       XDrawLine(disp, wId, gc, 10, 10, 40, 60)
' In questo caso disegna un quadrato:
       XDrawRectangle(disp, wId, gc, 50, 100, 40, 40)
' Disegna un rettangolo colorato di bianco:
       XFillRectangle(disp, wId, gc, 200, 30, 60, 40)
' Disegna un arco:
       XDrawArc(disp, wId, gc, 100, 150, 60, 40, 0, 360 * 20)
' Disegna un arco riempito di colore bianco:
       XFillArc(disp, wId, gc, 40, 20, 60, 40, 0, 360 * 20)
' Disegna un cerchio:
       XDrawArc(disp, wId, gc, 200, 100, 50, 50, 0, 360 * 64)
' Disegna un cerchio riempito di colore bianco:
       XFillArc(disp, wId, gc, 110, 40, 50, 50, 0, 360 * 64)
' Disegna una stringa di caratteri:
       XDrawString(disp, wId, gc, 20, 170, scriptum, Len(scriptum))
     Case KeyPress
       XLookupString(ev, testo, 255, VarPtr(key), Null)
       If testo[0] == Asc("q") Then
         chiude_X()
         Print "E' stato premuto il tasto: "; Chr(testo[0]), key
       Else
' Disegna il carattere del tasto premuto alle coordinate del puntatore del mouse:
         XDrawString(disp, wId, gc, ev.x, ev.y, Chr(key), 1)
       Endif
     Case ButtonPress     ' Indica le coordinate ove è stato premuto il tasto del mouse
       With ev
         Print "E' stato premuto il tasto del mouse alle seguenti coordinate:\n"; .x; " pixel dal margine sinistro della finestra;"
         Print .y; " pixel dal margine superiore della finestra;"
         Print .x_root; " pixel dal margine sinistro dello schermo;"
         Print .y_root; " pixel dal margine superiore dello schermo." 
       End With
       redraw()
   End Select
 Wend
   
End 

Private Procedure elencoFont()

 Dim po As Pointer
 Dim num_fonts, i As Integer
 Dim b As Byte
 
 po = XListFonts(disp, "*itstr*", 8196, VarPtr(num_fonts))
 If po == 0 Then Error.Raise("Impossibile caricare i font !")
 
 po = Pointer@(po)
  
' Dereferenziamo il "puntatore":
 For i = 0 To 8196
   b = Byte@(po + i)
   If b == 0 Then
     Print
   Else
     Print Chr(b);
   Endif
 Next
   
End 
 
Private Procedure redraw()
 
' Pulisce la superficie della finestra da ogni elemento presente:
 XClearWindow(disp, wId)
 
End


Private Procedure chiude_X()

 XFreeGC(disp, gc)
' Chiude la finestra:
 XDestroyWindow(disp, wId)
 XCloseDisplay(disp)
' "Quit" consente di terminare anche il programma medesimo:
 Quit
 
End


Creare una semplice finestra colorata, con il puntatore del mouse avente un disegno impostato liberamente da noi

In questo esempio quando si entrerà con il puntatore del mouse all'interno della finestra, l'aspetto del puntatore cambierà in quello di una matita, secondo un disegno da noi impostato.
Per chiudere la finestra, si dovrà cliccare al suo interno.

Library "libX11:6.4.0"

Public Struct XColor
  pixel As Long
  red As Short
  green As Short
  blue As Short
  flags As Byte
  pad As Byte
End Struct

Public Struct XButtonEvent
  type As Integer
  serial As Long
  send_event As Boolean
  display As Pointer
  window_ As Long
  root As Long
  subwindow As Long
  time_ As Long
  x As Integer
  y As Integer
  x_root As Integer
  y_root As Integer
  state As Integer
  keycode As Integer
  same_screen As Boolean
  pad[12] As Long         ' Completa la "Struttura" sino a 192 byte
End Struct

Private Const ButtonPressMask As Long = 4
Private Enum KeyPress = 2, KeyRelease, ButtonPress, ButtonRelease, MotionNotify, EnterNotify,
             LeaveNotify, FocusIn, FocusOut, KeymapNotify, Expose, GraphicsExpose, NoExpose

' Display *XOpenDisplay(char *display_name)
' Opens a connection to the X server that controls a display.
Private Extern XOpenDisplay(display As Pointer) As Pointer

' Screen *XDefaultScreenOfDisplay(Display *display)
' Returns a pointer to the default screen.
Private Extern XDefaultScreenOfDisplay(display As Pointer) As Pointer
 
' Window XDefaultRootWindow(Display *display)
' Return the root window for the default screen.
Private Extern XDefaultRootWindow(display As Pointer) As Integer

' Window XCreateSimpleWindow(Display *display, Window parent, int x, int y, unsigned int width, unsigned int height, unsigned int border_width, unsigned long border, unsigned long background)
' creates an unmapped InputOutput subwindow for a specified parent window, returns the window ID of the created window.
Private Extern XCreateSimpleWindow(display As Pointer, parent As Long, x As Integer, y As Integer, width As Integer, height As Integer, border_width As Integer, border As Integer, background As Long) As Integer
 
' XMapRaised (Display *display, Window w)
' raises the specified window to the top of the stack.
Private Extern XMapRaised(display As Pointer, w As Long)

' Pixmap XCreatePixmap(display *display, Drawable d, unsigned int width, unsigned int height, unsigned int depth)
' Creates a pixmap of the width, height, and depth you specified and returns a pixmap ID that identifies it.
Private Extern XCreatePixmap(display As Pointer, w As Long, width As Integer, height As Integer, depth As Integer) As Pointer

' Status XLookupColor(Display *display, Colormap colormap, char *color_name, XColor *exact_def_return, XColor *screen_def_return)
' Looks up the string name of a color with respect to the screen associated with the specified colormap.
Private Extern XLookupColor(display As Pointer, colormap As Pointer, color_name As String, exact_def_return As XColor, screen_def_return As XColor) As Integer

' Colormap XDefaultColormapOfScreen(Screen *screen)
' Returns the default colormap ID on the specified screen.
Private Extern XDefaultColormapOfScreen(scr As Pointer) As Pointer

' Pixmap XCreatePixmapFromBitmapData(display *display, Drawable d, char *data, unsigned int width, unsigned int height, unsigned long fg, unsigned long bg, unsigned int depth)
' Creates a pixmap of the given depth and then does a bitmap-format XPutImage() of the data into it.
Private Extern XCreatePixmapFromBitmapData(display As Pointer, w As Pointer, data As Byte[], width As Integer, height As Integer, fg As Long, bg As Long, depth As Integer) As Pointer

' Cursor XCreatePixmapCursor(Display *display, Pixmap source, Pixmap mask, XColor *foreground_color, XColor *background_color, unsigned int x, unsigned int y)
' Creates a cursor and returns the cursor ID associated with it.
Private Extern XCreatePixmapCursor(display As Pointer, source As Pointer, mask As Pointer, foreground_color As XColor, background_color As XColor, x As Integer, y As Integer) As Integer

' XDefineCursor(Display *display, Window w, Cursor cursor)
' If a cursor is set, it will be used when the pointer is in the window.
Private Extern XDefineCursor(display As Pointer, w As Long, cursorI As Integer) As Integer
 
' XSelectInput (Display *display, Window w, long event_mask)
' requests that the X server report the events associated with the specified Event mask.
Private Extern XSelectInput(display As Pointer, w As Long, event_mask As Long)
 
' XNextEvent (Display *display, XEvent *event_return)
' gets the next event and remove it from the queue
Private Extern XNextEvent(display As Pointer, event_return As XButtonEvent)

' XDestroyWindow(Display *display, Window w)
' destroys the specified window as well as all of its subwindows
Private Extern XDestroyWindow(display As Pointer, w As Long)

' XCloseDisplay(Display *display)
' Closes the connection to the X server for the display specified in the Display structure and destroys all windows.
Private Extern XCloseDisplay(display As Pointer)
 

Public Sub Main()

 Dim dsp, screen, pixmap, matita, matita_mask As Pointer
 Dim id, cursore_matita As Integer
 Dim inut, cursore_primopiano, sfondo_cursore As New XColor
 Dim ev As New XButtonEvent
 Dim matita_bits As Byte[] = [&00, &70, &00, &88, &00, &8C, &00, &96, &00, &69, &80, &30, &40, &10, &20, &08,
                              &10, &04, &08, &02, &08, &01, &94, &00, &64, &00, &1E, &00, &06, &00, &00, &00]
 Dim matita_mask_bits As Byte[] = [&00, &F8, &00, &FC, &00, &FE, &00, &FF, &80, &FF, &C0, &7F, &E0, &3F, &F0,
                                   &1F, &F8, &0F, &FC, &07, &FC, &03, &FE, &01, &FE, &00, &7F, &00, &1F, &00, &07, &00]
 Dim matita_width As Integer = 16
 Dim matita_height As Integer = 16
 Dim matita_X As Integer = 1
 Dim matita_Y As Integer = 15

 dsp = XOpenDisplay(0)
 If dsp == 0 Then Error.Raise("Impossibile connettersi al server X !")
 
 screen = XDefaultScreenOfDisplay(dsp)
 
 id = XCreateSimpleWindow(dsp, XDefaultRootWindow(dsp), 0, 0, 300, 200, 0, 0, &FF0000)
 Print "ID della finestra creata: "; Hex(id, 6)
 
 XMapRaised(dsp, id)
 
' Crea le pixmap per il cursore del mouse:
 pixmap = XCreatePixmap(dsp, XDefaultRootWindow(dsp), 1, 1, 1)
  
 XLookupColor(dsp, XDefaultColormapOfScreen(screen), "yellow", inut, cursore_primopiano)
 XLookupColor(dsp, XDefaultColormapOfScreen(screen), "blue", inut, sfondo_cursore)
 
 matita = XCreatePixmapFromBitmapData(dsp, pixmap, matita_bits, matita_width, matita_height, 1, 0, 1)
 matita_mask = XCreatePixmapFromBitmapData(dsp, pixmap, matita_mask_bits, matita_width, matita_height, 1, 0, 1)
   
 cursore_matita = XCreatePixmapCursor(dsp, matita, matita_mask, cursore_primopiano, sfondo_cursore, matita_X, matita_Y)
 
 XDefineCursor(dsp, id, cursore_matita)
 
 XSelectInput(dsp, id, ButtonPressMask)
 
 Repeat
   XNextEvent(dsp, ev)
 Until ev.type == ButtonPress
    
' Chiude la finestra:
 XDestroyWindow(dsp, id)
 XCloseDisplay(dsp)

End


Note

[1] Vedere questa pagina: https://tronche.com/gui/x/xlib/events/structures.html


Riferimenti