CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / widget / src / shared / WidgetUtils.cpp
blob133a475cc1ef3e632fd1c29f6a1fd016328e766b
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is the Mozilla browser.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Stuart Parmenter <pavlov@netscape.com>
25 * Mike Pinkerton <pinkerton@netscape.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #include "WidgetUtils.h"
43 #include "nsIBaseWindow.h"
44 #include "nsIDocShellTreeItem.h"
45 #include "nsIDocShell.h"
46 #include "nsIInterfaceRequestorUtils.h"
48 namespace mozilla {
49 namespace widget {
51 //static
52 already_AddRefed<nsIWidget>
53 WidgetUtils::DOMWindowToWidget(nsIDOMWindow *aDOMWindow)
55 nsCOMPtr<nsIWidget> widget;
57 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aDOMWindow);
58 if (window) {
59 nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(window->GetDocShell()));
61 while (!widget && baseWin) {
62 baseWin->GetParentWidget(getter_AddRefs(widget));
63 if (!widget) {
64 nsCOMPtr<nsIDocShellTreeItem> docShellAsItem(do_QueryInterface(baseWin));
65 if (!docShellAsItem)
66 return nsnull;
68 nsCOMPtr<nsIDocShellTreeItem> parent;
69 docShellAsItem->GetParent(getter_AddRefs(parent));
71 window = do_GetInterface(parent);
72 if (!window)
73 return nsnull;
75 baseWin = do_QueryInterface(window->GetDocShell());
80 return widget.forget();
83 // class BrightnessLockingWidget
85 BrightnessLockingWidget::BrightnessLockingWidget()
87 for (PRUint32 i = 0; i < nsIScreen_MOZILLA_2_0_BRANCH::BRIGHTNESS_LEVELS; i++)
88 mBrightnessLocks[i] = 0;
91 NS_IMETHODIMP
92 BrightnessLockingWidget::LockMinimumBrightness(PRUint32 aBrightness)
94 NS_ABORT_IF_FALSE(
95 aBrightness < nsIScreen_MOZILLA_2_0_BRANCH::BRIGHTNESS_LEVELS,
96 "Invalid brightness level to lock");
97 mBrightnessLocks[aBrightness]++;
98 NS_ABORT_IF_FALSE(mBrightnessLocks[aBrightness] > 0,
99 "Overflow after locking brightness level");
101 CheckMinimumBrightness();
103 return NS_OK;
106 NS_IMETHODIMP
107 BrightnessLockingWidget::UnlockMinimumBrightness(PRUint32 aBrightness)
109 NS_ABORT_IF_FALSE(
110 aBrightness < nsIScreen_MOZILLA_2_0_BRANCH::BRIGHTNESS_LEVELS,
111 "Invalid brightness level to lock");
112 NS_ABORT_IF_FALSE(mBrightnessLocks[aBrightness] > 0,
113 "Unlocking a brightness level with no corresponding lock");
114 mBrightnessLocks[aBrightness]--;
116 CheckMinimumBrightness();
118 return NS_OK;
121 void
122 BrightnessLockingWidget::CheckMinimumBrightness()
124 PRUint32 brightness = nsIScreen_MOZILLA_2_0_BRANCH::BRIGHTNESS_LEVELS;
125 for (PRUint32 i = 0; i < nsIScreen_MOZILLA_2_0_BRANCH::BRIGHTNESS_LEVELS; i++)
126 if (mBrightnessLocks[i] > 0)
127 brightness = i;
129 ApplyMinimumBrightness(brightness);
132 } // namespace widget
133 } // namespace mozilla