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