/*LINTLIBRARY*/

/*  @(#)items.c 1.4 90/04/05
 *
 *  Procedures for manipulating the othello game panel items.
 *
 *  Original SunView version by Ed Falk - Sun Microsystems Inc.
 *
 *  Rewritten for independent use by
 *          Rich Burridge, Sun Microsystems, Australia
 *
 *  Copyright (c) Ed Falk & Rich Burridge - Sun Microsystems.
 *                                          All rights reserved.
 *
 *  Permission is given to distribute these sources, as long as the
 *  introductory messages are not removed, and no monies are exchanged.
 *
 *  No responsibility is taken for any errors on inaccuracies inherent
 *  either to the comments or the code of this program, but if reported
 *  to me, then an attempt will be made to fix them.
 */

#include "color.h"
#include "othello.h"
#include "extern.h"


draw_button(item, color, image)      /* Draw a panel button. */
enum panel_type item ;
int color ;
enum image_type image ;
{
  char *label ;
  int x, y ;

  x = items[(int) item].x ;
  y = items[(int) item].y ;
  if (iscolor)
    {
      draw_stencil(x, y, BWIDTH, BHEIGHT, RSRC, color,
                   BUT_STENCIL, BUT_STENCIL) ;
      draw_image(x, y, BWIDTH, BHEIGHT, image) ;
    }
  else draw_stencil(x, y, BWIDTH, BHEIGHT, RSRC, C_WHITE, BUT_STENCIL, image) ;
  label = items[(int) item].text ;
  if (image == BUT_INVERT) color = C_WHITE ;
  else                   color = C_BLACK ;
/* Fix by MN 900516 - added -5 to x */
/*  x += BWIDTH - ;*/
/*  x += get_strwidth(NFONT,label)/2; */
/*  printf("Width of %s is %d\n",label,get_strwidth(NFONT,label));*/
/*  x += ((BWIDTH - get_strwidth(NFONT, label)) / 2 - 5) ; */
  x += 2;
  y += (nfont_height + (BHEIGHT - nfont_height) / 2) - 5 ;
  draw_text(x, y, NFONT, color, label) ;
}


draw_cycle(item, color, image)
enum panel_type item ;
int color ;
enum image_type image ;
{
  int x, y ;

  x = items[(int) item].x + (3 * BWIDTH) + (2 * BGAP) - CWIDTH ;
  y = items[(int) item].y ;
  if (iscolor)
    {
      draw_stencil(x, y, CWIDTH, CHEIGHT, RSRC, color, CY_STENCIL, CY_STENCIL) ;
      draw_image(x, y, CWIDTH, CHEIGHT, image) ;
    }
  else draw_stencil(x, y, CWIDTH, CHEIGHT, RSRC, C_WHITE, CY_STENCIL, image) ;
}


draw_cycle_item(item, choices, color, image)  /* Display cycle panel item. */
enum panel_type item ;
char *choices[] ;
int color ;
enum image_type image ;
{
  char *label ;
  int n, x, y ;

  n = (int) item ;
  label = items[n].text ;
  x = items[n].x ;
  y = items[n].y ;
  draw_text(x, y + bfont_height, BFONT, C_BLACK, label) ;
  set_cycle(item, choices[items[n].value]) ;
  draw_cycle(item, color, image) ;
}


draw_piece(piece, x, y, op)    /* Draw an othello piece on the board. */
int piece, x, y ;
enum optype op ;
{
  switch (piece)
    {
      case BLACK : draw_stencil(x, y, PSIZE, PSIZE, op, C_BLACK,
                                P_BLACK, P_BLACK) ;
                   break ;
      case WHITE : draw_stencil(x, y, PSIZE, PSIZE, op, C_BLACK,
                                P_BLACK, P_WHITE) ;
    }
}


make_message(item)         /* Display a message panel. */
enum panel_type item ;
{
  char *message ;
  int x, y ;

  message = items[(int) item].text ;
  x = items[(int) item].x ;
  y = items[(int) item].y ;
  color = (iscolor) ? C_BEIGE : C_WHITE ;
  color_area(x, y, TOTAL_WIDTH, 15, color) ;
  draw_text(x, y+nfont_height, NFONT, C_BLACK, message) ;
}


make_panel()
{
  draw_button(DONE_BUT,     C_LGREY, BUT_NORMAL) ;
  draw_button(LAST_BUT,     C_LGREY, BUT_NORMAL) ;
  draw_button(NEW_GAME_BUT, C_LGREY, BUT_NORMAL) ;
  draw_button(SUGGEST_BUT,  C_LGREY, BUT_NORMAL) ;
  draw_button(UNDO_BUT,     C_LGREY, BUT_NORMAL) ;
  draw_button(QUIT_BUT,     C_LGREY, BUT_NORMAL) ;

  draw_cycle_item(ASPIRATION,     asp_values,  C_LGREY, CY_NORMAL) ;
  draw_cycle_item(DIFFICULTY,     diff_values, C_LGREY, CY_NORMAL) ;
  draw_cycle_item(COMPUTER_PLAYS, comp_values, C_LGREY, CY_NORMAL) ;
  draw_cycle_item(REMARK,         rem_values,  C_LGREY, CY_NORMAL) ;

  make_message(REMARK_MES) ;
  make_message(PANEL_MES) ;
  make_message(SCORE_MES) ;
}


message(mtype, str)
enum panel_type mtype ;
char *str ;
{
  int n ;

  n = (int) mtype ;
  color = (iscolor) ? C_BEIGE : C_WHITE ;
  color_area(items[n].x, items[n].y,
             get_strwidth(NFONT, items[n].text), nfont_height + 5, color) ;
  draw_text(items[n].x, items[n].y + nfont_height, NFONT, C_BLACK, str) ;
  STRCPY(items[n].text, str) ;
}


set_cycle(mtype, str)
enum panel_type mtype ;
char *str ;
{
  int n, width, x ;

  n = (int) mtype ;
  color = (iscolor) ? C_BEIGE : C_WHITE ;

  width = (3 * BWIDTH) + (2 * BGAP) -
           get_strwidth(BFONT, items[n].text) - CWIDTH ;
  x = items[n].x + get_strwidth(BFONT, items[n].text) ;
  color_area(x, items[n].y, width, items[n].height, color) ;

  width = get_strwidth(NFONT, str) ;
  x = items[n].x + (3 * BWIDTH) + (2 * BGAP) - CWIDTH - width ;
  draw_text(x - 5, items[n].y + nfont_height, NFONT, C_BLACK, str) ;
}
