/*
 *  prefswindow.c - prefsWindow class
 *  Copyright (C) 1997 Aki Laukkanen
 *
 *  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
 *  (at your option) 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "prefs.h"
#include "macros.h"

/*
 * methods
 */

#define MUIM_prefsWindow_Finish (TAGBASE_AMLAUKKA + 1)
#define MUIM_prefsWindow_About  (TAGBASE_AMLAUKKA + 2)
#define MUIM_prefsWindow_Open   (TAGBASE_AMLAUKKA + 3)
#define MUIM_prefsWindow_SaveAs (TAGBASE_AMLAUKKA + 4)
#define MUIM_prefsWindow_Restore (TAGBASE_AMLAUKKA + 5)

/*
 * internal structures
 */

struct MUIP_prefsWindow_Finish
{
	ULONG MethodID;
	LONG level;
};

struct MUIP_prefsWindow_Restore
{
	ULONG MethodID;
	LONG envarc;
};

/*
 * function prototypes
 */

static ULONG prefsWindow_NEW(struct IClass*, Object*, struct opSet*);
static ULONG prefsWindow_Finish(struct IClass*, Object*, struct MUIP_prefsWindow_Finish*);
static ULONG prefsWindow_About(struct IClass*, Object*, Msg);
static ULONG prefsWindow_Open(struct IClass*, Object*, Msg);
static ULONG prefsWindow_SaveAs(struct IClass*, Object*, Msg);
static ULONG prefsWindow_Restore(struct IClass*, Object*, struct MUIP_prefsWindow_Restore*);

/*
 * global symbols
 */

enum { MEN_OPEN = 1, MEN_SAVEAS, MEN_ABOUT, MEN_QUIT, MEN_LASTSAVED, MEN_RESTORE, MEN_MUI };

static struct NewMenu prefsMenu[] =
{
	{ NM_TITLE, (STRPTR)"Project"    ,  0, 0, 0, (APTR)0        },
	{ NM_ITEM,  (STRPTR)"Open..."    ,"O", 0, 0, (APTR)MEN_OPEN },
	{ NM_ITEM,  (STRPTR)"Save as..." ,"A", 0, 0, (APTR)MEN_SAVEAS },
	{ NM_ITEM,  (STRPTR)NM_BARLABEL  ,  0, 0, 0, (APTR)0        },
	{ NM_ITEM,  (STRPTR)"About..."   ,"?", 0, 0, (APTR)MEN_ABOUT },
	{ NM_ITEM,  (STRPTR)NM_BARLABEL  ,  0, 0, 0, (APTR)0        },
	{ NM_ITEM,  (STRPTR)"Quit"       ,"Q", 0, 0, (APTR)MEN_QUIT },

	{ NM_TITLE, (STRPTR)"Edit"       ,  0, 0, 0, (APTR)0        },
	{ NM_ITEM,  (STRPTR)"Last saved" ,"L", 0, 0, (APTR)MEN_LASTSAVED },
	{ NM_ITEM,  (STRPTR)"Restore"    ,"R", 0, 0, (APTR)MEN_RESTORE },

	{ NM_END,   NULL                 ,  0, 0, 0, (APTR)0        }
};

/*
 * functions
 */

static ULONG
prefsWindow_NEW(struct IClass* cl, Object* obj, struct opSet* msg)
{
	struct prefsWindowData* data;
	Object* BT_Save;
	Object* BT_Use;
	Object* BT_Cancel;
	Object* MN_strip;
	Object* PA_evd;

	obj = (Object*)DoSuperNew(cl, obj,
		MUIA_Window_Title,  "TurboEVDPrefs",
		MUIA_Window_ID,     MAKE_ID('T','E','P','S'),
		MUIA_Window_Menustrip, MN_strip = MUI_MakeObject(MUIO_MenustripNM, prefsMenu, 0),
		WindowContents, VGroup,
			Child, PA_evd = NewObject(CL_evdPanel->mcc_Class, NULL, TAG_DONE),
			Child, MUI_MakeObject(MUIO_HBar, 2),
			Child, HGroup, MUIA_Group_SameSize, TRUE,
				Child, BT_Save = MakeButton("_Save"),
				Child, HSpace(0),
				Child, BT_Use = MakeButton("_Use"),
				Child, HSpace(0),
				Child, BT_Cancel = MakeButton("_Cancel"),
			End,
		End,
	TAG_MORE, msg->ops_AttrList);

	if (!obj)
		return 0;

	data = INST_DATA(cl, obj);

	data->PA_evd = PA_evd;

	DoMethod(obj      ,MUIM_Notify,MUIA_Window_CloseRequest,TRUE ,obj,2,MUIM_prefsWindow_Finish,0);
	DoMethod(BT_Cancel,MUIM_Notify,MUIA_Pressed            ,FALSE,obj,2,MUIM_prefsWindow_Finish,0);
	DoMethod(BT_Use   ,MUIM_Notify,MUIA_Pressed            ,FALSE,obj,2,MUIM_prefsWindow_Finish,1);
	DoMethod(BT_Save  ,MUIM_Notify,MUIA_Pressed            ,FALSE,obj,2,MUIM_prefsWindow_Finish,2);

	DoMethod((Object*)DoMethod(MN_strip,MUIM_FindUData,MEN_ABOUT    ),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,obj,1,MUIM_prefsWindow_About);
	DoMethod((Object*)DoMethod(MN_strip,MUIM_FindUData,MEN_OPEN     ),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,obj,1,MUIM_prefsWindow_Open);
	DoMethod((Object*)DoMethod(MN_strip,MUIM_FindUData,MEN_SAVEAS   ),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,obj,1,MUIM_prefsWindow_SaveAs);
	DoMethod((Object*)DoMethod(MN_strip,MUIM_FindUData,MEN_QUIT     ),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,obj,2,MUIM_prefsWindow_Finish,0);
	DoMethod((Object*)DoMethod(MN_strip,MUIM_FindUData,MEN_LASTSAVED),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,obj,2,MUIM_prefsWindow_Restore,1);
	DoMethod((Object*)DoMethod(MN_strip,MUIM_FindUData,MEN_RESTORE  ),MUIM_Notify,MUIA_Menuitem_Trigger,MUIV_EveryTime,obj,2,MUIM_prefsWindow_Restore,0);

	set(BT_Save  ,MUIA_ShortHelp,"Exit and save\nchanges to disk.");
	set(BT_Use   ,MUIA_ShortHelp,"Exit and save changes\n for current session.");
	set(BT_Cancel,MUIA_ShortHelp,"Exit and\n\33bdiscard\33n changes.");

	DoMethod(PA_evd, MUIM_evdPanel_Load, PREFS_FILENAME_USE);

	return (ULONG)obj;
}

static ULONG
prefsWindow_Finish(struct IClass* cl, Object* obj, struct MUIP_prefsWindow_Finish* msg)
{
	struct prefsWindowData *data = INST_DATA(cl, obj);

	if(msg->level>=1) DoMethod(data->PA_evd, MUIM_evdPanel_Save,PREFS_FILENAME_USE );
	if(msg->level>=2) DoMethod(data->PA_evd, MUIM_evdPanel_Save,PREFS_FILENAME_SAVE );

	DoMethod((Object*)xget(obj, MUIA_ApplicationObject), MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);

	return 0;
}

static ULONG
prefsWindow_About(struct IClass* cl, Object* obj, Msg msg)
{
	Object* app = (Object*)xget(obj,MUIA_ApplicationObject);
	static const char about[] = "\33b\33cTurboEVDPrefs\33n\n\n\33cVersion: %s\n\33c%s";

	MUI_Request(app,obj,0,NULL,"_Okay",(char*)about,
		((char*)xget(app,MUIA_Application_Version)),
		((char*)xget(app,MUIA_Application_Copyright)));

	return 0;
}

static ULONG
prefsWindow_Open(struct IClass* cl, Object* obj, Msg msg)
{
	struct prefsWindowData* data = INST_DATA(cl, obj);
	char* name;
	if(( name = getfilename(obj, "Select a preferences file", "envarc:", "#?.prefs", FALSE)) && *name)
	{
		DoMethod(data->PA_evd, MUIM_evdPanel_CloseWindows);
		DoMethod(data->PA_evd, MUIM_evdPanel_Load, name);
	}
	return 0;
}

static ULONG
prefsWindow_SaveAs(struct IClass* cl, Object* obj, Msg msg)
{
	struct prefsWindowData* data = INST_DATA(cl, obj);
	char* name;
	if(( name = getfilename(obj, "Select the preferences file", "envarc:", "#?.prefs", TRUE)) && *name)
	{
		DoMethod(data->PA_evd, MUIM_evdPanel_Save, name);
	}
	return 0;
}

static ULONG
prefsWindow_Restore(struct IClass* cl, Object* obj, struct MUIP_prefsWindow_Restore* msg)
{
	struct prefsWindowData* data = INST_DATA(cl, obj);
	DoMethod(data->PA_evd, MUIM_evdPanel_CloseWindows);
	DoMethod(data->PA_evd, MUIM_evdPanel_Load, msg->envarc ? PREFS_FILENAME_SAVE : PREFS_FILENAME_USE);
	return 0;
}

ULONG REGARGS SAVEDS
prefsWindow_Dispatcher(REG(a0, struct IClass* cl),REG(a2, Object* obj),REG(a1, Msg msg))
{
	switch(msg->MethodID)
	{
		case OM_NEW:                    return(prefsWindow_NEW(cl, obj, (struct opSet *)msg));
		case MUIM_prefsWindow_Finish:   return(prefsWindow_Finish(cl, obj, (struct MUIP_prefsWindow_Finish *) msg));
		case MUIM_prefsWindow_About:    return(prefsWindow_About(cl, obj, (APTR) msg));
		case MUIM_prefsWindow_Open:     return(prefsWindow_Open(cl, obj, (APTR) msg));
		case MUIM_prefsWindow_SaveAs:   return(prefsWindow_SaveAs(cl, obj, (APTR) msg));
		case MUIM_prefsWindow_Restore:  return(prefsWindow_Restore(cl, obj, (struct MUIP_prefsWindow_Restore *) msg));
	}

	return DoSuperMethodA(cl, obj, msg);
}

