Back to index

lightning-sunbird  0.9+nobinonly
nsTextEditRulesBidi.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsTextEditRules.h"
00039 #include "nsCOMPtr.h"
00040 #include "nsIDOMNode.h"
00041 #include "nsIContent.h"
00042 #include "nsIPresShell.h"
00043 #include "nsPresContext.h"
00044 #include "nsIFrame.h"
00045 
00046 // Test for distance between caret and text that will be deleted
00047 nsresult
00048 nsTextEditRules::CheckBidiLevelForDeletion(nsIDOMNode           *aSelNode, 
00049                                            PRInt32               aSelOffset, 
00050                                            nsIEditor::EDirection aAction,
00051                                            PRBool               *aCancel)
00052 {
00053   NS_ENSURE_ARG_POINTER(aCancel);
00054   *aCancel = PR_FALSE;
00055 
00056   nsCOMPtr<nsIPresShell> shell;
00057   nsresult res = mEditor->GetPresShell(getter_AddRefs(shell));
00058   if (NS_FAILED(res))
00059     return res;
00060   if (!shell)
00061     return NS_ERROR_NULL_POINTER;
00062   
00063   nsPresContext *context = shell->GetPresContext();
00064   if (!context)
00065     return NS_ERROR_NULL_POINTER;
00066   
00067   if (!context->BidiEnabled())
00068     return NS_OK;
00069   
00070   nsCOMPtr<nsIContent> content = do_QueryInterface(aSelNode);
00071   if (!content)
00072     return NS_ERROR_NULL_POINTER;
00073 
00074   if (content->IsContentOfType(nsIContent::eELEMENT))
00075   {
00076     content = content->GetChildAt(aSelOffset);    
00077     if (!content)
00078       return NS_ERROR_FAILURE;
00079     aSelOffset = 0;
00080   }    
00081   
00082   nsIFrame *primaryFrame;
00083   res = shell->GetPrimaryFrameFor(content, &primaryFrame);
00084   if (NS_FAILED(res))
00085     return res;
00086   if (!primaryFrame)
00087     return NS_ERROR_NULL_POINTER;
00088   
00089   nsIFrame *frameBefore;
00090   nsIFrame *frameAfter;
00091   PRInt32 frameOffset;
00092 
00093   res = primaryFrame->GetChildFrameContainingOffset(aSelOffset, PR_FALSE, &frameOffset, &frameBefore);
00094   if (NS_FAILED(res))
00095     return res;
00096   if (!frameBefore)
00097     return NS_ERROR_NULL_POINTER;
00098   
00099   PRUint8 levelAfter;
00100   nsCOMPtr<nsIAtom> embeddingLevel = do_GetAtom("EmbeddingLevel");
00101 
00102   // Get the bidi level of the frame before the caret
00103   PRUint8 levelBefore =
00104     NS_PTR_TO_INT32(frameBefore->GetPropertyExternal(embeddingLevel, nsnull));
00105 
00106   // If the caret is at the end of the frame, get the bidi level of the
00107   // frame after the caret
00108   PRInt32 start, end;
00109   frameBefore->GetOffsets(start, end);
00110   if (aSelOffset == end
00111      || aSelOffset == -1)
00112   {
00113     res = primaryFrame->GetChildFrameContainingOffset(aSelOffset, PR_TRUE, &frameOffset, &frameAfter);
00114     if (NS_FAILED(res))
00115       return res;
00116     if (!frameAfter)
00117       return NS_ERROR_NULL_POINTER;
00118     
00119     if (frameBefore==frameAfter)
00120     {
00121       // there was no frameAfter, i.e. the caret is at the end of the
00122       // document -- use the base paragraph level
00123       nsCOMPtr<nsIAtom> baseLevel = do_GetAtom("BaseLevel");
00124       levelAfter =
00125         NS_PTR_TO_INT32(frameBefore->GetPropertyExternal(baseLevel, nsnull));
00126     }
00127     else
00128     {
00129       levelAfter =
00130         NS_PTR_TO_INT32(frameAfter->GetPropertyExternal(embeddingLevel, nsnull));
00131     }
00132   }
00133   else
00134   {
00135     levelAfter = levelBefore;
00136   }
00137   PRUint8 currentCursorLevel;
00138   res = shell->GetCaretBidiLevel(&currentCursorLevel);
00139   if (NS_FAILED(res))
00140     return res;
00141 
00142   PRUint8 levelOfDeletion;
00143   levelOfDeletion = (nsIEditor::eNext==aAction) ? levelAfter : levelBefore;
00144 
00145   if (currentCursorLevel == levelOfDeletion)
00146     ; // perform the deletion
00147   else
00148   {
00149     if ((levelBefore==levelAfter)
00150         && (levelBefore & 1) == (currentCursorLevel & 1))
00151       ; // perform the deletion
00152     else
00153       *aCancel = PR_TRUE;
00154 
00155     // Set the bidi level of the caret to that of the
00156     // character that will be (or would have been) deleted
00157     res = shell->SetCaretBidiLevel(levelOfDeletion);
00158     if (NS_FAILED(res))
00159       return res;
00160   }
00161   return NS_OK;
00162 }
00163