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

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

//#define DJ_DBG
#include "djFileNameWidget.h"
#include <djButtonWidget.h>
#include <djTextWidget.h>

namespace dj {

FileNameWidget::FileNameWidget()
{
  _widget = new TextWidget;
  PushButtonWidget * browseWidget = new PushButtonWidget("Browse");

  sizePolicy(STRETCH, FIXED);
  HLayout * layout = new HLayout(this);
  layout->margin(0);
  layout->add(_widget);
  layout->stretch(_widget);
  layout->add(browseWidget);

  _widget->sig().set(this, widgetCallback);
  browseWidget->sig().set(this, browseCallback);
}

FileNameWidget::~FileNameWidget()
{
  die();
}

void FileNameWidget::die()
{
  fileBrowserDel(this, widgetCallback);
}

void FileNameWidget::set(const Str & in)
{
  if (in == _value) return;
  _value = in;
  _sig.emit(_value);
  widgetUpdate();
}

void FileNameWidget::widgetCallback(const Str & in)
{
  set(in);
}

void FileNameWidget::browseCallback(bool)
{
  fileBrowser(this, widgetCallback);
}

void FileNameWidget::widgetUpdate()
{
  _widget->set(_value);
}

}
