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

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

//#define DJ_DBG
#include "djWidget.h"
#include <FL/Fl.H>
#include <FL/fl_draw.H>

namespace dj {

WidgetBase::WidgetBase() :
  LayoutItem(WIDGET),
  _layout(0)
{
  DJ_DBG_FNC("dj::WidgetBase::WidgetBase");
}

WidgetBase::~WidgetBase()
{
  DJ_DBG_FNC("dj::WidgetBase::~WidgetBase");
  
  if (_layout) delete _layout;
}

void WidgetBase::tooltip(const Str & in)
{
  _tooltip = in;
}

const Vec2i WidgetBase::sizeHint() const
{
  return _layout ? _layout->sizeHint() : LayoutItem::sizeHint();
}

void WidgetBase::resize(int x, int y, int w, int h)
{
  LayoutItem::resize(x, y, w, h);
  if (_layout) _layout->resize(x, y, w, h);
}

void WidgetBase::layout(Layout * in)
{
  if (_layout) delete _layout;
  _layout = in;
  _layout->parent(this);
}

static void _drawItem(LayoutItem * in)
{
  const Box2i & geom = in->geom();
  const int x = geom.x(), y = geom.y(), w = geom.width(), h = geom.height();
  switch (in->type()) {
    case LayoutItem::WIDGET:
      fl_color(FL_RED);
      fl_rect(x + 0, y + 0, w - 0, h - 0);
      break;
    case LayoutItem::LAYOUT:
      fl_color(FL_BLUE);
      fl_rect(x + 0, y + 0, w - 0, h - 0);
      break;
    case LayoutItem::SPACER:
      fl_color(FL_CYAN);
      fl_rectf(x + 0, y + 0, w - 0, h - 0);
      break;
  }
}

static void _drawLayout(Layout * in)
{
  const List<LayoutItem *> & itemList = in->itemList();
  for (ConstListIt<LayoutItem *> it = itemList; it.isValid(); ++it)
    if (LayoutItem::LAYOUT == it()->type())
      _drawLayout(static_cast<Layout *>(it()));
    else
      _drawItem(it());
  _drawItem(in);
}

void WidgetBase::draw()
{
  //if (layout()) _drawLayout(layout());
  //_drawItem(this);
}

Widget::Widget() :
  Fl_Group(0, 0, 0, 0),
  _tooltipAscii(0)
{
  DJ_DBG_FNC("dj::Widget::Widget");
  
  labelfont(dj::font());
  labelsize(dj::fontSize());
  end();
}

Widget::~Widget()
{
  DJ_DBG_FNC("dj::Widget::~Widget");
  
  if (_tooltipAscii) delete [] _tooltipAscii;
}


int Widget::frameWidth() const { return Fl::box_dx(box()); }

const Vec2i Widget::sizeHint() const
{
  return
    (layout() ? layout()->sizeHint() : Vec2i()) +
    Vec2i(Fl::box_dw(box()), Fl::box_dh(box()));
}

void Widget::resize(int x, int y, int w, int h)
{
  Fl_Widget::resize(x, y, w, h);
  LayoutItem::resize(x, y, w, h);
  if (layout())
    layout()->resize(
      x + Fl::box_dx(box()), y + Fl::box_dy(box()),
      w - Fl::box_dw(box()), h - Fl::box_dh(box())
    );
}

int Widget::visible() const { return Fl_Group::visible(); }

void Widget::tooltip(const Str & in)
{
  if (_tooltipAscii) delete [] _tooltipAscii;
  _tooltipAscii = toAscii(in);
  Fl_Group::tooltip(_tooltipAscii);

  WidgetBase::tooltip(in);
}

void Widget::draw()
{
  Fl_Group::draw();
  WidgetBase::draw();
}

int Widget::handle(int in)
{
  switch (in) {
  
    case FL_KEYBOARD:
      switch (Fl::event_key()) {
        case FL_Left:
        case FL_Right:
        case FL_Up:
        case FL_Down: return 0;
      }
      break;
      
    case FL_ENTER:
    case FL_LEAVE: return 1;
  }
  
  return Fl_Group::handle(in);
}

}
