#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Dialog.h>
#include "RadioBtn.h"
#include "LinkText.h"
#include "PcBar.h"
#include "popup.h"

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

#include "find.h"
#include "xregal.h"
#include "utility.h"
#include "history.h"
#include "LinkView.h"
#include "searchHistory.h"

#define DIALOG_WIDTH 400

Boolean find_popped_up=False;
int clicks;

static char *searchstr;

Widget find_shell,
		find_form,
		find_box,
		btn_find_close,
		btn_find_next,
		find_label,
		find_text;

static void Cancel(Widget w, XtPointer client_data, XtPointer call_data) {	
	enableSearch();
	XtPopdown(find_shell);
	find_popped_up=False;

    XDefineCursor(XtDisplay(topLevel), XtWindow(find_form), unBusyCursor);
	}


static void Search(Widget w, XtPointer client_data, XtPointer call_data) {
	Boolean found;

	clicks++;
	XtVaGetValues(find_text, XtNstring, &searchstr, NULL);
	AddStringToHistory(find_text, searchstr);

	/* guard against dumb searches */
 	trim_text(searchstr, searchstr);
	if (strlen(searchstr)<1) {
		Alert("You haven't specified anything to search for.");
		XBell(XtDisplay(topLevel), 100);
		return;
		}

    XDefineCursor(XtDisplay(topLevel), XtWindow(find_form), busyCursor);

	found = LinkViewFindText(vport, searchstr, (clicks==1));
	if (!found) 
		XBell(XtDisplay(topLevel), 100);
		
    XDefineCursor(XtDisplay(topLevel), XtWindow(find_form), unBusyCursor);
	}

void CreateFindDialog(void) {
    static String myreturn = "#override <Key>Return:   set() notify() unset()\n";
    static String myescape = "#override <Key>Escape:   set() notify() unset()\n";
    XtAccelerators accel, escaccel;

    accel = XtParseAcceleratorTable(myreturn);
    escaccel = XtParseAcceleratorTable(myescape);

	find_shell=XtVaCreatePopupShell("find_shell", transientShellWidgetClass,
                topLevel,
                XtNallowShellResize, False,
                NULL);

	find_form=XtVaCreateManagedWidget("find_form",formWidgetClass, find_shell, NULL);
	find_box=XtVaCreateManagedWidget("find_box", boxWidgetClass, find_form, 
		XtNwidth, DIALOG_WIDTH,
		XtNborderWidth, 0,
		NULL);
	btn_find_close=XtVaCreateManagedWidget("btn_find_close",commandWidgetClass, find_box,
		XtNlabel, "Close",
        XtNaccelerators, escaccel,
		NULL);
	btn_find_next=XtVaCreateManagedWidget("btn_find_next",commandWidgetClass,find_box,
		XtNlabel, "Find Next",
        XtNaccelerators, accel,
		NULL);

	find_label=XtVaCreateManagedWidget("find_label",labelWidgetClass, find_form,
		XtNfromVert, find_box,
		XtNwidth, 120,
		XtNborderWidth, 0,
		XtNjustify, XtJustifyLeft,
		NULL);
	find_text = XtVaCreateManagedWidget("find_text",asciiTextWidgetClass, find_form,
        XtNeditType, XawtextEdit,
        XtNwidth, DIALOG_WIDTH-90,
        XtNresizable, True,
        XtNfromHoriz, find_label,
		XtNfromVert, find_box,
        XtNstring, searchstr,
        XtNuseStringInPlace, False,
        NULL);
	AddHistoryText(find_text);


    XtAddCallback(btn_find_close, XtNcallback, Cancel, 0);
    XtAddCallback(btn_find_next, XtNcallback, Search, 0);
    XtSetKeyboardFocus(find_form, find_text);
    XtInstallAccelerators(find_text, btn_find_close);
    XtInstallAccelerators(find_text, btn_find_next);
	}


void PopupFind(void) {	
	disableSearch();
	XtVaSetValues(find_text, XtNstring, NULL, NULL);
	SetPopupPosition(find_shell, topLevel);
	XtPopup(find_shell, XtGrabNone);
    XDefineCursor(XtDisplay(topLevel), XtWindow(find_form), unBusyCursor);
	find_popped_up=True;
	clicks=0;
	ResetHistory(find_text);
	}
