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