18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

C++ APPLICATION PROGRAMMING INTERFACE<br />

Step 3: Manage the widgets<br />

For the child to appear in the application, it must be managed. A single call,<br />

XtManageChild, will do this for you, placing control of the widget in its parent’s<br />

hands. This must be done for each widget. Place this line of code after the call to<br />

XmStringFree, as shown.<br />

XmStringFree(label);<br />

XtManageChild(button);<br />

Note: Even if a child is managed, it will not appear if its parent is not managed.<br />

Step 4: Implement event listening and callback functions for widgets<br />

You now have a button, but for that to be useful you must hook it to some<br />

functionality. Widgets are attached to behavior at certain events through special<br />

callback functions. You can add callbacks to a widget after it is created and either<br />

before or after it is managed. As done in most of the C++ samples for Motif,<br />

here you add the callback before it is managed.<br />

XmStringFree(label);<br />

XtAddCallback(button, XmNactivateCallback, ClickCallback, NULL);<br />

XtManageChild(button);<br />

These parameters have the following roles:<br />

• button—the widget to add the callback to.<br />

• XmNactivateCallback—the callback resource, defined by Motif to correspond<br />

to certain events. Here you will pick activation of the button. For other<br />

options, see the Motif reference at the end of this topic.<br />

• ClickCallback—pointer to the function to call on the event.<br />

• NULL—Client data to pass into the callback function. Here there is no data<br />

the function will need, so NULL is passed.<br />

Callback functions<br />

For the callback to work, you must implement the function that is being called<br />

on the event. Callbacks have a specific function signature, as follows:<br />

Make sure that the data passed to client_data<br />

will be in scope later when the callback routine<br />

is executed.<br />

void myCallbackName(Widget w, XtPointer client_data, XtPointer call_data)<br />

The parameters are:<br />

• w—the widget that was activated for this callback to be called.<br />

• client_data—any data passed into the function, as indicated in the last parameter<br />

of XtAddCallback.<br />

• call_data—a structure containing data specific to the type of widget with<br />

which the callback is associated.<br />

For this example, place this function after main in pbExample.cpp, and have it<br />

print to cerr the number of repeated clicks on this button (for example, “2<br />

clicks” if double-clicked). Remember to also place a forward declaration of it<br />

before main.<br />

Chapter 4 • <strong>Developer</strong> environments • 237

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!