Back to index

lightning-sunbird  0.9+nobinonly
nsDOMTextEvent.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* vim: set ts=2 sw=2 et tw=78: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Ilya Konstantinov (mozilla-code@future.shiny.co.il)
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsDOMTextEvent.h"
00041 #include "nsContentUtils.h"
00042 #include "nsPrivateTextRange.h"
00043 
00044 nsDOMTextEvent::nsDOMTextEvent(nsPresContext* aPresContext,
00045                                nsTextEvent* aEvent)
00046   : nsDOMUIEvent(aPresContext, aEvent ? aEvent :
00047                  new nsTextEvent(PR_FALSE, 0, nsnull))
00048 {
00049   NS_ASSERTION(mEvent->eventStructType == NS_TEXT_EVENT, "event type mismatch");
00050 
00051   if (aEvent) {
00052     mEventIsInternal = PR_FALSE;
00053   }
00054   else {
00055     mEventIsInternal = PR_TRUE;
00056     mEvent->time = PR_Now();
00057   }
00058 
00059   //
00060   // extract the IME composition string
00061   //
00062   nsTextEvent *te = NS_STATIC_CAST(nsTextEvent*, mEvent);
00063   mText = te->theText;
00064 
00065   //
00066   // build the range list -- ranges need to be DOM-ified since the
00067   // IME transaction will hold a ref, the widget representation
00068   // isn't persistent
00069   //
00070   nsIPrivateTextRange** tempTextRangeList = new nsIPrivateTextRange*[te->rangeCount];
00071   if (tempTextRangeList) {
00072     PRUint16 i;
00073 
00074     for(i = 0; i < te->rangeCount; i++) {
00075       nsPrivateTextRange* tempPrivateTextRange = new
00076         nsPrivateTextRange(te->rangeArray[i].mStartOffset,
00077                            te->rangeArray[i].mEndOffset,
00078                            te->rangeArray[i].mRangeType);
00079 
00080       if (tempPrivateTextRange) {
00081         NS_ADDREF(tempPrivateTextRange);
00082 
00083         tempTextRangeList[i] = (nsIPrivateTextRange*)tempPrivateTextRange;
00084       }
00085     }
00086   }
00087 
00088   // We need to create mTextRange even rangeCount is 0. 
00089   // If rangeCount is 0, mac carbon will return 0 for new and
00090   // tempTextRangeList will be null. but we should still create
00091   // mTextRange, otherwise, we will crash it later when some code
00092   // call GetInputRange and AddRef to the result
00093   mTextRange = new nsPrivateTextRangeList(te->rangeCount ,tempTextRangeList);
00094 }
00095 
00096 NS_IMPL_ADDREF_INHERITED(nsDOMTextEvent, nsDOMUIEvent)
00097 NS_IMPL_RELEASE_INHERITED(nsDOMTextEvent, nsDOMUIEvent)
00098 
00099 NS_INTERFACE_MAP_BEGIN(nsDOMTextEvent)
00100   NS_INTERFACE_MAP_ENTRY(nsIPrivateTextEvent)
00101 NS_INTERFACE_MAP_END_INHERITING(nsDOMUIEvent)
00102 
00103 NS_METHOD nsDOMTextEvent::GetText(nsString& aText)
00104 {
00105   aText = mText;
00106   return NS_OK;
00107 }
00108 
00109 NS_METHOD nsDOMTextEvent::GetInputRange(nsIPrivateTextRangeList** aInputRange)
00110 {
00111   if (mEvent->message == NS_TEXT_TEXT)
00112   {
00113     *aInputRange = mTextRange;
00114     return NS_OK;
00115   }
00116   aInputRange = 0;
00117   return NS_ERROR_FAILURE;
00118 }
00119 
00120 NS_METHOD nsDOMTextEvent::GetEventReply(nsTextEventReply** aReply)
00121 {
00122   if (mEvent->message == NS_TEXT_TEXT)
00123   {
00124      *aReply = &(NS_STATIC_CAST(nsTextEvent*, mEvent)->theReply);
00125      return NS_OK;
00126   }
00127   aReply = 0;
00128   return NS_ERROR_FAILURE;
00129 }
00130 
00131 nsresult NS_NewDOMTextEvent(nsIDOMEvent** aInstancePtrResult,
00132                             nsPresContext* aPresContext,
00133                             nsTextEvent *aEvent)
00134 {
00135   nsDOMTextEvent* it = new nsDOMTextEvent(aPresContext, aEvent);
00136   if (nsnull == it) {
00137     return NS_ERROR_OUT_OF_MEMORY;
00138   }
00139 
00140   return CallQueryInterface(it, aInstancePtrResult);
00141 }