/*------------------------------------------------------------------------------
 src/bin/djvlut/prefsDialog.cxx

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

//#define DJ_DBG
#include "prefsDialog.h"
#include "app.h"
#include <djGroupBoxWidget.h>
#include <djLabelWidget.h>
#include <djButtonWidget.h>
#include <djTabWidget.h>

namespace djvlut {

PrefsDialog::PrefsDialog() :
  DialogBase("Preferences")
{
  DJ_DBG_FNC("PrefsDialog::PrefsDialog");
  
  // Widgets.
  //
  TabWidget * tabWidget = new TabWidget;
  Widget * generalTab = new Widget;
  tabWidget->tabAdd("General", generalTab);

  // Miscellaneous widgets.
  //
  GroupBoxWidget * miscGroup = new GroupBoxWidget("Miscellaneous");
  _miscSaveWindowGeomWidget = new CheckButtonWidget("Save window geometry.");
  _miscSaveWindowGeomWidget->alignPolicy(LEFT, CENTER);
  _miscTooltipsWidget = new CheckButtonWidget("Tooltip help.");
  _miscTooltipsWidget->alignPolicy(LEFT, CENTER);
  
  // Image I/O widgets.
  //
  _imageLoadWidget = new TabWidget;
  tabWidget->tabAdd("Image Load", _imageLoadWidget);
  _imageSaveWidget = new TabWidget;
  tabWidget->tabAdd("Image Save", _imageSaveWidget);
  
  // Dialog widgets.
  //
  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(tabWidget);
  windowLayout->stretch(tabWidget);
  
  VLayout * layout = new VLayout(generalTab);
  layout->add(miscGroup);
  VLayout * groupLayout = new VLayout(miscGroup);
  groupLayout->add(_miscSaveWindowGeomWidget);
  groupLayout->add(_miscTooltipsWidget);
  groupLayout->stretchAdd();
  layout->stretchAdd();
  
  windowLayout->add(dialogFrameWidget);
  HLayout * dialogFrameLayout = new HLayout(dialogFrameWidget);
  dialogFrameLayout->stretchAdd();
  dialogFrameLayout->add(closeWidget);
  
  // Initialize.
  //
  widgetUpdate();
  
  // Callbacks.
  //
  _miscSaveWindowGeomWidget->sig().set(this, miscSaveWindowGeomCallback);
  _miscTooltipsWidget->sig().set(this, miscTooltipsCallback);
  closeWidget->sig().set(this, closeCallback);
}

void PrefsDialog::miscSaveWindowGeomCallback(bool in)
  { djvlutApp->saveWindowGeom(in); }

void PrefsDialog::miscTooltipsCallback(bool in)
  { djvlutApp->tooltips(in); }

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

void PrefsDialog::widgetUpdate()
{
  // Image I/O.
  //
  _imageLoadWidget->tabDel();
  const List<ImageLoad *> imageLoadList = djvlutApp->imageLoad().pluginList();
  for (ConstListIt<ImageLoad *> it = imageLoadList; it.isValid(); ++it) {
    ImageLoadWidget * widget =
      djvlutApp->imageLoadWidget().pluginCopy(it()->name());
    if (! widget) continue;
    widget->set(it());
    _imageLoadWidget->tabAdd(it()->name(), widget);
  }
  _imageSaveWidget->tabDel();
  const List<ImageSave *> imageSaveList = djvlutApp->imageSave().pluginList();
  for (ConstListIt<ImageSave *> it = imageSaveList; it.isValid(); ++it) {
    ImageSaveWidget * widget =
      djvlutApp->imageSaveWidget().pluginCopy(it()->name());
    if (! widget) continue;
    widget->set(it());
    _imageSaveWidget->tabAdd(it()->name(), widget);
  }
  
  // Miscellaneous.
  //
  _miscSaveWindowGeomWidget->set(djvlutApp->saveWindowGeom());
  _miscTooltipsWidget->set(djvlutApp->tooltips());
}

}
