Bug 574778 - Fix win widget's ConstrainPosition so that it supports full screen windo...
[mozilla-central.git] / editor / libeditor / text / nsTextEditRulesBidi.cpp
blob7fcfa787ad4b4365cfbf3d152d45cf0eccffa857
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsTextEditRules.h"
39 #include "nsCOMPtr.h"
40 #include "nsIDOMNode.h"
41 #include "nsIContent.h"
42 #include "nsIPresShell.h"
43 #include "nsPresContext.h"
44 #include "nsIFrame.h"
45 #include "nsISelectionPrivate.h"
46 #include "nsFrameSelection.h"
48 // Test for distance between caret and text that will be deleted
49 nsresult
50 nsTextEditRules::CheckBidiLevelForDeletion(nsISelection *aSelection,
51 nsIDOMNode *aSelNode,
52 PRInt32 aSelOffset,
53 nsIEditor::EDirection aAction,
54 PRBool *aCancel)
56 NS_ENSURE_ARG_POINTER(aCancel);
57 *aCancel = PR_FALSE;
59 nsCOMPtr<nsIPresShell> shell;
60 nsresult res = mEditor->GetPresShell(getter_AddRefs(shell));
61 NS_ENSURE_SUCCESS(res, res);
62 NS_ENSURE_TRUE(shell, NS_ERROR_NULL_POINTER);
64 nsPresContext *context = shell->GetPresContext();
65 NS_ENSURE_TRUE(context, NS_ERROR_NULL_POINTER);
67 if (!context->BidiEnabled())
68 return NS_OK;
70 nsCOMPtr<nsIContent> content = do_QueryInterface(aSelNode);
71 NS_ENSURE_TRUE(content, NS_ERROR_NULL_POINTER);
73 PRUint8 levelBefore;
74 PRUint8 levelAfter;
76 nsCOMPtr<nsISelectionPrivate> privateSelection(do_QueryInterface(aSelection));
77 NS_ENSURE_TRUE(privateSelection, NS_ERROR_NULL_POINTER);
79 nsCOMPtr<nsFrameSelection> frameSelection;
80 privateSelection->GetFrameSelection(getter_AddRefs(frameSelection));
81 NS_ENSURE_TRUE(frameSelection, NS_ERROR_NULL_POINTER);
83 nsPrevNextBidiLevels levels = frameSelection->
84 GetPrevNextBidiLevels(content, aSelOffset, PR_TRUE);
86 levelBefore = levels.mLevelBefore;
87 levelAfter = levels.mLevelAfter;
89 PRUint8 currentCaretLevel = frameSelection->GetCaretBidiLevel();
91 PRUint8 levelOfDeletion;
92 levelOfDeletion =
93 (nsIEditor::eNext==aAction || nsIEditor::eNextWord==aAction) ?
94 levelAfter : levelBefore;
96 if (currentCaretLevel == levelOfDeletion)
97 ; // perform the deletion
98 else
100 if (mDeleteBidiImmediately || levelBefore == levelAfter)
101 ; // perform the deletion
102 else
103 *aCancel = PR_TRUE;
105 // Set the bidi level of the caret to that of the
106 // character that will be (or would have been) deleted
107 frameSelection->SetCaretBidiLevel(levelOfDeletion);
109 return NS_OK;