| |
|
 |
Xlib tutorial 2 - Events and errors
|
Welcome back to the second Xlib tutorial. This time we will make our little program behave much more like a real application. We will learn how to handle events from the X server in order to process user input and resizing of our window. We will also set the title property of the window and how to handle errors.
|
|
The famous quit button
|
As a starting point for this tutorial I will use the object oriented version of the first tutorial. Only the new parts will be covered here, so if you have any questions about uncovered code take a look at tutorial 1.
The goal for today is to create a small application with a button that darkens on mouse over and quits the program when clicked. The application should also make the windowmanager show it's title in the titlebar.
The source code used in this tutorial can be downloaded with the link at the bottom of this page.
Setting an error handler
Whenever your program does something bad i.e calls drawing operations on a window that doesn't exist or you try to initialize a font with an attribute that is out of range the X server will generate a protocol error. Usually when this happens, and you haven't installed an error handler, the default error handler is called. The default error handler prints an explanatory message and then quits your program. This is quite a harsh reaction since such an error doesn't need to be fatal. In our program we won't extend the default behavior, only re implement it in order to get a feeling for how it works.
int errorHandler( Display *dpy, XErrorEvent *e )
{
char errorText[1024];
XGetErrorText( dpy, e->error_code, errorText, sizeof(errorText) );
printf( "**********************************\n" );
printf( "X Error: %s\n", errorText );
printf( "**********************************\n" );
exit( 1 );
}
| All error handler functions must take pointers to a Display structure and a XErrorEvent as parameters and return an integer. If you choose to ignore the error you can return any integer value you like as it is ignored by the server. Now you may ask what the display structure is good for as your program already knows the connection to the server! Or does it? Since errors can occur at any time you never know what you program was doing prior to the error call, and if your program connects to two servers it is nice to know which one generated the error!
The XErrorEvent structure holds information about the error that just occurred and the values of it can be used to determine what to do with the error, can we ignore it or is it fatal? Since we want to mimic the default error handler we need some textual information about the error. This is done with the XGetErrorText function which takes the usual display structure, an error code, a string to hold the error and the size of this string as parameters. XGetErrorText queries the servers error database and finds the string assigned to the given error. It is always a good idea to use this function instead of writing your own error messages. This is because X11 extensions may define their own error codes and error messages which may be unknown to you when you write your program.
After fetching the error message, we print it and exit the program.
Attached files:
Comment List
| Topic: |
Author: |
Time: |
|
Xlib ButtonHandler
|
Sant Kumar
|
16.11.2004 06:04
|
|
Hi
Everybody
I want to handle the mouse button to be clear
i am writing a Gui for which i have to handle the mouse i.e when ever the mouse comes to a particular rectangle it has to change its cursor and draw a line and this line should move with the mouse i.e this line has to repainted every time
is anyone to help me
Thanking u
Santhosh
|
|
salaf
|
osman altayeb
|
12.02.2004 11:29
|
|
i want to learn c++.
|
|
C++ make errors
|
Anthony Bargnesi
|
05.12.2003 17:18
|
|
I had troubles with
#include <stack> in painter.hpp
I noticed it wasn't including a header file, so i searched for stack.h...
I replaced #include <stack> with:
#include </c++/3.2.2/backward/stack.h>
where the stack.h header file was...
Now it works and compiles, and runs correctly.
Hope that helps somebody else, If not im the only one..haha..
Great Tutorials..COME OUT WITH MORE!
|
|
 |
|
|