Back to index

lightning-sunbird  0.9+nobinonly
BrowserFrame.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  *
00028  * ***** END LICENSE BLOCK ***** */
00029 
00030 #include "global.h"
00031 
00032 #include "wx/strconv.h"
00033 
00034 #include "BrowserFrame.h"
00035 
00036 #include "nsIURI.h"
00037 
00038 BEGIN_EVENT_TABLE(BrowserFrame, GeckoFrame)
00039 
00040     // View menu
00041     EVT_MENU(XRCID("view_pagesource"),  BrowserFrame::OnViewPageSource)
00042     EVT_UPDATE_UI(XRCID("view_pagesource"),
00043                                         BrowserFrame::OnUpdateViewPageSource)
00044 
00045     // Browser operations, back / forward etc.
00046     // TODO some of these can go in GeckoFrame
00047     EVT_MENU(XRCID("browse_back"),      BrowserFrame::OnBrowserBack)
00048     EVT_UPDATE_UI(XRCID("browse_back"), BrowserFrame::OnUpdateBrowserBack)
00049     EVT_MENU(XRCID("browse_fwd"),       BrowserFrame::OnBrowserForward)
00050     EVT_UPDATE_UI(XRCID("browse_fwd"),  BrowserFrame::OnUpdateBrowserForward)
00051     EVT_MENU(XRCID("browse_reload"),    BrowserFrame::OnBrowserReload)
00052     EVT_MENU(XRCID("browse_stop"),      BrowserFrame::OnBrowserStop)
00053     EVT_UPDATE_UI(XRCID("browse_stop"), BrowserFrame::OnUpdateBrowserStop)
00054     EVT_MENU(XRCID("browse_home"),      BrowserFrame::OnBrowserHome)
00055     EVT_BUTTON(XRCID("browser_go"),     BrowserFrame::OnBrowserGo)
00056     EVT_TEXT_ENTER(XRCID("url"),        BrowserFrame::OnBrowserUrl)
00057     EVT_MENU(XRCID("browser_open_in_new_window"),
00058                                         BrowserFrame::OnBrowserOpenLinkInNewWindow)
00059 END_EVENT_TABLE()
00060 
00061 BrowserFrame::BrowserFrame(wxWindow* aParent)
00062 {
00063     wxXmlResource::Get()->LoadFrame(this, aParent, wxT("browser_frame"));
00064 
00065     SetIcon(wxICON(appicon));
00066 
00067     SetName("browser");
00068 
00069     SetupDefaultGeckoWindow();
00070 
00071     CreateStatusBar();
00072 }
00073 
00074 
00075 nsresult BrowserFrame::LoadURI(const wchar_t *aURI)
00076 {
00077     if (mWebBrowser)
00078     {
00079         nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
00080         if (webNav)
00081         {
00082             return webNav->LoadURI(aURI,
00083                     nsIWebNavigation::LOAD_FLAGS_NONE,
00084                     nsnull,
00085                     nsnull,
00086                     nsnull);
00087         }
00088     }
00089     return NS_ERROR_FAILURE;
00090 }
00091 
00092 
00093 nsresult BrowserFrame::LoadURI(const char *aURI)
00094 {
00095     wxMBConv conv;
00096     return LoadURI(conv.cWX2WC(aURI));
00097 }
00098 
00099 
00101 // Browser specific handlers
00102 
00103 
00104 void BrowserFrame::OnFileSave(wxCommandEvent & WXUNUSED(event))
00105 {
00106 }
00107 
00108 void BrowserFrame::OnFilePrint(wxCommandEvent & WXUNUSED(event))
00109 {
00110 }
00111 
00112 void BrowserFrame::OnViewPageSource(wxCommandEvent &event)
00113 {
00114     nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
00115     if(!webNav)
00116         return;
00117 
00118     // Get the URI object whose source we want to view.
00119     nsresult rv = NS_OK;
00120     nsCOMPtr<nsIURI> currentURI;
00121     rv = webNav->GetCurrentURI(getter_AddRefs(currentURI));
00122     if(NS_FAILED(rv) || !currentURI)
00123         return;
00124 
00125     // Get the uri string associated with the nsIURI object
00126     nsCAutoString uriString;
00127     rv = currentURI->GetSpec(uriString);
00128     if(NS_FAILED(rv))
00129         return;
00130 
00131     // Build the view-source: url
00132     nsAutoString viewSrcUrl(L"view-source:");
00133     viewSrcUrl.AppendWithConversion(uriString.get());
00134 
00135     BrowserFrame *frame = new BrowserFrame(NULL);
00136     if (frame)
00137     {
00138         frame->Show(TRUE);
00139         frame->LoadURI(viewSrcUrl.get());
00140     }
00141 
00142 }
00143 
00144 void BrowserFrame::OnUpdateViewPageSource(wxUpdateUIEvent &event)
00145 {
00146 }
00147 
00148 void BrowserFrame::OnBrowserGo(wxCommandEvent & WXUNUSED(event))
00149 {
00150     wxTextCtrl *txtCtrl = (wxTextCtrl *) FindWindowById(XRCID("url"), this);
00151     wxString url = txtCtrl->GetValue();
00152     if (!url.IsEmpty())
00153     {
00154         LoadURI(url);
00155     }
00156 }
00157 
00158 void BrowserFrame::OnBrowserUrl(wxCommandEvent & event)
00159 {
00160     OnBrowserGo(event);
00161 }
00162 
00163 void BrowserFrame::OnBrowserBack(wxCommandEvent & WXUNUSED(event))
00164 {
00165     if (mWebBrowser)
00166     {
00167         nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
00168         webNav->GoBack();
00169     }
00170 }
00171 
00172 void BrowserFrame::OnUpdateBrowserBack(wxUpdateUIEvent &event)
00173 {
00174     PRBool canGoBack = PR_FALSE;
00175     if (mWebBrowser)
00176     {
00177         nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
00178         webNav->GetCanGoBack(&canGoBack);
00179     }
00180     event.Enable(canGoBack ? true : false);
00181 }
00182 
00183 void BrowserFrame::OnBrowserForward(wxCommandEvent & WXUNUSED(event))
00184 {
00185     if (mWebBrowser)
00186     {
00187         nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
00188         webNav->GoForward();
00189     }
00190 }
00191 
00192 void BrowserFrame::OnUpdateBrowserForward(wxUpdateUIEvent &event)
00193 {
00194     PRBool canGoForward = PR_FALSE;
00195     if (mWebBrowser)
00196     {
00197         nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
00198         webNav->GetCanGoForward(&canGoForward);
00199     }
00200     event.Enable(canGoForward ? true : false);
00201 }
00202 
00203 void BrowserFrame::OnBrowserReload(wxCommandEvent & WXUNUSED(event))
00204 {
00205     if (mWebBrowser)
00206     {
00207         nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
00208         webNav->Reload(nsIWebNavigation::LOAD_FLAGS_NONE);
00209     }
00210 }
00211 
00212 void BrowserFrame::OnBrowserStop(wxCommandEvent & WXUNUSED(event))
00213 {
00214     if (mWebBrowser)
00215     {
00216         nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
00217         webNav->Stop(nsIWebNavigation::STOP_ALL);
00218     }
00219 }
00220 
00221 void BrowserFrame::OnUpdateBrowserStop(wxUpdateUIEvent &event)
00222 {
00223     event.Enable(mBusy ? true : false);
00224 }
00225 
00226 void BrowserFrame::OnBrowserHome(wxCommandEvent & WXUNUSED(event))
00227 {
00228     LoadURI("http://www.mozilla.org/projects/embedding/");
00229 }
00230 
00231 void BrowserFrame::OnBrowserOpenLinkInNewWindow(wxCommandEvent & WXUNUSED(event))
00232 {
00233     BrowserFrame* frame = new BrowserFrame(NULL);
00234     frame->Show(TRUE);
00235     frame->LoadURI(mContextLinkUrl.get());
00236 }
00237 
00239 // GeckoContainerUI overrides
00240 
00241 nsresult BrowserFrame::CreateBrowserWindow(PRUint32 aChromeFlags,
00242          nsIWebBrowserChrome *aParent, nsIWebBrowserChrome **aNewWindow)
00243 {
00244     // Create the main frame window
00245     BrowserFrame* frame = new BrowserFrame(NULL);
00246     if (!frame)
00247         return NS_ERROR_OUT_OF_MEMORY;
00248     frame->Show(TRUE);
00249     GeckoContainer *container = frame->mGeckoWnd->GetGeckoContainer();
00250     return container->QueryInterface(NS_GET_IID(nsIWebBrowserChrome), (void **) aNewWindow);
00251 }
00252 
00253 void BrowserFrame::UpdateStatusBarText(const PRUnichar* aStatusText)
00254 {
00255     SetStatusText(aStatusText);
00256 }
00257 
00258 void BrowserFrame::UpdateCurrentURI()
00259 {
00260     if (mWebBrowser)
00261     {
00262         nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
00263         nsCOMPtr<nsIURI> currentURI;
00264         webNav->GetCurrentURI(getter_AddRefs(currentURI));
00265         nsCAutoString spec;
00266         currentURI->GetSpec(spec);
00267 
00268         wxTextCtrl *txtCtrl = (wxTextCtrl *) FindWindowById(XRCID("url"), this);
00269         if (txtCtrl)
00270         {
00271             txtCtrl->SetValue(spec.get());
00272         }
00273     }
00274 }
00275 
00276 #include "nsIDOMMouseEvent.h"
00277 
00278 void BrowserFrame::ShowContextMenu(PRUint32 aContextFlags, nsIContextMenuInfo *aContextMenuInfo)
00279 {
00280     nsCOMPtr<nsIDOMEvent> event;
00281     aContextMenuInfo->GetMouseEvent(getter_AddRefs(event));
00282     if (!event)
00283     {
00284         return;
00285     }
00286 
00287     mContextLinkUrl.SetLength(0);
00288 
00289     nsCOMPtr<nsIDOMMouseEvent> mouseEvent = do_QueryInterface(event);
00290     if (mouseEvent)
00291     {
00292         PRInt32 x, y;
00293         mouseEvent->GetScreenX(&x);
00294         mouseEvent->GetScreenY(&y);
00295 
00296         char *menuName = NULL;
00297 
00298         // CONTEXT_LINK                   <A>
00299         // CONTEXT_IMAGE                  <IMG>
00300         // CONTEXT_IMAGE | CONTEXT_LINK   <IMG> with <A> as an ancestor
00301         // CONTEXT_INPUT                  <INPUT>
00302         // CONTEXT_INPUT | CONTEXT_IMAGE  <INPUT> with type=image
00303         // CONTEXT_TEXT                   <TEXTAREA>
00304         // CONTEXT_DOCUMENT               <HTML>
00305         // CONTEXT_BACKGROUND_IMAGE       <HTML> with background image
00306         
00307         if (aContextFlags & nsIContextMenuListener2::CONTEXT_IMAGE)
00308         {
00309             if (aContextFlags & nsIContextMenuListener2::CONTEXT_LINK)
00310                 menuName = "context_browser_image"; // TODO
00311             else if (aContextFlags & nsIContextMenuListener2::CONTEXT_INPUT)
00312                 menuName = "context_browser_image"; // TODO
00313             else
00314                 menuName = "context_browser_image"; // TODO
00315         }
00316         else if (aContextFlags & nsIContextMenuListener2::CONTEXT_LINK)
00317         {
00318             menuName = "context_browser_link";
00319 
00320             aContextMenuInfo->GetAssociatedLink(mContextLinkUrl);
00321         }
00322         else if (aContextFlags & nsIContextMenuListener2::CONTEXT_INPUT)
00323         {
00324             menuName = "context_browser_input";
00325         }
00326         else if (aContextFlags & nsIContextMenuListener2::CONTEXT_TEXT)
00327         {
00328             menuName = "context_browser_text";
00329         }
00330         else if (aContextFlags & nsIContextMenuListener2::CONTEXT_DOCUMENT)
00331         {
00332             menuName = "context_browser_document";
00333         }
00334         else if (aContextFlags & nsIContextMenuListener2::CONTEXT_BACKGROUND_IMAGE)
00335         {
00336             menuName = "context_browser_document";
00337         }
00338 
00339         if (!menuName)
00340             return;
00341 
00342 // Hack for Win32 that has a #define for LoadMenu
00343 #undef LoadMenu
00344         wxMenu *menu = wxXmlResource::Get()->LoadMenu(menuName);
00345         if (menu)
00346         {
00347             int fX = 0, fY = 0;
00348             // Make screen coords relative to the frame window for accurate
00349             // popup menu position
00350             GetPosition(&fX, &fY);
00351             PopupMenu(menu, x - fX, y - fY);
00352         }
00353     }
00354 }
00355 
00356