CompositionEvent.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* -*- Mode: C++; tab-width: 8; 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 "mozilla/dom/CompositionEvent.h"
  6. #include "mozilla/TextEvents.h"
  7. #include "prtime.h"
  8. namespace mozilla {
  9. namespace dom {
  10. CompositionEvent::CompositionEvent(EventTarget* aOwner,
  11. nsPresContext* aPresContext,
  12. WidgetCompositionEvent* aEvent)
  13. : UIEvent(aOwner, aPresContext,
  14. aEvent ? aEvent :
  15. new WidgetCompositionEvent(false, eVoidEvent, nullptr))
  16. {
  17. NS_ASSERTION(mEvent->mClass == eCompositionEventClass,
  18. "event type mismatch");
  19. if (aEvent) {
  20. mEventIsInternal = false;
  21. } else {
  22. mEventIsInternal = true;
  23. mEvent->mTime = PR_Now();
  24. // XXX compositionstart is cancelable in draft of DOM3 Events.
  25. // However, it doesn't make sence for us, we cannot cancel composition
  26. // when we sends compositionstart event.
  27. mEvent->mFlags.mCancelable = false;
  28. }
  29. // XXX Do we really need to duplicate the data value?
  30. mData = mEvent->AsCompositionEvent()->mData;
  31. // TODO: Native event should have locale information.
  32. }
  33. NS_IMPL_ADDREF_INHERITED(CompositionEvent, UIEvent)
  34. NS_IMPL_RELEASE_INHERITED(CompositionEvent, UIEvent)
  35. NS_INTERFACE_MAP_BEGIN(CompositionEvent)
  36. NS_INTERFACE_MAP_END_INHERITING(UIEvent)
  37. void
  38. CompositionEvent::GetData(nsAString& aData) const
  39. {
  40. aData = mData;
  41. }
  42. void
  43. CompositionEvent::GetLocale(nsAString& aLocale) const
  44. {
  45. aLocale = mLocale;
  46. }
  47. void
  48. CompositionEvent::InitCompositionEvent(const nsAString& aType,
  49. bool aCanBubble,
  50. bool aCancelable,
  51. nsGlobalWindow* aView,
  52. const nsAString& aData,
  53. const nsAString& aLocale)
  54. {
  55. NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
  56. UIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, 0);
  57. mData = aData;
  58. mLocale = aLocale;
  59. }
  60. void
  61. CompositionEvent::GetRanges(TextClauseArray& aRanges)
  62. {
  63. // If the mRanges is not empty, we return the cached value.
  64. if (!mRanges.IsEmpty()) {
  65. aRanges = mRanges;
  66. return;
  67. }
  68. RefPtr<TextRangeArray> textRangeArray = mEvent->AsCompositionEvent()->mRanges;
  69. if (!textRangeArray) {
  70. return;
  71. }
  72. nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(mOwner);
  73. const TextRange* targetRange = textRangeArray->GetTargetClause();
  74. for (size_t i = 0; i < textRangeArray->Length(); i++) {
  75. const TextRange& range = textRangeArray->ElementAt(i);
  76. mRanges.AppendElement(new TextClause(window, range, targetRange));
  77. }
  78. aRanges = mRanges;
  79. }
  80. } // namespace dom
  81. } // namespace mozilla
  82. using namespace mozilla;
  83. using namespace mozilla::dom;
  84. already_AddRefed<CompositionEvent>
  85. NS_NewDOMCompositionEvent(EventTarget* aOwner,
  86. nsPresContext* aPresContext,
  87. WidgetCompositionEvent* aEvent)
  88. {
  89. RefPtr<CompositionEvent> event =
  90. new CompositionEvent(aOwner, aPresContext, aEvent);
  91. return event.forget();
  92. }