summaryrefslogtreecommitdiffstats
path: root/editor/composer/ComposerCommandsUpdater.cpp
blob: efeb9d95441b08de059b81e7162ce676febd22a9 (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
/* -*- 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/. */

#include "mozilla/ComposerCommandsUpdater.h"

#include "mozilla/mozalloc.h"            // for operator new
#include "mozilla/TransactionManager.h"  // for TransactionManager
#include "mozilla/dom/Selection.h"
#include "nsCommandManager.h"            // for nsCommandManager
#include "nsComponentManagerUtils.h"     // for do_CreateInstance
#include "nsDebug.h"                     // for NS_ENSURE_TRUE, etc
#include "nsDocShell.h"                  // for nsIDocShell
#include "nsError.h"                     // for NS_OK, NS_ERROR_FAILURE, etc
#include "nsID.h"                        // for NS_GET_IID, etc
#include "nsIInterfaceRequestorUtils.h"  // for do_GetInterface
#include "nsLiteralString.h"             // for NS_LITERAL_STRING
#include "nsPIDOMWindow.h"               // for nsPIDOMWindow

class nsITransaction;

namespace mozilla {

ComposerCommandsUpdater::ComposerCommandsUpdater()
    : mDirtyState(eStateUninitialized),
      mSelectionCollapsed(eStateUninitialized),
      mFirstDoOfFirstUndo(true) {}

ComposerCommandsUpdater::~ComposerCommandsUpdater() {
  // cancel any outstanding update timer
  if (mUpdateTimer) {
    mUpdateTimer->Cancel();
  }
}

NS_IMPL_CYCLE_COLLECTING_ADDREF(ComposerCommandsUpdater)
NS_IMPL_CYCLE_COLLECTING_RELEASE(ComposerCommandsUpdater)

NS_INTERFACE_MAP_BEGIN(ComposerCommandsUpdater)
  NS_INTERFACE_MAP_ENTRY(nsITimerCallback)
  NS_INTERFACE_MAP_ENTRY(nsINamed)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsITimerCallback)
  NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(ComposerCommandsUpdater)
NS_INTERFACE_MAP_END

NS_IMPL_CYCLE_COLLECTION(ComposerCommandsUpdater, mUpdateTimer, mDOMWindow,
                         mDocShell)

#if 0
#  pragma mark -
#endif

void ComposerCommandsUpdater::DidDoTransaction(
    TransactionManager& aTransactionManager) {
  // only need to update if the status of the Undo menu item changes.
  if (aTransactionManager.NumberOfUndoItems() == 1) {
    if (mFirstDoOfFirstUndo) {
      UpdateCommandGroup(CommandGroup::Undo);
    }
    mFirstDoOfFirstUndo = false;
  }
}

void ComposerCommandsUpdater::DidUndoTransaction(
    TransactionManager& aTransactionManager) {
  if (!aTransactionManager.NumberOfUndoItems()) {
    mFirstDoOfFirstUndo = true;  // reset the state for the next do
  }
  UpdateCommandGroup(CommandGroup::Undo);
}

void ComposerCommandsUpdater::DidRedoTransaction(
    TransactionManager& aTransactionManager) {
  UpdateCommandGroup(CommandGroup::Undo);
}

#if 0
#  pragma mark -
#endif

void ComposerCommandsUpdater::Init(nsPIDOMWindowOuter& aDOMWindow) {
  mDOMWindow = &aDOMWindow;
  mDocShell = aDOMWindow.GetDocShell();
}

nsresult ComposerCommandsUpdater::PrimeUpdateTimer() {
  if (!mUpdateTimer) {
    mUpdateTimer = NS_NewTimer();
  }
  const uint32_t kUpdateTimerDelay = 150;
  return mUpdateTimer->InitWithCallback(static_cast<nsITimerCallback*>(this),
                                        kUpdateTimerDelay,
                                        nsITimer::TYPE_ONE_SHOT);
}

MOZ_CAN_RUN_SCRIPT_BOUNDARY
void ComposerCommandsUpdater::TimerCallback() {
  mSelectionCollapsed = SelectionIsCollapsed();
  UpdateCommandGroup(CommandGroup::Style);
}

void ComposerCommandsUpdater::UpdateCommandGroup(CommandGroup aCommandGroup) {
  RefPtr<nsCommandManager> commandManager = GetCommandManager();
  if (NS_WARN_IF(!commandManager)) {
    return;
  }

  switch (aCommandGroup) {
    case CommandGroup::Undo:
      commandManager->CommandStatusChanged("cmd_undo");
      commandManager->CommandStatusChanged("cmd_redo");
      return;
    case CommandGroup::Style:
      commandManager->CommandStatusChanged("cmd_bold");
      commandManager->CommandStatusChanged("cmd_italic");
      commandManager->CommandStatusChanged("cmd_underline");
      commandManager->CommandStatusChanged("cmd_tt");

      commandManager->CommandStatusChanged("cmd_strikethrough");
      commandManager->CommandStatusChanged("cmd_superscript");
      commandManager->CommandStatusChanged("cmd_subscript");
      commandManager->CommandStatusChanged("cmd_nobreak");

      commandManager->CommandStatusChanged("cmd_em");
      commandManager->CommandStatusChanged("cmd_strong");
      commandManager->CommandStatusChanged("cmd_cite");
      commandManager->CommandStatusChanged("cmd_abbr");
      commandManager->CommandStatusChanged("cmd_acronym");
      commandManager->CommandStatusChanged("cmd_code");
      commandManager->CommandStatusChanged("cmd_samp");
      commandManager->CommandStatusChanged("cmd_var");

      commandManager->CommandStatusChanged("cmd_increaseFont");
      commandManager->CommandStatusChanged("cmd_decreaseFont");

      commandManager->CommandStatusChanged("cmd_paragraphState");
      commandManager->CommandStatusChanged("cmd_fontFace");
      commandManager->CommandStatusChanged("cmd_fontColor");
      commandManager->CommandStatusChanged("cmd_backgroundColor");
      commandManager->CommandStatusChanged("cmd_highlight");
      return;
    case CommandGroup::Save:
      commandManager->CommandStatusChanged("cmd_setDocumentModified");
      commandManager->CommandStatusChanged("cmd_save");
      return;
    default:
      MOZ_ASSERT_UNREACHABLE("New command group hasn't been implemented yet");
  }
}

nsresult ComposerCommandsUpdater::UpdateOneCommand(const char* aCommand) {
  RefPtr<nsCommandManager> commandManager = GetCommandManager();
  NS_ENSURE_TRUE(commandManager, NS_ERROR_FAILURE);
  commandManager->CommandStatusChanged(aCommand);
  return NS_OK;
}

bool ComposerCommandsUpdater::SelectionIsCollapsed() {
  if (NS_WARN_IF(!mDOMWindow)) {
    return true;
  }

  RefPtr<dom::Selection> domSelection = mDOMWindow->GetSelection();
  if (NS_WARN_IF(!domSelection)) {
    return false;
  }

  return domSelection->IsCollapsed();
}

nsCommandManager* ComposerCommandsUpdater::GetCommandManager() {
  if (NS_WARN_IF(!mDocShell)) {
    return nullptr;
  }
  return mDocShell->GetCommandManager();
}

NS_IMETHODIMP ComposerCommandsUpdater::GetName(nsACString& aName) {
  aName.AssignLiteral("ComposerCommandsUpdater");
  return NS_OK;
}

#if 0
#  pragma mark -
#endif

nsresult ComposerCommandsUpdater::Notify(nsITimer* aTimer) {
  NS_ASSERTION(aTimer == mUpdateTimer.get(), "Hey, this ain't my timer!");
  TimerCallback();
  return NS_OK;
}

#if 0
#  pragma mark -
#endif

}  // namespace mozilla