#ifndef MAKE_ID
#define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
#endif

/* Libraries */
#include <libraries/mui.h>
#include <libraries/gadtools.h> /* for Barlabel in MenuItem */
#include <exec/memory.h>

/* Prototypes */
#include <proto/muimaster.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>

#include "faxGUI.h"

static char *GetMBString(APTR ref)
{
        char *aux;

        aux = GetFaxString(ref);
        if (aux[1] == '\0') return(&aux[2]);
        else                return(aux);
}
extern struct Image schrifft_image;

struct ObjApp * CreateApp(void)
{
        struct ObjApp * Object;

        APTR    MNlabel1Projekt, MNlabel1ber, MNlabel1BarLabel0, MNlabel1Beenden;
        APTR    GROUP_ROOT_0, IM_label_1, GR_grp_1, GR_grp_4, Space_4, GROUP_ROOT_1;
        APTR    GR_grp_2, Space_2, GR_grp_3, Space_0, Space_1, Space_3;

        if (!(Object = AllocVec(sizeof(struct ObjApp),MEMF_PUBLIC|MEMF_CLEAR)))
                return(NULL);

        Object->STR_TX_ueber = GetMBString(MSG_TX_ueber);

        IM_label_1 = ImageObject,
                MUIA_Image_OldImage, &schrifft_image,
        End;

        Object->BT_Printer = SimpleButton(GetMBString(MSG_BT_Printer));

        Object->BT_Spooler = SimpleButton(GetMBString(MSG_BT_Spooler));

        Object->BT_Logbook = SimpleButton(GetMBString(MSG_BT_Logbook));

        Object->BT_Config = SimpleButton(GetMBString(MSG_BT_Config));

        Object->BT_Desktop = SimpleButton(GetMBString(MSG_BT_Desktop));

        Object->BT_Phone = SimpleButton(GetMBString(MSG_BT_Phone));

        GR_grp_1 = GroupObject,
                MUIA_Frame, MUIV_Frame_Button,
                MUIA_Group_Columns, 3,
                MUIA_Group_SameSize, TRUE,
                MUIA_Group_HorizSpacing, 10,
                MUIA_Group_VertSpacing, 5,
                Child, Object->BT_Printer,
                Child, Object->BT_Spooler,
                Child, Object->BT_Logbook,
                Child, Object->BT_Config,
                Child, Object->BT_Desktop,
                Child, Object->BT_Phone,
        End;

        Object->BT_View = SimpleButton(GetMBString(MSG_BT_View));

        Space_4 = HSpace(20);

        Object->BT_End = SimpleButton(GetMBString(MSG_BT_End));

        GR_grp_4 = GroupObject,
                MUIA_Frame, MUIV_Frame_Button,
                MUIA_Group_Horiz, TRUE,
                Child, Object->BT_View,
                Child, Space_4,
                Child, Object->BT_End,
        End;

        GROUP_ROOT_0 = GroupObject,
                MUIA_Group_SameWidth, TRUE,
                Child, IM_label_1,
                Child, GR_grp_1,
                Child, GR_grp_4,
        End;

        MNlabel1ber = MenuitemObject,
                MUIA_Menuitem_Title, GetMBString(MSG_MNlabel1ber),
                MUIA_Menuitem_Shortcut, GetMBString(MSG_MNlabel1berChar),
        End;

        MNlabel1BarLabel0 = MUI_MakeObject(MUIO_Menuitem, NM_BARLABEL, 0, 0, 0);

        MNlabel1Beenden = MenuitemObject,
                MUIA_Menuitem_Title, GetMBString(MSG_MNlabel1Beenden),
                MUIA_Menuitem_Shortcut, GetMBString(MSG_MNlabel1BeendenChar),
        End;

        MNlabel1Projekt = MenuitemObject,
                MUIA_Menuitem_Title, GetMBString(MSG_MNlabel1Projekt),
                MUIA_Family_Child, MNlabel1ber,
                MUIA_Family_Child, MNlabel1BarLabel0,
                MUIA_Family_Child, MNlabel1Beenden,
        End;

        Object->MN_label_1 = MenustripObject,
                MUIA_Family_Child, MNlabel1Projekt,
        End;

        Object->Fax_Win = WindowObject,
                MUIA_Window_Title, GetMBString(MSG_Fax_Win),
                MUIA_Window_Menustrip, Object->MN_label_1,
                MUIA_Window_ID, MAKE_ID('0', 'W', 'I', 'N'),
                WindowContents, GROUP_ROOT_0,
        End;

        Object->TX_ueber = TextObject,
                MUIA_Background, MUII_TextBack,
                MUIA_Frame, MUIV_Frame_Text,
                MUIA_Text_Contents, Object->STR_TX_ueber,
                MUIA_Text_PreParse, "\033c\0338",
                MUIA_Text_SetMin, TRUE,
        End;

        Space_2 = VSpace(10);

        Space_0 = HSpace(75);

        Object->BT_OK = SimpleButton(GetMBString(MSG_BT_OK));

        Space_1 = HSpace(75);

        GR_grp_3 = GroupObject,
                MUIA_Group_Columns, 3,
                Child, Space_0,
                Child, Object->BT_OK,
                Child, Space_1,
        End;

        Space_3 = VSpace(10);

        GR_grp_2 = GroupObject,
                Child, Object->TX_ueber,
                Child, Space_2,
                Child, GR_grp_3,
                Child, Space_3,
        End;

        GROUP_ROOT_1 = GroupObject,
                MUIA_Background, MUII_ListBack,
                Child, GR_grp_2,
        End;

        Object->Info_Win = WindowObject,
                MUIA_Window_Title, GetMBString(MSG_Info_Win),
                MUIA_Window_ID, MAKE_ID('1', 'W', 'I', 'N'),
                MUIA_Window_SizeGadget, FALSE,
                WindowContents, GROUP_ROOT_1,
        End;

        Object->App = ApplicationObject,
                MUIA_Application_Author, "Jürgen Späth",
                MUIA_Application_Base, "Multifax",
                MUIA_Application_Title, "Multifax-Desk",
                MUIA_Application_Version, "$VER: Version 1.9 (27.04.1996)",
                MUIA_Application_Copyright, GetMBString(MSG_AppCopyright),
                MUIA_Application_Description, GetMBString(MSG_AppDescription),
                SubWindow, Object->Fax_Win,
                SubWindow, Object->Info_Win,
        End;


        if (!Object->App)
        {
                FreeVec(Object);
                return(NULL);
        }

        DoMethod(MNlabel1ber,
                MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
                Object->Info_Win,
                3,
                MUIM_Set, MUIA_Window_Open, TRUE
                );

        DoMethod(MNlabel1ber,
                MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
                Object->Info_Win,
                3,
                MUIM_Set, MUIA_Window_ActiveObject, MUIV_Window_ActiveObject_Next
                );

        DoMethod(MNlabel1ber,
                MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
                Object->Info_Win,
                3,
                MUIM_Set, MUIA_Window_Activate, TRUE
                );

        DoMethod(MNlabel1Beenden,
                MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
                Object->Info_Win,
                3,
                MUIM_Set, MUIA_Window_Open, FALSE
                );

        DoMethod(MNlabel1Beenden,
                MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
                Object->Fax_Win,
                3,
                MUIM_Set, MUIA_Window_Open, FALSE
                );

        DoMethod(MNlabel1Beenden,
                MUIM_Notify, MUIA_Menuitem_Trigger, MUIV_EveryTime,
                Object->App,
                2,
                MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit
                );

        DoMethod(Object->Fax_Win,
                MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
                Object->Fax_Win,
                3,
                MUIM_Set, MUIA_Window_Open, FALSE
                );

        DoMethod(Object->Fax_Win,
                MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
                Object->App,
                2,
                MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit
                );

        DoMethod(Object->BT_Printer,
                MUIM_Notify, MUIA_Pressed, FALSE,
                Object->App,
                2,
                MUIM_Application_ReturnID, BT_PRINTER
                );

        DoMethod(Object->BT_Spooler,
                MUIM_Notify, MUIA_Pressed, FALSE,
                Object->App,
                2,
                MUIM_Application_ReturnID, BT_SPOOLER
                );

        DoMethod(Object->BT_Logbook,
                MUIM_Notify, MUIA_Pressed, FALSE,
                Object->App,
                2,
                MUIM_Application_ReturnID, BT_LOGBOOK
                );

        DoMethod(Object->BT_Config,
                MUIM_Notify, MUIA_Pressed, FALSE,
                Object->App,
                2,
                MUIM_Application_ReturnID, BT_CONFIG
                );

        DoMethod(Object->BT_Desktop,
                MUIM_Notify, MUIA_Pressed, FALSE,
                Object->App,
                2,
                MUIM_Application_ReturnID, BT_DESK
                );

        DoMethod(Object->BT_Phone,
                MUIM_Notify, MUIA_Pressed, FALSE,
                Object->App,
                2,
                MUIM_Application_ReturnID, BT_PHONE
                );

        DoMethod(Object->BT_View,
                MUIM_Notify, MUIA_Pressed, FALSE,
                Object->App,
                2,
                MUIM_Application_ReturnID, BT_VIEW
                );

        DoMethod(Object->BT_End,
                MUIM_Notify, MUIA_Pressed, FALSE,
                Object->Info_Win,
                3,
                MUIM_Set, MUIA_Window_Open, FALSE
                );

        DoMethod(Object->BT_End,
                MUIM_Notify, MUIA_Pressed, FALSE,
                Object->Fax_Win,
                3,
                MUIM_Set, MUIA_Window_Open, FALSE
                );

        DoMethod(Object->BT_End,
                MUIM_Notify, MUIA_Pressed, FALSE,
                Object->App,
                2,
                MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit
                );

        DoMethod(Object->Fax_Win,
                MUIM_Window_SetCycleChain, Object->BT_Printer,
                Object->BT_Spooler,
                Object->BT_Logbook,
                Object->BT_Config,
                Object->BT_Desktop,
                Object->BT_Phone,
                Object->BT_View,
                Object->BT_End,
                0
                );

        DoMethod(Object->BT_OK,
                MUIM_Notify, MUIA_Pressed, FALSE,
                Object->Info_Win,
                3,
                MUIM_Set, MUIA_Window_Open, FALSE
                );

        DoMethod(Object->BT_OK,
                MUIM_Notify, MUIA_Pressed, FALSE,
                Object->Fax_Win,
                3,
                MUIM_Set, MUIA_Window_Activate, TRUE
                );

        DoMethod(Object->Info_Win,
                MUIM_Window_SetCycleChain, Object->BT_OK,
                0
                );

        set(Object->Fax_Win,
                MUIA_Window_Open, TRUE
                );


        return(Object);
}

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