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/. */
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"
22 using namespace mozilla
;
23 using namespace mozilla::dom
;
25 // Test for distance between caret and text that will be deleted
27 nsTextEditRules::CheckBidiLevelForDeletion(Selection
* aSelection
,
30 nsIEditor::EDirection aAction
,
33 NS_ENSURE_ARG_POINTER(aCancel
);
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())
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
;
64 (nsIEditor::eNext
==aAction
|| nsIEditor::eNextWord
==aAction
) ?
65 levelAfter
: levelBefore
;
67 if (currentCaretLevel
== levelOfDeletion
)
68 ; // perform the deletion
71 if (mDeleteBidiImmediately
|| levelBefore
== levelAfter
)
72 ; // perform the deletion
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
);
84 nsTextEditRules::UndefineCaretBidiLevel(Selection
* aSelection
)
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();
96 frameSelection
->UndefineCaretBidiLevel();