/*
From:        Joey Mukherjee <studjoey@bigfoot.com>
To:          lesstif@hungry.com
Subject:     Drawnbutton problems....
Date:        Thu, 03 Dec 1998 13:39:56 -0600

Ok, I know I've whined about drawn buttons before, but this time I have
a simple way of making it fail.

If you change test5 in test/Xm/drawnbutton by adding the following three
lines at line 114:

	XmString btn_text;
        btn_text = XmStringCreateSimple ("Drawn Button");
        XtSetArg(al[ac], XmNlabelString, btn_text); ac++;
        XtSetArg(al[ac], XmNalignment, XmALIGNMENT_CENTER); ac++;

You'll see the text does not show up; however, under the real Motif it
does.

For some reason under the real Motif you must also set XmNalignment
in the resize/expose widget or else you will get the pixmap on top of
the text.  What I do is do a :

    XtVaSetValues (w,
        XmNalignment, XmALIGNMENT_END,
        NULL);

in my resize/expose widget.  After that, the pixmap is to the far left
and the text is to the right.  On LessTif, this will create the
"nervous" drawn button where it will continually resize/expose itself. 
Adding that line to line 58 of test5.c shows the problem.  

I've attached my test case to this message.  If you reply, I'd really
appreciate a carbon copy of the email.  I'm not subscribed to the list
anymore since I'm leaving for a while, but would love to know when/if
this is resolved.

Cheers!
Joey (joey@swri.org)
*/

/*
** Generated by X-Designer 
*/
/*
**LIBS: -lXm -lXt -lX11
*/

#include <X11/Xatom.h>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>

#include <Xm/Xm.h>
#include <Xm/DialogS.h>
#include <Xm/DrawnB.h>
#include <stdio.h>

extern void button_resize ();
extern void button_expose ();
extern Boolean aardvarkConverter ();
extern Boolean objStringConverter ();
Widget appshell = (Widget) NULL;
Widget drawnbutton = (Widget) NULL;
void create_gc();


GC gc=0;

#define LINE_WIDTH 10

void 
button_expose(w, client_data, call_data)
	Widget          w;
	XtPointer       client_data;
	XtPointer       call_data;
{
	Dimension       width, height, shadow, highlight;
	int             origin;
	unsigned int    arc_width, arc_height;
	/* Call a routine to create a Graphics Context */
	create_gc(w);
	/* First get the various dimensions */
	XtVaGetValues(w,
		      XmNwidth, &width,
		      XmNheight, &height,
		      XmNshadowThickness, &shadow,
		      XmNhighlightThickness, &highlight, NULL);
	origin = shadow + highlight + 1 + (LINE_WIDTH / 2);
	/* Don't draw 0 or negatively sized circles. */
	if (width < origin * 2
	    || height < origin * 2)
		return;
	/* Calculate arc sizes */
	arc_width = width - origin * 2;
	arc_height = height - origin * 2;
	/* Draw the Arc */
	XDrawArc(XtDisplay(w), XtWindow(w), gc, origin, origin, arc_width,
		 arc_height, 0, 360 * 64);
    XtVaSetValues (w,
        XmNalignment, XmALIGNMENT_CENTER,
        NULL);
}

void 
button_resize(w, client_data, call_data)
	Widget          w;
	XtPointer       client_data;
	XtPointer       call_data;
{
	XClearArea(XtDisplay(w), XtWindow(w), 0, 0, 0, 0, True);
}

void
create_gc(w)
	Widget          w;
{
	XGCValues       values;
	XColor          screen_def, exact_def;
	Display        *display = XtDisplay(w);
	Colormap        cmap = XDefaultColormapOfScreen(XtScreen(w));
	int             mask = 0;

	if (gc != 0)
		return;
	/* Allocate read-only colour cell for colour `red' */
	if (XAllocNamedColor(display, cmap, "red", &screen_def, &exact_def)
	    != 0) {
		/*
		 * Put the pixel value for red into the GC, ready for drawing
		 * operations
		 */
		values.foreground = screen_def.pixel;
		mask = GCForeground;
	}
	values.line_width=LINE_WIDTH;
	mask|=GCLineWidth;
	gc = XCreateGC(display, XtWindow(w), mask, &values);
}

void create_appshell (display, app_name, app_argc, app_argv)
Display *display;
char *app_name;
int app_argc;
char **app_argv;
{
	Arg al[64];                    /* Arg List */
	register int ac = 0;           /* Arg Count */
        XmString btn_text;

	XtSetArg(al[ac], XmNallowShellResize, TRUE); ac++;
	XtSetArg(al[ac], XmNtitle, "Drawn Button"); ac++;
	XtSetArg(al[ac], XmNargc, app_argc); ac++;
	XtSetArg(al[ac], XmNargv, app_argv); ac++;
	appshell = XtAppCreateShell ( app_name, "XApplication", applicationShellWidgetClass, display, al, ac );
	ac = 0;
	XtSetArg(al[ac], XmNwidth, 200); ac++;
	XtSetArg(al[ac], XmNheight, 200); ac++;
        btn_text = XmStringCreateSimple ("Drawn Button");
	XtSetArg(al[ac], XmNlabelString, btn_text); ac++;
        XtSetArg(al[ac], XmNalignment, XmALIGNMENT_CENTER); ac++;
	drawnbutton = XmCreateDrawnButton ( appshell, "drawnbutton", al, ac );
	ac = 0;
	XtAddCallback (drawnbutton, XmNresizeCallback, button_resize,NULL);
	XtAddCallback (drawnbutton, XmNexposeCallback, button_expose,NULL);
	XtManageChild ( drawnbutton);
}



XtAppContext app_context;
Display *display;       /*  Display             */

int main (argc,argv)
int    argc;
char            **argv;
{
	XtSetLanguageProc ( (XtAppContext) NULL, (XtLanguageProc) NULL, (XtPointer) NULL );
	XtToolkitInitialize ();
	app_context = XtCreateApplicationContext ();
	display = XtOpenDisplay (app_context, NULL, argv[0], "XApplication",
	                         NULL, 0, &argc, argv);
	if (!display)
	{
	    printf("%s: can't open display, exiting...\n", argv[0]);
	    exit (-1);
	}
	create_appshell ( display, argv[0], argc, argv );
	XtRealizeWidget (appshell);


/* Note: the following values are the result of
 * querying the current geometry.
 */
{
static XtWidgetGeometry Expected[] = {
   CWWidth | CWHeight            ,    0,    0,  200,  200, 0,0,0, /* Button1 */
};
/* toplevel should be replaced with to correct applicationShell */
PrintDetails(appshell, Expected);
}
LessTifTestMainLoop(appshell);
	exit (0);
}

