define USE_QA so that qa library is built
[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->Close();
107 }
108 return NPERR_NO_ERROR;
109 }
110
111 NPError wxPluginApp::NPP_DestroyStream(NPP instance, NPStream* stream, NPError reason)
112 {
113 return NPERR_NO_ERROR;
114 }
115
116 /*
117 jref wxPluginApp::NPP_GetJavaClass(void)
118 {
119 return 0;
120 }
121 */
122
123 // This can't work now because we don't have a global app until wxEntry is called.
124 // We'll put the wxEntry call in npshell.cpp instead.
125 NPError wxPluginApp::NPP_Initialize(void)
126 {
127 static int init = FALSE;
128
129 if ( init == TRUE )
130 MessageBox(NULL, "wxPluginApp::NPP_Initialize:\nabout to call wxEntry for 2nd time!!!", "wxPlugin", MB_OK);
131
132 wxEntry((WXHINSTANCE) GetModuleHandle(NULL));
133
134 init = TRUE;
135
136 // MessageBox(NULL, "wxPluginApp::NPP_Initialize: have called wxEntry", "wxPlugin", MB_OK);
137 return NPERR_NO_ERROR;
138 }
139
140 NPError wxPluginApp::NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode,
141 int16 argc, char* argn[], char* argv[], NPSavedData* saved)
142 {
143 // MessageBox(NULL, "wxPluginApp::NPP_New", "wxPlugin", MB_OK);
144
145 // Save values so frame can be created in first NPP_SetWindow
146 if ( m_data.m_instance != 0 )
147 {
148 MessageBox(NULL, "wxPluginApp::NPP_New: whoops, 2 NPP_New calls in succession without NPP_SetWindow.\n Need to modify my code!", "wxPlugin", MB_OK);
149 return NPERR_NO_ERROR;
150 }
151
152 m_data.m_instance = instance;
153 m_data.m_type = pluginType;
154 m_data.m_mode = mode;
155
156 SetAttributeValues(argc, argn, argv);
157
158 // Unfortunately, we may get a stream event before we've got a valid window
159 // handle, so we just have to go ahead and create a new instance.
160 wxPluginFrame *frame = OnNewInstance(m_data);
161
162 m_data.m_instance = NULL;
163 m_data.m_window = NULL;
164 delete[] m_data.m_argv;
165 delete[] m_data.m_argn;
166 m_data.m_argv = NULL;
167 m_data.m_argn = NULL;
168
169 return NPERR_NO_ERROR;
170 }
171
172 NPError wxPluginApp::NPP_NewStream(NPP instance, NPMIMEType type, NPStream *stream,
173 NPBool seekable, uint16* stype)
174 {
175 // By default, we want to receive a file instead of a stream.
176 wxPluginFrame *frame = FindFrame(instance);
177 if ( frame )
178 {
179 return frame->OnNPNewStream(type, stream, seekable, stype);
180 }
181 return NPERR_NO_ERROR;
182 }
183
184 void wxPluginApp::NPP_Print(NPP instance, NPPrint* printInfo)
185 {
186 if (instance == NULL)
187 return;
188
189 wxPluginFrame *frame = FindFrame(instance);
190 if ( frame )
191 {
192 frame->OnNPPrint(printInfo);
193 }
194 }
195
196 NPError wxPluginApp::NPP_SetWindow(NPP instance, NPWindow* window)
197 {
198 // MessageBox(NULL, "wxPluginApp::NPP_SetWindow", "wxPlugin", MB_OK);
199
200 if ( window )
201 wxDebugMsg("%d\n", (int) window->window);
202
203 wxPluginFrame *frame = FindFrame(instance);
204 if ( frame )
205 {
206 frame->SetNPWindow(window);
207 }
208 else
209 {
210 #if 0
211 // No such frame: must make it.
212 if ( m_data.m_instance == NULL )
213 {
214 MessageBox(NULL, "wxPluginApp::NPP_SetWindow: whoops, no data to create window. SetWindow called in funny order?", "wxPlugin", MB_OK);
215 return NPERR_NO_ERROR;
216 }
217
218 if ( window->window == NULL )
219 {
220 // We're receiving a NULL window before we've even received
221 // a valid window. Ignore this silly thing.
222 return NPERR_NO_ERROR;
223 }
224
225 m_data.m_window = window;
226 m_data.m_instance = instance;
227
228 // wxPluginFrame *frame = OnNewInstance(m_data);
229
230 m_data.m_instance = NULL;
231 m_data.m_window = NULL;
232 delete[] m_data.m_argv;
233 delete[] m_data.m_argn;
234 m_data.m_argv = NULL;
235 m_data.m_argn = NULL;
236 #endif
237 }
238 return NPERR_NO_ERROR;
239 }
240
241 void wxPluginApp::NPP_Shutdown(void)
242 {
243 // Clean up wxWidgets
244 CleanUp();
245 }
246
247 void wxPluginApp::NPP_StreamAsFile(NPP instance, NPStream* stream, const char *fname)
248 {
249 wxPluginFrame *frame = FindFrame(instance);
250 if ( frame )
251 {
252 wxString str(fname);
253 frame->OnNPNewFile(stream, str);
254 }
255 }
256
257 /*
258 void wxPluginApp::NPP_URLNotify(NPP instance, const char* url, NPReason reason,
259 void* notifyData)
260 {
261 }
262 */
263
264 int32 wxPluginApp::NPP_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
265 void* buf)
266 {
267 return len; // The number of bytes accepted
268 }
269
270 static int32 STREAMBUFSIZE = 0X0FFFFFFF; // If we are reading from a file in NPAsFile
271 // mode so we can take any size stream in our
272 // write call (since we ignore it)
273
274 int32 wxPluginApp::NPP_WriteReady(NPP instance, NPStream* stream)
275 {
276 return STREAMBUFSIZE; // Number of bytes ready to accept in NPP_Write()
277 }
278