Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / embedding / tests / wxEmbed / BrowserFrame.cpp
blobf6fe166fba7b5c2174acfa3b02d95358eeeddb0e
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: Mozilla-sample-code 1.0
4 * Copyright (c) 2002 Netscape Communications Corporation and
5 * other contributors
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.
26 * Contributor(s):
28 * ***** END LICENSE BLOCK ***** */
30 #include "global.h"
32 #include "wx/strconv.h"
34 #include "BrowserFrame.h"
36 #include "nsIURI.h"
38 BEGIN_EVENT_TABLE(BrowserFrame, GeckoFrame)
40 // View menu
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)
59 END_EVENT_TABLE()
61 BrowserFrame::BrowserFrame(wxWindow* aParent)
63 wxXmlResource::Get()->LoadFrame(this, aParent, wxT("browser_frame"));
65 SetIcon(wxICON(appicon));
67 SetName("browser");
69 SetupDefaultGeckoWindow();
71 CreateStatusBar();
75 nsresult BrowserFrame::LoadURI(const wchar_t *aURI)
77 if (mWebBrowser)
79 nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
80 if (webNav)
82 return webNav->LoadURI(aURI,
83 nsIWebNavigation::LOAD_FLAGS_NONE,
84 nsnull,
85 nsnull,
86 nsnull);
89 return NS_ERROR_FAILURE;
93 nsresult BrowserFrame::LoadURI(const char *aURI)
95 wxMBConv conv;
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);
115 if(!webNav)
116 return;
118 // Get the URI object whose source we want to view.
119 nsresult rv = NS_OK;
120 nsCOMPtr<nsIURI> currentURI;
121 rv = webNav->GetCurrentURI(getter_AddRefs(currentURI));
122 if(NS_FAILED(rv) || !currentURI)
123 return;
125 // Get the uri string associated with the nsIURI object
126 nsCAutoString uriString;
127 rv = currentURI->GetSpec(uriString);
128 if(NS_FAILED(rv))
129 return;
131 // Build the view-source: url
132 nsAutoString viewSrcUrl(L"view-source:");
133 viewSrcUrl.AppendWithConversion(uriString.get());
135 BrowserFrame *frame = new BrowserFrame(NULL);
136 if (frame)
138 frame->Show(TRUE);
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();
152 if (!url.IsEmpty())
154 LoadURI(url);
158 void BrowserFrame::OnBrowserUrl(wxCommandEvent & event)
160 OnBrowserGo(event);
163 void BrowserFrame::OnBrowserBack(wxCommandEvent & WXUNUSED(event))
165 if (mWebBrowser)
167 nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
168 webNav->GoBack();
172 void BrowserFrame::OnUpdateBrowserBack(wxUpdateUIEvent &event)
174 PRBool canGoBack = PR_FALSE;
175 if (mWebBrowser)
177 nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
178 webNav->GetCanGoBack(&canGoBack);
180 event.Enable(canGoBack ? true : false);
183 void BrowserFrame::OnBrowserForward(wxCommandEvent & WXUNUSED(event))
185 if (mWebBrowser)
187 nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
188 webNav->GoForward();
192 void BrowserFrame::OnUpdateBrowserForward(wxUpdateUIEvent &event)
194 PRBool canGoForward = PR_FALSE;
195 if (mWebBrowser)
197 nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
198 webNav->GetCanGoForward(&canGoForward);
200 event.Enable(canGoForward ? true : false);
203 void BrowserFrame::OnBrowserReload(wxCommandEvent & WXUNUSED(event))
205 if (mWebBrowser)
207 nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
208 webNav->Reload(nsIWebNavigation::LOAD_FLAGS_NONE);
212 void BrowserFrame::OnBrowserStop(wxCommandEvent & WXUNUSED(event))
214 if (mWebBrowser)
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);
234 frame->Show(TRUE);
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);
246 if (!frame)
247 return NS_ERROR_OUT_OF_MEMORY;
248 frame->Show(TRUE);
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()
260 if (mWebBrowser)
262 nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
263 nsCOMPtr<nsIURI> currentURI;
264 webNav->GetCurrentURI(getter_AddRefs(currentURI));
265 nsCAutoString spec;
266 currentURI->GetSpec(spec);
268 wxTextCtrl *txtCtrl = (wxTextCtrl *) FindWindowById(XRCID("url"), this);
269 if (txtCtrl)
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));
282 if (!event)
284 return;
287 mContextLinkUrl.SetLength(0);
289 nsCOMPtr<nsIDOMMouseEvent> mouseEvent = do_QueryInterface(event);
290 if (mouseEvent)
292 PRInt32 x, y;
293 mouseEvent->GetScreenX(&x);
294 mouseEvent->GetScreenY(&y);
296 char *menuName = NULL;
298 // CONTEXT_LINK <A>
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
313 else
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";
339 if (!menuName)
340 return;
342 // Hack for Win32 that has a #define for LoadMenu
343 #undef LoadMenu
344 wxMenu *menu = wxXmlResource::Get()->LoadMenu(menuName);
345 if (menu)
347 int fX = 0, fY = 0;
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);