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



Handling a resize

Allthough we do not yet know how to get a resize event, we do know what to do when it happens. This little piece of code does what we want: It fetches the size of the button and places it nicely in the middle of the available space.

void resize( Display *dpy, int mainW, int mainH, Window button )
{
    XWindowAttributes attr;
    // fetch button size
    XGetWindowAttributes( dpy, button, &attr );
    int buttonW = attr.width, buttonH = attr.height;
    
    // place button in middle
    XMoveWindow( dpy, button, (mainW - buttonW) /2, (mainH - buttonH)/2 );
}

The new function here is XMoveWindow which is one in a series of function to manipulate window attributes. XResizeWindow, XMoveResizeWindow and XConfigureWindow are the other functions in that series you will probably be using a lot. XMoveWindow takes two interesting parameters other than the usual display structure and the window you want to move; the x and the y coordinate you want to move your window to. This coordinate specifies where you want the top left corner of your window to be placed on the parent window. The coordinate is thus in the parent window coordinate space where 0,0 is the top left corner.

Telling X we want to receive events


Concept Event:An event is a way for the server to notify the client about changes in the program it is running. This could for example be that the mouse moves, the keyboard is pressed, that a part of your program has become visible and needs repainting or that your program window has been resized. Events will be queued up at the client until you decide to handle them or ignore them.
The relation between the event mask, the name of the event the event structure and the XEvent variable name for each event type can be a bit confusing in the beginning. This table might be helpfull if you get confused.

    XSelectInput( dpy, win, StructureNotifyMask | ExposureMask );
    XSelectInput( dpy, button, ButtonPressMask | ButtonReleaseMask 
                   | ExposureMask | EnterWindowMask | LeaveWindowMask );

Before we can start processing events we need to tell the server that we want to receive them. This is done with the XSelectInput function on a per window basis. If you want more than one event type to be propagated you can combine multiple masks with a logical or. For our application window we select the StructureNotifyMask and the ExposureMask. The StructureNotifyMask means that we will select ConfigureNotify events from this window. ConfigureNotify events are triggered for example by a window resize. The ExposureMask makes sure that we receive events when the main window or parts of it have become visible to the user.
For our button we also select ButtenPressMask, ButtonReleaseMask, EnterWindowMask and LeaveWindowMask. These are quite self explanatory as the button masks will allow mouse button (not keyboard) press and release events come through while the enter and leave window mask will generate events when the mouse moves into the button window or leaves the button window.

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