//  
//  Copyright (c) 1994 by Mike Romberg (  romberg@md.fsl.noaa.gov )
//  Debugged and Featurized by Michael Hemy
//
//  This file may be distributed under terms of the GPL
//

#include "fieldmeter.h"
#include "xosview.h"
#include <fstream.h>

extern int disable_free_legend_;

FieldMeter::FieldMeter( XOSView *parent, int x, int y, int width, int height, 
		        int numfields, const char *title, const char *legend,
		        int dolegends){
  dolegends_ = dolegends;
  title_ = legend_ = NULL;
  FieldMeter::title( title );
  FieldMeter::legend( legend );
  parent_ = parent;
  resize( x, y, width, height );
  numfields_ = numfields;
  fields_ = new float[numfields_];
  colors_ = new XColor[numfields_];

}

FieldMeter::~FieldMeter( void ){
  delete[] fields_;
  delete[] colors_;
  delete[] title_;
  delete[] legend_;
}

void FieldMeter::title( const char *title ){
  delete[] title_;
  title_ = new char[strlen( title ) + 1];
  strcpy( title_, title );
}


void FieldMeter::legend( const char *legend ){
  delete[] legend_;
  legend_ = new char[strlen( legend ) + 1];
  strcpy( legend_, legend );
}

void FieldMeter::resize( int x, int y, int width, int height ){
  x_ = x;
  y_ = y;
  width_ = width;
  height_ = height;
}


void FieldMeter::draw( void ){
  XSetForeground( parent_->display(), parent_->gc(), parent_->fg_pixel() );
  XDrawRectangle( parent_->display(), parent_->window(), parent_->gc(),
		  x_, y_, width_, height_);
  parent_->drawstr( x_ - parent_->xoff_left()+ 6, 
		   y_ -parent_->yoff_below(), title_ );
  drawlegend();
}

void FieldMeter::drawlegend( void ){
  char *tmp1, *tmp2, buff[100];
  int n, x = x_;
  int this_pixel;
  int num_legends = numfields_ - disable_free_legend_;  
  tmp1 = tmp2 = legend_;
  for ( int i = 0 ; i < num_legends ; i++ ){
    n = 0;
    while ( (*tmp2 != ' ') && (*tmp2 != '\0') ){
      tmp2++;
      n++;
    }
    tmp2++;
    this_pixel =  (i == numfields_ -1) ? parent_->free_pixel():
	parent_->busy_pixel(i);
    strncpy( buff, tmp1, n );
    buff[n] = '\0';
    if ( dolegends_ ){
      // first draw a legend rectangle
      XSetBackground( parent_->display(), parent_->gc(), this_pixel );
      parent_->drawstr( x, y_ - parent_->yoff_below(), "  " );    
      XSetBackground( parent_->display(), parent_->gc(), parent_->bg_pixel() );
      XSetForeground( parent_->display(), parent_->gc(), parent_->fg_pixel() );
      x += XTextWidth( parent_->xfont(), "   ", 3);
    }
    else {
      XSetForeground( parent_->display(), parent_->gc(), this_pixel );
    }
    parent_->drawstr( x, y_ - parent_->yoff_below(), buff );
    x += XTextWidth( parent_->xfont(), buff, n );
    x += XTextWidth( parent_->xfont(), " ", 1 );
    tmp1 = tmp2;
  }
}

void FieldMeter::drawfields( void ){
  int twidth, x = x_+1;
  int this_pixel;
  for ( int i = 0 ; i < numfields_ ; i++ ){
    twidth = (int)((fields_[i] * (width_-2)) / total_);
    if (i == numfields_ - 1) {
      twidth = x_ + width_ - x - 1;
      this_pixel =  parent_->free_pixel();
    }
    else {
      this_pixel = parent_->busy_pixel(i);
    }
    XSetForeground( parent_->display(), parent_->gc(), this_pixel );
    XFillRectangle( parent_->display(), parent_->window(), parent_->gc(),
		   x, y_+2, twidth, height_ -3 );
    x += twidth;
  }
  XSetForeground( parent_->display(), parent_->gc(), parent_->fg_pixel() );
  parent_->drawstr( x_ - parent_->xoff_left()+ 6, 
		   y_+parent_->yoff_above(), extra_str_ );
  parent_->flush();
}

void FieldMeter::checkevent( void ){
  drawfields();
}

