Search the FAQ Archives

3 - A - B - C - D - E - F - G - H - I - J - K - L - M
N - O - P - Q - R - S - T - U - V - W - X - Y - Z
faqs.org - Internet FAQ Archives

Motif FAQ (Part 4 of 9)
Section - 93) How can I replace characters typed with say a `*'? I want to

( Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Part8 - Part9 - Single Page )
[ Usenet FAQs | Web FAQs | Documents | RFC Index | Cities ]


Top Document: Motif FAQ (Part 4 of 9)
Previous Document: 92) How can I stop the text widget from echoing characters typed? I
Next Document: 94) How can I make a text widget insensitive without graying out the
See reader questions & answers on this topic! - Help others by sharing your knowledge
replace input for password entry.
[Last modified: Nov 96]

Answer: The solution involves the use of XmNmodifyVerifyCallback and then
examining the XmTextVerifyCallbackStruct.  The following program from Dan
Heller and Paula Ferguson illustrates this:

/* Written by Dan Heller and Paula Ferguson.
* Copyright 1994, O'Reilly & Associates, Inc.
* Permission to use, copy, and modify this program without
* restriction is hereby granted, as long as this copyright
* notice appears in each copy of the program source code.
* This program is freely distributable without licensing fees and
* is provided without guarantee or warrantee expressed or implied.
* This program is -not- in the public domain.
*
* 9Sept1996 RAF -- this is a working version, at least under Sol2.4,
* using gcc.  I only modified check_passwd().
*
*/

/* password.c -- prompt for a password. All input looks like
* a series of *'s.  Store the actual data typed by the user in
* an internal variable.  Don't allow paste operations.  Handle
* backspacing by deleting all text from insertion point to the
* end of text.
*/
#include <Xm/Text.h>
#include <Xm/LabelG.h>
#include <Xm/RowColumn.h>
#include <ctype.h>

void check_passwd();
char *passwd; /* store user-typed passwd here. */

main(argc, argv)
int argc;
char *argv[];
{
Widget        toplevel, text_w, rowcol;
XtAppContext  app;

XtSetLanguageProc (NULL, NULL, NULL);

toplevel = XtVaAppInitialize (&app, "Demos",
NULL, 0, &argc, argv, NULL, NULL);

rowcol = XtVaCreateWidget ("rowcol",
xmRowColumnWidgetClass, toplevel,
XmNorientation, XmHORIZONTAL,
NULL);

XtVaCreateManagedWidget ("Password:",
xmLabelGadgetClass, rowcol, NULL);
text_w = XtVaCreateManagedWidget ("text_w",
xmTextWidgetClass, rowcol, NULL);

XtAddCallback(text_w, XmNmodifyVerifyCallback, check_passwd, NULL);
XtAddCallback(text_w, XmNactivateCallback, check_passwd, NULL);

XtManageChild (rowcol);
XtRealizeWidget (toplevel);
XtAppMainLoop (app);
}

/* check_passwd() -- handle the input of a password. */
void
check_passwd(text_w, client_data, call_data)
Widget        text_w;
XtPointer     client_data;
XtPointer     call_data;
{
char *new;
char *oldpasswd;
int len;
XmTextVerifyCallbackStruct *cbs =
(XmTextVerifyCallbackStruct *) call_data;

if (cbs->reason == XmCR_ACTIVATE) {
printf ("4assword: %s0, passwd);
return;
}

if( cbs->text->length > 1 ) {
cbs->doit = False; /* don't allow paste operations; make the */
return;            /* user type the password! */
}

if( cbs->startPos < cbs->endPos ) {
/* shrink the password by moving endPos... characters forward to
/* the point of deletion */
memmove( &(passwd[cbs->startPos]),
         &(passwd[cbs->endPos]),
         strlen(passwd) - cbs->endPos + 1 );
if( cbs->text->length == 0 ) /* then just a delete, not a replace */
    return;
}

new = XtMalloc( cbs->text->length + 1 );
strncpy( new, cbs->text->ptr, cbs->text->length ); /* just the new chars */
new[cbs->text->length]=' ';

if( passwd ) {
/* open a hole for the new characters, and insert them
/* in the proper place */
passwd = XtRealloc( passwd, strlen(passwd) + cbs->text->length + 1 );
memmove( &(passwd[cbs->startPos + cbs->text->length]),
         &(passwd[cbs->startPos]),
         strlen(passwd) - cbs->startPos + 1 );
memcpy( &(passwd[cbs->startPos]), new, cbs->text->length );
} else {
passwd = new;
}

memset( cbs->text->ptr, '*', cbs->text->length ); /* makes it all stars */

}


Thanks to OM1_JDA@pki-nbg.philips.de (Joerg Danne) for alerting me to updating
this code sample. Thanks also to Russell Fink (rfink@polaris.umuc.edu) for
providing the Nov. 1996 code update.

User Contributions:

Comment about this article, ask questions, or add new information about this topic:




Top Document: Motif FAQ (Part 4 of 9)
Previous Document: 92) How can I stop the text widget from echoing characters typed? I
Next Document: 94) How can I make a text widget insensitive without graying out the

Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Part8 - Part9 - Single Page

[ Usenet FAQs | Web FAQs | Documents | RFC Index ]

Send corrections/additions to the FAQ Maintainer:
kenton@rahul.net (Ken Lee)





Last Update March 27 2014 @ 02:11 PM