1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/editor/composer/src/nsComposerCommands.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1532 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +#include <stdio.h> // for printf 1.11 + 1.12 +#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc 1.13 +#include "nsAString.h" 1.14 +#include "nsCOMPtr.h" // for nsCOMPtr, do_QueryInterface, etc 1.15 +#include "nsComponentManagerUtils.h" // for do_CreateInstance 1.16 +#include "nsComposerCommands.h" 1.17 +#include "nsDebug.h" // for NS_ENSURE_TRUE, etc 1.18 +#include "nsError.h" // for NS_OK, NS_ERROR_FAILURE, etc 1.19 +#include "nsGkAtoms.h" // for nsGkAtoms, nsGkAtoms::font, etc 1.20 +#include "nsIAtom.h" // for nsIAtom, etc 1.21 +#include "nsIClipboard.h" // for nsIClipboard, etc 1.22 +#include "nsICommandParams.h" // for nsICommandParams, etc 1.23 +#include "nsID.h" 1.24 +#include "nsIDOMElement.h" // for nsIDOMElement 1.25 +#include "nsIEditor.h" // for nsIEditor 1.26 +#include "nsIHTMLAbsPosEditor.h" // for nsIHTMLAbsPosEditor 1.27 +#include "nsIHTMLEditor.h" // for nsIHTMLEditor, etc 1.28 +#include "nsLiteralString.h" // for NS_LITERAL_STRING 1.29 +#include "nsReadableUtils.h" // for EmptyString 1.30 +#include "nsString.h" // for nsAutoString, nsString, etc 1.31 +#include "nsStringFwd.h" // for nsAFlatString 1.32 + 1.33 +class nsISupports; 1.34 + 1.35 +//prototype 1.36 +nsresult GetListState(nsIHTMLEditor* aEditor, bool* aMixed, 1.37 + nsAString& aLocalName); 1.38 +nsresult RemoveOneProperty(nsIHTMLEditor* aEditor, const nsAString& aProp); 1.39 +nsresult RemoveTextProperty(nsIHTMLEditor* aEditor, const nsAString& aProp); 1.40 +nsresult SetTextProperty(nsIHTMLEditor *aEditor, const nsAString& aProp); 1.41 + 1.42 + 1.43 +//defines 1.44 +#define STATE_ENABLED "state_enabled" 1.45 +#define STATE_ALL "state_all" 1.46 +#define STATE_ANY "state_any" 1.47 +#define STATE_MIXED "state_mixed" 1.48 +#define STATE_BEGIN "state_begin" 1.49 +#define STATE_END "state_end" 1.50 +#define STATE_ATTRIBUTE "state_attribute" 1.51 +#define STATE_DATA "state_data" 1.52 + 1.53 + 1.54 +nsBaseComposerCommand::nsBaseComposerCommand() 1.55 +{ 1.56 +} 1.57 + 1.58 +NS_IMPL_ISUPPORTS(nsBaseComposerCommand, nsIControllerCommand) 1.59 + 1.60 + 1.61 +nsBaseStateUpdatingCommand::nsBaseStateUpdatingCommand(nsIAtom* aTagName) 1.62 +: nsBaseComposerCommand() 1.63 +, mTagName(aTagName) 1.64 +{ 1.65 + MOZ_ASSERT(mTagName); 1.66 +} 1.67 + 1.68 +nsBaseStateUpdatingCommand::~nsBaseStateUpdatingCommand() 1.69 +{ 1.70 +} 1.71 + 1.72 +NS_IMPL_ISUPPORTS_INHERITED0(nsBaseStateUpdatingCommand, nsBaseComposerCommand) 1.73 + 1.74 +NS_IMETHODIMP 1.75 +nsBaseStateUpdatingCommand::IsCommandEnabled(const char *aCommandName, 1.76 + nsISupports *refCon, 1.77 + bool *outCmdEnabled) 1.78 +{ 1.79 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon); 1.80 + if (editor) 1.81 + return editor->GetIsSelectionEditable(outCmdEnabled); 1.82 + 1.83 + *outCmdEnabled = false; 1.84 + return NS_OK; 1.85 +} 1.86 + 1.87 + 1.88 +NS_IMETHODIMP 1.89 +nsBaseStateUpdatingCommand::DoCommand(const char *aCommandName, 1.90 + nsISupports *refCon) 1.91 +{ 1.92 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon); 1.93 + NS_ENSURE_TRUE(editor, NS_ERROR_NOT_INITIALIZED); 1.94 + 1.95 + return ToggleState(editor); 1.96 +} 1.97 + 1.98 +NS_IMETHODIMP 1.99 +nsBaseStateUpdatingCommand::DoCommandParams(const char *aCommandName, 1.100 + nsICommandParams *aParams, 1.101 + nsISupports *refCon) 1.102 +{ 1.103 + return DoCommand(aCommandName, refCon); 1.104 +} 1.105 + 1.106 +NS_IMETHODIMP 1.107 +nsBaseStateUpdatingCommand::GetCommandStateParams(const char *aCommandName, 1.108 + nsICommandParams *aParams, 1.109 + nsISupports *refCon) 1.110 +{ 1.111 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon); 1.112 + if (editor) 1.113 + return GetCurrentState(editor, aParams); 1.114 + 1.115 + return NS_OK; 1.116 +} 1.117 + 1.118 +NS_IMETHODIMP 1.119 +nsPasteNoFormattingCommand::IsCommandEnabled(const char * aCommandName, 1.120 + nsISupports *refCon, 1.121 + bool *outCmdEnabled) 1.122 +{ 1.123 + NS_ENSURE_ARG_POINTER(outCmdEnabled); 1.124 + *outCmdEnabled = false; 1.125 + 1.126 + // This command is only implemented by nsIHTMLEditor, since 1.127 + // pasting in a plaintext editor automatically only supplies 1.128 + // "unformatted" text 1.129 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(refCon); 1.130 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NOT_IMPLEMENTED); 1.131 + 1.132 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(htmlEditor); 1.133 + NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG); 1.134 + 1.135 + return editor->CanPaste(nsIClipboard::kGlobalClipboard, outCmdEnabled); 1.136 +} 1.137 + 1.138 + 1.139 +NS_IMETHODIMP 1.140 +nsPasteNoFormattingCommand::DoCommand(const char *aCommandName, 1.141 + nsISupports *refCon) 1.142 +{ 1.143 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(refCon); 1.144 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NOT_IMPLEMENTED); 1.145 + 1.146 + return htmlEditor->PasteNoFormatting(nsIClipboard::kGlobalClipboard); 1.147 +} 1.148 + 1.149 +NS_IMETHODIMP 1.150 +nsPasteNoFormattingCommand::DoCommandParams(const char *aCommandName, 1.151 + nsICommandParams *aParams, 1.152 + nsISupports *refCon) 1.153 +{ 1.154 + return DoCommand(aCommandName, refCon); 1.155 +} 1.156 + 1.157 +NS_IMETHODIMP 1.158 +nsPasteNoFormattingCommand::GetCommandStateParams(const char *aCommandName, 1.159 + nsICommandParams *aParams, 1.160 + nsISupports *refCon) 1.161 +{ 1.162 + NS_ENSURE_ARG_POINTER(aParams); 1.163 + 1.164 + bool enabled = false; 1.165 + nsresult rv = IsCommandEnabled(aCommandName, refCon, &enabled); 1.166 + NS_ENSURE_SUCCESS(rv, rv); 1.167 + 1.168 + return aParams->SetBooleanValue(STATE_ENABLED, enabled); 1.169 +} 1.170 + 1.171 +nsStyleUpdatingCommand::nsStyleUpdatingCommand(nsIAtom* aTagName) 1.172 +: nsBaseStateUpdatingCommand(aTagName) 1.173 +{ 1.174 +} 1.175 + 1.176 +nsresult 1.177 +nsStyleUpdatingCommand::GetCurrentState(nsIEditor *aEditor, 1.178 + nsICommandParams *aParams) 1.179 +{ 1.180 + NS_ASSERTION(aEditor, "Need editor here"); 1.181 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.182 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NOT_INITIALIZED); 1.183 + 1.184 + bool firstOfSelectionHasProp = false; 1.185 + bool anyOfSelectionHasProp = false; 1.186 + bool allOfSelectionHasProp = false; 1.187 + 1.188 + nsresult rv = htmlEditor->GetInlineProperty(mTagName, EmptyString(), 1.189 + EmptyString(), 1.190 + &firstOfSelectionHasProp, 1.191 + &anyOfSelectionHasProp, 1.192 + &allOfSelectionHasProp); 1.193 + 1.194 + aParams->SetBooleanValue(STATE_ENABLED, NS_SUCCEEDED(rv)); 1.195 + aParams->SetBooleanValue(STATE_ALL, allOfSelectionHasProp); 1.196 + aParams->SetBooleanValue(STATE_ANY, anyOfSelectionHasProp); 1.197 + aParams->SetBooleanValue(STATE_MIXED, anyOfSelectionHasProp 1.198 + && !allOfSelectionHasProp); 1.199 + aParams->SetBooleanValue(STATE_BEGIN, firstOfSelectionHasProp); 1.200 + aParams->SetBooleanValue(STATE_END, allOfSelectionHasProp);//not completely accurate 1.201 + return NS_OK; 1.202 +} 1.203 + 1.204 +nsresult 1.205 +nsStyleUpdatingCommand::ToggleState(nsIEditor *aEditor) 1.206 +{ 1.207 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.208 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NO_INTERFACE); 1.209 + 1.210 + //create some params now... 1.211 + nsresult rv; 1.212 + nsCOMPtr<nsICommandParams> params = 1.213 + do_CreateInstance(NS_COMMAND_PARAMS_CONTRACTID,&rv); 1.214 + if (NS_FAILED(rv) || !params) 1.215 + return rv; 1.216 + 1.217 + // tags "href" and "name" are special cases in the core editor 1.218 + // they are used to remove named anchor/link and shouldn't be used for insertion 1.219 + bool doTagRemoval; 1.220 + if (mTagName == nsGkAtoms::href || mTagName == nsGkAtoms::name) { 1.221 + doTagRemoval = true; 1.222 + } else { 1.223 + // check current selection; set doTagRemoval if formatting should be removed 1.224 + rv = GetCurrentState(aEditor, params); 1.225 + NS_ENSURE_SUCCESS(rv, rv); 1.226 + rv = params->GetBooleanValue(STATE_ALL, &doTagRemoval); 1.227 + NS_ENSURE_SUCCESS(rv, rv); 1.228 + } 1.229 + 1.230 + if (doTagRemoval) { 1.231 + // Also remove equivalent properties (bug 317093) 1.232 + if (mTagName == nsGkAtoms::b) { 1.233 + rv = RemoveTextProperty(htmlEditor, NS_LITERAL_STRING("strong")); 1.234 + NS_ENSURE_SUCCESS(rv, rv); 1.235 + } else if (mTagName == nsGkAtoms::i) { 1.236 + rv = RemoveTextProperty(htmlEditor, NS_LITERAL_STRING("em")); 1.237 + NS_ENSURE_SUCCESS(rv, rv); 1.238 + } else if (mTagName == nsGkAtoms::strike) { 1.239 + rv = RemoveTextProperty(htmlEditor, NS_LITERAL_STRING("s")); 1.240 + NS_ENSURE_SUCCESS(rv, rv); 1.241 + } 1.242 + 1.243 + rv = RemoveTextProperty(htmlEditor, nsDependentAtomString(mTagName)); 1.244 + } else { 1.245 + // Superscript and Subscript styles are mutually exclusive 1.246 + aEditor->BeginTransaction(); 1.247 + 1.248 + nsDependentAtomString tagName(mTagName); 1.249 + if (mTagName == nsGkAtoms::sub || mTagName == nsGkAtoms::sup) { 1.250 + rv = RemoveTextProperty(htmlEditor, tagName); 1.251 + } 1.252 + if (NS_SUCCEEDED(rv)) 1.253 + rv = SetTextProperty(htmlEditor, tagName); 1.254 + 1.255 + aEditor->EndTransaction(); 1.256 + } 1.257 + 1.258 + return rv; 1.259 +} 1.260 + 1.261 +nsListCommand::nsListCommand(nsIAtom* aTagName) 1.262 +: nsBaseStateUpdatingCommand(aTagName) 1.263 +{ 1.264 +} 1.265 + 1.266 +nsresult 1.267 +nsListCommand::GetCurrentState(nsIEditor* aEditor, nsICommandParams* aParams) 1.268 +{ 1.269 + NS_ASSERTION(aEditor, "Need editor here"); 1.270 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.271 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NO_INTERFACE); 1.272 + 1.273 + bool bMixed; 1.274 + nsAutoString localName; 1.275 + nsresult rv = GetListState(htmlEditor, &bMixed, localName); 1.276 + NS_ENSURE_SUCCESS(rv, rv); 1.277 + 1.278 + bool inList = mTagName->Equals(localName); 1.279 + aParams->SetBooleanValue(STATE_ALL, !bMixed && inList); 1.280 + aParams->SetBooleanValue(STATE_MIXED, bMixed); 1.281 + aParams->SetBooleanValue(STATE_ENABLED, true); 1.282 + return NS_OK; 1.283 +} 1.284 + 1.285 +nsresult 1.286 +nsListCommand::ToggleState(nsIEditor *aEditor) 1.287 +{ 1.288 + nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(aEditor); 1.289 + NS_ENSURE_TRUE(editor, NS_NOINTERFACE); 1.290 + 1.291 + nsresult rv; 1.292 + nsCOMPtr<nsICommandParams> params = 1.293 + do_CreateInstance(NS_COMMAND_PARAMS_CONTRACTID,&rv); 1.294 + if (NS_FAILED(rv) || !params) 1.295 + return rv; 1.296 + 1.297 + rv = GetCurrentState(aEditor, params); 1.298 + NS_ENSURE_SUCCESS(rv, rv); 1.299 + 1.300 + bool inList; 1.301 + rv = params->GetBooleanValue(STATE_ALL,&inList); 1.302 + NS_ENSURE_SUCCESS(rv, rv); 1.303 + 1.304 + nsDependentAtomString listType(mTagName); 1.305 + if (inList) { 1.306 + rv = editor->RemoveList(listType); 1.307 + } else { 1.308 + rv = editor->MakeOrChangeList(listType, false, EmptyString()); 1.309 + } 1.310 + 1.311 + return rv; 1.312 +} 1.313 + 1.314 +nsListItemCommand::nsListItemCommand(nsIAtom* aTagName) 1.315 +: nsBaseStateUpdatingCommand(aTagName) 1.316 +{ 1.317 +} 1.318 + 1.319 +nsresult 1.320 +nsListItemCommand::GetCurrentState(nsIEditor* aEditor, 1.321 + nsICommandParams *aParams) 1.322 +{ 1.323 + NS_ASSERTION(aEditor, "Need editor here"); 1.324 + // 39584 1.325 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.326 + NS_ENSURE_TRUE(htmlEditor, NS_NOINTERFACE); 1.327 + 1.328 + bool bMixed, bLI, bDT, bDD; 1.329 + nsresult rv = htmlEditor->GetListItemState(&bMixed, &bLI, &bDT, &bDD); 1.330 + NS_ENSURE_SUCCESS(rv, rv); 1.331 + 1.332 + bool inList = false; 1.333 + if (!bMixed) 1.334 + { 1.335 + if (bLI) { 1.336 + inList = mTagName == nsGkAtoms::li; 1.337 + } else if (bDT) { 1.338 + inList = mTagName == nsGkAtoms::dt; 1.339 + } else if (bDD) { 1.340 + inList = mTagName == nsGkAtoms::dd; 1.341 + } 1.342 + } 1.343 + 1.344 + aParams->SetBooleanValue(STATE_ALL, !bMixed && inList); 1.345 + aParams->SetBooleanValue(STATE_MIXED, bMixed); 1.346 + 1.347 + return NS_OK; 1.348 +} 1.349 + 1.350 +nsresult 1.351 +nsListItemCommand::ToggleState(nsIEditor *aEditor) 1.352 +{ 1.353 + NS_ASSERTION(aEditor, "Need editor here"); 1.354 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.355 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NOT_INITIALIZED); 1.356 + 1.357 + bool inList; 1.358 + // Need to use mTagName???? 1.359 + nsresult rv; 1.360 + nsCOMPtr<nsICommandParams> params = 1.361 + do_CreateInstance(NS_COMMAND_PARAMS_CONTRACTID,&rv); 1.362 + if (NS_FAILED(rv) || !params) 1.363 + return rv; 1.364 + rv = GetCurrentState(aEditor, params); 1.365 + rv = params->GetBooleanValue(STATE_ALL,&inList); 1.366 + NS_ENSURE_SUCCESS(rv, rv); 1.367 + NS_ENSURE_SUCCESS(rv, rv); 1.368 + 1.369 + if (inList) { 1.370 + // To remove a list, first get what kind of list we're in 1.371 + bool bMixed; 1.372 + nsAutoString localName; 1.373 + rv = GetListState(htmlEditor, &bMixed, localName); 1.374 + NS_ENSURE_SUCCESS(rv, rv); 1.375 + if (localName.IsEmpty() || bMixed) { 1.376 + return rv; 1.377 + } 1.378 + return htmlEditor->RemoveList(localName); 1.379 + } 1.380 + 1.381 + // Set to the requested paragraph type 1.382 + //XXX Note: This actually doesn't work for "LI", 1.383 + // but we currently don't use this for non DL lists anyway. 1.384 + // Problem: won't this replace any current block paragraph style? 1.385 + return htmlEditor->SetParagraphFormat(nsDependentAtomString(mTagName)); 1.386 +} 1.387 + 1.388 +NS_IMETHODIMP 1.389 +nsRemoveListCommand::IsCommandEnabled(const char * aCommandName, 1.390 + nsISupports *refCon, 1.391 + bool *outCmdEnabled) 1.392 +{ 1.393 + *outCmdEnabled = false; 1.394 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon); 1.395 + NS_ENSURE_TRUE(editor, NS_OK); 1.396 + 1.397 + bool isEditable = false; 1.398 + nsresult rv = editor->GetIsSelectionEditable(&isEditable); 1.399 + NS_ENSURE_SUCCESS(rv, rv); 1.400 + if (!isEditable) { 1.401 + return NS_OK; 1.402 + } 1.403 + 1.404 + // It is enabled if we are in any list type 1.405 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(refCon); 1.406 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NO_INTERFACE); 1.407 + 1.408 + bool bMixed; 1.409 + nsAutoString localName; 1.410 + rv = GetListState(htmlEditor, &bMixed, localName); 1.411 + NS_ENSURE_SUCCESS(rv, rv); 1.412 + 1.413 + *outCmdEnabled = bMixed || !localName.IsEmpty(); 1.414 + return NS_OK; 1.415 +} 1.416 + 1.417 + 1.418 +NS_IMETHODIMP 1.419 +nsRemoveListCommand::DoCommand(const char *aCommandName, nsISupports *refCon) 1.420 +{ 1.421 + nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon); 1.422 + 1.423 + nsresult rv = NS_OK; 1.424 + if (editor) 1.425 + { 1.426 + // This removes any list type 1.427 + rv = editor->RemoveList(EmptyString()); 1.428 + } 1.429 + 1.430 + return rv; 1.431 +} 1.432 + 1.433 +NS_IMETHODIMP 1.434 +nsRemoveListCommand::DoCommandParams(const char *aCommandName, 1.435 + nsICommandParams *aParams, 1.436 + nsISupports *refCon) 1.437 +{ 1.438 + return DoCommand(aCommandName, refCon); 1.439 +} 1.440 + 1.441 +NS_IMETHODIMP 1.442 +nsRemoveListCommand::GetCommandStateParams(const char *aCommandName, 1.443 + nsICommandParams *aParams, 1.444 + nsISupports *refCon) 1.445 +{ 1.446 + bool outCmdEnabled = false; 1.447 + IsCommandEnabled(aCommandName, refCon, &outCmdEnabled); 1.448 + return aParams->SetBooleanValue(STATE_ENABLED,outCmdEnabled); 1.449 +} 1.450 + 1.451 +NS_IMETHODIMP 1.452 +nsIndentCommand::IsCommandEnabled(const char * aCommandName, 1.453 + nsISupports *refCon, bool *outCmdEnabled) 1.454 +{ 1.455 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon); 1.456 + if (editor) 1.457 + return editor->GetIsSelectionEditable(outCmdEnabled); 1.458 + 1.459 + *outCmdEnabled = false; 1.460 + return NS_OK; 1.461 +} 1.462 + 1.463 + 1.464 +NS_IMETHODIMP 1.465 +nsIndentCommand::DoCommand(const char *aCommandName, nsISupports *refCon) 1.466 +{ 1.467 + nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon); 1.468 + 1.469 + nsresult rv = NS_OK; 1.470 + if (editor) 1.471 + { 1.472 + rv = editor->Indent(NS_LITERAL_STRING("indent")); 1.473 + } 1.474 + 1.475 + return rv; 1.476 +} 1.477 + 1.478 +NS_IMETHODIMP 1.479 +nsIndentCommand::DoCommandParams(const char *aCommandName, 1.480 + nsICommandParams *aParams, 1.481 + nsISupports *refCon) 1.482 +{ 1.483 + return DoCommand(aCommandName, refCon); 1.484 +} 1.485 + 1.486 +NS_IMETHODIMP 1.487 +nsIndentCommand::GetCommandStateParams(const char *aCommandName, 1.488 + nsICommandParams *aParams, 1.489 + nsISupports *refCon) 1.490 +{ 1.491 + bool outCmdEnabled = false; 1.492 + IsCommandEnabled(aCommandName, refCon, &outCmdEnabled); 1.493 + return aParams->SetBooleanValue(STATE_ENABLED,outCmdEnabled); 1.494 +} 1.495 + 1.496 + 1.497 +//OUTDENT 1.498 + 1.499 +NS_IMETHODIMP 1.500 +nsOutdentCommand::IsCommandEnabled(const char * aCommandName, 1.501 + nsISupports *refCon, 1.502 + bool *outCmdEnabled) 1.503 +{ 1.504 + *outCmdEnabled = false; 1.505 + 1.506 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon); 1.507 + if (editor) { 1.508 + nsresult rv = editor->GetIsSelectionEditable(outCmdEnabled); 1.509 + NS_ENSURE_SUCCESS(rv, rv); 1.510 + } 1.511 + 1.512 + return NS_OK; 1.513 +} 1.514 + 1.515 + 1.516 +NS_IMETHODIMP 1.517 +nsOutdentCommand::DoCommand(const char *aCommandName, nsISupports *refCon) 1.518 +{ 1.519 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(refCon); 1.520 + if (htmlEditor) 1.521 + return htmlEditor->Indent(NS_LITERAL_STRING("outdent")); 1.522 + 1.523 + return NS_OK; 1.524 +} 1.525 + 1.526 +NS_IMETHODIMP 1.527 +nsOutdentCommand::DoCommandParams(const char *aCommandName, 1.528 + nsICommandParams *aParams, 1.529 + nsISupports *refCon) 1.530 +{ 1.531 + return DoCommand(aCommandName, refCon); 1.532 +} 1.533 + 1.534 +NS_IMETHODIMP 1.535 +nsOutdentCommand::GetCommandStateParams(const char *aCommandName, 1.536 + nsICommandParams *aParams, 1.537 + nsISupports *refCon) 1.538 +{ 1.539 + bool outCmdEnabled = false; 1.540 + IsCommandEnabled(aCommandName, refCon, &outCmdEnabled); 1.541 + return aParams->SetBooleanValue(STATE_ENABLED,outCmdEnabled); 1.542 +} 1.543 + 1.544 +nsMultiStateCommand::nsMultiStateCommand() 1.545 +: nsBaseComposerCommand() 1.546 +{ 1.547 +} 1.548 + 1.549 +nsMultiStateCommand::~nsMultiStateCommand() 1.550 +{ 1.551 +} 1.552 + 1.553 +NS_IMPL_ISUPPORTS_INHERITED0(nsMultiStateCommand, nsBaseComposerCommand) 1.554 + 1.555 +NS_IMETHODIMP 1.556 +nsMultiStateCommand::IsCommandEnabled(const char * aCommandName, 1.557 + nsISupports *refCon, 1.558 + bool *outCmdEnabled) 1.559 +{ 1.560 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon); 1.561 + // should be disabled sometimes, like if the current selection is an image 1.562 + if (editor) 1.563 + return editor->GetIsSelectionEditable(outCmdEnabled); 1.564 + 1.565 + *outCmdEnabled = false; 1.566 + return NS_OK; 1.567 +} 1.568 + 1.569 + 1.570 +NS_IMETHODIMP 1.571 +nsMultiStateCommand::DoCommand(const char *aCommandName, nsISupports *refCon) 1.572 +{ 1.573 +#ifdef DEBUG 1.574 + printf("who is calling nsMultiStateCommand::DoCommand \ 1.575 + (no implementation)? %s\n", aCommandName); 1.576 +#endif 1.577 + 1.578 + return NS_OK; 1.579 +} 1.580 + 1.581 +NS_IMETHODIMP 1.582 +nsMultiStateCommand::DoCommandParams(const char *aCommandName, 1.583 + nsICommandParams *aParams, 1.584 + nsISupports *refCon) 1.585 +{ 1.586 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon); 1.587 + 1.588 + nsresult rv = NS_OK; 1.589 + if (editor) 1.590 + { 1.591 + nsAutoString tString; 1.592 + 1.593 + if (aParams) { 1.594 + nsXPIDLCString s; 1.595 + rv = aParams->GetCStringValue(STATE_ATTRIBUTE, getter_Copies(s)); 1.596 + if (NS_SUCCEEDED(rv)) 1.597 + tString.AssignWithConversion(s); 1.598 + else 1.599 + rv = aParams->GetStringValue(STATE_ATTRIBUTE, tString); 1.600 + } 1.601 + 1.602 + rv = SetState(editor, tString); 1.603 + } 1.604 + 1.605 + return rv; 1.606 +} 1.607 + 1.608 +NS_IMETHODIMP 1.609 +nsMultiStateCommand::GetCommandStateParams(const char *aCommandName, 1.610 + nsICommandParams *aParams, 1.611 + nsISupports *refCon) 1.612 +{ 1.613 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon); 1.614 + nsresult rv = NS_OK; 1.615 + if (editor) 1.616 + { 1.617 + rv = GetCurrentState(editor, aParams); 1.618 + } 1.619 + return rv; 1.620 +} 1.621 + 1.622 +nsParagraphStateCommand::nsParagraphStateCommand() 1.623 +: nsMultiStateCommand() 1.624 +{ 1.625 +} 1.626 + 1.627 +nsresult 1.628 +nsParagraphStateCommand::GetCurrentState(nsIEditor *aEditor, 1.629 + nsICommandParams *aParams) 1.630 +{ 1.631 + NS_ASSERTION(aEditor, "Need an editor here"); 1.632 + 1.633 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.634 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); 1.635 + 1.636 + bool outMixed; 1.637 + nsAutoString outStateString; 1.638 + nsresult rv = htmlEditor->GetParagraphState(&outMixed, outStateString); 1.639 + if (NS_SUCCEEDED(rv)) 1.640 + { 1.641 + nsAutoCString tOutStateString; 1.642 + tOutStateString.AssignWithConversion(outStateString); 1.643 + aParams->SetBooleanValue(STATE_MIXED,outMixed); 1.644 + aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get()); 1.645 + } 1.646 + return rv; 1.647 +} 1.648 + 1.649 + 1.650 +nsresult 1.651 +nsParagraphStateCommand::SetState(nsIEditor *aEditor, nsString& newState) 1.652 +{ 1.653 + NS_ASSERTION(aEditor, "Need an editor here"); 1.654 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.655 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); 1.656 + 1.657 + return htmlEditor->SetParagraphFormat(newState); 1.658 +} 1.659 + 1.660 +nsFontFaceStateCommand::nsFontFaceStateCommand() 1.661 +: nsMultiStateCommand() 1.662 +{ 1.663 +} 1.664 + 1.665 +nsresult 1.666 +nsFontFaceStateCommand::GetCurrentState(nsIEditor *aEditor, 1.667 + nsICommandParams *aParams) 1.668 +{ 1.669 + NS_ASSERTION(aEditor, "Need an editor here"); 1.670 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.671 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); 1.672 + 1.673 + nsAutoString outStateString; 1.674 + bool outMixed; 1.675 + nsresult rv = htmlEditor->GetFontFaceState(&outMixed, outStateString); 1.676 + if (NS_SUCCEEDED(rv)) 1.677 + { 1.678 + aParams->SetBooleanValue(STATE_MIXED,outMixed); 1.679 + aParams->SetCStringValue(STATE_ATTRIBUTE, NS_ConvertUTF16toUTF8(outStateString).get()); 1.680 + } 1.681 + return rv; 1.682 +} 1.683 + 1.684 + 1.685 +nsresult 1.686 +nsFontFaceStateCommand::SetState(nsIEditor *aEditor, nsString& newState) 1.687 +{ 1.688 + NS_ASSERTION(aEditor, "Need an editor here"); 1.689 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.690 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); 1.691 + 1.692 + if (newState.EqualsLiteral("tt")) { 1.693 + // The old "teletype" attribute 1.694 + nsresult rv = htmlEditor->SetInlineProperty(nsGkAtoms::tt, EmptyString(), 1.695 + EmptyString()); 1.696 + NS_ENSURE_SUCCESS(rv, rv); 1.697 + // Clear existing font face 1.698 + return htmlEditor->RemoveInlineProperty(nsGkAtoms::font, 1.699 + NS_LITERAL_STRING("face")); 1.700 + } 1.701 + 1.702 + // Remove any existing TT nodes 1.703 + nsresult rv = htmlEditor->RemoveInlineProperty(nsGkAtoms::tt, EmptyString()); 1.704 + NS_ENSURE_SUCCESS(rv, rv); 1.705 + 1.706 + if (newState.IsEmpty() || newState.EqualsLiteral("normal")) { 1.707 + return htmlEditor->RemoveInlineProperty(nsGkAtoms::font, 1.708 + NS_LITERAL_STRING("face")); 1.709 + } 1.710 + 1.711 + return htmlEditor->SetInlineProperty(nsGkAtoms::font, 1.712 + NS_LITERAL_STRING("face"), newState); 1.713 +} 1.714 + 1.715 +nsFontSizeStateCommand::nsFontSizeStateCommand() 1.716 + : nsMultiStateCommand() 1.717 +{ 1.718 +} 1.719 + 1.720 +// nsAutoCString tOutStateString; 1.721 +// tOutStateString.AssignWithConversion(outStateString); 1.722 +nsresult 1.723 +nsFontSizeStateCommand::GetCurrentState(nsIEditor *aEditor, 1.724 + nsICommandParams *aParams) 1.725 +{ 1.726 + NS_ASSERTION(aEditor, "Need an editor here"); 1.727 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.728 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_INVALID_ARG); 1.729 + 1.730 + nsAutoString outStateString; 1.731 + nsCOMPtr<nsIAtom> fontAtom = do_GetAtom("font"); 1.732 + bool firstHas, anyHas, allHas; 1.733 + nsresult rv = htmlEditor->GetInlinePropertyWithAttrValue(fontAtom, 1.734 + NS_LITERAL_STRING("size"), 1.735 + EmptyString(), 1.736 + &firstHas, &anyHas, &allHas, 1.737 + outStateString); 1.738 + NS_ENSURE_SUCCESS(rv, rv); 1.739 + 1.740 + nsAutoCString tOutStateString; 1.741 + tOutStateString.AssignWithConversion(outStateString); 1.742 + aParams->SetBooleanValue(STATE_MIXED, anyHas && !allHas); 1.743 + aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get()); 1.744 + aParams->SetBooleanValue(STATE_ENABLED, true); 1.745 + 1.746 + return rv; 1.747 +} 1.748 + 1.749 + 1.750 +// acceptable values for "newState" are: 1.751 +// -2 1.752 +// -1 1.753 +// 0 1.754 +// +1 1.755 +// +2 1.756 +// +3 1.757 +// medium 1.758 +// normal 1.759 +nsresult 1.760 +nsFontSizeStateCommand::SetState(nsIEditor *aEditor, nsString& newState) 1.761 +{ 1.762 + NS_ASSERTION(aEditor, "Need an editor here"); 1.763 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.764 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_INVALID_ARG); 1.765 + 1.766 + if (!newState.IsEmpty() && 1.767 + !newState.EqualsLiteral("normal") && 1.768 + !newState.EqualsLiteral("medium")) { 1.769 + return htmlEditor->SetInlineProperty(nsGkAtoms::font, 1.770 + NS_LITERAL_STRING("size"), newState); 1.771 + } 1.772 + 1.773 + // remove any existing font size, big or small 1.774 + nsresult rv = htmlEditor->RemoveInlineProperty(nsGkAtoms::font, 1.775 + NS_LITERAL_STRING("size")); 1.776 + NS_ENSURE_SUCCESS(rv, rv); 1.777 + 1.778 + rv = htmlEditor->RemoveInlineProperty(nsGkAtoms::big, EmptyString()); 1.779 + NS_ENSURE_SUCCESS(rv, rv); 1.780 + 1.781 + return htmlEditor->RemoveInlineProperty(nsGkAtoms::small, EmptyString()); 1.782 +} 1.783 + 1.784 +nsFontColorStateCommand::nsFontColorStateCommand() 1.785 +: nsMultiStateCommand() 1.786 +{ 1.787 +} 1.788 + 1.789 +nsresult 1.790 +nsFontColorStateCommand::GetCurrentState(nsIEditor *aEditor, 1.791 + nsICommandParams *aParams) 1.792 +{ 1.793 + NS_ASSERTION(aEditor, "Need an editor here"); 1.794 + 1.795 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.796 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); 1.797 + 1.798 + bool outMixed; 1.799 + nsAutoString outStateString; 1.800 + nsresult rv = htmlEditor->GetFontColorState(&outMixed, outStateString); 1.801 + NS_ENSURE_SUCCESS(rv, rv); 1.802 + 1.803 + nsAutoCString tOutStateString; 1.804 + tOutStateString.AssignWithConversion(outStateString); 1.805 + aParams->SetBooleanValue(STATE_MIXED, outMixed); 1.806 + aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get()); 1.807 + return NS_OK; 1.808 +} 1.809 + 1.810 +nsresult 1.811 +nsFontColorStateCommand::SetState(nsIEditor *aEditor, nsString& newState) 1.812 +{ 1.813 + NS_ASSERTION(aEditor, "Need an editor here"); 1.814 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.815 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); 1.816 + 1.817 + if (newState.IsEmpty() || newState.EqualsLiteral("normal")) { 1.818 + return htmlEditor->RemoveInlineProperty(nsGkAtoms::font, 1.819 + NS_LITERAL_STRING("color")); 1.820 + } 1.821 + 1.822 + return htmlEditor->SetInlineProperty(nsGkAtoms::font, 1.823 + NS_LITERAL_STRING("color"), newState); 1.824 +} 1.825 + 1.826 +nsHighlightColorStateCommand::nsHighlightColorStateCommand() 1.827 +: nsMultiStateCommand() 1.828 +{ 1.829 +} 1.830 + 1.831 +nsresult 1.832 +nsHighlightColorStateCommand::GetCurrentState(nsIEditor *aEditor, 1.833 + nsICommandParams *aParams) 1.834 +{ 1.835 + NS_ASSERTION(aEditor, "Need an editor here"); 1.836 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.837 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); 1.838 + 1.839 + bool outMixed; 1.840 + nsAutoString outStateString; 1.841 + nsresult rv = htmlEditor->GetHighlightColorState(&outMixed, outStateString); 1.842 + NS_ENSURE_SUCCESS(rv, rv); 1.843 + 1.844 + nsAutoCString tOutStateString; 1.845 + tOutStateString.AssignWithConversion(outStateString); 1.846 + aParams->SetBooleanValue(STATE_MIXED, outMixed); 1.847 + aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get()); 1.848 + return NS_OK; 1.849 +} 1.850 + 1.851 +nsresult 1.852 +nsHighlightColorStateCommand::SetState(nsIEditor *aEditor, nsString& newState) 1.853 +{ 1.854 + NS_ASSERTION(aEditor, "Need an editor here"); 1.855 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.856 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); 1.857 + 1.858 + if (newState.IsEmpty() || newState.EqualsLiteral("normal")) { 1.859 + return htmlEditor->RemoveInlineProperty(nsGkAtoms::font, 1.860 + NS_LITERAL_STRING("bgcolor")); 1.861 + } 1.862 + 1.863 + return htmlEditor->SetInlineProperty(nsGkAtoms::font, 1.864 + NS_LITERAL_STRING("bgcolor"), 1.865 + newState); 1.866 +} 1.867 + 1.868 +NS_IMETHODIMP 1.869 +nsHighlightColorStateCommand::IsCommandEnabled(const char * aCommandName, 1.870 + nsISupports *refCon, 1.871 + bool *outCmdEnabled) 1.872 +{ 1.873 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon); 1.874 + if (editor) 1.875 + return editor->GetIsSelectionEditable(outCmdEnabled); 1.876 + 1.877 + *outCmdEnabled = false; 1.878 + return NS_OK; 1.879 +} 1.880 + 1.881 + 1.882 +nsBackgroundColorStateCommand::nsBackgroundColorStateCommand() 1.883 +: nsMultiStateCommand() 1.884 +{ 1.885 +} 1.886 + 1.887 +nsresult 1.888 +nsBackgroundColorStateCommand::GetCurrentState(nsIEditor *aEditor, 1.889 + nsICommandParams *aParams) 1.890 +{ 1.891 + NS_ASSERTION(aEditor, "Need an editor here"); 1.892 + 1.893 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.894 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); 1.895 + 1.896 + bool outMixed; 1.897 + nsAutoString outStateString; 1.898 + nsresult rv = htmlEditor->GetBackgroundColorState(&outMixed, outStateString); 1.899 + NS_ENSURE_SUCCESS(rv, rv); 1.900 + 1.901 + nsAutoCString tOutStateString; 1.902 + tOutStateString.AssignWithConversion(outStateString); 1.903 + aParams->SetBooleanValue(STATE_MIXED, outMixed); 1.904 + aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get()); 1.905 + return NS_OK; 1.906 +} 1.907 + 1.908 +nsresult 1.909 +nsBackgroundColorStateCommand::SetState(nsIEditor *aEditor, nsString& newState) 1.910 +{ 1.911 + NS_ASSERTION(aEditor, "Need an editor here"); 1.912 + 1.913 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.914 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); 1.915 + 1.916 + return htmlEditor->SetBackgroundColor(newState); 1.917 +} 1.918 + 1.919 +nsAlignCommand::nsAlignCommand() 1.920 +: nsMultiStateCommand() 1.921 +{ 1.922 +} 1.923 + 1.924 +nsresult 1.925 +nsAlignCommand::GetCurrentState(nsIEditor *aEditor, nsICommandParams *aParams) 1.926 +{ 1.927 + NS_ASSERTION(aEditor, "Need an editor here"); 1.928 + 1.929 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.930 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); 1.931 + 1.932 + nsIHTMLEditor::EAlignment firstAlign; 1.933 + bool outMixed; 1.934 + nsresult rv = htmlEditor->GetAlignment(&outMixed, &firstAlign); 1.935 + 1.936 + NS_ENSURE_SUCCESS(rv, rv); 1.937 + 1.938 + nsAutoString outStateString; 1.939 + switch (firstAlign) 1.940 + { 1.941 + default: 1.942 + case nsIHTMLEditor::eLeft: 1.943 + outStateString.AssignLiteral("left"); 1.944 + break; 1.945 + 1.946 + case nsIHTMLEditor::eCenter: 1.947 + outStateString.AssignLiteral("center"); 1.948 + break; 1.949 + 1.950 + case nsIHTMLEditor::eRight: 1.951 + outStateString.AssignLiteral("right"); 1.952 + break; 1.953 + 1.954 + case nsIHTMLEditor::eJustify: 1.955 + outStateString.AssignLiteral("justify"); 1.956 + break; 1.957 + } 1.958 + nsAutoCString tOutStateString; 1.959 + tOutStateString.AssignWithConversion(outStateString); 1.960 + aParams->SetBooleanValue(STATE_MIXED,outMixed); 1.961 + aParams->SetCStringValue(STATE_ATTRIBUTE, tOutStateString.get()); 1.962 + return NS_OK; 1.963 +} 1.964 + 1.965 +nsresult 1.966 +nsAlignCommand::SetState(nsIEditor *aEditor, nsString& newState) 1.967 +{ 1.968 + NS_ASSERTION(aEditor, "Need an editor here"); 1.969 + 1.970 + nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(aEditor); 1.971 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); 1.972 + 1.973 + return htmlEditor->Align(newState); 1.974 +} 1.975 + 1.976 +nsAbsolutePositioningCommand::nsAbsolutePositioningCommand() 1.977 +: nsBaseStateUpdatingCommand(nsGkAtoms::_empty) 1.978 +{ 1.979 +} 1.980 + 1.981 +NS_IMETHODIMP 1.982 +nsAbsolutePositioningCommand::IsCommandEnabled(const char * aCommandName, 1.983 + nsISupports *aCommandRefCon, 1.984 + bool *outCmdEnabled) 1.985 +{ 1.986 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon); 1.987 + nsCOMPtr<nsIHTMLAbsPosEditor> htmlEditor = do_QueryInterface(aCommandRefCon); 1.988 + if (htmlEditor) 1.989 + { 1.990 + bool isEditable = false; 1.991 + nsresult rv = editor->GetIsSelectionEditable(&isEditable); 1.992 + NS_ENSURE_SUCCESS(rv, rv); 1.993 + if (isEditable) 1.994 + return htmlEditor->GetAbsolutePositioningEnabled(outCmdEnabled); 1.995 + } 1.996 + 1.997 + *outCmdEnabled = false; 1.998 + return NS_OK; 1.999 +} 1.1000 + 1.1001 +nsresult 1.1002 +nsAbsolutePositioningCommand::GetCurrentState(nsIEditor *aEditor, nsICommandParams *aParams) 1.1003 +{ 1.1004 + NS_ASSERTION(aEditor, "Need an editor here"); 1.1005 + 1.1006 + nsCOMPtr<nsIHTMLAbsPosEditor> htmlEditor = do_QueryInterface(aEditor); 1.1007 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); 1.1008 + 1.1009 + bool isEnabled; 1.1010 + htmlEditor->GetAbsolutePositioningEnabled(&isEnabled); 1.1011 + if (!isEnabled) { 1.1012 + aParams->SetBooleanValue(STATE_MIXED,false); 1.1013 + aParams->SetCStringValue(STATE_ATTRIBUTE, ""); 1.1014 + return NS_OK; 1.1015 + } 1.1016 + 1.1017 + nsCOMPtr<nsIDOMElement> elt; 1.1018 + nsresult rv = htmlEditor->GetAbsolutelyPositionedSelectionContainer(getter_AddRefs(elt)); 1.1019 + NS_ENSURE_SUCCESS(rv, rv); 1.1020 + 1.1021 + nsAutoString outStateString; 1.1022 + if (elt) 1.1023 + outStateString.AssignLiteral("absolute"); 1.1024 + 1.1025 + aParams->SetBooleanValue(STATE_MIXED,false); 1.1026 + aParams->SetCStringValue(STATE_ATTRIBUTE, NS_ConvertUTF16toUTF8(outStateString).get()); 1.1027 + return NS_OK; 1.1028 +} 1.1029 + 1.1030 +nsresult 1.1031 +nsAbsolutePositioningCommand::ToggleState(nsIEditor *aEditor) 1.1032 +{ 1.1033 + NS_ASSERTION(aEditor, "Need an editor here"); 1.1034 + 1.1035 + nsCOMPtr<nsIHTMLAbsPosEditor> htmlEditor = do_QueryInterface(aEditor); 1.1036 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); 1.1037 + 1.1038 + nsCOMPtr<nsIDOMElement> elt; 1.1039 + nsresult rv = htmlEditor->GetAbsolutelyPositionedSelectionContainer(getter_AddRefs(elt)); 1.1040 + NS_ENSURE_SUCCESS(rv, rv); 1.1041 + 1.1042 + return htmlEditor->AbsolutePositionSelection(!elt); 1.1043 +} 1.1044 + 1.1045 + 1.1046 +NS_IMETHODIMP 1.1047 +nsDecreaseZIndexCommand::IsCommandEnabled(const char * aCommandName, 1.1048 + nsISupports *refCon, 1.1049 + bool *outCmdEnabled) 1.1050 +{ 1.1051 + nsCOMPtr<nsIHTMLAbsPosEditor> htmlEditor = do_QueryInterface(refCon); 1.1052 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); 1.1053 + 1.1054 + htmlEditor->GetAbsolutePositioningEnabled(outCmdEnabled); 1.1055 + if (!(*outCmdEnabled)) 1.1056 + return NS_OK; 1.1057 + 1.1058 + nsCOMPtr<nsIDOMElement> positionedElement; 1.1059 + htmlEditor->GetPositionedElement(getter_AddRefs(positionedElement)); 1.1060 + *outCmdEnabled = false; 1.1061 + if (positionedElement) { 1.1062 + int32_t z; 1.1063 + nsresult res = htmlEditor->GetElementZIndex(positionedElement, &z); 1.1064 + NS_ENSURE_SUCCESS(res, res); 1.1065 + *outCmdEnabled = (z > 0); 1.1066 + } 1.1067 + 1.1068 + return NS_OK; 1.1069 +} 1.1070 + 1.1071 +NS_IMETHODIMP 1.1072 +nsDecreaseZIndexCommand::DoCommand(const char *aCommandName, 1.1073 + nsISupports *refCon) 1.1074 +{ 1.1075 + nsCOMPtr<nsIHTMLAbsPosEditor> htmlEditor = do_QueryInterface(refCon); 1.1076 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NOT_IMPLEMENTED); 1.1077 + 1.1078 + return htmlEditor->RelativeChangeZIndex(-1); 1.1079 +} 1.1080 + 1.1081 +NS_IMETHODIMP 1.1082 +nsDecreaseZIndexCommand::DoCommandParams(const char *aCommandName, 1.1083 + nsICommandParams *aParams, 1.1084 + nsISupports *refCon) 1.1085 +{ 1.1086 + return DoCommand(aCommandName, refCon); 1.1087 +} 1.1088 + 1.1089 +NS_IMETHODIMP 1.1090 +nsDecreaseZIndexCommand::GetCommandStateParams(const char *aCommandName, 1.1091 + nsICommandParams *aParams, 1.1092 + nsISupports *refCon) 1.1093 +{ 1.1094 + NS_ENSURE_ARG_POINTER(aParams); 1.1095 + 1.1096 + bool enabled = false; 1.1097 + nsresult rv = IsCommandEnabled(aCommandName, refCon, &enabled); 1.1098 + NS_ENSURE_SUCCESS(rv, rv); 1.1099 + 1.1100 + return aParams->SetBooleanValue(STATE_ENABLED, enabled); 1.1101 +} 1.1102 + 1.1103 +NS_IMETHODIMP 1.1104 +nsIncreaseZIndexCommand::IsCommandEnabled(const char * aCommandName, 1.1105 + nsISupports *refCon, 1.1106 + bool *outCmdEnabled) 1.1107 +{ 1.1108 + nsCOMPtr<nsIHTMLAbsPosEditor> htmlEditor = do_QueryInterface(refCon); 1.1109 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); 1.1110 + 1.1111 + htmlEditor->GetAbsolutePositioningEnabled(outCmdEnabled); 1.1112 + if (!(*outCmdEnabled)) 1.1113 + return NS_OK; 1.1114 + 1.1115 + nsCOMPtr<nsIDOMElement> positionedElement; 1.1116 + htmlEditor->GetPositionedElement(getter_AddRefs(positionedElement)); 1.1117 + *outCmdEnabled = (nullptr != positionedElement); 1.1118 + return NS_OK; 1.1119 +} 1.1120 + 1.1121 +NS_IMETHODIMP 1.1122 +nsIncreaseZIndexCommand::DoCommand(const char *aCommandName, 1.1123 + nsISupports *refCon) 1.1124 +{ 1.1125 + nsCOMPtr<nsIHTMLAbsPosEditor> htmlEditor = do_QueryInterface(refCon); 1.1126 + NS_ENSURE_TRUE(htmlEditor, NS_ERROR_NOT_IMPLEMENTED); 1.1127 + 1.1128 + return htmlEditor->RelativeChangeZIndex(1); 1.1129 +} 1.1130 + 1.1131 +NS_IMETHODIMP 1.1132 +nsIncreaseZIndexCommand::DoCommandParams(const char *aCommandName, 1.1133 + nsICommandParams *aParams, 1.1134 + nsISupports *refCon) 1.1135 +{ 1.1136 + return DoCommand(aCommandName, refCon); 1.1137 +} 1.1138 + 1.1139 +NS_IMETHODIMP 1.1140 +nsIncreaseZIndexCommand::GetCommandStateParams(const char *aCommandName, 1.1141 + nsICommandParams *aParams, 1.1142 + nsISupports *refCon) 1.1143 +{ 1.1144 + NS_ENSURE_ARG_POINTER(aParams); 1.1145 + 1.1146 + bool enabled = false; 1.1147 + nsresult rv = IsCommandEnabled(aCommandName, refCon, &enabled); 1.1148 + NS_ENSURE_SUCCESS(rv, rv); 1.1149 + 1.1150 + return aParams->SetBooleanValue(STATE_ENABLED, enabled); 1.1151 +} 1.1152 + 1.1153 + 1.1154 +NS_IMETHODIMP 1.1155 +nsRemoveStylesCommand::IsCommandEnabled(const char * aCommandName, 1.1156 + nsISupports *refCon, 1.1157 + bool *outCmdEnabled) 1.1158 +{ 1.1159 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon); 1.1160 + // test if we have any styles? 1.1161 + if (editor) 1.1162 + return editor->GetIsSelectionEditable(outCmdEnabled); 1.1163 + 1.1164 + *outCmdEnabled = false; 1.1165 + return NS_OK; 1.1166 +} 1.1167 + 1.1168 + 1.1169 + 1.1170 +NS_IMETHODIMP 1.1171 +nsRemoveStylesCommand::DoCommand(const char *aCommandName, 1.1172 + nsISupports *refCon) 1.1173 +{ 1.1174 + nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon); 1.1175 + 1.1176 + nsresult rv = NS_OK; 1.1177 + if (editor) 1.1178 + { 1.1179 + rv = editor->RemoveAllInlineProperties(); 1.1180 + } 1.1181 + 1.1182 + return rv; 1.1183 +} 1.1184 + 1.1185 +NS_IMETHODIMP 1.1186 +nsRemoveStylesCommand::DoCommandParams(const char *aCommandName, 1.1187 + nsICommandParams *aParams, 1.1188 + nsISupports *refCon) 1.1189 +{ 1.1190 + return DoCommand(aCommandName, refCon); 1.1191 +} 1.1192 + 1.1193 +NS_IMETHODIMP 1.1194 +nsRemoveStylesCommand::GetCommandStateParams(const char *aCommandName, 1.1195 + nsICommandParams *aParams, 1.1196 + nsISupports *refCon) 1.1197 +{ 1.1198 + bool outCmdEnabled = false; 1.1199 + IsCommandEnabled(aCommandName, refCon, &outCmdEnabled); 1.1200 + return aParams->SetBooleanValue(STATE_ENABLED,outCmdEnabled); 1.1201 +} 1.1202 + 1.1203 +NS_IMETHODIMP 1.1204 +nsIncreaseFontSizeCommand::IsCommandEnabled(const char * aCommandName, 1.1205 + nsISupports *refCon, 1.1206 + bool *outCmdEnabled) 1.1207 +{ 1.1208 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon); 1.1209 + // test if we are at max size? 1.1210 + if (editor) 1.1211 + return editor->GetIsSelectionEditable(outCmdEnabled); 1.1212 + 1.1213 + *outCmdEnabled = false; 1.1214 + return NS_OK; 1.1215 +} 1.1216 + 1.1217 + 1.1218 +NS_IMETHODIMP 1.1219 +nsIncreaseFontSizeCommand::DoCommand(const char *aCommandName, 1.1220 + nsISupports *refCon) 1.1221 +{ 1.1222 + nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon); 1.1223 + 1.1224 + nsresult rv = NS_OK; 1.1225 + if (editor) 1.1226 + { 1.1227 + rv = editor->IncreaseFontSize(); 1.1228 + } 1.1229 + 1.1230 + return rv; 1.1231 +} 1.1232 + 1.1233 +NS_IMETHODIMP 1.1234 +nsIncreaseFontSizeCommand::DoCommandParams(const char *aCommandName, 1.1235 + nsICommandParams *aParams, 1.1236 + nsISupports *refCon) 1.1237 +{ 1.1238 + return DoCommand(aCommandName, refCon); 1.1239 +} 1.1240 + 1.1241 +NS_IMETHODIMP 1.1242 +nsIncreaseFontSizeCommand::GetCommandStateParams(const char *aCommandName, 1.1243 + nsICommandParams *aParams, 1.1244 + nsISupports *refCon) 1.1245 +{ 1.1246 + bool outCmdEnabled = false; 1.1247 + IsCommandEnabled(aCommandName, refCon, &outCmdEnabled); 1.1248 + return aParams->SetBooleanValue(STATE_ENABLED,outCmdEnabled); 1.1249 +} 1.1250 + 1.1251 +NS_IMETHODIMP 1.1252 +nsDecreaseFontSizeCommand::IsCommandEnabled(const char * aCommandName, 1.1253 + nsISupports *refCon, 1.1254 + bool *outCmdEnabled) 1.1255 +{ 1.1256 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon); 1.1257 + // test if we are at min size? 1.1258 + if (editor) 1.1259 + return editor->GetIsSelectionEditable(outCmdEnabled); 1.1260 + 1.1261 + *outCmdEnabled = false; 1.1262 + return NS_OK; 1.1263 +} 1.1264 + 1.1265 + 1.1266 +NS_IMETHODIMP 1.1267 +nsDecreaseFontSizeCommand::DoCommand(const char *aCommandName, 1.1268 + nsISupports *refCon) 1.1269 +{ 1.1270 + nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon); 1.1271 + 1.1272 + nsresult rv = NS_OK; 1.1273 + if (editor) 1.1274 + { 1.1275 + rv = editor->DecreaseFontSize(); 1.1276 + } 1.1277 + 1.1278 + return rv; 1.1279 +} 1.1280 + 1.1281 +NS_IMETHODIMP 1.1282 +nsDecreaseFontSizeCommand::DoCommandParams(const char *aCommandName, 1.1283 + nsICommandParams *aParams, 1.1284 + nsISupports *refCon) 1.1285 +{ 1.1286 + return DoCommand(aCommandName, refCon); 1.1287 +} 1.1288 + 1.1289 +NS_IMETHODIMP 1.1290 +nsDecreaseFontSizeCommand::GetCommandStateParams(const char *aCommandName, 1.1291 + nsICommandParams *aParams, 1.1292 + nsISupports *refCon) 1.1293 +{ 1.1294 + bool outCmdEnabled = false; 1.1295 + IsCommandEnabled(aCommandName, refCon, &outCmdEnabled); 1.1296 + return aParams->SetBooleanValue(STATE_ENABLED,outCmdEnabled); 1.1297 +} 1.1298 + 1.1299 +NS_IMETHODIMP 1.1300 +nsInsertHTMLCommand::IsCommandEnabled(const char * aCommandName, 1.1301 + nsISupports *refCon, 1.1302 + bool *outCmdEnabled) 1.1303 +{ 1.1304 + NS_ENSURE_ARG_POINTER(outCmdEnabled); 1.1305 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon); 1.1306 + if (editor) 1.1307 + return editor->GetIsSelectionEditable(outCmdEnabled); 1.1308 + 1.1309 + *outCmdEnabled = false; 1.1310 + return NS_OK; 1.1311 +} 1.1312 + 1.1313 + 1.1314 +NS_IMETHODIMP 1.1315 +nsInsertHTMLCommand::DoCommand(const char *aCommandName, nsISupports *refCon) 1.1316 +{ 1.1317 + return NS_ERROR_NOT_IMPLEMENTED; 1.1318 +} 1.1319 + 1.1320 +NS_IMETHODIMP 1.1321 +nsInsertHTMLCommand::DoCommandParams(const char *aCommandName, 1.1322 + nsICommandParams *aParams, 1.1323 + nsISupports *refCon) 1.1324 +{ 1.1325 + NS_ENSURE_ARG_POINTER(aParams); 1.1326 + NS_ENSURE_ARG_POINTER(refCon); 1.1327 + 1.1328 + nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon); 1.1329 + NS_ENSURE_TRUE(editor, NS_ERROR_NOT_IMPLEMENTED); 1.1330 + 1.1331 + // Get HTML source string to insert from command params 1.1332 + nsAutoString html; 1.1333 + nsresult rv = aParams->GetStringValue(STATE_DATA, html); 1.1334 + NS_ENSURE_SUCCESS(rv, rv); 1.1335 + 1.1336 + if (!html.IsEmpty()) 1.1337 + return editor->InsertHTML(html); 1.1338 + 1.1339 + return NS_OK; 1.1340 +} 1.1341 + 1.1342 +NS_IMETHODIMP 1.1343 +nsInsertHTMLCommand::GetCommandStateParams(const char *aCommandName, 1.1344 + nsICommandParams *aParams, 1.1345 + nsISupports *refCon) 1.1346 +{ 1.1347 + NS_ENSURE_ARG_POINTER(aParams); 1.1348 + NS_ENSURE_ARG_POINTER(refCon); 1.1349 + 1.1350 + bool outCmdEnabled = false; 1.1351 + IsCommandEnabled(aCommandName, refCon, &outCmdEnabled); 1.1352 + return aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled); 1.1353 +} 1.1354 + 1.1355 +NS_IMPL_ISUPPORTS_INHERITED0(nsInsertTagCommand, nsBaseComposerCommand) 1.1356 + 1.1357 +nsInsertTagCommand::nsInsertTagCommand(nsIAtom* aTagName) 1.1358 +: nsBaseComposerCommand() 1.1359 +, mTagName(aTagName) 1.1360 +{ 1.1361 + MOZ_ASSERT(mTagName); 1.1362 +} 1.1363 + 1.1364 +nsInsertTagCommand::~nsInsertTagCommand() 1.1365 +{ 1.1366 +} 1.1367 + 1.1368 +NS_IMETHODIMP 1.1369 +nsInsertTagCommand::IsCommandEnabled(const char * aCommandName, 1.1370 + nsISupports *refCon, 1.1371 + bool *outCmdEnabled) 1.1372 +{ 1.1373 + NS_ENSURE_ARG_POINTER(outCmdEnabled); 1.1374 + nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon); 1.1375 + if (editor) 1.1376 + return editor->GetIsSelectionEditable(outCmdEnabled); 1.1377 + 1.1378 + *outCmdEnabled = false; 1.1379 + return NS_OK; 1.1380 +} 1.1381 + 1.1382 + 1.1383 +// corresponding STATE_ATTRIBUTE is: src (img) and href (a) 1.1384 +NS_IMETHODIMP 1.1385 +nsInsertTagCommand::DoCommand(const char *aCmdName, nsISupports *refCon) 1.1386 +{ 1.1387 + NS_ENSURE_TRUE(mTagName == nsGkAtoms::hr, NS_ERROR_NOT_IMPLEMENTED); 1.1388 + 1.1389 + nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon); 1.1390 + NS_ENSURE_TRUE(editor, NS_ERROR_NOT_IMPLEMENTED); 1.1391 + 1.1392 + nsCOMPtr<nsIDOMElement> domElem; 1.1393 + nsresult rv = editor->CreateElementWithDefaults( 1.1394 + nsDependentAtomString(mTagName), getter_AddRefs(domElem)); 1.1395 + NS_ENSURE_SUCCESS(rv, rv); 1.1396 + 1.1397 + return editor->InsertElementAtSelection(domElem, true); 1.1398 +} 1.1399 + 1.1400 +NS_IMETHODIMP 1.1401 +nsInsertTagCommand::DoCommandParams(const char *aCommandName, 1.1402 + nsICommandParams *aParams, 1.1403 + nsISupports *refCon) 1.1404 +{ 1.1405 + NS_ENSURE_ARG_POINTER(refCon); 1.1406 + 1.1407 + // inserting an hr shouldn't have an parameters, just call DoCommand for that 1.1408 + if (mTagName == nsGkAtoms::hr) { 1.1409 + return DoCommand(aCommandName, refCon); 1.1410 + } 1.1411 + 1.1412 + NS_ENSURE_ARG_POINTER(aParams); 1.1413 + 1.1414 + nsCOMPtr<nsIHTMLEditor> editor = do_QueryInterface(refCon); 1.1415 + NS_ENSURE_TRUE(editor, NS_ERROR_NOT_IMPLEMENTED); 1.1416 + 1.1417 + // do we have an href to use for creating link? 1.1418 + nsXPIDLCString s; 1.1419 + nsresult rv = aParams->GetCStringValue(STATE_ATTRIBUTE, getter_Copies(s)); 1.1420 + NS_ENSURE_SUCCESS(rv, rv); 1.1421 + nsAutoString attrib; attrib.AssignWithConversion(s); 1.1422 + 1.1423 + if (attrib.IsEmpty()) 1.1424 + return NS_ERROR_INVALID_ARG; 1.1425 + 1.1426 + // filter out tags we don't know how to insert 1.1427 + nsAutoString attributeType; 1.1428 + if (mTagName == nsGkAtoms::a) { 1.1429 + attributeType.AssignLiteral("href"); 1.1430 + } else if (mTagName == nsGkAtoms::img) { 1.1431 + attributeType.AssignLiteral("src"); 1.1432 + } else { 1.1433 + return NS_ERROR_NOT_IMPLEMENTED; 1.1434 + } 1.1435 + 1.1436 + nsCOMPtr<nsIDOMElement> domElem; 1.1437 + rv = editor->CreateElementWithDefaults(nsDependentAtomString(mTagName), 1.1438 + getter_AddRefs(domElem)); 1.1439 + NS_ENSURE_SUCCESS(rv, rv); 1.1440 + 1.1441 + rv = domElem->SetAttribute(attributeType, attrib); 1.1442 + NS_ENSURE_SUCCESS(rv, rv); 1.1443 + 1.1444 + // do actual insertion 1.1445 + if (mTagName == nsGkAtoms::a) 1.1446 + return editor->InsertLinkAroundSelection(domElem); 1.1447 + 1.1448 + return editor->InsertElementAtSelection(domElem, true); 1.1449 +} 1.1450 + 1.1451 +NS_IMETHODIMP 1.1452 +nsInsertTagCommand::GetCommandStateParams(const char *aCommandName, 1.1453 + nsICommandParams *aParams, 1.1454 + nsISupports *refCon) 1.1455 +{ 1.1456 + NS_ENSURE_ARG_POINTER(aParams); 1.1457 + NS_ENSURE_ARG_POINTER(refCon); 1.1458 + 1.1459 + bool outCmdEnabled = false; 1.1460 + IsCommandEnabled(aCommandName, refCon, &outCmdEnabled); 1.1461 + return aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled); 1.1462 +} 1.1463 + 1.1464 + 1.1465 +/****************************/ 1.1466 +//HELPER METHODS 1.1467 +/****************************/ 1.1468 + 1.1469 +nsresult 1.1470 +GetListState(nsIHTMLEditor* aEditor, bool* aMixed, nsAString& aLocalName) 1.1471 +{ 1.1472 + MOZ_ASSERT(aEditor); 1.1473 + MOZ_ASSERT(aMixed); 1.1474 + 1.1475 + *aMixed = false; 1.1476 + aLocalName.Truncate(); 1.1477 + 1.1478 + bool bOL, bUL, bDL; 1.1479 + nsresult rv = aEditor->GetListState(aMixed, &bOL, &bUL, &bDL); 1.1480 + NS_ENSURE_SUCCESS(rv, rv); 1.1481 + 1.1482 + if (*aMixed) { 1.1483 + return NS_OK; 1.1484 + } 1.1485 + 1.1486 + if (bOL) { 1.1487 + aLocalName.AssignLiteral("ol"); 1.1488 + } else if (bUL) { 1.1489 + aLocalName.AssignLiteral("ul"); 1.1490 + } else if (bDL) { 1.1491 + aLocalName.AssignLiteral("dl"); 1.1492 + } 1.1493 + return NS_OK; 1.1494 +} 1.1495 + 1.1496 +nsresult 1.1497 +RemoveOneProperty(nsIHTMLEditor* aEditor, const nsAString& aProp) 1.1498 +{ 1.1499 + MOZ_ASSERT(aEditor); 1.1500 + 1.1501 + /// XXX Hack alert! Look in nsIEditProperty.h for this 1.1502 + nsCOMPtr<nsIAtom> styleAtom = do_GetAtom(aProp); 1.1503 + NS_ENSURE_TRUE(styleAtom, NS_ERROR_OUT_OF_MEMORY); 1.1504 + 1.1505 + return aEditor->RemoveInlineProperty(styleAtom, EmptyString()); 1.1506 +} 1.1507 + 1.1508 + 1.1509 +// the name of the attribute here should be the contents of the appropriate 1.1510 +// tag, e.g. 'b' for bold, 'i' for italics. 1.1511 +nsresult 1.1512 +RemoveTextProperty(nsIHTMLEditor* aEditor, const nsAString& aProp) 1.1513 +{ 1.1514 + MOZ_ASSERT(aEditor); 1.1515 + 1.1516 + if (aProp.LowerCaseEqualsLiteral("all")) { 1.1517 + return aEditor->RemoveAllInlineProperties(); 1.1518 + } 1.1519 + 1.1520 + return RemoveOneProperty(aEditor, aProp); 1.1521 +} 1.1522 + 1.1523 +// the name of the attribute here should be the contents of the appropriate 1.1524 +// tag, e.g. 'b' for bold, 'i' for italics. 1.1525 +nsresult 1.1526 +SetTextProperty(nsIHTMLEditor* aEditor, const nsAString& aProp) 1.1527 +{ 1.1528 + MOZ_ASSERT(aEditor); 1.1529 + 1.1530 + /// XXX Hack alert! Look in nsIEditProperty.h for this 1.1531 + nsCOMPtr<nsIAtom> styleAtom = do_GetAtom(aProp); 1.1532 + NS_ENSURE_TRUE(styleAtom, NS_ERROR_OUT_OF_MEMORY); 1.1533 + 1.1534 + return aEditor->SetInlineProperty(styleAtom, EmptyString(), EmptyString()); 1.1535 +}