2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsGfxCheckboxControlFrame.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCSSRendering.h"
|
2011-04-07 18:04:40 -07:00
|
|
|
#include "nsRenderingContext.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef ACCESSIBILITY
|
2011-01-27 20:38:14 -08:00
|
|
|
#include "nsAccessibilityService.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIDOMHTMLInputElement.h"
|
|
|
|
#include "nsDisplayList.h"
|
2009-01-19 10:31:32 -08:00
|
|
|
#include "nsCSSAnonBoxes.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static void
|
2009-04-03 01:45:17 -07:00
|
|
|
PaintCheckMark(nsIFrame* aFrame,
|
2011-04-07 18:04:40 -07:00
|
|
|
nsRenderingContext* aCtx,
|
2009-04-03 01:45:17 -07:00
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
nsPoint aPt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-03 01:45:17 -07:00
|
|
|
nsRect rect(aPt, aFrame->GetSize());
|
|
|
|
rect.Deflate(aFrame->GetUsedBorderAndPadding());
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Points come from the coordinates on a 7X7 unit box centered at 0,0
|
|
|
|
const PRInt32 checkPolygonX[] = { -3, -1, 3, 3, -1, -3 };
|
|
|
|
const PRInt32 checkPolygonY[] = { -1, 1, -3, -1, 3, 1 };
|
|
|
|
const PRInt32 checkNumPoints = sizeof(checkPolygonX) / sizeof(PRInt32);
|
2009-04-03 01:45:17 -07:00
|
|
|
const PRInt32 checkSize = 9; // 2 units of padding on either side
|
|
|
|
// of the 7x7 unit checkmark
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Scale the checkmark based on the smallest dimension
|
2009-09-16 08:01:36 -07:00
|
|
|
nscoord paintScale = NS_MIN(rect.width, rect.height) / checkSize;
|
2009-04-03 01:45:17 -07:00
|
|
|
nsPoint paintCenter(rect.x + rect.width / 2,
|
|
|
|
rect.y + rect.height / 2);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsPoint paintPolygon[checkNumPoints];
|
|
|
|
// Convert checkmark for screen rendering
|
|
|
|
for (PRInt32 polyIndex = 0; polyIndex < checkNumPoints; polyIndex++) {
|
|
|
|
paintPolygon[polyIndex] = paintCenter +
|
|
|
|
nsPoint(checkPolygonX[polyIndex] * paintScale,
|
|
|
|
checkPolygonY[polyIndex] * paintScale);
|
|
|
|
}
|
|
|
|
|
2009-04-03 01:45:17 -07:00
|
|
|
aCtx->SetColor(aFrame->GetStyleColor()->mColor);
|
|
|
|
aCtx->FillPolygon(paintPolygon, checkNumPoints);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-01-21 16:07:44 -08:00
|
|
|
static void
|
2009-04-03 01:45:17 -07:00
|
|
|
PaintIndeterminateMark(nsIFrame* aFrame,
|
2011-04-07 18:04:40 -07:00
|
|
|
nsRenderingContext* aCtx,
|
2009-04-03 01:45:17 -07:00
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
nsPoint aPt)
|
2009-01-21 16:07:44 -08:00
|
|
|
{
|
2009-04-03 01:45:17 -07:00
|
|
|
nsRect rect(aPt, aFrame->GetSize());
|
|
|
|
rect.Deflate(aFrame->GetUsedBorderAndPadding());
|
|
|
|
|
|
|
|
rect.y += (rect.height - rect.height/4) / 2;
|
|
|
|
rect.height /= 4;
|
2009-01-21 16:07:44 -08:00
|
|
|
|
2009-04-03 01:45:17 -07:00
|
|
|
aCtx->SetColor(aFrame->GetStyleColor()->mColor);
|
|
|
|
aCtx->FillRect(rect);
|
2009-01-21 16:07:44 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//------------------------------------------------------------
|
|
|
|
nsIFrame*
|
2009-04-03 01:45:17 -07:00
|
|
|
NS_NewGfxCheckboxControlFrame(nsIPresShell* aPresShell,
|
|
|
|
nsStyleContext* aContext)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return new (aPresShell) nsGfxCheckboxControlFrame(aContext);
|
|
|
|
}
|
|
|
|
|
2009-09-12 09:49:24 -07:00
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsGfxCheckboxControlFrame)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
// Initialize GFX-rendered state
|
|
|
|
nsGfxCheckboxControlFrame::nsGfxCheckboxControlFrame(nsStyleContext* aContext)
|
|
|
|
: nsFormControlFrame(aContext)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsGfxCheckboxControlFrame::~nsGfxCheckboxControlFrame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-04-02 03:03:52 -07:00
|
|
|
#ifdef ACCESSIBILITY
|
2012-05-28 18:18:45 -07:00
|
|
|
already_AddRefed<Accessible>
|
2010-06-28 05:02:03 -07:00
|
|
|
nsGfxCheckboxControlFrame::CreateAccessible()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-01-27 20:38:14 -08:00
|
|
|
nsAccessibilityService* accService = nsIPresShell::AccService();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (accService) {
|
2010-06-28 05:02:03 -07:00
|
|
|
return accService->CreateHTMLCheckboxAccessible(mContent,
|
|
|
|
PresContext()->PresShell());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-06-28 05:02:03 -07:00
|
|
|
return nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGfxCheckboxControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
|
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
const nsDisplayListSet& aLists)
|
|
|
|
{
|
2009-04-03 01:45:17 -07:00
|
|
|
nsresult rv = nsFormControlFrame::BuildDisplayList(aBuilder, aDirtyRect,
|
|
|
|
aLists);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Get current checked state through content model.
|
2009-01-21 16:07:44 -08:00
|
|
|
if ((!IsChecked() && !IsIndeterminate()) || !IsVisibleForPainting(aBuilder))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK; // we're not checked or not visible, nothing to paint.
|
|
|
|
|
|
|
|
if (IsThemed())
|
|
|
|
return NS_OK; // No need to paint the checkmark. The theme will do it.
|
|
|
|
|
2009-04-03 01:45:17 -07:00
|
|
|
return aLists.Content()->AppendNewToTop(new (aBuilder)
|
2010-08-13 03:01:13 -07:00
|
|
|
nsDisplayGeneric(aBuilder, this,
|
2009-04-03 01:45:17 -07:00
|
|
|
IsIndeterminate()
|
|
|
|
? PaintIndeterminateMark : PaintCheckMark,
|
2010-07-15 14:07:49 -07:00
|
|
|
"CheckedCheckbox",
|
|
|
|
nsDisplayItem::TYPE_CHECKED_CHECKBOX));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2009-01-21 16:07:44 -08:00
|
|
|
nsGfxCheckboxControlFrame::IsChecked()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMHTMLInputElement> elem(do_QueryInterface(mContent));
|
2011-09-28 23:19:26 -07:00
|
|
|
bool retval = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
elem->GetChecked(&retval);
|
|
|
|
return retval;
|
|
|
|
}
|
2009-01-21 16:07:44 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2009-01-21 16:07:44 -08:00
|
|
|
nsGfxCheckboxControlFrame::IsIndeterminate()
|
|
|
|
{
|
2010-08-04 19:40:18 -07:00
|
|
|
nsCOMPtr<nsIDOMHTMLInputElement> elem(do_QueryInterface(mContent));
|
2011-09-28 23:19:26 -07:00
|
|
|
bool retval = false;
|
2009-01-21 16:07:44 -08:00
|
|
|
elem->GetIndeterminate(&retval);
|
|
|
|
return retval;
|
|
|
|
}
|