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

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

//#define DJ_DBG
#include "window.h"
#include "app.h"
#include <djImageOpStack.h>
#include <djMenuWidget.h>

namespace djview {

void Window::menuUpdate()
{
  _callbacks = false;
  
  _menuItems->del();
  
  // File menu.
  //
  _menuItems->add("&File", 0, 0, 0, FL_SUBMENU);
  _menuItems->add("&Open", FL_CTRL + 'o', fileOpenCallback, this);
  
  _menuItems->add("Re&cent", 0, 0, 0, FL_SUBMENU);
  _fileMenuRecentId.del();
  const StrList fileRecentList = djviewApp->fileRecentList();
  for (ConstStrListIt it = fileRecentList; it.isValid(); ++it)
    _fileMenuRecentId += _menuItems->add(it(), 0, fileRecentListCallback, this);
  _menuItems->end();
  
  _menuItems->add("&Reload", FL_CTRL + 'r', fileReloadCallback, this, 0);
  _menuItems->add(
    "Clos&e", FL_CTRL + 'e', fileCloseCallback, this, FL_MENU_DIVIDER
  );
  
  _menuItems->add("&Proxy Scale", 0, 0, 0, FL_SUBMENU);
  _fileMenuProxyId.del();
  const StrList proxyStr = fileProxyStr();
  for (ConstStrListIt it = proxyStr; it.isValid(); ++it)
    _fileMenuProxyId += _menuItems->add(
      it(), FL_F + 1 + it.count(), fileProxyCallback, this,
      FL_MENU_RADIO | (it.count() == _fileProxy ? FL_MENU_VALUE : 0)
    );
  _menuItems->end();
  
  _menuItems->add("Pixel &Format", 0, 0, 0, FL_SUBMENU);
  _fileMenuPixelFormatId.del();
  const StrList pixelFormatStr = filePixelFormatStr();
  for (ConstStrListIt it = pixelFormatStr; it.isValid(); ++it)
    _fileMenuPixelFormatId += _menuItems->add(
      it(), 0, filePixelFormatCallback, this,
      FL_MENU_RADIO | (it.count() == _filePixelFormat ? FL_MENU_VALUE : 0)
    );
  _menuItems->end();
  
  _menuItems->add("Pixel &Type", 0, 0, 0, FL_SUBMENU);
  _fileMenuPixelTypeId.del();
  const StrList pixelTypeStr = filePixelTypeStr();
  for (ConstStrListIt it = pixelTypeStr; it.isValid(); ++it)
    _fileMenuPixelTypeId += _menuItems->add(
      it(), 0, filePixelTypeCallback, this,
      FL_MENU_RADIO | (it.count() == _filePixelType ? FL_MENU_VALUE : 0)
    );
  _menuItems->end();
  
  _menuItems->add("&Preferences", 0, prefsCallback, this);
  _menuItems->add("&About", 0, aboutCallback, this, FL_MENU_DIVIDER);
  _menuItems->add("E&xit", FL_CTRL + 'q', exitCallback, this);
  _menuItems->end();
  
  // Window menu.
  //
  _menuItems->add("&Window", 0, 0, 0, FL_SUBMENU);
  _menuItems->add("&New", FL_CTRL + 'n', windowNewCallback, this);
  _menuItems->add("&Copy", FL_CTRL + 'c', windowCopyCallback, this);
  _menuItems->add(
    "Clos&e", FL_CTRL + 'w', windowDelCallback, this, FL_MENU_DIVIDER
  );
  _menuItems->add("&Fit To Image", 'f', windowFitCallback, this);
  _menuItems->add(
    "F&ullscreen", 'u', windowFullscreenCallback, this,
    FL_MENU_TOGGLE | (fullscreen() ? FL_MENU_VALUE : 0)
  );
  _menuItems->add(
    "C&ontrols", ' ', windowControlsCallback, this,
    FL_MENU_TOGGLE | (_windowControls ? FL_MENU_VALUE : 0)
  );
  _menuItems->end();
  
  // View menu.
  //
  _menuItems->add("&View", 0, 0, 0, FL_SUBMENU);
  _menuItems->add("Move &Left", FL_Left, viewLeftCallback, this);
  _menuItems->add("Move &Right", FL_Right, viewRightCallback, this);
  _menuItems->add("Move &Up", FL_Up, viewUpCallback, this);
  _menuItems->add(
    "Move &Down", FL_Down, viewDownCallback, this, FL_MENU_DIVIDER
  );
  _menuItems->add("Zoom &In", '=', viewZoomInCallback, this);
  _menuItems->add("Zoom &Out", '-', viewZoomOutCallback, this);
  _menuItems->add(
	  "Zoom R&eset", '0', viewZoomResetCallback, this, FL_MENU_DIVIDER
  );
  _menuItems->add("&Center", FL_End, viewCenterCallback, this);
  _menuItems->add("Re&set", FL_Home, viewResetCallback, this);
  _menuItems->add(
    "&Fit To Image", FL_Insert, viewFitCallback, this, FL_MENU_DIVIDER
  );
  
  _menuItems->add("&Mirror", 0, 0, 0, FL_SUBMENU);
  _viewMenuMirrorId.del();
  const StrList imageOpMirrorStr = dj::imageOpMirrorStr();
  for (ConstStrListIt it = imageOpMirrorStr; it.isValid(); ++it)
    _viewMenuMirrorId += _menuItems->add(
      it(), FL_F + 5 + it.count(), viewMirrorCallback, this,
      FL_MENU_RADIO | (it.count() == _imageMirror ? FL_MENU_VALUE : 0)
    );
  _menuItems->end();
  
  _menuItems->add("Ro&tate", 0, 0, 0, FL_SUBMENU);
  _viewMenuRotateId.del();
  const StrList imageOpRotate90Str = dj::imageOpRotate90Str();
  for (ConstStrListIt it = imageOpRotate90Str; it.isValid(); ++it)
    _viewMenuRotateId += _menuItems->add(
      it(), FL_CTRL + FL_F + 5 + it.count(), viewRotateCallback, this,
      FL_MENU_RADIO | (it.count() == _imageRotate ? FL_MENU_VALUE : 0)
    );
  _menuItems->end();
  
  _menuItems->add("Image O&perations", 0, 0, 0, FL_SUBMENU);
  _viewMenuImageOpId.del();
  _viewMenuImageOpId += _menuItems->add(
      "None", '`', viewImageOpCallback, this,
      FL_MENU_RADIO | (0 == _imageOp ? FL_MENU_VALUE : 0)
    );
  for (int i = 0; i < djviewApp->imageOpStack().stackSize(); ++i)
    _viewMenuImageOpId += _menuItems->add(
      djviewApp->imageOpStack().stackName(i), i <= 9 ? ('1' + i) : 0,
      viewImageOpCallback, this,
      FL_MENU_RADIO | ((i + 1) == _imageOp ? FL_MENU_VALUE : 0)
    );
  _menuItems->end();
  
  _menuItems->add(
    "&HUD", 'h', viewHudCallback, this,
    FL_MENU_TOGGLE | (_viewHud ? FL_MENU_VALUE : 0)
  );
  
  _menuItems->end();

  // Playback menu.
  //
  _menuItems->add("&Playback", 0, 0, 0, FL_SUBMENU);
  
  _playbackMenuId.del();
  _playbackMenuId += _menuItems->add(
    "&Stop Playback", '.', playbackStopCallback, this,
    FL_MENU_RADIO | (PLAYBACK_STOP == _playback ? FL_MENU_VALUE : 0)
  );
  _playbackMenuId += _menuItems->add(
    "Play &Forward", '/', playbackForwardCallback, this,
    FL_MENU_RADIO | (PLAYBACK_FORWARD == _playback ? FL_MENU_VALUE : 0)
  );
  _playbackMenuId += _menuItems->add(
    "Play &Reverse", ',', playbackReverseCallback, this,
    FL_MENU_RADIO | (PLAYBACK_REVERSE == _playback ? FL_MENU_VALUE : 0) |
      FL_MENU_DIVIDER
  );
  
  _menuItems->add("S&tart Frame", ';', playbackFrameStartCallback, this);
  _menuItems->add("&End Frame", '\'', playbackFrameEndCallback, this);
  _menuItems->add("Frame Forward", ']', playbackFrameNextCallback, this);
  _menuItems->add(
    "Frame Forward 10", FL_SHIFT + ']', playbackFrameNext10Callback, this
  );
  _menuItems->add("Frame Backward", '[', playbackFramePrevCallback, this);
  _menuItems->add(
    "Frame Backward 10", FL_SHIFT + '[', playbackFramePrev10Callback, this,
    FL_MENU_DIVIDER
  );
  _menuItems->add(
    "&In Point", 'i', playbackInCallback, this,
    FL_MENU_TOGGLE | (_playbackIn ? FL_MENU_VALUE : 0)
  );
  _menuItems->add(
    "&Out Point", 'o', playbackOutCallback, this,
    FL_MENU_TOGGLE | (_playbackOut ? FL_MENU_VALUE : 0)
  );
  _menuItems->add(
    "&Mark In Point", FL_SHIFT + 'i', playbackInMarkCallback, this
  );
  _menuItems->add(
    "M&ark Out Point", FL_SHIFT + 'o', playbackOutMarkCallback, this,
    FL_MENU_DIVIDER
  );
  _menuItems->add(
    "Play E&very Frame", 0, playbackEveryFrameCallback, this,
    FL_MENU_TOGGLE | (_playbackEveryFrame ? FL_MENU_VALUE : 0)
  );
  _menuItems->end();

  // Tool menu.
  //
  _menuItems->add("&Tools", 0, 0, 0, FL_SUBMENU);
  _menuItems->add(
    "&Tool Mode", 't', toolModeCallback, this,
    FL_MENU_TOGGLE | (_toolMode ? FL_MENU_VALUE : 0)
  );
  _menuItems->add("&Magnify Tool", FL_F + 9, toolMagnifyCallback, this);
  _menuItems->add(
    "&Color Sample Tool", FL_F + 10, toolColorSampleCallback, this
  );
  _menuItems->add(
    "Image &Information Tool", FL_F + 11, toolImageInfoCallback, this
  );
  _menuItems->add(
    "Image &Operation Tool", FL_F + 12, toolImageOpCallback, this
  );
  _menuItems->end();
  
  _menuItems->end();
  _menuItems->set(_menubar);
  
  _callbacks = true;
}

}
