]>
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 * ======================================================================
9 * |REMOVAL OR MODIFICATION OF THIS HEADER IS STRICTLY PROHIBITED BY LAW|
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. |
19 * |If you received this code in error or you would like to report |
20 * |improper use, please immediately contact SciTech Software, Inc. at |
23 * |REMOVAL OR MODIFICATION OF THIS HEADER IS STRICTLY PROHIBITED BY LAW|
24 * ======================================================================
29 * Description: Main wxHtmlAppletWindow class implementation
31 ****************************************************************************/
33 // For compilers that support precompilation
34 #include "wx/wxprec.h"
36 #include "wx/process.h"
37 #include "wx/spawnbrowser.h"
38 #include "wx/html/forcelnk.h"
40 // Include private headers
41 #include "wx/applet/applet.h"
42 #include "wx/applet/window.h"
43 #include "wx/applet/loadpage.h"
46 #include "wx/applet/prepinclude.h"
47 #include "wx/applet/prepecho.h"
48 #include "wx/applet/prepifelse.h"
50 /*---------------------------- Global variables ---------------------------*/
52 wxHashTable
wxHtmlAppletWindow::m_Cookies
;
54 /*------------------------- Implementation --------------------------------*/
56 // Empty event handler. We include this event handler simply so that
57 // sub-classes of wxApplet can reference wxApplet in the event tables
58 // that they create as necessary.
59 BEGIN_EVENT_TABLE(wxHtmlAppletWindow
, wxHtmlWindow
)
60 EVT_LOAD_PAGE(wxHtmlAppletWindow::OnLoadPage
)
61 EVT_PAGE_LOADED(wxHtmlAppletWindow::OnPageLoaded
)
64 // Implement the class functions for wxHtmlAppletWindow
65 IMPLEMENT_CLASS(wxHtmlAppletWindow
, wxHtmlWindow
);
67 // Define the wxAppletList implementation
68 #include "wx/listimpl.cpp"
69 WX_DEFINE_LIST(wxAppletList
);
71 /****************************************************************************
73 Constructor for the applet window class.
74 ****************************************************************************/
75 wxHtmlAppletWindow::wxHtmlAppletWindow(
78 wxToolBarBase
*navBar
,
85 const wxString
& docroot
)
86 : wxHtmlWindow(parent
,id
,pos
,size
,style
,name
)
91 // setup client navbars
94 m_NavBackId
= navBackId
;
95 m_NavForwardId
= navForwardId
;
104 // Add HTML preprocessors
105 // deleting preprocessors is done by the code within the window
107 incPreprocessor
= new wxIncludePrep(); // #include preprocessor
108 incPreprocessor
->ChangeDirectory(m_DocRoot
);
110 wxEchoPrep
* echoPreprocessor
= new wxEchoPrep(); // #echo preprocessor
111 wxIfElsePrep
* ifPreprocessor
= new wxIfElsePrep();
113 this->AddProcessor(incPreprocessor
);
114 this->AddProcessor(echoPreprocessor
);
115 this->AddProcessor(ifPreprocessor
);
118 /****************************************************************************
120 Destructor for the applet window class.
121 ****************************************************************************/
122 wxHtmlAppletWindow::~wxHtmlAppletWindow()
126 /****************************************************************************
128 className - Name of the applet class to create an object for
129 size - Initial size of the applet to be created
132 Pointer to the wxApplet created, or NULL if unable to create the applet.
135 This function is used to create new wxApplet objects dynamically based on the
136 class name as a string. This allows instances of wxApplet classes to be
137 created dynamically based on string values embedded in the custom tags of an
139 ****************************************************************************/
140 wxApplet
*wxHtmlAppletWindow::CreateApplet(
141 const wxString
& classId
,
142 const wxString
& iName
,
143 const wxHtmlTag
& params
,
146 // Dynamically create the class instance at runtime
147 wxClassInfo
*info
= wxClassInfo::FindClass(classId
.c_str());
150 wxObject
*obj
= info
->CreateObject();
153 wxApplet
*applet
= wxDynamicCast(obj
,wxApplet
);
156 if (!applet
->Create(this,params
,size
)) {
160 m_AppletList
.Append(iName
,applet
);
164 /****************************************************************************
166 appletName - Name of the applet class to find
169 Pointer to the wxApplet found, or NULL if not found.
172 Find an instance of an applet based on it's name
173 ****************************************************************************/
174 wxApplet
*wxHtmlAppletWindow::FindApplet(
175 const wxString
& appletName
)
177 wxAppletList::Node
*node
= m_AppletList
.Find(appletName
);
180 return node
->GetData();
183 /****************************************************************************
185 applet - Pointer to the applet object to remove from the list
188 True if the applet was found and removed, false if not. The applet itself
192 Remove an applet from the manager. Called during applet destruction
193 ****************************************************************************/
194 bool wxHtmlAppletWindow::RemoveApplet(
195 const wxApplet
*applet
)
197 for (wxAppletList::Node
*node
= m_AppletList
.GetFirst(); node
; node
= node
->GetNext()) {
198 if (node
->GetData() == applet
) {
199 m_AppletList
.DeleteNode(node
);
206 /****************************************************************************
208 URL - New URL for the page to load
211 True if page loaded successfully, false if not
214 Remove an applet from the manager. Called during applet destruction
215 ****************************************************************************/
216 bool wxHtmlAppletWindow::LoadPage(
217 const wxString
& link
)
221 // Check for abs path. If it is not then tack on the path
222 // supplied at creation.
223 if (!wxIsAbsolutePath(href
))
224 href
= m_DocRoot
+ href
;
226 // TODO: This needs to be made platform inde if possible.
227 if (link
.GetChar(0) == '?'){
228 wxString cmd
= link
.BeforeFirst('=');
229 wxString cmdValue
= link
.AfterFirst('=');
230 if(!(cmd
.CmpNoCase("?EXTERNAL"))){
231 return wxSpawnBrowser(this, cmdValue
.c_str());
233 if (!(cmd
.CmpNoCase("?EXECUTE"))){
234 wxProcess
*child
= new AppletProcess(this);
235 wxExecute(cmdValue
, false, child
);
238 if (!(cmd
.CmpNoCase("?VIRTUAL"))){
239 VirtualData
& temp
= *((VirtualData
*)FindCookie(cmdValue
));
241 href
= temp
.GetHref();
245 wxLogError(_T("VIRTUAL LINK ERROR: '%s' does not exist."), cmdValue
.c_str());
252 // Inform all the applets that the new page is being loaded
253 for (wxAppletList::Node
*node
= m_AppletList
.GetFirst(); node
; node
= node
->GetNext())
254 (node
->GetData())->OnLinkClicked(wxHtmlLinkInfo(href
));
256 bool stat
= wxHtmlWindow::LoadPage(href
);
259 // Enable/Dis the navbar tools
261 m_NavBar
->EnableTool(m_NavForwardId
,HistoryCanForward());
262 m_NavBar
->EnableTool(m_NavBackId
,HistoryCanBack());
267 /****************************************************************************
269 URL - String URL that we are navigating to
272 Called when the user navigates to a new URL from the current page. We simply
273 call the LoadPage function above to load the new page and display it.
274 ****************************************************************************/
275 void wxHtmlAppletWindow::OnLinkClicked(
276 const wxHtmlLinkInfo
& link
)
278 LoadPage(link
.GetHref());
281 /****************************************************************************
283 Called when the user navigates forward within the HTML history stack.
284 We call all the applets in turn allowing them to handle the navigation
285 command prior to being destructed when the current page is destroyed.
286 ****************************************************************************/
287 bool wxHtmlAppletWindow::HistoryForward()
289 if (!HistoryCanForward())
292 for (wxAppletList::Node
*node
= m_AppletList
.GetFirst(); node
; node
= node
->GetNext())
293 (node
->GetData())->OnHistoryForward();
295 return wxHtmlWindow::HistoryForward();
298 /****************************************************************************
300 Called when the user navigates backwards within the HTML history stack.
301 We call all the applets in turn allowing them to handle the navigation
302 command prior to being destructed when the current page is destroyed.
303 ****************************************************************************/
304 bool wxHtmlAppletWindow::HistoryBack()
306 if (!HistoryCanBack())
309 for (wxAppletList::Node
*node
= m_AppletList
.GetFirst(); node
; node
= node
->GetNext())
310 (node
->GetData())->OnHistoryBack();
312 return wxHtmlWindow::HistoryBack();
315 /****************************************************************************
317 msg - wxEvent message to be sent to all wxApplets
320 This function is called by the wxApplet's when they need to send a message
321 to all other applets on the current page. This is the primary form of
322 communication between applets on the page if they need to inform each
323 other of internal information.
325 Note that the event handling terminates as soon as the first wxApplet
326 handles the event. If the event should be handled by all wxApplet's,
327 the event handlers for the applets should not reset the wxEvent::Skip()
328 value (ie: by default it is true).
329 ****************************************************************************/
330 void wxHtmlAppletWindow::SendMessage(
333 // Preset the skip flag
336 // Process all applets in turn and send them the message
337 for (wxAppletList::Node
*node
= m_AppletList
.GetFirst(); node
; node
= node
->GetNext()) {
338 (node
->GetData())->OnMessage(msg
);
339 if (!msg
.GetSkipped()){
340 wxMessageBox("BREAK");
346 /****************************************************************************
348 name - Uniq wxString used as hash key
349 cookie - wxObject data returned when name is found.
352 True if new cookie was added, false if cookie with same name already exists.
355 This function is called by the wxApplet's when they need register a cookie
356 of data in the applet window's cookie table. Cookies are arbitrary data
357 objects that are references by unique name's by the wxApplet. These
358 values can be used to store and retrieve data that needs to remain
359 persisent across invocations of the wxApplet. Ie: The first time an
360 applet is created it would use the cookie to store data to maintain
361 it's present state so that if you navigated back to the same page
362 is would be able to re-load the prior state as though the applet
363 was never actually destructed.
365 Note: If a cookie with the same name already exists, this function returns
366 false. Hence if you wish to replace a cookie you should first call
367 UnRegisterCookie to ensure the cookie is deleted and then call this
369 ****************************************************************************/
370 bool wxHtmlAppletWindow::RegisterCookie(
371 const wxString
& name
,
374 // Fail if the named cookie already exists!
375 if (m_Cookies
.Get(name
))
377 m_Cookies
.Put(name
,cookie
);
381 /****************************************************************************
383 name - wxString uniq haskey used to remove item from hash
386 True if found and deleted, false if not found in table.
389 This function is called by the wxApplet's when they need de-register a
390 cookie of data in the applet window's cookie table. The data in the
391 cookie itself is also deleted before it is removed from the table.
392 ****************************************************************************/
393 bool wxHtmlAppletWindow::UnRegisterCookie(
394 const wxString
& name
)
396 wxObject
*data
= m_Cookies
.Delete(name
);
404 /****************************************************************************
406 msg - wxEvent message to be sent to all wxApplets
409 Pointer to the cookie data if found, NULL if not found.
412 This function is called by the wxApplet's when they need to find a cookie
413 of data given it's public name. If the cookie is not found, NULL is
415 ****************************************************************************/
416 wxObject
*wxHtmlAppletWindow::FindCookie(
417 const wxString
& name
)
419 return m_Cookies
.Get(name
);
422 /****************************************************************************
424 event - Event to handle
427 This function handles delayed LoadPage events posted from applets that
428 need to change the page for the current window to a new window.
429 ****************************************************************************/
430 void wxHtmlAppletWindow::OnLoadPage(
431 wxLoadPageEvent
&event
)
433 // Test the mutex lock.
436 if (event
.GetHtmlWindow() == this){
437 if (LoadPage(event
.GetHRef())){
438 // wxPageLoadedEvent evt;
439 // NOTE: This is reserved for later use as we might need to send
440 // page loaded events to applets.
447 /****************************************************************************
449 event - Event to handle
452 This function handles delayed LoadPage events posted from applets that
453 need to change the page for the current window to a new window.
454 ****************************************************************************/
455 void wxHtmlAppletWindow::OnPageLoaded(
461 /****************************************************************************
466 This function tries to lock the mutex. If it can't, returns
467 immediately with false.
468 ***************************************************************************/
469 bool wxHtmlAppletWindow::TryLock()
478 /****************************************************************************
480 name - name of the last applet that changed the data in this object
481 group - name of the group the allplet belongs to.
482 href - webpage to go to.
485 VirtualData is used to store information on the virtual links.
486 ****************************************************************************/
487 VirtualData::VirtualData(
497 /****************************************************************************
500 ****************************************************************************/
501 void AppletProcess::OnTerminate(
505 // we're not needed any more
509 #include "wx/html/m_templ.h"
511 /****************************************************************************
513 Implementation for the <embed> HTML tag handler. This handler takes care
514 of automatically constructing the wxApplet objects of the appropriate
515 class based on the <embed> tag information.
516 ****************************************************************************/
517 TAG_HANDLER_BEGIN(wxApplet
, "WXAPPLET")
519 TAG_HANDLER_PROC(tag
)
522 wxHtmlAppletWindow
*appletWindow
;
528 wnd
= m_WParser
->GetWindow();
530 if ((appletWindow
= wxDynamicCast(wnd
,wxHtmlAppletWindow
)) != NULL
){
531 tag
.ScanParam("WIDTH", "%i", &width
);
532 tag
.ScanParam("HEIGHT", "%i", &height
);
533 if (tag
.HasParam("CLASSID")){
534 classId
= tag
.GetParam("CLASSID");
535 if ( classId
.IsNull() || classId
.Len() == 0 ){
536 wxMessageBox("wxApplet tag error: CLASSID is NULL or empty.","Error",wxICON_ERROR
);
539 if (tag
.HasParam("NAME"))
540 name
= tag
.GetParam("NAME");
542 // If the name is NULL or len is zero then we assume that the html guy
543 // didn't include the name param which is optional.
544 if ( name
.IsNull() || name
.Len() == 0 )
547 // We got all the params and can now create the applet
548 if ((applet
= appletWindow
->CreateApplet(classId
, name
, tag
, wxSize(width
, height
))) != NULL
){
550 m_WParser
->OpenContainer()->InsertCell(new wxHtmlWidgetCell(applet
,0));
553 wxMessageBox("wxApplet error: Could not create:" + classId
+ "," + name
);
556 wxMessageBox("wxApplet tag error: Can not find CLASSID param.","Error",wxICON_ERROR
);
559 //Add more param parsing here. If or when spec changes.
560 //For now we'll ignore any other params those HTML guys
561 //might put in our tag.
567 TAG_HANDLER_END(wxApplet
)
569 TAGS_MODULE_BEGIN(wxApplet
)
570 TAGS_MODULE_ADD(wxApplet
)
571 TAGS_MODULE_END(wxApplet
)
573 // This is our little forcelink hack.