Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / widget / src / xpwidgets / nsNativeTheme.cpp
blob46a411ad7344cfd3e9fb25232170019fea56f48f
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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) 2002
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Brian Ryner <bryner@brianryner.com> (Original Author)
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 #include "nsNativeTheme.h"
40 #include "nsIDocument.h"
41 #include "nsIContent.h"
42 #include "nsIFrame.h"
43 #include "nsIPresShell.h"
44 #include "nsPresContext.h"
45 #include "nsIEventStateManager.h"
46 #include "nsString.h"
47 #include "nsINameSpaceManager.h"
48 #include "nsIDOMHTMLInputElement.h"
49 #include "nsILookAndFeel.h"
50 #include "nsThemeConstants.h"
51 #include "nsIComponentManager.h"
53 nsNativeTheme::nsNativeTheme()
57 nsIPresShell *
58 nsNativeTheme::GetPresShell(nsIFrame* aFrame)
60 if (!aFrame)
61 return nsnull;
63 // this is a workaround for the egcs 1.1.2 not inliningg
64 // aFrame->GetPresContext(), which causes an undefined symbol
65 nsPresContext *context = aFrame->GetStyleContext()->GetRuleNode()->GetPresContext();
66 return context ? context->GetPresShell() : nsnull;
69 PRInt32
70 nsNativeTheme::GetContentState(nsIFrame* aFrame, PRUint8 aWidgetType)
72 if (!aFrame)
73 return 0;
75 PRBool isXULCheckboxRadio =
76 (aWidgetType == NS_THEME_CHECKBOX ||
77 aWidgetType == NS_THEME_RADIO) &&
78 aFrame->GetContent()->IsNodeOfType(nsINode::eXUL);
79 if (isXULCheckboxRadio)
80 aFrame = aFrame->GetParent();
82 nsIPresShell *shell = GetPresShell(aFrame);
83 if (!shell)
84 return 0;
86 PRInt32 flags = 0;
87 shell->GetPresContext()->EventStateManager()->GetContentState(aFrame->GetContent(), flags);
89 if (isXULCheckboxRadio && aWidgetType == NS_THEME_RADIO) {
90 if (IsFocused(aFrame))
91 flags |= NS_EVENT_STATE_FOCUS;
94 return flags;
97 PRBool
98 nsNativeTheme::CheckBooleanAttr(nsIFrame* aFrame, nsIAtom* aAtom)
100 if (!aFrame)
101 return PR_FALSE;
103 nsIContent* content = aFrame->GetContent();
104 if (content->IsNodeOfType(nsINode::eHTML))
105 return content->HasAttr(kNameSpaceID_None, aAtom);
107 // For XML/XUL elements, an attribute must be equal to the literal
108 // string "true" to be counted as true. An empty string should _not_
109 // be counted as true.
110 return content->AttrValueIs(kNameSpaceID_None, aAtom,
111 NS_LITERAL_STRING("true"), eCaseMatters);
114 PRInt32
115 nsNativeTheme::CheckIntAttr(nsIFrame* aFrame, nsIAtom* aAtom, PRInt32 defaultValue)
117 if (!aFrame)
118 return defaultValue;
120 nsAutoString attr;
121 aFrame->GetContent()->GetAttr(kNameSpaceID_None, aAtom, attr);
122 PRInt32 err, value = attr.ToInteger(&err);
123 if (attr.IsEmpty() || NS_FAILED(err))
124 return defaultValue;
126 return value;
129 PRBool
130 nsNativeTheme::GetCheckedOrSelected(nsIFrame* aFrame, PRBool aCheckSelected)
132 if (!aFrame)
133 return PR_FALSE;
135 nsIContent* content = aFrame->GetContent();
137 if (content->IsNodeOfType(nsINode::eXUL)) {
138 // For a XUL checkbox or radio button, the state of the parent determines
139 // the checked state
140 aFrame = aFrame->GetParent();
141 } else {
142 // Check for an HTML input element
143 nsCOMPtr<nsIDOMHTMLInputElement> inputElt = do_QueryInterface(content);
144 if (inputElt) {
145 PRBool checked;
146 inputElt->GetChecked(&checked);
147 return checked;
151 return CheckBooleanAttr(aFrame, aCheckSelected ? nsWidgetAtoms::selected
152 : nsWidgetAtoms::checked);
155 PRBool
156 nsNativeTheme::IsWidgetStyled(nsPresContext* aPresContext, nsIFrame* aFrame,
157 PRUint8 aWidgetType)
159 // Check for specific widgets to see if HTML has overridden the style.
160 return aFrame &&
161 (aWidgetType == NS_THEME_BUTTON ||
162 aWidgetType == NS_THEME_TEXTFIELD ||
163 aWidgetType == NS_THEME_TEXTFIELD_MULTILINE ||
164 aWidgetType == NS_THEME_LISTBOX ||
165 aWidgetType == NS_THEME_DROPDOWN) &&
166 aFrame->GetContent()->IsNodeOfType(nsINode::eHTML) &&
167 aPresContext->HasAuthorSpecifiedRules(aFrame,
168 NS_AUTHOR_SPECIFIED_BORDER |
169 NS_AUTHOR_SPECIFIED_BACKGROUND);
172 PRBool
173 nsNativeTheme::IsFrameRTL(nsIFrame* aFrame)
175 return aFrame && aFrame->GetStyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL;
178 // scrollbar button:
179 PRInt32
180 nsNativeTheme::GetScrollbarButtonType(nsIFrame* aFrame)
182 if (!aFrame)
183 return 0;
185 static nsIContent::AttrValuesArray strings[] =
186 {&nsWidgetAtoms::scrollbarDownBottom, &nsWidgetAtoms::scrollbarDownTop,
187 &nsWidgetAtoms::scrollbarUpBottom, &nsWidgetAtoms::scrollbarUpTop,
188 nsnull};
190 switch (aFrame->GetContent()->FindAttrValueIn(kNameSpaceID_None,
191 nsWidgetAtoms::sbattr,
192 strings, eCaseMatters)) {
193 case 0: return eScrollbarButton_Down | eScrollbarButton_Bottom;
194 case 1: return eScrollbarButton_Down;
195 case 2: return eScrollbarButton_Bottom;
196 case 3: return eScrollbarButton_UpTop;
199 return 0;
202 // treeheadercell:
203 nsNativeTheme::TreeSortDirection
204 nsNativeTheme::GetTreeSortDirection(nsIFrame* aFrame)
206 if (!aFrame)
207 return eTreeSortDirection_Natural;
209 static nsIContent::AttrValuesArray strings[] =
210 {&nsWidgetAtoms::descending, &nsWidgetAtoms::ascending, nsnull};
211 switch (aFrame->GetContent()->FindAttrValueIn(kNameSpaceID_None,
212 nsWidgetAtoms::sortdirection,
213 strings, eCaseMatters)) {
214 case 0: return eTreeSortDirection_Descending;
215 case 1: return eTreeSortDirection_Ascending;
218 return eTreeSortDirection_Natural;
221 PRBool
222 nsNativeTheme::IsLastTreeHeaderCell(nsIFrame* aFrame)
224 if (!aFrame)
225 return PR_FALSE;
227 // A tree column picker is always the last header cell.
228 if (aFrame->GetContent()->Tag() == nsWidgetAtoms::treecolpicker)
229 return PR_TRUE;
231 // Find the parent tree.
232 nsIContent* parent = aFrame->GetContent()->GetParent();
233 while (parent && parent->Tag() != nsWidgetAtoms::tree) {
234 parent = parent->GetParent();
237 // If the column picker is visible, this can't be the last column.
238 if (parent && !parent->AttrValueIs(kNameSpaceID_None, nsWidgetAtoms::hidecolumnpicker,
239 NS_LITERAL_STRING("true"), eCaseMatters))
240 return PR_FALSE;
242 while ((aFrame = aFrame->GetNextSibling())) {
243 if (aFrame->GetRect().width > 0)
244 return PR_FALSE;
246 return PR_TRUE;
249 // tab:
250 PRBool
251 nsNativeTheme::IsBottomTab(nsIFrame* aFrame)
253 if (!aFrame)
254 return PR_FALSE;
256 nsAutoString classStr;
257 aFrame->GetContent()->GetAttr(kNameSpaceID_None, nsWidgetAtoms::_class, classStr);
258 return !classStr.IsEmpty() && classStr.Find("tab-bottom") != kNotFound;
261 PRBool
262 nsNativeTheme::IsFirstTab(nsIFrame* aFrame)
264 if (!aFrame)
265 return PR_FALSE;
267 return aFrame->GetContent()->HasAttr(kNameSpaceID_None, nsWidgetAtoms::firsttab);
270 PRBool
271 nsNativeTheme::IsLastTab(nsIFrame* aFrame)
273 if (!aFrame)
274 return PR_FALSE;
276 return aFrame->GetContent()->HasAttr(kNameSpaceID_None, nsWidgetAtoms::lasttab);
279 PRBool
280 nsNativeTheme::IsHorizontal(nsIFrame* aFrame)
282 if (!aFrame)
283 return PR_FALSE;
285 return !aFrame->GetContent()->AttrValueIs(kNameSpaceID_None, nsWidgetAtoms::orient,
286 nsWidgetAtoms::vertical,
287 eCaseMatters);
290 PRBool
291 nsNativeTheme::IsNextToSelectedTab(nsIFrame* aFrame, PRInt32 aOffset)
293 if (!aFrame)
294 return PR_FALSE;
296 if (aOffset == 0)
297 return IsSelectedTab(aFrame);
299 PRInt32 thisTabIndex = -1, selectedTabIndex = -1;
301 nsIFrame* currentTab = aFrame->GetParent()->GetFirstChild(NULL);
302 for (PRInt32 i = 0; currentTab; currentTab = currentTab->GetNextSibling()) {
303 if (currentTab->GetRect().width == 0)
304 continue;
305 if (aFrame == currentTab)
306 thisTabIndex = i;
307 if (IsSelectedTab(currentTab))
308 selectedTabIndex = i;
309 ++i;
312 if (thisTabIndex == -1 || selectedTabIndex == -1)
313 return PR_FALSE;
315 return (thisTabIndex - selectedTabIndex == aOffset);
318 // progressbar:
319 PRBool
320 nsNativeTheme::IsIndeterminateProgress(nsIFrame* aFrame)
322 if (!aFrame)
323 return PR_FALSE;
325 return aFrame->GetContent()->AttrValueIs(kNameSpaceID_None, nsWidgetAtoms::mode,
326 NS_LITERAL_STRING("undetermined"),
327 eCaseMatters);