NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Window.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 #include "base/Array.hpp"
30 #include "base/String.hpp"
31 #include "base/Math.hpp"
32 #include "base/Hash.hpp"
33 #include "gui/Keys.hpp"
34 #include "base/DLLImports.hpp"
35 #include "gpu/GLContext.hpp"
36 
37 namespace FW
38 {
39 //------------------------------------------------------------------------
40 
41 class Window
42 {
43 public:
44 
45  //------------------------------------------------------------------------
46 
47  enum EventType
48  {
49  EventType_AddListener, /* Listener has been added to a window. */
50  EventType_RemoveListener, /* Listener has been removed from a window. */
51  EventType_Close, /* User has tried to close the window. */
52  EventType_Resize, /* The window has been resized. */
53  EventType_KeyDown, /* User has pressed a key (or mouse button). */
54  EventType_KeyUp, /* User has released a key (or mouse button). */
55  EventType_Char, /* User has typed a character. */
56  EventType_Mouse, /* User has moved the mouse. */
57  EventType_Paint, /* Window contents need to be painted. */
58  EventType_PrePaint, /* Before processing EventType_Paint. */
59  EventType_PostPaint, /* After processing EventType_Paint. */
60  EventType_DropFiles, /* User has dropped one or multiple files into window. */
61  EventType_PasteFiles, /* User has pasted one or multiple files by pressing CTRL+V or SHIFT+INSERT. */
62  EventType_PasteImage, /* User has pasted an image by pressing CTRL+V or SHIFT+INSERT. */
63  };
64 
65  //------------------------------------------------------------------------
66 
67  struct Event
68  {
70  String key; /* Empty unless EventType_KeyDown or EventType_KeyUp. */
71  S32 keyUnicode; /* 0 unless EventType_KeyDown or EventType_KeyUp or if special key. */
72  S32 chr; /* Zero unless EventType_Text. */
73  bool mouseKnown; /* Unchanged unless EventType_Mouse. */
74  Vec2i mousePos; /* Unchanged unless EventType_Mouse. */
75  Vec2i mouseDelta; /* Zero unless EventType_Mouse. */
76  bool mouseDragging; /* One or more mouse buttons are down. */
77  Array<String> files; /* Names of dropped or pasted files. Only valid for EventType_DropFiles. */
78  Image* image; /* Pasted image, deleted by framework after event has been handled. Event handler can either set the pointer to NULL and take ownership, or take a copy of the image and let framework delete the original. */
80  };
81 
82  //------------------------------------------------------------------------
83 
84  class Listener
85  {
86  public:
87  Listener (void) {}
88  virtual ~Listener (void) {}
89 
90  virtual bool handleEvent (const Event& ev) = 0;
91  };
92 
93  //------------------------------------------------------------------------
94 
95 public:
96  Window (void);
97  ~Window (void);
98 
99  void setTitle (const String& title);
100 
101  void setSize (const Vec2i& size);
102  Vec2i getSize (void) const;
103 
104  void setVisible (bool visible);
105  bool isVisible (void) const { return m_isVisible; }
106  void setFullScreen (bool isFull);
107  bool isFullScreen (void) const { return m_isFullScreen; }
109  void realize (void);
110 
111  void setGLConfig (const GLContext::Config& config);
112  const GLContext::Config& getGLConfig (void) const { return m_glConfig; }
113  GLContext* getGL (void); // also makes the context current
114 
115  HWND getHandle (void) const { return m_hwnd; }
116 
117  void repaint (void);
118  void repaintNow (void);
119  void requestClose (void);
120 
121  void enableDrop (bool enable);
122  void enablePaste (bool enable);
123 
124  void addListener (Listener* listener);
125  void removeListener (Listener* listener);
126  void removeListeners (void);
127 
128  bool isKeyDown (const String& key) const { return m_keysDown.contains(key); }
129  bool isMouseKnown (void) const { return m_mouseKnown; }
130  bool isMouseDragging (void) const { return (m_mouseDragCount != 0); }
131  const Vec2i& getMousePos (void) const { return m_mousePos; }
132 
133  void showMessageDialog (const String& title, const String& text);
134  String showFileDialog (const String& title, bool save, const String& filters = "", const String& initialDir = "", bool forceInitialDir = false); // filters = "ext:Title,foo;bar:Title"
135  String showFileLoadDialog (const String& title, const String& filters = "", const String& initialDir = "", bool forceInitialDir = false) { return showFileDialog(title, false, filters, initialDir, forceInitialDir); }
136  String showFileSaveDialog (const String& title, const String& filters = "", const String& initialDir = "", bool forceInitialDir = false) { return showFileDialog(title, true, filters, initialDir, forceInitialDir); }
137  Array<String> showDirLoadDialog (const String& title, const String& initialDir = "");
138 
139  void showModalMessage (const String& msg);
140 
141  static void staticInit (void);
142  static void staticDeinit (void);
143  static HWND createHWND (void);
144  static UPTR setWindowLong (HWND hwnd, int idx, UPTR value);
145  static int getNumOpen (void) { return (s_inited) ? s_open->getSize() : 0; }
146  static void realizeAll (void);
147  static void pollMessages (void);
148 
149  static void traverseDirectory (const char *root, Array<String> &names);
150 
151 private:
152  Event createSimpleEvent (EventType type) { return createGenericEvent(type, "", 0, m_mouseKnown, m_mousePos); }
153  Event createKeyEvent (bool down, const String& key) { return createGenericEvent((down) ? EventType_KeyDown : EventType_KeyUp, key, 0, m_mouseKnown, m_mousePos); }
154  Event createCharEvent (S32 chr) { return createGenericEvent(EventType_Char, "", chr, m_mouseKnown, m_mousePos); }
155  Event createMouseEvent (bool mouseKnown, const Vec2i& mousePos) { return createGenericEvent(EventType_Mouse, "", 0, mouseKnown, mousePos); }
156  Event createFileEvent (EventType type, HANDLE hDrop);
157  Event createGenericEvent (EventType type, const String& key, S32 chr, bool mouseKnown, const Vec2i& mousePos);
158  void postEvent (const Event& ev);
159 
160  void create (void);
161  void destroy (void);
162  void recreate (void);
163 
164  static LRESULT CALLBACK staticWindowProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
165  LRESULT windowProc (UINT uMsg, WPARAM wParam, LPARAM lParam);
166 
167 private:
168  Window (const Window&); // forbidden
169  Window& operator= (const Window&); // forbidden
170 
171 private:
172  static bool s_inited;
173  static Array<Window*>* s_open;
174  static bool s_ignoreRepaint; // Prevents re-entering repaintNow() on Win32 or OpenGL failure.
175 
176  HWND m_hwnd;
177  HDC m_hdc;
178 
179  GLContext::Config m_glConfig;
180  bool m_glConfigDirty;
181  GLContext* m_gl;
182 
183  bool m_isRealized;
184  bool m_isVisible;
185  Array<Listener*> m_listeners;
186  bool m_enablePaste;
187 
188  String m_title;
189  bool m_isFullScreen;
190  Vec2i m_pendingSize;
191  DWORD m_origStyle;
192  RECT m_origRect;
193 
194  Set<String> m_keysDown;
195  bool m_pendingKeyFlush;
196 
197  bool m_mouseKnown;
198  Vec2i m_mousePos;
199  S32 m_mouseDragCount;
200  S32 m_mouseWheelAcc;
201 
202  Vec2i m_prevSize;
203 };
204 
205 //------------------------------------------------------------------------
206 }
static UPTR setWindowLong(HWND hwnd, int idx, UPTR value)
Definition: Window.cpp:573
const GLContext::Config & getGLConfig(void) const
Definition: Window.hpp:112
String showFileDialog(const String &title, bool save, const String &filters="", const String &initialDir="", bool forceInitialDir=false)
Definition: Window.cpp:321
void enableDrop(bool enable)
Definition: Window.cpp:268
__w64 U32 UPTR
Definition: Defs.hpp:106
static void staticDeinit(void)
Definition: Window.cpp:535
void addListener(Listener *listener)
Definition: Window.cpp:282
Vec2i mouseDelta
Definition: Window.hpp:75
HWND getHandle(void) const
Definition: Window.hpp:115
bool mouseDragging
Definition: Window.hpp:76
Window * window
Definition: Window.hpp:79
Vec2i getSize(void) const
Definition: Window.cpp:129
bool isVisible(void) const
Definition: Window.hpp:105
bool isMouseKnown(void) const
Definition: Window.hpp:129
virtual ~Listener(void)
Definition: Window.hpp:88
const Vec2i & getMousePos(void) const
Definition: Window.hpp:131
void toggleFullScreen(void)
Definition: Window.hpp:108
void repaint(void)
Definition: Window.cpp:216
void removeListener(Listener *listener)
Definition: Window.cpp:293
Image * image
Definition: Window.hpp:78
virtual bool handleEvent(const Event &ev)=0
void showModalMessage(const String &msg)
Definition: Window.cpp:487
void setVisible(bool visible)
Definition: Window.cpp:138
void realize(void)
Definition: Window.cpp:184
String showFileSaveDialog(const String &title, const String &filters="", const String &initialDir="", bool forceInitialDir=false)
Definition: Window.hpp:136
bool isMouseDragging(void) const
Definition: Window.hpp:130
signed int S32
Definition: Defs.hpp:88
String showFileLoadDialog(const String &title, const String &filters="", const String &initialDir="", bool forceInitialDir=false)
Definition: Window.hpp:135
void setSize(const Vec2i &size)
Definition: Window.cpp:106
bool isKeyDown(const String &key) const
Definition: Window.hpp:128
GLContext * getGL(void)
Definition: Window.cpp:203
void removeListeners(void)
Definition: Window.cpp:304
static int getNumOpen(void)
Definition: Window.hpp:145
static void traverseDirectory(const char *root, Array< String > &names)
Definition: Window.cpp:609
void showMessageDialog(const String &title, const String &text)
Definition: Window.cpp:312
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value
Definition: DLLImports.inl:84
static void pollMessages(void)
Definition: Window.cpp:593
void setGLConfig(const GLContext::Config &config)
Definition: Window.cpp:195
~Window(void)
Definition: Window.cpp:85
static HWND createHWND(void)
Definition: Window.cpp:550
static void staticInit(void)
Definition: Window.cpp:502
Window(void)
Definition: Window.cpp:54
Array< String > showDirLoadDialog(const String &title, const String &initialDir="")
Definition: Window.cpp:455
EventType type
Definition: Window.hpp:69
Array< String > files
Definition: Window.hpp:77
bool isFullScreen(void) const
Definition: Window.hpp:107
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type
Definition: DLLImports.inl:323
void setTitle(const String &title)
Definition: Window.cpp:95
void setFullScreen(bool isFull)
Definition: Window.cpp:147
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr size
Definition: DLLImports.inl:319
void enablePaste(bool enable)
Definition: Window.cpp:275
void requestClose(void)
Definition: Window.cpp:261
static void realizeAll(void)
Definition: Window.cpp:584
void repaintNow(void)
Definition: Window.cpp:223