]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/applet/appletwindow.cpp
Fix cvs conflict.
[wxWidgets.git] / contrib / src / applet / appletwindow.cpp
1 /****************************************************************************
2 *
3 * wxWindows HTML Applet Package
4 *
5 * Copyright (C) 1991-2001 SciTech Software, Inc.
6 * All rights reserved.
7 *
8 * ======================================================================
9 * |REMOVAL OR MODIFICATION OF THIS HEADER IS STRICTLY PROHIBITED BY LAW|
10 * | |
11 * |This copyrighted computer code is a proprietary trade secret of |
12 * |SciTech Software, Inc., located at 505 Wall Street, Chico, CA 95928 |
13 * |USA (www.scitechsoft.com). ANY UNAUTHORIZED POSSESSION, USE, |
14 * |VIEWING, COPYING, MODIFICATION OR DISSEMINATION OF THIS CODE IS |
15 * |STRICTLY PROHIBITED BY LAW. Unless you have current, express |
16 * |written authorization from SciTech to possess or use this code, you |
17 * |may be subject to civil and/or criminal penalties. |
18 * | |
19 * |If you received this code in error or you would like to report |
20 * |improper use, please immediately contact SciTech Software, Inc. at |
21 * |530-894-8400. |
22 * | |
23 * |REMOVAL OR MODIFICATION OF THIS HEADER IS STRICTLY PROHIBITED BY LAW|
24 * ======================================================================
25 *
26 * Language: ANSI C++
27 * Environment: Any
28 *
29 * Description: Main wxHtmlAppletWindow class implementation
30 *
31 ****************************************************************************/
32
33 // For compilers that support precompilation
34 #include "wx/wxprec.h"
35
36 // Include private headers
37 #include "wx/applet/applet.h"
38
39 /*------------------------- Implementation --------------------------------*/
40
41 // Empty event handler. We include this event handler simply so that
42 // sub-classes of wxApplet can reference wxApplet in the event tables
43 // that they create as necessary.
44 BEGIN_EVENT_TABLE(wxHtmlAppletWindow, wxHtmlWindow)
45 END_EVENT_TABLE()
46
47 // Implement the class functions for wxHtmlAppletWindow
48 IMPLEMENT_CLASS(wxHtmlAppletWindow, wxHtmlWindow);
49
50 // Define the wxAppletList implementation
51 #include "wx/listimpl.cpp"
52 WX_DEFINE_LIST(wxAppletList);
53
54 /****************************************************************************
55 REMARKS:
56 Constructor for the applet window class.
57 ****************************************************************************/
58 wxHtmlAppletWindow::wxHtmlAppletWindow(
59 wxWindow *parent,
60 wxWindowID id,
61 const wxPoint& pos,
62 const wxSize& size,
63 long style,
64 const wxString& name)
65 : wxHtmlWindow(parent,id,pos,size,style,name)
66 {
67 // Ensure all cookie data is destroyed when window is killed
68 m_Cookies.DeleteContents(true);
69 }
70
71 /****************************************************************************
72 REMARKS:
73 Destructor for the applet window class.
74 ****************************************************************************/
75 wxHtmlAppletWindow::~wxHtmlAppletWindow()
76 {
77 }
78
79 /****************************************************************************
80 PARAMETERS:
81 className - Name of the applet class to create an object for
82 size - Initial size of the applet to be created
83
84 RETURNS:
85 Pointer to the wxApplet created, or NULL if unable to create the applet.
86
87 REMARKS:
88 This function is used to create new wxApplet objects dynamically based on the
89 class name as a string. This allows instances of wxApplet classes to be
90 created dynamically based on string values embedded in the custom tags of an
91 HTML page.
92 ****************************************************************************/
93 wxApplet *wxHtmlAppletWindow::CreateApplet(
94 const wxString& className,
95 const wxSize& size)
96 {
97 // We presently only allow one applet per page of the same class!
98 if (m_AppletList.Find(className))
99 return NULL;
100
101 // Dynamically create the class instance at runtime
102 wxClassInfo *info = wxClassInfo::FindClass(className.c_str());
103 if (!info)
104 return NULL;
105 wxObject *obj = info->CreateObject();
106 if (!obj)
107 return NULL;
108 wxApplet *applet = wxDynamicCast(obj,wxApplet);
109 if (!applet)
110 return NULL;
111 if (!applet->Create(this,size)) {
112 delete applet;
113 return NULL;
114 }
115 m_AppletList.Append(className,applet);
116 return applet;
117 }
118
119 /****************************************************************************
120 PARAMETERS:
121 appletName - Name of the applet class to find
122
123 RETURNS:
124 Pointer to the wxApplet found, or NULL if not found.
125
126 REMARKS:
127 Find an instance of an applet based on it's name
128 ****************************************************************************/
129 wxApplet *wxHtmlAppletWindow::FindApplet(
130 const wxString& appletName)
131 {
132 wxAppletList::Node *node = m_AppletList.Find(appletName);
133 if (!node)
134 return NULL;
135 return node->GetData();
136 }
137
138 /****************************************************************************
139 PARAMETERS:
140 applet - Pointer to the applet object to remove from the list
141
142 RETURNS:
143 True if the applet was found and removed, false if not. The applet itself
144 is *not* destroyed!
145
146 REMARKS:
147 Remove an applet from the manager. Called during applet destruction
148 ****************************************************************************/
149 bool wxHtmlAppletWindow::RemoveApplet(
150 const wxApplet *applet)
151 {
152 for (wxAppletList::Node *node = m_AppletList.GetFirst(); node; node = node->GetNext()) {
153 if (node->GetData() == applet) {
154 m_AppletList.DeleteNode(node);
155 return true;
156 }
157 }
158 return false;
159 }
160
161 /****************************************************************************
162 PARAMETERS:
163 URL - New URL for the page to load
164
165 RETURNS:
166 True if page loaded successfully, false if not
167
168 REMARKS:
169 Remove an applet from the manager. Called during applet destruction
170 ****************************************************************************/
171 bool wxHtmlAppletWindow::LoadPage(
172 const wxString& hRef)
173 {
174 for (wxAppletList::Node *node = m_AppletList.GetFirst(); node; node = node->GetNext())
175 (node->GetData())->OnLinkClicked(hRef);
176 return wxHtmlWindow::LoadPage(hRef);
177 }
178
179 /****************************************************************************
180 PARAMETERS:
181 URL - String URL that we are navigating to
182
183 REMARKS:
184 Called when the user navigates to a new URL from the current page. We simply
185 call the LoadPage function above to load the new page and display it.
186 ****************************************************************************/
187 void wxHtmlAppletWindow::OnLinkClicked(
188 const wxHtmlLinkInfo& link)
189 {
190 for (wxAppletList::Node *node = m_AppletList.GetFirst(); node; node = node->GetNext())
191 (node->GetData())->OnLinkClicked(link);
192 wxHtmlWindow::LoadPage(link.GetHref());
193 }
194
195 /****************************************************************************
196 REMARKS:
197 Called when the user navigates forward within the HTML history stack.
198 We call all the applets in turn allowing them to handle the navigation
199 command prior to being destructed when the current page is destroyed.
200 ****************************************************************************/
201 bool wxHtmlAppletWindow::HistoryForward()
202 {
203 if (!HistoryCanForward())
204 return false;
205 for (wxAppletList::Node *node = m_AppletList.GetFirst(); node; node = node->GetNext())
206 (node->GetData())->OnHistoryForward();
207 return wxHtmlWindow::HistoryForward();
208 }
209
210 /****************************************************************************
211 REMARKS:
212 Called when the user navigates backwards within the HTML history stack.
213 We call all the applets in turn allowing them to handle the navigation
214 command prior to being destructed when the current page is destroyed.
215 ****************************************************************************/
216 bool wxHtmlAppletWindow::HistoryBack()
217 {
218 if (!HistoryCanBack())
219 return false;
220 for (wxAppletList::Node *node = m_AppletList.GetFirst(); node; node = node->GetNext())
221 (node->GetData())->OnHistoryBack();
222 return wxHtmlWindow::HistoryBack();
223 }
224
225 /****************************************************************************
226 PARAMETERS:
227 msg - wxEvent message to be sent to all wxApplets
228
229 REMARKS:
230 This function is called by the wxApplet's when they need to send a message
231 to all other applets on the current page. This is the primary form of
232 communication between applets on the page if they need to inform each
233 other of internal information.
234
235 Note that the event handling terminates as soon as the first wxApplet
236 handles the event. If the event should be handled by all wxApplet's,
237 the event handlers for the applets should not reset the wxEvent::Skip()
238 value (ie: by default it is true).
239 ****************************************************************************/
240 void wxHtmlAppletWindow::SendMessage(
241 wxEvent& msg)
242 {
243 // Preset the skip flag
244 msg.Skip();
245
246 // Process all applets in turn and send them the message
247 for (wxAppletList::Node *node = m_AppletList.GetFirst(); node; node = node->GetNext()) {
248 (node->GetData())->OnMessage(msg);
249 if (!msg.GetSkipped())
250 break;
251 }
252 }
253
254 /****************************************************************************
255 PARAMETERS:
256 msg - wxEvent message to be sent to all wxApplets
257
258 RETURNS:
259 True if new cookie was added, false if cookie with same name already exists.
260
261 REMARKS:
262 This function is called by the wxApplet's when they need register a cookie
263 of data in the applet window's cookie table. Cookies are arbitrary data
264 objects that are references by unique name's by the wxApplet. These
265 values can be used to store and retrieve data that needs to remain
266 persisent across invocations of the wxApplet. Ie: The first time an
267 applet is created it would use the cookie to store data to maintain
268 it's present state so that if you navigated back to the same page
269 is would be able to re-load the prior state as though the applet
270 was never actually destructed.
271
272 Note: If a cookie with the same name already exists, this function returns
273 false. Hence if you wish to replace a cookie you should first call
274 UnRegisterCookie to ensure the cookie is deleted and then call this
275 function.
276 ****************************************************************************/
277 bool wxHtmlAppletWindow::RegisterCookie(
278 const wxString& name,
279 wxObject *cookie)
280 {
281 // Fail if the named cookie already exists!
282 if (m_Cookies.Get(name))
283 return false;
284 m_Cookies.Put(name,cookie);
285 return true;
286 }
287
288 /****************************************************************************
289 PARAMETERS:
290 msg - wxEvent message to be sent to all wxApplets
291
292 RETURNS:
293 True if found and deleted, false if not found in table.
294
295 REMARKS:
296 This function is called by the wxApplet's when they need de-register a
297 cookie of data in the applet window's cookie table. The data in the
298 cookie itself is also deleted before it is removed from the table.
299 ****************************************************************************/
300 bool wxHtmlAppletWindow::UnRegisterCookie(
301 const wxString& name)
302 {
303 wxObject *data = m_Cookies.Delete(name);
304 if (data) {
305 delete data;
306 return true;
307 }
308 return false;
309 }
310
311 /****************************************************************************
312 PARAMETERS:
313 msg - wxEvent message to be sent to all wxApplets
314
315 RETURNS:
316 Pointer to the cookie data if found, NULL if not found.
317
318 REMARKS:
319 This function is called by the wxApplet's when they need to find a cookie
320 of data given it's public name. If the cookie is not found, NULL is
321 returned.
322 ****************************************************************************/
323 wxObject *wxHtmlAppletWindow::FindCookie(
324 const wxString& name)
325 {
326 return m_Cookies.Get(name);
327 }
328
329 #include "wx/html/m_templ.h"
330
331 /****************************************************************************
332 REMARKS:
333 Implementation for the <embed> HTML tag handler. This handler takes care
334 of automatically constructing the wxApplet objects of the appropriate
335 class based on the <embed> tag information.
336 ****************************************************************************/
337 TAG_HANDLER_BEGIN(Embed, "EMBED")
338
339 TAG_HANDLER_PROC(tag)
340 {
341 wxWindow *wnd;
342 wxHtmlAppletWindow *appletWindow;
343 wxApplet *applet;
344 int width, height;
345 int floatPercent = 0;
346
347 wnd = m_WParser->GetWindow();
348 if ((appletWindow = wxDynamicCast(wnd,wxHtmlAppletWindow)) != NULL) {
349 tag.ScanParam("WIDTH", "%i", &width);
350 tag.ScanParam("HEIGHT", "%i", &height);
351 if (tag.HasParam("FLOAT"))
352 tag.ScanParam("FLOAT", "%i", &floatPercent);
353 if (tag.HasParam("APPLET")) {
354 if ((applet = appletWindow->CreateApplet(tag.GetParam("APPLET"), wxSize(width, height))) != NULL) {
355 applet->Show(true);
356 m_WParser->OpenContainer()->InsertCell(new wxHtmlWidgetCell(applet,floatPercent));
357 }
358 }
359 else if (tag.HasParam("TEXT")) {
360 // TODO: Somehow get the text returned from this class displayed on the page!
361 }
362 }
363 return false;
364 }
365
366 TAG_HANDLER_END(Embed)
367
368 TAGS_MODULE_BEGIN(Embed)
369 TAGS_MODULE_ADD(Embed)
370 TAGS_MODULE_END(Embed)
371