XmTextInsert() to add the dashes when appropriate. The problem with this approach is that XmTextInsert()
activates the XmNmodifyVerifyCallback function again, so the dash would be subject to the input filtering.
As a result, the only way to handle the situation is to actually add the dashes in the
XmN-modifyVerifyCallback routine at the same time the digits are added. This approach involves modifying
the ptr and length fields of the XmTextBlock structure in the XmTextVerifyCallbackStruct. The
check_phone() routine checks the current length of the phone number. If it is either two or six characters long, the
routine reallocates ptr to hold two characters, adds the dash, and increments length to account for the dash.
When the Text widget adds the digit and the dash, it positions the insertion cursor at the end of the new text. Prior to
Motif 1.2, the position of the insertion cursor was not affected by the amount of text that was added. The workaround
to this problem was to use the XmNvalueChangedCallback and call XmTextSetInsertionPosition().
Although we haven't demonstrated its use, the XmNvalueChangedCallback is useful when you need to keep
track of the changes in a Text widget, but you don't need to monitor or change the input before it is displayed. This
callback is invoked after the text has been modified in any way, which means that it is called for each insertion and
deletion. The call_data parameter to the routine is of type XmAnyCallbackStruct; the reason field is
always XmCR_VALUE_CHANGED.
The check_phone() routine is fairly simple, in that it only allows text insertions and deletions that occur when the
insertion cursor is at the end of the text. While it is possible to handle modifications in the middle of the text, the code
quickly becomes a large bowl of spaghetti. We do not allow clipboard copies of more than one character at a time for
the same reason. Our routine is sufficient for demonstration purposes, but for a real application, you should handle
these cases.
15.5.3 The Cursor Movement Callback
The XmNmotionVerifyCallback can be used to monitor the position of the insertion cursor. This callback is
invoked when the user moves the location cursor using the mouse or the arrow keys, when the user drags the mouse or
multi−clicks to extend the text selection, or when the application calls a Text widget function that moves the cursor or
adds, deletes, or replaces text. However, if the cursor does not move as a result of a function being called, the callback
is not invoked. The XmNmotionVerifyCallback allows an application to intercept and prevent cursor
movement.
The XmNmotionVerifyCallback uses the XmTextVerifyCallbackStruct as its callback structure, just
like the XmNmodifyVerifyCallback. However, for motion callbacks, the reason is
XmCR_MOVING_INSERT_CURSOR and the startPos, endPos, and text fields are invalid. The doit field can
be set to False to reject requests to reposition the insertion cursor.
If the cursor motion occurs as a result of a user action, the event field should point to an XEvent structure
describing the action that caused the cursor position to be modified, When the cursor moves as a result of an
application action, the field should be set to NULL. However, the event field is currently set to NULL regardless of
what caused the cursor motion. This bug makes it impossible to tell the difference between a cursor motion performed
by the user and one caused by the application.
We can use the XmNmotionVerifyCallback to tie up a loose end in prompt_phone.c. To make the text
verification simpler, we don't want to allow the user to move the insertion cursor except by entering digits or
backspacing. the source code shows a new version of the check_phone() routine that prevents cursor movement.
main(argc, argv)
int argc;
char *argv[];
15 Text Widgets 15.5.3 The Cursor Movement Callback
423
{
Widget text_w;
...
XtAddCallback (text_w, XmNmodifyVerifyCallback, check_phone, NULL);
...
}
/* check_phone() −− handle phone number input. */
void
check_phone(text_w, client_data, call_data)
Widget text_w;
XtPointer client_data;
XtPointer call_data;
{
char c;
int len = XmTextGetLastPosition (text_w);
XmTextVerifyCallbackStruct *cbs =
(XmTextVerifyCallbackStruct *) call_data;
if (cbs−>reason == XmCR_MOVING_INSERT_CURSOR) {
if (cbs−>newInsert != len)
cbs−>doit = False;
return;
}
/* no backspacing, typing or stuffing in middle of string */
if (cbs−>currInsert < len) {
cbs−>doit = False;
return;
}
if (cbs−>text−>length == 0) { /* backspace */
if (cbs−>startPos == 3 || cbs−>startPos == 7)
cbs−>startPos−−; /* delete the hyphen too */
return;
}
if (cbs−>text−>length > 1) { /* don't allow clipboard copies */
cbs−>doit = False;
return;
}
/* don't allow non−digits or let the input exceed 12 chars */
if (!isdigit (c = cbs−>text−>ptr[0]) || len >= 12)
cbs−>doit = False;
else if (len == 2 || len == 6) {
cbs−>text−>ptr = XtRealloc (cbs−>text−>ptr, 2);
cbs−>text−>length = 2;
cbs−>text−>ptr[0] = c;
cbs−>text−>ptr[1] = '−';
}
}
We check the value of newInsert against the length of the current string to determine whether or not the intended
cursor position is at the end of the text string. If it is not, we set doit to False to prevent the cursor movement. The
XmNmotionVerifyCallback function can also be used to monitor pointer dragging for text selections.
15 Text Widgets 15.5.3 The Cursor Movement Callback
424
Get Volume 6A: Motif Programming Manual now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.