Latest

Archive

Community news

C++

Communities and Content

Databases

Editorials

Emacs

General

HTML

Java

Notices

PHP

XML

Apache

C++

Database

General

HTML

Java

Javascript

Linux

Object oriented programming

Open source

Perl

PHP

Python

Ruby

SOAP

XML

Suggest a link

Advertise on zez

Contribute

Contact us

About zez


Xlib tutorial 2 - Events and errors



Running a real event loop


    XEvent event;
    bool running = true;
    bool buttonHover = false;
    while( running )
    {
        XNextEvent( dpy, &event );
        switch( event.type )
        {
	  // the event handling code comes here.
	}
    }

Next up we create an instance of the XEvent union. Whenever you get an event with it will be of this type, and you have to examine it's type variable in order to determine the correct event type. The running variable is used to keep our program in the event loop until the user pressed our button. We'll set the buttonHover boolean to true if the mouse pointer is over the button and should be drawn with the shaded style.
Next up we enter the eventloop and call XNextEvent. XNextEvent is the simplest of the functions that manipulate the event queue. It just takes the first event and puts it into the event structure, if there are none it will block until an event is received. This is perfect for the global event loop, why do something if there is nothing to do?


Events, events and more events


Now that we have got an eventloop it is a good idea to actually do something with the events we have received.

     case Expose:
     {
          paintButton( dpy, painter, button );
     }
     break;


An expose event happens when one of the windows we specified ExposureMask for is shown and needs repainting. Since we have only one window where this can happen (the button) we simply call paintButton when we receive an ExposeEvent.
If your program has several windows that can receive expose events you must only repaint the window that was actually exposed. This can be done by checking the window member of the xconfigure structure received in the event (event.xconfigure.window in our case).

    case ConfigureNotify:
    {
         resize( dpy, event.xconfigure.width, event.xconfigure.height, button );
    }


You'll receive a ConfigureNotify event when something has physically changed with one of your windows i.e. if it has been moved or resized. We know that any ConfigureNotify events we received are on the main window since this is the only window we have specified the StructureNotifyMask on. When we receive the event we do now know if it was triggered because of a move or a resize. In this example we don't care since the overhead of re-computing the position of the button is minimal.
It is not necessary to repaint after a move. The X server will trigger expose events if this is needed.

     case ButtonPress:
     {
         paintButton( dpy, painter, button, PRESSED );
     }
     break;

If the mouse button is pressed, repaint the button in the PRESSED state.

     case EnterNotify:
     case LeaveNotify:
     {
          buttonHover = (event.type == EnterNotify ) ? true : false;
	  paintButton( dpy, painter, button, buttonHover ? HOVER : NORMAL );
     }
     break;

We get EnterNotify and LeaveNotify evenents when the mouse enters or leaves the button.Since we want to know if the mouse is really over the button in case it is pressed we toggle the buttonHover boolean according to the event before repainting the button with the correct state.

     case ButtonRelease:
     {
         if( event.xbutton.window == button && buttonHover ) 
         {
             paintButton( dpy, painter, button, HOVER );
             XFlush( dpy );  
             running = false;
         }
     }

When the mouse button is released we check that the event really happened on the button and that we are int the buttonHover state. When this is done we paint repaint the button one final time before setting running to false which will quit the event loop. However, since we are quitting the program we want to make sure that the button is actually repainted before we quit. This is done by calling XFlush which sends all buffered calls to the server. How is it that we don't need to do this after the other painting calls? That is because XNextEvent implicitly flushes the buffer before fetching the next event.

Attached files:


<< Previous page | 1 | 2 | 3 | < 4 > | 5 | Next page >> | Printer-friendly page |

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!




Forgot your password?

Register a new user

Results

Polls