LINUX - XT PROGRAM EXAMPLE 2

This example adds to XT Example 1 by adding a Mouse Pushbutton event handler. Pressing on any mouse button will display a message on the xterm window where you invoked this program. Pressing the right mouse button (Button 3) will also exit the program.

New concepts include:

Events - all procedures are called by XWindows events. The only event used by this example is
ButtonPress - received whenever a mouse button is pressed
Event Handlers - each desired event is assigned an event handler via the XtAddEventHandler function.
The XEvent structure is actually a union of many structures, one for each type of event. For example, the XButtonEvent xbutton structure is used for button events and includes information on which button was pressed (xbutton.button), mouse coordinates when pressed (xbutton.x, .y), time event occurred, etc. (See structures in Word Document, below)

Go here to get some simplified X Window and X Toolkit Intrinsics Documentation on calls used in this example


Create a file named "demo_xt2.cpp" with the contents shown in the box (the changes from demo_xt1.cpp are highlighted):
#include <X11/IntrinsicP.h> // IntrinsicP.h has some faster macros than Intrinsic.h
#include <X11/ShellP.h> // Needed for Widget class definitions
#include <stdio.h> //fprintf, stderr
#include < stdlib.h > // exit

XtAppContext app;
Widget main_widget; //Main Window
//------------------------------------------------
// This function is called by a ButtonPress event in the main widget
// Button 1 is the left mouse button, Button 3 is the right button,
// Button 2 is the middle button (or the left & right button pressed together)
// Button 3 quits the program, Buttons 2 & 3 change the main widget's size
void EvProc(Widget w, XtPointer client_data, XEvent *event, char* CTmp)
{
if (event->xbutton.button == Button1)
{
fprintf(stderr, "Button 1 Pressed\n");
XtMakeResizeRequest(main_widget, 200, 600, NULL, NULL);
}
if (event->xbutton.button == Button2)
{
fprintf(stderr, "Button 2 Pressed\n");
XtMakeResizeRequest(main_widget, 400, 100, NULL, NULL);
}
if (event->xbutton.button == Button3)
{
fprintf(stderr, "Button 3 Pressed\n");
exit(0);
}
}

//------------------------------------------------
void main (int argc, char **argv)
{
short unsigned int xx,yy;
//--- Create and initialize the top-level widget
main_widget = XtOpenApplication(&app, "My Application", NULL, 0, &argc, argv, NULL, applicationShellWidgetClass, NULL, 0);
//--- Make it the desired size
XtMakeResizeRequest(main_widget, 300, 100, &xx, &yy);
//--- Realize the main widget
XtRealizeWidget(main_widget);

//--- Add Mouse Button Event Handler
XtAddEventHandler(main_widget, ButtonPressMask, FALSE, EvProc, NULL);
//-----
XtAppMainLoop(app);

}


Create a file named "Makefile" with the contents shown in the box :

cc=g++
cflags=-I/usr/X11R6/include
lflags=-L/usr/X11R6/lib #
# Linking
#
demo_xt2: <tab> demo_xt2.o
<tab> $(cc) -o demo_xt2 demo_xt2.o $(lflags) -lX11 -lXt
#
# Compiling
#
demo_xt2.o: <tab> demo_xt2.cpp
<tab> $(cc) -c $(cflags) demo_xt2.cpp
#
# clean
#
clean:
<tab> rm -rf *.o
Whenever you see <tab>, hit the TAB key.
The # symbol is used to denote a comment line and is not used for anything

The Makefile is not that useful yet, but becomes more useful when there are several .CPP files that all need to be compiled and linked together.

To compile and link this program, type
make
This will compile and link demo_xt2.cpp into the executable file demo_xt2
Now type
demo_xt2 (you may have to enter ./demo_xt2)
and a window should now appear.
Clicking on any of the three mouse buttons will display a message in the XTerm window. Pressing the right mouse button will also terminate the program.