/*
 * slider.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>.
 * All rights reserved.
 *
 * $Log: slider.c,v $
 * Revision 1.17  2001/10/12 13:51:17  root
 * #
 *
 * Revision 1.16  2000/12/01 04:42:19  root
 * #
 *
 * Revision 1.15  2000/12/01 04:13:52  root
 * #
 *
 * Revision 1.14  2000/10/07 18:30:10  root
 * #
 *
 * Revision 1.13  2000/10/06 19:52:45  root
 * #
 *
 * Revision 1.12  2000/09/29 07:53:48  root
 * #
 *
 * Revision 1.11  2000/09/29 07:53:02  root
 * #
 *
 * Revision 1.10  2000/09/24 01:46:07  root
 * #
 *
 * Revision 1.9  2000/09/24 01:12:53  root
 * #
 *
 * Revision 1.8  2000/02/20 03:16:39  root
 * #
 *
 * Revision 1.7  2000/02/20 02:51:44  root
 * #
 *
 * Revision 1.6  2000/02/16 11:34:56  root
 * #
 *
 * Revision 1.5  2000/02/16 11:34:21  root
 * #
 *
 * Revision 1.4  2000/02/15 22:16:16  root
 * #
 *
 * Revision 1.3  2000/02/15 22:14:16  root
 * #
 *
 * Revision 1.2  2000/02/15 22:13:52  root
 * #
 *
 * Revision 1.1  2000/02/15 22:13:40  root
 * #
 *
 *
 */

static char rcsid[] =
  "$Id: slider.c,v 1.17 2001/10/12 13:51:17 root Exp root $";

#include "project.h"

#define SLWIDTH 2
#define SLHEIGHT 5

static Word SliderForm, SliderId; //Locking to prevent SetSlider when in UI

void
WinDrawNoFillRectangle (RectangleType * rt)
{
  WinDrawLine (rt->topLeft.x, rt->topLeft.y,
               rt->topLeft.x + rt->extent.x - 1, rt->topLeft.y);

  WinDrawLine (rt->topLeft.x + rt->extent.x - 1, rt->topLeft.y + 1,
               rt->topLeft.x + rt->extent.x - 1,
               rt->topLeft.y + rt->extent.y - 2);

  WinDrawLine (rt->topLeft.x + rt->extent.x - 1,
               rt->topLeft.y + rt->extent.y - 1, rt->topLeft.x,
               rt->topLeft.y + rt->extent.y - 1);

  WinDrawLine (rt->topLeft.x, rt->topLeft.y + rt->extent.y - 1,
               rt->topLeft.x, rt->topLeft.y);
}

int
GetBounds (Word formid, Word id, RectangleType * bounds)
{
  FormPtr formptr;
  Word index;

  formptr = FrmGetFormPtr (formid);
  if (!formptr)
    return 0;
  index = FrmGetObjectIndex (formptr, id);
  FrmGetObjectBounds (formptr, index, bounds);

  return 1;
}

void
DrawSlider (RectangleType * bounds, int v)
{
  int lx, rx;
  int x;
  RectangleType rt;

  SWord my = bounds->extent.y - 1;
  my = my / 2;
  my += bounds->topLeft.y;


  WinEraseRectangle (bounds, 0);

  lx = bounds->topLeft.x;
  rx = bounds->topLeft.x + bounds->extent.x - 1;

  lx += SLWIDTH;
  rx -= SLWIDTH;

  x = (v * (rx - lx)) / 100;
  x += lx;

  WinDrawLine (lx, my, rx, my);

  if (x < lx)
    x = lx;
  if (x > rx)
    x = rx;


  rt.topLeft.x = x - SLWIDTH;
  rt.topLeft.y = my - SLHEIGHT;

  rt.extent.x = 2 * SLWIDTH;
  rt.extent.y = 2 * SLHEIGHT;

  WinDrawNoFillRectangle (&rt);

}


void
slider_dispatch (EventType * e, Word formid, Word sliderid,
                 void (*callback) (Word, Word, int))
{
  int lx, rx, a;
  SWord x, y;
  Boolean p;
  RectangleType bounds;

/*FIXME: do coord xform */

  x = e->screenX;
  y = e->screenY;


  if (!GetBounds (formid, sliderid, &bounds))
    return;

  if (RctPtInRectangle (x, y, &bounds))
    {
      SliderId = sliderid;
      SliderForm = formid;

      do
        {
          PenGetPoint (&x, &y, &p);
          dispatch_hook ();

          lx = bounds.topLeft.x;
          rx = bounds.topLeft.x + bounds.extent.x;

          lx += SLWIDTH;
          rx -= SLWIDTH;

          a = 100 * (x - lx);
          a = a / (rx - lx);

          if (a < 0)
            a = 0;
          if (a > 100)
            a = 100;


          DrawSlider (&bounds, a);
          (*callback) (formid, sliderid, a);

          {
            Int32 d;
            d = SysTicksPerSecond ();
            d = d / 10;
            SysTaskDelay (d);
          }

        }
      while (p);
      SliderId = 0;
      SliderForm = 0;

    }

}

SliderSet (Word formid, Word sliderid, int a)
{
  RectangleType bounds;
  if (!GetBounds (formid, sliderid, &bounds))
    return;
  if ((formid == SliderForm) && (sliderid == SliderId))
    return;
  DrawSlider (&bounds, a);
}
