nsGfxCheckboxControlFrame.cpp 4.8 KB

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