/** GadMXSel.c
 *
 * Routines to select/deselect gadgets. These particularly seem to work
 * reliably with gadgets with IntuiText and Borders with GADGHCOMP
 * highlighting. Jim Mackraz will probably say that they will break
 * with 1.4, but sofar I'm not worried... ;-)
 *
 * An earlier version of this appeared on Fish Disk 52, with
 * a little demo, but this is muchly improved, if I say so myself...
 *
 *                                            W.G.J. Langeveld
 *
**/
#include <exec/types.h>
#include <exec/io.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <intuition/intuition.h>
#include <stdlib.h>
#include <string.h>
#include <functions.h>

#include <simpreq.h>

/**
 *
 *   Function to select one gadget (1) and deselect others (2 - 6).
 *
**/
void GadMXSel( struct Window *win, struct Gadget *gad1, struct Gadget *gad2, struct Gadget *gad3, struct Gadget *gad4, struct Gadget *gad5, struct Gadget *gad6 )
{
	GadMXSet(win, gad1);
	GadMXClr(win, gad2);
	GadMXClr(win, gad3);
	GadMXClr(win, gad4);
	GadMXClr(win, gad5);
	GadMXClr(win, gad6);
	
	return;
}

/**
 *
 *   Function to select a gadget. Sideeffect: adds gadget to the END of the
 *   gadgetlist. Not a problem for me, and extension to put it back where
 *   it belongs is obvious, but I don't need it.
 *
**/
void GadMXSet( struct Window *win, struct Gadget *gad1 )
{
	/*
	 *   Now select gad1 and refresh.
	 */
	if (win && gad1) 
	{
		if ((gad1->Flags & SELECTED) == 0) 
		{
			RemoveGadget(win, gad1);
			gad1->Flags |= SELECTED;
			AddGadget(win, gad1, -1L);
			RefreshGadgets(gad1, win, NULL);
		}
	}
	
	return;
}

/**
 *
 *   Function to DEselect a gadget. Sideeffect: adds gadget to the END of the
 *   gadgetlist.
 *
**/
void GadMXClr( struct Window *win, struct Gadget *gad1 )
{
	/*
	 *   First select gad1 (yes!) and refresh it.
	 */
	if (win && gad1) 
	{
		if (gad1->Flags & SELECTED) 
		{
			RemoveGadget(win, gad1);
			gad1->Flags |= SELECTED;
			AddGadget(win, gad1, -1L);
			RefreshGadgets(gad1, win, NULL);
			/*
			 *   Now deselect gad1 and refresh.
			 */
			RemoveGadget(win, gad1);
			gad1->Flags &= ~SELECTED;
			AddGadget(win, gad1, -1L);
			RefreshGadgets(gad1, win, NULL);
		}
	}
	
	return;
}

/**
 *
 *   Function to toggle a gadget's select status. Side effect: adds gadget
 *   back to the END of the list.
 *
**/
void GadMXToggle( struct Window *win, struct Gadget *gad1 )
{
	if (win && gad1) 
	{
		if (gad1->Flags & SELECTED)
			GadMXClr(win, gad1);
		else
			GadMXSet(win, gad1);
	}
	
	return;
}

