/*
 *  Gadget source generated with GadToolsBox V1.3
 *  which is (c) Copyright 1991,92 Jaba Development
 *
 *  NB: edited.
 *
 *  ColorSwitch
 *
 *  Lets you change palettes - 1.3, 2.0 and your own - at the click of
 *  a mouse. Based on an idea by Guido Wegener.
 *
 *  Martin W. Scott, 28 June 1992.
 */
#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/imageclass.h>
#include <intuition/gadgetclass.h>
#include <intuition/preferences.h>
#include <libraries/gadtools.h>
#include <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <string.h>

#include <pragmas/exec_pragmas.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/gadtools_pragmas.h>
#include <pragmas/graphics_pragmas.h>

struct IntuitionBase *IntuitionBase;
struct GadToolsBase *GadToolsBase;

UWORD palettes[3][4] = {
	{ 0x05a, 0xfff, 0x002, 0xf80 },	/* WB1.x prefs */
	{ 0xaaa, 0x001, 0xfff, 0x57a }	/* WB2.0 prefs */
	/* user prefs */
};

#include "colorswitch.h"

struct Screen        *Scr = NULL;
APTR                  VisualInfo = NULL;
struct Window        *SwitchWnd = NULL;
struct Gadget        *SwitchGList = NULL;
struct Gadget        *SwitchGadgets[1];
UWORD                 SwitchLeft = 460;
UWORD                 SwitchTop = 18;
UWORD                 SwitchWidth = 125;
UWORD                 SwitchHeight = 33;
UBYTE                *SwitchWdt = (UBYTE *)"Colors";

WORD SwitchZoom[4] = {460, 18, 125, 0 };

UBYTE         *PaletteLabels[] = {
    (UBYTE *)"WB 1.x",
    (UBYTE *)"WB 2.0",
    (UBYTE *)"Prefs",
    NULL };

struct TextAttr topaz8 = {
    ( STRPTR )"topaz.font", 8, 0x00, 0x00 };

int SetupScreen( void )
{
    if ( ! ( Scr = LockPubScreen((UBYTE * )"Workbench" )))
        return( 1L );

    if ( ! ( VisualInfo = GetVisualInfo( Scr, TAG_DONE )))
        return( 2L );

    return( 0L );
}

void CloseDownScreen( void )
{
    if ( VisualInfo ) {
        FreeVisualInfo( VisualInfo );
        VisualInfo = NULL;
    }

    if ( Scr        ) {
        UnlockPubScreen( NULL, Scr );
        Scr = NULL;
    }
}

int OpenSwitchWindow( void )
{
    struct NewGadget     ng;
    struct Gadget       *g;
    UWORD               offx, offy;

    offx = Scr->WBorLeft;
    offy = Scr->WBorTop + Scr->RastPort.TxHeight + 1;
    SwitchZoom[3] = offy;

    if ( ! ( g = CreateContext( &SwitchGList )))
        return( 1L );

    ng.ng_LeftEdge        =    offx + 20;
    ng.ng_TopEdge         =    offy + 2;
    ng.ng_Width           =    17;
    ng.ng_Height          =    9;
    ng.ng_GadgetText      =    NULL;
    ng.ng_TextAttr        =    &topaz8;
    ng.ng_GadgetID        =    GD_Palette;
    ng.ng_Flags           =    PLACETEXT_RIGHT;
    ng.ng_VisualInfo      =    VisualInfo;

    g = CreateGadget( MX_KIND, g, &ng, GTMX_Active, 2, GTMX_Labels, &PaletteLabels[0], TAG_DONE );

    SwitchGadgets[ 0 ] = g;

    if ( ! g )
        return( 2L );

    if ( ! ( SwitchWnd = OpenWindowTags( NULL,
                    WA_Left,          SwitchLeft,
                    WA_Top,           SwitchTop,
                    WA_Width,         SwitchWidth,
                    WA_Height,        SwitchHeight + offy,
                    WA_IDCMP,         MXIDCMP|IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW,
                    WA_Flags,         WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH,
                    WA_Gadgets,       SwitchGList,
                    WA_Title,         SwitchWdt,
                    WA_ScreenTitle,   "ColorSwitch",
		    WA_Zoom,	      SwitchZoom,
                    TAG_DONE )))
        return( 4L );

    GT_RefreshWindow( SwitchWnd, NULL );

    return( 0L );
}

void CloseSwitchWindow( void )
{
    if ( SwitchWnd        ) {
        CloseWindow( SwitchWnd );
        SwitchWnd = NULL;
    }

    if ( SwitchGList      ) {
        FreeGadgets( SwitchGList );
        SwitchGList = NULL;
    }
}

void CloseLibs()
{
	if (IntuitionBase) CloseLibrary(IntuitionBase);
	if (GadToolsBase) CloseLibrary(GadToolsBase);
}

BOOL OpenLibs()
{
	if ((IntuitionBase = (void *)OpenLibrary("intuition.library",37L)) &&
	    (GadToolsBase =  (void *)OpenLibrary("gadtools.library",37L)))
		return TRUE;

	CloseLibs();
	return FALSE;
}

void GetSysPalette(UWORD *palette)
{
	struct Preferences prefs;

	GetPrefs(&prefs, sizeof(prefs));
	*palette++ = prefs.color0;
	*palette++ = prefs.color1;
	*palette++ = prefs.color2;
	*palette = prefs.color3;
}

void SetSysPalette(UWORD *palette)
{
	struct Preferences prefs;

	GetPrefs(&prefs, sizeof(prefs));
	prefs.color0 = *palette++;
	prefs.color1 = *palette++;
	prefs.color2 = *palette++;
	prefs.color3 = *palette;
	SetPrefs(&prefs, sizeof(prefs), TRUE);
}

void _main()
{
	if (!OpenLibs())
		return;

	if (!SetupScreen())
	{
		if (!OpenSwitchWindow())
		{
			GetSysPalette(&palettes[2][0]);

			for (;;)
    			{
				struct IntuiMessage *msg;
				ULONG class;
				UWORD code;

				WaitPort(SwitchWnd->UserPort);
				while (msg = GT_GetIMsg(SwitchWnd->UserPort))
				{
					class = msg->Class;
					code = msg->Code;
					GT_ReplyIMsg(msg);

					if (class == CLOSEWINDOW)
						goto byebye; /* goto... naughty! */
					else if (class == MXIDCMP)
						SetSysPalette(&palettes[code][0]);
				}

			} /* for (;;) */

		    byebye:
			CloseSwitchWindow();
			SetSysPalette(&palettes[2][0]);
		}
		CloseDownScreen();
	}
	CloseLibs();

} /* main */