[comp.windows.x] Dynamic Creating and Destroying Athena Widgets - Problem?

stung@iuvax.cs.indiana.edu (Sho-Huan Simon Tung) (08/15/89)

/* The following is a program which should allow widgets to be created
   and destroyed dynamically. However, if the user try to create
   new widgets after destroying old ones the program crash with
   a segmentation fault in `VPaned`Resize(). I don't know how to fix
   the problem. Are there anyone who might know the solution?
   Any help will be greatly appreciated.

   Sho-Huan Simon Tung                   stung@iuvax.cs.indiana.edu
   Department of computer Science
   Indiana University
*/
#include <stdio.h>
#include <X11/Intrinsic.h>
#include <X11/Box.h>
#include <X11/Command.h>
#include <X11/Form.h>
#include <X11/VPaned.h>
#include <X11/Viewport.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>

int x, y;

XtCallbackProc DoClose(w, client_data, call_data)
    Widget w;
    caddr_t client_data ;
    caddr_t call_data ;
{
    XtDestroyWidget(client_data);
    }

XtCallbackProc DoNewWC(w, client_data, call_data)
    Widget w;
    caddr_t client_data ;
    caddr_t call_data ;
{
    Widget new_widget, box0,box1,label;
    int n;
    Arg args[5];

    n =0;
    x = x + 93; y = y + 45;
    XtSetArg(args[n], XtNhorizDistance, (XtArgVal)x); n++;
    XtSetArg(args[n], XtNvertDistance, (XtArgVal)y); n++;
    XtSetArg(args[n], XtNwidth, (XtArgVal)90); n++;
    XtSetArg(args[n], XtNheight, (XtArgVal)43); n++;

    box0 = XtCreateManagedWidget("box", vPanedWidgetClass,client_data,args,n);
    label = XtCreateManagedWidget("A widget", labelWidgetClass,box0,NULL,0);
    box1 = XtCreateManagedWidget("box", boxWidgetClass,box0,NULL,0);
    new_widget = XtCreateManagedWidget("Close",
				     commandWidgetClass, box1,NULL,0);
    XtAddCallback(new_widget, XtNcallback, DoClose, box0);    
    XtRealizeWidget(client_data);
}

void main(argc, argv)
    unsigned int argc;
    char **argv;
{

    Arg args[9] ;
    int n ;
    Widget toplevel, outer_vpaned_widget, outer_command_line, outer_form,
           outer_viewport, new_button;

    x = y = 0;
    toplevel = XtInitialize ("test1", "Test1", NULL, 0, &argc, argv);
    outer_vpaned_widget = XtCreateManagedWidget( "outer_vpaned", 
						vPanedWidgetClass, 
						toplevel, NULL, 0);
    outer_command_line = XtCreateManagedWidget("outer_command_line",
					       boxWidgetClass,
					       outer_vpaned_widget, NULL, 0);

    n=0;
    XtSetArg(args[n], XtNallowHoriz, (XtArgVal)True); n++;
    XtSetArg(args[n], XtNallowVert, (XtArgVal)True); n++;
    XtSetArg(args[n], XtNforceBars, (XtArgVal)True); n++;
    outer_viewport = XtCreateManagedWidget("outer_viewport", 
					   viewportWidgetClass,
					   outer_vpaned_widget,  args, n);
    n=0;
    XtSetArg(args[n],XtNwidth,(XtArgVal)100); n++;
    XtSetArg(args[n],XtNheight, (XtArgVal)100); n++;
    outer_form = XtCreateManagedWidget("outer_form",
				      formWidgetClass, outer_viewport,args,n);
    new_button = XtCreateManagedWidget("New", commandWidgetClass, 
					    outer_command_line, NULL, 0);
    XtAddCallback(new_button, XtNcallback, DoNewWC, outer_form);

    XtRealizeWidget(toplevel);
    XtMainLoop() ;
}