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

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

#include "prefsDialog.h"
#include "app.h"
#include <djGroupBoxWidget.h>
#include <djLabelWidget.h>
#include <djButtonWidget.h>
#include <djNumWidget.h>
#include <djEnumWidget.h>
#include <djTabWidget.h>
#include <djColorWidget.h>

namespace djview {

PrefsDialog::PrefsDialog() :
  DialogBase("Preferences")
{
  // Widgets.
  //
  TabWidget * tabWidget = new TabWidget;
  Widget * generalTab = new Widget;
  tabWidget->tabAdd("General", generalTab);
  
  // Image widgets.
  //
  GroupBoxWidget * imageGroup = new GroupBoxWidget("Images");
  LabelWidget * imageCacheLabel =
    new LabelWidget("Image cache size (megabytes):");
  _imageCacheWidget = new IntWidget;
  _imageCacheEnabledWidget = new CheckButtonWidget("Image cache.");
  _imageCacheEnabledWidget->alignPolicy(LEFT, CENTER);
  
  // File widgets.
  //
  GroupBoxWidget * fileGroup = new GroupBoxWidget("Files");
  LabelWidget * filePixelFormatLabel = new LabelWidget("Default pixel format:");
  _filePixelFormatWidget = new EnumWidget<int>(filePixelFormatStr());
  LabelWidget * filePixelTypeLabel = new LabelWidget("Default pixel type:");
  _filePixelTypeWidget = new EnumWidget<int>(filePixelTypeStr());
  
  List<LabelWidget *> labelList;
  labelList += imageCacheLabel;
  labelList += filePixelFormatLabel;
  labelList += filePixelTypeLabel;
  imageCacheLabel->labelGroup(labelList);
  filePixelFormatLabel->labelGroup(labelList);
  filePixelTypeLabel->labelGroup(labelList);
  
  // Window widgets.
  //
  GroupBoxWidget * windowGroup = new GroupBoxWidget("Windows");
  _windowFileFitWidget =
    new CheckButtonWidget("Fit window when opening files.");
  _windowFileFitWidget->alignPolicy(LEFT, CENTER);
  _windowFullscreenFitWidget =
    new CheckButtonWidget("Fit image when changing fullscreen mode.");
  _windowFullscreenFitWidget->alignPolicy(LEFT, CENTER);
  
  // View widgets.
  //
  GroupBoxWidget * viewGroup = new GroupBoxWidget("Views");
  LabelWidget * viewBgLabel = new LabelWidget("Background color:");
  _viewBgWidget = new ColorWidget;
  
  // Playback widgets.
  //
  GroupBoxWidget * playbackGroup = new GroupBoxWidget("Playback");
  _playbackFileWidget =
    new CheckButtonWidget("Start playback when opening files.");
  _playbackFileWidget->alignPolicy(LEFT, CENTER);
  _playbackFileSpeedWidget =
    new CheckButtonWidget("Use file's playback speed.");
  _playbackFileSpeedWidget->alignPolicy(LEFT, CENTER);
  _playbackEveryFrameWidget = new CheckButtonWidget("Playback every frame.");
  _playbackEveryFrameWidget->alignPolicy(LEFT, CENTER);
  
  // Miscellaneous widgets.
  //
  GroupBoxWidget * miscGroup = new GroupBoxWidget("Miscellaneous");
  _miscTooltipsWidget = new CheckButtonWidget("Tooltip help.");
  _miscTooltipsWidget->alignPolicy(LEFT, CENTER);
  
  // Image I/O widgets.
  //
  TabWidget * imageLoadTabWidget = new TabWidget;
  tabWidget->tabAdd("Image Load", imageLoadTabWidget);
  const List<ImageLoad *> imageLoadPluginList =
    djviewApp->imageLoad().pluginList();
  for (ConstListIt<ImageLoad *> it = imageLoadPluginList; it.isValid(); ++it) {
    ImageLoadWidget * widget =
      djviewApp->imageLoadWidget().pluginCopy(it()->name());
    if (! widget) continue;
    widget->set(it());
    imageLoadTabWidget->tabAdd(it()->name(), widget);
  }
  
  // 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(imageGroup);
  VLayout * groupLayout = new VLayout(imageGroup);
  groupLayout->add(_imageCacheEnabledWidget);
  HLayout * subLayout = new HLayout(groupLayout);
  subLayout->margin(0);
  subLayout->add(imageCacheLabel);
  subLayout->add(_imageCacheWidget);
  subLayout->stretchAdd();
  
  layout->add(fileGroup);
  groupLayout = new VLayout(fileGroup);
  subLayout = new HLayout(groupLayout);
  subLayout->margin(0);
  subLayout->add(filePixelFormatLabel);
  subLayout->add(_filePixelFormatWidget);
  subLayout->stretchAdd();
  subLayout = new HLayout(groupLayout);
  subLayout->margin(0);
  subLayout->add(filePixelTypeLabel);
  subLayout->add(_filePixelTypeWidget);
  groupLayout->stretchAdd();
  
  layout->add(windowGroup);
  groupLayout = new VLayout(windowGroup);
  groupLayout->add(_windowFileFitWidget);
  groupLayout->add(_windowFullscreenFitWidget);
  groupLayout->stretchAdd();
  
  layout->add(viewGroup);
  groupLayout = new VLayout(viewGroup);
  VLayout * viewBgLayout = new VLayout(groupLayout);
  viewBgLayout->margin(0);
  viewBgLayout->add(viewBgLabel);
  viewBgLayout->add(_viewBgWidget);
  groupLayout->stretchAdd();
  
  layout->add(playbackGroup);
  groupLayout = new VLayout(playbackGroup);
  groupLayout->add(_playbackFileWidget);
  groupLayout->add(_playbackFileSpeedWidget);
  groupLayout->add(_playbackEveryFrameWidget);
  groupLayout->stretchAdd();
  
  layout->add(miscGroup);
  groupLayout = new VLayout(miscGroup);
  groupLayout->add(_miscTooltipsWidget);
  groupLayout->stretchAdd();
  layout->stretchAdd();
  
  windowLayout->add(dialogFrameWidget);
  HLayout * dialogFrameLayout = new HLayout(dialogFrameWidget);
  dialogFrameLayout->stretchAdd();
  dialogFrameLayout->add(closeWidget);
  
  // Callbacks.
  //
  _imageCacheWidget->sig().set(this, imageCacheCallback);
  _imageCacheEnabledWidget->sig().set(this, imageCacheEnabledCallback);
  _filePixelFormatWidget->sig().set(this, filePixelFormatCallback);
  _filePixelTypeWidget->sig().set(this, filePixelTypeCallback);
  _windowFileFitWidget->sig().set(this, windowFileFitCallback);
  _windowFullscreenFitWidget->sig().set(this, windowFullscreenFitCallback);
  _viewBgWidget->sig().set(this, viewBgCallback);
  _playbackFileWidget->sig().set(this, playbackFileCallback);
  _playbackFileSpeedWidget->sig().set(this, playbackFileSpeedCallback);
  _playbackEveryFrameWidget->sig().set(this, playbackEveryFrameCallback);
  _miscTooltipsWidget->sig().set(this, miscTooltipsCallback);
  closeWidget->sig().set(this, closeCallback);
}

void PrefsDialog::imageCacheCallback(int in)
  { djviewApp->imageCache().max(in); }
void PrefsDialog::imageCacheEnabledCallback(bool in)
  { djviewApp->imageCacheEnabled(in); }

void PrefsDialog::filePixelFormatCallback(int in)
  { djviewApp->filePixelFormat(in); }
void PrefsDialog::filePixelTypeCallback(int in)
  { djviewApp->filePixelType(in); }

void PrefsDialog::windowFileFitCallback(bool in)
  { djviewApp->windowFileFit(in); }
void PrefsDialog::windowFullscreenFitCallback(bool in)
  { djviewApp->windowFullscreenFit(in); }

void PrefsDialog::viewBgCallback(const Color & in)
  { djviewApp->viewBg(in); }

void PrefsDialog::playbackFileCallback(bool in)
  { djviewApp->playbackFile(in); }
void PrefsDialog::playbackFileSpeedCallback(bool in)
  { djviewApp->playbackFileSpeed(in); }
void PrefsDialog::playbackEveryFrameCallback(bool in)
  { djviewApp->playbackEveryFrame(in); }

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

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

void PrefsDialog::widgetUpdate()
{
  _imageCacheWidget->set(djviewApp->imageCache().max());
  _imageCacheEnabledWidget->set(djviewApp->imageCacheEnabled());

  _filePixelFormatWidget->set(djviewApp->filePixelFormat());
  _filePixelTypeWidget->set(djviewApp->filePixelType());

  _windowFileFitWidget->set(djviewApp->windowFileFit());
  _windowFullscreenFitWidget->set(djviewApp->windowFullscreenFit());
  
  _viewBgWidget->set(djviewApp->viewBg());

  _playbackFileWidget->set(djviewApp->playbackFile());
  _playbackFileSpeedWidget->set(djviewApp->playbackFileSpeed());
  _playbackEveryFrameWidget->set(djviewApp->playbackEveryFrame());

  _miscTooltipsWidget->set(djviewApp->tooltips());
}

}
