Bug 1247796. Use keyboardFocusIndicatorColor for ActiveBorder system color keyword...
[gecko.git] / editor / libeditor / nsTextEditRulesBidi.cpp
bloba03f3a240e75d45d73691590710d4b28f2bd16cb
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 "mozilla/dom/Selection.h"
16 #include "nsISupportsImpl.h"
17 #include "nsPlaintextEditor.h"
18 #include "nsPresContext.h"
19 #include "nsTextEditRules.h"
20 #include "nscore.h"
22 using namespace mozilla;
23 using namespace mozilla::dom;
25 // Test for distance between caret and text that will be deleted
26 nsresult
27 nsTextEditRules::CheckBidiLevelForDeletion(Selection* aSelection,
28 nsIDOMNode *aSelNode,
29 int32_t aSelOffset,
30 nsIEditor::EDirection aAction,
31 bool *aCancel)
33 NS_ENSURE_ARG_POINTER(aCancel);
34 *aCancel = false;
36 nsCOMPtr<nsIPresShell> shell = mEditor->GetPresShell();
37 NS_ENSURE_TRUE(shell, NS_ERROR_NOT_INITIALIZED);
39 nsPresContext *context = shell->GetPresContext();
40 NS_ENSURE_TRUE(context, NS_ERROR_NULL_POINTER);
42 if (!context->BidiEnabled())
43 return NS_OK;
45 nsCOMPtr<nsIContent> content = do_QueryInterface(aSelNode);
46 NS_ENSURE_TRUE(content, NS_ERROR_NULL_POINTER);
48 nsBidiLevel levelBefore;
49 nsBidiLevel levelAfter;
50 RefPtr<nsFrameSelection> frameSelection =
51 static_cast<Selection*>(aSelection)->GetFrameSelection();
52 NS_ENSURE_TRUE(frameSelection, NS_ERROR_NULL_POINTER);
54 nsPrevNextBidiLevels levels = frameSelection->
55 GetPrevNextBidiLevels(content, aSelOffset, true);
57 levelBefore = levels.mLevelBefore;
58 levelAfter = levels.mLevelAfter;
60 nsBidiLevel currentCaretLevel = frameSelection->GetCaretBidiLevel();
62 nsBidiLevel levelOfDeletion;
63 levelOfDeletion =
64 (nsIEditor::eNext==aAction || nsIEditor::eNextWord==aAction) ?
65 levelAfter : levelBefore;
67 if (currentCaretLevel == levelOfDeletion)
68 ; // perform the deletion
69 else
71 if (mDeleteBidiImmediately || levelBefore == levelAfter)
72 ; // perform the deletion
73 else
74 *aCancel = true;
76 // Set the bidi level of the caret to that of the
77 // character that will be (or would have been) deleted
78 frameSelection->SetCaretBidiLevel(levelOfDeletion);
80 return NS_OK;
83 void
84 nsTextEditRules::UndefineCaretBidiLevel(Selection* aSelection)
86 /**
87 * After inserting text the caret Bidi level must be set to the level of the
88 * inserted text.This is difficult, because we cannot know what the level is
89 * until after the Bidi algorithm is applied to the whole paragraph.
91 * So we set the caret Bidi level to UNDEFINED here, and the caret code will
92 * set it correctly later
94 RefPtr<nsFrameSelection> frameSelection = aSelection->GetFrameSelection();
95 if (frameSelection) {
96 frameSelection->UndefineCaretBidiLevel();