From: mark@BHANU.ANDYNE.ON.CA (Mark Levison)
Newsgroups: comp.windows.x.motif
Subject: (none)
Date: 8 Apr 92 23:56:07 GMT
Distribution: inet
Organization: The Internet

Subject: text widgets, focus and selections


	I am trying to setup a group of text widgets so that widget when
the user focuses on the widget the text is automatically selected for
replacement.  The example below works as long as the user tabs from widget
to widget.  But if they mouse into a widget it gains the selection but then
loses immeadiately (sp?) with a loss of selection callback.  Any suggestions
would be welcomed.

Cheers
Mark Levison
mark@bhanu.andyne.on.ca

-----------------------------------Sample Code Cut Here----------------
#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <X11/MwmUtil.h>

#include <Xm/Xm.h>
#include <Xm/DrawingA.h>
#include <Xm/MainW.h>
#include <Xm/Text.h>

#include <stdlib.h>
#include <stdio.h>

#define MAX_TEXT 4
#define DRAW_WIDTH	500
#define DRAW_HEIGHT 800

Display        *display;
XtAppContext    appc;
Widget          toplevel, main_window;
short			loop;
Boolean			done;
Widget			text[MAX_TEXT];

static void atomTextGainFocus(Widget, caddr_t, XmAnyCallbackStruct*);
static void atomTextEverything(Widget, caddr_t, XmAnyCallbackStruct*);

static void
atomTextGainFocus(Widget parent, caddr_t client_data, XmAnyCallbackStruct* call_info)
{
    Time            curr_time;
    XEvent          event;
    XmTextPosition  last;

    printf("atomTextGainFocus attempting to get selection\n");
    curr_time = XtLastTimestampProcessed(XtDisplay(parent));
    last = XmTextGetLastPosition(parent);

    printf("time %d\n", curr_time);
    XmTextSetSelection(parent, 0, last, curr_time);
    {
      XmTextPosition    a, b;

      if (XmTextGetSelectionPosition(parent, &a, &b) == False)
        printf("do not have selection\n");
      else
        printf("have selection\n");
    }
}

static void
atomTextEverything(Widget parent, caddr_t client_data, XmAnyCallbackStruct* call_info)
{
    printf("text: %s :", XmTextGetString(parent));
    if (call_info -> reason == XmCR_VALUE_CHANGED)
      printf("value changed callback\n");
    else if (call_info -> reason == XmCR_ACTIVATE)
      printf("activate callback\n");
    else if (call_info -> reason == XmCR_FOCUS)
      printf("focus gained\n");
    else if (call_info -> reason == XmCR_LOSING_FOCUS)
      printf("focus lost\n");
    else if (call_info -> reason == XmCR_MODIFYING_TEXT_VALUE)
      printf("modifying text\n");
    else if (call_info -> reason == XmCR_MOVING_INSERT_CURSOR)
      printf("moving insert cursor\n");
    else if (call_info -> reason == XmCR_GAIN_PRIMARY)
      printf("primary selection gained\n");
    else if (call_info -> reason == XmCR_LOSE_PRIMARY)
      printf("primary selection lost\n");
}
void main(argc,argv)
unsigned int    argc;
char        **argv;
{
	char	buffer[100];
	Widget  drawing_area;
	Position	x, y;
	short	ac;
	Arg		al[20];

    /* init toolkit */
    XtToolkitInitialize();
    appc = XtCreateApplicationContext();
    display = XtOpenDisplay (
                  appc,          /* application context = default */
                  NULL,        /* use DISPLAY from environment  */
                  NULL,        /* use last of argv[0] as name   */
                  NULL,
                  NULL,        /* no additional cmd line options*/
                  0,           /* ditto                         */
                  &argc,
                  argv);       /* use and delete std options    */

    if (display == NULL)       /* necessary !                   */
        XtErrorMsg (
            "NErrCannotOpen", NULL,    /* resource name         */
            NULL,                      /* resource class        */
            "cannot open display",     /* default message       */
            NULL, NULL);               /* parameters            */

    /* create application shell */
    ac = 0;
    XtSetArg(al[ac], XmNheight, 200); ac++;
    XtSetArg(al[ac], XmNwidth, 512); ac++;
    toplevel = XtAppCreateShell (
                   NULL,               /* use same program name */
                   NULL,               /* repeat class param    */
                   applicationShellWidgetClass,
                   display,
                   al, ac);           /* argument list         */

    /* create main window */
	ac = 0;
	XtSetArg(al[ac], XmNvisualPolicy, XmCONSTANT); ac++;
	XtSetArg(al[ac], XmNscrollBarDisplayPolicy, XmSTATIC); ac++;
	XtSetArg(al[ac], XmNscrollingPolicy, XmAUTOMATIC); ac++;
    main_window = XmCreateMainWindow(toplevel, "main", al, ac);
    XtManageChild(main_window);
	
	ac = 0;
	XtSetArg(al[ac], XmNwidth, DRAW_WIDTH); ac++;
	XtSetArg(al[ac], XmNheight, DRAW_HEIGHT); ac++;
	drawing_area = XmCreateDrawingArea(main_window, " ", al, ac);
	
	x = 10; y = 10;

	for(loop = 0; loop < MAX_TEXT; loop++)
	{
	  ac = 0;
	  XtSetArg(al[ac], XmNx, x); ac++;
	  XtSetArg(al[ac], XmNy, y); ac++;
	  XtSetArg(al[ac], XmNwidth, 90); ac++;
	  text[loop] = XmCreateText(drawing_area, "text", al, ac);

	  sprintf(buffer, "l %d", loop);
	  XmTextSetString(text[loop], buffer);

	  XtAddCallback(text[loop], XmNfocusCallback, atomTextGainFocus, NULL);

	  XtAddCallback(text[loop], XmNvalueChangedCallback, atomTextEverything, NULL);
      XtAddCallback(text[loop], XmNactivateCallback, atomTextEverything, NULL);
      XtAddCallback(text[loop], XmNfocusCallback, atomTextEverything, NULL);
      XtAddCallback(text[loop], XmNlosingFocusCallback, atomTextEverything, NULL);
      XtAddCallback(text[loop], XmNmodifyVerifyCallback, atomTextEverything, NULL);
      XtAddCallback(text[loop], XmNmotionVerifyCallback, atomTextEverything, NULL);
      XtAddCallback(text[loop], XmNgainPrimaryCallback, atomTextEverything, NULL);
      XtAddCallback(text[loop], XmNlosePrimaryCallback, atomTextEverything, NULL);

	  XtManageChild(text[loop]);

	  x += 110;
	}

    XtManageChild(drawing_area);

	XmMainWindowSetAreas(main_window, NULL, NULL, NULL, NULL, drawing_area);
	/* start loop */

    XtRealizeWidget (toplevel);
    XtAppMainLoop (appc);
}
Xref: sparky comp.windows.x.motif:3147 comp.windows.x:9848