FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
PluginWindowlessWin.cpp
1 #include "PluginWindowlessWin.h"
2 #include "ConstructDefaultPluginWindows.h"
3 #include "AsyncDrawService.h"
4 
5 #include "PluginEvents/WindowsEvent.h"
6 #include "PluginEvents/GeneralEvents.h"
7 #include "PluginEvents/DrawingEvents.h"
8 #include "PluginEvents/MouseEvents.h"
9 #include "PluginEvents/KeyboardEvents.h"
10 #include "precompiled_headers.h" // On windows, everything above this line in PCH
11 
12 using namespace std; using namespace FB;
13 
14 FB::PluginWindowlessWin* FB::createPluginWindowless(const FB::WindowContextWindowless& ctx)
15 {
16  return new PluginWindowlessWin(ctx);
17 }
18 
19 PluginWindowlessWin::PluginWindowlessWin(const FB::WindowContextWindowless& ctx)
20  : m_hdc(ctx.drawable)
21  , m_browserHwnd(NULL)
22  , m_x(0), m_y(0), m_width(0), m_height(0)
23  , m_clipTop(0), m_clipLeft(0), m_clipBottom(0), m_clipRight(0)
24  , m_asyncDraw(ctx.asyncDraw)
25 {}
26 
27 PluginWindowlessWin::~PluginWindowlessWin() {}
28 
29 void PluginWindowlessWin::translateWindowToPlugin(int32_t &x, int32_t &y) const {
30  int32_t tempX, tempY;
31  tempX = x - m_x;
32  tempY = y - m_y;
33  x = tempX;
34  y = tempY;
35 }
36 
37 bool PluginWindowlessWin::HandleEvent(uint32_t event, uint32_t wParam, uint32_t lParam, LRESULT& lRes)
38 {
39  {
40  LRESULT lres(0);
41  WindowsEvent nEvt((HWND)NULL, event, wParam, lParam, lres);
42  if (SendEvent(&nEvt)) {
43  lRes = nEvt.lRes;
44  return TRUE;
45  }
46  }
47  unsigned int modifierState = (GetKeyState(VK_SHIFT) & 0x8000) != 0 ? MouseButtonEvent::ModifierState_Shift : 0;
48  modifierState += (GetKeyState(VK_CONTROL) & 0x8000) != 0 ? MouseButtonEvent::ModifierState_Control : 0;
49  modifierState += (GetKeyState(VK_MENU) & 0x8000) != 0 ? MouseButtonEvent::ModifierState_Menu : 0;
50  switch(event) {
51  case WM_LBUTTONDOWN:
52  {
53  int32_t x = GET_X_LPARAM(lParam);
54  int32_t y = GET_Y_LPARAM(lParam);
55  translateWindowToPlugin(x, y);
56  MouseDownEvent ev(MouseButtonEvent::MouseButton_Left, x, y, modifierState);
57  return SendEvent(&ev);
58  }
59  case WM_RBUTTONDOWN:
60  {
61  int32_t x = GET_X_LPARAM(lParam);
62  int32_t y = GET_Y_LPARAM(lParam);
63  translateWindowToPlugin(x, y);
64  MouseDownEvent ev(MouseButtonEvent::MouseButton_Right, x, y, modifierState);
65  return SendEvent(&ev);
66  }
67  case WM_MBUTTONDOWN:
68  {
69  int32_t x = GET_X_LPARAM(lParam);
70  int32_t y = GET_Y_LPARAM(lParam);
71  translateWindowToPlugin(x, y);
72  MouseDownEvent ev(MouseButtonEvent::MouseButton_Middle, x, y, modifierState);
73  return SendEvent(&ev);
74  }
75  case WM_LBUTTONUP:
76  {
77  int32_t x = GET_X_LPARAM(lParam);
78  int32_t y = GET_Y_LPARAM(lParam);
79  translateWindowToPlugin(x, y);
80  MouseUpEvent ev(MouseButtonEvent::MouseButton_Left, x, y, modifierState);
81  return SendEvent(&ev);
82  }
83  case WM_RBUTTONUP:
84  {
85  int32_t x = GET_X_LPARAM(lParam);
86  int32_t y = GET_Y_LPARAM(lParam);
87  translateWindowToPlugin(x, y);
88  MouseUpEvent ev(MouseButtonEvent::MouseButton_Right, x, y, modifierState);
89  return SendEvent(&ev);
90  }
91  case WM_MBUTTONUP:
92  {
93  int32_t x = GET_X_LPARAM(lParam);
94  int32_t y = GET_Y_LPARAM(lParam);
95  translateWindowToPlugin(x, y);
96  MouseUpEvent ev(MouseButtonEvent::MouseButton_Middle, x, y, modifierState);
97  return SendEvent(&ev);
98  }
99  case WM_MOUSEMOVE:
100  {
101  int32_t x = GET_X_LPARAM(lParam);
102  int32_t y = GET_Y_LPARAM(lParam);
103  translateWindowToPlugin(x, y);
104  MouseMoveEvent ev(x, y);
105  return SendEvent(&ev);
106  }
107  case WM_KEYUP:
108  {
109  FBKeyCode fb_key = WinKeyCodeToFBKeyCode((unsigned int)wParam);
110  KeyUpEvent ev(fb_key, (unsigned int)wParam);
111  return SendEvent(&ev);
112  }
113  case WM_KEYDOWN:
114  {
115  FBKeyCode fb_key = WinKeyCodeToFBKeyCode((unsigned int)wParam);
116  KeyDownEvent ev(fb_key, (unsigned int)wParam);
117  return SendEvent(&ev);
118  }
119  }
120  return 0;
121 }
122 
123 bool PluginWindowlessWin::HandleDraw(HDC dc, FB::Rect bounds) {
124  if (dc != m_hdc) {
125  setHDC(dc);
126  }
127 
128  RefreshEvent ev(bounds);
129  if (!SendEvent(&ev)) {
130  FB::Rect pos = getWindowPosition();
131  ::SetTextAlign(dc, TA_CENTER|TA_BASELINE);
132  LPCTSTR pszText = _T("FireBreath Plugin");
133  ::TextOut(dc, pos.left + (pos.right - pos.left) / 2, pos.top + (pos.bottom - pos.top) / 2, pszText, lstrlen(pszText));
134  }
135 
136  return true;
137 }
138 
140  int32_t top = m_y;
141  int32_t left = m_x;
142  int32_t bottom = m_y + m_height;
143  int32_t right = m_x + m_width;
144  FB::Rect r = { top, left, bottom, right };
145  return r;
146 }
147 
148 void PluginWindowlessWin::setWindowPosition(int32_t x, int32_t y, uint32_t width, uint32_t height) {
149  bool changed = x != m_x || y != m_y || height != m_height || width != m_width;
150 
151  m_x = x;
152  m_y = y;
153  m_height = height;
154  m_width = width;
155 
156  if (changed) {
157  if (m_asyncDraw)
158  m_asyncDraw->resized(width, height);
159  ResizedEvent ev;
160  SendEvent(&ev); //notify the plugin the window has changed position/size
161  }
162 }
163 
165  FB::Rect r = { m_clipTop, m_clipLeft, m_clipBottom, m_clipRight };
166  return r;
167 }
168 
169 void PluginWindowlessWin::setWindowClipping(int32_t top, int32_t left, int32_t bottom, int32_t right) {
170  m_clipTop = top;
171  m_clipLeft = left;
172  m_clipBottom = bottom;
173  m_clipRight = right;
174 }
175 
176 void PluginWindowlessWin::setWindowClipping(FB::Rect clip) {
177  m_clipTop = clip.top;
178  m_clipLeft = clip.left;
179  m_clipBottom = clip.bottom;
180  m_clipRight = clip.right;
181 }
182 
184 {
185  if (m_invalidateWindow)
186  m_invalidateWindow(0, 0, getWindowWidth(), getWindowHeight());
187 }
188 
189 AsyncDrawServicePtr PluginWindowlessWin::getAsyncDrawService() const
190 {
191  return m_asyncDraw;
192 }
Fired for a key down event.
Fired when the mouse moves.
Definition: MouseEvents.h:50
Windows specific implementation of PluginWindow.
Generic windows event. All windows events going through the winproc get first fired as a WindowEvent ...
Definition: WindowsEvent.h:36
Fired when a mouse up event occurs.
Definition: MouseEvents.h:105
Fired for a key up event.
FBKeyCode WinKeyCodeToFBKeyCode(unsigned int wparam)
Converts a windows key code to a FireBreath key code.
Definition: KeyCodesWin.cpp:22
FBKeyCode
Values that represent different keys in a platform agnostic way.
Definition: KeyCodes.h:26
void InvalidateWindow() const
Invalidate window. This should tell the OS to send a REFRESH event.
FB::Rect getWindowPosition() const
Gets the position rect of the window.
Fired when a mouse down event occurs.
Definition: MouseEvents.h:83
FB::Rect getWindowClipping() const
Gets the clipping rect of the window.
Fired when the plugin should repaint itself (such as on windows when WM_PAINT is received) ...
Definition: DrawingEvents.h:55
virtual bool SendEvent(PluginEvent *evt)
Sends an event to all attached sinks.
Fired when the plugin window is resized.
Definition: DrawingEvents.h:36