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

//#define DJ_DBG
#include "djImageIoPpmWidget.h"
#include <djLabelWidget.h>

namespace dj {
namespace ImageIoPpm {

SaveWidget::SaveWidget() :
  _callbacks(true)
{
  LabelWidget * modeLabel = new LabelWidget("Mode:");
  _modeWidget = new EnumWidget<Save::MODE>(modeStr());
  _modeWidget->alignPolicy(LEFT, CENTER);
  
  VLayout * layout = new VLayout(this);
  HLayout * subLayout = new HLayout(layout);
  subLayout->margin(0);
  subLayout->add(modeLabel);
  subLayout->add(_modeWidget);
  layout->stretchAdd();
  
  widgetUpdate();
  
  _modeWidget->sig().set(this, modeCallback);
}

void SaveWidget::set(ImageSave * in)
{
  ImageSaveWidget::set(in);
  widgetUpdate();
}

void SaveWidget::widgetUpdate()
{
  _callbacks = false;
  
  Save::MODE mode = Save::MODE(0);
  if (_save) {
    StrList data = _save->attr(saveAttrStr()[Save::ATTR_MODE]);
    inflateStr(data, &mode, modeStr());
  }
  _modeWidget->set(mode);
  
  _callbacks = true;
}

void SaveWidget::modeCallback(Save::MODE in)
{
  if (! _callbacks || ! _save) return;
  _save->attr(saveAttrStr()[Save::ATTR_MODE], deflateStr(in, modeStr()));
  sig().emit(true);
  widgetUpdate();
}

}
}
