Panda3D
winGraphicsWindow.h
1 // Filename: winGraphicsWindow.h
2 // Created by: drose (20Dec02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef WINGRAPHICSWINDOW_H
16 #define WINGRAPHICSWINDOW_H
17 
18 #include "pandabase.h"
19 #include "graphicsWindow.h"
20 #ifndef WIN32_LEAN_AND_MEAN
21  #define WIN32_LEAN_AND_MEAN 1
22 #endif
23 #include <windows.h>
24 
25 class WinGraphicsPipe;
26 
27 #define PM_ACTIVE (WM_APP+123)
28 
29 #define PM_INACTIVE (WM_APP+124)
30 
31 #define MAX_TOUCHES 20
32 
33 typedef struct {
34  int x;
35  int y;
36  int width;
37  int height;
38 }
40 
41 ////////////////////////////////////////////////////////////////////
42 // Class : WinGraphicsWindow
43 // Description : An abstract base class for glGraphicsWindow and
44 // dxGraphicsWindow (and, in general, graphics windows
45 // that interface with the Microsoft Windows API).
46 //
47 // This class includes all the code for manipulating
48 // windows themselves: opening them, closing them,
49 // responding to user keyboard and mouse input, and so
50 // on. It does not make any 3-D rendering calls into
51 // the window; that is the province of the
52 // GraphicsStateGuardian.
53 ////////////////////////////////////////////////////////////////////
54 class EXPCL_PANDAWIN WinGraphicsWindow : public GraphicsWindow {
55 public:
57  const string &name,
58  const FrameBufferProperties &fb_prop,
59  const WindowProperties &win_prop,
60  int flags,
62  GraphicsOutput *host);
63  virtual ~WinGraphicsWindow();
64 
65  virtual bool move_pointer(int device, int x, int y);
66 
67  virtual void close_ime();
68 
69  virtual void begin_flip();
70 
71  virtual void process_events();
72  virtual void set_properties_now(WindowProperties &properties);
73  void receive_windows_message(unsigned int msg, int wparam, int lparam);
74  virtual LONG window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
75  static LONG WINAPI static_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
76  virtual bool handle_mouse_motion(int x, int y);
77  virtual void handle_mouse_exit();
78 
79  INLINE HWND get_ime_hwnd();
80 
81  virtual void add_window_proc( const GraphicsWindowProc* wnd_proc_object );
82  virtual void remove_window_proc( const GraphicsWindowProc* wnd_proc_object );
83  virtual void clear_window_procs();
84  virtual bool supports_window_procs() const;
85 
86  virtual bool is_touch_event(GraphicsWindowProcCallbackData* callbackData);
87  virtual int get_num_touches();
88  virtual TouchInfo get_touch_info(int index);
89 
90 protected:
91  void trigger_flip();
92  virtual void close_window();
93  virtual bool open_window();
94  virtual void fullscreen_minimized(WindowProperties &properties);
95  virtual void fullscreen_restored(WindowProperties &properties);
96 
97  virtual bool do_reshape_request(int x_origin, int y_origin, bool has_origin,
98  int x_size, int y_size);
99 
100  virtual void handle_reshape();
101  virtual bool do_fullscreen_resize(int x_size, int y_size);
102 
103  virtual bool do_fullscreen_switch();
104  virtual bool do_windowed_switch();
105  virtual bool do_fullscreen_enable();
106  virtual bool do_fullscreen_disable();
107 
108  virtual bool calculate_metrics(bool fullscreen, DWORD style,
109  WINDOW_METRICS &metrics, bool &has_origin);
110 
111  virtual DWORD make_style(bool fullscreen);
112 
113  virtual void reconsider_fullscreen_size(DWORD &x_size, DWORD &y_size,
114  DWORD &bitdepth);
115 
116  virtual void support_overlay_window(bool flag);
117 
118 private:
119  bool open_graphic_window(bool fullscreen);
120  void adjust_z_order();
121  void adjust_z_order(WindowProperties::ZOrder last_z_order,
122  WindowProperties::ZOrder this_z_order);
123  void initialize_input_devices();
124  void handle_raw_input(HRAWINPUT hraw);
125  void track_mouse_leaving(HWND hwnd);
126 
127  void set_focus();
128 
129  static void process_1_event();
130 
131  void handle_keypress(ButtonHandle key, int x, int y, double time);
132  void handle_keyresume(ButtonHandle key, double time);
133  void handle_keyrelease(ButtonHandle key, double time);
134  void handle_raw_keypress(ButtonHandle key, double time);
135  void handle_raw_keyrelease(ButtonHandle key, double time);
136  ButtonHandle lookup_key(WPARAM wparam) const;
137  ButtonHandle lookup_raw_key(LPARAM lparam) const;
138  virtual ButtonMap *get_keyboard_map() const;
139  INLINE int translate_mouse(int pos) const;
140  INLINE void set_cursor_in_window();
141  INLINE void set_cursor_out_of_window();
142 
143  INLINE static double get_message_time();
144 
145  void resend_lost_keypresses();
146  void release_all_buttons();
147  static void update_cursor_window(WinGraphicsWindow *to_window);
148  static void hide_or_show_cursor(bool hide_cursor);
149 
150  static bool find_acceptable_display_mode(DWORD dwWidth, DWORD dwHeight,
151  DWORD bpp, DEVMODE &dm);
152  static void show_error_message(DWORD message_id = 0);
153 
154 protected:
155  HWND _hWnd;
156  HWND _hparent;
157 
158 private:
159  HWND _ime_hWnd;
160  bool _ime_open;
161  bool _ime_active;
162  bool _tracking_mouse_leaving;
163  bool _bCursor_in_WindowClientArea;
164  HANDLE _input_device_handle[32];
165  HCURSOR _cursor;
166  DEVMODE _fullscreen_display_mode;
167 
168  bool _lost_keypresses;
169 
170  // These are used to store the status of the individual left and right
171  // shift, control, and alt keys. Keyboard events are not sent for
172  // these individual keys, but for each pair as a whole. The status
173  // of each key must be checked as keypress and keyrelease events are
174  // received.
175  bool _lshift_down;
176  bool _rshift_down;
177  bool _lcontrol_down;
178  bool _rcontrol_down;
179  bool _lalt_down;
180  bool _ralt_down;
181 
182  // following adds support platform specfic window processing
183  // functions.
185  WinProcClasses _window_proc_classes;
186 
187 #ifdef HAVE_WIN_TOUCHINPUT
188  UINT _numTouches;
189  TOUCHINPUT _touches[MAX_TOUCHES];
190 #endif
191 
192 private:
193  // We need this map to support per-window calls to window_proc().
194  typedef map<HWND, WinGraphicsWindow *> WindowHandles;
195  static WindowHandles _window_handles;
196 
197  // And we need a static pointer to the current WinGraphicsWindow we
198  // are creating at the moment, since CreateWindow() starts
199  // generating window events before it gives us the window handle.
200  static WinGraphicsWindow *_creating_window;
201 
202  // This tracks the current GraphicsWindow whose client area contains
203  // the mouse. There will only be one of these at a time, and
204  // storing the pointer here allows us to handle ambiguities in the
205  // order in which messages are passed from Windows to the various
206  // windows we manage. This pointer is used by
207  // set_cursor_in_window() to determine when it is time to call
208  // update_cursor() to hide the cursor (or do other related
209  // operations).
210  static WinGraphicsWindow *_cursor_window;
211  static bool _cursor_hidden;
212  static bool _got_saved_params;
213  static int _saved_mouse_trails;
214  static BOOL _saved_cursor_shadow;
215  static BOOL _saved_mouse_vanish;
216 
217  // The mouse constraints before applying mouse mode M_confined.
218  static RECT _mouse_unconfined_cliprect;
219 
220  // Since the Panda API requests icons and cursors by filename, we
221  // need a table mapping filenames to handles, so we can avoid
222  // re-reading the file each time we change icons.
224  static IconFilenames _icon_filenames;
225  static IconFilenames _cursor_filenames;
226 
227  static HICON get_icon(const Filename &filename);
228  static HCURSOR get_cursor(const Filename &filename);
229 
230  // The table of window classes we have registered. We need to
231  // register a different window class for each different window icon
232  // (the cursor we can specify dynamically, later). We might have
233  // other requirements too, later.
234  class WindowClass {
235  public:
236  INLINE WindowClass(const WindowProperties &props);
237  INLINE bool operator < (const WindowClass &other) const;
238 
239  wstring _name;
240  HICON _icon;
241  };
242 
244  static WindowClasses _window_classes;
245  static int _window_class_index;
246 
247  static const WindowClass &register_window_class(const WindowProperties &props);
248 private:
249  // This subclass of WindowHandle is stored in _window_handle to
250  // represent this particular window. We use it to add hooks for
251  // communicating with the parent window, in particular to receive
252  // keyboard events from the parent window when necessary.
253  class WinWindowHandle : public WindowHandle {
254  public:
255  WinWindowHandle(WinGraphicsWindow *window,
256  const WindowHandle &copy);
257  void clear_window();
258 
259  protected:
260  virtual void receive_windows_message(unsigned int msg, int wparam, int lparam);
261 
262  private:
263  // Not reference-counted, to avoid a circular reference count.
264  WinGraphicsWindow *_window;
265 
266  public:
267  static TypeHandle get_class_type() {
268  return _type_handle;
269  }
270  static void init_type() {
271  WindowHandle::init_type();
272  register_type(_type_handle, "WinWindowHandle",
273  WindowHandle::get_class_type());
274  }
275  virtual TypeHandle get_type() const {
276  return get_class_type();
277  }
278  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
279 
280  private:
281  static TypeHandle _type_handle;
282  };
283 
284 public:
285  static TypeHandle get_class_type() {
286  return _type_handle;
287  }
288  static void init_type() {
289  GraphicsWindow::init_type();
290  register_type(_type_handle, "WinGraphicsWindow",
291  GraphicsWindow::get_class_type());
292  WinWindowHandle::init_type();
293  }
294  virtual TypeHandle get_type() const {
295  return get_class_type();
296  }
297  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
298 
299 private:
300  static TypeHandle _type_handle;
301 };
302 
303 #define PRINT_LAST_ERROR 0
304 extern EXPCL_PANDAWIN void PrintErrorMessage(DWORD msgID);
305 extern EXPCL_PANDAWIN void ClearToBlack(HWND hWnd, const WindowProperties &props);
306 extern EXPCL_PANDAWIN void get_client_rect_screen(HWND hwnd, RECT *view_rect);
307 
308 #include "winGraphicsWindow.I"
309 
310 #endif
This specialization on CallbackData is passed when the callback is initiated from from an implementat...
This is our own Panda specialization on the default STL map.
Definition: pmap.h:52
This object represents a window on the desktop, not necessarily a Panda window.
Definition: windowHandle.h:40
A window, fullscreen or on a desktop, into which a graphics device sends its output for interactive d...
A ButtonHandle represents a single button from any device, including keyboard buttons and mouse butto...
Definition: buttonHandle.h:28
A container for the various kinds of properties we might ask to have on a graphics window before we o...
An abstract base class for glGraphicsWindow and dxGraphicsWindow (and, in general, graphics windows that interface with the Microsoft Windows API).
Stores information for a single touch event.
Definition: touchInfo.h:24
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
An object to create GraphicsOutputs that share a particular 3-D API.
Definition: graphicsPipe.h:58
This is a base class for the various different classes that represent the result of a frame of render...
Defines an interface for storing platform-specific window processor methods.
This is an abstract base class for wglGraphicsPipe and wdxGraphicsPipe; that is, those graphics pipes...
Encapsulates all the communication with a particular instance of a given rendering backend...
This class is the main interface to controlling the render process.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
A container for the various kinds of properties we might ask to have on a graphics frameBuffer before...
This class represents a map containing all of the buttons of a (keyboard) device, though it can also ...
Definition: buttonMap.h:33