Bug 827007: Implement Stop for UserMediaStreams; add NotifyRemoved for MediaStream...
[gecko.git] / editor / libeditor / text / nsTextEditRulesBidi.cpp
blob15af76f635d555f142b45b07e0b04f250384adcd
1 /* -*- Mode: C++; tab-width: 2; 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/. */
6 #include "nsAutoPtr.h"
7 #include "nsCOMPtr.h"
8 #include "nsDebug.h"
9 #include "nsError.h"
10 #include "nsFrameSelection.h"
11 #include "nsIContent.h"
12 #include "nsIDOMNode.h"
13 #include "nsIEditor.h"
14 #include "nsIPresShell.h"
15 #include "nsISelection.h"
16 #include "nsISelectionPrivate.h"
17 #include "nsISupportsImpl.h"
18 #include "nsPlaintextEditor.h"
19 #include "nsPresContext.h"
20 #include "nsTextEditRules.h"
21 #include "nscore.h"
23 // Test for distance between caret and text that will be deleted
24 nsresult
25 nsTextEditRules::CheckBidiLevelForDeletion(nsISelection *aSelection,
26 nsIDOMNode *aSelNode,
27 int32_t aSelOffset,
28 nsIEditor::EDirection aAction,
29 bool *aCancel)
31 NS_ENSURE_ARG_POINTER(aCancel);
32 *aCancel = false;
34 nsCOMPtr<nsIPresShell> shell = mEditor->GetPresShell();
35 NS_ENSURE_TRUE(shell, NS_ERROR_NOT_INITIALIZED);
37 nsPresContext *context = shell->GetPresContext();
38 NS_ENSURE_TRUE(context, NS_ERROR_NULL_POINTER);
40 if (!context->BidiEnabled())
41 return NS_OK;
43 nsCOMPtr<nsIContent> content = do_QueryInterface(aSelNode);
44 NS_ENSURE_TRUE(content, NS_ERROR_NULL_POINTER);
46 uint8_t levelBefore;
47 uint8_t levelAfter;
49 nsCOMPtr<nsISelectionPrivate> privateSelection(do_QueryInterface(aSelection));
50 NS_ENSURE_TRUE(privateSelection, NS_ERROR_NULL_POINTER);
52 nsRefPtr<nsFrameSelection> frameSelection;
53 privateSelection->GetFrameSelection(getter_AddRefs(frameSelection));
54 NS_ENSURE_TRUE(frameSelection, NS_ERROR_NULL_POINTER);
56 nsPrevNextBidiLevels levels = frameSelection->
57 GetPrevNextBidiLevels(content, aSelOffset, true);
59 levelBefore = levels.mLevelBefore;
60 levelAfter = levels.mLevelAfter;
62 uint8_t currentCaretLevel = frameSelection->GetCaretBidiLevel();
64 uint8_t levelOfDeletion;
65 levelOfDeletion =
66 (nsIEditor::eNext==aAction || nsIEditor::eNextWord==aAction) ?
67 levelAfter : levelBefore;
69 if (currentCaretLevel == levelOfDeletion)
70 ; // perform the deletion
71 else
73 if (mDeleteBidiImmediately || levelBefore == levelAfter)
74 ; // perform the deletion
75 else
76 *aCancel = true;
78 // Set the bidi level of the caret to that of the
79 // character that will be (or would have been) deleted
80 frameSelection->SetCaretBidiLevel(levelOfDeletion);
82 return NS_OK;