/**
 * Scout - The Amiga System Monitor
 *
 *------------------------------------------------------------------
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * You must not use this source code to gain profit of any kind!
 *
 *------------------------------------------------------------------
 *
 * @author Andreas Gelhausen
 * @author Richard Körber <rkoerber@gmx.de>
 */

#include "system_headers.h"

#define ScoutWindow(idx, class, cmd) {                                  \
    struct MainWinData *mwd = INST_DATA(cl, obj);                       \
                                                                        \
    if (mwd->mwd_Windows[idx]) {                                        \
        DoMethod(mwd->mwd_Windows[idx], MUIM_Window_ToFront);           \
        set(mwd->mwd_Windows[idx], MUIA_Window_Activate, TRUE);         \
    } else {                                                            \
       APTR win;                                                        \
                                                                        \
       if (win = class, MUIA_Window_ParentWindow, obj,                  \
                        MUIA_Window_MaxChildWindowCount, (opts.SingleWindows) ? 1 : 0, \
                        End) {                                          \
           mwd->mwd_Windows[idx] = win;                                 \
           DoMethod(win, MUIM_Notify, MUIA_Window_CloseRequest,         \
                    TRUE, obj, 3, MUIM_CallHook, &close_callhook, idx); \
           set(win, MUIA_Window_Open, TRUE);                            \
           if (cmd) DoMethod(win, cmd);                                 \
       }                                                                \
    }                                                                   \
                                                                        \
    return 0;                                                           \
}

static __asm __saveds LONG close_callfunc( register __a0 struct Hook *hook, register __a1 ULONG *index, register __a2 struct Object *obj )
{
    struct MainWinData *mwd = INST_DATA(OCLASS(obj), obj);

    mwd->mwd_Windows[*index] = NULL;

    return 0;
}

MakeHook(close_callhook, close_callfunc);

static ULONG __saveds mNew( struct IClass *cl,
                            Object *obj,
                            struct opSet *msg )
{
    APTR button[30];

    if (obj = (Object *)DoSuperNew(cl, obj,
        MUIA_HelpNode, "Main",
        MUIA_Window_ID, MakeID('M','A','I','N'),
        WindowContents, HGroup,

            MUIA_Group_SameHeight, TRUE,
            Child, VGroup,                  /* This is the SCOUT logo */
                MUIA_Weight    , 0,
                MUIA_Frame     , MUIV_Frame_Text,
                MUIA_Background, MUII_SHADOW,
                Child, VSpace(0),
                Child, BodychunkObject,
                    MUIA_FixWidth, SCOUT_LOGO_WIDTH,
                    MUIA_FixHeight, SCOUT_LOGO_HEIGHT,
                    MUIA_Bitmap_Width, SCOUT_LOGO_WIDTH,
                    MUIA_Bitmap_Height, SCOUT_LOGO_HEIGHT,
                    MUIA_Bodychunk_Depth, SCOUT_LOGO_DEPTH ,
                    MUIA_Bodychunk_Body, scout_logo_body,
                    MUIA_Bodychunk_Compression, SCOUT_LOGO_COMPRESSION,
                    MUIA_Bodychunk_Masking, SCOUT_LOGO_MASKING,
                    MUIA_Bitmap_SourceColors, scout_logo_colors,
                    MUIA_Bitmap_Transparent, FALSE,
                End,
                Child, VSpace(0),
            End,
            Child, ColGroup(5), MUIA_Group_SameSize, TRUE,
                Child, button[ 0] = MakeButton("_Allocations"),
                Child, button[ 1] = MakeButton("Assi_gns"),
                Child, button[ 2] = MakeButton("_BoopsiClasses"),
                Child, button[ 3] = MakeButton("Commodities"),

                Child, button[ 4] = MakeButton("_Devices"),
                Child, button[ 5] = MakeButton("E_xpansions"),
                Child, button[ 6] = MakeButton("_Fonts"),
                Child, button[ 7] = MakeButton("Input_Handlers"),

                Child, button[ 8] = MakeButton("_Interrupts"),
                Child, button[ 9] = MakeButton("_Libraries"),
                Child, button[10] = MakeButton("L_ocks"),
                Child, button[11] = MakeButton("LowMemory"),

                Child, button[12] = MakeButton("_Memory"),
                Child, button[13] = MakeButton("Mou_nted Devs"),
                Child, button[14] = MakeButton("_Ports"),
                Child, button[15] = MakeButton("_Residents"),

                Child, button[16] = MakeButton("_Commands"),
                Child, button[17] = MakeButton("Reso_urces"),
                Child, button[18] = MakeButton("ScreenModes"),
                Child, button[19] = MakeButton("_Semaphores"),

                Child, button[20] = MakeButton("S_ystem"),
                Child, button[21] = MakeButton("_Tasks"),
                Child, button[22] = MakeButton("Tim_er"),
                Child, button[23] = MakeButton("_Vectors"),

                Child, button[24] = MakeButton("_Windows"),
                Child, button[25] = MakeButton("Patches"),
                Child, button[26] = MakeButton("Catalogs"),
                Child, button[27] = MakeButton("AudioModes"),

                Child, HVSpace,
                Child, HVSpace,
            End,
        End,
        TAG_MORE, msg->ops_AttrList))
    {
        struct MainWinData *mwd = INST_DATA(cl, obj);
        struct PatchPort *pp;
        struct SetManPort *sp;

        set(obj, MUIA_Window_Title, MyGetWindowTitle("\0", mwd->mwd_Title, sizeof(mwd->mwd_Title)));

        DoMethod(obj,        MUIM_Notify,  MUIA_Window_CloseRequest, TRUE,  MUIV_Notify_Application, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);

        DoMethod(button[ 0], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowAllocations);
        DoMethod(button[ 1], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowAssigns);
        DoMethod(button[ 2], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowClasses);
        DoMethod(button[ 3], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowCommodities);
        DoMethod(button[ 4], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowDevices);
        DoMethod(button[ 5], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowExpansions);
        DoMethod(button[ 6], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowFonts);
        DoMethod(button[ 7], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowInputHandlers);
        DoMethod(button[ 8], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowInterrupts);
        DoMethod(button[ 9], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowLibraries);
        DoMethod(button[10], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowLocks);
        DoMethod(button[11], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowLowMemory);
        DoMethod(button[12], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowMemory);
        DoMethod(button[13], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowMounts);
        DoMethod(button[14], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowPorts);
        DoMethod(button[15], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowResidents);
        DoMethod(button[16], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowCommands);
        DoMethod(button[17], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowResources);
        DoMethod(button[18], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowScreenModes);
        DoMethod(button[19], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowSemaphores);
        DoMethod(button[20], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowSystem);
        DoMethod(button[21], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowTasks);
        DoMethod(button[22], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowTimers);
        DoMethod(button[23], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowVectors);
        DoMethod(button[24], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowWindows);
        DoMethod(button[25], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowPatches);
        DoMethod(button[26], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowCatalogs);
        DoMethod(button[27], MUIM_Notify,  MUIA_Pressed, FALSE, obj, 1, MUIM_MainWin_ShowAudioModes);

        Forbid();
        pp = (struct PatchPort *)FindPort(PATCHPORT_NAME);
        sp = (struct SetManPort *)FindPort(SETMANPORT_NAME);
        Permit();
        set(button[25], MUIA_Disabled, (pp == NULL && sp == NULL));
    }

    return (ULONG)obj;
}

static ULONG __saveds mDispose( struct IClass *cl,
                                Object *obj,
                                struct opSet *msg )
{
    set(obj, MUIA_Window_Open, FALSE);

    return (DoSuperMethodA(cl, obj, msg));
}

static ULONG __saveds mAbout( struct IClass *cl,
                              Object *obj,
                              Msg msg )
{
    APTR aboutWin;

    if (aboutWin = AboutWindowObject,
            MUIA_Window_ParentWindow, obj,
            MUIA_Window_NoMenus, TRUE,
        End) {
        APTR app;

        get(obj, MUIA_ApplicationObject, &app);
        DoMethod(app, OM_ADDMEMBER, aboutWin);

        DoMethod(aboutWin, MUIM_AboutWin_About);

        DoMethod(app, OM_REMMEMBER, aboutWin);

        MUI_DisposeObject(aboutWin);
    }

    return 0;
}

static ULONG __saveds mShowAllocations( struct IClass *cl,
                                        Object *obj,
                                        Msg msg )
{
    ScoutWindow(0, AllocationsWindowObject, MUIM_AllocationsWin_Update);
}

static ULONG __saveds mShowAssigns( struct IClass *cl,
                                    Object *obj,
                                    Msg msg )
{
    ScoutWindow(1, AssignsWindowObject, MUIM_AssignsWin_Update);
}

static ULONG __saveds mShowClasses( struct IClass *cl,
                                    Object *obj,
                                    Msg msg )
{
    ScoutWindow(2, ClassesWindowObject, MUIM_ClassesWin_Update);
}

static ULONG __saveds mShowCommodities( struct IClass *cl,
                                        Object *obj,
                                        Msg msg )
{
    ScoutWindow(3, CommoditiesWindowObject, MUIM_CommoditiesWin_Update);
}

static ULONG __saveds mShowDevices( struct IClass *cl,
                                    Object *obj,
                                    Msg msg )
{
    ScoutWindow(4, DevicesWindowObject, MUIM_DevicesWin_Update);
}

static ULONG __saveds mShowExpansions( struct IClass *cl,
                                       Object *obj,
                                       Msg msg )
{
    ScoutWindow(5, ExpansionsWindowObject, MUIM_ExpansionsWin_Update);
}

static ULONG __saveds mShowFonts( struct IClass *cl,
                                  Object *obj,
                                  Msg msg )
{
    ScoutWindow(6, FontsWindowObject, MUIM_FontsWin_Update);
}

static ULONG __saveds mShowInputHandlers( struct IClass *cl,
                                          Object *obj,
                                          Msg msg )
{
    ScoutWindow(7, InputHandlersWindowObject, MUIM_InputHandlersWin_Update);
}

static ULONG __saveds mShowInterrupts( struct IClass *cl,
                                       Object *obj,
                                       Msg msg )
{
    ScoutWindow(8, InterruptsWindowObject, MUIM_InterruptsWin_Update);
}

static ULONG __saveds mShowLibraries( struct IClass *cl,
                                      Object *obj,
                                      Msg msg )
{
    ScoutWindow(9, LibrariesWindowObject, MUIM_LibrariesWin_Update);
}

static ULONG __saveds mShowLocks( struct IClass *cl,
                                  Object *obj,
                                  Msg msg )
{
    ScoutWindow(10, LocksWindowObject, 0);
}

static ULONG __saveds mShowLowMemory( struct IClass *cl,
                                      Object *obj,
                                      Msg msg )
{
    ScoutWindow(11, LowMemoryWindowObject, MUIM_LowMemoryWin_Update);
}

static ULONG __saveds mShowMemory( struct IClass *cl,
                                   Object *obj,
                                   Msg msg )
{
    ScoutWindow(12, MemoryWindowObject, MUIM_MemoryWin_Update);
}

static ULONG __saveds mShowMounts( struct IClass *cl,
                                   Object *obj,
                                   Msg msg )
{
    ScoutWindow(13, MountsWindowObject, MUIM_MountsWin_Update);
}

static ULONG __saveds mShowPorts( struct IClass *cl,
                                  Object *obj,
                                  Msg msg )
{
    ScoutWindow(14, PortsWindowObject, MUIM_PortsWin_Update);
}

static ULONG __saveds mShowResidents( struct IClass *cl,
                                      Object *obj,
                                      Msg msg )
{
    ScoutWindow(15, ResidentsWindowObject, MUIM_ResidentsWin_Update);
}

static ULONG __saveds mShowCommands( struct IClass *cl,
                                     Object *obj,
                                     Msg msg )
{
    ScoutWindow(16, CommandsWindowObject, MUIM_CommandsWin_Update);
}

static ULONG __saveds mShowResources( struct IClass *cl,
                                      Object *obj,
                                      Msg msg )
{
    ScoutWindow(17, ResourcesWindowObject, MUIM_ResourcesWin_Update);
}

static ULONG __saveds mShowScreenModes( struct IClass *cl,
                                        Object *obj,
                                        Msg msg )
{
    ScoutWindow(18, ScreenModesWindowObject, MUIM_ScreenModesWin_Update);
}

static ULONG __saveds mShowSemaphores( struct IClass *cl,
                                       Object *obj,
                                       Msg msg )
{
    ScoutWindow(19, SemaphoresWindowObject, MUIM_SemaphoresWin_Update);
}

static ULONG __saveds mShowSystem( struct IClass *cl,
                                   Object *obj,
                                   Msg msg )
{
    ScoutWindow(20, SystemWindowObject, MUIM_SystemWin_Update);
}

static ULONG __saveds mShowTasks( struct IClass *cl,
                                  Object *obj,
                                  Msg msg )
{
    ScoutWindow(21, TasksWindowObject, MUIM_TasksWin_Update);
}

static ULONG __saveds mShowTimers( struct IClass *cl,
                                   Object *obj,
                                   Msg msg )
{
    ScoutWindow(22, TimersWindowObject, MUIM_TimersWin_Update);
}

static ULONG __saveds mShowVectors( struct IClass *cl,
                                    Object *obj,
                                    Msg msg )
{
    ScoutWindow(23, VectorsWindowObject, MUIM_VectorsWin_Update);
}

static ULONG __saveds mShowWindows( struct IClass *cl,
                                    Object *obj,
                                    Msg msg )
{
    ScoutWindow(24, WindowsWindowObject, MUIM_WindowsWin_Update);
}

static ULONG __saveds mShowPatches( struct IClass *cl,
                                    Object *obj,
                                    Msg msg )
{
    ScoutWindow(25, PatchesWindowObject, MUIM_PatchesWin_Update);
}

static ULONG __saveds mShowCatalogs( struct IClass *cl,
                                    Object *obj,
                                    Msg msg )
{
    ScoutWindow(26, CatalogsWindowObject, MUIM_CatalogsWin_Update);
}

static ULONG __saveds mShowAudioModes( struct IClass *cl,
                                       Object *obj,
                                       Msg msg )
{
    ScoutWindow(27, AudioModesWindowObject, MUIM_AudioModesWin_Update);
}

static ULONG __saveds mFlushDevices( struct IClass *cl,
                                     Object *obj,
                                     Msg msg )
{
    MyDoCommand("FLUSHDEVS");

    return 0;
}

static ULONG __saveds mFlushFonts( struct IClass *cl,
                                   Object *obj,
                                   Msg msg )
{
    MyDoCommand("FLUSHFONTS");

    return 0;
}

static ULONG __saveds mFlushLibraries( struct IClass *cl,
                                       Object *obj,
                                       Msg msg )
{
    MyDoCommand("FLUSHLIBS");

    return 0;
}

static ULONG __saveds mFlushAll( struct IClass *cl,
                                 Object *obj,
                                 Msg msg )
{
    MyDoCommand("FLUSHALL");

    return 0;
}

ULONG __asm __saveds MainWinDispatcher( register __a0 struct IClass *cl,
                                        register __a2 Object *obj,
                                        register __a1 Msg msg )
{
    switch (msg->MethodID) {
        case OM_NEW:                         return (mNew(cl, obj, (APTR)msg));
        case OM_DISPOSE:                     return (mDispose(cl, obj, (APTR)msg));
        case MUIM_MainWin_About:             return (mAbout(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowAllocations:   return (mShowAllocations(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowAssigns:       return (mShowAssigns(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowClasses:       return (mShowClasses(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowCommodities:   return (mShowCommodities(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowDevices:       return (mShowDevices(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowExpansions:    return (mShowExpansions(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowFonts:         return (mShowFonts(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowInputHandlers: return (mShowInputHandlers(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowInterrupts:    return (mShowInterrupts(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowLibraries:     return (mShowLibraries(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowLocks:         return (mShowLocks(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowLowMemory:     return (mShowLowMemory(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowMemory:        return (mShowMemory(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowMounts:        return (mShowMounts(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowPorts:         return (mShowPorts(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowResidents:     return (mShowResidents(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowCommands:      return (mShowCommands(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowResources:     return (mShowResources(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowScreenModes:   return (mShowScreenModes(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowSemaphores:    return (mShowSemaphores(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowSystem:        return (mShowSystem(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowTasks:         return (mShowTasks(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowTimers:        return (mShowTimers(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowVectors:       return (mShowVectors(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowWindows:       return (mShowWindows(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowPatches:       return (mShowPatches(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowCatalogs:      return (mShowCatalogs(cl, obj, (APTR)msg));
        case MUIM_MainWin_ShowAudioModes:    return (mShowAudioModes(cl, obj, (APTR)msg));
        case MUIM_MainWin_FlushDevices:      return (mFlushDevices(cl, obj, (APTR)msg));
        case MUIM_MainWin_FlushFonts:        return (mFlushFonts(cl, obj, (APTR)msg));
        case MUIM_MainWin_FlushLibraries:    return (mFlushLibraries(cl, obj, (APTR)msg));
        case MUIM_MainWin_FlushAll:          return (mFlushAll(cl, obj, (APTR)msg));
    }

    return (DoSuperMethodA(cl, obj, msg));
}

