Back to index

lightning-sunbird  0.9+nobinonly
nsAEUtils.h
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 
00041 #ifndef __AEUTILS__
00042 #define __AEUTILS__
00043 
00044 #include <MacTypes.h>
00045 #include <AppleEvents.h>
00046 #include <AERegistry.h>
00047 #include <ASRegistry.h>
00048 #include <AEObjects.h>
00049 #include <FinderRegistry.h>
00050 
00051 #include <Files.h>
00052 #include <Errors.h>
00053 #include <Aliases.h>
00054 #include <MacWindows.h>
00055 
00056 #include "nsAEDefs.h"
00057 #include "nsMacUtils.h"
00058 
00059 
00060 #define ThrowIfNil(a)                     do { if (a == nil) { throw((OSErr)memFullErr); } } while (0)
00061 #define ThrowErrIfNil(a, err)             do { if (a == nil) { OSErr theErr = (err); throw((OSErr)theErr); } } while (0)
00062 #define ThrowErrIfTrue(a, err)            do { if (a) { OSErr theErr = (err); throw((OSErr)theErr); } } while (0)
00063 #define ThrowIfOSErr(err)                 do { OSErr theErr = (err); if (theErr != noErr) { throw((OSErr)theErr); } } while (0)
00064 #define ThrowOSErr(err)                   do { throw((OSErr)err); } while (0)
00065 #define ThrowNoErr()                      do { throw((OSErr)noErr); } while (0)
00066 
00067 
00068 #define pObjectType                       'pObT'
00069 
00070 // Windows
00071 #define pTitle                                   'pTit'
00072 #define pIsModeless                       'pNMo'
00073 #define pIsMovableModal                   'pMMo'
00074 #define pIsSuspended               'pSus'
00075 #define pIsPalette                        'pPal'
00076 #define pIsDialog                         'pDlg'
00077 #define pLocation                         'pLcn'   // the upper left corner of the object's bounding box
00078 
00079 // Application 
00080 
00081 #define pFreeMemory                'pMem'
00082 #define pTicks                                   'pTic'
00083 
00084 
00085 
00086 // data given to a spawned thread for suspending and resuming Apple Events.
00087 // there should be one of these per event.
00088 typedef struct TThreadAEInfo
00089 {
00090        AppleEvent           mAppleEvent;
00091        AppleEvent           mReply;
00092        SInt32               mSuspendCount;                     // how many suspend calls we have
00093        Boolean                     mGotEventData;
00094 } TThreadAEInfo, *TThreadAEInfoPtr;
00095 
00096 
00097 
00098 
00099 #ifdef __cplusplus
00100 extern "C" {
00101 #endif
00102 
00103 OSErr CreateAliasAEDesc(AliasHandle theAlias, AEDesc *ioDesc);
00104 OSErr GetTextFromAEDesc(const AEDesc *inDesc, Handle *outTextHandle);
00105 
00106 OSErr CreateThreadAEInfo(const AppleEvent *event, AppleEvent *reply, TThreadAEInfoPtr *outThreadAEInfo);
00107 void DisposeThreadAEInfo(TThreadAEInfo *threadAEInfo);
00108 
00109 OSErr SuspendThreadAE(TThreadAEInfo *threadAEInfo);
00110 OSErr ResumeThreadAE(TThreadAEInfo *threadAEInfo, OSErr threadError);
00111 
00112 #if !TARGET_CARBON
00113 
00114 OSErr AEGetDescData(const AEDesc *theAEDesc,  void * dataPtr, Size maximumSize);
00115 Size AEGetDescDataSize(const AEDesc *theAEDesc);
00116 OSErr AEReplaceDescData(DescType typeCode, const void *dataPtr, Size dataSize, AEDesc* theAEDesc);
00117 
00118 #endif /* TARGET_CARBON */
00119 
00120 #ifdef __cplusplus
00121 }
00122 #endif
00123 
00124 
00125 // a stack-based, exception safely class for an AEDesc
00126 
00127 class StAEDesc: public AEDesc
00128 {
00129 public:
00130                                    StAEDesc()
00131                                    {
00132                                           descriptorType = typeNull;
00133                                           dataHandle = nil;
00134                                    }
00135                                    
00136                                    StAEDesc(const StAEDesc& rhs);                          // copy constructor
00137                                    
00138                                    ~StAEDesc()
00139                                    {
00140                                           ::AEDisposeDesc(this);
00141                                    }
00142 
00143                                    void   Clear()
00144                                    {
00145                                           ::AEDisposeDesc(this);
00146                                           descriptorType = typeNull;
00147                                           dataHandle = nil;
00148                                    }
00149                                    
00150                                    void CheckDataType(DescType expectedType)
00151                                    {
00152                                           if (descriptorType != expectedType)
00153                                                  ThrowOSErr(errAEWrongDataType);
00154                                    }
00155                                    
00156        StAEDesc&            operator= (const StAEDesc&rhs);           // throws OSErrs
00157 
00158        Size                 GetDataSize()
00159                        {
00160                         return AEGetDescDataSize(this);
00161                        }
00162               
00163        Boolean                     GetBoolean();
00164        SInt16               GetShort();
00165        SInt32               GetLong();
00166        DescType             GetEnumType();
00167        
00168        void                 GetRect(Rect& outRect);
00169        void                 GetRGBColor(RGBColor& outColor);
00170        void                 GetLongDateTime(LongDateTime& outDateTime);
00171        void                 GetFileSpec(FSSpec &outFileSpec);
00172        void                 GetCString(char *outString, short maxLen);
00173        void                 GetPString(Str255 outString);
00174        
00175        Handle               GetTextHandle();
00176        
00177 };
00178 
00179 
00180 class StEventSuspender
00181 {
00182 public:
00183 
00184                                    // use deleteData = false when calling threads, which take ownership of the TThreadAEInfoPtr
00185                                    StEventSuspender(const AppleEvent *appleEvent, AppleEvent *reply, Boolean deleteData = true);
00186                                    ~StEventSuspender();
00187 
00188        void                        SetDeleteData(Boolean deleteData)  { mDeleteData = deleteData; }
00189        void                        SuspendEvent();
00190        void                        ResumeEvent();
00191        
00192        TThreadAEInfoPtr     GetThreadAEInfo()    { return mThreadAEInfo; }
00193        
00194 protected:
00195 
00196        TThreadAEInfoPtr     mThreadAEInfo;
00197        Boolean                     mDeleteData;
00198 };
00199 
00200 class StHandleHolder
00201 {
00202 public:
00203                                    StHandleHolder(Handle inHandle = nil);           // takes ownership of the handle
00204                                    ~StHandleHolder();
00205                                    
00206        StHandleHolder&      operator=(Handle rhs);
00207                                    
00208        void                        Lock();
00209        void                        Unlock();
00210        
00211        Handle               GetHandle()          { return mHandle; }
00212        Size                        GetHandleSize()             { return (mHandle) ? ::GetHandleSize(mHandle) : 0; } 
00213        Ptr                         GetPtr()                    { return (mHandle) ? *mHandle : nil; }
00214        
00215        class getter
00216        {
00217        public:
00218                                    getter(StHandleHolder& handleHolder)
00219                                    :      mHandleHolder(handleHolder)
00220                                    ,      mHandle(nil)
00221                                    {
00222                                    }
00223                                    
00224                                    ~getter()
00225                                    {
00226                                           mHandleHolder = mHandle;
00227                                    }
00228                                    
00229                                    operator Handle*()
00230                                    {
00231                                           return &mHandle;
00232                                    }
00233        private:
00234               StHandleHolder&      mHandleHolder;
00235               Handle               mHandle;
00236        };
00237 
00238        // it is risky to define operator&, but, hey, we know what we're doing.
00239        getter               operator &()
00240                                    {
00241                                           return getter(*this);
00242                                    }
00243        // Does the same as operator&, but more explicitly.
00244        getter               AssignHandle()
00245                                    {
00246                                           return getter(*this);
00247                                    }
00248        
00249 protected:
00250        
00251        Handle               mHandle;
00252        SInt32               mLockCount;
00253        UInt8                mOldHandleState;
00254 };
00255 
00256 
00257 class AEListUtils
00258 {
00259 public:
00260 
00261        static Boolean       TokenContainsTokenList(const AEDesc *token) { return (token->descriptorType == typeAEList); }
00262        static OSErr  GetFirstNonListToken(const AEDesc *token, AEDesc *result);
00263        static OSErr  FlattenAEList(AEDescList *deepList, AEDescList *flatList);
00264 };
00265 
00266 
00267 // simple iterator for AEDescs which may be lists. Is NOT safe to the list changing under it.
00268 // also, it does not handle nested lists.
00269 class AEListIterator
00270 {
00271 public:
00272                             AEListIterator(AEDesc *token);
00273                             ~AEListIterator() {}
00274                             
00275        Boolean              Next(AEDesc* outItemData);                // data should be deleted by the caller.
00276        SInt32        GetNumItems()  { return mNumItems; }
00277        
00278 protected:
00279        AEDesc        mListToken;          // we don't own this.
00280        SInt32        mNumItems;
00281        SInt32        mCurItem;
00282        Boolean              mIsListDesc;
00283 
00284 };
00285 
00286 
00287 OSErr  DescToPString(const AEDesc* desc, Str255 aPString, short maxLength);
00288 OSErr  DescToCString(const AEDesc* desc, CStr255 aPString, short maxLength);
00289 OSErr  DescToDescType(const AEDesc *desc, DescType *descType);
00290 OSErr  DescToBoolean(const AEDesc* desc, Boolean* aBoolean);
00291 OSErr  DescToFixed(const  AEDesc* desc, Fixed* aFixed);
00292 OSErr  DescToFloat(const  AEDesc* desc, float* aFloat);
00293 OSErr  DescToLong(const  AEDesc* desc, long* aLong);
00294 OSErr  DescToRGBColor(const  AEDesc* desc, RGBColor* aRGBColor);
00295 OSErr  DescToShort(const  AEDesc* desc, short* aShort);
00296 OSErr  DescToTextHandle(const AEDesc* desc, Handle *text);
00297 OSErr  DescToRect(const  AEDesc* desc, Rect* aRect);
00298 OSErr  DescToPoint(const  AEDesc* desc, Point* aPoint);
00299 
00300 OSErr  CheckForUnusedParameters(const AppleEvent* appleEvent);
00301 OSErr  PutReplyErrorNumber(AppleEvent* reply, long errorNumber);
00302 OSErr  PutReplyErrorMessage(AppleEvent* reply, char *message);
00303 OSErr  GetObjectClassFromAppleEvent(const AppleEvent *appleEvent, DescType *objectClass);
00304 OSErr  NormalizeAbsoluteIndex(const AEDesc *keyData, long *index, long maxIndex, Boolean *isAllItems);
00305 OSErr  ProcessFormRange(AEDesc *keyData, AEDesc *start, AEDesc *stop);
00306 
00307 
00308 #endif /* __AEUTILS__ */