/* /////////////////////////////////////////////////////////////////////////////
 * File:        winstl_window_visible_scope.h
 *
 * Purpose:     Window enable-state scoping class.
 *
 * Created:     26th May 2004
 * Updated:     26th May 2004
 *
 * Author:      Matthew Wilson, Synesis Software Pty Ltd.
 *
 * License:     (Licensed under the Synesis Software Standard Source License)
 *
 *              Copyright (C) 2002-2004, Synesis Software Pty Ltd.
 *
 *              All rights reserved.
 *
 *              www:        http://www.synesis.com.au/winstl
 *                          http://www.winstl.org/
 *
 *              email:      submissions@winstl.org  for submissions
 *                          admin@winstl.org        for other enquiries
 *
 *              Redistribution and use in source and binary forms, with or
 *              without modification, are permitted provided that the following
 *              conditions are met:
 *
 *              (i) Redistributions of source code must retain the above
 *              copyright notice and contact information, this list of
 *              conditions and the following disclaimer.
 *
 *              (ii) Any derived versions of this software (howsoever modified)
 *              remain the sole property of Synesis Software.
 *
 *              (iii) Any derived versions of this software (howsoever modified)
 *              remain subject to all these conditions.
 *
 *              (iv) Neither the name of Synesis Software nor the names of any
 *              subdivisions, employees or agents of Synesis Software, nor the
 *              names of any other contributors to this software may be used to
 *              endorse or promote products derived from this software without
 *              specific prior written permission.
 *
 *              This source code is provided by Synesis Software "as is" and any
 *              warranties, whether expressed or implied, including, but not
 *              limited to, the implied warranties of merchantability and
 *              fitness for a particular purpose are disclaimed. In no event
 *              shall the Synesis Software be liable for any direct, indirect,
 *              incidental, special, exemplary, or consequential damages
 *              (including, but not limited to, procurement of substitute goods
 *              or services; loss of use, data, or profits; or business
 *              interruption) however caused and on any theory of liability,
 *              whether in contract, strict liability, or tort (including
 *              negligence or otherwise) arising in any way out of the use of
 *              this software, even if advised of the possibility of such
 *              damage.
 *
 * ////////////////////////////////////////////////////////////////////////// */


#ifndef WINSTL_INCL_H_WINSTL_WINDOW_VISIBLE_SCOPE
#define WINSTL_INCL_H_WINSTL_WINDOW_VISIBLE_SCOPE

#ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
# define _WINSTL_VER_H_WINSTL_WINDOW_VISIBLE_SCOPE_MAJOR     1
# define _WINSTL_VER_H_WINSTL_WINDOW_VISIBLE_SCOPE_MINOR     0
# define _WINSTL_VER_H_WINSTL_WINDOW_VISIBLE_SCOPE_REVISION  2
# define _WINSTL_VER_H_WINSTL_WINDOW_VISIBLE_SCOPE_EDIT      2
#endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* /////////////////////////////////////////////////////////////////////////////
 * Includes
 */

#ifndef WINSTL_INCL_H_WINSTL
# include "winstl.h"                // Include the WinSTL root header
#endif /* WINSTL_INCL_H_WINSTL */
#ifndef WINSTL_INCL_H_WINSTL_WINDOW_ACCESS
# include "winstl_window_access.h"  // winstl::window_access
#endif /* WINSTL_INCL_H_WINSTL_WINDOW_ACCESS */

/* /////////////////////////////////////////////////////////////////////////////
 * Namespace
 */

#ifndef _WINSTL_NO_NAMESPACE
# ifdef _STLSOFT_NO_NAMESPACE
/* There is no stlsoft namespace, so must define ::winstl */
namespace winstl
{
# else
/* Define stlsoft::winstl_project */

namespace stlsoft
{

namespace winstl_project
{

# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */

/* ////////////////////////////////////////////////////////////////////////// */

/// \weakgroup libraries STLSoft Libraries
/// \brief The individual libraries

/// \weakgroup libraries_window Window Library
/// \ingroup libraries
/// \brief This library provides facilities for defining and manipulating GUI windows

/// \defgroup winstl_window_library Window Library (WinSTL)
/// \ingroup WinSTL libraries_window
/// \brief This library provides facilities for defining and manipulating Win32 GUI windows
/// @{

/* /////////////////////////////////////////////////////////////////////////////
 * Classes
 */

// window_visible_scope
/// Provides scoping of the visible status of a window.
///
/// This class provides scoping of the visible status of a window via the API
/// function EnableWindow()..
class window_visible_scope
{
public:
    typedef window_visible_scope class_type;

// Construction
public:
    /// \brief Toggles the window visible state
    ///
    /// Takes a HWND and changes it's current visible-status, which is set back to
    /// the original state in the destructor.
    ///
    /// \param wnd The window whose visible state is to be controlled
    ss_explicit_k window_visible_scope(HWND wnd)
        : m_hwnd(wnd)
        , m_stateOnDtor(::IsWindowVisible(m_hwnd) ? SW_SHOW : SW_HIDE)
    {
        ::ShowWindow(m_hwnd, (SW_SHOW == m_stateOnDtor) ? SW_HIDE : SW_SHOW);
    }

#ifdef __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
    /// \brief Toggles the window visible state
    ///
    /// Takes a HWND and changes it's current visible-status, which is set back to
    /// the original state in the destructor.
    ///
    /// \param wnd The window whose visible state is to be controlled
    template <ss_typename_param_k W>
    ss_explicit_k window_visible_scope(W &wnd)
        : m_hwnd(get_hwnd(wnd))
        , m_stateOnDtor(::IsWindowVisible(m_hwnd) ? SW_SHOW : SW_HIDE)
    {
        ::ShowWindow(m_hwnd, (SW_SHOW == m_stateOnDtor) ? SW_HIDE : SW_SHOW);
    }
#endif /* __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */

    /// Modifies the window visible state
    ///
    /// \param wnd The window whose visible state is to be controlled
    /// \param stateOnCtor The state to set in the constructor
    /// \param stateOnDtor The state it is reset to in the destructor
    window_visible_scope(HWND wnd, ws_int_t stateOnCtor, ws_int_t stateOnDtor)
        : m_hwnd(wnd)
        , m_stateOnDtor(stateOnDtor)
    {
        ::ShowWindow(m_hwnd, stateOnCtor);
    }
#ifdef __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
    /// Modifies the window visible state
    ///
    /// \param wnd The window whose visible state is to be controlled
    /// \param stateOnCtor The state to set in the constructor
    /// \param stateOnDtor The state it is reset to in the destructor
    template <ss_typename_param_k W>
    window_visible_scope(W &wnd, ws_int_t stateOnCtor, ws_int_t stateOnDtor)
        : m_hwnd(get_hwnd(wnd))
        , m_stateOnDtor(stateOnDtor)
    {
        ::ShowWindow(m_hwnd, stateOnCtor);
    }
#endif /* __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */

    /// Resets the visible status
    ~window_visible_scope() winstl_throw_0()
    {
        winstl_static_assert(stlsoft_raw_offsetof(class_type, m_hwnd) < stlsoft_raw_offsetof(class_type, m_stateOnDtor));

        ::ShowWindow(m_hwnd, m_stateOnDtor);
    }

// Members
private:
    HWND const      m_hwnd;
    ws_int_t const  m_stateOnDtor;

// Not to be implemented
private:
    window_visible_scope(window_visible_scope const &rhs);
    window_visible_scope const &operator =(window_visible_scope const &rhs);
};

/* ////////////////////////////////////////////////////////////////////////// */

/// @} // end of group winstl_window_library

/* ////////////////////////////////////////////////////////////////////////// */

#ifndef _WINSTL_NO_NAMESPACE
# ifdef _STLSOFT_NO_NAMESPACE
} // namespace winstl
# else
} // namespace winstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */

/* ////////////////////////////////////////////////////////////////////////// */

#endif /* WINSTL_INCL_H_WINSTL_WINDOW_VISIBLE_SCOPE */

/* ////////////////////////////////////////////////////////////////////////// */
