[ Previous | Next | Contents | Glossary | Home | Search ]
Motif 2.1 Programmer's Guide



Text Editing and Callbacks

Text has a number of callback lists for communication with the application. Text invokes callbacks whenever the widget gains or loses focus, when it gains or loses the primary selection, before the insertion cursor is moved or text is modified, and when the text string changes or the activate() action is invoked.

Text passes these callbacks a pointer to either an XmAnyCallbackStruct or an XmTextVerifyCallbackStruct (or XmTextVerifyCallbackStructWcs) structure. The two verification structures contain the current and new positions of the insertion cursor, the starting and ending positions of the text to be modified, a pointer to an XmTextBlockRec (or XmTextBlockRecWcs) structure with information about the text to be modified, and a Boolean in/out doit member that the callback procedure can set to tell the widget whether or not to go ahead with the modification.

Following is a summary of the callbacks:

XmNmotionVerifyCallback

Text invokes this list, passing a pointer to an XmTextVerifyCallbackStruct as the widget data, before moving the insertion cursor. The application can prevent the action by setting the doit member of the callback struct to False.

XmNmodifyVerifyCallback or XmNmodifyVerifyCallbackWcs

Text invokes this list, passing a pointer to an XmTextVerifyCallbackStruct structure or an XmTextVerifyCallbackStructWcs structure as the widget data, before deleting or inserting any text. The application can prevent the action by setting the doit member of the callback struct to False.

XmNvalueChangedCallback

Text invokes this list, passing a pointer to an XmAnyCallbackStruct as the widget data, after text is inserted or deleted.

XmNfocusCallback

Text invokes this list, passing a pointer to an XmAnyCallbackStruct as the widget data, when the widget gains input focus.

XmNlosingFocusCallback

Text invokes this list, passing a pointer to an XmTextVerifyCallbackStruct as the widget data, before the widget loses input focus. The application can prevent the action by setting the doit member of the callback struct to False.

XmNgainPrimaryCallback

Text invokes this list, passing a pointer to an XmAnyCallbackStruct as the widget data, when the widget gains ownership of the primary selection.

XmNlosePrimaryCallback

Text invokes this list, passing a pointer to an XmAnyCallbackStruct as the widget data, when the widget loses ownership of the primary selection.

XmNactivateCallback

Text invokes this list, passing a pointer to an XmAnyCallbackStruct as the widget data, when the activate() action is invoked. By default no translations are bound to this action, but in a single-line Text widget or a TextField widget, pressing osfActivate invokes theXmNactivateCallback callbacks.

These callbacks provide a great deal of flexibility for an application to alter the behavior of the Text widget. For example, an application can prevent text from being inserted, as when the user types a password, by using the XmNmodifyVerifyCallback or XmNmodifyVerifyCallbackWcs callbacks. The application can prevent any text from appearing by setting the doit member of the XmTextVerifyCallbackStruct (or XmTextVerifyCallbackStructWcs) to False. The application can also alter the text that will appear by creating a new text string and setting the ptr member of the XmTextBlockRec structure (or the wcsptr member of the XmTextBlockRecWcs structure) to the new string.

Following is an example of an XmNmodifyVerifyCallback that substitutes a string of characters for any text a user enters. Because the XmNmodifyVerifyCallback procedures are most commonly invoked after the user enters a character, this routine usually substitutes the replacement string for each character the user types. This example could be used with a single-line Text widget as part of a simple password-entry program. In this case, the XmNmodifyVerifyCallback procedure would need additional code to save the characters the user types, and the program would need an XmNactivateCallback procedure to check whether the saved characters match the password.

/* XmNmodifyVerifyCallback procedure that replaces text the user enters
 * with a replacement string passed in as application data. */
void ModifyVerifyCB(Widget w, XtPointer app_data,
                    XtPointer widget_data)
{
  char *replace_string = (char *) app_data;
  XmTextVerifyCallbackStruct *widget_info =
    (XmTextVerifyCallbackStruct *) widget_data;
  if (widget_info->text->length > 0) {
    widget_info->text->length = strlen(replace_string);
    widget_info->text->ptr = replace_string;
/* The widget will automatically free this ptr.  Therefore,
 * your app should make a copy of it with strcpy. */
  }
}

Text and TextField differ from most other Motif widgets in that calling some convenience routines and setting some resources causes the widget to invoke callback procedures. In general

  1. Setting resources or calling convenience routines that change the contents of the text invokes the XmNmodifyVerifyCallback and XmNmodifyVerifyCallbackWcs callbacks. If these procedures allow the text to be modified, the XmNvalueChangedCallback callbacks are invoked.

  2. Setting resources or calling convenience routines that change the position of the insertion cursor invokes the XmNmotionVerifyCallback callbacks.

  3. Setting resources or calling convenience routines that cause the widget to gain the primary selection invokes the XmNgainPrimaryCallback callbacks.

  4. Setting resources or calling convenience routines that cause the widget to lose the primary selection invokes the XmNlosePrimaryCallback callbacks.

    If the application needs to distinguish between callbacks invoked as a result of user action and callbacks invoked as a result of application action (such as setting a resource or calling a convenience routine), it needs to set a flag before taking the application action and clear the flag afterward.


  5. [ Previous | Next | Contents | Glossary | Home | Search ]