Follow-on fix for bug 457825. Use sheet principal for agent and user sheets. r=dbaron...
[wine-gecko.git] / embedding / qa / testembed / BrowserFrameGlue.cpp
blob1a84ef7c1755523b389bb4dbc8c08e8e3753d244
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Chak Nanga <chak@netscape.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 // File Overview....
41 // This file has the IBrowserFrameGlueObj implementation
42 // This frame glue object is nested inside of the BrowserFrame
43 // object(See BrowserFrm.h for more info)
45 // This is the place where all the platform specific interaction
46 // with the browser frame window takes place in response to
47 // callbacks from Gecko interface methods
48 //
49 // The main purpose of this interface is to separate the cross
50 // platform code in BrowserImpl*.cpp from the platform specific
51 // code(which is in this file)
53 // You'll also notice the use of a macro named "METHOD_PROLOGUE"
54 // through out this file. This macro essentially makes the pointer
55 // to a "containing" class available inside of the class which is
56 // being contained via a var named "pThis". In our case, the
57 // BrowserFrameGlue object is contained inside of the BrowserFrame
58 // object so "pThis" will be a pointer to a BrowserFrame object
59 // Refer to MFC docs for more info on METHOD_PROLOGUE macro
62 #include "stdafx.h"
63 #include "TestEmbed.h"
64 #include "BrowserFrm.h"
65 #include "Dialogs.h"
66 #include "nsReadableUtils.h"
68 /////////////////////////////////////////////////////////////////////////////
69 // IBrowserFrameGlue implementation
71 void CBrowserFrame::BrowserFrameGlueObj::UpdateStatusBarText(const PRUnichar *aMessage)
73 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
75 nsCString strStatus;
77 if(aMessage)
78 strStatus.AssignWithConversion(aMessage);
80 pThis->m_wndStatusBar.SetPaneText(0, strStatus.get());
83 void CBrowserFrame::BrowserFrameGlueObj::UpdateProgress(PRInt32 aCurrent, PRInt32 aMax)
85 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
87 pThis->m_wndProgressBar.SetRange32(0, aMax);
88 pThis->m_wndProgressBar.SetPos(aCurrent);
91 void CBrowserFrame::BrowserFrameGlueObj::UpdateBusyState(PRBool aBusy)
93 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
95 // Just notify the view of the busy state
96 // There's code in there which will take care of
97 // updating the STOP toolbar btn. etc
99 pThis->m_wndBrowserView.UpdateBusyState(aBusy);
102 // Called from the OnLocationChange() method in the nsIWebProgressListener
103 // interface implementation in CBrowserImpl to update the current URI
104 // Will get called after a URI is successfully loaded in the browser
105 // We use this info to update the URL bar's edit box
107 void CBrowserFrame::BrowserFrameGlueObj::UpdateCurrentURI(nsIURI *aLocation)
109 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
111 if(aLocation)
113 nsCAutoString uriString;
114 // nsXPIDLCString uriString;
115 aLocation->GetSpec(uriString);
117 pThis->m_wndUrlBar.SetCurrentURL(uriString.get());
121 void CBrowserFrame::BrowserFrameGlueObj::GetBrowserFrameTitle(PRUnichar **aTitle)
123 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
125 CString title;
126 pThis->GetWindowText(title);
128 if(!title.IsEmpty())
130 nsString nsTitle;
131 nsTitle.AssignWithConversion(title.GetBuffer(0));
133 *aTitle = ToNewUnicode(nsTitle);
137 void CBrowserFrame::BrowserFrameGlueObj::SetBrowserFrameTitle(const PRUnichar *aTitle)
139 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
141 USES_CONVERSION;
143 if(W2T(aTitle))
145 pThis->SetWindowText(W2T(aTitle));
147 else
149 // Use the AppName i.e. testembed as the title if we
150 // do not get one from GetBrowserWindowTitle()
152 CString cs;
153 cs.LoadString(AFX_IDS_APP_TITLE);
154 pThis->SetWindowText(cs);
158 void CBrowserFrame::BrowserFrameGlueObj::SetBrowserFrameSize(PRInt32 aCX, PRInt32 aCY)
160 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
162 pThis->SetWindowPos(NULL, 0, 0, aCX, aCY,
163 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER
167 void CBrowserFrame::BrowserFrameGlueObj::GetBrowserFrameSize(PRInt32 *aCX, PRInt32 *aCY)
169 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
171 RECT wndRect;
172 pThis->GetWindowRect(&wndRect);
174 if (aCX)
175 *aCX = wndRect.right - wndRect.left;
177 if (aCY)
178 *aCY = wndRect.bottom - wndRect.top;
181 void CBrowserFrame::BrowserFrameGlueObj::SetBrowserFramePosition(PRInt32 aX, PRInt32 aY)
183 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
185 pThis->SetWindowPos(NULL, aX, aY, 0, 0,
186 SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER);
189 void CBrowserFrame::BrowserFrameGlueObj::GetBrowserFramePosition(PRInt32 *aX, PRInt32 *aY)
191 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
193 RECT wndRect;
194 pThis->GetWindowRect(&wndRect);
196 if (aX)
197 *aX = wndRect.left;
199 if (aY)
200 *aY = wndRect.top;
203 void CBrowserFrame::BrowserFrameGlueObj::GetBrowserFramePositionAndSize(PRInt32 *aX, PRInt32 *aY, PRInt32 *aCX, PRInt32 *aCY)
205 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
207 RECT wndRect;
208 pThis->GetWindowRect(&wndRect);
210 if (aX)
211 *aX = wndRect.left;
213 if (aY)
214 *aY = wndRect.top;
216 if (aCX)
217 *aCX = wndRect.right - wndRect.left;
219 if (aCY)
220 *aCY = wndRect.bottom - wndRect.top;
223 void CBrowserFrame::BrowserFrameGlueObj::SetBrowserFramePositionAndSize(PRInt32 aX, PRInt32 aY, PRInt32 aCX, PRInt32 aCY, PRBool fRepaint)
225 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
227 pThis->SetWindowPos(NULL, aX, aY, aCX, aCY,
228 SWP_NOACTIVATE | SWP_NOZORDER);
231 void CBrowserFrame::BrowserFrameGlueObj::SetFocus()
233 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
235 pThis->SetFocus();
238 void CBrowserFrame::BrowserFrameGlueObj::FocusAvailable(PRBool *aFocusAvail)
240 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
242 HWND focusWnd = GetFocus()->m_hWnd;
244 if ((focusWnd == pThis->m_hWnd) || ::IsChild(pThis->m_hWnd, focusWnd))
245 *aFocusAvail = PR_TRUE;
246 else
247 *aFocusAvail = PR_FALSE;
250 void CBrowserFrame::BrowserFrameGlueObj::ShowBrowserFrame(PRBool aShow)
252 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
254 if(aShow)
256 pThis->ShowWindow(SW_SHOW);
257 pThis->SetActiveWindow();
258 pThis->UpdateWindow();
260 else
262 pThis->ShowWindow(SW_HIDE);
266 void CBrowserFrame::BrowserFrameGlueObj::GetBrowserFrameVisibility(PRBool *aVisible)
268 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
270 // Is the current BrowserFrame the active one?
271 if (GetActiveWindow()->m_hWnd != pThis->m_hWnd)
273 *aVisible = PR_FALSE;
274 return;
277 // We're the active one
278 //Return FALSE if we're minimized
279 WINDOWPLACEMENT wpl;
280 pThis->GetWindowPlacement(&wpl);
282 if ((wpl.showCmd == SW_RESTORE) || (wpl.showCmd == SW_MAXIMIZE))
283 *aVisible = PR_TRUE;
284 else
285 *aVisible = PR_FALSE;
288 PRBool CBrowserFrame::BrowserFrameGlueObj::CreateNewBrowserFrame(PRUint32 chromeMask,
289 PRInt32 x, PRInt32 y,
290 PRInt32 cx, PRInt32 cy,
291 nsIWebBrowser** aWebBrowser)
293 NS_ENSURE_ARG_POINTER(aWebBrowser);
295 *aWebBrowser = nsnull;
297 CTestEmbedApp *pApp = (CTestEmbedApp *)AfxGetApp();
298 if(!pApp)
299 return PR_FALSE;
301 // Note that we're calling with the last param set to "false" i.e.
302 // this instructs not to show the frame window
303 // This is mainly needed when the window size is specified in the window.open()
304 // JS call. In those cases Gecko calls us to create the browser with a default
305 // size (all are -1) and then it calls the SizeBrowserTo() method to set
306 // the proper window size. If this window were to be visible then you'll see
307 // the window size changes on the screen causing an unappealing flicker
310 CBrowserFrame* pFrm = pApp->CreateNewBrowserFrame(chromeMask, x, y, cx, cy, PR_FALSE);
311 if(!pFrm)
312 return PR_FALSE;
314 // At this stage we have a new CBrowserFrame and a new CBrowserView
315 // objects. The CBrowserView also would have an embedded browser
316 // object created. Get the mWebBrowser member from the CBrowserView
317 // and return it. (See CBrowserView's CreateBrowser() on how the
318 // embedded browser gets created and how it's mWebBrowser member
319 // gets initialized)
321 NS_IF_ADDREF(*aWebBrowser = pFrm->m_wndBrowserView.mWebBrowser);
323 return PR_TRUE;
326 void CBrowserFrame::BrowserFrameGlueObj::DestroyBrowserFrame()
328 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
330 pThis->PostMessage(WM_CLOSE);
333 #define GOTO_BUILD_CTX_MENU { bContentHasFrames = FALSE; goto BUILD_CTX_MENU; }
335 void CBrowserFrame::BrowserFrameGlueObj::ShowContextMenu(PRUint32 aContextFlags, nsIDOMNode *aNode)
337 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
339 UINT nIDResource = IDR_CTXMENU_DOCUMENT;
341 if(aContextFlags & nsIContextMenuListener::CONTEXT_DOCUMENT)
342 nIDResource = IDR_CTXMENU_DOCUMENT;
343 else if(aContextFlags & nsIContextMenuListener::CONTEXT_TEXT)
344 nIDResource = IDR_CTXMENU_TEXT;
345 else if(aContextFlags & nsIContextMenuListener::CONTEXT_LINK)
347 nIDResource = IDR_CTXMENU_LINK;
349 // Since we handle all the browser menu/toolbar commands
350 // in the View, we basically setup the Link's URL in the
351 // BrowserView object. When a menu selection in the context
352 // menu is made, the appropriate command handler in the
353 // BrowserView will be invoked and the value of the URL
354 // will be accesible in the view
356 // Reset the value from the last invocation
357 // (A new value will be set after we determine it below)
359 nsAutoString strUrlUcs2;
360 pThis->m_wndBrowserView.SetCtxMenuLinkUrl(strUrlUcs2);
362 // Get the URL from the link. This is two step process
363 // 1. We first get the nsIDOMHTMLAnchorElement
364 // 2. We then get the URL associated with the link
365 nsresult rv = NS_OK;
366 nsCOMPtr<nsIDOMHTMLAnchorElement> linkElement(do_QueryInterface(aNode, &rv));
367 if(NS_FAILED(rv))
368 return;
370 rv = linkElement->GetHref(strUrlUcs2);
371 if(NS_FAILED(rv))
372 return;
374 // Update the view with the new LinkUrl
375 // Note that this string is in UCS2 format
376 pThis->m_wndBrowserView.SetCtxMenuLinkUrl(strUrlUcs2);
378 else if(aContextFlags & nsIContextMenuListener::CONTEXT_IMAGE)
380 nIDResource = IDR_CTXMENU_IMAGE;
382 nsAutoString strImgSrcUcs2;
383 pThis->m_wndBrowserView.SetCtxMenuImageSrc(strImgSrcUcs2); // Clear it
385 // Get the IMG SRC
386 nsresult rv = NS_OK;
387 nsCOMPtr<nsIDOMHTMLImageElement> imgElement(do_QueryInterface(aNode, &rv));
388 if(NS_FAILED(rv))
389 return;
391 rv = imgElement->GetSrc(strImgSrcUcs2);
392 if(NS_FAILED(rv))
393 return;
395 pThis->m_wndBrowserView.SetCtxMenuImageSrc(strImgSrcUcs2); // Set the new Img Src
398 CMenu ctxMenu;
399 if(ctxMenu.LoadMenu(nIDResource))
401 POINT cursorPos;
402 GetCursorPos(&cursorPos);
404 (ctxMenu.GetSubMenu(0))->TrackPopupMenu(TPM_LEFTALIGN, cursorPos.x, cursorPos.y, pThis);
408 HWND CBrowserFrame::BrowserFrameGlueObj::GetBrowserFrameNativeWnd()
410 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
411 return pThis->m_hWnd;
414 void CBrowserFrame::BrowserFrameGlueObj::ShowTooltip(PRInt32 aXCoords, PRInt32 aYCoords, const PRUnichar *aTipText)
416 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
417 pThis->m_wndTooltip.SetTipText(CString(aTipText));
418 pThis->m_wndTooltip.Show(&pThis->m_wndBrowserView, aXCoords, aYCoords);
421 void CBrowserFrame::BrowserFrameGlueObj::HideTooltip()
423 METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
424 pThis->m_wndTooltip.Hide();