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



Painting a button

Our program is supposed to paint a button in the center of the program window. To ease this task we implement a function paintButton which in addition to the display structure, a pointer to the painter object we are using and the window to paint the button in takes a state parameter. The state parameter can be NORMAL, HOVER or PRESSED.
When the button is in PRESSED or HOVER state we fill it with a darker Grey than when it's normal. In addition to this we move the text 2 pixels down and to the right when the button is PRESSED. This creates a 3d effect.

enum buttonState { NORMAL, HOVER, PRESSED };
void paintButton( Display *dpy, Painter *p, Window button, buttonState state = NORMAL )
{
    XWindowAttributes attr;
    // fetch window size
    XGetWindowAttributes( dpy, button, &attr );
    if( state == NORMAL )
        p->setForeground( "gray" );
    else
        p->setForeground( "darkgray" );

    p->drawRectangle( button, 0, 0, attr.width, attr.height, true );

    p->setForeground( "red" );
    p->drawRectangle( button, 0, 0, attr.width -1, attr.height -1, false );

    // draw the button text
    const char *text = "Quit";

    // fetch the Font from the painter
    const FFont *font = p->font();
    // fetch the height of the font, and the width of the text.
    int fontHeight = font->fontHeight(); // max height above and below baseline
    int textWidth = font->textWidth( text );
    
    // center the text at the top of the window
    p->setForeground( "black" );
    if( state == PRESSED )
        p->drawText( button, 2 + (attr.width - textWidth)/2, 2 + (attr.height + fontHeight)/2, text );
    else
        p->drawText( button, (attr.width - textWidth)/2, (attr.height + fontHeight)/2, text );
        
}

I'm not going to explain exactly what we're doing here since it the function is quite straight forward, and this functionality was covered in the last tutorial. It is worth noting however that we use XGetWindowAttributes to fetch the size of the button before painting it. This is of course not optimal if you have many buttons to paint since asking the server for this information can be slow. The solution for a large scale application is to have a local cache of the button sizes which you update when there are changes in the layout.

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