/*  gfxutil.cpp
    Some graphic utility functions

    22.12.2001: tk, initial implementation.

    Copyright (c)2000 by Thomas Kindler, thomas.kindler@gmx.de

    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. Read the
    full License at http://www.gnu.org/copyleft for more details.
*/

// include files -----
//
#include <stdio.h>
#include "gfxcore.h"
#include "gfxutil.h"


/**
 * Draw a rounded rectangle.
 * This function draws a nicely rounded rectangle, with an
 * optional shadow and variable roundness. (left,top) is the
 * upper left corner of the rectangle, and (right,bottom)
 * is its lower right corner. Notice that the rectangle
 * gets one pixel wider and taller if a shadow is drawn.
 *
 * \author  robert dubber
 * \param gfx      Graphics context to use.
 * \param x,y      coordinates of top-left corner
 * \param w,h      width and height
 * \param r        roundness      (default: 2)
 * \param shadow   draw a shadow  (default: true)
 *
 */
void GFX_RoundRect(Graphics *gfx, int x, int y, int w, int h,
                                  int r, bool shadow)
{
  // draw frame
  GFX_HLine(gfx, x+r,   y,     w-r*2);  // top
  GFX_HLine(gfx, x+r,   y+h-1, w-r*2);  // bottom
  GFX_VLine(gfx, x,     y+r,   h-r*2);  // left
  GFX_VLine(gfx, x+w-1, y+r,   h-r*2);  // right

  // draw edges
  GFX_Line(gfx, x+1,   y+r-1, x+r-1, y+1  ); // top-left
  GFX_Line(gfx, x+w-r, y+1,   x+w-2, y+r-1); // top-right
  GFX_Line(gfx, x+1,   y+h-r, x+r-1, y+h-2); // bottom-left
  GFX_Line(gfx, x+w-r, y+h-2, x+w-2, y+h-r); // bottom-right

  // draw shadow
  if (shadow) {
    GFX_HLine(gfx, x+r+1, y+h,   w-r*2-1);  // bottom
    GFX_VLine(gfx, x+w  , y+r+1, h-r*2-1);  // right
    GFX_Line (gfx, x+w-r-1, y+h, x+w-1, y+h-r); // bottom-right
  }
}


/**
 * Draw a progress bar.
 * \param  gfx  graphics context to draw on
 * \param  x,y  coordinates of top-left corner
 * \param  w,h  width and height of progressbar
 * \param  value,min,max   minimum, maximum and current value
 *                         (default: min=0, max=100)
 */
void GFX_ProgressBar( Graphics *gfx, int x, int y, int w, int h,
                      float value, float min, float max )
{
  Graphics g;
  GFX_Init(&g, gfx);    // work on a copy of the context..

  if (value>max) value = max;
  if (value<min) value = min;
  int width = ((value-min)/(max-min))*(w-4);

  GFX_Rectangle(&g, x, y, w, h);

  GFX_SetBlitMode(&g, BLT_COPY);
  GFX_SetFillStyle(&g, emptyFill);
  GFX_FillRect(&g, x+width+2, y+1, w-width-4, h-2);

  GFX_SetBlitMode(&g, BLT_XOR);
  GFX_SetFillStyle(&g, xLeaveFill);
  GFX_FillRect(&g, x+2, y+2, width, h-4);
}
