|
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/. */ |
|
5 |
|
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 "nsISelectionPrivate.h" |
|
17 #include "nsISupportsImpl.h" |
|
18 #include "nsPlaintextEditor.h" |
|
19 #include "nsPresContext.h" |
|
20 #include "nsTextEditRules.h" |
|
21 #include "nscore.h" |
|
22 |
|
23 using namespace mozilla; |
|
24 using namespace mozilla::dom; |
|
25 |
|
26 // Test for distance between caret and text that will be deleted |
|
27 nsresult |
|
28 nsTextEditRules::CheckBidiLevelForDeletion(nsISelection *aSelection, |
|
29 nsIDOMNode *aSelNode, |
|
30 int32_t aSelOffset, |
|
31 nsIEditor::EDirection aAction, |
|
32 bool *aCancel) |
|
33 { |
|
34 NS_ENSURE_ARG_POINTER(aCancel); |
|
35 *aCancel = false; |
|
36 |
|
37 nsCOMPtr<nsIPresShell> shell = mEditor->GetPresShell(); |
|
38 NS_ENSURE_TRUE(shell, NS_ERROR_NOT_INITIALIZED); |
|
39 |
|
40 nsPresContext *context = shell->GetPresContext(); |
|
41 NS_ENSURE_TRUE(context, NS_ERROR_NULL_POINTER); |
|
42 |
|
43 if (!context->BidiEnabled()) |
|
44 return NS_OK; |
|
45 |
|
46 nsCOMPtr<nsIContent> content = do_QueryInterface(aSelNode); |
|
47 NS_ENSURE_TRUE(content, NS_ERROR_NULL_POINTER); |
|
48 |
|
49 uint8_t levelBefore; |
|
50 uint8_t levelAfter; |
|
51 nsRefPtr<nsFrameSelection> frameSelection = |
|
52 static_cast<Selection*>(aSelection)->GetFrameSelection(); |
|
53 NS_ENSURE_TRUE(frameSelection, NS_ERROR_NULL_POINTER); |
|
54 |
|
55 nsPrevNextBidiLevels levels = frameSelection-> |
|
56 GetPrevNextBidiLevels(content, aSelOffset, true); |
|
57 |
|
58 levelBefore = levels.mLevelBefore; |
|
59 levelAfter = levels.mLevelAfter; |
|
60 |
|
61 uint8_t currentCaretLevel = frameSelection->GetCaretBidiLevel(); |
|
62 |
|
63 uint8_t levelOfDeletion; |
|
64 levelOfDeletion = |
|
65 (nsIEditor::eNext==aAction || nsIEditor::eNextWord==aAction) ? |
|
66 levelAfter : levelBefore; |
|
67 |
|
68 if (currentCaretLevel == levelOfDeletion) |
|
69 ; // perform the deletion |
|
70 else |
|
71 { |
|
72 if (mDeleteBidiImmediately || levelBefore == levelAfter) |
|
73 ; // perform the deletion |
|
74 else |
|
75 *aCancel = true; |
|
76 |
|
77 // Set the bidi level of the caret to that of the |
|
78 // character that will be (or would have been) deleted |
|
79 frameSelection->SetCaretBidiLevel(levelOfDeletion); |
|
80 } |
|
81 return NS_OK; |
|
82 } |
|
83 |