michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsNativeTheme.h" michael@0: #include "nsIWidget.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsIContent.h" michael@0: #include "nsIFrame.h" michael@0: #include "nsIPresShell.h" michael@0: #include "nsNumberControlFrame.h" michael@0: #include "nsPresContext.h" michael@0: #include "nsString.h" michael@0: #include "nsNameSpaceManager.h" michael@0: #include "nsIDOMHTMLInputElement.h" michael@0: #include "nsIDOMXULMenuListElement.h" michael@0: #include "nsThemeConstants.h" michael@0: #include "nsIComponentManager.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsProgressFrame.h" michael@0: #include "nsMeterFrame.h" michael@0: #include "nsMenuFrame.h" michael@0: #include "nsRangeFrame.h" michael@0: #include "nsCSSRendering.h" michael@0: #include "mozilla/EventStates.h" michael@0: #include "mozilla/dom/Element.h" michael@0: #include "mozilla/dom/HTMLBodyElement.h" michael@0: #include "mozilla/dom/HTMLProgressElement.h" michael@0: #include "nsIDocumentInlines.h" michael@0: #include michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::dom; michael@0: michael@0: nsNativeTheme::nsNativeTheme() michael@0: : mAnimatedContentTimeout(UINT32_MAX) michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsNativeTheme, nsITimerCallback) michael@0: michael@0: nsIPresShell * michael@0: nsNativeTheme::GetPresShell(nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame) michael@0: return nullptr; michael@0: michael@0: // this is a workaround for the egcs 1.1.2 not inlining michael@0: // aFrame->PresContext(), which causes an undefined symbol michael@0: nsPresContext *context = aFrame->StyleContext()->RuleNode()->PresContext(); michael@0: return context ? context->GetPresShell() : nullptr; michael@0: } michael@0: michael@0: EventStates michael@0: nsNativeTheme::GetContentState(nsIFrame* aFrame, uint8_t aWidgetType) michael@0: { michael@0: if (!aFrame) michael@0: return EventStates(); michael@0: michael@0: bool isXULCheckboxRadio = michael@0: (aWidgetType == NS_THEME_CHECKBOX || michael@0: aWidgetType == NS_THEME_RADIO) && michael@0: aFrame->GetContent()->IsXUL(); michael@0: if (isXULCheckboxRadio) michael@0: aFrame = aFrame->GetParent(); michael@0: michael@0: if (!aFrame->GetContent()) michael@0: return EventStates(); michael@0: michael@0: nsIPresShell *shell = GetPresShell(aFrame); michael@0: if (!shell) michael@0: return EventStates(); michael@0: michael@0: nsIContent* frameContent = aFrame->GetContent(); michael@0: EventStates flags; michael@0: if (frameContent->IsElement()) { michael@0: flags = frameContent->AsElement()->State(); michael@0: michael@0: // needs special handling since its nested native michael@0: // anonymous takes focus for it. michael@0: if (aWidgetType == NS_THEME_NUMBER_INPUT && michael@0: frameContent->IsHTML(nsGkAtoms::input)) { michael@0: nsNumberControlFrame *numberControlFrame = do_QueryFrame(aFrame); michael@0: if (numberControlFrame && numberControlFrame->IsFocused()) { michael@0: flags |= NS_EVENT_STATE_FOCUS; michael@0: } michael@0: } michael@0: michael@0: nsNumberControlFrame* numberControlFrame = michael@0: nsNumberControlFrame::GetNumberControlFrameForSpinButton(aFrame); michael@0: if (numberControlFrame && michael@0: numberControlFrame->GetContent()->AsElement()->State(). michael@0: HasState(NS_EVENT_STATE_DISABLED)) { michael@0: flags |= NS_EVENT_STATE_DISABLED; michael@0: } michael@0: } michael@0: michael@0: if (isXULCheckboxRadio && aWidgetType == NS_THEME_RADIO) { michael@0: if (IsFocused(aFrame)) michael@0: flags |= NS_EVENT_STATE_FOCUS; michael@0: } michael@0: michael@0: // On Windows and Mac, only draw focus rings if they should be shown. This michael@0: // means that focus rings are only shown once the keyboard has been used to michael@0: // focus something in the window. michael@0: #if defined(XP_MACOSX) michael@0: // Mac always draws focus rings for textboxes and lists. michael@0: if (aWidgetType == NS_THEME_NUMBER_INPUT || michael@0: aWidgetType == NS_THEME_TEXTFIELD || michael@0: aWidgetType == NS_THEME_TEXTFIELD_MULTILINE || michael@0: aWidgetType == NS_THEME_SEARCHFIELD || michael@0: aWidgetType == NS_THEME_LISTBOX) { michael@0: return flags; michael@0: } michael@0: #endif michael@0: #if defined(XP_WIN) michael@0: // On Windows, focused buttons are always drawn as such by the native theme. michael@0: if (aWidgetType == NS_THEME_BUTTON) michael@0: return flags; michael@0: #endif michael@0: #if defined(XP_MACOSX) || defined(XP_WIN) michael@0: nsIDocument* doc = aFrame->GetContent()->OwnerDoc(); michael@0: nsPIDOMWindow* window = doc->GetWindow(); michael@0: if (window && !window->ShouldShowFocusRing()) michael@0: flags &= ~NS_EVENT_STATE_FOCUS; michael@0: #endif michael@0: michael@0: return flags; michael@0: } michael@0: michael@0: /* static */ michael@0: bool michael@0: nsNativeTheme::CheckBooleanAttr(nsIFrame* aFrame, nsIAtom* aAtom) michael@0: { michael@0: if (!aFrame) michael@0: return false; michael@0: michael@0: nsIContent* content = aFrame->GetContent(); michael@0: if (!content) michael@0: return false; michael@0: michael@0: if (content->IsHTML()) michael@0: return content->HasAttr(kNameSpaceID_None, aAtom); michael@0: michael@0: // For XML/XUL elements, an attribute must be equal to the literal michael@0: // string "true" to be counted as true. An empty string should _not_ michael@0: // be counted as true. michael@0: return content->AttrValueIs(kNameSpaceID_None, aAtom, michael@0: NS_LITERAL_STRING("true"), eCaseMatters); michael@0: } michael@0: michael@0: /* static */ michael@0: int32_t michael@0: nsNativeTheme::CheckIntAttr(nsIFrame* aFrame, nsIAtom* aAtom, int32_t defaultValue) michael@0: { michael@0: if (!aFrame) michael@0: return defaultValue; michael@0: michael@0: nsAutoString attr; michael@0: aFrame->GetContent()->GetAttr(kNameSpaceID_None, aAtom, attr); michael@0: nsresult err; michael@0: int32_t value = attr.ToInteger(&err); michael@0: if (attr.IsEmpty() || NS_FAILED(err)) michael@0: return defaultValue; michael@0: michael@0: return value; michael@0: } michael@0: michael@0: /* static */ michael@0: double michael@0: nsNativeTheme::GetProgressValue(nsIFrame* aFrame) michael@0: { michael@0: // When we are using the HTML progress element, michael@0: // we can get the value from the IDL property. michael@0: if (aFrame && aFrame->GetContent()->IsHTML(nsGkAtoms::progress)) { michael@0: return static_cast(aFrame->GetContent())->Value(); michael@0: } michael@0: michael@0: return (double)nsNativeTheme::CheckIntAttr(aFrame, nsGkAtoms::value, 0); michael@0: } michael@0: michael@0: /* static */ michael@0: double michael@0: nsNativeTheme::GetProgressMaxValue(nsIFrame* aFrame) michael@0: { michael@0: // When we are using the HTML progress element, michael@0: // we can get the max from the IDL property. michael@0: if (aFrame && aFrame->GetContent()->IsHTML(nsGkAtoms::progress)) { michael@0: return static_cast(aFrame->GetContent())->Max(); michael@0: } michael@0: michael@0: return (double)std::max(nsNativeTheme::CheckIntAttr(aFrame, nsGkAtoms::max, 100), 1); michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::GetCheckedOrSelected(nsIFrame* aFrame, bool aCheckSelected) michael@0: { michael@0: if (!aFrame) michael@0: return false; michael@0: michael@0: nsIContent* content = aFrame->GetContent(); michael@0: michael@0: if (content->IsXUL()) { michael@0: // For a XUL checkbox or radio button, the state of the parent determines michael@0: // the checked state michael@0: aFrame = aFrame->GetParent(); michael@0: } else { michael@0: // Check for an HTML input element michael@0: nsCOMPtr inputElt = do_QueryInterface(content); michael@0: if (inputElt) { michael@0: bool checked; michael@0: inputElt->GetChecked(&checked); michael@0: return checked; michael@0: } michael@0: } michael@0: michael@0: return CheckBooleanAttr(aFrame, aCheckSelected ? nsGkAtoms::selected michael@0: : nsGkAtoms::checked); michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::IsButtonTypeMenu(nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame) michael@0: return false; michael@0: michael@0: nsIContent* content = aFrame->GetContent(); michael@0: return content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type, michael@0: NS_LITERAL_STRING("menu"), eCaseMatters); michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::IsPressedButton(nsIFrame* aFrame) michael@0: { michael@0: EventStates eventState = GetContentState(aFrame, NS_THEME_TOOLBAR_BUTTON); michael@0: if (IsDisabled(aFrame, eventState)) michael@0: return false; michael@0: michael@0: return IsOpenButton(aFrame) || michael@0: eventState.HasAllStates(NS_EVENT_STATE_ACTIVE | NS_EVENT_STATE_HOVER); michael@0: } michael@0: michael@0: michael@0: bool michael@0: nsNativeTheme::GetIndeterminate(nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame) michael@0: return false; michael@0: michael@0: nsIContent* content = aFrame->GetContent(); michael@0: michael@0: if (content->IsXUL()) { michael@0: // For a XUL checkbox or radio button, the state of the parent determines michael@0: // the state michael@0: return CheckBooleanAttr(aFrame->GetParent(), nsGkAtoms::indeterminate); michael@0: } michael@0: michael@0: // Check for an HTML input element michael@0: nsCOMPtr inputElt = do_QueryInterface(content); michael@0: if (inputElt) { michael@0: bool indeterminate; michael@0: inputElt->GetIndeterminate(&indeterminate); michael@0: return indeterminate; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::IsWidgetStyled(nsPresContext* aPresContext, nsIFrame* aFrame, michael@0: uint8_t aWidgetType) michael@0: { michael@0: // Check for specific widgets to see if HTML has overridden the style. michael@0: if (!aFrame) michael@0: return false; michael@0: michael@0: // Resizers have some special handling, dependent on whether in a scrollable michael@0: // container or not. If so, use the scrollable container's to determine michael@0: // whether the style is overriden instead of the resizer. This allows a michael@0: // non-native transparent resizer to be used instead. Otherwise, we just michael@0: // fall through and return false. michael@0: if (aWidgetType == NS_THEME_RESIZER) { michael@0: nsIFrame* parentFrame = aFrame->GetParent(); michael@0: if (parentFrame && parentFrame->GetType() == nsGkAtoms::scrollFrame) { michael@0: // if the parent is a scrollframe, the resizer should be native themed michael@0: // only if the scrollable area doesn't override the widget style. michael@0: parentFrame = parentFrame->GetParent(); michael@0: if (parentFrame) { michael@0: return IsWidgetStyled(aPresContext, parentFrame, michael@0: parentFrame->StyleDisplay()->mAppearance); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Progress bar appearance should be the same for the bar and the container michael@0: * frame. nsProgressFrame owns the logic and will tell us what we should do. michael@0: */ michael@0: if (aWidgetType == NS_THEME_PROGRESSBAR_CHUNK || michael@0: aWidgetType == NS_THEME_PROGRESSBAR) { michael@0: nsProgressFrame* progressFrame = do_QueryFrame(aWidgetType == NS_THEME_PROGRESSBAR_CHUNK michael@0: ? aFrame->GetParent() : aFrame); michael@0: if (progressFrame) { michael@0: return !progressFrame->ShouldUseNativeStyle(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Meter bar appearance should be the same for the bar and the container michael@0: * frame. nsMeterFrame owns the logic and will tell us what we should do. michael@0: */ michael@0: if (aWidgetType == NS_THEME_METERBAR_CHUNK || michael@0: aWidgetType == NS_THEME_METERBAR) { michael@0: nsMeterFrame* meterFrame = do_QueryFrame(aWidgetType == NS_THEME_METERBAR_CHUNK michael@0: ? aFrame->GetParent() : aFrame); michael@0: if (meterFrame) { michael@0: return !meterFrame->ShouldUseNativeStyle(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * An nsRangeFrame and its children are treated atomically when it michael@0: * comes to native theming (either all parts, or no parts, are themed). michael@0: * nsRangeFrame owns the logic and will tell us what we should do. michael@0: */ michael@0: if (aWidgetType == NS_THEME_RANGE || michael@0: aWidgetType == NS_THEME_RANGE_THUMB) { michael@0: nsRangeFrame* rangeFrame = michael@0: do_QueryFrame(aWidgetType == NS_THEME_RANGE_THUMB michael@0: ? aFrame->GetParent() : aFrame); michael@0: if (rangeFrame) { michael@0: return !rangeFrame->ShouldUseNativeStyle(); michael@0: } michael@0: } michael@0: michael@0: if (aWidgetType == NS_THEME_SPINNER_UP_BUTTON || michael@0: aWidgetType == NS_THEME_SPINNER_DOWN_BUTTON) { michael@0: nsNumberControlFrame* numberControlFrame = michael@0: nsNumberControlFrame::GetNumberControlFrameForSpinButton(aFrame); michael@0: if (numberControlFrame) { michael@0: return !numberControlFrame->ShouldUseNativeStyleForSpinner(); michael@0: } michael@0: } michael@0: michael@0: return (aWidgetType == NS_THEME_NUMBER_INPUT || michael@0: aWidgetType == NS_THEME_BUTTON || michael@0: aWidgetType == NS_THEME_TEXTFIELD || michael@0: aWidgetType == NS_THEME_TEXTFIELD_MULTILINE || michael@0: aWidgetType == NS_THEME_LISTBOX || michael@0: aWidgetType == NS_THEME_DROPDOWN) && michael@0: aFrame->GetContent()->IsHTML() && michael@0: aPresContext->HasAuthorSpecifiedRules(aFrame, michael@0: NS_AUTHOR_SPECIFIED_BORDER | michael@0: NS_AUTHOR_SPECIFIED_BACKGROUND); michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::IsDisabled(nsIFrame* aFrame, EventStates aEventStates) michael@0: { michael@0: if (!aFrame) { michael@0: return false; michael@0: } michael@0: michael@0: nsIContent* content = aFrame->GetContent(); michael@0: if (!content) { michael@0: return false; michael@0: } michael@0: michael@0: if (content->IsHTML()) { michael@0: return aEventStates.HasState(NS_EVENT_STATE_DISABLED); michael@0: } michael@0: michael@0: // For XML/XUL elements, an attribute must be equal to the literal michael@0: // string "true" to be counted as true. An empty string should _not_ michael@0: // be counted as true. michael@0: return content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled, michael@0: NS_LITERAL_STRING("true"), eCaseMatters); michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::IsFrameRTL(nsIFrame* aFrame) michael@0: { michael@0: return aFrame && aFrame->StyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL; michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::IsHTMLContent(nsIFrame *aFrame) michael@0: { michael@0: if (!aFrame) { michael@0: return false; michael@0: } michael@0: nsIContent* content = aFrame->GetContent(); michael@0: return content && content->IsHTML(); michael@0: } michael@0: michael@0: michael@0: // scrollbar button: michael@0: int32_t michael@0: nsNativeTheme::GetScrollbarButtonType(nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame) michael@0: return 0; michael@0: michael@0: static nsIContent::AttrValuesArray strings[] = michael@0: {&nsGkAtoms::scrollbarDownBottom, &nsGkAtoms::scrollbarDownTop, michael@0: &nsGkAtoms::scrollbarUpBottom, &nsGkAtoms::scrollbarUpTop, michael@0: nullptr}; michael@0: michael@0: switch (aFrame->GetContent()->FindAttrValueIn(kNameSpaceID_None, michael@0: nsGkAtoms::sbattr, michael@0: strings, eCaseMatters)) { michael@0: case 0: return eScrollbarButton_Down | eScrollbarButton_Bottom; michael@0: case 1: return eScrollbarButton_Down; michael@0: case 2: return eScrollbarButton_Bottom; michael@0: case 3: return eScrollbarButton_UpTop; michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: // treeheadercell: michael@0: nsNativeTheme::TreeSortDirection michael@0: nsNativeTheme::GetTreeSortDirection(nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame || !aFrame->GetContent()) michael@0: return eTreeSortDirection_Natural; michael@0: michael@0: static nsIContent::AttrValuesArray strings[] = michael@0: {&nsGkAtoms::descending, &nsGkAtoms::ascending, nullptr}; michael@0: switch (aFrame->GetContent()->FindAttrValueIn(kNameSpaceID_None, michael@0: nsGkAtoms::sortDirection, michael@0: strings, eCaseMatters)) { michael@0: case 0: return eTreeSortDirection_Descending; michael@0: case 1: return eTreeSortDirection_Ascending; michael@0: } michael@0: michael@0: return eTreeSortDirection_Natural; michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::IsLastTreeHeaderCell(nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame) michael@0: return false; michael@0: michael@0: // A tree column picker is always the last header cell. michael@0: if (aFrame->GetContent()->Tag() == nsGkAtoms::treecolpicker) michael@0: return true; michael@0: michael@0: // Find the parent tree. michael@0: nsIContent* parent = aFrame->GetContent()->GetParent(); michael@0: while (parent && parent->Tag() != nsGkAtoms::tree) { michael@0: parent = parent->GetParent(); michael@0: } michael@0: michael@0: // If the column picker is visible, this can't be the last column. michael@0: if (parent && !parent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::hidecolumnpicker, michael@0: NS_LITERAL_STRING("true"), eCaseMatters)) michael@0: return false; michael@0: michael@0: while ((aFrame = aFrame->GetNextSibling())) { michael@0: if (aFrame->GetRect().width > 0) michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: // tab: michael@0: bool michael@0: nsNativeTheme::IsBottomTab(nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame) michael@0: return false; michael@0: michael@0: nsAutoString classStr; michael@0: aFrame->GetContent()->GetAttr(kNameSpaceID_None, nsGkAtoms::_class, classStr); michael@0: return !classStr.IsEmpty() && classStr.Find("tab-bottom") != kNotFound; michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::IsFirstTab(nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame) michael@0: return false; michael@0: michael@0: nsIFrame* first = aFrame->GetParent()->GetFirstPrincipalChild(); michael@0: while (first) { michael@0: if (first->GetRect().width > 0 && first->GetContent()->Tag() == nsGkAtoms::tab) michael@0: return (first == aFrame); michael@0: first = first->GetNextSibling(); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::IsHorizontal(nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame) michael@0: return false; michael@0: michael@0: return !aFrame->GetContent()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::orient, michael@0: nsGkAtoms::vertical, michael@0: eCaseMatters); michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::IsNextToSelectedTab(nsIFrame* aFrame, int32_t aOffset) michael@0: { michael@0: if (!aFrame) michael@0: return false; michael@0: michael@0: if (aOffset == 0) michael@0: return IsSelectedTab(aFrame); michael@0: michael@0: int32_t thisTabIndex = -1, selectedTabIndex = -1; michael@0: michael@0: nsIFrame* currentTab = aFrame->GetParent()->GetFirstPrincipalChild(); michael@0: for (int32_t i = 0; currentTab; currentTab = currentTab->GetNextSibling()) { michael@0: if (currentTab->GetRect().width == 0) michael@0: continue; michael@0: if (aFrame == currentTab) michael@0: thisTabIndex = i; michael@0: if (IsSelectedTab(currentTab)) michael@0: selectedTabIndex = i; michael@0: ++i; michael@0: } michael@0: michael@0: if (thisTabIndex == -1 || selectedTabIndex == -1) michael@0: return false; michael@0: michael@0: return (thisTabIndex - selectedTabIndex == aOffset); michael@0: } michael@0: michael@0: // progressbar: michael@0: bool michael@0: nsNativeTheme::IsIndeterminateProgress(nsIFrame* aFrame, michael@0: EventStates aEventStates) michael@0: { michael@0: if (!aFrame || !aFrame->GetContent()) michael@0: return false; michael@0: michael@0: if (aFrame->GetContent()->IsHTML(nsGkAtoms::progress)) { michael@0: return aEventStates.HasState(NS_EVENT_STATE_INDETERMINATE); michael@0: } michael@0: michael@0: return aFrame->GetContent()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::mode, michael@0: NS_LITERAL_STRING("undetermined"), michael@0: eCaseMatters); michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::IsVerticalProgress(nsIFrame* aFrame) michael@0: { michael@0: return aFrame && michael@0: aFrame->StyleDisplay()->mOrient == NS_STYLE_ORIENT_VERTICAL; michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::IsVerticalMeter(nsIFrame* aFrame) michael@0: { michael@0: NS_PRECONDITION(aFrame, "You have to pass a non-null aFrame"); michael@0: return aFrame->StyleDisplay()->mOrient == NS_STYLE_ORIENT_VERTICAL; michael@0: } michael@0: michael@0: // menupopup: michael@0: bool michael@0: nsNativeTheme::IsSubmenu(nsIFrame* aFrame, bool* aLeftOfParent) michael@0: { michael@0: if (!aFrame) michael@0: return false; michael@0: michael@0: nsIContent* parentContent = aFrame->GetContent()->GetParent(); michael@0: if (!parentContent || parentContent->Tag() != nsGkAtoms::menu) michael@0: return false; michael@0: michael@0: nsIFrame* parent = aFrame; michael@0: while ((parent = parent->GetParent())) { michael@0: if (parent->GetContent() == parentContent) { michael@0: if (aLeftOfParent) { michael@0: nsIntRect selfBounds, parentBounds; michael@0: aFrame->GetNearestWidget()->GetScreenBounds(selfBounds); michael@0: parent->GetNearestWidget()->GetScreenBounds(parentBounds); michael@0: *aLeftOfParent = selfBounds.x < parentBounds.x; michael@0: } michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::IsRegularMenuItem(nsIFrame *aFrame) michael@0: { michael@0: nsMenuFrame *menuFrame = do_QueryFrame(aFrame); michael@0: return !(menuFrame && (menuFrame->IsOnMenuBar() || michael@0: menuFrame->GetParentMenuListType() != eNotMenuList)); michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::IsMenuListEditable(nsIFrame *aFrame) michael@0: { michael@0: bool isEditable = false; michael@0: nsCOMPtr menulist = do_QueryInterface(aFrame->GetContent()); michael@0: if (menulist) michael@0: menulist->GetEditable(&isEditable); michael@0: return isEditable; michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::QueueAnimatedContentForRefresh(nsIContent* aContent, michael@0: uint32_t aMinimumFrameRate) michael@0: { michael@0: NS_ASSERTION(aContent, "Null pointer!"); michael@0: NS_ASSERTION(aMinimumFrameRate, "aMinimumFrameRate must be non-zero!"); michael@0: NS_ASSERTION(aMinimumFrameRate <= 1000, michael@0: "aMinimumFrameRate must be less than 1000!"); michael@0: michael@0: uint32_t timeout = 1000 / aMinimumFrameRate; michael@0: timeout = std::min(mAnimatedContentTimeout, timeout); michael@0: michael@0: if (!mAnimatedContentTimer) { michael@0: mAnimatedContentTimer = do_CreateInstance(NS_TIMER_CONTRACTID); michael@0: NS_ENSURE_TRUE(mAnimatedContentTimer, false); michael@0: } michael@0: michael@0: if (mAnimatedContentList.IsEmpty() || timeout != mAnimatedContentTimeout) { michael@0: nsresult rv; michael@0: if (!mAnimatedContentList.IsEmpty()) { michael@0: rv = mAnimatedContentTimer->Cancel(); michael@0: NS_ENSURE_SUCCESS(rv, false); michael@0: } michael@0: michael@0: rv = mAnimatedContentTimer->InitWithCallback(this, timeout, michael@0: nsITimer::TYPE_ONE_SHOT); michael@0: NS_ENSURE_SUCCESS(rv, false); michael@0: michael@0: mAnimatedContentTimeout = timeout; michael@0: } michael@0: michael@0: if (!mAnimatedContentList.AppendElement(aContent)) { michael@0: NS_WARNING("Out of memory!"); michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNativeTheme::Notify(nsITimer* aTimer) michael@0: { michael@0: NS_ASSERTION(aTimer == mAnimatedContentTimer, "Wrong timer!"); michael@0: michael@0: // XXX Assumes that calling nsIFrame::Invalidate won't reenter michael@0: // QueueAnimatedContentForRefresh. michael@0: michael@0: uint32_t count = mAnimatedContentList.Length(); michael@0: for (uint32_t index = 0; index < count; index++) { michael@0: nsIFrame* frame = mAnimatedContentList[index]->GetPrimaryFrame(); michael@0: if (frame) { michael@0: frame->InvalidateFrame(); michael@0: } michael@0: } michael@0: michael@0: mAnimatedContentList.Clear(); michael@0: mAnimatedContentTimeout = UINT32_MAX; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsNativeTheme::GetAdjacentSiblingFrameWithSameAppearance(nsIFrame* aFrame, michael@0: bool aNextSibling) michael@0: { michael@0: if (!aFrame) michael@0: return nullptr; michael@0: michael@0: // Find the next visible sibling. michael@0: nsIFrame* sibling = aFrame; michael@0: do { michael@0: sibling = aNextSibling ? sibling->GetNextSibling() : sibling->GetPrevSibling(); michael@0: } while (sibling && sibling->GetRect().width == 0); michael@0: michael@0: // Check same appearance and adjacency. michael@0: if (!sibling || michael@0: sibling->StyleDisplay()->mAppearance != aFrame->StyleDisplay()->mAppearance || michael@0: (sibling->GetRect().XMost() != aFrame->GetRect().x && michael@0: aFrame->GetRect().XMost() != sibling->GetRect().x)) michael@0: return nullptr; michael@0: return sibling; michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::IsRangeHorizontal(nsIFrame* aFrame) michael@0: { michael@0: nsIFrame* rangeFrame = aFrame; michael@0: if (rangeFrame->GetType() != nsGkAtoms::rangeFrame) { michael@0: // If the thumb's frame is passed in, get its range parent: michael@0: rangeFrame = aFrame->GetParent(); michael@0: } michael@0: if (rangeFrame->GetType() == nsGkAtoms::rangeFrame) { michael@0: return static_cast(rangeFrame)->IsHorizontal(); michael@0: } michael@0: // Not actually a range frame - just use the ratio of the frame's size to michael@0: // decide: michael@0: return aFrame->GetSize().width >= aFrame->GetSize().height; michael@0: } michael@0: michael@0: static nsIFrame* michael@0: GetBodyFrame(nsIFrame* aCanvasFrame) michael@0: { michael@0: nsIContent* content = aCanvasFrame->GetContent(); michael@0: if (!content) { michael@0: return nullptr; michael@0: } michael@0: nsIDocument* document = content->OwnerDoc(); michael@0: nsIContent* body = document->GetBodyElement(); michael@0: if (!body) { michael@0: return nullptr; michael@0: } michael@0: return body->GetPrimaryFrame(); michael@0: } michael@0: michael@0: bool michael@0: nsNativeTheme::IsDarkBackground(nsIFrame* aFrame) michael@0: { michael@0: nsIScrollableFrame* scrollFrame = nullptr; michael@0: while (!scrollFrame && aFrame) { michael@0: scrollFrame = aFrame->GetScrollTargetFrame(); michael@0: aFrame = aFrame->GetParent(); michael@0: } michael@0: if (!scrollFrame) michael@0: return false; michael@0: michael@0: nsIFrame* frame = scrollFrame->GetScrolledFrame(); michael@0: if (nsCSSRendering::IsCanvasFrame(frame)) { michael@0: // For canvas frames, prefer to look at the body first, because the body michael@0: // background color is most likely what will be visible as the background michael@0: // color of the page, even if the html element has a different background michael@0: // color which prevents that of the body frame to propagate to the viewport. michael@0: nsIFrame* bodyFrame = GetBodyFrame(frame); michael@0: if (bodyFrame) { michael@0: frame = bodyFrame; michael@0: } michael@0: } michael@0: nsStyleContext* bgSC = nullptr; michael@0: if (!nsCSSRendering::FindBackground(frame, &bgSC) || michael@0: bgSC->StyleBackground()->IsTransparent()) { michael@0: nsIFrame* backgroundFrame = nsCSSRendering::FindNonTransparentBackgroundFrame(frame, true); michael@0: nsCSSRendering::FindBackground(backgroundFrame, &bgSC); michael@0: } michael@0: if (bgSC) { michael@0: nscolor bgColor = bgSC->StyleBackground()->mBackgroundColor; michael@0: // Consider the background color dark if the sum of the r, g and b values is michael@0: // less than 384 in a semi-transparent document. This heuristic matches what michael@0: // WebKit does, and we can improve it later if needed. michael@0: return NS_GET_A(bgColor) > 127 && michael@0: NS_GET_R(bgColor) + NS_GET_G(bgColor) + NS_GET_B(bgColor) < 384; michael@0: } michael@0: return false; michael@0: }