summaryrefslogtreecommitdiffstats
path: root/widget/ContentData.h
blob: 3e641a2684555819a1c98ee02dd88c9765a756c5 (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
/* -*- Mode: C++; tab-width: 40; 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 mozilla_ContentData_h
#define mozilla_ContentData_h

#include <sstream>

#include "mozilla/Attributes.h"
#include "mozilla/Debug.h"
#include "mozilla/EventForwards.h"
#include "mozilla/Maybe.h"
#include "mozilla/WritingModes.h"
#include "mozilla/widget/IMEData.h"

/**
 * This file is intended for declaring classes which store DOM content data for
 * widget classes.  Those data should be retrived by `WidgetQueryContentEvent`,
 * notified with `IMENotification`, or set assumed data as result of dispatching
 * widget events such as `WidgetKeyboardEvent`, `WidgetCompositionEvent`,
 * `WidgetSelectionEvent` etc.
 */

namespace mozilla {

/**
 * ContentSelection stores DOM selection in flattend text by
 * ContentEventHandler.  It should be retrieved with `eQuerySelectedText` event,
 * notified with `NOTIFY_IME_OF_SELECTION_CHANGE` or set by widget itself.
 */
class ContentSelection {
 public:
  using SelectionChangeDataBase =
      widget::IMENotification::SelectionChangeDataBase;
  ContentSelection() = default;
  explicit ContentSelection(const SelectionChangeDataBase& aSelectionChangeData)
      : mOffsetAndData(Some(aSelectionChangeData.ToUint32OffsetAndData())),
        mWritingMode(aSelectionChangeData.GetWritingMode()) {}
  explicit ContentSelection(const WidgetQueryContentEvent& aSelectedTextEvent);
  ContentSelection(uint32_t aOffset, const WritingMode& aWritingMode)
      : mOffsetAndData(Some(OffsetAndData<uint32_t>(
            aOffset, EmptyString(), OffsetAndDataFor::SelectedString))),
        mWritingMode(aWritingMode) {}

  const OffsetAndData<uint32_t>& OffsetAndDataRef() const {
    return mOffsetAndData.ref();
  }

  void Collapse(uint32_t aOffset) {
    if (mOffsetAndData.isSome()) {
      mOffsetAndData->Collapse(aOffset);
    } else {
      mOffsetAndData.emplace(aOffset, EmptyString(),
                             OffsetAndDataFor::SelectedString);
    }
  }
  void Clear() {
    mOffsetAndData.reset();
    mWritingMode = WritingMode();
  }

  bool HasRange() const { return mOffsetAndData.isSome(); }
  const WritingMode& WritingModeRef() const { return mWritingMode; }

  friend std::ostream& operator<<(std::ostream& aStream,
                                  const ContentSelection& aContentSelection) {
    if (aContentSelection.HasRange()) {
      return aStream << "{ HasRange()=false }";
    }
    aStream << "{ mOffsetAndData=" << aContentSelection.mOffsetAndData
            << ", mWritingMode=" << aContentSelection.mWritingMode << " }";
    return aStream;
  }

 private:
  Maybe<OffsetAndData<uint32_t>> mOffsetAndData;
  WritingMode mWritingMode;
};

}  // namespace mozilla

#endif  // #ifndef mozilla_ContentData_h