Back to index

lightning-sunbird  0.9+nobinonly
nsBarProps.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Travis Bogard <travis@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsCOMPtr.h"
00040 #include "nscore.h"
00041 #include "nsBarProps.h"
00042 #include "nsGlobalWindow.h"
00043 #include "nsStyleConsts.h"
00044 #include "nsIDocShell.h"
00045 #include "nsIScriptSecurityManager.h"
00046 #include "nsIScrollable.h"
00047 #include "nsIWebBrowserChrome.h"
00048 #include "nsIDOMWindowInternal.h"
00049 #include "nsDOMClassInfo.h"
00050 
00051 //
00052 //  Basic (virtual) BarProp class implementation
00053 //
00054 nsBarProp::nsBarProp() : mBrowserChrome(nsnull)
00055 {
00056 }
00057 
00058 nsBarProp::~nsBarProp()
00059 {
00060 }
00061 
00062 
00063 // QueryInterface implementation for BarProp
00064 NS_INTERFACE_MAP_BEGIN(nsBarProp)
00065   NS_INTERFACE_MAP_ENTRY(nsIDOMBarProp)
00066   NS_INTERFACE_MAP_ENTRY(nsISupports)
00067   NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(BarProp)
00068 NS_INTERFACE_MAP_END
00069 
00070 
00071 NS_IMPL_ADDREF(nsBarProp)
00072 NS_IMPL_RELEASE(nsBarProp)
00073 
00074 
00075 NS_IMETHODIMP
00076 nsBarProp::SetWebBrowserChrome(nsIWebBrowserChrome* aBrowserChrome)
00077 {
00078   mBrowserChrome = aBrowserChrome;
00079   return NS_OK;
00080 }
00081 
00082 NS_IMETHODIMP
00083 nsBarProp::GetVisibleByFlag(PRBool *aVisible, PRUint32 aChromeFlag)
00084 {
00085   NS_ENSURE_TRUE(mBrowserChrome, NS_ERROR_FAILURE);
00086 
00087   PRUint32 chromeFlags;
00088   *aVisible = PR_FALSE;
00089 
00090   NS_ENSURE_SUCCESS(mBrowserChrome->GetChromeFlags(&chromeFlags),
00091                     NS_ERROR_FAILURE);
00092   if(chromeFlags & aChromeFlag)
00093     *aVisible = PR_TRUE;
00094 
00095   return NS_OK;
00096 }
00097 
00098 NS_IMETHODIMP
00099 nsBarProp::SetVisibleByFlag(PRBool aVisible, PRUint32 aChromeFlag)
00100 {
00101   NS_ENSURE_TRUE(mBrowserChrome, NS_ERROR_FAILURE);
00102 
00103   PRBool   enabled = PR_FALSE;
00104 
00105   nsCOMPtr<nsIScriptSecurityManager>
00106            securityManager(do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID));
00107   if (securityManager)
00108     securityManager->IsCapabilityEnabled("UniversalBrowserWrite", &enabled);
00109   if (!enabled)
00110     return NS_OK;
00111 
00112   PRUint32 chromeFlags;
00113 
00114   NS_ENSURE_SUCCESS(mBrowserChrome->GetChromeFlags(&chromeFlags),
00115                     NS_ERROR_FAILURE);
00116   if(aVisible)
00117     chromeFlags |= aChromeFlag;
00118   else
00119     chromeFlags &= ~aChromeFlag;
00120   NS_ENSURE_SUCCESS(mBrowserChrome->SetChromeFlags(chromeFlags),
00121                     NS_ERROR_FAILURE);
00122 
00123   return NS_OK;
00124 }
00125 
00126 //
00127 // MenubarProp class implementation
00128 //
00129 
00130 nsMenubarProp::nsMenubarProp()
00131 {
00132 }
00133 
00134 nsMenubarProp::~nsMenubarProp()
00135 {
00136 }
00137 
00138 NS_IMETHODIMP
00139 nsMenubarProp::GetVisible(PRBool *aVisible)
00140 {
00141   return nsBarProp::GetVisibleByFlag(aVisible,
00142                                      nsIWebBrowserChrome::CHROME_MENUBAR);
00143 }
00144 
00145 NS_IMETHODIMP
00146 nsMenubarProp::SetVisible(PRBool aVisible)
00147 {
00148   return nsBarProp::SetVisibleByFlag(aVisible,
00149                                      nsIWebBrowserChrome::CHROME_MENUBAR);
00150 }
00151 
00152 //
00153 // ToolbarProp class implementation
00154 //
00155 
00156 nsToolbarProp::nsToolbarProp()
00157 {
00158 }
00159 
00160 nsToolbarProp::~nsToolbarProp()
00161 {
00162 }
00163 
00164 NS_IMETHODIMP
00165 nsToolbarProp::GetVisible(PRBool *aVisible)
00166 {
00167   return nsBarProp::GetVisibleByFlag(aVisible,
00168                                      nsIWebBrowserChrome::CHROME_TOOLBAR);
00169 }
00170 
00171 NS_IMETHODIMP
00172 nsToolbarProp::SetVisible(PRBool aVisible)
00173 {
00174   return nsBarProp::SetVisibleByFlag(aVisible,
00175                                      nsIWebBrowserChrome::CHROME_TOOLBAR);
00176 }
00177 
00178 //
00179 // LocationbarProp class implementation
00180 //
00181 
00182 nsLocationbarProp::nsLocationbarProp()
00183 {
00184 }
00185 
00186 nsLocationbarProp::~nsLocationbarProp()
00187 {
00188 }
00189 
00190 NS_IMETHODIMP
00191 nsLocationbarProp::GetVisible(PRBool *aVisible)
00192 {
00193   return
00194     nsBarProp::GetVisibleByFlag(aVisible,
00195                                 nsIWebBrowserChrome::CHROME_LOCATIONBAR);
00196 }
00197 
00198 NS_IMETHODIMP
00199 nsLocationbarProp::SetVisible(PRBool aVisible)
00200 {
00201   return
00202     nsBarProp::SetVisibleByFlag(aVisible,
00203                                 nsIWebBrowserChrome::CHROME_LOCATIONBAR);
00204 }
00205 
00206 //
00207 // PersonalbarProp class implementation
00208 //
00209 
00210 nsPersonalbarProp::nsPersonalbarProp()
00211 {
00212 }
00213 
00214 nsPersonalbarProp::~nsPersonalbarProp()
00215 {
00216 }
00217 
00218 NS_IMETHODIMP
00219 nsPersonalbarProp::GetVisible(PRBool *aVisible)
00220 {
00221   return
00222     nsBarProp::GetVisibleByFlag(aVisible,
00223                                 nsIWebBrowserChrome::CHROME_PERSONAL_TOOLBAR);
00224 }
00225 
00226 NS_IMETHODIMP
00227 nsPersonalbarProp::SetVisible(PRBool aVisible)
00228 {
00229   return
00230     nsBarProp::SetVisibleByFlag(aVisible,
00231                                 nsIWebBrowserChrome::CHROME_PERSONAL_TOOLBAR);
00232 }
00233 
00234 //
00235 // StatusbarProp class implementation
00236 //
00237 
00238 nsStatusbarProp::nsStatusbarProp()
00239 {
00240 }
00241 
00242 nsStatusbarProp::~nsStatusbarProp()
00243 {
00244 }
00245 
00246 NS_IMETHODIMP
00247 nsStatusbarProp::GetVisible(PRBool *aVisible)
00248 {
00249   return nsBarProp::GetVisibleByFlag(aVisible,
00250                                      nsIWebBrowserChrome::CHROME_STATUSBAR);
00251 }
00252 
00253 NS_IMETHODIMP
00254 nsStatusbarProp::SetVisible(PRBool aVisible)
00255 {
00256   return nsBarProp::SetVisibleByFlag(aVisible,
00257                                      nsIWebBrowserChrome::CHROME_STATUSBAR);
00258 }
00259 
00260 //
00261 // ScrollbarsProp class implementation
00262 //
00263 
00264 nsScrollbarsProp::nsScrollbarsProp(nsGlobalWindow *aWindow)
00265 {
00266   mDOMWindow = aWindow;
00267   nsISupports *supwin = NS_STATIC_CAST(nsIScriptGlobalObject *, aWindow);
00268   mDOMWindowWeakref = do_GetWeakReference(supwin);
00269 }
00270 
00271 nsScrollbarsProp::~nsScrollbarsProp()
00272 {
00273 }
00274 
00275 NS_IMETHODIMP
00276 nsScrollbarsProp::GetVisible(PRBool *aVisible)
00277 {
00278   *aVisible = PR_TRUE; // one assumes
00279 
00280   nsCOMPtr<nsIDOMWindow> domwin(do_QueryReferent(mDOMWindowWeakref));
00281   if (domwin) { // dom window not deleted
00282     nsCOMPtr<nsIScrollable> scroller =
00283       do_QueryInterface(mDOMWindow->GetDocShell());
00284 
00285     if (scroller) {
00286       PRInt32 prefValue;
00287       scroller->GetDefaultScrollbarPreferences(
00288                   nsIScrollable::ScrollOrientation_Y, &prefValue);
00289       if (prefValue == nsIScrollable::Scrollbar_Never) // try the other way
00290         scroller->GetDefaultScrollbarPreferences(
00291                     nsIScrollable::ScrollOrientation_X, &prefValue);
00292 
00293       if (prefValue == nsIScrollable::Scrollbar_Never)
00294         *aVisible = PR_FALSE;
00295     }
00296   }
00297 
00298   return NS_OK;
00299 }
00300 
00301 NS_IMETHODIMP
00302 nsScrollbarsProp::SetVisible(PRBool aVisible)
00303 {
00304   PRBool   enabled = PR_FALSE;
00305 
00306   nsCOMPtr<nsIScriptSecurityManager>
00307            securityManager(do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID));
00308   if (securityManager)
00309     securityManager->IsCapabilityEnabled("UniversalBrowserWrite", &enabled);
00310   if (!enabled)
00311     return NS_OK;
00312 
00313   /* Scrollbars, unlike the other barprops, implement visibility directly
00314      rather than handing off to the superclass (and from there to the
00315      chrome window) because scrollbar visibility uniquely applies only
00316      to the window making the change (arguably. it does now, anyway.)
00317      and because embedding apps have no interface for implementing this
00318      themselves, and therefore the implementation must be internal. */
00319 
00320   nsCOMPtr<nsIDOMWindow> domwin(do_QueryReferent(mDOMWindowWeakref));
00321   if (domwin) { // dom window must still exist. use away.
00322     nsCOMPtr<nsIScrollable> scroller =
00323       do_QueryInterface(mDOMWindow->GetDocShell());
00324 
00325     if (scroller) {
00326       PRInt32 prefValue;
00327 
00328       if (aVisible) {
00329         prefValue = nsIScrollable::Scrollbar_Auto;
00330       } else {
00331         prefValue = nsIScrollable::Scrollbar_Never;
00332       }
00333 
00334       scroller->SetDefaultScrollbarPreferences(
00335                   nsIScrollable::ScrollOrientation_Y, prefValue);
00336       scroller->SetDefaultScrollbarPreferences(
00337                   nsIScrollable::ScrollOrientation_X, prefValue);
00338     }
00339   }
00340 
00341   /* Notably absent is the part where we notify the chrome window using
00342      mBrowserChrome->SetChromeFlags(). Given the possibility of multiple
00343      DOM windows (multiple top-level windows, even) within a single
00344      chrome window, the historical concept of a single "has scrollbars"
00345      flag in the chrome is inapplicable, and we can't tell at this level
00346      whether we represent the particular DOM window that makes this decision
00347      for the chrome.
00348 
00349      So only this object (and its corresponding DOM window) knows whether
00350      scrollbars are visible. The corresponding chrome window will need to
00351      ask (one of) its DOM window(s) when it needs to know about scrollbar
00352      visibility, rather than caching its own copy of that information.
00353   */
00354 
00355   return NS_OK;
00356 }
00357