/******************************************************************************

    MODUL
	prefs.c

    DESCRIPTION
	Everything to set, edit, get, load and save settings !

    NOTES

    BUGS

    TODO

    EXAMPLES

    SEE ALSO

    INDEX

    HISTORY
	14. Nov 1992	ada created

******************************************************************************/

/**************************************
		Includes
**************************************/
#include "defs.h"


/**************************************
	    Globale Variable
**************************************/
Prototype void do_margin	  (void);
Prototype void do_wordwrap	  (void);
Prototype void do_global	  (void);
Prototype void do_setparcol	  (void);
Prototype void do_tabstop	  (void);
Prototype void do_chfilename	  (void);
Prototype void do_savetabs	  (void);
Prototype void do_setfont	  (void);
Prototype void do_ignorecase	  (void);
Prototype void loadconfig	  (ED *);
Prototype void do_saveconfig	  (void);
Prototype void do_fgpen 	  (void);
Prototype void do_bgpen 	  (void);
Prototype void do_hgpen 	  (void);
Prototype void do_bbpen 	  (void);
Prototype void do_tpen		  (void);
Prototype void do_modified	  (void);
Prototype void do_autoindent	  (void);
Prototype void do_sizewindow	  (void);
Prototype void do_autosplit	  (void);
Prototype void do_taskpri	  (void);
Prototype void do_debug 	  (void);
Prototype void do_autounblock	  (void);
Prototype void do_followcursor	  (void);
Prototype void do_setlinedistance (void);


/**************************************
      Interne Defines & Strukturen
**************************************/


/**************************************
	    Interne Variable
**************************************/


/**************************************
	   Interne Prototypes
**************************************/


void do_margin (void)
{
    Ep->config.margin = atoi((char *)av[1]);
} /* do_margin */


void do_wordwrap (void)
{
    if (Ep->config.wordwrap = test_arg (av[1], Ep->config.wordwrap))
	title("Wordwrap ON");
    else
	title("Wordwrap OFF");
} /* do_wordwrap() */


void do_global (void)
{
    if (globalflags.global_search = test_arg (av[1], globalflags.global_search))
	title ("Global search ON");
    else
	title ("Global search OFF");
} /* global_search */


void do_setparcol (void)
{
    Ep->config.wwcol = atoi ((char *)av[1]);
} /* do_setparcol() */


void do_tabstop (void)
{
    Ep->config.tabstop = atoi ((char *)av[1]);
} /* do_tabstop() */


void do_insertmode (void)
{
    Ep->config.insertmode = test_arg (av[1], Ep->config.insertmode);

    window_title ();
} /* do_insertmode() */


void do_chfilename (void)
{
    BPTR    new_lock;
    UBYTE * ptr;

    text_sync ();

    getpathto (Ep->dirlock, av[1], tmp_buffer);

    ptr = FilePart (tmp_buffer);
    if (ptr != tmp_buffer)
    {
	char c = *ptr;
	*ptr = 0;

	if (new_lock = Lock (tmp_buffer, SHARED_LOCK))
	{
	    UnLock (Ep->dirlock);

	    Ep->dirlock = new_lock;
	} else
	{
	    error ("%s:\nCannot get lock for\n`%s'.", av[0], tmp_buffer);

	    return;
	}

	*ptr = c;
    }

    strncpy ((char *)Ep->name, ptr, 63);
    Ep->name[63] = 0;

    globalflags.MForceTitle = 1;
} /* do_chfilename */


void do_savetabs (void)
{
    globalflags.Savetabs = test_arg (av[1], globalflags.Savetabs);
} /* do_savetabs() */


/*
 *  SETFONT font size
 */

void do_setfont (void)
{
    FONT * font;
    ED	 * ep	= Ep;
    ULONG  size;

    size = atoi ((char *)av[2]);

    if (size > MAX_FONT_SIZE)
    {
	error ("setfont:\nSize %ld too large !", size);
	return;
    }

    font = (FONT *)GetFont ((char *)av[1], size);

    if (font)
    {
	if (ep->font)
	    CloseFont (ep->font);

	ep->font = font;

	SetFont (ep->win->RPort, font);
	SetRast (ep->win->RPort, 0);
	RefreshWindowFrame (ep->win);
	set_window_params ();
	text_adjust (TRUE);
    } else
    {
	error ("setfont:\nUnable to find font\n%s/%ld", av[1], size);
    }
} /* do_setfont */


void do_ignorecase (void)
{
    ED *ep = Ep;

    ep->config.ignorecase = test_arg (av[1], ep->config.ignorecase);

    if (ep->config.ignorecase)
	title ("Case Insensitive");
    else
	title ("Case Sensitive");
} /* do_ignorecase */


void do_saveconfig (void)
{
    ED	 * ep = Ep;
    FILE * fi;
    WIN  * win = ep->win;

   if (ep->iconmode)
   {
	ep->config.iwinx = win->LeftEdge;
	ep->config.iwiny = win->TopEdge;
   } else
   {
	ep->config.winx      = win->LeftEdge;
	ep->config.winy      = win->TopEdge;
	ep->config.winwidth  = win->Width;
	ep->config.winheight = win->Height;
   }

    if (fi = fopen (XDME_CONFIG, "w"))
    {
	fwrite (CONFIG_VERSION, sizeof(CONFIG_VERSION)-1, 1, fi);
	fwrite (&ep->beginconfig, CONFIG_SIZE, 1, fi);
	fclose (fi);
    }
} /* do_saveconfig */


void loadconfig (ED * ep)
{
    static int showed_error = 0;
    FILE * fi;

    /* Always init the fields */
    movmem (&default_config, &ep->beginconfig, CONFIG_SIZE);

    if (fi = fopen (XDME_CONFIG, "r"))
    {
	fread (tmp_buffer, sizeof(CONFIG_VERSION)-1, 1, fi);

	if (strncmp (CONFIG_VERSION, tmp_buffer, sizeof(CONFIG_VERSION)-1) )
	{
	    if (!showed_error)
	    {
		error ("loadconfig:\n"
			"Wrong version for\n"
			"configuration file !\n"
			"Using defaults");
		showed_error = 1;
	    }
	} else
	    fread (&ep->beginconfig, 1, CONFIG_SIZE, fi);

	fclose (fi);
    }
} /* loadconfig */


void do_fgpen (void)
{
    int pen;

    pen = atoi((char *)av[1]);

    if (pen != TEXT_BPEN)
    {
	TEXT_FPEN = pen;
	text_redisplay ();
    } else
	error ("fgpen:\n"
	       "Cannot choose same color\n"
	       "for both back- and fore-\n"
	       "ground");
} /* do_fgpen */


void do_tpen (void)
{
    int pen;

    pen = atoi((char *)av[1]);

    if (pen != TEXT_FPEN)
    {
	TITLE_BPEN = pen;
	text_redisplay ();
    } else
	error ("tpen:\n"
	       "Cannot choose same color\n"
	       "for both back- and fore-\n"
	       "ground");
} /* do_tpen */


void do_bgpen (void)
{
    int pen;

    pen = atoi((char *)av[1]);

    if (pen != TEXT_FPEN)
    {
	TEXT_BPEN = pen;
	text_redisplay ();
    } else
	error ("bgpen:\n"
	       "Cannot choose same color\n"
	       "for both back- and fore-\n"
	       "ground");
} /* do_bgpen */


void do_hgpen (void)
{
    int pen;

    pen = atoi((char *)av[1]);

    if (pen != BLOCK_BPEN)
    {
	BLOCK_FPEN = pen;

	if (block_ok ())
	    text_redisplay ();
    } else
	error ("hgpen:\n"
	       "Cannot choose same color\n"
	       "for both back- and fore-\n"
	       "ground");
} /* do_hgpen */


void do_bbpen (void)
{
    int pen;

    pen = atoi((char *)av[1]);

    if (pen != BLOCK_FPEN)
    {
	BLOCK_BPEN = pen;

	if (block_ok ())
	    text_redisplay ();
    } else
	error ("bbpen:\n"
	       "Cannot choose same color\n"
	       "for both back- and fore-\n"
	       "ground");
} /* do_hgpen */


void do_modified (void)
{
    register ED *ep = Ep;

    if (av[1][0])
    {
	switch(av[1][1] & 0x1F)
	{
	case 'n' & 0x1F:
	    ep->modified = 1;
	    break;
	case 'f' & 0x1F:
	    ep->modified = 0;
	    break;
	case 'o' & 0x1F:
	    ep->modified = ep->modified ? 0 : 1;
	    break;
	}
    }
} /* do_modified */


void do_autoindent (void)
{
    Ep->config.autoindent = test_arg ((char *)av[1], Ep->config.autoindent);

    if (Ep->config.autoindent)
	title ("Autoindent ON");
    else
	title ("Autoindent OFF");
} /* do_autoindent */


void do_sizewindow (void)
{
    WIN * win = Ep->win;
    struct NewWindow nw;
    int mdx, mdy;

    GeometryToNW (av[1], &nw);

    if (nw.LeftEdge + nw.Width <= win->WScreen->Width &&
	    nw.TopEdge + nw.Height <= win->WScreen->Height &&
	    nw.Width >= win->MinWidth &&
	    nw.Height >= win->MinHeight)
	    {

	mdx = nw.LeftEdge - win->LeftEdge;
	mdy = nw.TopEdge - win->TopEdge;

	if (mdx > 0)
	    mdx = 0;

	if (mdy > 0)
	    mdy = 0;

	MoveWindow (win, mdx, mdy);
	SizeWindow (win, nw.Width - win->Width, nw.Height - win->Height);
	MoveWindow (win, nw.LeftEdge - win->LeftEdge, nw.TopEdge - win->TopEdge);
    }
}


void do_autosplit (void)
{
    Ep->config.autosplit = test_arg (av[1], Ep->config.autosplit);

    if (Ep->config.autosplit)
	title ("AutoSplit is now ON");
    else
	title ("AutoSplit is now OFF");
} /* do_autosplit */


void do_taskpri (void)
{
    LONG pri;

    pri = atoi (av[1]);

    if (pri > 5 || pri < -5)
    {
	error ("taskpri:\nWrong TaskPri %ld. Taskpri must\nbe between -5 and 5 !", pri);
	return ;
    }

    SetTaskPri (FindTask(NULL), pri);
} /* do_taskpri */


void do_debug (void)
{
    if (globalflags.debug = test_arg (av[1], globalflags.debug))
	title ("DEBUG is now ON");
    else
	title ("DEBUG is now OFF");
} /* do_debug */


/*****************************************************************************

    NAME

    PARAMETER

    RETURN

    DESCRIPTION

******************************************************************************/

void do_autounblock (void)
{
    Ep->config.autounblock = test_arg (av[1], Ep->config.autounblock);
} /* do_autounblock */


/*****************************************************************************

    NAME

    PARAMETER

    RETURN

    DESCRIPTION

******************************************************************************/

void do_followcursor (void)
{
    globalflags.FollowCursor = test_arg (av[1], globalflags.FollowCursor);
} /* do_followcursor */


/*****************************************************************************

    NAME
	do_setlinedistance

    PARAMETER
	av[1] : integer offset (>= 0)

    DESCRIPTION
	Sets the LineDistance. The offset must be positive.

******************************************************************************/

void do_setlinedistance (void)
{
    int offset = strtol (av[1], NULL, 0);

    if (offset < 0)
	error ("%s:\nYou cannot set a negative\nspace between the lines", av[0]);
    else
    {
	ED   * ep	 = Ep;
	RP   * rp	 = ep->win->RPort;
	UWORD old_Xpixs = Xpixs;
	UWORD old_Ypixs = Ypixs;

	LineDistance = offset;

	set_window_params ();       /* recalc new offsets */

	if (old_Xpixs != Xpixs || old_Ypixs != Ypixs)
	{
	    UWORD x1, x2, y1, y2;

	    /* Prepare for clear */

	    SetAPen (rp, TEXT_BPEN);
	    SetWrMsk (rp, BLOCK_MASK);

	    /* #1 is lower, #2 is higher coordinate */

	    if (old_Xpixs < Xpixs)
	    {
		x1 = old_Xpixs;
		x2 = Xpixs;
	    } else
	    {
		x2 = old_Xpixs;
		x1 = Xpixs;
	    }

	    if (old_Ypixs < Ypixs)
	    {
		y1 = old_Ypixs;
		y2 = Ypixs;
	    } else
	    {
		y2 = old_Ypixs;
		y1 = Ypixs;
	    }

	    /* clear obsolete areas */

	    if (x1 != x2)
		RectFill (rp, x1, Ybase, x2, y2);

	    if (y1 != y2)
		RectFill (rp, Xbase, y1, x2, y2);
	}

	text_adjust (TRUE);
    }
} /* do_setlinedistance */


/******************************************************************************
*****  ENDE prefs.c
******************************************************************************/
