1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "nsGfxCheckboxControlFrame.h"
7 
8 #include "gfxUtils.h"
9 #include "mozilla/gfx/2D.h"
10 #include "nsIContent.h"
11 #include "nsCOMPtr.h"
12 #include "nsLayoutUtils.h"
13 #include "nsRenderingContext.h"
14 #include "nsIDOMHTMLInputElement.h"
15 #include "nsDisplayList.h"
16 #include <algorithm>
17 
18 using namespace mozilla;
19 using namespace mozilla::gfx;
20 
21 static void
PaintCheckMark(nsIFrame * aFrame,DrawTarget * aDrawTarget,const nsRect & aDirtyRect,nsPoint aPt)22 PaintCheckMark(nsIFrame* aFrame,
23                DrawTarget* aDrawTarget,
24                const nsRect& aDirtyRect,
25                nsPoint aPt)
26 {
27   nsRect rect(aPt, aFrame->GetSize());
28   rect.Deflate(aFrame->GetUsedBorderAndPadding());
29 
30   // Points come from the coordinates on a 7X7 unit box centered at 0,0
31   const int32_t checkPolygonX[] = { -3, -1,  3,  3, -1, -3 };
32   const int32_t checkPolygonY[] = { -1,  1, -3, -1,  3,  1 };
33   const int32_t checkNumPoints = sizeof(checkPolygonX) / sizeof(int32_t);
34   const int32_t checkSize      = 9; // 2 units of padding on either side
35                                     // of the 7x7 unit checkmark
36 
37   // Scale the checkmark based on the smallest dimension
38   nscoord paintScale = std::min(rect.width, rect.height) / checkSize;
39   nsPoint paintCenter(rect.x + rect.width  / 2,
40                       rect.y + rect.height / 2);
41 
42   RefPtr<PathBuilder> builder = aDrawTarget->CreatePathBuilder();
43   nsPoint p = paintCenter + nsPoint(checkPolygonX[0] * paintScale,
44                                     checkPolygonY[0] * paintScale);
45 
46   int32_t appUnitsPerDevPixel = aFrame->PresContext()->AppUnitsPerDevPixel();
47   builder->MoveTo(NSPointToPoint(p, appUnitsPerDevPixel));
48   for (int32_t polyIndex = 1; polyIndex < checkNumPoints; polyIndex++) {
49     p = paintCenter + nsPoint(checkPolygonX[polyIndex] * paintScale,
50                               checkPolygonY[polyIndex] * paintScale);
51     builder->LineTo(NSPointToPoint(p, appUnitsPerDevPixel));
52   }
53   RefPtr<Path> path = builder->Finish();
54   aDrawTarget->Fill(path,
55                     ColorPattern(ToDeviceColor(aFrame->StyleColor()->mColor)));
56 }
57 
58 static void
PaintIndeterminateMark(nsIFrame * aFrame,DrawTarget * aDrawTarget,const nsRect & aDirtyRect,nsPoint aPt)59 PaintIndeterminateMark(nsIFrame* aFrame,
60                        DrawTarget* aDrawTarget,
61                        const nsRect& aDirtyRect,
62                        nsPoint aPt)
63 {
64   int32_t appUnitsPerDevPixel = aFrame->PresContext()->AppUnitsPerDevPixel();
65 
66   nsRect rect(aPt, aFrame->GetSize());
67   rect.Deflate(aFrame->GetUsedBorderAndPadding());
68   rect.y += (rect.height - rect.height/4) / 2;
69   rect.height /= 4;
70 
71   Rect devPxRect = NSRectToSnappedRect(rect, appUnitsPerDevPixel, *aDrawTarget);
72 
73   aDrawTarget->FillRect(
74     devPxRect, ColorPattern(ToDeviceColor(aFrame->StyleColor()->mColor)));
75 }
76 
77 //------------------------------------------------------------
78 nsIFrame*
NS_NewGfxCheckboxControlFrame(nsIPresShell * aPresShell,nsStyleContext * aContext)79 NS_NewGfxCheckboxControlFrame(nsIPresShell* aPresShell,
80                               nsStyleContext* aContext)
81 {
82   return new (aPresShell) nsGfxCheckboxControlFrame(aContext);
83 }
84 
NS_IMPL_FRAMEARENA_HELPERS(nsGfxCheckboxControlFrame)85 NS_IMPL_FRAMEARENA_HELPERS(nsGfxCheckboxControlFrame)
86 
87 
88 //------------------------------------------------------------
89 // Initialize GFX-rendered state
90 nsGfxCheckboxControlFrame::nsGfxCheckboxControlFrame(nsStyleContext* aContext)
91 : nsFormControlFrame(aContext)
92 {
93 }
94 
~nsGfxCheckboxControlFrame()95 nsGfxCheckboxControlFrame::~nsGfxCheckboxControlFrame()
96 {
97 }
98 
99 #ifdef ACCESSIBILITY
100 a11y::AccType
AccessibleType()101 nsGfxCheckboxControlFrame::AccessibleType()
102 {
103   return a11y::eHTMLCheckboxType;
104 }
105 #endif
106 
107 //------------------------------------------------------------
108 void
BuildDisplayList(nsDisplayListBuilder * aBuilder,const nsRect & aDirtyRect,const nsDisplayListSet & aLists)109 nsGfxCheckboxControlFrame::BuildDisplayList(nsDisplayListBuilder*   aBuilder,
110                                             const nsRect&           aDirtyRect,
111                                             const nsDisplayListSet& aLists)
112 {
113   nsFormControlFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);
114 
115   // Get current checked state through content model.
116   if ((!IsChecked() && !IsIndeterminate()) || !IsVisibleForPainting(aBuilder))
117     return;   // we're not checked or not visible, nothing to paint.
118 
119   if (IsThemed())
120     return; // No need to paint the checkmark. The theme will do it.
121 
122   aLists.Content()->AppendNewToTop(new (aBuilder)
123     nsDisplayGeneric(aBuilder, this,
124                      IsIndeterminate()
125                      ? PaintIndeterminateMark : PaintCheckMark,
126                      "CheckedCheckbox",
127                      nsDisplayItem::TYPE_CHECKED_CHECKBOX));
128 }
129 
130 //------------------------------------------------------------
131 bool
IsChecked()132 nsGfxCheckboxControlFrame::IsChecked()
133 {
134   nsCOMPtr<nsIDOMHTMLInputElement> elem(do_QueryInterface(mContent));
135   bool retval = false;
136   elem->GetChecked(&retval);
137   return retval;
138 }
139 
140 bool
IsIndeterminate()141 nsGfxCheckboxControlFrame::IsIndeterminate()
142 {
143   nsCOMPtr<nsIDOMHTMLInputElement> elem(do_QueryInterface(mContent));
144   bool retval = false;
145   elem->GetIndeterminate(&retval);
146   return retval;
147 }
148