2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04: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 09:13:07 +04:00
|
|
|
|
|
|
|
#include "nsGfxCheckboxControlFrame.h"
|
|
|
|
#include "nsIContent.h"
|
2006-07-13 09:05:54 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2000-03-24 18:02:02 +03:00
|
|
|
#include "nsCSSRendering.h"
|
2011-04-08 05:04:40 +04:00
|
|
|
#include "nsRenderingContext.h"
|
2001-04-01 05:01:33 +04:00
|
|
|
#include "nsIServiceManager.h"
|
2002-06-04 04:44:04 +04:00
|
|
|
#include "nsIDOMHTMLInputElement.h"
|
2006-01-26 05:29:17 +03:00
|
|
|
#include "nsDisplayList.h"
|
2009-01-19 21:31:32 +03:00
|
|
|
#include "nsCSSAnonBoxes.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2006-07-06 14:43:51 +04:00
|
|
|
|
2012-09-29 01:53:44 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2006-07-06 14:43:51 +04:00
|
|
|
static void
|
2009-04-03 12:45:17 +04:00
|
|
|
PaintCheckMark(nsIFrame* aFrame,
|
2011-04-08 05:04:40 +04:00
|
|
|
nsRenderingContext* aCtx,
|
2009-04-03 12:45:17 +04:00
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
nsPoint aPt)
|
2006-07-06 14:43:51 +04:00
|
|
|
{
|
2009-04-03 12:45:17 +04:00
|
|
|
nsRect rect(aPt, aFrame->GetSize());
|
|
|
|
rect.Deflate(aFrame->GetUsedBorderAndPadding());
|
|
|
|
|
2006-07-13 09:05:54 +04:00
|
|
|
// Points come from the coordinates on a 7X7 unit box centered at 0,0
|
2012-08-22 19:56:38 +04:00
|
|
|
const int32_t checkPolygonX[] = { -3, -1, 3, 3, -1, -3 };
|
|
|
|
const int32_t checkPolygonY[] = { -1, 1, -3, -1, 3, 1 };
|
|
|
|
const int32_t checkNumPoints = sizeof(checkPolygonX) / sizeof(int32_t);
|
|
|
|
const int32_t checkSize = 9; // 2 units of padding on either side
|
2009-04-03 12:45:17 +04:00
|
|
|
// of the 7x7 unit checkmark
|
2006-07-13 09:05:54 +04:00
|
|
|
|
|
|
|
// Scale the checkmark based on the smallest dimension
|
2013-01-15 16:22:03 +04:00
|
|
|
nscoord paintScale = std::min(rect.width, rect.height) / checkSize;
|
2009-04-03 12:45:17 +04:00
|
|
|
nsPoint paintCenter(rect.x + rect.width / 2,
|
|
|
|
rect.y + rect.height / 2);
|
2006-07-13 09:05:54 +04:00
|
|
|
|
|
|
|
nsPoint paintPolygon[checkNumPoints];
|
|
|
|
// Convert checkmark for screen rendering
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t polyIndex = 0; polyIndex < checkNumPoints; polyIndex++) {
|
2006-07-13 09:05:54 +04:00
|
|
|
paintPolygon[polyIndex] = paintCenter +
|
|
|
|
nsPoint(checkPolygonX[polyIndex] * paintScale,
|
|
|
|
checkPolygonY[polyIndex] * paintScale);
|
2006-07-06 14:43:51 +04:00
|
|
|
}
|
2006-07-13 09:05:54 +04:00
|
|
|
|
2013-02-17 01:51:02 +04:00
|
|
|
aCtx->SetColor(aFrame->StyleColor()->mColor);
|
2009-04-03 12:45:17 +04:00
|
|
|
aCtx->FillPolygon(paintPolygon, checkNumPoints);
|
2006-07-06 14:43:51 +04:00
|
|
|
}
|
1999-08-06 09:13:07 +04:00
|
|
|
|
2009-01-22 03:07:44 +03:00
|
|
|
static void
|
2009-04-03 12:45:17 +04:00
|
|
|
PaintIndeterminateMark(nsIFrame* aFrame,
|
2011-04-08 05:04:40 +04:00
|
|
|
nsRenderingContext* aCtx,
|
2009-04-03 12:45:17 +04:00
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
nsPoint aPt)
|
2009-01-22 03:07:44 +03:00
|
|
|
{
|
2009-04-03 12:45:17 +04:00
|
|
|
nsRect rect(aPt, aFrame->GetSize());
|
|
|
|
rect.Deflate(aFrame->GetUsedBorderAndPadding());
|
|
|
|
|
|
|
|
rect.y += (rect.height - rect.height/4) / 2;
|
|
|
|
rect.height /= 4;
|
2009-01-22 03:07:44 +03:00
|
|
|
|
2013-02-17 01:51:02 +04:00
|
|
|
aCtx->SetColor(aFrame->StyleColor()->mColor);
|
2009-04-03 12:45:17 +04:00
|
|
|
aCtx->FillRect(rect);
|
2009-01-22 03:07:44 +03:00
|
|
|
}
|
|
|
|
|
2000-02-09 22:34:39 +03:00
|
|
|
//------------------------------------------------------------
|
2005-11-04 05:38:33 +03:00
|
|
|
nsIFrame*
|
2009-04-03 12:45:17 +04:00
|
|
|
NS_NewGfxCheckboxControlFrame(nsIPresShell* aPresShell,
|
|
|
|
nsStyleContext* aContext)
|
1999-08-06 09:13:07 +04:00
|
|
|
{
|
2006-03-27 01:30:36 +04:00
|
|
|
return new (aPresShell) nsGfxCheckboxControlFrame(aContext);
|
1999-08-06 09:13:07 +04:00
|
|
|
}
|
|
|
|
|
2009-09-12 20:49:24 +04:00
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsGfxCheckboxControlFrame)
|
|
|
|
|
1999-08-11 08:45:49 +04:00
|
|
|
|
2000-02-09 22:34:39 +03:00
|
|
|
//------------------------------------------------------------
|
1999-08-11 08:45:49 +04:00
|
|
|
// Initialize GFX-rendered state
|
2006-03-27 01:30:36 +04:00
|
|
|
nsGfxCheckboxControlFrame::nsGfxCheckboxControlFrame(nsStyleContext* aContext)
|
2006-07-13 09:05:54 +04:00
|
|
|
: nsFormControlFrame(aContext)
|
1999-08-06 09:13:07 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2000-03-24 18:02:02 +03:00
|
|
|
nsGfxCheckboxControlFrame::~nsGfxCheckboxControlFrame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-04-02 14:03:52 +04:00
|
|
|
#ifdef ACCESSIBILITY
|
2012-09-29 01:53:44 +04:00
|
|
|
a11y::AccType
|
|
|
|
nsGfxCheckboxControlFrame::AccessibleType()
|
2001-05-18 03:52:32 +04:00
|
|
|
{
|
2012-12-18 05:25:52 +04:00
|
|
|
return a11y::eHTMLCheckboxType;
|
2001-05-18 03:52:32 +04:00
|
|
|
}
|
2001-08-17 07:13:07 +04:00
|
|
|
#endif
|
2001-05-18 03:52:32 +04:00
|
|
|
|
2000-02-09 22:34:39 +03:00
|
|
|
//------------------------------------------------------------
|
2013-02-14 15:12:27 +04:00
|
|
|
void
|
2006-01-26 05:29:17 +03:00
|
|
|
nsGfxCheckboxControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
|
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
const nsDisplayListSet& aLists)
|
1999-08-06 09:13:07 +04:00
|
|
|
{
|
2013-02-14 15:08:08 +04:00
|
|
|
nsFormControlFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);
|
2006-01-26 05:29:17 +03:00
|
|
|
|
|
|
|
// Get current checked state through content model.
|
2009-01-22 03:07:44 +03:00
|
|
|
if ((!IsChecked() && !IsIndeterminate()) || !IsVisibleForPainting(aBuilder))
|
2013-02-14 15:12:27 +04:00
|
|
|
return; // we're not checked or not visible, nothing to paint.
|
2006-01-26 05:29:17 +03:00
|
|
|
|
|
|
|
if (IsThemed())
|
2013-02-14 15:12:27 +04:00
|
|
|
return; // No need to paint the checkmark. The theme will do it.
|
2006-01-26 05:29:17 +03:00
|
|
|
|
2013-02-14 15:08:08 +04:00
|
|
|
aLists.Content()->AppendNewToTop(new (aBuilder)
|
2010-08-13 14:01:13 +04:00
|
|
|
nsDisplayGeneric(aBuilder, this,
|
2009-04-03 12:45:17 +04:00
|
|
|
IsIndeterminate()
|
|
|
|
? PaintIndeterminateMark : PaintCheckMark,
|
2010-07-16 01:07:49 +04:00
|
|
|
"CheckedCheckbox",
|
|
|
|
nsDisplayItem::TYPE_CHECKED_CHECKBOX));
|
1999-08-06 09:13:07 +04:00
|
|
|
}
|
|
|
|
|
2000-02-09 22:34:39 +03:00
|
|
|
//------------------------------------------------------------
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2009-01-22 03:07:44 +03:00
|
|
|
nsGfxCheckboxControlFrame::IsChecked()
|
1999-08-06 09:13:07 +04:00
|
|
|
{
|
2002-01-24 22:04:55 +03:00
|
|
|
nsCOMPtr<nsIDOMHTMLInputElement> elem(do_QueryInterface(mContent));
|
2011-09-29 10:19:26 +04:00
|
|
|
bool retval = false;
|
2002-01-24 22:04:55 +03:00
|
|
|
elem->GetChecked(&retval);
|
|
|
|
return retval;
|
1999-08-06 09:13:07 +04:00
|
|
|
}
|
2009-01-22 03:07:44 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2009-01-22 03:07:44 +03:00
|
|
|
nsGfxCheckboxControlFrame::IsIndeterminate()
|
|
|
|
{
|
2010-08-05 06:40:18 +04:00
|
|
|
nsCOMPtr<nsIDOMHTMLInputElement> elem(do_QueryInterface(mContent));
|
2011-09-29 10:19:26 +04:00
|
|
|
bool retval = false;
|
2009-01-22 03:07:44 +03:00
|
|
|
elem->GetIndeterminate(&retval);
|
|
|
|
return retval;
|
|
|
|
}
|