#include <stdio.h>
#include <Xmt/Xmt.h>
#include <Xmt/Layout.h>
#include <Xmt/Chooser.h>    
#include <Xmt/MsgLine.h>
#include <Xmt/Symbols.h>

extern void printf();
extern void exit();
static char cmd[50];
Widget chooser, line;


void input(Widget w, XtPointer tag, XtPointer data)
{
    printf("MsgLine input: '%s'\n", data);
    printf("MsgLine symbol: '%s'\n", cmd);
    XtVaSetValues(line, XmtNallowAsyncInput, False, NULL);
    XtSetSensitive(chooser, True);
}

void test(Widget w, XtPointer tag, XtPointer data)
{
    XmtChooserCallbackStruct *cb = (XmtChooserCallbackStruct *)data;
    String stat;
    char search[100], replace[100];
    int c;
    static int count = 0;

    switch(cb->item) {
    case 0:
	XmtMsgLineClear(line, XmtMsgLineNow);
	break;
    case 1:
	XmtMsgLineSet(line, "Testing");
	break;
    case 2:
	XmtMsgLineAppend(line, "...");
	break;
    case 3:
	XmtMsgLineClear(line, XmtMsgLineNow);
	XmtMsgLinePrintf(line, "You've pressed that button %d times.",
			 ++count);
	break;
    case 4:
	XmtMsgLineSet(line, "Search for: ");
	stat = XmtMsgLineGetString(line, search, 100);
	if (stat == NULL) {
	    XmtMsgLineClear(line, XmtMsgLineNow);
	    break;
	}
	XmtMsgLineAppend(line, " Replace with: ");
	stat = XmtMsgLineGetString(line, replace, 100);
	if (stat == NULL) {
	    XmtMsgLineClear(line, XmtMsgLineNow);
	    break;
	}
	printf("Replacing '%s' with '%s'.\n", search, replace);
	XmtMsgLineClear(line, XmtMsgLineOnAction);
	break;
    case 5:
	XmtMsgLineSet(line, "Really Quit? (y/n): ");
	c = XmtMsgLineGetChar(line);
	while ((c != 'y') && (c != 'n') && (c != EOF)) {
	    XBell(XtDisplay(w), 0);
	    XmtMsgLineSet(line, "Please type 'y' or 'n'.  Really Quit?: ");
	    c = XmtMsgLineGetChar(line);
	}
	if (c == 'y') exit(0);
	XmtMsgLineClear(line, XmtMsgLineNow);
	break;
    case 6:
	XtSetSensitive(chooser, False);
	XtVaSetValues(line,
		      XmtNallowAsyncInput, True,
		      NULL);
	break;
    }
}

/* emacs-style bindings */    
static String translations = "\
Ctrl<Key>a: beginning-of-line()\n\
Ctrl<Key>e: end-of-line()\n\
Ctrl<Key>b: backward-character()\n\
Ctrl<Key>f: forward-character()\n\
Ctrl<Key>d: delete-next-character()\n\
Ctrl<Key>k: kill-to-end-of-line()\n\
Meta<Key>b: backward-word()\n\
Meta<Key>f: forward-word()\n\
Meta<Key>d: kill-next-word()\n\
Meta<Key>osfBackSpace: kill-previous-word()\
";
  
static String labels[] = {"XmtMsgLineClear", "XmtMsgLineSet",
			      "XmtMsgLineAppend", "XmtMsgLinePrintf",
			      "XmtMsgLineGetString", "XmtMsgLineGetChar",
			      "Async Input"};
int main(argc,argv)
int argc;
char **argv;
{
    XtAppContext app;
    Widget toplevel, layout;

    toplevel = XtAppInitialize(&app, "Msgline", NULL, 0, &argc, argv,
			       NULL, NULL, 0);

    layout = XmtCreateLayout(toplevel, "layout", NULL, 0);
    XtManageChild(layout);

    XmtVaRegisterSymbols("command", XmtRBuffer, sizeof(cmd), cmd,
			 NULL);

    chooser = XtVaCreateManagedWidget("chooser", xmtChooserWidgetClass, layout,
				      XmtNchooserType, XmtChooserButtonBox,
				      XmtNstrings, labels,
				      XmtNnumItems, XtNumber(labels),
				      XmNorientation, XmHORIZONTAL,
				      XmNnumColumns, 2,
				      NULL);

    line = XtVaCreateManagedWidget("msgline", xmtMsgLineWidgetClass, layout,
				   XmtNsymbolName, "command",
				   XmtNmsgLineTranslations,
				     XtParseTranslationTable(translations),
				   XmNshadowThickness, 0,
				   XmNhighlightThickness, 0, 
				   XmtNlayoutFrameType, XmtLayoutFrameTop,
				   XmtNlayoutFrameLineType,
				     XmtLayoutFrameSingleLine,
				   XmtNlayoutFrameThickness, 4,
				   NULL);
    
    XtAddCallback(chooser, XmtNvalueChangedCallback, test, (XtPointer)line);
    XtAddCallback(line, XmtNinputCallback, input, (XtPointer) NULL);
    
    XtRealizeWidget(toplevel);
    XtAppMainLoop(app);
}
