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

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

//#define DJ_DBG
#include "djGroupBoxWidget.h"
#include <FL/fl_draw.h>

namespace dj {

GroupBoxWidget::GroupBoxWidget(const Str & label) :
  _label(label)
{
  labelfont(dj::fontBold());
  labelsize(dj::fontSize());
  
  new VLayout(this);
}

void GroupBoxWidget::label(const Str & in)
{
  _label = in;
  redraw();
}

const Vec2i GroupBoxWidget::sizeHint() const
{
  DJ_DBG_FNC("dj::GroupBoxWidget::sizeHint");
  
  const Vec2i size = layout() ? layout()->sizeHint() : Vec2i();
  DJ_DBG_PRN("size = " << size);
  
  fl_font(labelfont(), labelsize());
  const Vec2i out(
    max(
      ceil(fl_width(_label.toAscii())) + layoutWidgetMargin() * 4,
      size.x()
    ) + 2 * 2,
    labelsize() + size.y() + layoutWidgetMargin() + 2
  );
  DJ_DBG_PRN("out = " << out);
  return out;
}

void GroupBoxWidget::resize(int x, int y, int w, int h)
{
  //DJ_DBG_LOG();
  DJ_DBG_FNC("dj::GroupBoxWidget::resize");
  DJ_DBG_PRN("in = " << Box2i(x, y, w, h));
  
  Fl_Widget::resize(x, y, w, h);
  LayoutItem::resize(x, y, w, h);
  if (layout())
    layout()->resize(
      x + 2, y + labelsize() + layoutWidgetMargin(),
      w - 2 * 2, h - labelsize() - layoutWidgetMargin() - 2
    );
}
  
void GroupBoxWidget::draw()
{
  Widget::draw();

  // Draw box.
  //
  //fl_draw_box(FL_BORDER_FRAME, x(), y(), w(), h(), FL_BLACK);
  fl_draw_box(
    FL_ENGRAVED_FRAME,
    x(), y() + labelsize() / 2 + layoutWidgetMargin(),
    w(), h() - labelsize() / 2 - layoutWidgetMargin(),
    color()
  );
  
  // Draw label.
  //
  fl_font(labelfont(), labelsize());
  fl_color(FL_BACKGROUND_COLOR);
  fl_rectf(
    x() + layoutWidgetMargin() + 2,
    y(),
    ceil(fl_width(_label.toAscii())) + layoutWidgetMargin() * 2,
    labelsize() + layoutWidgetMargin()
  );
  fl_color(labelcolor());
  fl_draw(
    _label.toAscii(),
    x() + layoutWidgetMargin() * 2 + 2,
    y() + labelsize() - fl_descent() + layoutWidgetMargin()
  );
}

}
