]> git.saurik.com Git - wxWidgets.git/blob - utils/nplugin/src/npapp.cpp
now MSW stuff is complete
[wxWidgets.git] / utils / nplugin / src / npapp.cpp
1 /*
2 * File: NPApp.cc
3 * Purpose: wxPluginApp implementation
4 * Author: Julian Smart
5 * Created: 1997
6 * Updated:
7 * Copyright: (c) Julian Smart
8 */
9
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #include "NPApp.h"
18 #include "NPFrame.h"
19
20 #include <windows.h>
21
22 IMPLEMENT_ABSTRACT_CLASS(wxPluginApp, wxApp)
23
24 wxPluginApp *wxGetPluginApp(void)
25 {
26 if ( wxTheApp && wxTheApp->IsKindOf(CLASSINFO(wxPluginApp)))
27 return (wxPluginApp *)wxTheApp;
28 else
29 return NULL;
30 }
31
32 wxPluginApp::wxPluginApp(void)
33 {
34 m_data.m_argc = NULL;
35 m_data.m_argn = NULL;
36 m_data.m_argv = NULL;
37 m_data.m_type = 0;
38 m_data.m_instance = 0;
39 m_data.m_mode = 0;
40 m_data.m_window = 0;
41 }
42
43 wxPluginApp::~wxPluginApp(void)
44 {
45 if ( m_data.m_argn )
46 delete[] m_data.m_argn;
47 if ( m_data.m_argv )
48 delete[] m_data.m_argv;
49 }
50
51 // Add a frame
52 void wxPluginApp::AddFrame(wxPluginFrame *frame)
53 {
54 m_frames.Append(frame);
55 }
56
57 // Remove a frame
58 void wxPluginApp::RemoveFrame(wxPluginFrame *frame)
59 {
60 m_frames.DeleteObject(frame);
61 }
62
63 // Find a frame given a NP instance
64 wxPluginFrame *wxPluginApp::FindFrame(NPP instance)
65 {
66 wxNode *node = m_frames.First();
67 while ( node )
68 {
69 wxPluginFrame *frame = (wxPluginFrame *)node->Data();
70 if ( frame->GetInstance() == instance )
71 {
72 return frame;
73 }
74 node = node->Next();
75 }
76 return NULL;
77 }
78
79 void wxPluginApp::SetAttributeValues(const int n, char *argn[], char *argv[])
80 {
81 if ( m_data.m_argn )
82 delete[] m_data.m_argn;
83 if ( m_data.m_argv )
84 delete[] m_data.m_argv;
85
86 m_data.m_argc = n;
87
88 m_data.m_argn = new wxString[n];
89 m_data.m_argv = new wxString[n];
90 int i;
91 for ( i = 0; i < n ; i ++)
92 {
93 m_data.m_argn[i] = argn[i];
94 m_data.m_argv[i] = argv[i];
95 }
96 }
97
98 ///////////////////////////////////////////////////////////////
99 // Netscape Plugin API calls routed via wxPluginApp
100
101 NPError wxPluginApp::NPP_Destroy(NPP instance, NPSavedData** save)
102 {
103 wxPluginFrame *frame = FindFrame(instance);
104 if ( frame )
105 {
106 frame->OnClose();
107 delete frame;
108 }
109 return NPERR_NO_ERROR;
110 }
111
112 NPError wxPluginApp::NPP_DestroyStream(NPP instance, NPStream* stream, NPError reason)
113 {
114 return NPERR_NO_ERROR;
115 }
116
117 /*
118 jref wxPluginApp::NPP_GetJavaClass(void)
119 {
120 return 0;
121 }
122 */
123
124 NPError wxPluginApp::NPP_Initialize(void)
125 {
126 static int init = FALSE;
127
128 if ( init == TRUE )
129 MessageBox(NULL, "wxPluginApp::NPP_Initialize:\nabout to call wxEntry for 2nd time!!!", "wxPlugin", MB_OK);
130
131 wxEntry((WXHINSTANCE) GetModuleHandle(NULL));
132
133 init = TRUE;
134
135 // MessageBox(NULL, "wxPluginApp::NPP_Initialize: have called wxEntry", "wxPlugin", MB_OK);
136 return NPERR_NO_ERROR;
137 }
138
139 NPError wxPluginApp::NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode,
140 int16 argc, char* argn[], char* argv[], NPSavedData* saved)
141 {
142 // MessageBox(NULL, "wxPluginApp::NPP_New", "wxPlugin", MB_OK);
143
144 // Save values so frame can be created in first NPP_SetWindow
145 if ( m_data.m_instance != 0 )
146 {
147 MessageBox(NULL, "wxPluginApp::NPP_New: whoops, 2 NPP_New calls in succession without NPP_SetWindow.\n Need to modify my code!", "wxPlugin", MB_OK);
148 return NPERR_NO_ERROR;
149 }
150
151 m_data.m_instance = instance;
152 m_data.m_type = pluginType;
153 m_data.m_mode = mode;
154
155 SetAttributeValues(argc, argn, argv);
156
157 // Unfortunately, we may get a stream event before we've got a valid window
158 // handle, so we just have to go ahead and create a new instance.
159 wxPluginFrame *frame = OnNewInstance(m_data);
160
161 m_data.m_instance = NULL;
162 m_data.m_window = NULL;
163 delete[] m_data.m_argv;
164 delete[] m_data.m_argn;
165 m_data.m_argv = NULL;
166 m_data.m_argn = NULL;
167
168 return NPERR_NO_ERROR;
169 }
170
171 NPError wxPluginApp::NPP_NewStream(NPP instance, NPMIMEType type, NPStream *stream,
172 NPBool seekable, uint16* stype)
173 {
174 // By default, we want to receive a file instead of a stream.
175 wxPluginFrame *frame = FindFrame(instance);
176 if ( frame )
177 {
178 return frame->OnNPNewStream(type, stream, seekable, stype);
179 }
180 return NPERR_NO_ERROR;
181 }
182
183 void wxPluginApp::NPP_Print(NPP instance, NPPrint* printInfo)
184 {
185 if (instance == NULL)
186 return;
187
188 wxPluginFrame *frame = FindFrame(instance);
189 if ( frame )
190 {
191 frame->OnNPPrint(printInfo);
192 }
193 }
194
195 NPError wxPluginApp::NPP_SetWindow(NPP instance, NPWindow* window)
196 {
197 // MessageBox(NULL, "wxPluginApp::NPP_SetWindow", "wxPlugin", MB_OK);
198
199 if ( window )
200 wxDebugMsg("%d\n", (int) window->window);
201
202 wxPluginFrame *frame = FindFrame(instance);
203 if ( frame )
204 {
205 frame->SetNPWindow(window);
206 }
207 else
208 {
209 #if 0
210 // No such frame: must make it.
211 if ( m_data.m_instance == NULL )
212 {
213 MessageBox(NULL, "wxPluginApp::NPP_SetWindow: whoops, no data to create window. SetWindow called in funny order?", "wxPlugin", MB_OK);
214 return NPERR_NO_ERROR;
215 }
216
217 if ( window->window == NULL )
218 {
219 // We're receiving a NULL window before we've even received
220 // a valid window. Ignore this silly thing.
221 return NPERR_NO_ERROR;
222 }
223
224 m_data.m_window = window;
225 m_data.m_instance = instance;
226
227 // wxPluginFrame *frame = OnNewInstance(m_data);
228
229 m_data.m_instance = NULL;
230 m_data.m_window = NULL;
231 delete[] m_data.m_argv;
232 delete[] m_data.m_argn;
233 m_data.m_argv = NULL;
234 m_data.m_argn = NULL;
235 #endif
236 }
237 return NPERR_NO_ERROR;
238 }
239
240 void wxPluginApp::NPP_Shutdown(void)
241 {
242 // Clean up wxWindows
243 CleanUp();
244 }
245
246 void wxPluginApp::NPP_StreamAsFile(NPP instance, NPStream* stream, const char *fname)
247 {
248 wxPluginFrame *frame = FindFrame(instance);
249 if ( frame )
250 {
251 wxString str(fname);
252 frame->OnNPNewFile(stream, str);
253 }
254 }
255
256 /*
257 void wxPluginApp::NPP_URLNotify(NPP instance, const char* url, NPReason reason,
258 void* notifyData)
259 {
260 }
261 */
262
263 int32 wxPluginApp::NPP_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
264 void* buf)
265 {
266 return len; // The number of bytes accepted
267 }
268
269 static int32 STREAMBUFSIZE = 0X0FFFFFFF; // If we are reading from a file in NPAsFile
270 // mode so we can take any size stream in our
271 // write call (since we ignore it)
272
273 int32 wxPluginApp::NPP_WriteReady(NPP instance, NPStream* stream)
274 {
275 return STREAMBUFSIZE; // Number of bytes ready to accept in NPP_Write()
276 }
277