2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00: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/. */
|
1999-08-06 05:13:07 +00:00
|
|
|
|
|
|
|
#include "nsGfxCheckboxControlFrame.h"
|
|
|
|
#include "nsIContent.h"
|
2006-07-13 05:05:54 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2000-03-24 15:02:02 +00:00
|
|
|
#include "nsCSSRendering.h"
|
2011-04-08 01:04:40 +00:00
|
|
|
#include "nsRenderingContext.h"
|
2001-08-17 03:13:07 +00:00
|
|
|
#ifdef ACCESSIBILITY
|
2011-01-28 04:38:14 +00:00
|
|
|
#include "nsAccessibilityService.h"
|
2001-08-17 03:13:07 +00:00
|
|
|
#endif
|
2001-04-01 01:01:33 +00:00
|
|
|
#include "nsIServiceManager.h"
|
2002-06-04 00:44:04 +00:00
|
|
|
#include "nsIDOMHTMLInputElement.h"
|
2006-01-26 02:29:17 +00:00
|
|
|
#include "nsDisplayList.h"
|
2009-01-19 18:31:32 +00:00
|
|
|
#include "nsCSSAnonBoxes.h"
|
2006-07-06 10:43:51 +00:00
|
|
|
|
|
|
|
static void
|
2009-04-03 08:45:17 +00:00
|
|
|
PaintCheckMark(nsIFrame* aFrame,
|
2011-04-08 01:04:40 +00:00
|
|
|
nsRenderingContext* aCtx,
|
2009-04-03 08:45:17 +00:00
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
nsPoint aPt)
|
2006-07-06 10:43:51 +00:00
|
|
|
{
|
2009-04-03 08:45:17 +00:00
|
|
|
nsRect rect(aPt, aFrame->GetSize());
|
|
|
|
rect.Deflate(aFrame->GetUsedBorderAndPadding());
|
|
|
|
|
2006-07-13 05:05:54 +00: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 08:45:17 +00:00
|
|
|
const PRInt32 checkSize = 9; // 2 units of padding on either side
|
|
|
|
// of the 7x7 unit checkmark
|
2006-07-13 05:05:54 +00:00
|
|
|
|
|
|
|
// Scale the checkmark based on the smallest dimension
|
2009-09-16 15:01:36 +00:00
|
|
|
nscoord paintScale = NS_MIN(rect.width, rect.height) / checkSize;
|
2009-04-03 08:45:17 +00:00
|
|
|
nsPoint paintCenter(rect.x + rect.width / 2,
|
|
|
|
rect.y + rect.height / 2);
|
2006-07-13 05:05:54 +00: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);
|
2006-07-06 10:43:51 +00:00
|
|
|
}
|
2006-07-13 05:05:54 +00:00
|
|
|
|
2009-04-03 08:45:17 +00:00
|
|
|
aCtx->SetColor(aFrame->GetStyleColor()->mColor);
|
|
|
|
aCtx->FillPolygon(paintPolygon, checkNumPoints);
|
2006-07-06 10:43:51 +00:00
|
|
|
}
|
1999-08-06 05:13:07 +00:00
|
|
|
|
2009-01-22 00:07:44 +00:00
|
|
|
static void
|
2009-04-03 08:45:17 +00:00
|
|
|
PaintIndeterminateMark(nsIFrame* aFrame,
|
2011-04-08 01:04:40 +00:00
|
|
|
nsRenderingContext* aCtx,
|
2009-04-03 08:45:17 +00:00
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
nsPoint aPt)
|
2009-01-22 00:07:44 +00:00
|
|
|
{
|
2009-04-03 08:45:17 +00:00
|
|
|
nsRect rect(aPt, aFrame->GetSize());
|
|
|
|
rect.Deflate(aFrame->GetUsedBorderAndPadding());
|
|
|
|
|
|
|
|
rect.y += (rect.height - rect.height/4) / 2;
|
|
|
|
rect.height /= 4;
|
2009-01-22 00:07:44 +00:00
|
|
|
|
2009-04-03 08:45:17 +00:00
|
|
|
aCtx->SetColor(aFrame->GetStyleColor()->mColor);
|
|
|
|
aCtx->FillRect(rect);
|
2009-01-22 00:07:44 +00:00
|
|
|
}
|
|
|
|
|
2000-02-09 19:34:39 +00:00
|
|
|
//------------------------------------------------------------
|
2005-11-04 02:38:33 +00:00
|
|
|
nsIFrame*
|
2009-04-03 08:45:17 +00:00
|
|
|
NS_NewGfxCheckboxControlFrame(nsIPresShell* aPresShell,
|
|
|
|
nsStyleContext* aContext)
|
1999-08-06 05:13:07 +00:00
|
|
|
{
|
2006-03-26 21:30:36 +00:00
|
|
|
return new (aPresShell) nsGfxCheckboxControlFrame(aContext);
|
1999-08-06 05:13:07 +00:00
|
|
|
}
|
|
|
|
|
2009-09-12 16:49:24 +00:00
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsGfxCheckboxControlFrame)
|
|
|
|
|
1999-08-11 04:45:49 +00:00
|
|
|
|
2000-02-09 19:34:39 +00:00
|
|
|
//------------------------------------------------------------
|
1999-08-11 04:45:49 +00:00
|
|
|
// Initialize GFX-rendered state
|
2006-03-26 21:30:36 +00:00
|
|
|
nsGfxCheckboxControlFrame::nsGfxCheckboxControlFrame(nsStyleContext* aContext)
|
2006-07-13 05:05:54 +00:00
|
|
|
: nsFormControlFrame(aContext)
|
1999-08-06 05:13:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2000-03-24 15:02:02 +00:00
|
|
|
nsGfxCheckboxControlFrame::~nsGfxCheckboxControlFrame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-04-02 10:03:52 +00:00
|
|
|
#ifdef ACCESSIBILITY
|
2012-05-29 01:18:45 +00:00
|
|
|
already_AddRefed<Accessible>
|
2010-06-28 12:02:03 +00:00
|
|
|
nsGfxCheckboxControlFrame::CreateAccessible()
|
2001-05-17 23:52:32 +00:00
|
|
|
{
|
2011-01-28 04:38:14 +00:00
|
|
|
nsAccessibilityService* accService = nsIPresShell::AccService();
|
2001-05-17 23:52:32 +00:00
|
|
|
if (accService) {
|
2010-06-28 12:02:03 +00:00
|
|
|
return accService->CreateHTMLCheckboxAccessible(mContent,
|
|
|
|
PresContext()->PresShell());
|
2001-05-17 23:52:32 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2001-05-17 23:52:32 +00:00
|
|
|
}
|
2001-08-17 03:13:07 +00:00
|
|
|
#endif
|
2001-05-17 23:52:32 +00:00
|
|
|
|
2000-02-09 19:34:39 +00:00
|
|
|
//------------------------------------------------------------
|
2006-01-26 02:29:17 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGfxCheckboxControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
|
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
const nsDisplayListSet& aLists)
|
1999-08-06 05:13:07 +00:00
|
|
|
{
|
2009-04-03 08:45:17 +00:00
|
|
|
nsresult rv = nsFormControlFrame::BuildDisplayList(aBuilder, aDirtyRect,
|
|
|
|
aLists);
|
2006-01-26 02:29:17 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Get current checked state through content model.
|
2009-01-22 00:07:44 +00:00
|
|
|
if ((!IsChecked() && !IsIndeterminate()) || !IsVisibleForPainting(aBuilder))
|
2006-01-26 02:29:17 +00: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 08:45:17 +00:00
|
|
|
return aLists.Content()->AppendNewToTop(new (aBuilder)
|
2010-08-13 10:01:13 +00:00
|
|
|
nsDisplayGeneric(aBuilder, this,
|
2009-04-03 08:45:17 +00:00
|
|
|
IsIndeterminate()
|
|
|
|
? PaintIndeterminateMark : PaintCheckMark,
|
2010-07-15 21:07:49 +00:00
|
|
|
"CheckedCheckbox",
|
|
|
|
nsDisplayItem::TYPE_CHECKED_CHECKBOX));
|
1999-08-06 05:13:07 +00:00
|
|
|
}
|
|
|
|
|
2000-02-09 19:34:39 +00:00
|
|
|
//------------------------------------------------------------
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2009-01-22 00:07:44 +00:00
|
|
|
nsGfxCheckboxControlFrame::IsChecked()
|
1999-08-06 05:13:07 +00:00
|
|
|
{
|
2002-01-24 19:04:55 +00:00
|
|
|
nsCOMPtr<nsIDOMHTMLInputElement> elem(do_QueryInterface(mContent));
|
2011-09-29 06:19:26 +00:00
|
|
|
bool retval = false;
|
2002-01-24 19:04:55 +00:00
|
|
|
elem->GetChecked(&retval);
|
|
|
|
return retval;
|
1999-08-06 05:13:07 +00:00
|
|
|
}
|
2009-01-22 00:07:44 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2009-01-22 00:07:44 +00:00
|
|
|
nsGfxCheckboxControlFrame::IsIndeterminate()
|
|
|
|
{
|
2010-08-05 02:40:18 +00:00
|
|
|
nsCOMPtr<nsIDOMHTMLInputElement> elem(do_QueryInterface(mContent));
|
2011-09-29 06:19:26 +00:00
|
|
|
bool retval = false;
|
2009-01-22 00:07:44 +00:00
|
|
|
elem->GetIndeterminate(&retval);
|
|
|
|
return retval;
|
|
|
|
}
|