Complete Example with Motif

Motif is chosen only as an example of an X-based toolkit. A better way to integrate Views with Motif is to use the standard Views library libmviews that already does the integration for you. The following example is just meant to illustrate what would need to be done if libmviews was not available (samples/xlib/ilvmotif.cc):

// -------------------------------------------------------------

// Integration of Views, pure XLib version into a Motif

// application

// -------------------------------------------------------------

#include <ilviews/contain.h>

#include <ilviews/label.h>

 

#include <X11/Intrinsic.h>

#include <Xm/Xm.h>

#include <Xm/DrawingA.h>

#include <X11/StringDefs.h>

 

// Define the default input mask for the window

#define INPUT_MASK (unsigned long)(ButtonPressMask | \

ButtonReleaseMask | \

KeyPressMask | \

KeyReleaseMask | \

ButtonMotionMask | \

EnterWindowMask | \

LeaveWindowMask | \

PointerMotionMask | \

ExposureMask | \

StructureNotifyMask)

 

// -------------------------------------------------------------

// This will be called by Xt when events of any of the

// types specified in INPUT_MASK occur.

// To do this, we call upon the XtAddEventHandler function call

// (see main()).

// -------------------------------------------------------------

 

static void

ManageInput(Widget, XtPointer view, XEvent* xevent, Boolean*)

{

IlvEventLoop::getEventLoop()->dispatchEvent(xevent);

}

// -------------------------------------------------------------

void

main(int argc,char** argv)

{

// Initialize X Window:

Widget toplevel = XtInitialize("", "IlvXlib", NULL, NULL,

// XtInitialize has a new specific signature in X11r5

#if defined(XlibSpecificationRelease) && (XlibSpecificationRelease >= 5)

&argc,

#else

(Cardinal*)&argc,

#endif

argv);

// If the top shell couldn’t be created, exit

if (!toplevel)

exit(1);

 

// Create a Motif widget to draw to

Widget drawArea = XtVaCreateManagedWidget("ilvview",

xmDrawingAreaWidgetClass,

(Widget)toplevel,

XtNwidth, 400,

XtNheight, 400,

0);

XtRealizeWidget(toplevel);

 

// Create an IlvDisplay instance from the existing Display

IlvDisplay* display = new IlvDisplay(XtDisplay(drawArea), "Views");

 

// Create a container associated with the drawing area:

IlvContainer* container =

new IlvContainer(display, (IlvSystemView)XtWindow(drawArea));

 

// Create a graphic object in the container

container->addObject(new IlvLabel(display,

IlvPoint(30, 30),

"an IlvLabel instance"));

// Let Views know about the events

XtAddEventHandler(drawArea, INPUT_MASK, IlFalse, ManageInput, NULL);

 

// Wait for events to occur

XtMainLoop();

}

The directory samples/foundation/xlib/ contains more examples applying to various toolkits: ilvmotif.cpp is another integration with Motif, similar to this one, and ilvxview.cpp an integration with XView.