cmake_minimum_required(VERSION 2.6)

project(KWWidgets)

# --------------------------------------------------------------------------
# To use this library in a larger project you will need to SUBDIR into this
# directory so that it gets built and then you will use the following variables
# in your CMakeLists files to get the proper include paths and libraries

set(KWWidgets_INCLUDE_PATH 
  "${CMAKE_CURRENT_SOURCE_DIR};${CMAKE_CURRENT_BINARY_DIR}/AutoGeneratedClasses;${CMAKE_CURRENT_BINARY_DIR}"
  CACHE INTERNAL "include paths for KWWidgets"
  )

set(KWWidgets_LIBRARIES KWWidgets CACHE INTERNAL "libraries for KWWidgets")

# You will also need to define a value for the following variables:
# KWWidgets_INSTALL_BIN_DIR          - binary dir (executables)
# KWWidgets_INSTALL_LIB_DIR          - library dir (libs)
# KWWidgets_INSTALL_DATA_DIR         - share dir (say, examples, data, etc)
# KWWidgets_INSTALL_INCLUDE_DIR      - include dir (headers)
# KWWidgets_INSTALL_PACKAGE_DIR      - package/export configuration files
# KWWidgets_VTK_INSTALL_PACKAGE_DIR  - VTK package/export configuration files
# KWWidgets_INSTALL_NO_DEVELOPMENT   - do not install development files
# KWWidgets_INSTALL_NO_RUNTIME       - do not install runtime files
# KWWidgets_INSTALL_NO_DOCUMENTATION - do not install documentation files
# --------------------------------------------------------------------------

# --------------------------------------------------------------------------
# Version

set(KWWidgets_MAJOR_VERSION 1)
set(KWWidgets_MINOR_VERSION 0)
set(KWWidgets_PATCH_VERSION 0)
set(KWWidgets_VERSION
  "${KWWidgets_MAJOR_VERSION}.${KWWidgets_MINOR_VERSION}.${KWWidgets_PATCH_VERSION}")

# --------------------------------------------------------------------------
# We need VTK

if(NOT VTK_SOURCE_DIR)
  find_package(VTK REQUIRED)
  include(${VTK_USE_FILE})
  if(NOT VTK_WRAP_TCL)
    message(FATAL_ERROR "Please set VTK_WRAP_TCL to ON in VTK")
  endif(NOT VTK_WRAP_TCL)
  if(VTK_MAJOR_VERSION LESS 5)
    message(FATAL_ERROR "Please use VTK 5.0 or later")
  endif(VTK_MAJOR_VERSION LESS 5)
else(NOT VTK_SOURCE_DIR)
  if(NOT VTK_WRAP_TCL)
    include_directories(${VTK_TK_RESOURCES_DIR})
  endif(NOT VTK_WRAP_TCL)
endif(NOT VTK_SOURCE_DIR)

# --------------------------------------------------------------------------
# Output directories

if(NOT LIBRARY_OUTPUT_PATH)
  set(LIBRARY_OUTPUT_PATH ${KWWidgets_BINARY_DIR}/bin CACHE INTERNAL
    "Single output directory for building all libraries.")
endif(NOT LIBRARY_OUTPUT_PATH)
set(KWWidgets_LIBRARY_DIRS ${LIBRARY_OUTPUT_PATH})
set(KWWidgets_RUNTIME_DIRS ${LIBRARY_OUTPUT_PATH})

if(NOT EXECUTABLE_OUTPUT_PATH)
  set(EXECUTABLE_OUTPUT_PATH ${KWWidgets_BINARY_DIR}/bin CACHE INTERNAL
    "Single output directory for building all executables.")
endif(NOT EXECUTABLE_OUTPUT_PATH)

# --------------------------------------------------------------------------
# Some flags

include_regular_expression("^(vtk|kw|icons).*$")
set(CMAKE_C_FLAGS "${CMAKE_ANSI_CFLAGS} ${CMAKE_C_FLAGS}")
if(CMAKE_COMPILER_2005)
  add_definitions(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE)
endif(CMAKE_COMPILER_2005)

set(BUILD_SHARED_LIBS ${VTK_BUILD_SHARED_LIBS})
set(KWWidgets_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})

set(KWWidgets_CMAKE_DIR "${KWWidgets_SOURCE_DIR}/CMake" CACHE INTERNAL "")
subdirs(CMake)
# Make sure VTK_CMAKE_DIR is at the beginning so that VTK's FindTCL is
# used before CMake's FindTCL.
set(CMAKE_MODULE_PATH "${VTK_CMAKE_DIR}" ${CMAKE_MODULE_PATH} "${KWWidgets_CMAKE_DIR}")

set(KWWidgets_CONFIGURATION_TYPES ${CMAKE_CONFIGURATION_TYPES})

mark_as_advanced(CMAKE_BACKWARDS_COMPATIBILITY)

include("${KWWidgets_SOURCE_DIR}/Utilities/tkdnd/TkDnD.cmake")
tkdnd_get_library(KWWidgets_USE_TKDND TKDND_SRCS TKDND_INCLUDE_PATH TKDND_LIBS)

include("${KWWidgets_SOURCE_DIR}/Utilities/TkTreeCtrl/TkTreeCtrl.cmake")
tktreectrl_get_library(KWWidgets_USE_TKTREECTRL TKTREECTRL_SRCS TKTREECTRL_INCLUDE_PATH TKTREECTRL_LIBS)

# --------------------------------------------------------------------------
# Install directories

if(NOT KWWidgets_INSTALL_BIN_DIR)
  set(KWWidgets_INSTALL_BIN_DIR "/bin")
endif(NOT KWWidgets_INSTALL_BIN_DIR)

if(NOT KWWidgets_INSTALL_LIB_DIR)
  set(KWWidgets_INSTALL_LIB_DIR "/lib/${PROJECT_NAME}")
endif(NOT KWWidgets_INSTALL_LIB_DIR)

if(NOT KWWidgets_INSTALL_DATA_DIR)
  set(KWWidgets_INSTALL_DATA_DIR "/share/${PROJECT_NAME}"
    CACHE INTERNAL "")
endif(NOT KWWidgets_INSTALL_DATA_DIR)

if(NOT KWWidgets_INSTALL_INCLUDE_DIR)
  set(KWWidgets_INSTALL_INCLUDE_DIR "/include/${PROJECT_NAME}")
endif(NOT KWWidgets_INSTALL_INCLUDE_DIR)

if(NOT KWWidgets_INSTALL_PACKAGE_DIR)
  set(KWWidgets_INSTALL_PACKAGE_DIR ${KWWidgets_INSTALL_LIB_DIR}
    CACHE INTERNAL "")
endif(NOT KWWidgets_INSTALL_PACKAGE_DIR)

if(NOT KWWidgets_VTK_INSTALL_PACKAGE_DIR)
  set(KWWidgets_VTK_INSTALL_PACKAGE_DIR ${VTK_INSTALL_PACKAGE_DIR})
endif(NOT KWWidgets_VTK_INSTALL_PACKAGE_DIR)

if(NOT KWWidgets_INSTALL_NO_DEVELOPMENT)
  set(KWWidgets_INSTALL_NO_DEVELOPMENT 0)
endif(NOT KWWidgets_INSTALL_NO_DEVELOPMENT)

if(NOT KWWidgets_INSTALL_NO_RUNTIME)
  set(KWWidgets_INSTALL_NO_RUNTIME 0)
endif(NOT KWWidgets_INSTALL_NO_RUNTIME)

if(NOT KWWidgets_INSTALL_NO_DOCUMENTATION)
  set(KWWidgets_INSTALL_NO_DOCUMENTATION 0)
endif(NOT KWWidgets_INSTALL_NO_DOCUMENTATION)

set(KWWidgets_INSTALL_NO_LIBRARIES)
if(KWWidgets_BUILD_SHARED_LIBS)
  if(KWWidgets_INSTALL_NO_RUNTIME AND KWWidgets_INSTALL_NO_DEVELOPMENT)
    set(KWWidgets_INSTALL_NO_LIBRARIES 1)
  endif(KWWidgets_INSTALL_NO_RUNTIME AND KWWidgets_INSTALL_NO_DEVELOPMENT)
else(KWWidgets_BUILD_SHARED_LIBS)
  if(KWWidgets_INSTALL_NO_DEVELOPMENT)
    set(KWWidgets_INSTALL_NO_LIBRARIES 1)
  endif(KWWidgets_INSTALL_NO_DEVELOPMENT)
endif(KWWidgets_BUILD_SHARED_LIBS)

# --------------------------------------------------------------------------
# Sources

option(KWWidgets_BUILD_VTK_WIDGETS "Build the widgets that can be interfaced to complex VTK classes. Set it to OFF to build a core version of the library." ON)
mark_as_advanced(KWWidgets_BUILD_VTK_WIDGETS)

set(KWWidgets_SRCS
  vtkKWApplication.cxx
  vtkKWApplicationSettingsInterface.cxx
  vtkKWBalloonHelpManager.cxx
  vtkKWCanvas.cxx
  vtkKWChangeColorButton.cxx
  vtkKWCheckButton.cxx
  vtkKWCheckButtonWithChangeColorButton.cxx
  vtkKWCheckButtonWithPopupFrame.cxx
  vtkKWClipboardHelper.cxx
  vtkKWColorPickerDialog.cxx
  vtkKWColorPickerWidget.cxx
  vtkKWColorPresetSelector.cxx
  vtkKWColorSpectrumWidget.cxx
  vtkKWColorSwatchesWidget.cxx
  vtkKWComboBox.cxx
  vtkKWCompositeWidget.cxx
  vtkKWCoreWidget.cxx
  vtkKWDialog.cxx
  vtkKWDirectoryExplorer.cxx 
  vtkKWDirectoryPresetSelector.cxx
  vtkKWDragAndDropTargetSet.cxx
  vtkKWEntry.cxx
  vtkKWEvent.cxx
  vtkKWEventMap.cxx
  vtkKWExtent.cxx
  vtkKWFavoriteDirectoriesFrame.cxx 
  vtkKWFileBrowserDialog.cxx
  vtkKWFileBrowserWidget.cxx 
  vtkKWFileListTable.cxx 
  vtkKWFrame.cxx
  vtkKWFrameWithLabel.cxx
  vtkKWFrameWithScrollbar.cxx
  vtkKWHSVColorSelector.cxx
  vtkKWIcon.cxx
  vtkKWInternationalization.cxx
  vtkKWKeyBindingsManager.cxx
  vtkKWKeyBindingsWidget.cxx
  vtkKWLabel.cxx
  vtkKWLanguage.cxx
  vtkKWListBox.cxx
  vtkKWListBoxToListBoxSelectionEditor.cxx
  vtkKWLoadSaveButton.cxx
  vtkKWLoadSaveDialog.cxx
  vtkKWLogDialog.cxx
  vtkKWLogWidget.cxx
  vtkKWMaterialPropertyWidget.cxx
  vtkKWMatrixWidget.cxx
  vtkKWMenu.cxx
  vtkKWMenuButton.cxx
  vtkKWMessage.cxx
  vtkKWMessageDialog.cxx
  vtkKWMostRecentFilesManager.cxx
  vtkKWMultiColumnList.cxx
  vtkKWNotebook.cxx
  vtkKWObject.cxx
  vtkKWOptionDataBase.cxx
  vtkKWOptions.cxx
  vtkKWPopupButton.cxx
  vtkKWPopupFrame.cxx
  vtkKWPresetSelector.cxx
  vtkKWProgressGauge.cxx
  vtkKWPushButton.cxx
  vtkKWPushButtonWithMenu.cxx
  vtkKWRadioButton.cxx
  vtkKWRadioButtonSet.cxx
  vtkKWRange.cxx
  vtkKWRegistryHelper.cxx
  vtkKWResourceUtilities.cxx
  vtkKWSaveImageDialog.cxx
  vtkKWScalarComponentSelectionWidget.cxx
  vtkKWScale.cxx
  vtkKWScaleWithEntry.cxx
  vtkKWScrollbar.cxx
  vtkKWSeparator.cxx
  vtkKWSimpleEntryDialog.cxx
  vtkKWSmallCounterLabel.cxx
  vtkKWSpinBox.cxx
  vtkKWSpinButtons.cxx
  vtkKWSplashScreen.cxx
  vtkKWSplitFrame.cxx
  vtkKWStartupPageWidget.cxx
  vtkKWStateMachine.cxx
  vtkKWStateMachineCluster.cxx
  vtkKWStateMachineDOTWriter.cxx
  vtkKWStateMachineInput.cxx
  vtkKWStateMachineState.cxx
  vtkKWStateMachineTransition.cxx
  vtkKWStateMachineWriter.cxx
  vtkKWTclInteractor.cxx
  vtkKWText.cxx
  vtkKWTheme.cxx
  vtkKWThumbWheel.cxx
  vtkKWTkUtilities.cxx
  vtkKWTkcon.cxx
  vtkKWToolbar.cxx
  vtkKWToolbarSet.cxx
  vtkKWTopLevel.cxx
  vtkKWTree.cxx
  vtkKWTreeWithScrollbars.cxx
  vtkKWUserInterfaceManager.cxx
  vtkKWUserInterfaceManagerDialog.cxx
  vtkKWUserInterfaceManagerNotebook.cxx
  vtkKWUserInterfacePanel.cxx
  vtkKWWidget.cxx
  vtkKWWidgetSet.cxx
  vtkKWWidgetWithLabel.cxx
  vtkKWWidgetWithScrollbars.cxx
  vtkKWWidgetWithSpinButtons.cxx
  vtkKWWidgetsVersion.cxx
  vtkKWWindow.cxx
  vtkKWWindowBase.cxx
  vtkKWWindowLevelPresetSelector.cxx
  vtkKWWizardDialog.cxx
  vtkKWWizardStep.cxx
  vtkKWWizardWidget.cxx
  vtkKWWizardWorkflow.cxx
  )

if(KWWidgets_BUILD_VTK_WIDGETS)
  if(NOT VTK_USE_TK)
    message(FATAL_ERROR "Please set VTK_USE_TK to ON in VTK for KWWidgets_BUILD_VTK_WIDGETS to be ON as well.")
  endif(NOT VTK_USE_TK)
  set(KWWidgets_SRCS ${KWWidgets_SRCS}
    vtkKWColorPresetMenu.cxx
    vtkKWColorTransferFunctionEditor.cxx
    vtkKWCornerAnnotationEditor.cxx
    vtkKWGenericRenderWindowInteractor.cxx
    vtkKWHeaderAnnotationEditor.cxx
    vtkKWHistogram.cxx
    vtkKWHistogramSet.cxx
    vtkKWParameterValueFunctionEditor.cxx
    vtkKWParameterValueFunctionInterface.cxx
    vtkKWParameterValueHermiteFunctionEditor.cxx
    vtkKWPiecewiseFunctionEditor.cxx
    vtkKWRenderWidget.cxx
    vtkKWScalarBarAnnotation.cxx
    vtkKWSelectionFrame.cxx
    vtkKWSelectionFrameLayoutManager.cxx
    vtkKWSimpleAnimationWidget.cxx
    vtkKWSurfaceMaterialPropertyWidget.cxx
    vtkKWTextPropertyEditor.cxx
    vtkKWVolumePropertyHelper.cxx
    vtkKWVolumeMaterialPropertyWidget.cxx
    vtkKWVolumePropertyPresetSelector.cxx
    vtkKWVolumePropertyWidget.cxx
    )
endif(KWWidgets_BUILD_VTK_WIDGETS)

# Utilities/Extra sources

set(KWWidgets_UTILS_SRCS
  ${CMAKE_CURRENT_SOURCE_DIR}/Wrapping/Tcl/vtkKWWidgetsInitializeCommand.cxx
  ${CMAKE_CURRENT_SOURCE_DIR}/Utilities/BWidgets/vtkKWBWidgetsInit.cxx
  ${CMAKE_CURRENT_SOURCE_DIR}/Utilities/Tablelist/vtkKWTablelistInit.cxx
  ${CMAKE_CURRENT_SOURCE_DIR}/Utilities/tkcon/vtkKWTkconInit.cxx
  )
if(TKDND_SRCS)
  set(KWWidgets_UTILS_SRCS ${KWWidgets_UTILS_SRCS} ${TKDND_SRCS})
endif(TKDND_SRCS)
if(TKTREECTRL_SRCS)
  set(KWWidgets_UTILS_SRCS ${KWWidgets_UTILS_SRCS} ${TKTREECTRL_SRCS})
endif(TKTREECTRL_SRCS)

# Abstract/pure virtual classes

set_source_files_properties(
  vtkKWClipboardHelper.cxx
  vtkKWMaterialPropertyWidget.cxx
  vtkKWParameterValueFunctionEditor.cxx
  vtkKWParameterValueFunctionInterface.cxx
  vtkKWParameterValueHermiteFunctionEditor.cxx
  vtkKWRegistryHelper.cxx
  vtkKWStateMachineWriter.cxx
  vtkKWUserInterfaceManager.cxx
  vtkKWWidgetSet.cxx
  vtkKWWidgetWithScrollbars.cxx
  ${KWWidgets_UTILS_SRCS}
  ABSTRACT
  )

# Helper classes

set_source_files_properties(
  vtkKWEvent.cxx
  ${KWWidgets_UTILS_SRCS}
  WRAP_EXCLUDE
  )

if(NOT VTK_WRAP_TCL)
  set_source_files_properties(
    vtkKWGenericRenderWindowInteractor.cxx
    WRAP_EXCLUDE
    )
endif(NOT VTK_WRAP_TCL)

# Add the registry code, and the set_icon command on win32

if(WIN32)
  set(KWWidgets_UTILS_SRCS ${KWWidgets_UTILS_SRCS}
    ${CMAKE_CURRENT_SOURCE_DIR}/Utilities/ApplicationIcon/vtkKWSetApplicationIconTclCommand.c)
  set(KWWidgets_SRCS ${KWWidgets_SRCS} 
    vtkKWWin32RegistryHelper.cxx)
  set_source_files_properties(
    ${CMAKE_CURRENT_SOURCE_DIR}/Utilities/ApplicationIcon/vtkKWSetApplicationIconTclCommand.c
    WRAP_EXCLUDE)
else(WIN32)
  set(KWWidgets_SRCS ${KWWidgets_SRCS}
    vtkKWUNIXRegistryHelper.cxx)
endif(WIN32)

# --------------------------------------------------------------------------
# Sources generated automatically from templates

set(KWWidgets_TEMPLATES_DIR ${KWWidgets_SOURCE_DIR}/Templates)
set(KWWidgets_AUTOGENERATED_DIR ${KWWidgets_BINARY_DIR}/AutoGeneratedClasses)
set(KWWidgets_AUTOGENERATED_SRCS)
set(KWWidgets_TEMPLATE_SRCS)

subdirs(Templates)

macro(KWWidgets_AUTOGENERATE_CLASS template build_dir class suffix src_list)
  set(WidgetType ${class})
  configure_file(
    ${template}.h.in
    ${build_dir}/${class}${suffix}.h
    IMMEDIATE)
  configure_file(
    ${template}.cxx.in
    ${build_dir}/${class}${suffix}.cxx
    IMMEDIATE)
  set(${src_list} ${${src_list}} ${build_dir}/${class}${suffix}.cxx)
endmacro(KWWidgets_AUTOGENERATE_CLASS)

# The "set of widgets" are automatically generated from some core widgets
# (i.e., vtkKWCheckButton => vtkKWCheckButtonSet).

set(list 
  vtkKWCheckButton
  vtkKWComboBox
  vtkKWEntry
  vtkKWFrame
  vtkKWLabel
  vtkKWLabelWithLabel
  vtkKWPushButton
  vtkKWScale
  vtkKWScaleWithEntry
  vtkKWScaleWithLabel
  vtkKWSpinBox
  )
if(KWWidgets_BUILD_VTK_WIDGETS)
  set(list ${list}
    vtkKWColorTransferFunctionEditor
    vtkKWPiecewiseFunctionEditor
    )
endif(KWWidgets_BUILD_VTK_WIDGETS)
foreach(WidgetType ${list})
  kwwidgets_autogenerate_class(
    ${KWWidgets_TEMPLATES_DIR}/vtkKWWidgetSetSubclass
    ${KWWidgets_AUTOGENERATED_DIR}
    ${WidgetType}
    Set
    KWWidgets_AUTOGENERATED_SRCS)
endforeach(WidgetType)
set(KWWidgets_TEMPLATE_SRCS ${KWWidgets_TEMPLATE_SRCS} 
  ${KWWidgets_TEMPLATES_DIR}/vtkKWWidgetSetSubclass.cxx.in)

# The "labeled widgets" are automatically generated from some core widgets
# (i.e., vtkKWCheckButton => vtkKWCheckButtonWithLabel)

set(list 
  vtkKWCheckButton
  vtkKWCheckButtonSet
  vtkKWComboBox
  vtkKWEntry
  vtkKWLabel
  vtkKWLoadSaveButton
  vtkKWListBoxWithScrollbars
  vtkKWMatrixWidget
  vtkKWMenuButton
  vtkKWMenuButtonWithSpinButtons
  vtkKWMessage
  vtkKWMultiColumnList
  vtkKWMultiColumnListWithScrollbars
  vtkKWPopupButton
  vtkKWPushButton
  vtkKWPushButtonSet
  vtkKWRadioButtonSet
  vtkKWScale
  vtkKWScaleWithEntrySet
  vtkKWSpinBox
  vtkKWText
  vtkKWTextWithScrollbars
  )
foreach(WidgetType ${list})
  kwwidgets_autogenerate_class(
    ${KWWidgets_TEMPLATES_DIR}/vtkKWWidgetWithLabelSubclass
    ${KWWidgets_AUTOGENERATED_DIR}
    ${WidgetType}
    WithLabel
    KWWidgets_AUTOGENERATED_SRCS)
endforeach(WidgetType)
set(KWWidgets_TEMPLATE_SRCS ${KWWidgets_TEMPLATE_SRCS} 
  ${KWWidgets_TEMPLATES_DIR}/vtkKWWidgetWithLabelSubclass.cxx.in)

# The "widgets with scrollbars" are automatically generated from some core
# widgets
# (i.e., vtkKWText => vtkKWTextWithScrollbars)

set(list 
  vtkKWCanvas
  vtkKWListBox
  vtkKWMultiColumnList
  vtkKWText
  )
foreach(WidgetType ${list})
  kwwidgets_autogenerate_class(
    ${KWWidgets_TEMPLATES_DIR}/vtkKWWidgetWithScrollbarsSubclass
    ${KWWidgets_AUTOGENERATED_DIR}
    ${WidgetType}
    WithScrollbars
    KWWidgets_AUTOGENERATED_SRCS)
endforeach(WidgetType)
set(KWWidgets_TEMPLATE_SRCS ${KWWidgets_TEMPLATE_SRCS} 
  ${KWWidgets_TEMPLATES_DIR}/vtkKWWidgetWithScrollbarsSubclass.cxx.in)

# The "widgets with spinbuttons" are automatically generated from some
# core widgets
# (i.e., vtkKWMenuButton => vtkKWMenuButtonWithSpinButtons)

set(list 
  vtkKWMenuButton
  )
foreach(WidgetType ${list})
  kwwidgets_autogenerate_class(
    ${KWWidgets_TEMPLATES_DIR}/vtkKWWidgetWithSpinButtonsSubclass
    ${KWWidgets_AUTOGENERATED_DIR}
    ${WidgetType}
    WithSpinButtons
    KWWidgets_AUTOGENERATED_SRCS)
endforeach(WidgetType)
set(KWWidgets_TEMPLATE_SRCS ${KWWidgets_TEMPLATE_SRCS} 
  ${KWWidgets_TEMPLATES_DIR}/vtkKWWidgetWithSpinButtonsSubclass.cxx.in)

# --------------------------------------------------------------------------
# SOViewer support

option(KWWidgets_USE_SOVIEWER "Use the SOViewer library (SOV_DIR) and build the corresponding support classes and examples." OFF)
mark_as_advanced(KWWidgets_USE_SOVIEWER)
if(KWWidgets_USE_SOVIEWER)
  find_package(SOV REQUIRED)
  if(SOV_FOUND)
    mark_as_advanced(FLTK_FLUID_EXECUTABLE SOV_DIR)
  endif(SOV_FOUND)
endif(KWWidgets_USE_SOVIEWER)

# --------------------------------------------------------------------------
# HTML Help can help
# http://msdn.microsoft.com/
# http://msdn.microsoft.com/library/default.asp?url=/library/en-us/htmlhelp/html/vsconwhtshw.asp

if(WIN32)
  if(HTML_HELP_INCLUDE_PATH AND HTML_HELP_LIBRARY)
  else(HTML_HELP_INCLUDE_PATH AND HTML_HELP_LIBRARY)
    include(FindHTMLHelp)
  endif(HTML_HELP_INCLUDE_PATH AND HTML_HELP_LIBRARY)
  if(HTML_HELP_INCLUDE_PATH AND HTML_HELP_LIBRARY)
    set(KWWidgets_USE_HTML_HELP 1)
  else(HTML_HELP_INCLUDE_PATH AND HTML_HELP_LIBRARY)
    set(KWWidgets_USE_HTML_HELP 0)
  endif(HTML_HELP_INCLUDE_PATH AND HTML_HELP_LIBRARY)
  if(KWWidgets_USE_HTML_HELP)
    set(KWWidgets_INCLUDE_PATH ${KWWidgets_INCLUDE_PATH} 
      ${HTML_HELP_INCLUDE_PATH} CACHE INTERNAL "" FORCE)
  endif(KWWidgets_USE_HTML_HELP)
endif(WIN32)

# --------------------------------------------------------------------------
# Internationalization

set(default_val 0)
if(DEFINED KWWidgets_USE_INTERNATIONALIZATION_DEFAULT)
  set(default_val ${KWWidgets_USE_INTERNATIONALIZATION_DEFAULT})
endif(DEFINED KWWidgets_USE_INTERNATIONALIZATION_DEFAULT)
option(KWWidgets_USE_INTERNATIONALIZATION 
  "Enable internationalization support in KWWidgets." ${default_val})
mark_as_advanced(KWWidgets_USE_INTERNATIONALIZATION)
if(KWWidgets_USE_INTERNATIONALIZATION)
  find_package(Gettext)
  if(GETTEXT_FOUND)
    include("${KWWidgets_CMAKE_DIR}/KWWidgetsInternationalizationMacros.cmake")
    set(KWWidgets_INCLUDE_PATH ${KWWidgets_INCLUDE_PATH}
      ${GETTEXT_INCLUDE_DIR} CACHE INTERNAL "" FORCE)
    kwwidgets_create_gettext_targets(
      DOMAIN_NAME "KWWidgets"
      LOCALE_LIST "fr"
      COPYRIGHT_HOLDER "Kitware, Inc."
      SOURCES "${KWWidgets_SRCS};${KWWidgets_TEMPLATE_SRCS}"
      PO_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Resources/po"
      POT_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/Resources/po"
      PO_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/Resources/po"
      MO_INSTALL_DIR "${KWWidgets_INSTALL_DATA_DIR}/locale"
      ADD_MO_TARGET_TO_ALL)
  else(GETTEXT_FOUND)
    set(msg "KWWidgets_USE_INTERNATIONALIZATION is ON but the gettext runtime library and tools were not found.\n\nMore information about internationalization support in KWWidgets can be found at http://public.kitware.com/Wiki/KWWidgets/i18n.\n\n${GETTEXT_INFO_MSG}")
    message(FATAL_ERROR "${msg}")
  endif(GETTEXT_FOUND)
endif(KWWidgets_USE_INTERNATIONALIZATION)

# --------------------------------------------------------------------------
# Use [incr Tcl]

set(default_val 0)
if(DEFINED KWWidgets_USE_INCR_TCL_DEFAULT)
  set(default_val ${KWWidgets_USE_INCR_TCL_DEFAULT})
endif(DEFINED KWWidgets_USE_INCR_TCL_DEFAULT)
option(KWWidgets_USE_INCR_TCL 
  "Enable [incr Tcl] support in KWWidgets." ${default_val})
mark_as_advanced(KWWidgets_USE_INCR_TCL)
if(KWWidgets_USE_INCR_TCL)
  find_package(IncrTCL REQUIRED)
endif(KWWidgets_USE_INCR_TCL)

# --------------------------------------------------------------------------
# Include dirs

include_directories(
  ${KWWidgets_INCLUDE_PATH}
  ${VTK_INCLUDE_DIR}
  ${TKDND_INCLUDE_PATH}
  )

# A tricky one. Tcl/Tk 8.5.0 provides (for the first time) some of the
# "internal headers" we usually provide in VTK (tclPort.h, tclWinPort.h, etc).
# Alas, tclPort.h is buggy by default, as it tries to explicitly include
# "../win/tclWinPort.h" instead of "tclWinPort.h". Our VTK internal headers
# were fixed. The trick is therefore to include ours before ActiveState's,
# by using the BEFORE keyword below.

include_directories(BEFORE ${TKTREECTRL_INCLUDE_PATH})

# --------------------------------------------------------------------------
# Wrapping

subdirs(Wrapping)

set(KWWidgets_SRCS ${KWWidgets_SRCS} ${KWWidgets_AUTOGENERATED_SRCS})

# --------------------------------------------------------------------------
# Wrap the classes in Tcl

include("${KWWidgets_CMAKE_DIR}/KWWidgetsWrappingMacros.cmake")
set(VTK_WRAP_HINTS ${KWWidgets_SOURCE_DIR}/hints)
kwwidgets_wrap_tcl(
  KWWidgets
  KWWidgetsTCL_SRCS
  "${KWWidgets_SRCS}"
  "vtkKWWidgetsInitializeCommand"
  "${KWWidgets_MAJOR_VERSION}.${KWWidgets_MINOR_VERSION}")

set(KWWidgets_TCL_PACKAGE_INDEX_DIR
  ${KWWidgets_BINARY_DIR}/Wrapping/Tcl)

# If needed, copy the Tcl/Tk support files required at run-time
# to initialize Tcl/Tk. This is used by the examples or larger
# projects that include KWWidgets directly in their source trees.

if(TclTk_INSTALL_LIB_DIR)
  set(KWWidgets_TclTk_INSTALL_LIB_DIR "${TclTk_INSTALL_LIB_DIR}")
else(TclTk_INSTALL_LIB_DIR)
  set(KWWidgets_TclTk_INSTALL_LIB_DIR "${KWWidgets_INSTALL_LIB_DIR}/TclTk/lib")
endif(TclTk_INSTALL_LIB_DIR)

include("${KWWidgets_CMAKE_DIR}/KWWidgetsTclTkMacros.cmake")
kwwidgets_copy_tcl_tk_support_files("${PROJECT_BINARY_DIR}/lib")
kwwidgets_install_tcl_tk_support_files("${KWWidgets_TclTk_INSTALL_LIB_DIR}")

incr_tcl_get_version("INCR_TCL_MAJOR_VERSION" "INCR_TCL_MINOR_VERSION")

# --------------------------------------------------------------------------
# Wrap the classes in Python

if(VTK_WRAP_PYTHON)

  set(KWWidgetsPythonExtra_SRCS)
  set(KWWidgetsPython_LIBS vtkCommonPythonD)

  include("${VTK_CMAKE_DIR}/vtkWrapPython.cmake")

  vtk_wrap_python3(KWWidgetsPython KWWidgetsPython_SRCS
    "${KWWidgets_SRCS} ${KWWidgetsPythonExtra_SRCS}")

  set(KWWidgets_PYTHON_MODULE_DIR
    ${KWWidgets_BINARY_DIR}/Wrapping/Python)

  if(BORLAND)
    write_file(${LIBRARY_OUTPUT_PATH}/KWWidgetsPython.def
      "EXPORTS\ninitKWWidgetsPython=_initKWWidgetsPython\n")
  endif(BORLAND)

  add_library(KWWidgetsPythonD 
    ${KWWidgetsPython_SRCS} 
    ${KWWidgetsPythonExtra_SRCS}
    )
  target_link_libraries(KWWidgetsPythonD KWWidgets ${KWWidgetsPython_LIBS})
  if(KWWidgets_BUILD_VTK_WIDGETS AND 
      VTK_USE_RENDERING OR VTK_USE_RENDERING_ISSET)
    target_link_libraries(KWWidgetsPythonD vtkRenderingPythonD)
  endif(KWWidgets_BUILD_VTK_WIDGETS AND 
    VTK_USE_RENDERING OR VTK_USE_RENDERING_ISSET)

  if(WIN32 OR APPLE)
    target_link_libraries(KWWidgetsPythonD ${VTK_PYTHON_LIBRARIES})
  endif(WIN32 OR APPLE)

  if(NOT KWWidgets_INSTALL_NO_LIBRARIES)
    install_targets(
      ${KWWidgets_INSTALL_LIB_DIR}
      RUNTIME_DIRECTORY ${KWWidgets_INSTALL_BIN_DIR}
      KWWidgetsPythonD)
  endif(NOT KWWidgets_INSTALL_NO_LIBRARIES)

  # Create a python module that can be loaded dynamically.

  add_library(KWWidgetsPython MODULE
    Wrapping/Python/KWWidgetsPythonExtraInit.cxx)
  target_link_libraries(KWWidgetsPython KWWidgetsPythonD)

  # Python extension modules on Windows must have the extension ".pyd"
  # instead of ".dll" as of Python 2.5.  Older python versions do support
  # this suffix.
  if(WIN32 AND NOT CYGWIN)
    set_target_properties(KWWidgetsPython PROPERTIES SUFFIX ".pyd")
  endif(WIN32 AND NOT CYGWIN)

  if(WIN32 OR APPLE)
    target_link_libraries(KWWidgetsPython ${VTK_PYTHON_LIBRARIES})
  endif(WIN32 OR APPLE)

  if(KWWidgets_BUILD_VTK_WIDGETS AND 
      VTK_USE_RENDERING OR VTK_USE_RENDERING_ISSET)
    # TARGET_LINK_LIBRARIES(KWWidgetsPython vtkRenderingPython)
  endif(KWWidgets_BUILD_VTK_WIDGETS AND
    VTK_USE_RENDERING OR VTK_USE_RENDERING_ISSET)

  find_program(PYTHON_EXECUTABLE
    NAMES python2.6 python2.5 python2.4 python2.3 python2.2 python
    PATHS
    [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\2.6\\InstallPath]
    [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\2.5\\InstallPath]
    [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\2.4\\InstallPath]
    [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\2.3\\InstallPath]
    [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\2.2\\InstallPath]
    )
  mark_as_advanced(PYTHON_EXECUTABLE)

endif(VTK_WRAP_PYTHON)

# --------------------------------------------------------------------------
# Create the instantiators

set(VTK_USE_INSTANTIATOR_NEW 1)
vtk_make_instantiator3(KWWidgetsInstantiator 
  KWWidgets_Instantiator_SRCS
  "${KWWidgets_SRCS}"
  KWWidgets_EXPORT
  ${KWWidgets_BINARY_DIR} 
  "vtkKWWidgets.h")

# --------------------------------------------------------------------------
# Build the library

add_library(KWWidgets
  ${KWWidgetsTCL_SRCS}
  ${KWWidgets_SRCS}
  ${KWWidgets_Instantiator_SRCS}
  ${KWWidgets_UTILS_SRCS})

target_link_libraries(KWWidgets vtkCommonTCL)
if(KWWidgets_BUILD_VTK_WIDGETS)
  if(VTK_WRAP_TCL)
    if(VTK_USE_RENDERING OR VTK_USE_RENDERING_ISSET)
      target_link_libraries(KWWidgets vtkRenderingTCL)
      target_link_libraries(KWWidgets vtkHybridTCL)
      if(VTK_MAJOR_VERSION GREATER 4)
        target_link_libraries(KWWidgets vtkWidgetsTCL)
        target_link_libraries(KWWidgets vtkVolumeRenderingTCL)
      endif(VTK_MAJOR_VERSION GREATER 4)
    endif(VTK_USE_RENDERING OR VTK_USE_RENDERING_ISSET)
    if(VTK_USE_PARALLEL OR VTK_USE_PARALLEL_ISSET)
      target_link_libraries(KWWidgets vtkParallelTCL)
    endif(VTK_USE_PARALLEL OR VTK_USE_PARALLEL_ISSET)
  else(VTK_WRAP_TCL)
    if(VTK_USE_RENDERING OR VTK_USE_RENDERING_ISSET)
      target_link_libraries(KWWidgets vtkRendering)
      target_link_libraries(KWWidgets vtkHybrid)
      if(VTK_MAJOR_VERSION GREATER 4)
        target_link_libraries(KWWidgets vtkWidgets)
        target_link_libraries(KWWidgets vtkVolumeRendering)
      endif(VTK_MAJOR_VERSION GREATER 4)
    endif(VTK_USE_RENDERING OR VTK_USE_RENDERING_ISSET)
    if(VTK_USE_PARALLEL OR VTK_USE_PARALLEL_ISSET)
      target_link_libraries(KWWidgets vtkParallel)
    endif(VTK_USE_PARALLEL OR VTK_USE_PARALLEL_ISSET)
  endif(VTK_WRAP_TCL)
else(KWWidgets_BUILD_VTK_WIDGETS)
  if(TK_LIBRARY_DEBUG)
    set(KWWidgets_TK_LIBRARIES 
      optimized ${TK_LIBRARY} debug ${TK_LIBRARY_DEBUG})
  else(TK_LIBRARY_DEBUG)
    set(KWWidgets_TK_LIBRARIES ${TK_LIBRARY})
  endif(TK_LIBRARY_DEBUG)
  target_link_libraries(KWWidgets ${KWWidgets_TK_LIBRARIES})
endif(KWWidgets_BUILD_VTK_WIDGETS)

target_link_libraries(KWWidgets
  ${VTK_PNG_LIBRARIES}
  ${KWSYS_NAMESPACE}
  )

if(WIN32)
  target_link_libraries(KWWidgets comctl32 mpr)
  if(HTML_HELP_INCLUDE_PATH AND HTML_HELP_LIBRARY)
    target_link_libraries(KWWidgets ${HTML_HELP_LIBRARY} )
  endif(HTML_HELP_INCLUDE_PATH AND HTML_HELP_LIBRARY)
endif(WIN32)

# link ApplicationServices.framework on OSX for vtkKWClipboardHelper 
if(APPLE)
  target_link_libraries (KWWidgets "-framework ApplicationServices")
  # As much as we should try to avoid linking against Carbon, I'm
  # afraid we will need it to run an AppleScript using OSAExecute()
  # (see vtkKWApplication::RunAppleScript and SendEmail)
  target_link_libraries (KWWidgets "-framework Carbon")
endif(APPLE)

if(KWWidgets_USE_INTERNATIONALIZATION AND GETTEXT_RUNTIME_FOUND)
  target_link_libraries(KWWidgets ${GETTEXT_LIBRARIES})
endif(KWWidgets_USE_INTERNATIONALIZATION AND GETTEXT_RUNTIME_FOUND)

if(KWWidgets_USE_INCR_TCL AND INCR_TCL_FOUND)
  target_link_libraries(KWWidgets ${INCR_TCL_LIBRARY} ${INCR_TK_LIBRARY})
endif(KWWidgets_USE_INCR_TCL AND INCR_TCL_FOUND)

if(TKDND_LIBS)
  target_link_libraries(KWWidgets ${TKDND_LIBS})
endif(TKDND_LIBS)

if(TKTREECTRL_LIBS)
  target_link_libraries(KWWidgets ${TKTREECTRL_LIBS})
endif(TKTREECTRL_LIBS)

if(NOT KWWidgets_INSTALL_NO_LIBRARIES)
  install_targets(
    ${KWWidgets_INSTALL_LIB_DIR}
    RUNTIME_DIRECTORY ${KWWidgets_INSTALL_BIN_DIR}
    ${KWWidgets_LIBRARIES})
endif(NOT KWWidgets_INSTALL_NO_LIBRARIES)

# --------------------------------------------------------------------------
# Examples

set(KWWidgets_EXAMPLES_DIR ${KWWidgets_SOURCE_DIR}/Examples)

option(KWWidgets_BUILD_EXAMPLES "Build KWWidgets examples." ${BUILD_EXAMPLES})
mark_as_advanced(KWWidgets_BUILD_EXAMPLES)

set(KWWidgets_NEED_TO_BUILD_EXAMPLES ${KWWidgets_BUILD_EXAMPLES})

# --------------------------------------------------------------------------
# Testing (requires some of the examples)

include(Dart)
mark_as_advanced(BUILD_TESTING DART_ROOT TCL_TCLSH)

set(default_val 1)
if(DEFINED KWWidgets_BUILD_TESTING_DEFAULT)
  set(default_val ${KWWidgets_BUILD_TESTING_DEFAULT})
endif(DEFINED KWWidgets_BUILD_TESTING_DEFAULT)
option(KWWidgets_BUILD_TESTING 
  "Enable testing in KWWidgets (if BUILD_TESTING is ON as well)." 
  ${default_val})
mark_as_advanced(KWWidgets_BUILD_TESTING)
if(BUILD_TESTING AND KWWidgets_BUILD_TESTING)
  enable_testing()
  #SET(BUILDNAME "${PROJECT_NAME}-${BUILDNAME}")
  set(KWWidgets_NEED_TO_BUILD_EXAMPLES 1)
  option(KWWidgets_TEST_OUT_OF_SOURCE "Perform existing tests as out-of-source tests. This checks that external projects can be built against a KWWidgets build tree." OFF)
  mark_as_advanced(KWWidgets_TEST_OUT_OF_SOURCE)
  if(KWWidgets_TEST_OUT_OF_SOURCE)
    option(KWWidgets_TEST_INSTALLATION "Perform existing out-of-source tests against an installed KWWidgets library as well. This checks that external projects can be built against a KWWidgets installation tree. Make sure CMAKE_INSTALL_PREFIX is set and KWWidgets has been installed properly before running the tests." OFF)
    mark_as_advanced(KWWidgets_TEST_INSTALLATION)
  endif(KWWidgets_TEST_OUT_OF_SOURCE)
  subdirs(Testing)
endif(BUILD_TESTING AND KWWidgets_BUILD_TESTING)

# --------------------------------------------------------------------------
# Build the utilities

subdirs(Utilities)

# --------------------------------------------------------------------------
# Install the headers

if(NOT KWWidgets_INSTALL_NO_DEVELOPMENT)
  install_files(${KWWidgets_INSTALL_INCLUDE_DIR} .h ${KWWidgets_SRCS})
  install_files(${KWWidgets_INSTALL_INCLUDE_DIR} FILES vtkKWWidgets.h)
endif(NOT KWWidgets_INSTALL_NO_DEVELOPMENT)

# --------------------------------------------------------------------------
# Resources

set(KWWidgets_RESOURCES_DIR 
  ${KWWidgets_SOURCE_DIR}/Resources CACHE INTERNAL "")
subdirs(Resources)

# --------------------------------------------------------------------------
# Configure the export configuration

subdirs(CMake/ExportConfiguration)

# --------------------------------------------------------------------------
# Build the examples
# This need to be done *at the end*, so that the installation process
# goes as far as possible before trying to install the examples;
# examples would stop the install process if they failed to build (say, if a
# third-party lib like ITK or SOV was broken).

if(KWWidgets_NEED_TO_BUILD_EXAMPLES)
  subdirs(Examples)
endif(KWWidgets_NEED_TO_BUILD_EXAMPLES)

# --------------------------------------------------------------------------
# Testing with Squish

if(BUILD_TESTING AND KWWidgets_BUILD_TESTING)
  option(KWWidgets_USE_Squish "Test KWWidgets With Squish" OFF)
  mark_as_advanced(KWWidgets_USE_Squish)
  if(KWWidgets_USE_Squish)
    find_package(Squish)  
    if(NOT SQUISH_FOUND)                                                            
      MESSAGE("Warning: Squish not found, can't add Squish Tk GUI tests")           
    endif(NOT SQUISH_FOUND)
  endif(KWWidgets_USE_Squish)
endif(BUILD_TESTING AND KWWidgets_BUILD_TESTING)
