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

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

//#define DJ_DBG
#include "djNumWidget.h"
#include <djButtonWidget.h>
#include <djTextWidget.h>
#include <FL/fl_draw.h>
#include <FL/Fl.h>

namespace dj {

NumWidget::NumWidget() :
  _toolWidget(true)
{
  _widget = new TextWidget;
  _incWidget = new ToolButtonWidget("toolInc", 0);
  _incWidget->type(ToolButtonWidget::REPEAT);
  _decWidget = new ToolButtonWidget("toolDec", 0);
  _decWidget->type(ToolButtonWidget::REPEAT);
  
  sizePolicy(FIXED, FIXED);
  HLayout * layout = new HLayout(this);
  layout->spacing(2);
  layout->margin(0);
  layout->add(_widget);
  layout->stretch(_widget);
  VLayout * subLayout = new VLayout(layout);
  subLayout->spacing(0);
  subLayout->margin(0);
  subLayout->add(_incWidget);
  subLayout->stretchAdd();
  subLayout->add(_decWidget);

  toolWidgetUpdate();

  _widget->sig().set(this, widgetCallback);
  _incWidget->sig().set(this, incCallback);
  _decWidget->sig().set(this, decCallback);
}

void NumWidget::toolWidget(bool in)
{
  if (in == _toolWidget) return;
  _toolWidget = in;
  toolWidgetUpdate();
}

int NumWidget::handle(int in)
{
  switch (in) {
  
    case FL_KEYBOARD:
      switch (Fl::event_key()) {
      
        case FL_Up: inc(); return 1;
        case FL_Page_Up: incBig(); return 1;
        case FL_Down: dec(); return 1;
        case FL_Page_Down: decBig(); return 1;
      }
      break;
  }
  
  return Widget::handle(in);
}

void NumWidget::widgetCallback(const Str &) {}

void NumWidget::incCallback(bool)
{
  inc();
}

void NumWidget::decCallback(bool)
{
  dec();
}

void NumWidget::widgetUpdate(const Str & in)
{
  _widget->set(in);
}

void NumWidget::toolWidgetUpdate()
{
  if (_toolWidget) {
    _incWidget->show();
    _decWidget->show();
  } else {
    _incWidget->hide();
    _decWidget->hide();
  }
}

}
