summaryrefslogtreecommitdiffstats
path: root/accessible/base/AccEvent.h
blob: e56506fdea7a3a3b33d171c553a9ea0d8b57f8d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef _AccEvent_H_
#define _AccEvent_H_

#include "nsIAccessibleEvent.h"

#include "mozilla/a11y/LocalAccessible.h"

class nsEventShell;
namespace mozilla {

namespace dom {
class Selection;
}

namespace a11y {

class DocAccessible;
class EventQueue;
class TextRange;

// Constants used to point whether the event is from user input.
enum EIsFromUserInput {
  // eNoUserInput: event is not from user input
  eNoUserInput = 0,
  // eFromUserInput: event is from user input
  eFromUserInput = 1,
  // eAutoDetect: the value should be obtained from event state manager
  eAutoDetect = -1
};

/**
 * Generic accessible event.
 */
class AccEvent {
 public:
  // Rule for accessible events.
  // The rule will be applied when flushing pending events.
  enum EEventRule {
    // eAllowDupes : More than one event of the same type is allowed.
    //    This event will always be emitted. This flag is used for events that
    //    don't support coalescence.
    eAllowDupes,

    // eCoalesceReorder : For reorder events from the same subtree or the same
    //    node, only the umbrella event on the ancestor will be emitted.
    eCoalesceReorder,

    // eCoalesceOfSameType : For events of the same type, only the newest event
    // will be processed.
    eCoalesceOfSameType,

    // eCoalesceSelectionChange: coalescence of selection change events.
    eCoalesceSelectionChange,

    // eCoalesceStateChange: coalesce state change events.
    eCoalesceStateChange,

    // eCoalesceTextSelChange: coalescence of text selection change events.
    eCoalesceTextSelChange,

    // eRemoveDupes : For repeat events, only the newest event in queue
    //    will be emitted.
    eRemoveDupes,

    // eDoNotEmit : This event is confirmed as a duplicate, do not emit it.
    eDoNotEmit
  };

  // Initialize with an accessible.
  AccEvent(uint32_t aEventType, LocalAccessible* aAccessible,
           EIsFromUserInput aIsFromUserInput = eAutoDetect,
           EEventRule aEventRule = eRemoveDupes);

  // AccEvent
  uint32_t GetEventType() const { return mEventType; }
  EEventRule GetEventRule() const { return mEventRule; }
  bool IsFromUserInput() const { return mIsFromUserInput; }
  EIsFromUserInput FromUserInput() const {
    return static_cast<EIsFromUserInput>(mIsFromUserInput);
  }

  LocalAccessible* GetAccessible() const { return mAccessible; }
  DocAccessible* Document() const { return mAccessible->Document(); }

  /**
   * Down casting.
   */
  enum EventGroup {
    eGenericEvent,
    eStateChangeEvent,
    eTextChangeEvent,
    eTreeMutationEvent,
    eMutationEvent,
    eReorderEvent,
    eHideEvent,
    eShowEvent,
    eCaretMoveEvent,
    eTextSelChangeEvent,
    eSelectionChangeEvent,
    eTableChangeEvent,
    eVirtualCursorChangeEvent,
    eObjectAttrChangedEvent,
    eScrollingEvent,
    eAnnouncementEvent,
  };

  static const EventGroup kEventGroup = eGenericEvent;
  virtual unsigned int GetEventGroups() const { return 1U << eGenericEvent; }

  /**
   * Reference counting and cycle collection.
   */
  NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(AccEvent)
  NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(AccEvent)

 protected:
  virtual ~AccEvent() {}

  bool mIsFromUserInput;
  uint32_t mEventType;
  EEventRule mEventRule;
  RefPtr<LocalAccessible> mAccessible;

  friend class EventQueue;
  friend class EventTree;
  friend class ::nsEventShell;
  friend class NotificationController;
};

/**
 * Accessible state change event.
 */
class AccStateChangeEvent : public AccEvent {
 public:
  AccStateChangeEvent(LocalAccessible* aAccessible, uint64_t aState,
                      bool aIsEnabled,
                      EIsFromUserInput aIsFromUserInput = eAutoDetect)
      : AccEvent(nsIAccessibleEvent::EVENT_STATE_CHANGE, aAccessible,
                 aIsFromUserInput, eCoalesceStateChange),
        mState(aState),
        mIsEnabled(aIsEnabled) {}

  AccStateChangeEvent(LocalAccessible* aAccessible, uint64_t aState)
      : AccEvent(::nsIAccessibleEvent::EVENT_STATE_CHANGE, aAccessible,
                 eAutoDetect, eCoalesceStateChange),
        mState(aState) {
    mIsEnabled = (mAccessible->State() & mState) != 0;
  }

  // AccEvent
  static const EventGroup kEventGroup = eStateChangeEvent;
  virtual unsigned int GetEventGroups() const override {
    return AccEvent::GetEventGroups() | (1U << eStateChangeEvent);
  }

  // AccStateChangeEvent
  uint64_t GetState() const { return mState; }
  bool IsStateEnabled() const { return mIsEnabled; }

 private:
  uint64_t mState;
  bool mIsEnabled;

  friend class EventQueue;
};

/**
 * Accessible text change event.
 */
class AccTextChangeEvent : public AccEvent {
 public:
  AccTextChangeEvent(LocalAccessible* aAccessible, int32_t aStart,
                     const nsAString& aModifiedText, bool aIsInserted,
                     EIsFromUserInput aIsFromUserInput = eAutoDetect);

  // AccEvent
  static const EventGroup kEventGroup = eTextChangeEvent;
  virtual unsigned int GetEventGroups() const override {
    return AccEvent::GetEventGroups() | (1U << eTextChangeEvent);
  }

  // AccTextChangeEvent
  int32_t GetStartOffset() const { return mStart; }
  uint32_t GetLength() const { return mModifiedText.Length(); }
  bool IsTextInserted() const { return mIsInserted; }
  void GetModifiedText(nsAString& aModifiedText) {
    aModifiedText = mModifiedText;
  }
  const nsString& ModifiedText() const { return mModifiedText; }

 private:
  int32_t mStart;
  bool mIsInserted;
  nsString mModifiedText;

  friend class EventTree;
  friend class NotificationController;
};

/**
 * A base class for events related to tree mutation, either an AccMutation
 * event, or an AccReorderEvent.
 */
class AccTreeMutationEvent : public AccEvent {
 public:
  AccTreeMutationEvent(uint32_t aEventType, LocalAccessible* aTarget)
      : AccEvent(aEventType, aTarget, eAutoDetect, eCoalesceReorder),
        mGeneration(0) {}

  // Event
  static const EventGroup kEventGroup = eTreeMutationEvent;
  virtual unsigned int GetEventGroups() const override {
    return AccEvent::GetEventGroups() | (1U << eTreeMutationEvent);
  }

  void SetNextEvent(AccTreeMutationEvent* aNext) { mNextEvent = aNext; }
  void SetPrevEvent(AccTreeMutationEvent* aPrev) { mPrevEvent = aPrev; }
  AccTreeMutationEvent* NextEvent() const { return mNextEvent; }
  AccTreeMutationEvent* PrevEvent() const { return mPrevEvent; }

  /**
   * A sequence number to know when this event was fired.
   */
  uint32_t EventGeneration() const { return mGeneration; }
  void SetEventGeneration(uint32_t aGeneration) { mGeneration = aGeneration; }

 private:
  RefPtr<AccTreeMutationEvent> mNextEvent;
  RefPtr<AccTreeMutationEvent> mPrevEvent;
  uint32_t mGeneration;
};

/**
 * Base class for show and hide accessible events.
 */
class AccMutationEvent : public AccTreeMutationEvent {
 public:
  AccMutationEvent(uint32_t aEventType, LocalAccessible* aTarget)
      : AccTreeMutationEvent(aEventType, aTarget) {
    // Don't coalesce these since they are coalesced by reorder event. Coalesce
    // contained text change events.
    mParent = mAccessible->LocalParent();
  }
  virtual ~AccMutationEvent() {}

  // Event
  static const EventGroup kEventGroup = eMutationEvent;
  virtual unsigned int GetEventGroups() const override {
    return AccTreeMutationEvent::GetEventGroups() | (1U << eMutationEvent);
  }

  // MutationEvent
  bool IsShow() const { return mEventType == nsIAccessibleEvent::EVENT_SHOW; }
  bool IsHide() const { return mEventType == nsIAccessibleEvent::EVENT_HIDE; }

  LocalAccessible* LocalParent() const { return mParent; }

 protected:
  RefPtr<LocalAccessible> mParent;
  RefPtr<AccTextChangeEvent> mTextChangeEvent;

  friend class EventTree;
  friend class NotificationController;
};

/**
 * Accessible hide event.
 */
class AccHideEvent : public AccMutationEvent {
 public:
  explicit AccHideEvent(LocalAccessible* aTarget, bool aNeedsShutdown = true);

  // Event
  static const EventGroup kEventGroup = eHideEvent;
  virtual unsigned int GetEventGroups() const override {
    return AccMutationEvent::GetEventGroups() | (1U << eHideEvent);
  }

  // AccHideEvent
  LocalAccessible* TargetParent() const { return mParent; }
  LocalAccessible* TargetNextSibling() const { return mNextSibling; }
  LocalAccessible* TargetPrevSibling() const { return mPrevSibling; }
  bool NeedsShutdown() const { return mNeedsShutdown; }

 protected:
  bool mNeedsShutdown;
  RefPtr<LocalAccessible> mNextSibling;
  RefPtr<LocalAccessible> mPrevSibling;

  friend class EventTree;
  friend class NotificationController;
};

/**
 * Accessible show event.
 */
class AccShowEvent : public AccMutationEvent {
 public:
  explicit AccShowEvent(LocalAccessible* aTarget)
      : AccMutationEvent(::nsIAccessibleEvent::EVENT_SHOW, aTarget) {}

  // Event
  static const EventGroup kEventGroup = eShowEvent;
  virtual unsigned int GetEventGroups() const override {
    return AccMutationEvent::GetEventGroups() | (1U << eShowEvent);
  }
};

/**
 * Class for reorder accessible event. Takes care about
 */
class AccReorderEvent : public AccTreeMutationEvent {
 public:
  explicit AccReorderEvent(LocalAccessible* aTarget)
      : AccTreeMutationEvent(::nsIAccessibleEvent::EVENT_REORDER, aTarget) {}
  virtual ~AccReorderEvent() {}

  // Event
  static const EventGroup kEventGroup = eReorderEvent;
  virtual unsigned int GetEventGroups() const override {
    return AccTreeMutationEvent::GetEventGroups() | (1U << eReorderEvent);
  }

  /*
   * Make this an inner reorder event that is coalesced into
   * a reorder event of an ancestor.
   */
  void SetInner() { mEventType = ::nsIAccessibleEvent::EVENT_INNER_REORDER; }
};

/**
 * Accessible caret move event.
 */
class AccCaretMoveEvent : public AccEvent {
 public:
  AccCaretMoveEvent(LocalAccessible* aAccessible, int32_t aCaretOffset,
                    bool aIsSelectionCollapsed, bool aIsAtEndOfLine,
                    int32_t aGranularity,
                    EIsFromUserInput aIsFromUserInput = eAutoDetect)
      : AccEvent(::nsIAccessibleEvent::EVENT_TEXT_CARET_MOVED, aAccessible,
                 aIsFromUserInput),
        mCaretOffset(aCaretOffset),
        mIsSelectionCollapsed(aIsSelectionCollapsed),
        mIsAtEndOfLine(aIsAtEndOfLine),
        mGranularity(aGranularity) {}
  virtual ~AccCaretMoveEvent() {}

  // AccEvent
  static const EventGroup kEventGroup = eCaretMoveEvent;
  virtual unsigned int GetEventGroups() const override {
    return AccEvent::GetEventGroups() | (1U << eCaretMoveEvent);
  }

  // AccCaretMoveEvent
  int32_t GetCaretOffset() const { return mCaretOffset; }

  bool IsSelectionCollapsed() const { return mIsSelectionCollapsed; }
  bool IsAtEndOfLine() { return mIsAtEndOfLine; }

  int32_t GetGranularity() const { return mGranularity; }

 private:
  int32_t mCaretOffset;

  bool mIsSelectionCollapsed;
  bool mIsAtEndOfLine;
  int32_t mGranularity;
};

/**
 * Accessible text selection change event.
 */
class AccTextSelChangeEvent : public AccEvent {
 public:
  AccTextSelChangeEvent(HyperTextAccessible* aTarget,
                        dom::Selection* aSelection, int32_t aReason,
                        int32_t aGranularity);
  virtual ~AccTextSelChangeEvent();

  // AccEvent
  static const EventGroup kEventGroup = eTextSelChangeEvent;
  virtual unsigned int GetEventGroups() const override {
    return AccEvent::GetEventGroups() | (1U << eTextSelChangeEvent);
  }

  // AccTextSelChangeEvent

  /**
   * Return true if the text selection change wasn't caused by pure caret move.
   */
  bool IsCaretMoveOnly() const;

  int32_t GetGranularity() const { return mGranularity; }

  /**
   * Return selection ranges in document/control.
   */
  void SelectionRanges(nsTArray<a11y::TextRange>* aRanges) const;

 private:
  RefPtr<dom::Selection> mSel;
  int32_t mReason;
  int32_t mGranularity;

  friend class EventQueue;
  friend class SelectionManager;
};

/**
 * Accessible widget selection change event.
 */
class AccSelChangeEvent : public AccEvent {
 public:
  enum SelChangeType { eSelectionAdd, eSelectionRemove };

  AccSelChangeEvent(LocalAccessible* aWidget, LocalAccessible* aItem,
                    SelChangeType aSelChangeType);

  virtual ~AccSelChangeEvent() {}

  // AccEvent
  static const EventGroup kEventGroup = eSelectionChangeEvent;
  virtual unsigned int GetEventGroups() const override {
    return AccEvent::GetEventGroups() | (1U << eSelectionChangeEvent);
  }

  // AccSelChangeEvent
  LocalAccessible* Widget() const { return mWidget; }

 private:
  RefPtr<LocalAccessible> mWidget;
  RefPtr<LocalAccessible> mItem;
  SelChangeType mSelChangeType;
  uint32_t mPreceedingCount;
  AccSelChangeEvent* mPackedEvent;

  friend class EventQueue;
};

/**
 * Accessible table change event.
 */
class AccTableChangeEvent : public AccEvent {
 public:
  AccTableChangeEvent(LocalAccessible* aAccessible, uint32_t aEventType,
                      int32_t aRowOrColIndex, int32_t aNumRowsOrCols);

  // AccEvent
  static const EventGroup kEventGroup = eTableChangeEvent;
  virtual unsigned int GetEventGroups() const override {
    return AccEvent::GetEventGroups() | (1U << eTableChangeEvent);
  }

  // AccTableChangeEvent
  uint32_t GetIndex() const { return mRowOrColIndex; }
  uint32_t GetCount() const { return mNumRowsOrCols; }

 private:
  uint32_t mRowOrColIndex;  // the start row/column after which the rows are
                            // inserted/deleted.
  uint32_t mNumRowsOrCols;  // the number of inserted/deleted rows/columns
};

/**
 * Accessible virtual cursor change event.
 */
class AccVCChangeEvent : public AccEvent {
 public:
  AccVCChangeEvent(LocalAccessible* aAccessible,
                   LocalAccessible* aOldAccessible, int32_t aOldStart,
                   int32_t aOldEnd, LocalAccessible* aNewAccessible,
                   int32_t aNewStart, int32_t aNewEnd, int16_t aReason,
                   int16_t aBoundaryType,
                   EIsFromUserInput aIsFromUserInput = eFromUserInput);

  virtual ~AccVCChangeEvent() {}

  // AccEvent
  static const EventGroup kEventGroup = eVirtualCursorChangeEvent;
  virtual unsigned int GetEventGroups() const override {
    return AccEvent::GetEventGroups() | (1U << eVirtualCursorChangeEvent);
  }

  // AccVCChangeEvent
  LocalAccessible* OldAccessible() const { return mOldAccessible; }
  int32_t OldStartOffset() const { return mOldStart; }
  int32_t OldEndOffset() const { return mOldEnd; }
  LocalAccessible* NewAccessible() const { return mNewAccessible; }
  int32_t NewStartOffset() const { return mNewStart; }
  int32_t NewEndOffset() const { return mNewEnd; }
  int32_t Reason() const { return mReason; }
  int32_t BoundaryType() const { return mBoundaryType; }

 private:
  RefPtr<LocalAccessible> mOldAccessible;
  RefPtr<LocalAccessible> mNewAccessible;
  int32_t mOldStart;
  int32_t mNewStart;
  int32_t mOldEnd;
  int32_t mNewEnd;
  int16_t mReason;
  int16_t mBoundaryType;
};

/**
 * Accessible object attribute changed event.
 */
class AccObjectAttrChangedEvent : public AccEvent {
 public:
  AccObjectAttrChangedEvent(LocalAccessible* aAccessible, nsAtom* aAttribute)
      : AccEvent(::nsIAccessibleEvent::EVENT_OBJECT_ATTRIBUTE_CHANGED,
                 aAccessible),
        mAttribute(aAttribute) {}

  // AccEvent
  static const EventGroup kEventGroup = eObjectAttrChangedEvent;
  virtual unsigned int GetEventGroups() const override {
    return AccEvent::GetEventGroups() | (1U << eObjectAttrChangedEvent);
  }

  // AccObjectAttrChangedEvent
  nsAtom* GetAttribute() const { return mAttribute; }

 private:
  RefPtr<nsAtom> mAttribute;

  virtual ~AccObjectAttrChangedEvent() {}
};

/**
 * Accessible scroll event.
 */
class AccScrollingEvent : public AccEvent {
 public:
  AccScrollingEvent(uint32_t aEventType, LocalAccessible* aAccessible,
                    uint32_t aScrollX, uint32_t aScrollY, uint32_t aMaxScrollX,
                    uint32_t aMaxScrollY)
      : AccEvent(aEventType, aAccessible),
        mScrollX(aScrollX),
        mScrollY(aScrollY),
        mMaxScrollX(aMaxScrollX),
        mMaxScrollY(aMaxScrollY) {}

  virtual ~AccScrollingEvent() {}

  // AccEvent
  static const EventGroup kEventGroup = eScrollingEvent;
  virtual unsigned int GetEventGroups() const override {
    return AccEvent::GetEventGroups() | (1U << eScrollingEvent);
  }

  // The X scrolling offset of the container when the event was fired.
  uint32_t ScrollX() { return mScrollX; }
  // The Y scrolling offset of the container when the event was fired.
  uint32_t ScrollY() { return mScrollY; }
  // The max X offset of the container.
  uint32_t MaxScrollX() { return mMaxScrollX; }
  // The max Y offset of the container.
  uint32_t MaxScrollY() { return mMaxScrollY; }

 private:
  uint32_t mScrollX;
  uint32_t mScrollY;
  uint32_t mMaxScrollX;
  uint32_t mMaxScrollY;
};

/**
 * Accessible announcement event.
 */
class AccAnnouncementEvent : public AccEvent {
 public:
  AccAnnouncementEvent(LocalAccessible* aAccessible,
                       const nsAString& aAnnouncement, uint16_t aPriority)
      : AccEvent(nsIAccessibleEvent::EVENT_ANNOUNCEMENT, aAccessible),
        mAnnouncement(aAnnouncement),
        mPriority(aPriority) {}

  virtual ~AccAnnouncementEvent() {}

  // AccEvent
  static const EventGroup kEventGroup = eAnnouncementEvent;
  virtual unsigned int GetEventGroups() const override {
    return AccEvent::GetEventGroups() | (1U << eAnnouncementEvent);
  }

  const nsString& Announcement() const { return mAnnouncement; }

  uint16_t Priority() { return mPriority; }

 private:
  nsString mAnnouncement;
  uint16_t mPriority;
};

/**
 * Downcast the generic accessible event object to derived type.
 */
class downcast_accEvent {
 public:
  explicit downcast_accEvent(AccEvent* e) : mRawPtr(e) {}

  template <class Destination>
  operator Destination*() {
    if (!mRawPtr) return nullptr;

    return mRawPtr->GetEventGroups() & (1U << Destination::kEventGroup)
               ? static_cast<Destination*>(mRawPtr)
               : nullptr;
  }

 private:
  AccEvent* mRawPtr;
};

/**
 * Return a new xpcom accessible event for the given internal one.
 */
already_AddRefed<nsIAccessibleEvent> MakeXPCEvent(AccEvent* aEvent);

}  // namespace a11y
}  // namespace mozilla

#endif