include/cinder/app/AppImplMsw.h
Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2012, The Cinder Project, All rights reserved.
00003 
00004  This code is intended for use with the Cinder C++ library: http://libcinder.org
00005 
00006  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
00007  the following conditions are met:
00008 
00009     * Redistributions of source code must retain the above copyright notice, this list of conditions and
00010     the following disclaimer.
00011     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
00012     the following disclaimer in the documentation and/or other materials provided with the distribution.
00013 
00014  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00015  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00016  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00017  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00018  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00019  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00020  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00021  POSSIBILITY OF SUCH DAMAGE.
00022 */
00023 
00024 #pragma once
00025 
00026 #include "cinder/Stream.h"
00027 #include "cinder/Vector.h"
00028 #include "cinder/app/MouseEvent.h"
00029 #include "cinder/app/KeyEvent.h"
00030 #include "cinder/app/TouchEvent.h"
00031 #include "cinder/app/Renderer.h"
00032 #include "cinder/Display.h"
00033 #include "cinder/app/Window.h"
00034 #include <string>
00035 #include <vector>
00036 #include <list>
00037 #include <windows.h>
00038 #undef min
00039 #undef max
00040 
00041 // we declare all of the MultiTouch stuff in Win7 here to prevent requiring users to use the Win7 headers
00042 #if ! defined( WM_TOUCH )
00043 DECLARE_HANDLE(HTOUCHINPUT);
00044 typedef struct tagTOUCHINPUT {
00045     LONG x;
00046     LONG y;
00047     HANDLE hSource;
00048     DWORD dwID;
00049     DWORD dwFlags;
00050     DWORD dwMask;
00051     DWORD dwTime;
00052     ULONG_PTR dwExtraInfo;
00053     DWORD cxContact;
00054     DWORD cyContact;
00055 } TOUCHINPUT, *PTOUCHINPUT;
00056 typedef TOUCHINPUT const * PCTOUCHINPUT;
00057 #define TOUCH_COORD_TO_PIXEL(l)         ((l) / 100)
00058 #define WM_TOUCH                        0x0240
00059 #endif
00060 
00061 namespace cinder { namespace app {
00062 
00063 class AppImplMsw {
00064  public:
00065     AppImplMsw( class App *aApp );
00066     virtual ~AppImplMsw();
00067     
00068     class App*      getApp() { return mApp; }
00069 
00070     float           getFrameRate() const { return mFrameRate; }
00071     virtual float   setFrameRate( float aFrameRate ) { return -1.0f; }
00072     virtual void    quit() = 0;
00073 
00074     virtual WindowRef   getWindow() const { return mActiveWindow; }
00075     void                setWindow( WindowRef window ) { mActiveWindow = window; }
00076     
00077     static void hideCursor();
00078     static void showCursor();
00079     
00080     static Buffer   loadResource( int id, const std::string &type );
00081     
00082     static fs::path     getAppPath();   
00083     static fs::path     getOpenFilePath( const fs::path &initialPath, std::vector<std::string> extensions );
00084     static fs::path     getSaveFilePath( const fs::path &initialPath, std::vector<std::string> extensions );
00085     static fs::path     getFolderPath( const fs::path &initialPath );
00086     
00087   protected:
00088     bool                    setupHasBeenCalled() const { return mSetupHasBeenCalled; }
00089     virtual void            closeWindow( class WindowImplMsw *windowImpl ) = 0;
00090     virtual void            setForegroundWindow( WindowRef window ) = 0;
00091 
00092     class App               *mApp;
00093     float                   mFrameRate;
00094     WindowRef               mActiveWindow;
00095     bool                    mSetupHasBeenCalled;
00096     ULONG_PTR               mGdiplusToken;
00097 
00098     friend class WindowImplMsw;
00099     friend LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
00100 };
00101 
00102 class WindowImplMsw {
00103   public:
00104     WindowImplMsw( const Window::Format &format, RendererRef sharedRenderer, AppImplMsw *appImpl );
00105     WindowImplMsw( HWND hwnd, RendererRef renderer, RendererRef sharedRenderer, AppImplMsw *appImpl );
00106     virtual ~WindowImplMsw() {}
00107 
00108     virtual bool        isFullScreen() { return mFullScreen; }
00109     virtual void        setFullScreen( bool fullScreen, const app::FullScreenOptions &options );
00110     virtual Vec2i       getSize() const { return Vec2i( mWindowWidth, mWindowHeight ); }
00111     virtual void        setSize( const Vec2i &size );
00112     virtual Vec2i       getPos() const { return mWindowOffset; }
00113     virtual void        setPos( const Vec2i &pos );
00114     virtual void        close();
00115     virtual std::string getTitle() const;
00116     virtual void        setTitle( const std::string &title );
00117     virtual void        hide();
00118     virtual void        show();
00119     virtual bool        isHidden() const;
00120     virtual DisplayRef  getDisplay() const { return mDisplay; }
00121     virtual RendererRef getRenderer() const { return mRenderer; }
00122     virtual const std::vector<TouchEvent::Touch>&   getActiveTouches() const { return mActiveTouches; }
00123     virtual void*       getNative() { return mWnd; }
00124 
00125     void            enableMultiTouch();
00126     bool            isBorderless() const { return mBorderless; }
00127     void            setBorderless( bool borderless );
00128     bool            isAlwaysOnTop() const { return mAlwaysOnTop; }
00129     void            setAlwaysOnTop( bool alwaysOnTop );
00130 
00131     AppImplMsw*             getAppImpl() { return mAppImpl; }
00132     WindowRef               getWindow() { return mWindowRef; }
00133     virtual void            keyDown( const KeyEvent &event );
00134     virtual void            draw();
00135     virtual void            redraw();
00136     virtual void            resize();
00137 
00138     void            privateClose();
00139   protected:
00140     void            createWindow( const Vec2i &windowSize, const std::string &title, DisplayRef display, RendererRef sharedRenderer );
00141     void            completeCreation();
00142     static void     registerWindowClass();
00143     void            getScreenSize( int clientWidth, int clientHeight, int *resultWidth, int *resultHeight );
00144     void            onTouch( HWND hWnd, WPARAM wParam, LPARAM lParam );
00145     virtual void    toggleFullScreen( const app::FullScreenOptions &options );
00146 
00147     AppImplMsw              *mAppImpl;
00148     WindowRef               mWindowRef;
00149     HWND                    mWnd;
00150     HDC                     mDC;
00151     DWORD                   mWindowStyle, mWindowExStyle;
00152     Vec2i                   mWindowOffset;
00153     bool                    mHidden;
00154     int                     mWindowWidth, mWindowHeight;
00155     bool                    mFullScreen, mBorderless, mAlwaysOnTop, mResizable;
00156     Vec2i                   mWindowedPos, mWindowedSize;
00157     DisplayRef              mDisplay;
00158     RendererRef             mRenderer;
00159     std::map<DWORD,Vec2f>           mMultiTouchPrev;
00160     std::vector<TouchEvent::Touch>  mActiveTouches;
00161     bool                    mIsDragging;
00162 
00163     friend AppImplMsw;
00164     friend LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
00165 };
00166 
00167 typedef std::shared_ptr<class BlankingWindow>       BlankingWindowRef;
00168 
00169 class BlankingWindow {
00170   public:
00171     static BlankingWindowRef    create( DisplayRef display ) { return BlankingWindowRef( new BlankingWindow( display ) ); }
00172     BlankingWindow( DisplayRef display );
00173 
00174     void    destroy();
00175 
00176   protected:
00177     static void     registerWindowClass();
00178     
00179     HWND            mWnd;
00180 };
00181 
00182 } } // namespace cinder::app