/*! \file yof_window.hpp \brief Window API wrapper \author Kenta Yoshimura $Id$ */ /* Copyright (C) 2004-2005 Kenta Yoshimura. All Rights Reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY AUTHOR PROJECT ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS 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 YOF_WINDOW_HPP #define YOF_WINDOW_HPP #if !defined(_MSC_VER) | _MSC_VER > 1000 #pragma once #endif #include "yof_io.hpp" namespace yoffy { //! Very thin wrapper of WindowProc. class Window { public: typedef Window self; virtual ~Window() { ::SetWindowLongPtr( m_hWnd, GWL_WNDPROC, reinterpret_cast< LONG_PTR >( InitialWindowProc ) ); } HWND GetHandle( void ) { return m_hWnd; } operator HWND() { return m_hWnd; } /*! \brief Creating window. \return If scceed is true. Please create a window using RegisterWindow[Ex] and CreateWindow[Ex]. Please pass a pointer of Drived to lpParam of CreateWindow[Ex] in that case. */ virtual bool Create( const _TCHAR * title, int width, int height ) = 0; protected: HWND m_hWnd; /*! \brief Window message handler \return Succeed is TRUE. */ virtual LRESULT windowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { return ::DefWindowProc( hWnd, uMsg, wParam, lParam ); } /*! \brief 初期化用ウィンドウプロシージャ CreateWindow[Ex] の直後に WM_CREATE 以外のイベントが発生するため、 安易に GWLP_USERDATA を手に入れる事が出来ません。\n GWLP_USERDATA の入手前後でプロシージャを差し替える事でこれに対応します。 */ static LRESULT CALLBACK InitialWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { LRESULT result = TRUE; switch ( uMsg ) { case WM_CREATE: { Window * wnd = *reinterpret_cast< Window ** >( lParam ); ::SetWindowLongPtr( hWnd, GWLP_USERDATA, reinterpret_cast< LONG_PTR >( wnd ) ); ::SetWindowLongPtr( hWnd, GWL_WNDPROC, reinterpret_cast< LONG_PTR >( WindowProc ) ); wnd->m_hWnd = hWnd; result = wnd->windowProc( hWnd, uMsg, wParam, lParam ); } break; default: { result = ::DefWindowProc( hWnd, uMsg, wParam, lParam ); } } return result; } //! 通常時用ウィンドウプロシージャ static LRESULT CALLBACK WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { Window * wnd = reinterpret_cast< Window * >( ::GetWindowLongPtr( hWnd, GWLP_USERDATA ) ); return wnd->windowProc( hWnd, uMsg, wParam, lParam ); } }; } // namespace yoffy #endif // #ifndef YOF_WINDOW_HPP