]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/applet/appletwindow.cpp
1 /****************************************************************************
3 * wxWindows HTML Applet Package
5 * Copyright (C) 1991-2001 SciTech Software, Inc.
8 * ========================================================================
10 * The contents of this file are subject to the wxWindows License
11 * Version 3.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.wxwindows.org/licence3.txt
15 * Software distributed under the License is distributed on an
16 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17 * implied. See the License for the specific language governing
18 * rights and limitations under the License.
20 * ========================================================================
25 * Description: Main wxHtmlAppletWindow class implementation
27 ****************************************************************************/
29 // For compilers that support precompilation
30 #include "wx/wxprec.h"
31 #include "wx/html/forcelnk.h"
33 // Include private headers
34 #include "wx/applet/applet.h"
35 #include "wx/applet/window.h"
36 #include "wx/applet/loadpage.h"
39 #include "wx/applet/prepinclude.h"
40 #include "wx/applet/prepecho.h"
41 #include "wx/applet/prepifelse.h"
44 /*---------------------------- Global variables ---------------------------*/
46 wxHashTable
wxHtmlAppletWindow::m_Cookies
;
48 /*------------------------- Implementation --------------------------------*/
50 // Empty event handler. We include this event handler simply so that
51 // sub-classes of wxApplet can reference wxApplet in the event tables
52 // that they create as necessary.
53 BEGIN_EVENT_TABLE(wxHtmlAppletWindow
, wxHtmlWindow
)
54 EVT_LOAD_PAGE(wxHtmlAppletWindow::OnLoadPage
)
55 EVT_PAGE_LOADED(wxHtmlAppletWindow::OnPageLoaded
)
58 // Implement the class functions for wxHtmlAppletWindow
59 IMPLEMENT_CLASS(wxHtmlAppletWindow
, wxHtmlWindow
);
61 // Define the wxAppletList implementation
62 #include "wx/listimpl.cpp"
63 WX_DEFINE_LIST(wxAppletList
);
65 /****************************************************************************
67 Constructor for the applet window class.
68 ****************************************************************************/
69 wxHtmlAppletWindow::wxHtmlAppletWindow(
72 wxToolBarBase
*navBar
,
79 : wxHtmlWindow(parent
,id
,pos
,size
,style
,name
)
84 // setup client navbars
87 m_NavBackId
= navBackId
;
88 m_NavForwardId
= navForwardId
;
94 // Add HTML preprocessors
95 // deleting preprocessors is done by the code within the window
97 incPreprocessor
= new wxIncludePrep(); // #include preprocessor
98 wxEchoPrep
* echoPreprocessor
= new wxEchoPrep(); // #echo preprocessor
99 wxIfElsePrep
* ifPreprocessor
= new wxIfElsePrep();
101 this->AddProcessor(incPreprocessor
);
102 this->AddProcessor(echoPreprocessor
);
103 this->AddProcessor(ifPreprocessor
);
106 /****************************************************************************
108 Destructor for the applet window class.
109 ****************************************************************************/
110 wxHtmlAppletWindow::~wxHtmlAppletWindow()
114 /****************************************************************************
116 className - Name of the applet class to create an object for
117 size - Initial size of the applet to be created
120 Pointer to the wxApplet created, or NULL if unable to create the applet.
123 This function is used to create new wxApplet objects dynamically based on the
124 class name as a string. This allows instances of wxApplet classes to be
125 created dynamically based on string values embedded in the custom tags of an
127 ****************************************************************************/
128 wxApplet
*wxHtmlAppletWindow::CreateApplet(
129 const wxString
& classId
,
130 const wxString
& iName
,
131 const wxHtmlTag
& params
,
134 // Dynamically create the class instance at runtime
135 wxClassInfo
*info
= wxClassInfo::FindClass(classId
.c_str());
138 wxObject
*obj
= info
->CreateObject();
141 wxApplet
*applet
= wxDynamicCast(obj
,wxApplet
);
144 if (!applet
->Create(this,params
,size
)) {
148 m_AppletList
.Append(iName
,applet
);
152 /****************************************************************************
154 appletName - Name of the applet class to find
157 Pointer to the wxApplet found, or NULL if not found.
160 Find an instance of an applet based on it's name
161 ****************************************************************************/
162 wxApplet
*wxHtmlAppletWindow::FindApplet(
163 const wxString
& appletName
)
165 wxAppletList::Node
*node
= m_AppletList
.Find(appletName
);
168 return node
->GetData();
171 /****************************************************************************
173 applet - Pointer to the applet object to remove from the list
176 True if the applet was found and removed, false if not. The applet itself
180 Remove an applet from the manager. Called during applet destruction
181 ****************************************************************************/
182 bool wxHtmlAppletWindow::RemoveApplet(
183 const wxApplet
*applet
)
185 for (wxAppletList::Node
*node
= m_AppletList
.GetFirst(); node
; node
= node
->GetNext()) {
186 if (node
->GetData() == applet
) {
187 m_AppletList
.DeleteNode(node
);
194 /****************************************************************************
196 URL - New URL for the page to load
199 True if page loaded successfully, false if not
202 Remove an applet from the manager. Called during applet destruction
203 ****************************************************************************/
204 bool wxHtmlAppletWindow::LoadPage(
205 const wxString
& link
)
209 // TODO: This needs to be made platform inde if possible.
210 if (link
.GetChar(0) == '?'){
211 wxString cmd
= link
.BeforeFirst('=');
212 wxString cmdValue
= link
.AfterFirst('=');
214 if(!(cmd
.CmpNoCase("?EXTERNAL"))){
216 ShellExecute(this ? (HWND
)this->GetHWND() : NULL
,NULL
,cmdValue
.c_str(),NULL
,"",SW_SHOWNORMAL
);
218 #error Platform not implemented yet!
222 if (!(cmd
.CmpNoCase("?EXECUTE"))){
223 wxMessageBox(cmdValue
);
226 if (!(cmd
.CmpNoCase("?VIRTUAL"))){
227 VirtualData
& temp
= *((VirtualData
*)FindCookie(cmdValue
));
229 href
= temp
.GetHref();
233 wxMessageBox("VIRTUAL LINK ERROR: " + cmdValue
+ " does not exist.");
240 // Make a copy of the current path the translate for <!-- include files from what will be the new path
241 // we cannot just grab this value from the base class since it is not correct until after LoadPage is
242 // called. And we need the incPreprocessor to know the right path before LoadPage.
244 fs
.ChangePathTo(m_FS
->GetPath(), true);
245 fs
.ChangePathTo(link
);
246 incPreprocessor
->ChangeDirectory(fs
.GetPath());
248 // Inform all the applets that the new page is being loaded
249 for (wxAppletList::Node
*node
= m_AppletList
.GetFirst(); node
; node
= node
->GetNext())
250 (node
->GetData())->OnLinkClicked(wxHtmlLinkInfo(href
));
251 bool stat
= wxHtmlWindow::LoadPage(href
);
255 // Enable/Dis the navbar tools
257 m_NavBar
->EnableTool(m_NavForwardId
,HistoryCanForward());
258 m_NavBar
->EnableTool(m_NavBackId
,HistoryCanBack());
263 /****************************************************************************
265 URL - String URL that we are navigating to
268 Called when the user navigates to a new URL from the current page. We simply
269 call the LoadPage function above to load the new page and display it.
270 ****************************************************************************/
271 void wxHtmlAppletWindow::OnLinkClicked(
272 const wxHtmlLinkInfo
& link
)
274 LoadPage(link
.GetHref());
277 /****************************************************************************
279 Called when the user navigates forward within the HTML history stack.
280 We call all the applets in turn allowing them to handle the navigation
281 command prior to being destructed when the current page is destroyed.
282 ****************************************************************************/
283 bool wxHtmlAppletWindow::HistoryForward()
285 if (!HistoryCanForward())
288 for (wxAppletList::Node
*node
= m_AppletList
.GetFirst(); node
; node
= node
->GetNext())
289 (node
->GetData())->OnHistoryForward();
291 return wxHtmlWindow::HistoryForward();
294 /****************************************************************************
296 Called when the user navigates backwards within the HTML history stack.
297 We call all the applets in turn allowing them to handle the navigation
298 command prior to being destructed when the current page is destroyed.
299 ****************************************************************************/
300 bool wxHtmlAppletWindow::HistoryBack()
302 if (!HistoryCanBack())
305 for (wxAppletList::Node
*node
= m_AppletList
.GetFirst(); node
; node
= node
->GetNext())
306 (node
->GetData())->OnHistoryBack();
308 return wxHtmlWindow::HistoryBack();
311 /****************************************************************************
313 msg - wxEvent message to be sent to all wxApplets
316 This function is called by the wxApplet's when they need to send a message
317 to all other applets on the current page. This is the primary form of
318 communication between applets on the page if they need to inform each
319 other of internal information.
321 Note that the event handling terminates as soon as the first wxApplet
322 handles the event. If the event should be handled by all wxApplet's,
323 the event handlers for the applets should not reset the wxEvent::Skip()
324 value (ie: by default it is true).
325 ****************************************************************************/
326 void wxHtmlAppletWindow::SendMessage(
329 // Preset the skip flag
332 // Process all applets in turn and send them the message
333 for (wxAppletList::Node
*node
= m_AppletList
.GetFirst(); node
; node
= node
->GetNext()) {
334 (node
->GetData())->OnMessage(msg
);
335 if (!msg
.GetSkipped()){
336 wxMessageBox("BREAK");
342 /****************************************************************************
344 name - Uniq wxString used as hash key
345 cookie - wxObject data returned when name is found.
348 True if new cookie was added, false if cookie with same name already exists.
351 This function is called by the wxApplet's when they need register a cookie
352 of data in the applet window's cookie table. Cookies are arbitrary data
353 objects that are references by unique name's by the wxApplet. These
354 values can be used to store and retrieve data that needs to remain
355 persisent across invocations of the wxApplet. Ie: The first time an
356 applet is created it would use the cookie to store data to maintain
357 it's present state so that if you navigated back to the same page
358 is would be able to re-load the prior state as though the applet
359 was never actually destructed.
361 Note: If a cookie with the same name already exists, this function returns
362 false. Hence if you wish to replace a cookie you should first call
363 UnRegisterCookie to ensure the cookie is deleted and then call this
365 ****************************************************************************/
366 bool wxHtmlAppletWindow::RegisterCookie(
367 const wxString
& name
,
370 // Fail if the named cookie already exists!
371 if (m_Cookies
.Get(name
))
373 m_Cookies
.Put(name
,cookie
);
377 /****************************************************************************
379 name - wxString uniq haskey used to remove item from hash
382 True if found and deleted, false if not found in table.
385 This function is called by the wxApplet's when they need de-register a
386 cookie of data in the applet window's cookie table. The data in the
387 cookie itself is also deleted before it is removed from the table.
388 ****************************************************************************/
389 bool wxHtmlAppletWindow::UnRegisterCookie(
390 const wxString
& name
)
392 wxObject
*data
= m_Cookies
.Delete(name
);
400 /****************************************************************************
402 msg - wxEvent message to be sent to all wxApplets
405 Pointer to the cookie data if found, NULL if not found.
408 This function is called by the wxApplet's when they need to find a cookie
409 of data given it's public name. If the cookie is not found, NULL is
411 ****************************************************************************/
412 wxObject
*wxHtmlAppletWindow::FindCookie(
413 const wxString
& name
)
415 return m_Cookies
.Get(name
);
418 /****************************************************************************
420 event - Event to handle
423 This function handles delayed LoadPage events posted from applets that
424 need to change the page for the current window to a new window.
425 ****************************************************************************/
426 void wxHtmlAppletWindow::OnLoadPage(
427 wxLoadPageEvent
&event
)
429 // Test the mutex lock. We have to do this here because wxHTML constantly
430 // calls wxYield which processes the message queue. This in turns means
431 // that we may end up processing a new 'loadPage' event while the new
432 // page is still loading! We need to avoid this so we use a simple
433 // lock to avoid loading a page while presently loading another page.
435 if (event
.GetHtmlWindow() == this){
436 if (LoadPage(event
.GetHRef())){
437 // wxPageLoadedEvent evt;
438 // NOTE: This is reserved for later use as we might need to send
439 // page loaded events to applets.
446 /****************************************************************************
448 event - Event to handle
451 This function handles delayed LoadPage events posted from applets that
452 need to change the page for the current window to a new window.
453 ****************************************************************************/
454 void wxHtmlAppletWindow::OnPageLoaded(
460 /****************************************************************************
465 This function tries to lock the mutex. If it can't, returns
466 immediately with false.
467 ***************************************************************************/
468 bool wxHtmlAppletWindow::TryLock()
477 /****************************************************************************
479 name - name of the last applet that changed the data in this object
480 group - name of the group the allplet belongs to.
481 href - webpage to go to.
484 VirtualData is used to store information on the virtual links.
485 ****************************************************************************/
486 VirtualData::VirtualData(
496 #include "wx/html/m_templ.h"
498 /****************************************************************************
500 Implementation for the <embed> HTML tag handler. This handler takes care
501 of automatically constructing the wxApplet objects of the appropriate
502 class based on the <embed> tag information.
503 ****************************************************************************/
504 TAG_HANDLER_BEGIN(wxApplet
, "WXAPPLET")
506 TAG_HANDLER_PROC(tag
)
509 wxHtmlAppletWindow
*appletWindow
;
515 wnd
= m_WParser
->GetWindow();
517 if ((appletWindow
= wxDynamicCast(wnd
,wxHtmlAppletWindow
)) != NULL
){
518 tag
.ScanParam("WIDTH", "%i", &width
);
519 tag
.ScanParam("HEIGHT", "%i", &height
);
520 if (tag
.HasParam("CLASSID")){
521 classId
= tag
.GetParam("CLASSID");
522 if ( classId
.IsNull() || classId
.Len() == 0 ){
523 wxMessageBox("wxApplet tag error: CLASSID is NULL or empty.","Error",wxICON_ERROR
);
526 if (tag
.HasParam("NAME"))
527 name
= tag
.GetParam("NAME");
529 // If the name is NULL or len is zero then we assume that the html guy
530 // didn't include the name param which is optional.
531 if ( name
.IsNull() || name
.Len() == 0 )
534 // We got all the params and can now create the applet
535 if ((applet
= appletWindow
->CreateApplet(classId
, name
, tag
, wxSize(width
, height
))) != NULL
){
537 m_WParser
->OpenContainer()->InsertCell(new wxHtmlWidgetCell(applet
,0));
540 wxMessageBox("wxApplet error: Could not create:" + classId
+ "," + name
);
543 wxMessageBox("wxApplet tag error: Can not find CLASSID param.","Error",wxICON_ERROR
);
546 //Add more param parsing here. If or when spec changes.
547 //For now we'll ignore any other params those HTML guys
548 //might put in our tag.
554 TAG_HANDLER_END(wxApplet
)
556 TAGS_MODULE_BEGIN(wxApplet
)
557 TAGS_MODULE_ADD(wxApplet
)
558 TAGS_MODULE_END(wxApplet
)
560 // This is our little forcelink hack.