/*------------------------------------------------------------------------------
 src/lib/djWidget/floatSliderWidget.cxx

 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
------------------------------------------------------------------------------*/

//#define DJ_DBG
#include "djSliderWidget.h"
#include <FL/Fl.h>

namespace dj {

FloatSliderWidget::FloatSliderWidget() :
  _value(0.0),
  _min(0.0), _max(1.0),
  _inc(0.1), _dec(-0.1)
{}

void FloatSliderWidget::set(double in)
{
  const double tmp = clamp(in, _min, _max);
  if (compare(tmp, _value)) return;
  _value = tmp;
  _sig.emit(_value);
  redraw();
}

void FloatSliderWidget::range(double min, double max)
{
  if (compare(min, _min) && compare(max, _max)) return;
  _min = min;
  _max = max;
  set(_value);
}

void FloatSliderWidget::inc(double inc, double dec)
{
  _inc = inc;
  _dec = dec;
}

void FloatSliderWidget::inc() { set(_value + _inc); }
void FloatSliderWidget::incBig() { set(_value + _inc * 10.0); }
void FloatSliderWidget::dec() { set(_value + _dec); }
void FloatSliderWidget::decBig() { set(_value + _dec * 10.0); }

void FloatSliderWidget::draw()
{
  drawSlider((_value - _min) / (_max - _min));
}

int FloatSliderWidget::handle(int in)
{
  const Box2i box = contentsBox();

  switch (in) {
  
    case FL_PUSH:
    case FL_DRAG: {
      take_focus();
      const double v =
        (Fl::event_x() - box.x() - handleWidth() / 2) /
        double(box.width() - handleWidth());
      set(_min + v * (_max - _min));
    } return 1;
  }
  
  return SliderWidget::handle(in);
}

}
