/*
 * tab.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>.
 * All rights reserved.
 *
 * $Log: tab.c,v $
 * Revision 1.10  2000/09/29 07:53:02  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:14:16  root
 * #
 *
 * Revision 1.3  2000/02/15 22:13:52  root
 * #
 *
 * Revision 1.2  2000/02/15 22:13:40  root
 * #
 *
 * Revision 1.1  2000/02/15 16:58:01  root
 * #
 *
 *
 */

static char rcsid[] = "$Id: tab.c,v 1.10 2000/09/29 07:53:02 root Exp $";

#include "project.h"


static void
null_dispatch (EventType * e)
{
}

static void (*dispatcher) (EventType *) = null_dispatch;

#define TABLEN 4
static SWord tab_shift[TABLEN] = { 4, 2, 1, 1 };
static SWord tab_len[TABLEN] = { 1, 1, 0, 0 };

#define TABYCHSHIFT 0           /*Up from Base of rectangle */

void
TabTextPos (CharPtr chars, Word len, RectangleType * r, SWord * x, SWord * y)
{
  SWord w = FntCharsWidth (chars, len);
  SWord h = FntCharHeight ();

  *y = r->topLeft.y + r->extent.y;
  *y -= TABYCHSHIFT;            /*A ++ and -- cancel out */
  *y -= h;

  *x = r->extent.x - w;
  *x = *x / 2;
  *x += r->topLeft.x;

}



void
DrawFullTab (CharPtr chars, Word len, RectangleType * r)
{
  SWord sx, ex;
  SWord y;
  RectangleType rt;

  sx = r->topLeft.x;
  ex = sx + r->extent.x;
  ex--;

  for (y = 0; y < TABLEN; ++y)
    {
      WinDrawLine (sx + tab_shift[y], y + r->topLeft.y, ex - tab_shift[y],
                   y + r->topLeft.y);
    }

  rt = *r;
  rt.topLeft.y += TABLEN;
  rt.extent.y -= TABLEN;

  WinDrawRectangle (&rt, 0);

  TabTextPos (chars, len, r, &sx, &y);
  WinEraseChars (chars, len, sx, y);
}


void
DrawEmptyTab (CharPtr chars, Word len, RectangleType * r)
{
  SWord sx, ex;
  SWord y, ey;

  TabTextPos (chars, len, r, &sx, &y);
  WinDrawChars (chars, len, sx, y);

  sx = r->topLeft.x;
  ex = sx + r->extent.x;
  ex--;
  sx += *tab_shift;
  ex -= *tab_shift;

  WinDrawLine (sx, r->topLeft.y, ex, r->topLeft.y);

  for (y = 1; y < TABLEN; ++y)
    {
      sx = r->topLeft.x;
      ex = sx + r->extent.x;
      ex--;
      sx += tab_shift[y];
      ex -= tab_shift[y];

      WinDrawLine (sx, y, sx + tab_len[y], y);
      WinDrawLine (ex, y, ex - tab_len[y], y);
    }

  sx = r->topLeft.x;
  ex = sx + r->extent.x;
  ex--;
  ey = r->topLeft.y + r->extent.y;
  ey--;

  WinDrawLine (sx, y, sx, ey);
  WinDrawLine (ex, y, ex, ey);
  WinDrawLine (sx + 1, ey, ex - 1, ey);

}


typedef struct
{
  RectangleType r;
  Word formid;
  Byte *name;
  void (*dispatch) (EventType *);
}
Tab;


static Tab tabs[] = {
  {
   {
    {2, 0},
    {29, TabHeight}}, SystemForm, "Systm", system_dispatch},
  {
   {
    {32, 0},
    {27, TabHeight}}, TunerForm, "Tuner", tuner_dispatch},
  {
   {
    {60, 0},
    {18, TabHeight}}, CdForm, "Cd", cd_dispatch},
  {
   {
    {79, 0},
    {27, TabHeight}}, MpegForm, "MPEG", mpeg_dispatch},
  {
   {
    {107, 0},
    {26, TabHeight}}, WaveForm, "Wave", wave_dispatch},
  {
   {
    {134, 0},
    {24, TabHeight}}, OggForm, "Ogg", ogg_dispatch},
};

#define ntabs (sizeof(tabs)/sizeof(Tab))

DrawTabs (void)
{
  Word id = FrmGetActiveFormID ();
  int i;

  for (i = 0; i < ntabs; ++i)
    {
      if (id == tabs[i].formid)
        {
          DrawFullTab (tabs[i].name, StrLen (tabs[i].name), &tabs[i].r);
          dispatcher = tabs[i].dispatch;
        }
      else
        {
          DrawEmptyTab (tabs[i].name, StrLen (tabs[i].name), &tabs[i].r);
        }
    }
  WinDrawLine (0, TabHeight - 1, ScreenWidth - 1, TabHeight - 1);
}

void
TabDispatch (EventType * e)
{
  int i;

  SWord x = e->screenX;
  SWord y = e->screenY;
  Boolean p;


  switch (e->eType)
    {
    case penDownEvent:
      if ((y <= (tabs[0].r.topLeft.y + tabs[0].r.extent.y))
          && (y >= tabs[0].r.topLeft.y))
        {

          for (i = 0; i < ntabs; ++i)
            {
              if (RctPtInRectangle (x, y, &tabs[i].r))
                {
                  do
                    {
                      PenGetPoint (&x, &y, &p);
                    }
                  while (p);
//  if (!RctPtInRectangle (x, y, &tabs[i].r)) return;

                  FrmGotoForm (tabs[i].formid);
                  return;

                }
            }
        }
      break;
    case frmOpenEvent:
      DrawTabs ();
      break;
    }

  (*dispatcher) (e);


}
