FreeWRL/FreeX3D  3.0.0
plugin.cpp
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Netscape Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/NPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is mozilla.org code.
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the NPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the NPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 
39 //
40 // CPlugin class implementation
41 //
42 #include <assert.h>
43 #include "plugin.h"
44 #include "npfunctions.h"
45 #include "BasePlugin.h"
46 #include "dllFreeWRL.h"
47 //extern "C"
48 //{
49 // #include "libFreeWRL.h"
50 // //#include <internal.h>
51 // //#include <iglobal.h>
52 //}
53 
54 #ifdef XP_WIN
55 #include <windows.h>
56 #include <windowsx.h>
57 #include <string>
58 #endif
59 
60 #ifdef XP_MAC
61 #include <TextEdit.h>
62 #endif
63 
64 #ifdef XP_UNIX
65 #include <string.h>
66 #endif
67 
68 
69 
73 CPlugin::CPlugin(NPP pNPInstance) :
74  m_pNPInstance(pNPInstance),
75  m_pNPStream(NULL),
76  m_bInitialized(FALSE),
77  m_pScriptableObject(NULL)
78 {
79 #ifdef XP_WIN
80  m_hWnd = NULL;
81 #endif
82  m_pfreeWRLPlayer = NULL;
83 
84  m_pfreeWRLPlayer = new CdllFreeWRL();
85 
86  assert(m_pfreeWRLPlayer);
87 
88  m_sceneUrl = NULL;
89 
90  m_EAIflag = false;
91 
92  NPN_GetValue(m_pNPInstance, NPNVWindowNPObject, &sWindowObj);
93 }
94 
95 CPlugin::~CPlugin()
96 {
97  if (m_pScriptableObject)
98  NPN_ReleaseObject(m_pScriptableObject);
99 
100  if (sWindowObj)
101  NPN_ReleaseObject(sWindowObj);
102 
103  sWindowObj = 0;
104 }
105 
106 #ifdef XP_WIN
107 static LRESULT CALLBACK PluginWinProc(HWND, UINT, WPARAM, LPARAM);
108 static WNDPROC lpOldProc = NULL;
109 #endif
110 
111 NPBool CPlugin::init(NPWindow* pNPWindow)
112 {
113  if(pNPWindow == NULL)
114  return FALSE;
115 
116  m_Window = pNPWindow;
117 
118 #ifdef XP_WIN
119  m_hWnd = (HWND)pNPWindow->window;
120  if(m_hWnd == NULL)
121  return FALSE;
122 
123  // subclass window so we can intercept window messages and
124  // do our drawing to it
125  lpOldProc = SubclassWindow(m_hWnd, (WNDPROC)PluginWinProc);
126 
127  // associate window with our CPlugin object so we can access
128  // it in the window procedure
129  SetWindowLong(m_hWnd, GWL_USERDATA, (LONG)this);
130 #else
131  m_hWnd = (long int*) pNPWindow->window;
132 
133 #endif
134  //m_pfreeWRLPlayer = new CdllFreeWRL(pNPWindow->width,pNPWindow->height,pNPWindow->window,m_EAIflag);
135  //m_pfreeWRLPlayer->onInit(pNPWindow->window,pNPWindow->width,pNPWindow->height, m_EAIflag);
136  bool frontend_handles_display_thread = false;
137  m_pfreeWRLPlayer->onInit(pNPWindow->width,pNPWindow->height, pNPWindow->window, m_EAIflag, frontend_handles_display_thread);
138 
139  //m_pfreeWRLPlayer->onLoad(pNPWindow->window,m_sceneUrl);
140  m_pfreeWRLPlayer->onLoad(m_sceneUrl);
141 
142 
143  m_bInitialized = TRUE;
144  return TRUE;
145 }
146 
147 void CPlugin::shut()
148 {
149  m_pfreeWRLPlayer->onClose(); //(m_Window->window);
150  //TODO: since we are closing a bunch of threads, maybe should be better to set a check on them.
151 
152 #ifdef XP_WIN
153  // subclass it back
154  SubclassWindow(m_hWnd, lpOldProc);
155  m_hWnd = NULL;
156 #endif
157 
158  if(m_sceneUrl)
159  free(m_sceneUrl);
160 
161 
162  delete m_pfreeWRLPlayer;
163 
164  m_bInitialized = FALSE;
165 }
166 
167 NPBool CPlugin::isInitialized()
168 {
169  return m_bInitialized;
170 }
171 
172 int16_t CPlugin::handleEvent(void* event)
173 {
174 #ifdef XP_MAC
175  NPEvent* ev = (NPEvent*)event;
176  if (m_Window) {
177  Rect box = { m_Window->y, m_Window->x,
178  m_Window->y + m_Window->height, m_Window->x + m_Window->width };
179  if (ev->what == updateEvt) {
180  ::TETextBox(m_String, strlen(m_String), &box, teJustCenter);
181  }
182  }
183 #endif
184  return 0;
185 }
186 
187 // this will force to draw a version string in the plugin window
188 void CPlugin::showVersion()
189 {
190  const char *ua = NPN_UserAgent(m_pNPInstance);
191  strcpy_s(m_String, ua);
192 
193 #ifdef XP_WIN
194  InvalidateRect(m_hWnd, NULL, TRUE);
195  UpdateWindow(m_hWnd);
196 #endif
197 
198  /*if (m_Window) {
199  NPRect r =
200  {
201  (uint16_t)m_Window->y, (uint16_t)m_Window->x,
202  (uint16_t)(m_Window->y + m_Window->height),
203  (uint16_t)(m_Window->x + m_Window->width)
204  };
205 
206  NPN_InvalidateRect(m_pNPInstance, &r);
207  }*/
208 }
209 
210 // this will clean the plugin window
211 void CPlugin::clear()
212 {
213  strcpy_s(m_String, "");
214 
215 #ifdef XP_WIN
216  InvalidateRect(m_hWnd, NULL, TRUE);
217  UpdateWindow(m_hWnd);
218 #endif
219 }
220 
221 void CPlugin::getVersion(char* *aVersion)
222 {
223  const char *ua = NPN_UserAgent(m_pNPInstance);
224  char*& version = *aVersion;
225  version = (char*)NPN_MemAlloc(1 + strlen(ua));
226  if (version)
227  strcpy_s(version, sizeof(version), ua);
228 }
229 
230 NPObject *
231 CPlugin::GetScriptableObject()
232 {
233  if (!m_pScriptableObject) {
234  m_pScriptableObject =
235  NPN_CreateObject(m_pNPInstance,
236  GET_NPOBJECT_CLASS(BasePlugin));
237  }
238 
239  if (m_pScriptableObject) {
240  NPN_RetainObject(m_pScriptableObject);
241  }
242 
243  return m_pScriptableObject;
244 }
245 #define SCURSE 1
246 #define ACURSE 0
247 #define NCURSE 2
248 static HCURSOR hSensor, hArrow;
249 static HCURSOR cursor;
250 void loadCursors()
251 {
252  hSensor = LoadCursor(NULL,IDC_HAND); /* prepare sensor_cursor */
253  hArrow = LoadCursor( NULL, IDC_ARROW );
254 }
255 void updateCursorStyle0(int cstyle)
256 {
257  if(!hSensor) loadCursors();
258  switch(cstyle){
259  case SCURSE:
260  SetCursor(hSensor); break;
261  case ACURSE:
262  SetCursor(hArrow); break;
263  case NCURSE:
264  SetCursor(NULL); break;
265  default:
266  SetCursor(hArrow);
267  }
268 }
269 LRESULT CPlugin::handleWindowEvents(HWND hWnd, UINT eventmsg, WPARAM wParam, LPARAM lParam)
270 {
271  int cursorStyle;
272  switch(eventmsg)
273  {
274  //resize is bombing in FF and Chrome, but not IE, whether or not I have any of the below uncommented.
275  //case WM_SIZE:
276  // {
277  // if(wParam == SIZE_RESTORED)
278  // m_pfreeWRLPlayer->onResize(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
279  // return 0;
280  // }
281  //case WM_PAINT:
282  // {
283  // RECT rc;
284  // GetClientRect(hWnd, &rc);
285  // m_pfreeWRLPlayer->onResize(rc.right, rc.bottom);
286  // return 0;
287  // }
288  case WM_LBUTTONDOWN:
289  {
290  cursorStyle = m_pfreeWRLPlayer->onMouse(4, 1,GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam));
291  updateCursorStyle0(cursorStyle);
292  return 0;
293  }
294  case WM_LBUTTONUP:
295  {
296  cursorStyle = m_pfreeWRLPlayer->onMouse(5, 1,GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam));
297  updateCursorStyle0(cursorStyle);
298  return 0;
299  }
300  case WM_MBUTTONDOWN:
301  {
302  cursorStyle = m_pfreeWRLPlayer->onMouse(4, 2,GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam));
303  updateCursorStyle0(cursorStyle);
304  return 0;
305  }
306  case WM_MBUTTONUP:
307  {
308  cursorStyle = m_pfreeWRLPlayer->onMouse(5, 2,GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam));
309  updateCursorStyle0(cursorStyle);
310  return 0;
311  }
312  case WM_RBUTTONDOWN:
313  {
314  cursorStyle = m_pfreeWRLPlayer->onMouse(4, 3,GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam));
315  updateCursorStyle0(cursorStyle);
316  return 0;
317  }
318  case WM_RBUTTONUP:
319  {
320  cursorStyle = m_pfreeWRLPlayer->onMouse(5, 3,GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam));
321  updateCursorStyle0(cursorStyle);
322  updateCursorStyle0(cursorStyle);
323  return 0;
324  }
325  case WM_MOUSEMOVE:
326  {
327  cursorStyle = m_pfreeWRLPlayer->onMouse(6, 0,GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam));
328  updateCursorStyle0(cursorStyle);
329  return 0;
330  }
331  case WM_KEYDOWN:
332  {
333  m_pfreeWRLPlayer->onKey(CdllFreeWRL::KEYDOWN,wParam);
334  return 0;
335  }
336  case WM_KEYUP:
337  {
338  m_pfreeWRLPlayer->onKey(CdllFreeWRL::KEYUP,wParam);
339  return 0;
340  }
341  case WM_CHAR:
342  {
343  m_pfreeWRLPlayer->onKey(CdllFreeWRL::KEYPRESS,wParam);
344  return 0;
345  }
346  case WM_DESTROY:
347  {
348 
349  break;
350  }
351 
352  default:
353  break;
354  }
355 
356  return 0;
357 }
358 
359 void CPlugin::setSceneUrl(char* sceneUrl)
360 {
361  //char* buf = (char*) NPN_MemAlloc(strlen(sceneUrl)+1);
362  size_t buffersize = strlen(sceneUrl)+1;
363 
364  char* buf = (char*) calloc(buffersize,sizeof(char));
365 
366  strcpy_s(buf,buffersize,sceneUrl);
367 
368  m_sceneUrl = buf;
369 }
370 
371 void CPlugin::setEAIFlag()
372 {
373  m_EAIflag = true;
374 }
375 
376 
377 #ifdef XP_WIN
378 static LRESULT CALLBACK PluginWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
379 {
380  CPlugin *plugin = (CPlugin *)(LONG_PTR) GetWindowLong(hWnd, GWL_USERDATA);
381 
382  if (!plugin){
383  switch (msg) {
384  case WM_PAINT:
385  {
386  std::string errormsg("Instance Error: Plugin has not been initialized");
387 
388  PAINTSTRUCT ps;
389  RECT rc;
390  HDC hdc = BeginPaint(hWnd, &ps);
391 
392  GetClientRect(hWnd, &rc);
393  FrameRect(hdc, &rc, GetStockBrush(BLACK_BRUSH));
394  DrawText(hdc, (LPCTSTR) errormsg.c_str(), errormsg.length(), &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
395  EndPaint(hWnd, &ps);
396  }
397  break;
398  default:
399  break;
400  }
401  }
402  else
403  {
404  plugin->handleWindowEvents(hWnd,msg,wParam,lParam);
405  }
406  return DefWindowProc(hWnd, msg, wParam, lParam);
407 }
408 #endif
Definition: plugin.h:46
CPlugin(NPP pNPInstance)
CPlugin class.constructor.
Definition: plugin.cpp:73
Definition: npapi.h:148