/********************************************************************/
/*                                                                  */
/*            L   I  QQ  U U I DD    W   W  A  RR    555            */
/*            L   I Q  Q U U I D D   W   W A A R R   5              */
/*            L   I Q  Q U U I D D   W W W AAA RR    55             */
/*            L   I Q Q  U U I D D   WW WW A A R R     5            */
/*            LLL I  Q Q  U  I DD    W   W A A R R   55             */
/*                                                                  */
/*                             b                                    */
/*                             bb  y y                              */
/*                             b b yyy                              */
/*                             bb    y                              */
/*                                 yy                               */
/*                                                                  */
/*                     U U       FFF  O   O  TTT                    */
/*                     U U       F   O O O O  T                     */
/*                     U U TIRET FF  O O O O  T                     */
/*                     U U       F   O O O O  T                     */
/*                      U        F    O   O   T                     */
/*                                                                  */
/********************************************************************/

/*****************************************************************************/
/* Liquid War is a multiplayer wargame                                       */
/* Copyright (C) 1998-2004 Christian Mauduit                                 */
/*                                                                           */
/* 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 */
/*                                                                           */
/* Liquid War homepage : http://www.ufoot.org/liquidwar                      */
/* Contact author      : ufoot@ufoot.org                                     */
/*****************************************************************************/

/********************************************************************/
/* nom           : alleg2.c                                         */
/* contenu       : reprogrammation de qq fonctions d'allegro        */
/* date de modif : 3 mai 98                                         */
/********************************************************************/

/*==================================================================*/
/* includes                                                         */
/*==================================================================*/

#include <string.h>

#include "alleg2.h"
#include "exit.h"
#include "macro.h"

/*==================================================================*/
/* variables globales                                               */
/*==================================================================*/

/* typedef for the listbox callback functions */
typedef char *(*my_getfuncptr) (int, int *);

/*==================================================================*/
/* variables statiques                                              */
/*==================================================================*/
extern void _draw_scrollable_frame (DIALOG * d,
				    int listsize, int offset, int height,
				    int fg_color, int bg);
extern int isspace (int c);

/*==================================================================*/
/* fonctions                                                        */
/*==================================================================*/

/*------------------------------------------------------------------*/
/* utilitaires                                                      */
/*------------------------------------------------------------------*/

static void
dotted_rect (int x1, int y1, int x2, int y2, int fg, int bg)
{
  int x = ((x1 + y1) & 1) ? 1 : 0;
  int c;

  for (c = x1; c <= x2; c++)
    {
      putpixel (screen, c, y1, (((c + y1) & 1) == x) ? fg : bg);
      putpixel (screen, c, y2, (((c + y2) & 1) == x) ? fg : bg);
    }

  for (c = y1 + 1; c < y2; c++)
    {
      putpixel (screen, x1, c, (((c + x1) & 1) == x) ? fg : bg);
      putpixel (screen, x2, c, (((c + x2) & 1) == x) ? fg : bg);
    }
}

/* _draw_textbox:
 *  Helper function to draw a textbox object.
 */
static void my_draw_textbox
  (char *thetext, int *listsize, int draw, int offset,
   int wword, int tabsize, int x, int y, int w, int h,
   int disabled, int fore, int deselect, int disable)
{
  int fg = fore;
  int y1 = y + 4;
  int x1;
  int len;
  int ww = w - 4;
  char s[16];
  char text[16];
  char space[16];
  char *printed = text;
  char *scanned = text;
  char *oldscan = text;
  char *ignore = NULL;
  char *tmp, *ptmp;
  int width;
  int line = 0;
  int i = 0;

  usetc (s + usetc (s, '.'), 0);
  usetc (text + usetc (text, ' '), 0);
  usetc (space + usetc (space, ' '), 0);

  /* find the correct text */
  if (thetext != NULL)
    {
      printed = thetext;
      scanned = thetext;
    }

  /* do some drawing setup */
  if (draw)
    {
      /* initial start blanking at the top */
      rectfill (screen, x + 2, y + 2, x + w - 1, y1 - 1, deselect);
    }

  /* choose the text color */
  if (disabled)
    fg = disable;

  text_mode (deselect);

  /* loop over the entire string */
  while (1)
    {
      width = 0;

      /* find the next break */
      while (ugetc (scanned))
	{
	  /* check for a forced break */
	  if (ugetc (scanned) == '\n')
	    {
	      scanned += uwidth (scanned);

	      /* we are done parsing the line end */
	      break;
	    }

	  /* the next character length */
	  usetc (s + usetc (s, ugetc (scanned)), 0);
	  len = text_length (font, s);

	  /* modify length if its a tab */
	  if (ugetc (s) == '\t')
	    len = tabsize * text_length (font, space);

	  /* check for the end of a line by excess width of next char */
	  if (width + len >= ww)
	    {
	      /* we have reached end of line do we go back to find start */
	      if (wword)
		{
		  /* remember where we were */
		  oldscan = scanned;

		  /* go backwards looking for start of word */
		  while (!uisspace (ugetc (scanned)))
		    {
		      /* don't wrap too far */
		      if (scanned == printed)
			{
			  /* the whole line is filled, so stop here */
			  scanned = oldscan;
			  break;
			}
		      /* look further backwards to wrap */
		      tmp = ptmp = printed;
		      while (tmp < scanned)
			{
			  ptmp = tmp;
			  tmp += uwidth (tmp);
			}
		      scanned = ptmp;
		    }
		  /* put the space at the end of the line */
		  ignore = scanned;
		  scanned += uwidth (scanned);

		  /* check for endline at the convenient place */
		  if (ugetc (scanned) == '\n')
		    scanned += uwidth (scanned);
		}
	      /* we are done parsing the line end */
	      break;
	    }
	  /* the character can be added */
	  scanned += uwidth (scanned);
	  width += len;
	}

      /* check if we are to print it */
      if ((draw) && (line >= offset)
	  && (y1 + text_height (font) < (y + h - 1)))
	{
	  x1 = x + 4;

	  /* the initial blank bit */
	  rectfill (screen, x + 2, y1, x1 - 1, y1 + text_height (font),
		    deselect);

	  /* print up to the marked character */
	  while (printed != scanned)
	    {
	      /* do special stuff for each charater */
	      switch (ugetc (printed))
		{

		case '\r':
		case '\n':
		  /* don't print endlines in the text */
		  break;

		  /* possibly expand the tabs */
		case '\t':
		  for (i = 0; i < tabsize; i++)
		    {
		      usetc (s + usetc (s, ' '), 0);
		      textout (screen, font, s, x1, y1, -1);
		      x1 += text_length (font, s);
		    }
		  break;

		  /* print a normal charater */
		default:
		  if (printed != ignore)
		    {
		      usetc (s + usetc (s, ugetc (printed)), 0);
		      textout (screen, font, s, x1, y1, -1);
		      x1 += text_length (font, s);
		    }
		}

	      /* goto the next character */
	      printed += uwidth (printed);
	    }
	  /* the last blank bit */
	  if (x1 < x + w - 1)
	    rectfill (screen, x1, y1, x + w - 1, y1 + text_height (font) - 1,
		      deselect);

	  /* print the line end */
	  y1 += text_height (font);
	}
      printed = scanned;

      /* we have done a line */
      line++;

      /* check if we are at the end of the string */
      if (!ugetc (printed))
	{
	  /* the under blank bit */
	  if (draw)
	    rectfill (screen, x + 1, y1, x + w - 1, y + h - 1, deselect);

	  /* tell how many lines we found */
	  *listsize = line;
	  return;
	}
    }
}



/* d_textbox_proc:
 *  A text box object. The dp field points to a char * which is the text
 *  to be displayed in the text box. If the text is long, there will be
 *  a vertical scrollbar on the right hand side of the object which can
 *  be used to scroll through the text. The default is to print the text
 *  with word wrapping, but if the D_SELECTED flag is set, the text will
 *  be printed with character wrapping. The d1 field is used internally
 *  to store the number of lines of text, and d2 is used to store how far
 *  it has scrolled through the text.
 */
int
my_textbox_proc (int msg, DIALOG * d, int c)
{
  int height, bar;
  int fg_color = (d->flags & D_DISABLED) ? gui_mg_color : d->fg;

  /* calculate the actual height */
  height = (d->h - 4) / text_height (font);

  if (msg == MSG_DRAW)
    {
      /* tell the object to sort of draw, but only calculate the listsize */
      my_draw_textbox (d->dp, &d->d1, 0,	/* DONT DRAW anything */
		       d->d2, !(d->flags & D_SELECTED), 8,
		       d->x, d->y, d->w, d->h,
		       (d->flags & D_DISABLED), 0, 0, 0);

      if (d->d1 > height)
	{
	  bar = 12;
	}
      else
	{
	  bar = 0;
	  d->d2 = 0;
	}

      /* now do the actual drawing */
      my_draw_textbox (d->dp, &d->d1, 1, d->d2,
		       !(d->flags & D_SELECTED), 8,
		       d->x, d->y, d->w - bar - 1, d->h,
		       (d->flags & D_DISABLED),
		       fg_color, d->bg, gui_mg_color);

      /* draw the frame around */
      _draw_scrollable_frame (d, d->d1, d->d2, height, fg_color, d->bg);
      return D_O_K;
    }
  else
    return d_textbox_proc (msg, d, c);
}

int
my_button_proc (int msg, DIALOG * d, int c)
{
  int state1, state2;
  int g;

  if (msg == MSG_DRAW)
    {
      if (d->flags & D_SELECTED)
	{
	  g = 1;
	  state1 = d->bg;
	  state2 = (d->flags & D_DISABLED) ? gui_mg_color : d->fg;
	}
      else
	{
	  g = 0;
	  state1 = (d->flags & D_DISABLED) ? gui_mg_color : d->fg;
	  state2 = d->bg;
	}

      rectfill (screen, d->x + 1 + g, d->y + 1 + g, d->x + d->w - 2 + g,
		d->y + d->h - 2 + g, state2);
      rect (screen, d->x + g, d->y + g, d->x + d->w - 1 + g,
	    d->y + d->h - 1 + g, state1);
      text_mode (-1);
      gui_textout (screen, d->dp, d->x + d->w / 2 + g, d->y + d->h / 2 - text_height (font) / 2 + g,	/*state1 */
		   -1, TRUE);

      if (d->flags & D_SELECTED)
	{
	  vline (screen, d->x, d->y, d->y + d->h - 1, d->bg);
	  hline (screen, d->x, d->y, d->x + d->w - 1, d->bg);
	}
      else
	{
	  vline (screen, d->x + d->w, d->y + 1, d->y + d->h - 1, d->fg);
	  hline (screen, d->x + 1, d->y + d->h, d->x + d->w, d->fg);
	}
      if ((d->flags & D_GOTFOCUS) &&
	  (!(d->flags & D_SELECTED) || !(d->flags & D_EXIT)))
	dotted_rect (d->x + 1 + g, d->y + 1 + g, d->x + d->w - 2 + g,
		     d->y + d->h - 2 + g, state1, state2);
      return D_O_K;
    }
  else
    return d_button_proc (msg, d, c);
}

/* d_text_proc:
 *  Simple dialog procedure: draws the text string which is pointed to by dp.
 */
int
my_text_proc (int msg, DIALOG * d, int c)
{
  LW_MACRO_NOP (c);

  if (msg == MSG_DRAW)
    {
      text_mode (-1);
      gui_textout (screen, d->dp, d->x, d->y, -1, FALSE);
    }

  return D_O_K;
}

/* d_ctext_proc:
 *  Simple dialog procedure: draws the text string which is pointed to by dp,
 *  centering it around the object's x coordinate.
 */
int
my_ctext_proc (int msg, DIALOG * d, int c)
{
  LW_MACRO_NOP (c);

  if (msg == MSG_DRAW)
    {
      text_mode (-1);
      gui_textout (screen, d->dp, d->x, d->y, -1, TRUE);
    }

  return D_O_K;
}


/* d_slider_proc:
 *  A slider control object. This object returns a value in d2, in the
 *  range from 0 to d1. It will display as a vertical slider if h is
 *  greater than or equal to w; otherwise, it will display as a horizontal
 *  slider. dp can contain an optional bitmap to use for the slider handle; 
 *  dp2 can contain an optional callback function, which is called each 
 *  time d2 changes. The callback function should have the following
 *  prototype:
 *
 *  int function(void *dp3, int d2);
 *
 *  The d_slider_proc object will return the value of the callback function.
 */
int
my_slider_proc (int msg, DIALOG * d, int c)
{
  BITMAP *slhan = NULL;
  int oldpos, newpos;
  int sfg;			/* slider foreground color */
  int vert = TRUE;		/* flag: is slider vertical? */
  int hh = 7;			/* handle height (width for horizontal sliders) */
  int hmar;			/* handle margin */
  int slp;			/* slider position */
  int mp;			/* mouse position */
  int irange;
  int slx, sly, slh, slw;
  int msx, msy;
  int retval = D_O_K;
  int upkey, downkey;
  int pgupkey, pgdnkey;
  int homekey, endkey;
  int delta;
  fixed slratio, slmax, slpos;
  int (*proc) (void *cbpointer, int d2value);

  /* check for slider direction */
  if (d->h < d->w)
    vert = FALSE;

  /* set up the metrics for the control */
  if (d->dp != NULL)
    {
      slhan = (BITMAP *) d->dp;
      if (vert)
	hh = slhan->h;
      else
	hh = slhan->w;
    }

  hmar = hh / 2;
  irange = (vert) ? d->h : d->w;
  slmax = itofix (irange - hh);
  slratio = slmax / (d->d1);
  slpos = slratio * d->d2;
  slp = fixtoi (slpos);

  switch (msg)
    {

    case MSG_DRAW:
      sfg = (d->flags & D_DISABLED) ? gui_mg_color : d->fg;

      if (vert)
	{
	  rectfill (screen, d->x, d->y, d->x + d->w / 2 - 2, d->y + d->h,
		    d->bg);
	  rectfill (screen, d->x + d->w / 2 - 1, d->y, d->x + d->w / 2 + 1,
		    d->y + d->h, sfg);
	  rectfill (screen, d->x + d->w / 2 + 2, d->y, d->x + d->w,
		    d->y + d->h, d->bg);
	}
      else
	{
	  rectfill (screen, d->x, d->y, d->x + d->w, d->y + d->h / 2 - 2,
		    d->bg);
	  rectfill (screen, d->x, d->y + d->h / 2 - 1, d->x + d->w,
		    d->y + d->h / 2 + 1, sfg);
	  rectfill (screen, d->x, d->y + d->h / 2 + 2, d->x + d->w,
		    d->y + d->h, d->bg);
	}

      rect (screen, d->x, d->y, d->x + d->w, d->y + d->h, sfg);
      /* ligne du desssus ajoutee par moi pour le cadre */

      /* okay, background and slot are drawn, now draw the handle */
      if (slhan)
	{
	  if (vert)
	    {
	      slx = d->x + (d->w / 2) - (slhan->w / 2);
	      sly = d->y + d->h - (hh + slp);
	    }
	  else
	    {
	      slx = d->x + slp;
	      sly = d->y + (d->h / 2) - (slhan->h / 2);
	    }
	  draw_sprite (screen, slhan, slx, sly);
	}
      else
	{
	  /* draw default handle */
	  if (vert)
	    {
	      slx = d->x;
	      sly = d->y + d->h - (hh + slp);
	      slw = d->w;
	      slh = hh;
	    }
	  else
	    {
	      slx = d->x + slp;
	      sly = d->y;
	      slw = hh;
	      slh = d->h;
	    }

	  /* draw body */
	  rectfill (screen, slx + 2, sly, slx + (slw - 2), sly + slh, sfg);
	  vline (screen, slx + 1, sly + 1, sly + slh - 1, sfg);
	  vline (screen, slx + slw - 1, sly + 1, sly + slh - 1, sfg);
	  vline (screen, slx, sly + 2, sly + slh - 2, sfg);
	  vline (screen, slx + slw, sly + 2, sly + slh - 2, sfg);
	  vline (screen, slx + 1, sly + 2, sly + slh - 2, d->bg);
	  hline (screen, slx + 2, sly + 1, slx + slw - 2, d->bg);
	  putpixel (screen, slx + 2, sly + 2, d->bg);
	}

      if (d->flags & D_GOTFOCUS)
	dotted_rect (d->x + 1, d->y + 1, d->x + d->w - 1, d->y + d->h - 1,
		     sfg, d->bg);
      /* coord du dotted rect modifiee par moi , cad +1 et -2 */
      break;

    case MSG_WANTFOCUS:
    case MSG_LOSTFOCUS:
      return D_WANTFOCUS;

    case MSG_KEY:
      if (!(d->flags & D_GOTFOCUS))
	return D_WANTFOCUS;
      else
	return D_O_K;

    case MSG_CHAR:
      /* handle movement keys to move slider */
      c >>= 8;

      if (vert)
	{
	  upkey = KEY_UP;
	  downkey = KEY_DOWN;
	  pgupkey = KEY_PGUP;
	  pgdnkey = KEY_PGDN;
	  homekey = KEY_END;
	  endkey = KEY_HOME;
	}
      else
	{
	  upkey = KEY_RIGHT;
	  downkey = KEY_LEFT;
	  pgupkey = KEY_PGDN;
	  pgdnkey = KEY_PGUP;
	  homekey = KEY_HOME;
	  endkey = KEY_END;
	}

      if (c == upkey)
	delta = 1;
      else if (c == downkey)
	delta = -1;
      else if (c == pgdnkey)
	delta = -d->d1 / 16;
      else if (c == pgupkey)
	delta = d->d1 / 16;
      else if (c == homekey)
	delta = -d->d2;
      else if (c == endkey)
	delta = d->d1 - d->d2;
      else
	delta = 0;

      if (delta)
	{
	  oldpos = slp;

	  while (1)
	    {
	      d->d2 = d->d2 + delta;
	      slpos = slratio * d->d2;
	      slp = fixtoi (slpos);
	      if ((slp != oldpos) || (d->d2 <= 0) || (d->d2 >= d->d1))
		break;
	    }

	  if (d->d2 < 0)
	    d->d2 = 0;
	  if (d->d2 > d->d1)
	    d->d2 = d->d1;

	  retval = D_USED_CHAR;

	  /* call callback function here */
	  if (d->dp2)
	    {
	      proc = d->dp2;
	      retval |= (*proc) (d->dp3, d->d2);
	    }

	  show_mouse (NULL);
	  SEND_MESSAGE (d, MSG_DRAW, 0);
	  show_mouse (screen);
	}
      break;

    case MSG_CLICK:
      /* track the mouse until it is released */
      mp = slp;

      while (mouse_b)
	{
	  msx = mouse_x;
	  msy = mouse_y;
	  oldpos = d->d2;
	  if (vert)
	    mp = (d->y + d->h - hmar) - msy;
	  else
	    mp = msx - (d->x + hmar);
	  if (mp < 0)
	    mp = 0;
	  if (mp > irange - hh)
	    mp = irange - hh;
	  slpos = itofix (mp);
	  slmax = fdiv (slpos, slratio);
	  newpos = fixtoi (slmax);
	  if (newpos != oldpos)
	    {
	      d->d2 = newpos;

	      /* call callback function here */
	      if (d->dp2 != NULL)
		{
		  proc = d->dp2;
		  retval |= (*proc) (d->dp3, d->d2);
		}

	      if (d->d2 != oldpos)
		{
		  show_mouse (NULL);
		  SEND_MESSAGE (d, MSG_DRAW, 0);
		  show_mouse (screen);
		}
	    }

	  /* let other objects continue to animate */
	  broadcast_dialog_message (MSG_IDLE, 0);
	}
      break;
    }

  return retval;
}

/* d_edit_proc:
 *  An editable text object (the dp field points to the string). When it
 *  has the input focus (obtained by clicking on it with the mouse), text
 *  can be typed into this object. The d1 field specifies the maximum
 *  number of characters that it will accept, and d2 is the text cursor 
 *  position within the string.
 */
int
my_edit_proc (int msg, DIALOG * d, int c)
{
  static int ignore_next_uchar = FALSE;
  int last_was_space, new_pos, i, k;
  int f, l, p, w, x, fg, b, scroll;
  char buf[16];
  char *s, *t;
  int rtm;

  s = d->dp;
  l = ustrlen (s);
  if (d->d2 > l)
    d->d2 = l;

  /* calculate maximal number of displayable characters */
  if (d->d2 == l)
    {
      usetc (buf + usetc (buf, ' '), 0);
      x = text_length (font, buf);
    }
  else
    x = 0;

  b = 0;

  for (p = d->d2; p >= 0; p--)
    {
      usetc (buf + usetc (buf, ugetat (s, p)), 0);
      x += text_length (font, buf);
      b++;
      if (x > d->w)
	break;
    }

  if (x <= d->w)
    {
      b = l;
      scroll = FALSE;
    }
  else
    {
      b--;
      scroll = TRUE;
    }

  switch (msg)
    {

    case MSG_START:
      d->d2 = l;
      break;

    case MSG_DRAW:
      fg = (d->flags & D_DISABLED) ? gui_mg_color : d->fg;
      x = 0;

      if (scroll)
	{
	  p = d->d2 - b + 1;
	  b = d->d2;
	}
      else
	p = 0;

      for (; p <= b; p++)
	{
	  f = ugetat (s, p);
	  usetc (buf + usetc (buf, (f) ? f : ' '), 0);
	  w = text_length (font, buf);
	  if (x + w > d->w)
	    break;
	  f = ((p == d->d2) && (d->flags & D_GOTFOCUS));
	  rtm = text_mode ((f) ? fg : d->bg);
	  textout (screen, font, buf, d->x + x, d->y, (f) ? d->bg : fg);
	  /*
	   * Now we redraw with color -1 to use the font's native look
	   */
	  text_mode (-1);
	  textout (screen, font, buf, d->x + x, d->y, -1);
	  /*
	   * End of the ugly patch
	   */
	  text_mode (rtm);
	  x += w;
	}
      if (x < d->w)
	rectfill (screen, d->x + x, d->y, d->x + d->w - 1,
		  d->y + text_height (font) - 1, d->bg);
      break;

    case MSG_CLICK:
      x = d->x;

      if (scroll)
	{
	  p = d->d2 - b + 1;
	  b = d->d2;
	}
      else
	p = 0;

      for (; p < b; p++)
	{
	  usetc (buf + usetc (buf, ugetat (s, p)), 0);
	  x += text_length (font, buf);
	  if (x > gui_mouse_x ())
	    break;
	}
      d->d2 = MID (0, p, l);
      scare_mouse ();
      object_message (d, MSG_DRAW, 0);
      unscare_mouse ();
      break;

    case MSG_WANTFOCUS:
    case MSG_LOSTFOCUS:
    case MSG_KEY:
      return D_WANTFOCUS;

    case MSG_CHAR:
      ignore_next_uchar = FALSE;

      if ((c >> 8) == KEY_LEFT)
	{
	  if (d->d2 > 0)
	    {
	      if (key_shifts & KB_CTRL_FLAG)
		{
		  last_was_space = TRUE;
		  new_pos = 0;
		  t = s;
		  for (i = 0; i < d->d2; i++)
		    {
		      k = ugetx (&t);
		      if (uisspace (k))
			last_was_space = TRUE;
		      else if (last_was_space)
			{
			  last_was_space = FALSE;
			  new_pos = i;
			}
		    }
		  d->d2 = new_pos;
		}
	      else
		d->d2--;
	    }
	}
      else if ((c >> 8) == KEY_RIGHT)
	{
	  if (d->d2 < l)
	    {
	      if (key_shifts & KB_CTRL_FLAG)
		{
		  t = s + uoffset (s, d->d2);
		  for (k = ugetx (&t); uisspace (k); k = ugetx (&t))
		    d->d2++;
		  for (; k && !uisspace (k); k = ugetx (&t))
		    d->d2++;
		}
	      else
		d->d2++;
	    }
	}
      else if ((c >> 8) == KEY_HOME)
	{
	  d->d2 = 0;
	}
      else if ((c >> 8) == KEY_END)
	{
	  d->d2 = l;
	}
      else if ((c >> 8) == KEY_DEL)
	{
	  if (d->d2 < l)
	    uremove (s, d->d2);
	}
      else if ((c >> 8) == KEY_BACKSPACE)
	{
	  if (d->d2 > 0)
	    {
	      d->d2--;
	      uremove (s, d->d2);
	    }
	}
      else if ((c >> 8) == KEY_ENTER)
	{
	  if (d->flags & D_EXIT)
	    {
	      scare_mouse ();
	      object_message (d, MSG_DRAW, 0);
	      unscare_mouse ();
	      return D_CLOSE;
	    }
	  else
	    return D_O_K;
	}
      else if ((c >> 8) == KEY_TAB)
	{
	  ignore_next_uchar = TRUE;
	  return D_O_K;
	}
      else
	{
	  /* don't process regular keys here: MSG_UCHAR will do that */
	  break;
	}
      scare_mouse ();
      object_message (d, MSG_DRAW, 0);
      unscare_mouse ();
      return D_USED_CHAR;

    case MSG_UCHAR:
      if ((c >= ' ') && (uisok (c)) && (!ignore_next_uchar))
	{
	  if (l < d->d1)
	    {
	      uinsert (s, d->d2, c);
	      d->d2++;

	      scare_mouse ();
	      object_message (d, MSG_DRAW, 0);
	      unscare_mouse ();
	    }
	  return D_USED_CHAR;
	}
      break;
    }

  return D_O_K;
}


/* _handle_scrollable_click:
 *  Helper to process a click on a scrollable object.
 */
void
my_handle_scrollable_scroll_click (DIALOG * d, int listsize, int *offset,
				   int height)
{
  int xx, yy;
  int hh = d->h - 5;

  while (gui_mouse_b ())
    {
      int i = (hh * height + listsize / 2) / listsize;
      int len = (hh * (*offset) + listsize / 2) / listsize + 2;

      if ((gui_mouse_y () >= d->y + len)
	  && (gui_mouse_y () <= d->y + len + i))
	{
	  xx = gui_mouse_y () - len + 2;
	  while (gui_mouse_b ())
	    {
	      yy = (listsize * (gui_mouse_y () - xx) + hh / 2) / hh;
	      if (yy > listsize - height)
		yy = listsize - height;

	      if (yy < 0)
		yy = 0;

	      if (yy != *offset)
		{
		  *offset = yy;
		  scare_mouse ();
		  object_message (d, MSG_DRAW, 0);
		  unscare_mouse ();
		}

	      /* let other objects continue to animate */
	      broadcast_dialog_message (MSG_IDLE, 0);
	    }
	}
      else
	{
	  if (gui_mouse_y () <= d->y + len)
	    yy = *offset - height;
	  else
	    yy = *offset + height;

	  if (yy > listsize - height)
	    yy = listsize - height;

	  if (yy < 0)
	    yy = 0;

	  if (yy != *offset)
	    {
	      *offset = yy;
	      scare_mouse ();
	      object_message (d, MSG_DRAW, 0);
	      unscare_mouse ();
	    }
	}

      /* let other objects continue to animate */
      broadcast_dialog_message (MSG_IDLE, 0);
    }
}



/* _handle_scrollable_scroll:
 *  Helper function to scroll through a scrollable object.
 */
void
my_handle_scrollable_scroll (DIALOG * d, int listsize, int *index,
			     int *offset)
{
  int height = (d->h - 4) / text_height (font);

  if (listsize <= 0)
    {
      *index = *offset = 0;
      return;
    }

  /* check selected item */
  if (*index < 0)
    *index = 0;
  else if (*index >= listsize)
    *index = listsize - 1;

  /* check scroll position */
  while ((*offset > 0) && (*offset + height > listsize))
    (*offset)--;

  if (*offset >= *index)
    {
      if (*index < 0)
	*offset = 0;
      else
	*offset = *index;
    }
  else
    {
      while ((*offset + height - 1) < *index)
	(*offset)++;
    }
}



/* idle_cb:
 *  rest_callback() routine to keep dialogs animating nice and smoothly.
 */
static void
my_idle_cb (void)
{
  broadcast_dialog_message (MSG_IDLE, 0);
}



/* _handle_listbox_click:
 *  Helper to process a click on a listbox, doing hit-testing and moving
 *  the selection.
 */
void
my_handle_listbox_click (DIALOG * d)
{
  char *sel = d->dp2;
  int listsize, height;
  int i, j;

  (*(my_getfuncptr) d->dp) (-1, &listsize);
  if (!listsize)
    return;

  height = (d->h - 4) / text_height (font);

  i = MID (0, ((gui_mouse_y () - d->y - 2) / text_height (font)),
	   ((d->h - 4) / text_height (font) - 1));
  i += d->d2;
  if (i < d->d2)
    i = d->d2;
  else
    {
      if (i > d->d2 + height - 1)
	i = d->d2 + height - 1;
      if (i >= listsize)
	i = listsize - 1;
    }

  if (gui_mouse_y () <= d->y)
    i = MAX (i - 1, 0);
  else if (gui_mouse_y () >= d->y + d->h - 1)
    i = MIN (i + 1, listsize - 1);

  if (i != d->d1)
    {
      if (sel)
	{
	  if (key_shifts & (KB_SHIFT_FLAG | KB_CTRL_FLAG))
	    {
	      if ((key_shifts & KB_SHIFT_FLAG) || (d->flags & D_INTERNAL))
		{
		  for (j = MIN (i, d->d1); j <= MAX (i, d->d1); j++)
		    sel[j] = TRUE;
		}
	      else
		sel[i] = !sel[i];
	    }
	}

      d->d1 = i;
      i = d->d2;
      my_handle_scrollable_scroll (d, listsize, &d->d1, &d->d2);

      d->flags |= D_DIRTY;

      if (i != d->d2)
	rest_callback (MID (10, text_height (font) * 16 - d->h - 1, 100),
		       my_idle_cb);
    }
}



/* draw_listbox:
 *  Helper function to draw a listbox object.
 */
void
my_draw_listbox (DIALOG * d)
{
  int height, listsize, i, len, bar, x, y, w;
  int fg_color, fg, bg;
  char *sel = d->dp2;
  char s[1024];
  int rtm;

  (*(my_getfuncptr) d->dp) (-1, &listsize);
  height = (d->h - 4) / text_height (font);
  bar = (listsize > height);
  w = (bar ? d->w - 15 : d->w - 3);
  fg_color = (d->flags & D_DISABLED) ? gui_mg_color : d->fg;

  /* draw box contents */
  for (i = 0; i < height; i++)
    {
      if (d->d2 + i < listsize)
	{
	  if (d->d2 + i == d->d1)
	    {
	      fg = d->bg;
	      bg = fg_color;
	    }
	  else if ((sel) && (sel[d->d2 + i]))
	    {
	      fg = d->bg;
	      bg = gui_mg_color;
	    }
	  else
	    {
	      fg = fg_color;
	      bg = d->bg;
	    }
	  ustrzcpy (s, sizeof (s),
		    (*(my_getfuncptr) d->dp) (i + d->d2, NULL));
	  x = d->x + 2;
	  y = d->y + 2 + i * text_height (font);
	  rtm = text_mode (bg);
	  rectfill (screen, x, y, x + 7, y + text_height (font) - 1, bg);
	  x += 8;
	  len = ustrlen (s);
	  while (text_length (font, s) >= MAX (d->w - 1 - (bar ? 22 : 10), 1))
	    {
	      len--;
	      usetat (s, len, 0);
	    }
	  textout (screen, font, s, x, y, -1);
	  text_mode (rtm);
	  x += text_length (font, s);
	  if (x <= d->x + w)
	    rectfill (screen, x, y, d->x + w, y + text_height (font) - 1, bg);
	}
      else
	{
	  rectfill (screen, d->x + 2, d->y + 2 + i * text_height (font),
		    d->x + w, d->y + 1 + (i + 1) * text_height (font), d->bg);
	}
    }

  if (d->y + 2 + i * text_height (font) <= d->y + d->h - 3)
    rectfill (screen, d->x + 2, d->y + 2 + i * text_height (font),
	      d->x + w, d->y + d->h - 3, d->bg);

  /* draw frame, maybe with scrollbar */
  _draw_scrollable_frame (d, listsize, d->d2, height, fg_color, d->bg);
}

/* d_list_proc:
 *  A list box object. The dp field points to a function which it will call
 *  to obtain information about the list. This should follow the form:
 *     const char *<list_func_name> (int index, int *list_size);
 *  If index is zero or positive, the function should return a pointer to
 *  the string which is to be displayed at position index in the list. If
 *  index is  negative, it should return null and list_size should be set
 *  to the number of items in the list. The list box object will allow the
 *  user to scroll through the list and to select items list by clicking
 *  on them, and if it has the input focus also by using the arrow keys. If 
 *  the D_EXIT flag is set, double clicking on a list item will cause it to 
 *  close the dialog. The index of the selected item is held in the d1 
 *  field, and d2 is used to store how far it has scrolled through the list.
 */
int
my_list_proc (int msg, DIALOG * d, int c)
{
  int listsize, i, bottom, height, bar, orig;
  char *sel = d->dp2;
  int redraw = FALSE;

  switch (msg)
    {

    case MSG_START:
      (*(my_getfuncptr) d->dp) (-1, &listsize);
      my_handle_scrollable_scroll (d, listsize, &d->d1, &d->d2);
      break;

    case MSG_DRAW:
      my_draw_listbox (d);
      break;

    case MSG_CLICK:
      (*(my_getfuncptr) d->dp) (-1, &listsize);
      height = (d->h - 4) / text_height (font);
      bar = (listsize > height);
      if ((!bar) || (gui_mouse_x () < d->x + d->w - 13))
	{
	  if ((sel) && (!(key_shifts & KB_CTRL_FLAG)))
	    {
	      for (i = 0; i < listsize; i++)
		{
		  if (sel[i])
		    {
		      redraw = TRUE;
		      sel[i] = FALSE;
		    }
		}
	      if (redraw)
		{
		  scare_mouse ();
		  object_message (d, MSG_DRAW, 0);
		  unscare_mouse ();
		}
	    }
	  my_handle_listbox_click (d);
	  while (gui_mouse_b ())
	    {
	      broadcast_dialog_message (MSG_IDLE, 0);
	      d->flags |= D_INTERNAL;
	      my_handle_listbox_click (d);
	      d->flags &= ~D_INTERNAL;
	    }
	}
      else
	{
	  my_handle_scrollable_scroll_click (d, listsize, &d->d2, height);
	}
      break;

    case MSG_DCLICK:
      (*(my_getfuncptr) d->dp) (-1, &listsize);
      height = (d->h - 4) / text_height (font);
      bar = (listsize > height);
      if ((!bar) || (gui_mouse_x () < d->x + d->w - 13))
	{
	  if (d->flags & D_EXIT)
	    {
	      if (listsize)
		{
		  i = d->d1;
		  object_message (d, MSG_CLICK, 0);
		  if (i == d->d1)
		    return D_CLOSE;
		}
	    }
	}
      break;

    case MSG_WHEEL:
      (*(my_getfuncptr) d->dp) (-1, &listsize);
      height = (d->h - 4) / text_height (font);
      if (height < listsize)
	{
	  int delta = (height > 3) ? 3 : 1;
	  if (c > 0)
	    i = MAX (0, d->d2 - delta);
	  else
	    i = MIN (listsize - height, d->d2 + delta);
	  if (i != d->d2)
	    {
	      d->d2 = i;
	      scare_mouse ();
	      object_message (d, MSG_DRAW, 0);
	      unscare_mouse ();
	    }
	}
      break;

    case MSG_KEY:
      (*(my_getfuncptr) d->dp) (-1, &listsize);
      if ((listsize) && (d->flags & D_EXIT))
	return D_CLOSE;
      break;

    case MSG_WANTFOCUS:
      return D_WANTFOCUS;

    case MSG_CHAR:
      (*(my_getfuncptr) d->dp) (-1, &listsize);

      if (listsize)
	{
	  c >>= 8;

	  bottom = d->d2 + (d->h - 4) / text_height (font) - 1;
	  if (bottom >= listsize - 1)
	    bottom = listsize - 1;

	  orig = d->d1;

	  if (c == KEY_UP)
	    d->d1--;
	  else if (c == KEY_DOWN)
	    d->d1++;
	  else if (c == KEY_HOME)
	    d->d1 = 0;
	  else if (c == KEY_END)
	    d->d1 = listsize - 1;
	  else if (c == KEY_PGUP)
	    {
	      if (d->d1 > d->d2)
		d->d1 = d->d2;
	      else
		d->d1 -= (bottom - d->d2) ? bottom - d->d2 : 1;
	    }
	  else if (c == KEY_PGDN)
	    {
	      if (d->d1 < bottom)
		d->d1 = bottom;
	      else
		d->d1 += (bottom - d->d2) ? bottom - d->d2 : 1;
	    }
	  else
	    return D_O_K;

	  if (sel)
	    {
	      if (!(key_shifts & (KB_SHIFT_FLAG | KB_CTRL_FLAG)))
		{
		  for (i = 0; i < listsize; i++)
		    sel[i] = FALSE;
		}
	      else if (key_shifts & KB_SHIFT_FLAG)
		{
		  for (i = MIN (orig, d->d1); i <= MAX (orig, d->d1); i++)
		    {
		      if (key_shifts & KB_CTRL_FLAG)
			sel[i] = (i != d->d1);
		      else
			sel[i] = TRUE;
		    }
		}
	    }

	  /* if we changed something, better redraw... */
	  my_handle_scrollable_scroll (d, listsize, &d->d1, &d->d2);
	  d->flags |= D_DIRTY;
	  return D_USED_CHAR;
	}
      break;
    }

  return D_O_K;
}



/*------------------------------------------------------------------*/
BITMAP *
my_create_bitmap (int w, int h)
{
  BITMAP *bmp;

  bmp = create_bitmap (w, h);
  if (bmp == NULL)
    my_exit (EXIT_CODE_MEM_TROUBLE);

  return bmp;
}
