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 "nsGfxCheckboxControlFrame.h" michael@0: #include "nsIContent.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsRenderingContext.h" michael@0: #include "nsIDOMHTMLInputElement.h" michael@0: #include "nsDisplayList.h" michael@0: #include michael@0: michael@0: using namespace mozilla; michael@0: michael@0: static void michael@0: PaintCheckMark(nsIFrame* aFrame, michael@0: nsRenderingContext* aCtx, michael@0: const nsRect& aDirtyRect, michael@0: nsPoint aPt) michael@0: { michael@0: nsRect rect(aPt, aFrame->GetSize()); michael@0: rect.Deflate(aFrame->GetUsedBorderAndPadding()); michael@0: michael@0: // Points come from the coordinates on a 7X7 unit box centered at 0,0 michael@0: const int32_t checkPolygonX[] = { -3, -1, 3, 3, -1, -3 }; michael@0: const int32_t checkPolygonY[] = { -1, 1, -3, -1, 3, 1 }; michael@0: const int32_t checkNumPoints = sizeof(checkPolygonX) / sizeof(int32_t); michael@0: const int32_t checkSize = 9; // 2 units of padding on either side michael@0: // of the 7x7 unit checkmark michael@0: michael@0: // Scale the checkmark based on the smallest dimension michael@0: nscoord paintScale = std::min(rect.width, rect.height) / checkSize; michael@0: nsPoint paintCenter(rect.x + rect.width / 2, michael@0: rect.y + rect.height / 2); michael@0: michael@0: nsPoint paintPolygon[checkNumPoints]; michael@0: // Convert checkmark for screen rendering michael@0: for (int32_t polyIndex = 0; polyIndex < checkNumPoints; polyIndex++) { michael@0: paintPolygon[polyIndex] = paintCenter + michael@0: nsPoint(checkPolygonX[polyIndex] * paintScale, michael@0: checkPolygonY[polyIndex] * paintScale); michael@0: } michael@0: michael@0: aCtx->SetColor(aFrame->StyleColor()->mColor); michael@0: aCtx->FillPolygon(paintPolygon, checkNumPoints); michael@0: } michael@0: michael@0: static void michael@0: PaintIndeterminateMark(nsIFrame* aFrame, michael@0: nsRenderingContext* aCtx, michael@0: const nsRect& aDirtyRect, michael@0: nsPoint aPt) michael@0: { michael@0: nsRect rect(aPt, aFrame->GetSize()); michael@0: rect.Deflate(aFrame->GetUsedBorderAndPadding()); michael@0: michael@0: rect.y += (rect.height - rect.height/4) / 2; michael@0: rect.height /= 4; michael@0: michael@0: aCtx->SetColor(aFrame->StyleColor()->mColor); michael@0: aCtx->FillRect(rect); michael@0: } michael@0: michael@0: //------------------------------------------------------------ michael@0: nsIFrame* michael@0: NS_NewGfxCheckboxControlFrame(nsIPresShell* aPresShell, michael@0: nsStyleContext* aContext) michael@0: { michael@0: return new (aPresShell) nsGfxCheckboxControlFrame(aContext); michael@0: } michael@0: michael@0: NS_IMPL_FRAMEARENA_HELPERS(nsGfxCheckboxControlFrame) michael@0: michael@0: michael@0: //------------------------------------------------------------ michael@0: // Initialize GFX-rendered state michael@0: nsGfxCheckboxControlFrame::nsGfxCheckboxControlFrame(nsStyleContext* aContext) michael@0: : nsFormControlFrame(aContext) michael@0: { michael@0: } michael@0: michael@0: nsGfxCheckboxControlFrame::~nsGfxCheckboxControlFrame() michael@0: { michael@0: } michael@0: michael@0: #ifdef ACCESSIBILITY michael@0: a11y::AccType michael@0: nsGfxCheckboxControlFrame::AccessibleType() michael@0: { michael@0: return a11y::eHTMLCheckboxType; michael@0: } michael@0: #endif michael@0: michael@0: //------------------------------------------------------------ michael@0: void michael@0: nsGfxCheckboxControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder, michael@0: const nsRect& aDirtyRect, michael@0: const nsDisplayListSet& aLists) michael@0: { michael@0: nsFormControlFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists); michael@0: michael@0: // Get current checked state through content model. michael@0: if ((!IsChecked() && !IsIndeterminate()) || !IsVisibleForPainting(aBuilder)) michael@0: return; // we're not checked or not visible, nothing to paint. michael@0: michael@0: if (IsThemed()) michael@0: return; // No need to paint the checkmark. The theme will do it. michael@0: michael@0: aLists.Content()->AppendNewToTop(new (aBuilder) michael@0: nsDisplayGeneric(aBuilder, this, michael@0: IsIndeterminate() michael@0: ? PaintIndeterminateMark : PaintCheckMark, michael@0: "CheckedCheckbox", michael@0: nsDisplayItem::TYPE_CHECKED_CHECKBOX)); michael@0: } michael@0: michael@0: //------------------------------------------------------------ michael@0: bool michael@0: nsGfxCheckboxControlFrame::IsChecked() michael@0: { michael@0: nsCOMPtr elem(do_QueryInterface(mContent)); michael@0: bool retval = false; michael@0: elem->GetChecked(&retval); michael@0: return retval; michael@0: } michael@0: michael@0: bool michael@0: nsGfxCheckboxControlFrame::IsIndeterminate() michael@0: { michael@0: nsCOMPtr elem(do_QueryInterface(mContent)); michael@0: bool retval = false; michael@0: elem->GetIndeterminate(&retval); michael@0: return retval; michael@0: }