1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: Mozilla-sample-code 1.0
4 * Copyright (c) 2002 Netscape Communications Corporation and
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this Mozilla sample software and associated documentation files
9 * (the "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to permit
12 * persons to whom the Software is furnished to do so, subject to the
13 * following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
28 * ***** END LICENSE BLOCK ***** */
32 #include "wx/strconv.h"
34 #include "BrowserFrame.h"
38 BEGIN_EVENT_TABLE(BrowserFrame
, GeckoFrame
)
41 EVT_MENU(XRCID("view_pagesource"), BrowserFrame::OnViewPageSource
)
42 EVT_UPDATE_UI(XRCID("view_pagesource"),
43 BrowserFrame::OnUpdateViewPageSource
)
45 // Browser operations, back / forward etc.
46 // TODO some of these can go in GeckoFrame
47 EVT_MENU(XRCID("browse_back"), BrowserFrame::OnBrowserBack
)
48 EVT_UPDATE_UI(XRCID("browse_back"), BrowserFrame::OnUpdateBrowserBack
)
49 EVT_MENU(XRCID("browse_fwd"), BrowserFrame::OnBrowserForward
)
50 EVT_UPDATE_UI(XRCID("browse_fwd"), BrowserFrame::OnUpdateBrowserForward
)
51 EVT_MENU(XRCID("browse_reload"), BrowserFrame::OnBrowserReload
)
52 EVT_MENU(XRCID("browse_stop"), BrowserFrame::OnBrowserStop
)
53 EVT_UPDATE_UI(XRCID("browse_stop"), BrowserFrame::OnUpdateBrowserStop
)
54 EVT_MENU(XRCID("browse_home"), BrowserFrame::OnBrowserHome
)
55 EVT_BUTTON(XRCID("browser_go"), BrowserFrame::OnBrowserGo
)
56 EVT_TEXT_ENTER(XRCID("url"), BrowserFrame::OnBrowserUrl
)
57 EVT_MENU(XRCID("browser_open_in_new_window"),
58 BrowserFrame::OnBrowserOpenLinkInNewWindow
)
61 BrowserFrame::BrowserFrame(wxWindow
* aParent
)
63 wxXmlResource::Get()->LoadFrame(this, aParent
, wxT("browser_frame"));
65 SetIcon(wxICON(appicon
));
69 SetupDefaultGeckoWindow();
75 nsresult
BrowserFrame::LoadURI(const wchar_t *aURI
)
79 nsCOMPtr
<nsIWebNavigation
> webNav
= do_QueryInterface(mWebBrowser
);
82 return webNav
->LoadURI(aURI
,
83 nsIWebNavigation::LOAD_FLAGS_NONE
,
89 return NS_ERROR_FAILURE
;
93 nsresult
BrowserFrame::LoadURI(const char *aURI
)
96 return LoadURI(conv
.cWX2WC(aURI
));
100 ///////////////////////////////////////////////////////////////////////////////
101 // Browser specific handlers
104 void BrowserFrame::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
108 void BrowserFrame::OnFilePrint(wxCommandEvent
& WXUNUSED(event
))
112 void BrowserFrame::OnViewPageSource(wxCommandEvent
&event
)
114 nsCOMPtr
<nsIWebNavigation
> webNav
= do_QueryInterface(mWebBrowser
);
118 // Get the URI object whose source we want to view.
120 nsCOMPtr
<nsIURI
> currentURI
;
121 rv
= webNav
->GetCurrentURI(getter_AddRefs(currentURI
));
122 if(NS_FAILED(rv
) || !currentURI
)
125 // Get the uri string associated with the nsIURI object
126 nsCAutoString uriString
;
127 rv
= currentURI
->GetSpec(uriString
);
131 // Build the view-source: url
132 nsAutoString
viewSrcUrl(L
"view-source:");
133 viewSrcUrl
.AppendWithConversion(uriString
.get());
135 BrowserFrame
*frame
= new BrowserFrame(NULL
);
139 frame
->LoadURI(viewSrcUrl
.get());
144 void BrowserFrame::OnUpdateViewPageSource(wxUpdateUIEvent
&event
)
148 void BrowserFrame::OnBrowserGo(wxCommandEvent
& WXUNUSED(event
))
150 wxTextCtrl
*txtCtrl
= (wxTextCtrl
*) FindWindowById(XRCID("url"), this);
151 wxString url
= txtCtrl
->GetValue();
158 void BrowserFrame::OnBrowserUrl(wxCommandEvent
& event
)
163 void BrowserFrame::OnBrowserBack(wxCommandEvent
& WXUNUSED(event
))
167 nsCOMPtr
<nsIWebNavigation
> webNav
= do_QueryInterface(mWebBrowser
);
172 void BrowserFrame::OnUpdateBrowserBack(wxUpdateUIEvent
&event
)
174 PRBool canGoBack
= PR_FALSE
;
177 nsCOMPtr
<nsIWebNavigation
> webNav
= do_QueryInterface(mWebBrowser
);
178 webNav
->GetCanGoBack(&canGoBack
);
180 event
.Enable(canGoBack
? true : false);
183 void BrowserFrame::OnBrowserForward(wxCommandEvent
& WXUNUSED(event
))
187 nsCOMPtr
<nsIWebNavigation
> webNav
= do_QueryInterface(mWebBrowser
);
192 void BrowserFrame::OnUpdateBrowserForward(wxUpdateUIEvent
&event
)
194 PRBool canGoForward
= PR_FALSE
;
197 nsCOMPtr
<nsIWebNavigation
> webNav
= do_QueryInterface(mWebBrowser
);
198 webNav
->GetCanGoForward(&canGoForward
);
200 event
.Enable(canGoForward
? true : false);
203 void BrowserFrame::OnBrowserReload(wxCommandEvent
& WXUNUSED(event
))
207 nsCOMPtr
<nsIWebNavigation
> webNav
= do_QueryInterface(mWebBrowser
);
208 webNav
->Reload(nsIWebNavigation::LOAD_FLAGS_NONE
);
212 void BrowserFrame::OnBrowserStop(wxCommandEvent
& WXUNUSED(event
))
216 nsCOMPtr
<nsIWebNavigation
> webNav
= do_QueryInterface(mWebBrowser
);
217 webNav
->Stop(nsIWebNavigation::STOP_ALL
);
221 void BrowserFrame::OnUpdateBrowserStop(wxUpdateUIEvent
&event
)
223 event
.Enable(mBusy
? true : false);
226 void BrowserFrame::OnBrowserHome(wxCommandEvent
& WXUNUSED(event
))
228 LoadURI("http://www.mozilla.org/projects/embedding/");
231 void BrowserFrame::OnBrowserOpenLinkInNewWindow(wxCommandEvent
& WXUNUSED(event
))
233 BrowserFrame
* frame
= new BrowserFrame(NULL
);
235 frame
->LoadURI(mContextLinkUrl
.get());
238 ///////////////////////////////////////////////////////////////////////////////
239 // GeckoContainerUI overrides
241 nsresult
BrowserFrame::CreateBrowserWindow(PRUint32 aChromeFlags
,
242 nsIWebBrowserChrome
*aParent
, nsIWebBrowserChrome
**aNewWindow
)
244 // Create the main frame window
245 BrowserFrame
* frame
= new BrowserFrame(NULL
);
247 return NS_ERROR_OUT_OF_MEMORY
;
249 GeckoContainer
*container
= frame
->mGeckoWnd
->GetGeckoContainer();
250 return container
->QueryInterface(NS_GET_IID(nsIWebBrowserChrome
), (void **) aNewWindow
);
253 void BrowserFrame::UpdateStatusBarText(const PRUnichar
* aStatusText
)
255 SetStatusText(aStatusText
);
258 void BrowserFrame::UpdateCurrentURI()
262 nsCOMPtr
<nsIWebNavigation
> webNav
= do_QueryInterface(mWebBrowser
);
263 nsCOMPtr
<nsIURI
> currentURI
;
264 webNav
->GetCurrentURI(getter_AddRefs(currentURI
));
266 currentURI
->GetSpec(spec
);
268 wxTextCtrl
*txtCtrl
= (wxTextCtrl
*) FindWindowById(XRCID("url"), this);
271 txtCtrl
->SetValue(spec
.get());
276 #include "nsIDOMMouseEvent.h"
278 void BrowserFrame::ShowContextMenu(PRUint32 aContextFlags
, nsIContextMenuInfo
*aContextMenuInfo
)
280 nsCOMPtr
<nsIDOMEvent
> event
;
281 aContextMenuInfo
->GetMouseEvent(getter_AddRefs(event
));
287 mContextLinkUrl
.SetLength(0);
289 nsCOMPtr
<nsIDOMMouseEvent
> mouseEvent
= do_QueryInterface(event
);
293 mouseEvent
->GetScreenX(&x
);
294 mouseEvent
->GetScreenY(&y
);
296 char *menuName
= NULL
;
299 // CONTEXT_IMAGE <IMG>
300 // CONTEXT_IMAGE | CONTEXT_LINK <IMG> with <A> as an ancestor
301 // CONTEXT_INPUT <INPUT>
302 // CONTEXT_INPUT | CONTEXT_IMAGE <INPUT> with type=image
303 // CONTEXT_TEXT <TEXTAREA>
304 // CONTEXT_DOCUMENT <HTML>
305 // CONTEXT_BACKGROUND_IMAGE <HTML> with background image
307 if (aContextFlags
& nsIContextMenuListener2::CONTEXT_IMAGE
)
309 if (aContextFlags
& nsIContextMenuListener2::CONTEXT_LINK
)
310 menuName
= "context_browser_image"; // TODO
311 else if (aContextFlags
& nsIContextMenuListener2::CONTEXT_INPUT
)
312 menuName
= "context_browser_image"; // TODO
314 menuName
= "context_browser_image"; // TODO
316 else if (aContextFlags
& nsIContextMenuListener2::CONTEXT_LINK
)
318 menuName
= "context_browser_link";
320 aContextMenuInfo
->GetAssociatedLink(mContextLinkUrl
);
322 else if (aContextFlags
& nsIContextMenuListener2::CONTEXT_INPUT
)
324 menuName
= "context_browser_input";
326 else if (aContextFlags
& nsIContextMenuListener2::CONTEXT_TEXT
)
328 menuName
= "context_browser_text";
330 else if (aContextFlags
& nsIContextMenuListener2::CONTEXT_DOCUMENT
)
332 menuName
= "context_browser_document";
334 else if (aContextFlags
& nsIContextMenuListener2::CONTEXT_BACKGROUND_IMAGE
)
336 menuName
= "context_browser_document";
342 // Hack for Win32 that has a #define for LoadMenu
344 wxMenu
*menu
= wxXmlResource::Get()->LoadMenu(menuName
);
348 // Make screen coords relative to the frame window for accurate
349 // popup menu position
350 GetPosition(&fX
, &fY
);
351 PopupMenu(menu
, x
- fX
, y
- fY
);