/*------------------------------------------------------------------------------
 src/lib/djWidget/sliderWidget.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>
#include <FL/fl_draw.h>

namespace dj {

SliderWidget::SliderWidget()
{
  box(FL_DOWN_FRAME);
  visible_focus(true);

  sizePolicy(STRETCH, FIXED);
}

SliderWidget::~SliderWidget() {}

const Vec2i SliderWidget::sizeHint() const
{
  return Vec2i(layoutButtonSize() * 2, labelsize()) + frameWidth() * 2 + 2 * 2;
}

const Box2i SliderWidget::contentsBox() const
{
  return Box2i(
    x() + frameWidth(), y() + frameWidth(),
    w() - frameWidth() * 2, h() - frameWidth() * 2
  );
}

void SliderWidget::drawSlider(double in)
{
  WidgetBase::draw();
  
  // Draw background.
  //
  const Box2i box = contentsBox();
  drawBackground(box);
  
  // Draw box.
  //
  Widget::draw();
  
  // Draw handle.
  //
  const Box2i handleBox(
    box.x() + int((box.width() - handleWidth()) * in), box.y(),
    handleWidth(), box.height()
  );
  fl_draw_box(
    FL_UP_BOX,
    handleBox.x(), handleBox.y(), handleBox.width(), handleBox.height(),
    color()
  );
  if (Fl::focus() == this)
    draw_focus(
      FL_UP_BOX,
      handleBox.x(), handleBox.y(), handleBox.width(), handleBox.height()
    );
}

void SliderWidget::drawBackground(const Box2i & in)
{
  fl_color(FL_BACKGROUND2_COLOR);
  //fl_color(FL_RED);
  fl_rectf(in.x(), in.y(), in.width(), in.height());
}

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

  switch (in) {
      
    case FL_KEYBOARD: {
      switch (Fl::event_key()) {
        case FL_Up:
        case FL_Right: inc(); return 1;
        case FL_Page_Up: incBig(); return 1;
        case FL_Down:
        case FL_Left: dec(); return 1;
        case FL_Page_Down: decBig(); return 1;
      }
    } break;
      
    case FL_FOCUS:
    case FL_UNFOCUS:
      redraw();
      return 1;
  }
  
  return Widget::handle(in);
}

}
