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

#include "djDialog.h"
#include <djButtonWidget.h>
#include <djLabelWidget.h>
#include <djTextWidget.h>

namespace dj {

DialogBase::DialogBase(const Str & title) :
  Window(title)
{}

DialogBase::~DialogBase()
{}

MessageDialog::MessageDialog(const Str & message) :
  DialogBase("Message")
{
  // Widgets.
  //
  Widget * frameWidget = new Widget;
  frameWidget->box(FL_THIN_UP_BOX);
  _widget = new LabelWidget(message);
  _widget->sizePolicy(STRETCH, STRETCH);
  _widget->alignPolicy(CENTER, CENTER);
  Widget * dialogFrameWidget = new Widget;
  dialogFrameWidget->box(FL_THIN_UP_BOX);
  PushButtonWidget * closeWidget = new PushButtonWidget("Clos&e");

  // Layout.
  //
  VLayout * windowLayout = new VLayout(this);
  windowLayout->margin(0);
  windowLayout->spacing(0);
  windowLayout->add(frameWidget);
  windowLayout->stretch(frameWidget);
  VLayout * layout = new VLayout(frameWidget);
  layout->add(_widget);
  layout->stretch(_widget);
  windowLayout->add(dialogFrameWidget);
  HLayout * dialogLayout = new HLayout(dialogFrameWidget);
  dialogLayout->stretchAdd();
  dialogLayout->add(closeWidget);
  
  // Initialize.
  //
  size(max(sizeHint(), dialogSize()));
  
  // Callbacks.
  //
  closeWidget->sig().set(this, closeCallback);
}

void MessageDialog::set(const Str & in)
{
  _widget->set(in);
  size(max(sizeHint(), dialogSize()));
}

const Str MessageDialog::get() const
{
  return _widget->get();
}

void MessageDialog::closeCallback(bool)
{
  hide();
}

QuestionDialog::QuestionDialog(const Str & message) :
  DialogBase("Question")
{
  // Widgets.
  //
  Widget * frameWidget = new Widget;
  frameWidget->box(FL_THIN_UP_BOX);
  _widget = new LabelWidget(message);
  _widget->sizePolicy(STRETCH, STRETCH);
  _widget->alignPolicy(CENTER, CENTER);
  Widget * dialogFrameWidget = new Widget;
  dialogFrameWidget->box(FL_THIN_UP_BOX);
  PushButtonWidget * okWidget = new PushButtonWidget("&Yes");
  PushButtonWidget * closeWidget = new PushButtonWidget("&No");

  // Layout.
  //
  VLayout * windowLayout = new VLayout(this);
  windowLayout->margin(0);
  windowLayout->spacing(0);
  windowLayout->add(frameWidget);
  windowLayout->stretch(frameWidget);
  VLayout * layout = new VLayout(frameWidget);
  layout->add(_widget);
  layout->stretch(_widget);
  windowLayout->add(dialogFrameWidget);
  HLayout * dialogLayout = new HLayout(dialogFrameWidget);
  dialogLayout->stretchAdd();
  dialogLayout->add(okWidget);
  dialogLayout->add(closeWidget);
  
  // Initialize.
  //
  size(max(sizeHint(), dialogSize()));
  
  // Callbacks.
  //
  okWidget->sig().set(this, okCallback);
  closeWidget->sig().set(this, closeCallback);
}

void QuestionDialog::set(const Str & in)
{
  _widget->set(in);
  size(max(sizeHint(), dialogSize()));
}

const Str QuestionDialog::get() const
{
  return _widget->get();
}

void QuestionDialog::okCallback(bool)
{
  _okSig.emit(true);
  hide();
}

void QuestionDialog::closeCallback(bool)
{
  hide();
}

InputDialog::InputDialog(const Str & message, const Str & value) :
  DialogBase("Input")
{
  // Widgets.
  //
  Widget * frameWidget = new Widget;
  frameWidget->box(FL_THIN_UP_BOX);
  _widget = new LabelWidget(message);
  //_widget->sizePolicy(STRETCH, STRETCH);
  _widget->alignPolicy(LEFT, LEFT);
  _inputWidget = new TextWidget(value);
  _inputWidget->sizePolicy(STRETCH, STRETCH);
  Widget * dialogFrameWidget = new Widget;
  dialogFrameWidget->box(FL_THIN_UP_BOX);
  PushButtonWidget * okWidget = new PushButtonWidget("&Ok");
  PushButtonWidget * closeWidget = new PushButtonWidget("Clos&e");

  // Layout.
  //
  VLayout * windowLayout = new VLayout(this);
  windowLayout->margin(0);
  windowLayout->spacing(0);
  windowLayout->add(frameWidget);
  windowLayout->stretch(frameWidget);
  VLayout * layout = new VLayout(frameWidget);
  layout->add(_widget);
  layout->stretch(_widget);
  layout->add(_inputWidget);
  windowLayout->add(dialogFrameWidget);
  HLayout * dialogLayout = new HLayout(dialogFrameWidget);
  dialogLayout->stretchAdd();
  dialogLayout->add(okWidget);
  dialogLayout->add(closeWidget);
  
  // Initialize.
  //
  size(max(sizeHint(), dialogSize()));
  
  // Callbacks.
  //
  _inputWidget->actionSig().set(this, inputCallback);
  okWidget->sig().set(this, okCallback);
  closeWidget->sig().set(this, closeCallback);
}

void InputDialog::set(const Str & in)
{
  _widget->set(in);
  size(max(sizeHint(), dialogSize()));
}

const Str InputDialog::get() const
{
  return _widget->get();
}

void InputDialog::value(const Str & in)
{
  _inputWidget->set(in);
  size(max(sizeHint(), dialogSize()));
}

const Str InputDialog::value() const
{
  return _inputWidget->get();
}

void InputDialog::inputCallback(bool)
{
  _okSig.emit(_inputWidget->get());
  hide();
}

void InputDialog::okCallback(bool)
{
  _okSig.emit(_inputWidget->get());
  hide();
}

void InputDialog::closeCallback(bool)
{
  hide();
}

}
