[comp.windows.x] XtManageChild/XtUnmanageChild problem?

zebulon@uwocsd.uwo.ca (Scott Feeney) (07/18/89)

Environment: Sun 3/60, SunOS4.0, X11R3, gcc 1.35, Purdue2.1, 
	     twm, Athena Widgets

Answer #1: I've found a bug.

Answer #2: I've made a stupid error somewhere.

Personally, I'd _like_ to subscribe to #1 but it wouldn't be the first time 
for #2. 8^)

The following program is a small part of an application I'm working on.
Pressing the Copy command widget will cause 2 label widgets to be unmanaged
and a label widget and an asciiString widget to be managed in their places. At
this point, pressing the Cancel command widget will unmanage the label and
asciiString widgets and manage the original 2 label widgets. 

The Problem: One of the original label widgets has an empty string for a
label and its width is defined by XtNwidth. When originally realized, it is
mapped with the value of XtNwidth as its width. When it is remanaged the first
time, it is mapped with the width of an empty string. By pressing the Copy 
and Cancel command widgets again, the label is mapped with the value of
XtNwidth as its width. The width of this label widget will toggle back and
forth like this until you get fed up and press the Quit command widget.

Anybody know what's going on here? TIA.

- scott

---
Scott Feeney,  Research Associate,  Department of Computer Science
The University of Western Ontario,  London Canada, N6A 5B7
zebulon@uwocsd.uwo.ca, zebulon@uwocsd.UUCP
---

#include <stdio.h>
#include <X11/Xatom.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/AsciiText.h>
#include <X11/Command.h>
#include <X11/Label.h>
#include <X11/Form.h>

#include <X11/Cardinals.h>

Widget file_button[5], file_string[2];

static XrmOptionDescRec options[] = {
{"-label",      XtNlabel,       XrmoptionSepArg,        NULL}
};


/* ARGSUSED */
Syntax(call)
        char *call;
{
}

/* ARGSUSED */
void Quit(widget,closure,callData)
    Widget widget;
    caddr_t closure;            /* Widget */
    caddr_t callData;
{
    XtDestroyWidget((Widget)closure);
}

/* ARGSUSED */
void File_copy(widget,closure,callData)
  Widget widget;
  caddr_t closure;            /* Widget */
  caddr_t callData;
{
  Arg arglist[3];

  XtSetArg(arglist[0], XtNsensitive, True);
  XtSetValues(file_button[1],arglist, (Cardinal) 1);
  XtUnmanageChild(file_button[2]);
  XtUnmanageChild(file_string[0]);
  XtManageChild(file_button[3]);
  XtManageChild(file_string[1]);
}

/* ARGSUSED */
void File_cancel(widget,closure,callData)
  Widget widget;
  caddr_t closure;            /* Widget */
  caddr_t callData;
{
  Arg arglist[3];

  XtSetArg(arglist[0], XtNsensitive, False);
  XtSetValues(file_button[1],arglist, (Cardinal) 1);

  if (XtIsManaged(file_button[3]))
    {
      XtUnmanageChild(file_button[3]);
      XtManageChild(file_button[2]);
      XtSetArg(arglist[0], XtNwidth, 300);
      XtSetArg(arglist[1], XtNlabel, "");
      XtSetValues(file_string[0], arglist, 2);
/* Uncommenting the next line (i.e. setting the values twice) makes
   everything OK. sheesh */
/*      XtSetValues(file_string[0], arglist, 2);*/
      XtUnmanageChild(file_string[1]);
      XtManageChild(file_string[0]);
    }
}

/* ARGSUSED */
void Finish(client_data, id)
    caddr_t      client_data;
    XtIntervalId id;
{
    XCloseDisplay((Display*)client_data);
    exit(0);
}


/* ARGSUSED */
void Destroyed(widget, closure, callData)
    Widget widget;
    caddr_t closure;            /* unused */
    caddr_t callData;           /* unused */
{
    XtAddTimeOut(1000, Finish, (caddr_t)XtDisplay(widget));
}


void main(argc, argv)
    unsigned int argc;
    char **argv;
{
    Widget toplevel, outer;
    static XtCallbackRec callback[2]; /* K&R: initialized to NULL */
    Arg arg[10];

    toplevel = XtInitialize( NULL, "Why???", options, XtNumber(options),
                             &argc, argv);

    if (argc != 1) Syntax(argv[0]);

    outer = XtCreateManagedWidget( "form", formWidgetClass, toplevel,
                                   arg, 0 );
    XtAddCallback( outer, XtNdestroyCallback, Destroyed, NULL );

    callback[0].callback = File_copy;
    callback[0].closure = NULL;
    XtSetArg(arg[0], XtNcallback, callback);
    XtSetArg(arg[1], XtNlabel, "Copy");
    file_button[0] = XtCreateManagedWidget("file_b0",
                commandWidgetClass,outer,arg,2);

    callback[0].callback = File_cancel;
    callback[0].closure = NULL;
    XtSetArg(arg[0], XtNcallback, callback);
    XtSetArg(arg[1], XtNlabel, "Cancel");
    XtSetArg(arg[2], XtNfromHoriz, file_button[0]);
    XtSetArg(arg[3], XtNsensitive, False);
    XtSetArg(arg[4], XtNhorizDistance, 100);
    file_button[1] = XtCreateManagedWidget("file_b1",
                commandWidgetClass,outer,arg,5);

    XtSetArg(arg[0], XtNlabel, "Cur File:");
    XtSetArg(arg[1], XtNfromVert, file_button[0]);
    file_button[2] = XtCreateManagedWidget("file_b2",
                commandWidgetClass,outer,arg,2);

    XtSetArg(arg[0], XtNlabel, "Copy to :");
    XtSetArg(arg[1], XtNfromVert, file_button[0]);
    file_button[3] = XtCreateManagedWidget("file_b3",
                commandWidgetClass,outer,arg,2);

    callback[0].callback = Quit;
    callback[0].closure = (caddr_t)toplevel;
    XtSetArg( arg[0], XtNcallback, callback );
    XtSetArg(arg[1], XtNfromHoriz, file_button[0]);
    XtSetArg(arg[2], XtNhorizDistance, 320);
    file_button[4] = XtCreateManagedWidget( "quit",
                commandWidgetClass, outer, arg, 3 );

    XtSetArg(arg[0], XtNfromHoriz, file_button[2]);
    XtSetArg(arg[1], XtNfromVert, file_button[0]);
    XtSetArg(arg[2], XtNlabel, "");
    XtSetArg(arg[3], XtNwidth, 300);
    file_string[0] = XtCreateManagedWidget("",
                labelWidgetClass,outer,arg,4);

    XtSetArg(arg[0], XtNfromHoriz, file_button[3]);
    XtSetArg(arg[1], XtNfromVert, file_button[0]);
    XtSetArg(arg[2], XtNeditType, XttextEdit);
    file_string[1] = XtCreateManagedWidget("file_s2",
                asciiStringWidgetClass,outer,arg,3);

    XtRealizeWidget(toplevel);
    XtMainLoop();
}

kit@EXPO.LCS.MIT.EDU (Chris D. Peterson) (07/26/89)

> The Problem: One of the original label widgets has an empty string for a
> label and its width is defined by XtNwidth. When originally realized, it is
> mapped with the value of XtNwidth as its width. When it is remanaged the first
> time, it is mapped with the width of an empty string. By pressing the Copy 
> and Cancel command widgets again, the label is mapped with the value of
> XtNwidth as its width. The width of this label widget will toggle back and
> forth like this until you get fed up and press the Quit command widget.

> Anybody know what's going on here? TIA.

Your example works correctly for me, but then I am running R3++++++.  My guess
is that you found a bug, that has since been fixed, and will go away in R4.


						Chris D. Peterson     
						MIT X Consortium 

Net:	 kit@expo.lcs.mit.edu
Phone:   (617) 253 - 9608	
Address: MIT - Room NE43-213