1999-08-06 09:13:07 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The contents of this file are subject to the Netscape 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/NPL/
|
1999-08-06 09:13:07 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03: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.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-08-06 09:13:07 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:40:37 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-08-06 09:13:07 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsGfxCheckboxControlFrame.h"
|
|
|
|
#include "nsICheckButton.h"
|
|
|
|
#include "nsHTMLAtoms.h"
|
|
|
|
#include "nsHTMLParts.h"
|
|
|
|
#include "nsFormFrame.h"
|
|
|
|
#include "nsIFormControl.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsWidgetsCID.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewGfxCheckboxControlFrame(nsIFrame** aNewFrame)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aNewFrame, "null OUT ptr");
|
|
|
|
if (nsnull == aNewFrame) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
nsGfxCheckboxControlFrame* it = new nsGfxCheckboxControlFrame;
|
|
|
|
if (!it) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
*aNewFrame = it;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-08-11 08:45:49 +04:00
|
|
|
|
|
|
|
// Initialize GFX-rendered state
|
1999-08-06 09:13:07 +04:00
|
|
|
nsGfxCheckboxControlFrame::nsGfxCheckboxControlFrame()
|
1999-08-11 08:45:49 +04:00
|
|
|
: mChecked(eOff)
|
1999-08-06 09:13:07 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1999-11-24 09:03:41 +03:00
|
|
|
nsGfxCheckboxControlFrame::PaintCheckBox(nsIPresContext* aPresContext,
|
1999-08-06 09:13:07 +04:00
|
|
|
nsIRenderingContext& aRenderingContext,
|
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
nsFramePaintLayer aWhichLayer)
|
|
|
|
{
|
|
|
|
aRenderingContext.PushState();
|
|
|
|
|
|
|
|
float p2t;
|
1999-11-24 09:03:41 +03:00
|
|
|
aPresContext->GetScaledPixelsToTwips(&p2t);
|
1999-08-06 09:13:07 +04:00
|
|
|
|
1999-08-27 18:31:59 +04:00
|
|
|
const nsStyleSpacing* spacing =
|
|
|
|
(const nsStyleSpacing*)mStyleContext->GetStyleData(eStyleStruct_Spacing);
|
|
|
|
|
|
|
|
nsRect checkRect(0,0, mRect.width, mRect.height);
|
|
|
|
|
|
|
|
nsMargin borderPadding;
|
|
|
|
spacing->CalcBorderPaddingFor(this, borderPadding);
|
|
|
|
checkRect.Deflate(borderPadding);
|
|
|
|
|
|
|
|
const nsStyleColor* color = (const nsStyleColor*)
|
|
|
|
mStyleContext->GetStyleData(eStyleStruct_Color);
|
|
|
|
aRenderingContext.SetColor(color->mColor);
|
|
|
|
|
1999-08-11 08:45:49 +04:00
|
|
|
if ( IsTristateCheckbox() ) {
|
|
|
|
// Get current checked state through content model.
|
|
|
|
// XXX this won't work under printing. does that matter???
|
|
|
|
CheckState checked = GetCheckboxState();
|
|
|
|
switch ( checked ) {
|
|
|
|
case eOn:
|
1999-08-27 18:31:59 +04:00
|
|
|
nsFormControlHelper::PaintCheckMark(aRenderingContext, p2t, checkRect);
|
1999-08-11 08:45:49 +04:00
|
|
|
break;
|
1999-08-27 18:31:59 +04:00
|
|
|
|
1999-08-11 08:45:49 +04:00
|
|
|
case eMixed:
|
1999-08-27 18:31:59 +04:00
|
|
|
PaintMixedMark(aRenderingContext, p2t, checkRect);
|
1999-08-11 08:45:49 +04:00
|
|
|
break;
|
1999-10-20 22:59:00 +04:00
|
|
|
|
|
|
|
// no special drawing otherwise
|
|
|
|
default:
|
|
|
|
break;
|
1999-08-11 08:45:49 +04:00
|
|
|
} // case of value of checkbox
|
1999-08-27 18:31:59 +04:00
|
|
|
} else {
|
1999-08-06 09:13:07 +04:00
|
|
|
// Get current checked state through content model.
|
|
|
|
// XXX: This is very inefficient, but it is necessary in the case of printing.
|
|
|
|
// During printing the Paint is called but the actual state of the checkbox
|
|
|
|
// is in a frame in presentation shell 0.
|
1999-08-11 08:45:49 +04:00
|
|
|
PRBool checked = PR_FALSE;
|
|
|
|
GetCurrentCheckState(&checked);
|
|
|
|
if ( checked ) {
|
1999-08-27 18:31:59 +04:00
|
|
|
nsFormControlHelper::PaintCheckMark(aRenderingContext, p2t, checkRect);
|
1999-08-11 08:45:49 +04:00
|
|
|
}
|
1999-08-06 09:13:07 +04:00
|
|
|
}
|
1999-08-11 08:45:49 +04:00
|
|
|
|
1999-08-06 09:13:07 +04:00
|
|
|
PRBool clip;
|
|
|
|
aRenderingContext.PopState(clip);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-11 08:45:49 +04:00
|
|
|
//
|
|
|
|
// PaintMixedMark
|
|
|
|
//
|
|
|
|
// Like nsFormControlHelper::PaintCheckMark(), but paints the horizontal "mixed"
|
|
|
|
// bar inside the box. Only used for tri-state checkboxes.
|
|
|
|
//
|
|
|
|
void
|
|
|
|
nsGfxCheckboxControlFrame::PaintMixedMark ( nsIRenderingContext& aRenderingContext,
|
1999-08-27 18:31:59 +04:00
|
|
|
float aPixelsToTwips, const nsRect& aRect)
|
1999-08-11 08:45:49 +04:00
|
|
|
{
|
|
|
|
const PRUint32 checkpoints = 4;
|
|
|
|
const PRUint32 checksize = 6; //This is value is determined by added 2 units to the end
|
|
|
|
//of the 7X& pixel rectangle below to provide some white space
|
|
|
|
//around the checkmark when it is rendered.
|
|
|
|
|
|
|
|
// Points come from the coordinates on a 7X7 pixels
|
|
|
|
// box with 0,0 at the lower left.
|
|
|
|
nscoord checkedPolygonDef[] = { 1,2, 5,2, 5,4, 1,4 };
|
|
|
|
// Location of the center point of the checkmark
|
|
|
|
const PRUint32 centerx = 3;
|
|
|
|
const PRUint32 centery = 3;
|
|
|
|
|
|
|
|
nsPoint checkedPolygon[checkpoints];
|
|
|
|
PRUint32 defIndex = 0;
|
|
|
|
PRUint32 polyIndex = 0;
|
|
|
|
|
|
|
|
// Scale the checkmark based on the smallest dimension
|
1999-08-27 18:31:59 +04:00
|
|
|
PRUint32 size = aRect.width / checksize;
|
|
|
|
if (aRect.height < aRect.width)
|
|
|
|
size = aRect.height / checksize;
|
1999-08-11 08:45:49 +04:00
|
|
|
|
|
|
|
// Center and offset each point in the polygon definition.
|
|
|
|
for (defIndex = 0; defIndex < (checkpoints * 2); defIndex++) {
|
1999-08-27 18:31:59 +04:00
|
|
|
checkedPolygon[polyIndex].x = nscoord((((checkedPolygonDef[defIndex]) - centerx) * (size)) + (aRect.width / 2) + aRect.x);
|
1999-08-11 08:45:49 +04:00
|
|
|
defIndex++;
|
1999-08-27 18:31:59 +04:00
|
|
|
checkedPolygon[polyIndex].y = nscoord((((checkedPolygonDef[defIndex]) - centery) * (size)) + (aRect.height / 2) + aRect.y);
|
1999-08-11 08:45:49 +04:00
|
|
|
polyIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRenderingContext.FillPolygon(checkedPolygon, checkpoints);
|
|
|
|
|
|
|
|
} // PaintMixedMark
|
|
|
|
|
|
|
|
|
1999-08-06 09:13:07 +04:00
|
|
|
NS_METHOD
|
1999-11-24 09:03:41 +03:00
|
|
|
nsGfxCheckboxControlFrame::Paint(nsIPresContext* aPresContext,
|
1999-08-06 09:13:07 +04:00
|
|
|
nsIRenderingContext& aRenderingContext,
|
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
nsFramePaintLayer aWhichLayer)
|
|
|
|
{
|
|
|
|
const nsStyleDisplay* disp = (const nsStyleDisplay*)
|
1999-08-11 08:45:49 +04:00
|
|
|
mStyleContext->GetStyleData(eStyleStruct_Display);
|
|
|
|
if (!disp->mVisible)
|
|
|
|
return NS_OK;
|
1999-08-06 09:13:07 +04:00
|
|
|
|
1999-08-11 08:45:49 +04:00
|
|
|
// Paint the background
|
1999-08-06 09:13:07 +04:00
|
|
|
Inherited::Paint(aPresContext, aRenderingContext, aDirtyRect, aWhichLayer);
|
|
|
|
if (NS_FRAME_PAINT_LAYER_FOREGROUND == aWhichLayer) {
|
1999-08-11 08:45:49 +04:00
|
|
|
// Paint the checkmark
|
1999-08-06 09:13:07 +04:00
|
|
|
PaintCheckBox(aPresContext, aRenderingContext, aDirtyRect, aWhichLayer);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-11 08:45:49 +04:00
|
|
|
nsCheckboxControlFrame::CheckState
|
|
|
|
nsGfxCheckboxControlFrame :: GetCheckboxState ( )
|
1999-08-06 09:13:07 +04:00
|
|
|
{
|
|
|
|
return mChecked;
|
|
|
|
}
|
|
|
|
|
1999-08-11 08:45:49 +04:00
|
|
|
void
|
1999-10-26 08:44:41 +04:00
|
|
|
nsGfxCheckboxControlFrame :: SetCheckboxState (nsIPresContext* aPresContext,
|
|
|
|
nsCheckboxControlFrame::CheckState aValue )
|
1999-08-06 09:13:07 +04:00
|
|
|
{
|
|
|
|
mChecked = aValue;
|
1999-10-26 08:44:41 +04:00
|
|
|
nsFormControlHelper::ForceDrawFrame(aPresContext, this);
|
1999-08-06 09:13:07 +04:00
|
|
|
}
|
|
|
|
|
1999-10-08 01:07:38 +04:00
|
|
|
#ifdef DEBUG_rods
|
|
|
|
NS_IMETHODIMP
|
1999-11-24 09:03:41 +03:00
|
|
|
nsGfxCheckboxControlFrame::Reflow(nsIPresContext* aPresContext,
|
1999-10-08 01:07:38 +04:00
|
|
|
nsHTMLReflowMetrics& aDesiredSize,
|
|
|
|
const nsHTMLReflowState& aReflowState,
|
|
|
|
nsReflowStatus& aStatus)
|
|
|
|
{
|
|
|
|
nsresult rv = nsNativeFormControlFrame::Reflow(aPresContext, aDesiredSize, aReflowState, aStatus);
|
|
|
|
|
|
|
|
COMPARE_QUIRK_SIZE("nsGfxCheckboxControlFrame", 13, 13)
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
#endif
|