/*------------------------------------------------------------------------------
 src/lib/djWidget/intSliderWidget.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 {

IntSliderWidget::IntSliderWidget() :
  _value(0),
  _min(0), _max(100),
  _inc(1), _dec(-1)
{}

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

void IntSliderWidget::range(int min, int max)
{
  if (min == _min && max == _max) return;
  _min = min;
  _max = max;
  set(_value);
}

void IntSliderWidget::inc(int inc, int dec)
{
  _inc = inc;
  _dec = dec;
}

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

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

int IntSliderWidget::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 + int(v * (_max - _min) + 0));
    } return 1;
  }
  
  return SliderWidget::handle(in);
}

}
