//
// Copyright (C) 1999-2002 Toshikaz Hirabayashi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// TOSHIKAZ HIRABAYASHI BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// Except as contained in this notice, the name of Toshikaz Hirabayashi shall
// not be used in advertising or otherwise to promote the sale, use or other
// dealings in this Software without prior written authorization from
// Toshikaz Hirabayashi.

#include <WScom.h>
#include <WSCvlineGraph.h>
#include <WSCclassInformation.h>
#include <WSCdevice.h>
#include <WSCblink.h>
#include <WSCcolorSet.h>
#include <WSCimageSet.h>
#include <WSDdev.h>
#include <WSDcolor.h>
#include <WSCfontSet.h>
#include <WSDappDev.h>

WSMFguiClassInitialize(WSCvlineGraph, WSCnwbase);
WSMFversion(WSCvlineGraph, WSCnwbase);

WSCvlineGraph::WSCvlineGraph(WSCbase* base, char* objname):
			WSCnwbase(base, objname){
  _values = WSGFstrdup("0,30,20,40,10,50,10,80,50,70");
//  _barw = 10;
  _orientation  = WS_VERTICAL;
  _rev = False;
  _num = 10;
  _lw = 2;
  _lt = 0;
  _stippled = False;
  _hatch_pattern = 0;
  _hatch_color = WS_DF_NWBACKCOLOR;

  WSMFpropertyCreateStart
    WSMFparentCheckVerSrc(WSCvlineGraph);

    WSMFpropertyCreate(WSNvalue,  char*,   _values,WSSvalue);
    WSMFpropertyCreate(WSNcolumns, WSCushort, _num,WSScolumns);
//    WSMFpropertyCreate(WSNbarThickness,  WSCuchar, _barw,WSSbarThickness);
    WSMFpropertyCreate(WSNorientation,  WSCuchar, _orientation,WSSorientation);
      WSMFpropertySetSelection(WSRorientation, WSRorientationD);
    WSMFpropertyCreate(WSNreverseFlag,  WSCbool, _rev,WSSreverseFlag);
      WSMFpropertySetSelection(WSRbool, WSRboolD);
    WSMFpropertyCreate(WSNlineWidth, WSCuchar, _lw,WSSlineWidth);
    WSMFpropertyCreate(WSNlineType,  WSCuchar, _lt,WSSlineType);
      WSMFpropertySetSelection(WSRlineType, WSRlineTypeD);
    WSMFpropertyCreate(WSNstippled,WSCbool,_stippled,WSSstippled );
      WSMFpropertySetSelection(WSRbool4, WSRbool4D);
    WSMFpropertyCreate(WSNhatchPattern,WSCuchar,_hatch_pattern,WSShatchPattern);
      WSMFpropertySetSelection(WSRhatchPattern, WSRhatchPatternD);
    WSMFpropertyCreate(WSNhatchColor,short,_hatch_color,WSShatchColor);

  WSMFpropertyCreateEnd

}
WSMFproperty( WSCvlineGraph, WSNvalue, char*, _values ,WSGFstrdup("0,30,20,40,10,50,10,80,50,70"));
WSMFproperty( WSCvlineGraph, WSNcolumns, WSCushort, _num ,10);
//WSMFproperty( WSCvlineGraph, WSNbarThickness, WSCuchar, _barw ,10);
WSMFproperty( WSCvlineGraph, WSNorientation, WSCuchar, _orientation ,WS_VERTICAL);
WSMFproperty( WSCvlineGraph, WSNreverseFlag, WSCbool, _rev ,False);
WSMFproperty( WSCvlineGraph, WSNlineWidth, WSCuchar, _lw, 2);
WSMFproperty( WSCvlineGraph, WSNlineType,  WSCuchar,      _lt, 0);
WSMFproperty( WSCvlineGraph, WSNstippled,  WSCbool,_stippled,     False);
WSMFproperty( WSCvlineGraph, WSNhatchPattern, WSCuchar,   _hatch_pattern,0    );
WSMFproperty( WSCvlineGraph, WSNhatchColor,  short,  _hatch_color,WS_DF_NWBACKCOLOR);



void WSCvlineGraph::setWorkWSNvalue(char*){};
void WSCvlineGraph::getWorkWSNvalue(char**){};
void WSCvlineGraph::setWorkWSNcolumns(WSCushort){};
void WSCvlineGraph::getWorkWSNcolumns(WSCushort*){};
//void WSCvlineGraph::setWorkWSNbarThickness(WSCuchar){};
//void WSCvlineGraph::getWorkWSNbarThickness(WSCuchar*){};
void WSCvlineGraph::setWorkWSNorientation(WSCuchar){};
void WSCvlineGraph::getWorkWSNorientation(WSCuchar*){};
void WSCvlineGraph::setWorkWSNreverseFlag(WSCbool){};
void WSCvlineGraph::getWorkWSNreverseFlag(WSCbool*){};
void WSCvlineGraph::setWorkWSNlineWidth(WSCuchar){ }
void WSCvlineGraph::getWorkWSNlineWidth(WSCuchar*){ }
void WSCvlineGraph::setWorkWSNlineType(WSCuchar){ }
void WSCvlineGraph::getWorkWSNlineType(WSCuchar*){ }
void WSCvlineGraph::setWorkWSNstippled(WSCbool){ }
void WSCvlineGraph::getWorkWSNstippled(WSCbool*){ }
void WSCvlineGraph::setWorkWSNhatchPattern(WSCuchar){ }
void WSCvlineGraph::getWorkWSNhatchPattern(WSCuchar*){ }
void WSCvlineGraph::setWorkWSNhatchColor(short){ }
void WSCvlineGraph::getWorkWSNhatchColor(short*){ }

WSCvlineGraph::~WSCvlineGraph() {
  if (_values != NULL){
    delete _values;
    _values = NULL;
  }
}

long WSCvlineGraph::draw(){
  if (getVisible() == False){
    return WS_NO_ERR;
  }
  WSDdev* dev = getowndev();
  if (dev == NULL){
    return WS_ERR;
  }
  if (_num == 0){
    return WS_NO_ERR;
  }

  short x = _x;
  short y = _y;
  WSCushort w = _w;
  WSCushort h = _h;
  WSCbool absolute = getAbsoluteDraw();
  if (absolute == False && dev->isExposed(x, y, w, h) == False){
    return WS_NO_ERR;
  }
  WSCbase::update();
  setAbsoluteDraw(False);
  long err = dev->beginDraw(x, y, w, h, absolute);
  if (err != WS_NO_ERR){
     return WS_NO_ERR;
  }
  if (_orientation == WS_VERTICAL){
    h -= _lw;
  }else{
    w -= _lw;
  }
  long i;
  if (_num < 2){
    _num = 2;
  }
  WSCushort interval = w/(_num-1);
  if (_orientation != WS_VERTICAL){
    interval = h/(_num-1);
  }

  WSCstring vlist(_values);
  long lnum = vlist.getWords(",");
  long num = _num;
  if (lnum < _num){
    num = lnum;
  }
  dev->setLineWidth(0);
  dev->setHatchPattern(_hatch_pattern);
  dev->setForeColor(_hatch_color);
  WSCpoint* points;
  if (_stippled == False){
    points = new WSCpoint[num];
  }else{
    points = new WSCpoint[num+2];
  }
  if (_stippled != False){
    if (_orientation == WS_VERTICAL){
      for(i=0; i<num; i++){
        WSCstring tmp = vlist.getWord(i,",");
        long val = atoi(tmp);
        if (_rev == False){
          points[i].x = interval*i;
          points[i].y =  h - h*val/100 +_lw/2;
        }else{
          points[i].x = interval*i;
          points[i].y = _lw/2;
        }
      }
      if (_rev == False){
        points[i].x = interval*num;
        points[i].y = h -_lw/2;
        points[i+1].x = 0;
        points[i+1].y = h -_lw/2;
      }else{
        points[i].x = interval*num;
        points[i].y = _lw/2;
        points[i+1].x = 0;
        points[i+1].y = _lw/2;
      }
      dev->drawFillPoly(points,num+2);
    }else{
      for(i=0; i<num; i++){
        WSCstring tmp = vlist.getWord(i,",");
        long val = atoi(tmp);
        if (_rev == False){
          points[i].x = w - w*val/100 + _lw/2;
          points[i].y = interval*i;
        }else{
          points[i].x = _lw/2;
          points[i].y = interval*i;
        }
      }
      if (_rev == False){
        points[i].x = w - _lw/2;
        points[i].y = interval*num;
        points[i+1].x = w - _lw/2;
        points[i+1].y = 0;
      }else{
        points[i].x = _lw/2;
        points[i].y = interval*num;
        points[i].x = _lw/2;
        points[i+1].y = 0;
      }
      dev->drawFillPoly(points,num+2);
    }
  }
  dev->setLineWidth(_lw);
  dev->setLineDashType(_lt);
  dev->setForeColor(_fore_color);
  dev->setHatchPattern(0);

  if (_orientation == WS_VERTICAL){
    for(i=0; i<num; i++){
      WSCstring tmp = vlist.getWord(i,",");
      long val = atoi(tmp);
      if (_rev == False){
        points[i].x = interval*i;
        points[i].y =  h - h*val/100 +_lw/2;
      }else{
        points[i].x = interval*i;
        points[i].y = _lw/2;
      }
    }
    dev->drawLines(points,num);
  }else{
    for(i=0; i<num; i++){
      WSCstring tmp = vlist.getWord(i,",");
      long val = atoi(tmp);
      if (_rev == False){
        points[i].x = w - w*val/100 + _lw/2;
        points[i].y = interval*i;
      }else{
        points[i].x = _lw/2;
        points[i].y = interval*i;
      }
    }
    dev->drawLines(points,num);
  }

  dev->endDraw();
  return WS_NO_ERR;
}



