2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 18:30:37 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-08-06 09:13:07 +04:00
|
|
|
*
|
2004-04-18 18:30:37 +04:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
1999-08-06 09:13:07 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-08-06 09:13:07 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 18:30:37 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:40:37 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 18:30:37 +04:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-29 00:14:13 +04:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 18:30:37 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 18:30:37 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
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"
|
2001-08-17 07:13:07 +04:00
|
|
|
#ifdef ACCESSIBILITY
|
2001-04-01 05:01:33 +04:00
|
|
|
#include "nsIAccessibilityService.h"
|
2001-08-17 07:13:07 +04:00
|
|
|
#endif
|
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"
|
2006-07-06 14:43:51 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
PaintCheckMark(nsIRenderingContext& aRenderingContext,
|
2006-07-13 09:05:54 +04:00
|
|
|
const nsRect& aRect)
|
2006-07-06 14:43:51 +04:00
|
|
|
{
|
2006-07-13 09:05:54 +04: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);
|
|
|
|
const PRInt32 checkSize = 9; // This is value is determined by adding 2
|
|
|
|
// units to pad the 7x7 unit checkmark
|
|
|
|
|
|
|
|
// Scale the checkmark based on the smallest dimension
|
|
|
|
nscoord paintScale = PR_MIN(aRect.width, aRect.height) / checkSize;
|
|
|
|
nsPoint paintCenter(aRect.x + aRect.width / 2,
|
|
|
|
aRect.y + aRect.height / 2);
|
|
|
|
|
|
|
|
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 14:43:51 +04:00
|
|
|
}
|
2006-07-13 09:05:54 +04:00
|
|
|
|
|
|
|
aRenderingContext.FillPolygon(paintPolygon, checkNumPoints);
|
2006-07-06 14:43:51 +04:00
|
|
|
}
|
1999-08-06 09:13:07 +04:00
|
|
|
|
2000-02-09 22:34:39 +03:00
|
|
|
//------------------------------------------------------------
|
2005-11-04 05:38:33 +03:00
|
|
|
nsIFrame*
|
2006-03-27 01:30:36 +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
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-09 22:34:39 +03:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// nsISupports
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Frames are not refcounted, no need to AddRef
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGfxCheckboxControlFrame::QueryInterface(const nsIID& aIID, void** aInstancePtr)
|
|
|
|
{
|
2007-07-04 06:15:31 +04:00
|
|
|
NS_PRECONDITION(aInstancePtr, "null out param");
|
2002-03-31 14:14:01 +04:00
|
|
|
|
2000-03-24 18:02:02 +03:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsICheckboxControlFrame))) {
|
2007-07-08 11:08:04 +04:00
|
|
|
*aInstancePtr = static_cast<nsICheckboxControlFrame*>(this);
|
2000-03-24 18:02:02 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-04-01 05:01:33 +04:00
|
|
|
|
2000-02-09 22:34:39 +03:00
|
|
|
return nsFormControlFrame::QueryInterface(aIID, aInstancePtr);
|
|
|
|
}
|
|
|
|
|
2001-08-17 07:13:07 +04:00
|
|
|
#ifdef ACCESSIBILITY
|
2001-05-18 03:52:32 +04:00
|
|
|
NS_IMETHODIMP nsGfxCheckboxControlFrame::GetAccessible(nsIAccessible** aAccessible)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAccessibilityService> accService = do_GetService("@mozilla.org/accessibilityService;1");
|
|
|
|
|
|
|
|
if (accService) {
|
2007-07-08 11:08:04 +04:00
|
|
|
return accService->CreateHTMLCheckboxAccessible(static_cast<nsIFrame*>(this), aAccessible);
|
2001-05-18 03:52:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2001-08-17 07:13:07 +04:00
|
|
|
#endif
|
2001-05-18 03:52:32 +04:00
|
|
|
|
2000-03-24 18:02:02 +03:00
|
|
|
//--------------------------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
2003-02-22 03:32:13 +03:00
|
|
|
nsGfxCheckboxControlFrame::SetCheckboxFaceStyleContext(nsStyleContext *aCheckboxFaceStyleContext)
|
2000-03-24 18:02:02 +03:00
|
|
|
{
|
|
|
|
mCheckButtonFaceStyle = aCheckboxFaceStyleContext;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-02-09 22:34:39 +03:00
|
|
|
|
2000-03-24 18:02:02 +03:00
|
|
|
//--------------------------------------------------------------
|
2003-02-22 03:32:13 +03:00
|
|
|
nsStyleContext*
|
|
|
|
nsGfxCheckboxControlFrame::GetAdditionalStyleContext(PRInt32 aIndex) const
|
2000-03-24 18:02:02 +03:00
|
|
|
{
|
|
|
|
switch (aIndex) {
|
|
|
|
case NS_GFX_CHECKBOX_CONTROL_FRAME_FACE_CONTEXT_INDEX:
|
2003-02-22 03:32:13 +03:00
|
|
|
return mCheckButtonFaceStyle;
|
2000-03-24 18:02:02 +03:00
|
|
|
break;
|
|
|
|
default:
|
2003-02-22 03:32:13 +03:00
|
|
|
return nsnull;
|
2000-03-24 18:02:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------
|
2003-02-22 03:32:13 +03:00
|
|
|
void
|
2000-03-24 18:02:02 +03:00
|
|
|
nsGfxCheckboxControlFrame::SetAdditionalStyleContext(PRInt32 aIndex,
|
2003-02-22 03:32:13 +03:00
|
|
|
nsStyleContext* aStyleContext)
|
2000-03-24 18:02:02 +03:00
|
|
|
{
|
|
|
|
switch (aIndex) {
|
|
|
|
case NS_GFX_CHECKBOX_CONTROL_FRAME_FACE_CONTEXT_INDEX:
|
|
|
|
mCheckButtonFaceStyle = aStyleContext;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-09 22:34:39 +03:00
|
|
|
//------------------------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
2004-08-01 03:15:21 +04:00
|
|
|
nsGfxCheckboxControlFrame::OnChecked(nsPresContext* aPresContext,
|
2002-01-24 22:04:55 +03:00
|
|
|
PRBool aChecked)
|
2000-02-09 22:34:39 +03:00
|
|
|
{
|
2004-08-26 04:07:56 +04:00
|
|
|
Invalidate(GetOverflowRect(), PR_FALSE);
|
2006-07-13 09:05:54 +04:00
|
|
|
return NS_OK;
|
2000-02-09 22:34:39 +03:00
|
|
|
}
|
|
|
|
|
2006-01-26 05:29:17 +03:00
|
|
|
static void PaintCheckMarkFromStyle(nsIFrame* aFrame,
|
|
|
|
nsIRenderingContext* aCtx, const nsRect& aDirtyRect, nsPoint aPt) {
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<nsGfxCheckboxControlFrame*>(aFrame)
|
2006-01-26 05:29:17 +03:00
|
|
|
->PaintCheckBoxFromStyle(*aCtx, aPt, aDirtyRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
class nsDisplayCheckMark : public nsDisplayItem {
|
|
|
|
public:
|
2006-01-29 21:48:58 +03:00
|
|
|
nsDisplayCheckMark(nsGfxCheckboxControlFrame* aFrame)
|
2006-03-14 23:43:18 +03:00
|
|
|
: nsDisplayItem(aFrame) {
|
2006-01-29 21:48:58 +03:00
|
|
|
MOZ_COUNT_CTOR(nsDisplayCheckMark);
|
|
|
|
}
|
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
|
|
|
virtual ~nsDisplayCheckMark() {
|
|
|
|
MOZ_COUNT_DTOR(nsDisplayCheckMark);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-01-26 05:29:17 +03:00
|
|
|
virtual void Paint(nsDisplayListBuilder* aBuilder, nsIRenderingContext* aCtx,
|
|
|
|
const nsRect& aDirtyRect);
|
2006-01-27 01:58:22 +03:00
|
|
|
NS_DISPLAY_DECL_NAME("CheckMark")
|
2006-01-26 05:29:17 +03:00
|
|
|
};
|
1999-08-06 09:13:07 +04:00
|
|
|
|
|
|
|
void
|
2006-01-26 05:29:17 +03:00
|
|
|
nsDisplayCheckMark::Paint(nsDisplayListBuilder* aBuilder,
|
|
|
|
nsIRenderingContext* aCtx, const nsRect& aDirtyRect) {
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<nsGfxCheckboxControlFrame*>(mFrame)->
|
2006-03-14 23:43:18 +03:00
|
|
|
PaintCheckBox(*aCtx, aBuilder->ToReferenceFrame(mFrame), aDirtyRect);
|
2006-01-26 05:29:17 +03:00
|
|
|
}
|
2002-09-21 05:03:38 +04:00
|
|
|
|
1999-08-06 09:13:07 +04:00
|
|
|
|
2006-01-26 05:29:17 +03:00
|
|
|
//------------------------------------------------------------
|
|
|
|
void
|
|
|
|
nsGfxCheckboxControlFrame::PaintCheckBox(nsIRenderingContext& aRenderingContext,
|
|
|
|
nsPoint aPt,
|
|
|
|
const nsRect& aDirtyRect)
|
|
|
|
{
|
|
|
|
// REVIEW: moved the mAppearance test out so we avoid constructing
|
|
|
|
// a display item if it's not needed
|
|
|
|
nsRect checkRect(aPt, mRect.Size());
|
2006-12-20 04:23:45 +03:00
|
|
|
checkRect.Deflate(GetUsedBorderAndPadding());
|
1999-08-27 18:31:59 +04:00
|
|
|
|
2003-05-15 07:42:21 +04:00
|
|
|
const nsStyleColor* color = GetStyleColor();
|
1999-08-27 18:31:59 +04:00
|
|
|
aRenderingContext.SetColor(color->mColor);
|
|
|
|
|
2006-07-13 09:05:54 +04:00
|
|
|
PaintCheckMark(aRenderingContext, checkRect);
|
1999-08-06 09:13:07 +04:00
|
|
|
}
|
|
|
|
|
2000-02-09 22:34:39 +03:00
|
|
|
//------------------------------------------------------------
|
2006-01-26 05:29:17 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGfxCheckboxControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
|
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
const nsDisplayListSet& aLists)
|
1999-08-06 09:13:07 +04:00
|
|
|
{
|
2006-01-26 05:29:17 +03:00
|
|
|
nsresult rv = nsFormControlFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Get current checked state through content model.
|
|
|
|
if (!GetCheckboxState() || !IsVisibleForPainting(aBuilder))
|
|
|
|
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.
|
|
|
|
|
|
|
|
// Paint the checkmark
|
|
|
|
if (mCheckButtonFaceStyle) {
|
2006-07-13 09:05:54 +04:00
|
|
|
// This code actually works now; not sure how useful it'll be
|
|
|
|
// (The putpose is to allow the UA stylesheet can substitute its own
|
|
|
|
// checkmark for the default one)
|
|
|
|
const nsStyleBackground* myBackground = mCheckButtonFaceStyle->GetStyleBackground();
|
|
|
|
if (!myBackground->IsTransparent())
|
2006-01-26 05:29:17 +03:00
|
|
|
return aLists.Content()->AppendNewToTop(new (aBuilder)
|
|
|
|
nsDisplayGeneric(this, PaintCheckMarkFromStyle, "CheckMarkFromStyle"));
|
2001-01-27 17:09:34 +03:00
|
|
|
}
|
1999-08-06 09:13:07 +04:00
|
|
|
|
2006-01-26 05:29:17 +03:00
|
|
|
return aLists.Content()->AppendNewToTop(new (aBuilder) nsDisplayCheckMark(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGfxCheckboxControlFrame::PaintCheckBoxFromStyle(
|
|
|
|
nsIRenderingContext& aRenderingContext, nsPoint aPt, const nsRect& aDirtyRect) {
|
|
|
|
const nsStylePadding* myPadding = mCheckButtonFaceStyle->GetStylePadding();
|
|
|
|
const nsStylePosition* myPosition = mCheckButtonFaceStyle->GetStylePosition();
|
|
|
|
const nsStyleBorder* myBorder = mCheckButtonFaceStyle->GetStyleBorder();
|
2006-07-13 09:05:54 +04:00
|
|
|
const nsStyleBackground* myBackground = mCheckButtonFaceStyle->GetStyleBackground();
|
2006-01-26 05:29:17 +03:00
|
|
|
|
2007-05-04 03:11:00 +04:00
|
|
|
NS_ASSERTION(myPosition->mWidth.GetUnit() == eStyleUnit_Coord &&
|
|
|
|
myPosition->mHeight.GetUnit() == eStyleUnit_Coord,
|
|
|
|
"styles for :-moz-checkbox are incorrect or author-accessible");
|
2006-01-26 05:29:17 +03:00
|
|
|
nscoord width = myPosition->mWidth.GetCoordValue();
|
|
|
|
nscoord height = myPosition->mHeight.GetCoordValue();
|
2006-07-13 09:05:54 +04:00
|
|
|
// Position the button centered within the control's rectangle.
|
2006-01-26 05:29:17 +03:00
|
|
|
nscoord x = (mRect.width - width) / 2;
|
|
|
|
nscoord y = (mRect.height - height) / 2;
|
|
|
|
nsRect rect(aPt.x + x, aPt.y + y, width, height);
|
|
|
|
|
2007-03-31 01:11:41 +04:00
|
|
|
nsCSSRendering::PaintBackgroundWithSC(PresContext(), aRenderingContext,
|
2006-07-13 09:05:54 +04:00
|
|
|
this, aDirtyRect, rect, *myBackground,
|
|
|
|
*myBorder, *myPadding, PR_FALSE);
|
2007-03-31 01:11:41 +04:00
|
|
|
nsCSSRendering::PaintBorder(PresContext(), aRenderingContext, this,
|
2006-01-26 05:29:17 +03:00
|
|
|
aDirtyRect, rect, *myBorder, mCheckButtonFaceStyle, 0);
|
1999-08-06 09:13:07 +04:00
|
|
|
}
|
|
|
|
|
2000-02-09 22:34:39 +03:00
|
|
|
//------------------------------------------------------------
|
2002-01-24 22:04:55 +03:00
|
|
|
PRBool
|
2000-02-09 22:34:39 +03:00
|
|
|
nsGfxCheckboxControlFrame::GetCheckboxState ( )
|
1999-08-06 09:13:07 +04:00
|
|
|
{
|
2002-01-24 22:04:55 +03:00
|
|
|
nsCOMPtr<nsIDOMHTMLInputElement> elem(do_QueryInterface(mContent));
|
|
|
|
PRBool retval = PR_FALSE;
|
|
|
|
elem->GetChecked(&retval);
|
|
|
|
return retval;
|
1999-08-06 09:13:07 +04:00
|
|
|
}
|