Back to index

lightning-sunbird  0.9+nobinonly
EmbedApp.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: Mozilla-sample-code 1.0
00003  *
00004  * Copyright (c) 2002 Netscape Communications Corporation and
00005  * other contributors
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a
00008  * copy of this Mozilla sample software and associated documentation files
00009  * (the "Software"), to deal in the Software without restriction, including
00010  * without limitation the rights to use, copy, modify, merge, publish,
00011  * distribute, sublicense, and/or sell copies of the Software, and to permit
00012  * persons to whom the Software is furnished to do so, subject to the
00013  * following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included
00016  * in all copies or substantial portions of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00021  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00023  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00024  * DEALINGS IN THE SOFTWARE.
00025  *
00026  * Contributor(s):
00027  *   Adam Lock <adamlock@netscape.com>
00028  *
00029  * ***** END LICENSE BLOCK ***** */
00030 
00031 #include "global.h"
00032 
00033 #include "nsIProfile.h"
00034 #include "nsIWindowWatcher.h"
00035 #include "nsMemory.h"
00036 #include "nsEmbedString.h"
00037 
00038 #include "BrowserFrame.h"
00039 #include "MailFrame.h"
00040 #include "ChatFrame.h"
00041 #include "EditorFrame.h"
00042 
00043 #include "GeckoProtocolHandler.h"
00044 #include "GeckoWindowCreator.h"
00045 
00046 class FooCallback : public GeckoChannelCallback
00047 {
00048 public:
00049     virtual nsresult GetData(
00050         nsIURI *aURI,
00051         nsIChannel *aChannel,
00052         nsACString &aContentType,
00053         void **aData,
00054         PRUint32 *aSize)
00055     {
00056         nsCAutoString spec;
00057         nsCAutoString txt("<html><body>Hello, your URL was \'");
00058         aURI->GetSpec(spec);
00059         txt.Append(spec);
00060         txt.Append("\'</body></html>");
00061         aContentType.Assign("text/html");
00062 
00063         size_t size = txt.Length();
00064         *aData = (void *) nsMemory::Alloc(size + 1);
00065         if (!*aData)
00066             return NS_ERROR_OUT_OF_MEMORY;
00067         memset(*aData, 0, size + 1);
00068         memcpy(*aData, txt.get(), size);
00069         *aSize = size;
00070         return NS_OK;
00071     }
00072 };
00073 
00074 class EmbedApp : public wxApp
00075 {
00076     virtual bool OnInit();
00077     virtual int OnExit();
00078 
00079     DECLARE_EVENT_TABLE()
00080     void OnQuit(wxCommandEvent &event);
00081     void OnAbout(wxCommandEvent &event);
00082     void OnMail(wxCommandEvent &event);
00083     void OnChat(wxCommandEvent &event);
00084     void OnEditor(wxCommandEvent &event);
00085 };
00086 
00087 BEGIN_EVENT_TABLE(EmbedApp, wxApp)
00088     EVT_MENU(XRCID("menu_quit"),   EmbedApp::OnQuit)
00089     EVT_MENU(XRCID("menu_about"),  EmbedApp::OnAbout)
00090     EVT_MENU(XRCID("menu_mail"),   EmbedApp::OnMail)
00091     EVT_MENU(XRCID("menu_chat"),   EmbedApp::OnChat)
00092     EVT_MENU(XRCID("menu_editor"), EmbedApp::OnEditor)
00093 END_EVENT_TABLE()
00094 
00096 
00097 IMPLEMENT_APP(EmbedApp)
00098 
00099 bool EmbedApp::OnInit()
00100 {
00101     wxImage::AddHandler(new wxGIFHandler);
00102     wxXmlResource::Get()->InitAllHandlers();
00103     InitXmlResource();
00104 
00105     if (NS_FAILED(NS_InitEmbedding(nsnull, nsnull)))
00106     {
00107         return FALSE;
00108     }
00109 
00110     nsresult rv;
00111     nsCOMPtr<nsIProfile> profileService = 
00112              do_GetService(NS_PROFILE_CONTRACTID, &rv);
00113     rv = profileService->CreateNewProfile(L"wxEmbed", nsnull, nsnull, PR_FALSE);
00114     if (NS_FAILED(rv)) return FALSE;
00115     rv = profileService->SetCurrentProfile(L"wxEmbed");
00116     if (NS_FAILED(rv)) return FALSE;
00117 
00118     GeckoProtocolHandler::RegisterHandler("foo", "Test handler", new FooCallback);
00119 
00120     // Register a window creator for popups
00121     GeckoWindowCreator *creatorCallback = new GeckoWindowCreator();
00122     if (creatorCallback)
00123     {
00124         nsCOMPtr<nsIWindowCreator> windowCreator(NS_STATIC_CAST(nsIWindowCreator *, creatorCallback));
00125         if (windowCreator)
00126         {
00127             nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService(NS_WINDOWWATCHER_CONTRACTID));
00128             if (wwatch)
00129             {
00130                 wwatch->SetWindowCreator(windowCreator);
00131             }
00132         }
00133     }
00134 
00135     // Create the main frame window
00136     BrowserFrame* frame = new BrowserFrame(NULL);
00137     frame->Show(TRUE);
00138     // frame->OnBrowserHome(wxCommandEvent());
00139 
00140     SetTopWindow(frame);
00141 
00142     return TRUE;
00143 }
00144 
00145 int EmbedApp::OnExit()
00146 {
00147     // Shutdown the profile
00148     nsresult rv;
00149     nsCOMPtr<nsIProfile> profileService = 
00150              do_GetService(NS_PROFILE_CONTRACTID, &rv);
00151     if (NS_SUCCEEDED(rv))
00152     {
00153         profileService->ShutDownCurrentProfile(nsIProfile::SHUTDOWN_PERSIST); 
00154     }
00155     NS_TermEmbedding();
00156 
00157     return 0;
00158 }
00159 
00160 void EmbedApp::OnQuit(wxCommandEvent & WXUNUSED(event))
00161 {
00162     wxFrame * frame = NULL;
00163     
00164 /*
00165     do {
00166         frame = (wxFrame *) wxWindow::FindWindowByName("browser", NULL);;
00167         if (frame)
00168             frame->Close(TRUE);
00169     } while (frame);
00170     frame = (wxFrame *) wxWindow::FindWindowByName("mail", NULL);;
00171     if (frame)
00172         frame->Close(TRUE);
00173     frame = (wxFrame *) wxWindow::FindWindowByName("editor", NULL);;
00174     if (frame)
00175         frame->Close(TRUE);
00176     frame = (wxFrame *) wxWindow::FindWindowByName("chat", NULL);;
00177     if (frame)
00178         frame->Close(TRUE);
00179 */
00180 
00181 
00182     GetTopWindow()->Close(TRUE);
00183 }
00184 
00185 void EmbedApp::OnAbout(wxCommandEvent & WXUNUSED(event))
00186 {
00187     (void)wxMessageBox(_T("wxEmbed"), _T("About wxEmbed"));
00188 }
00189 
00190 void EmbedApp::OnMail(wxCommandEvent & WXUNUSED(event))
00191 {
00192     // Create the mail frame window
00193     MailFrame * frame = (MailFrame *) wxWindow::FindWindowByName("mail", NULL);;
00194     if (!frame)
00195         frame = new MailFrame(NULL);
00196     if (frame)
00197         frame->Show(TRUE);
00198 }
00199 
00200 void EmbedApp::OnChat(wxCommandEvent & WXUNUSED(event))
00201 {
00202     ChatFrame * frame = (ChatFrame *) wxWindow::FindWindowByName("chat", NULL);
00203     if (!frame)
00204         frame = new ChatFrame(NULL);
00205     if (frame)
00206         frame->Show(TRUE);
00207 }
00208 
00209 void EmbedApp::OnEditor(wxCommandEvent & WXUNUSED(event))
00210 {
00211     EditorFrame * frame = (EditorFrame *) wxWindow::FindWindowByName("editor", NULL);
00212     if (!frame)
00213         frame = new EditorFrame(NULL);
00214     if (frame)
00215         frame->Show(TRUE);
00216 }