nsGfxRadioControlFrame.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "nsGfxRadioControlFrame.h"
  6. #include "gfx2DGlue.h"
  7. #include "gfxUtils.h"
  8. #include "mozilla/gfx/2D.h"
  9. #include "mozilla/gfx/PathHelpers.h"
  10. #include "nsLayoutUtils.h"
  11. #include "nsRenderingContext.h"
  12. #include "nsDisplayList.h"
  13. using namespace mozilla;
  14. using namespace mozilla::gfx;
  15. nsIFrame*
  16. NS_NewGfxRadioControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
  17. {
  18. return new (aPresShell) nsGfxRadioControlFrame(aContext);
  19. }
  20. NS_IMPL_FRAMEARENA_HELPERS(nsGfxRadioControlFrame)
  21. nsGfxRadioControlFrame::nsGfxRadioControlFrame(nsStyleContext* aContext):
  22. nsFormControlFrame(aContext)
  23. {
  24. }
  25. nsGfxRadioControlFrame::~nsGfxRadioControlFrame()
  26. {
  27. }
  28. #ifdef ACCESSIBILITY
  29. a11y::AccType
  30. nsGfxRadioControlFrame::AccessibleType()
  31. {
  32. return a11y::eHTMLRadioButtonType;
  33. }
  34. #endif
  35. //--------------------------------------------------------------
  36. // Draw the dot for a non-native radio button in the checked state.
  37. static void
  38. PaintCheckedRadioButton(nsIFrame* aFrame,
  39. DrawTarget* aDrawTarget,
  40. const nsRect& aDirtyRect,
  41. nsPoint aPt)
  42. {
  43. // The dot is an ellipse 2px on all sides smaller than the content-box,
  44. // drawn in the foreground color.
  45. nsRect rect(aPt, aFrame->GetSize());
  46. rect.Deflate(aFrame->GetUsedBorderAndPadding());
  47. rect.Deflate(nsPresContext::CSSPixelsToAppUnits(2),
  48. nsPresContext::CSSPixelsToAppUnits(2));
  49. Rect devPxRect =
  50. ToRect(nsLayoutUtils::RectToGfxRect(rect,
  51. aFrame->PresContext()->AppUnitsPerDevPixel()));
  52. ColorPattern color(ToDeviceColor(aFrame->StyleColor()->mColor));
  53. RefPtr<PathBuilder> builder = aDrawTarget->CreatePathBuilder();
  54. AppendEllipseToPath(builder, devPxRect.Center(), devPxRect.Size());
  55. RefPtr<Path> ellipse = builder->Finish();
  56. aDrawTarget->Fill(ellipse, color);
  57. }
  58. void
  59. nsGfxRadioControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
  60. const nsDisplayListSet& aLists)
  61. {
  62. nsFormControlFrame::BuildDisplayList(aBuilder, aLists);
  63. if (!IsVisibleForPainting(aBuilder))
  64. return;
  65. if (IsThemed())
  66. return; // The theme will paint the check, if any.
  67. bool checked = true;
  68. GetCurrentCheckState(&checked); // Get check state from the content model
  69. if (!checked)
  70. return;
  71. aLists.Content()->AppendNewToTop(new (aBuilder)
  72. nsDisplayGeneric(aBuilder, this, PaintCheckedRadioButton,
  73. "CheckedRadioButton",
  74. nsDisplayItem::TYPE_CHECKED_RADIOBUTTON));
  75. }