AppImplMsw.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012, The Cinder Project, All rights reserved.
3 
4  This code is intended for use with the Cinder C++ library: http://libcinder.org
5 
6  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
7  the following conditions are met:
8 
9  * Redistributions of source code must retain the above copyright notice, this list of conditions and
10  the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
12  the following disclaimer in the documentation and/or other materials provided with the distribution.
13 
14  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
15  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
16  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
17  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
18  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21  POSSIBILITY OF SUCH DAMAGE.
22 */
23 
24 #pragma once
25 
26 #include "cinder/Stream.h"
27 #include "cinder/Vector.h"
28 #include "cinder/app/MouseEvent.h"
29 #include "cinder/app/KeyEvent.h"
30 #include "cinder/app/TouchEvent.h"
31 #include "cinder/app/Renderer.h"
32 #include "cinder/Display.h"
33 #include "cinder/app/Window.h"
34 #include <string>
35 #include <vector>
36 #include <list>
37 #include <windows.h>
38 #undef min
39 #undef max
40 
41 // we declare all of the MultiTouch stuff in Win7 here to prevent requiring users to use the Win7 headers
42 #if ! defined( WM_TOUCH )
43 DECLARE_HANDLE(HTOUCHINPUT);
44 typedef struct tagTOUCHINPUT {
45  LONG x;
46  LONG y;
47  HANDLE hSource;
48  DWORD dwID;
49  DWORD dwFlags;
50  DWORD dwMask;
51  DWORD dwTime;
52  ULONG_PTR dwExtraInfo;
53  DWORD cxContact;
54  DWORD cyContact;
56 typedef TOUCHINPUT const * PCTOUCHINPUT;
57 #define TOUCH_COORD_TO_PIXEL(l) ((l) / 100)
58 #define WM_TOUCH 0x0240
59 #endif
60 
61 namespace cinder { namespace app {
62 
63 class AppImplMsw {
64  public:
65  AppImplMsw( class App *aApp );
66  virtual ~AppImplMsw();
67 
68  class App* getApp() { return mApp; }
69 
70  float getFrameRate() const { return mFrameRate; }
71  virtual float setFrameRate( float aFrameRate ) { return -1.0f; }
72  virtual void quit() = 0;
73 
74  virtual WindowRef getWindow() const { return mActiveWindow; }
75  void setWindow( WindowRef window ) { mActiveWindow = window; }
76 
77  static void hideCursor();
78  static void showCursor();
79 
80  static Buffer loadResource( int id, const std::string &type );
81 
82  static fs::path getAppPath();
83  static fs::path getOpenFilePath( const fs::path &initialPath, std::vector<std::string> extensions );
84  static fs::path getSaveFilePath( const fs::path &initialPath, std::vector<std::string> extensions );
85  static fs::path getFolderPath( const fs::path &initialPath );
86 
87  protected:
88  bool setupHasBeenCalled() const { return mSetupHasBeenCalled; }
89  virtual void closeWindow( class WindowImplMsw *windowImpl ) = 0;
90  virtual void setForegroundWindow( WindowRef window ) = 0;
91 
92  class App *mApp;
93  float mFrameRate;
96  ULONG_PTR mGdiplusToken;
97 
98  friend class WindowImplMsw;
99  friend LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
100 };
101 
103  public:
104  WindowImplMsw( const Window::Format &format, RendererRef sharedRenderer, AppImplMsw *appImpl );
105  WindowImplMsw( HWND hwnd, RendererRef renderer, RendererRef sharedRenderer, AppImplMsw *appImpl );
106  virtual ~WindowImplMsw() {}
107 
108  virtual bool isFullScreen() { return mFullScreen; }
109  virtual void setFullScreen( bool fullScreen, const app::FullScreenOptions &options );
110  virtual Vec2i getSize() const { return Vec2i( mWindowWidth, mWindowHeight ); }
111  virtual void setSize( const Vec2i &size );
112  virtual Vec2i getPos() const { return mWindowOffset; }
113  virtual void setPos( const Vec2i &pos );
114  virtual void close();
115  virtual std::string getTitle() const;
116  virtual void setTitle( const std::string &title );
117  virtual void hide();
118  virtual void show();
119  virtual bool isHidden() const;
120  virtual DisplayRef getDisplay() const { return mDisplay; }
121  virtual RendererRef getRenderer() const { return mRenderer; }
122  virtual const std::vector<TouchEvent::Touch>& getActiveTouches() const { return mActiveTouches; }
123  virtual void* getNative() { return mWnd; }
124 
125  void enableMultiTouch();
126  bool isBorderless() const { return mBorderless; }
127  void setBorderless( bool borderless );
128  bool isAlwaysOnTop() const { return mAlwaysOnTop; }
129  void setAlwaysOnTop( bool alwaysOnTop );
130 
133  virtual void keyDown( const KeyEvent &event );
134  virtual void draw();
135  virtual void redraw();
136  virtual void resize();
137 
138  void privateClose();
139  protected:
140  void createWindow( const Vec2i &windowSize, const std::string &title, DisplayRef display, RendererRef sharedRenderer );
141  void completeCreation();
142  static void registerWindowClass();
143  void getScreenSize( int clientWidth, int clientHeight, int *resultWidth, int *resultHeight );
144  void onTouch( HWND hWnd, WPARAM wParam, LPARAM lParam );
145  virtual void toggleFullScreen( const app::FullScreenOptions &options );
146 
149  HWND mWnd;
150  HDC mDC;
153  bool mHidden;
159  std::map<DWORD,Vec2f> mMultiTouchPrev;
160  std::vector<TouchEvent::Touch> mActiveTouches;
162 
163  friend AppImplMsw;
164  friend LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
165 };
166 
167 typedef std::shared_ptr<class BlankingWindow> BlankingWindowRef;
168 
170  public:
171  static BlankingWindowRef create( DisplayRef display ) { return BlankingWindowRef( new BlankingWindow( display ) ); }
172  BlankingWindow( DisplayRef display );
173 
174  void destroy();
175 
176  protected:
177  static void registerWindowClass();
178 
179  HWND mWnd;
180 };
181 
182 } } // namespace cinder::app