#include #ifndef MAX_FIELD_LENGTH #define MAX_FIELD_LENGTH 8 #endif /* MAX_FIELD_LENGTH */ void NextFieldAfterEightCharactersCallback(Widget w, XtPointer client_data, XtPointer xt_call_data) { XmTextVerifyCallbackStruct *call_data = (XmTextVerifyCallbackStruct *) xt_call_data ; if (call_data != (XmTextVerifyCallbackStruct *) 0) { if (call_data->text->length == 0) { /* Deletion - allow this */ } else { /* ** A simple check of current_length + call_data->text->length >= MAX_FIELD_LENGTH ** is wrong, because if the user is typing over selected text, the overall length ** can in fact decrease as the new text replaces the old selection. */ /* This returns the length of text before insertion */ XmTextPosition current_length = XmTextGetLastPosition(w) ; /* The length of data to insert */ int insertion_length = call_data->text->length ; /* A selection change will have end > start */ long change_length = call_data->endPos - call_data->startPos ; /* The final new length of the text after insertion */ long new_length = (current_length + insertion_length - change_length) ; if (new_length == MAX_FIELD_LENGTH) { XmProcessTraversal(w, XmTRAVERSE_NEXT_TAB_GROUP) ; } else if (new_length > MAX_FIELD_LENGTH) { /* This can happen because of mouse paste */ /* It can also happen because of a rogue programmatic XmTextSetString, */ /* or XtVaSetValues(w, XmNvalue, ...) which apply too much data */ call_data->doit = False ; } } } }