Back to index

lightning-sunbird  0.9+nobinonly
nsAEWordClass.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 Communicator client 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  *   Simon Fraser <sfraser@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 
00040 #include "def.h"
00041 #include "wasteutil.h"
00042 
00043 #include "AEUtils.h"
00044 #include "AETextClass.h"
00045 
00046 #include "AEWordClass.h"
00047 
00048 #include "nsGfxUtils.h"
00049 
00050 
00051 /*----------------------------------------------------------------------------
00052        GetNumItems 
00053        
00054 ----------------------------------------------------------------------------*/
00055 UInt32 AEWordIterator::GetNumItems(const AEDesc* containerToken)
00056 {
00057        AETokenDesc   container(containerToken);
00058        WEReference   theWE = container.GetWEReference();
00059 
00060        return theWE ? GetWENumWords(theWE) : 0;
00061 }
00062 
00063 /*----------------------------------------------------------------------------
00064        GetIndexedItemReference 
00065        
00066 ----------------------------------------------------------------------------*/
00067 AEClassIterator::ItemRef AEWordIterator::GetIndexedItemReference(const AEDesc* containerToken, TAEListIndex itemIndex)
00068 {
00069        return (ItemRef)itemIndex;
00070 }
00071 
00072 /*----------------------------------------------------------------------------
00073        GetIndexFromItemID 
00074        
00075 ----------------------------------------------------------------------------*/
00076 TAEListIndex AEWordIterator::GetIndexFromItemID(const AEDesc* containerToken, ItemID itemID)
00077 {
00078        return (TAEListIndex)itemID;
00079 }
00080 
00081 /*----------------------------------------------------------------------------
00082        GetIndexedItemID 
00083        
00084 ----------------------------------------------------------------------------*/
00085 AEClassIterator::ItemID AEWordIterator::GetIndexedItemID(const AEDesc* containerToken, TAEListIndex itemIndex)
00086 {
00087        return (ItemID)GetIndexedItemReference(containerToken, itemIndex);
00088 }
00089 
00090 /*----------------------------------------------------------------------------
00091        GetIDFromReference 
00092        
00093 ----------------------------------------------------------------------------*/
00094 AEClassIterator::ItemID AEWordIterator::GetIDFromReference(const AEDesc* containerToken, ItemRef itemRef)
00095 {
00096        return (ItemID)itemRef;
00097 }
00098 
00099 /*----------------------------------------------------------------------------
00100        GetReferenceFromID 
00101        
00102 ----------------------------------------------------------------------------*/
00103 AEClassIterator::ItemRef AEWordIterator::GetReferenceFromID(const AEDesc* containerToken, ItemID itemID)
00104 {
00105        return (ItemRef)itemID;
00106 }
00107 
00108 /*----------------------------------------------------------------------------
00109        GetItemIDFromToken 
00110        
00111 ----------------------------------------------------------------------------*/
00112 AEClassIterator::ItemID AEWordIterator::GetItemIDFromToken(const AEDesc* token)
00113 {
00114        AETokenDesc   container(token);
00115        return (ItemID)container.GetElementNumber();
00116 }
00117 
00118 /*----------------------------------------------------------------------------
00119        SetItemIDInCoreToken 
00120        
00121 ----------------------------------------------------------------------------*/
00122 void AEWordIterator::SetItemIDInCoreToken(const AEDesc* containerToken, CoreTokenRecord* tokenRecord, ItemID itemID)
00123 {
00124        AETokenDesc          tokenDesc(containerToken);
00125        WindowPtr            wind = tokenDesc.GetWindowPtr();
00126        WEReference          theWE = tokenDesc.GetWEReference();
00127 
00128        tokenRecord->window = wind;
00129        tokenRecord->theWE = theWE;
00130        
00131        tokenRecord->elementNumber = (TAEListIndex)itemID;
00132 }
00133 
00134 #pragma mark -
00135 
00136 /*----------------------------------------------------------------------------
00137        AEWordClass 
00138        
00139 ----------------------------------------------------------------------------*/
00140 
00141 AEWordClass::AEWordClass()
00142 :      AEGenericClass(cWord, AETextClass::cTEText)
00143 {
00144 }
00145 
00146 /*----------------------------------------------------------------------------
00147        ~AEWordClass 
00148        
00149 ----------------------------------------------------------------------------*/
00150 AEWordClass::~AEWordClass()
00151 {
00152 
00153 }
00154 
00155 #pragma mark -
00156 
00157 
00158 /*----------------------------------------------------------------------------
00159        GetItemFromContainer 
00160        
00161 ----------------------------------------------------------------------------*/
00162 void AEWordClass::GetItemFromContainer(          DescType                    desiredClass,
00163                                                                       const AEDesc*        containerToken,
00164                                                                       DescType                    containerClass, 
00165                                                                       DescType                    keyForm,
00166                                                                       const AEDesc*        keyData,
00167                                                                       AEDesc*                     resultToken)
00168 {
00169        AEWordIterator              wordIterator;
00170        wordIterator.GetItemFromContainer(desiredClass, containerToken, containerClass, keyForm, keyData, resultToken);
00171 }
00172 
00173 
00174 /*----------------------------------------------------------------------------
00175        CountObjects 
00176        
00177 ----------------------------------------------------------------------------*/
00178 void AEWordClass::CountObjects(                         DescType                    desiredType,
00179                                                                       DescType                    containerClass,
00180                                                                       const AEDesc *              container,
00181                                                                       long *               result)
00182 {
00183        AEWordIterator              wordIterator;
00184        long          numItems = wordIterator.GetNumItems(container);
00185        *result = numItems;
00186 }
00187 
00188 #pragma mark -
00189 
00190 
00191 /*----------------------------------------------------------------------------
00192        GetDataFromObject 
00193        
00194 ----------------------------------------------------------------------------*/
00195 void AEWordClass::GetDataFromObject(const AEDesc *token, AEDesc *desiredTypes, AEDesc *data)
00196 {
00197        AETokenDesc          tokenDesc(token);
00198 
00199        DescType                    aDescType            = cWord;
00200        DescType                    propertyCode         = tokenDesc.GetPropertyCode();
00201        WindowPtr            window                      = tokenDesc.GetWindowPtr();
00202        WEReference          theWE                = tokenDesc.GetWEReference();
00203        TAEListIndex         wordIndex                   = tokenDesc.GetElementNumber();
00204        OSErr                err                         = noErr;
00205        long                        wordStart, wordEnd;
00206        
00207        ThrowErrIfNil(theWE, errAENoSuchObject);
00208        
00209        switch (propertyCode)
00210        {
00211               case pProperties:
00212                      err = AECreateList(nil, 0, true, data);
00213                      ThrowIfOSErr(err);
00214 
00215                      err = AEPutKeyPtr(data, pObjectType, typeType, &aDescType, sizeof(DescType));
00216                      break;
00217                      
00218               case pBestType:
00219               case pClass:
00220               case pDefaultType:
00221               case pObjectType:
00222                      err = AECreateDesc(typeType, &aDescType, sizeof(DescType), data);
00223                      ThrowIfOSErr(err);
00224                      break;
00225        
00226               case pContents:
00227               case typeNull:
00228                      {
00229                             err = GetWEIndexedWordOffsets(theWE, wordIndex, &wordStart, &wordEnd);
00230                             ThrowIfOSErr(err);
00231                      
00232                             Handle               weHand = WEGetText(theWE);
00233                             StHandleLocker              lockHand(weHand);
00234                             
00235                             err = AECreateDesc(typeChar, *weHand + wordStart, wordEnd - wordStart,data);
00236                             ThrowIfOSErr(err);
00237                      }
00238                      break;
00239                      
00240               default:
00241                      Inherited::GetDataFromObject(token, desiredTypes, data);
00242                      break;
00243        }
00244        
00245        ThrowIfOSErr(err);
00246 }
00247 
00248 
00249 /*----------------------------------------------------------------------------
00250        GetDataFromObject 
00251        
00252 ----------------------------------------------------------------------------*/
00253 void AEWordClass::SetDataForObject(const AEDesc *token, AEDesc *data)
00254 {
00255        AETokenDesc          tokenDesc(token);
00256 
00257        DescType                    aDescType            = cWord;
00258        DescType                    propertyCode         = tokenDesc.GetPropertyCode();
00259        WindowPtr            window                      = tokenDesc.GetWindowPtr();
00260        WEReference          theWE                = tokenDesc.GetWEReference();
00261        TAEListIndex         wordIndex                   = tokenDesc.GetElementNumber();
00262        OSErr                err                         = noErr;
00263        long                        wordStart, wordEnd;
00264        
00265        ThrowErrIfNil(theWE, errAENoSuchObject);
00266        
00267        switch (propertyCode)
00268        {
00269        
00270               case pContents:
00271               case typeNull:
00272                      {
00273                             err = GetWEIndexedWordOffsets(theWE, wordIndex, &wordStart, &wordEnd);
00274                             ThrowIfOSErr(err);
00275                      
00276                      }
00277                      break;
00278        }
00279        
00280        ThrowIfOSErr(err);
00281 }
00282 
00283 #pragma mark -
00284 
00285 
00286 /*----------------------------------------------------------------------------
00287        CanSetProperty 
00288               
00289 ----------------------------------------------------------------------------*/
00290 Boolean AEWordClass::CanSetProperty(DescType propertyCode)
00291 {
00292        Boolean result = false;
00293        
00294        switch (propertyCode)
00295        {
00296               case pBestType:
00297               case pClass:
00298               case pDefaultType:
00299               case pObjectType:
00300                      result = false;
00301                      break;
00302               
00303               case typeNull:
00304                      result = true;              // contents
00305                      break;
00306                      
00307               case pProperties:
00308               default:
00309                      result = Inherited::CanSetProperty(propertyCode);
00310                      break;
00311        }
00312        
00313        return result;
00314 }
00315 
00316 
00317 /*----------------------------------------------------------------------------
00318        CanGetProperty 
00319        
00320 ----------------------------------------------------------------------------*/
00321 Boolean AEWordClass::CanGetProperty(DescType propertyCode)
00322 {
00323        Boolean result = false;
00324        
00325        switch (propertyCode)
00326        {
00327               // Properties we can get:
00328 
00329               case pBestType:
00330               case pClass:
00331               case pDefaultType:
00332               case pObjectType:
00333               case pProperties:
00334                      result = true;
00335                      break;
00336                      
00337               default:
00338                      result = Inherited::CanGetProperty(propertyCode);
00339                      break;
00340        }
00341        
00342        return result;
00343 }
00344 
00345 #pragma mark -
00346 
00347 
00348 /*----------------------------------------------------------------------------
00349        CreateSelfSpecifier 
00350 
00351 ----------------------------------------------------------------------------*/
00352 void AEWordClass::CreateSelfSpecifier(const AEDesc *token, AEDesc *outSpecifier)
00353 {
00354        AETokenDesc                 tokenDesc(token);
00355 
00356        WEReference                 theWE = tokenDesc.GetWEReference();
00357        TAEListIndex                wordIndex = tokenDesc.GetElementNumber();
00358        
00359        AEDesc                      selfDesc = { typeNull, nil };
00360        AEDesc                      containerSpecifier = { typeNull, nil };
00361        OSErr                       err = noErr;
00362        
00363        GetContainerSpecifier(token, &containerSpecifier);
00364        
00365        err = ::AECreateDesc(typeLongInteger, &wordIndex, sizeof(TAEListIndex), &selfDesc);
00366        ThrowIfOSErr(err);
00367        
00368        err = ::CreateObjSpecifier(mClass, &containerSpecifier, formAbsolutePosition, &selfDesc, true, outSpecifier);
00369        ThrowIfOSErr(err);
00370 }