Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / layout / forms / nsGfxCheckboxControlFrame.cpp
blob934372aeed48095c43db8e90571c4200327eed34
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsGfxCheckboxControlFrame.h"
39 #include "nsIContent.h"
40 #include "nsCOMPtr.h"
41 #include "nsCSSRendering.h"
42 #ifdef ACCESSIBILITY
43 #include "nsAccessibilityService.h"
44 #endif
45 #include "nsIServiceManager.h"
46 #include "nsIDOMHTMLInputElement.h"
47 #include "nsDisplayList.h"
48 #include "nsCSSAnonBoxes.h"
49 #include "nsIDOMHTMLInputElement.h"
51 static void
52 PaintCheckMark(nsIFrame* aFrame,
53 nsIRenderingContext* aCtx,
54 const nsRect& aDirtyRect,
55 nsPoint aPt)
57 nsRect rect(aPt, aFrame->GetSize());
58 rect.Deflate(aFrame->GetUsedBorderAndPadding());
60 // Points come from the coordinates on a 7X7 unit box centered at 0,0
61 const PRInt32 checkPolygonX[] = { -3, -1, 3, 3, -1, -3 };
62 const PRInt32 checkPolygonY[] = { -1, 1, -3, -1, 3, 1 };
63 const PRInt32 checkNumPoints = sizeof(checkPolygonX) / sizeof(PRInt32);
64 const PRInt32 checkSize = 9; // 2 units of padding on either side
65 // of the 7x7 unit checkmark
67 // Scale the checkmark based on the smallest dimension
68 nscoord paintScale = NS_MIN(rect.width, rect.height) / checkSize;
69 nsPoint paintCenter(rect.x + rect.width / 2,
70 rect.y + rect.height / 2);
72 nsPoint paintPolygon[checkNumPoints];
73 // Convert checkmark for screen rendering
74 for (PRInt32 polyIndex = 0; polyIndex < checkNumPoints; polyIndex++) {
75 paintPolygon[polyIndex] = paintCenter +
76 nsPoint(checkPolygonX[polyIndex] * paintScale,
77 checkPolygonY[polyIndex] * paintScale);
80 aCtx->SetColor(aFrame->GetStyleColor()->mColor);
81 aCtx->FillPolygon(paintPolygon, checkNumPoints);
84 static void
85 PaintIndeterminateMark(nsIFrame* aFrame,
86 nsIRenderingContext* aCtx,
87 const nsRect& aDirtyRect,
88 nsPoint aPt)
90 nsRect rect(aPt, aFrame->GetSize());
91 rect.Deflate(aFrame->GetUsedBorderAndPadding());
93 rect.y += (rect.height - rect.height/4) / 2;
94 rect.height /= 4;
96 aCtx->SetColor(aFrame->GetStyleColor()->mColor);
97 aCtx->FillRect(rect);
100 //------------------------------------------------------------
101 nsIFrame*
102 NS_NewGfxCheckboxControlFrame(nsIPresShell* aPresShell,
103 nsStyleContext* aContext)
105 return new (aPresShell) nsGfxCheckboxControlFrame(aContext);
108 NS_IMPL_FRAMEARENA_HELPERS(nsGfxCheckboxControlFrame)
111 //------------------------------------------------------------
112 // Initialize GFX-rendered state
113 nsGfxCheckboxControlFrame::nsGfxCheckboxControlFrame(nsStyleContext* aContext)
114 : nsFormControlFrame(aContext)
118 nsGfxCheckboxControlFrame::~nsGfxCheckboxControlFrame()
122 #ifdef ACCESSIBILITY
123 already_AddRefed<nsAccessible>
124 nsGfxCheckboxControlFrame::CreateAccessible()
126 nsAccessibilityService* accService = nsIPresShell::AccService();
127 if (accService) {
128 return accService->CreateHTMLCheckboxAccessible(mContent,
129 PresContext()->PresShell());
132 return nsnull;
134 #endif
136 //------------------------------------------------------------
137 NS_IMETHODIMP
138 nsGfxCheckboxControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
139 const nsRect& aDirtyRect,
140 const nsDisplayListSet& aLists)
142 nsresult rv = nsFormControlFrame::BuildDisplayList(aBuilder, aDirtyRect,
143 aLists);
144 NS_ENSURE_SUCCESS(rv, rv);
146 // Get current checked state through content model.
147 if ((!IsChecked() && !IsIndeterminate()) || !IsVisibleForPainting(aBuilder))
148 return NS_OK; // we're not checked or not visible, nothing to paint.
150 if (IsThemed())
151 return NS_OK; // No need to paint the checkmark. The theme will do it.
153 return aLists.Content()->AppendNewToTop(new (aBuilder)
154 nsDisplayGeneric(aBuilder, this,
155 IsIndeterminate()
156 ? PaintIndeterminateMark : PaintCheckMark,
157 "CheckedCheckbox",
158 nsDisplayItem::TYPE_CHECKED_CHECKBOX));
161 //------------------------------------------------------------
162 PRBool
163 nsGfxCheckboxControlFrame::IsChecked()
165 nsCOMPtr<nsIDOMHTMLInputElement> elem(do_QueryInterface(mContent));
166 PRBool retval = PR_FALSE;
167 elem->GetChecked(&retval);
168 return retval;
171 PRBool
172 nsGfxCheckboxControlFrame::IsIndeterminate()
174 nsCOMPtr<nsIDOMHTMLInputElement> elem(do_QueryInterface(mContent));
175 PRBool retval = PR_FALSE;
176 elem->GetIndeterminate(&retval);
177 return retval;