#include "gui.h"

#include <inline/muimaster.h>

struct Library *MUIMasterBase;

/*************************/
/* Init & Fail Functions */
/*************************/

static VOID fail(APTR app,char *str)
{
        if (app)
                MUI_DisposeObject(app);

        if (MUIMasterBase)
                CloseLibrary(MUIMasterBase);

        if (str)
        {
                puts(str);
                exit(20);
        }
        exit(0);
}


static void init(void)
{
        if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
                fail(NULL,"Failed to open "MUIMASTER_NAME".");
}

/************************/
/* DisposeApp           */
/************************/
void DisposeApp(struct ObjApp * App)
{
        MUI_DisposeObject(App->app);
        FreeVec(App);
}

/************************/
/* CreateGUI            */
/************************/

struct ObjApp *creategui (void)
{
        struct ObjApp *App;

        init();

        if (!(App = AllocVec(sizeof(struct ObjApp), MEMF_ANY|MEMF_CLEAR))) return(NULL);

        /* Create the application object */
           App->app = ApplicationObject,
                /* Default information */
                MUIA_Application_Title      , "SearchHTML",
                MUIA_Application_Version    , "$VER: SearchHTML 0.4 (22.06.99)",
                MUIA_Application_Copyright  , ")1999, Brendan Rogers",
                MUIA_Application_Author     , "Brendan Rogers",
                MUIA_Application_Description, "Search string GUI.",
                MUIA_Application_Base       , "SHTM",

                /* Define a window object*/
                SubWindow, App->window = WindowObject,
                        /* Put a title on your window */
                        MUIA_Window_Title, "Search entire document",
                        /* Give your window a unique ID */
                        MUIA_Window_ID   , MAKE_ID('S','H','T','M'),

                        /* Define what is inside your window */
                        WindowContents, VGroup, /* Everything inside this group will be layed out vertically */
                            Child, HGroup,
                                Child, App->label1        = Label ("Match whole word:"),
                                Child, App->checkmark1    = CheckMark(TRUE),
                                Child, App->label2        = Label ("case sensitive:"),
                                Child, App->checkmark2    = CheckMark(FALSE),
                                Child, App->label3        = Label ("search HTML code:"),
                                Child, App->checkmark3    = CheckMark(FALSE),
                                End,
                            Child, HGroup,
                                Child, App->label4        = Label ("Search text"),
                                Child, App->string1       = String ("Enter search text here",50),
                                End,
                            Child, HGroup,
                                Child, App->simplebutton1 = SimpleButton ("Search"),
                                Child, HSpace (4),
                                Child, App->simplebutton2 = SimpleButton ("Cancel"),
                                End,
                            End,
                        End,
                End;

        /* Check to see if the interface was made, if it wasn't, exit */
        if (!App->app) {
            FreeVec(App);
            fail(App->app,"Failed to create Application.");
        }

        /* Shut down the application when the user closes the window, connect the close window gadget to make it */
        /* return MUIV_Application_ReturnID_Quit to out main loop */
        DoMethod(App->window,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,App->app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);

        /* shut down the application with the cancel button */
        DoMethod(App->simplebutton2,MUIM_Notify,MUIA_Pressed,FALSE,App->app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);

        /* user hits the search button */
        DoMethod(App->simplebutton1,MUIM_Notify,MUIA_Pressed,FALSE,App->app,2,MUIM_Application_ReturnID,GO_SEARCH);

        /* Add my objects to a cyclechain so a user can use the tab key to cycle through my app */
        DoMethod(App->window,MUIM_Window_SetCycleChain,App->checkmark1,App->checkmark2,App->checkmark3,App->string1,App->simplebutton1,App->simplebutton2,NULL);

        /* Help text for bubbles, normally this should be localized */
        set(App->checkmark1,   MUIA_ShortHelp,"Check me to ignore partial matches");
        set(App->checkmark2,   MUIA_ShortHelp,"case sensitive search");
        set(App->checkmark3,   MUIA_ShortHelp,"Search inside HTML code,\nincluding links to files and graphics");
        set(App->string1,      MUIA_ShortHelp,"Enter search text here");
        set(App->simplebutton1,MUIA_ShortHelp,"Click to start search");
        set(App->simplebutton2,MUIA_ShortHelp,"Click to cancel search");

        /* advance from search string to search button with CR */
        set(App->string1, MUIA_String_AdvanceOnCR, TRUE);
        /* Open the applications window */
        set(App->window,MUIA_Window_Open,TRUE);

        return(App);
}



