Back to index

lightning-sunbird  0.9+nobinonly
nsNativeAppSupportMac.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  *
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 #include "nsNativeAppSupport.h"
00038 #include "nsString.h"
00039 
00040 #include <Gestalt.h>
00041 #include <Dialogs.h>
00042 #include <Resources.h>
00043 #include <TextUtils.h>
00044 #include <ControlDefinitions.h>
00045 
00046 #include "nsCOMPtr.h"
00047 #include "nsNativeAppSupportBase.h"
00048 
00049 #include "nsIAppShellService.h"
00050 #include "nsIAppStartup.h"
00051 #include "nsIBaseWindow.h"
00052 #include "nsICmdLineService.h"
00053 #include "nsIDOMWindowInternal.h"
00054 #include "nsIDocShellTreeItem.h"
00055 #include "nsIDocShellTreeOwner.h"
00056 #include "nsIInterfaceRequestorUtils.h"
00057 #include "nsIObserver.h"
00058 #include "nsIServiceManager.h"
00059 #include "nsIWebNavigation.h"
00060 #include "nsIWidget.h"
00061 #include "nsIWindowMediator.h"
00062 
00063 #include "nsXPFEComponentsCID.h"
00064 
00065 #define rSplashDialog 512
00066 
00067 static Boolean VersGreaterThan4(const FSSpec *fSpec);
00068 
00069 const OSType kNSCreator = 'MOSS';
00070 const OSType kMozCreator = 'MOZZ';
00071 const SInt16 kNSCanRunStrArrayID = 1000;
00072 const SInt16 kAnotherVersionStrIndex = 1;
00073 
00074 nsresult
00075 GetNativeWindowPointerFromDOMWindow(nsIDOMWindowInternal *window, WindowRef *nativeWindow);
00076 
00077 const SInt16 kNSOSVersErrsStrArrayID = 1001;
00078 
00079 enum {
00080         eOSXVersTooOldErrIndex = 1,
00081         eOSXVersTooOldExplanationIndex,
00082         eContinueButtonTextIndex,
00083         eQuitButtonTextIndex,
00084         eCarbonLibVersTooOldIndex,
00085         eCarbonLibVersTooOldExplanationIndex
00086      };
00087 
00088 class nsNativeAppSupportMac : public nsNativeAppSupportBase,
00089                               public nsIObserver
00090 {
00091 public:
00092 
00093     // dialog items
00094     enum {
00095       eSplashPictureItem = 1,
00096       eSplashStatusTextItem    
00097     };
00098     
00099             nsNativeAppSupportMac();    
00100     virtual ~nsNativeAppSupportMac();
00101 
00102     NS_DECL_ISUPPORTS
00103     NS_DECL_NSINATIVEAPPSUPPORT
00104     NS_DECL_NSIOBSERVER
00105 
00106 protected:
00107 
00108     DialogPtr mDialog;
00109 
00110 }; // class nsSplashScreenMac
00111 
00112 
00113 nsNativeAppSupportMac::nsNativeAppSupportMac()
00114 : mDialog(nsnull)
00115 {
00116 }
00117 
00118 
00119 nsNativeAppSupportMac::~nsNativeAppSupportMac()
00120 {
00121   HideSplashScreen();
00122 }
00123 
00124 NS_IMPL_ISUPPORTS2(nsNativeAppSupportMac, nsINativeAppSupport, nsIObserver)
00125 
00126 /* boolean start (); */
00127 NS_IMETHODIMP nsNativeAppSupportMac::Start(PRBool *_retval)
00128 {
00129   Str255 str1;
00130   Str255 str2;
00131   SInt16 outItemHit;
00132   long   response = 0;
00133   OSErr  err = ::Gestalt (gestaltSystemVersion, &response);
00134   // check for at least MacOS 8.5
00135   if ( err || response < 0x850)
00136   {
00137     ::StopAlert (5000, NULL);
00138     *_retval = PR_FALSE;
00139     return NS_ERROR_FAILURE;
00140   }
00141   
00142 #if TARGET_CARBON
00143   // If we're running under Mac OS X check for at least Mac OS X 10.1
00144   // If that fails display a StandardAlert giving the user the option
00145   // to continue running the app or quitting
00146   if (response >= 0x00001000 && response < 0x00001010)
00147   {
00148     // put up error dialog
00149     Str255 continueButtonLabel;
00150     Str255 quitButtonLabel;
00151     ::GetIndString(str1, kNSOSVersErrsStrArrayID, eOSXVersTooOldErrIndex);
00152     ::GetIndString(str2, kNSOSVersErrsStrArrayID, eOSXVersTooOldExplanationIndex);
00153     ::GetIndString(continueButtonLabel, kNSOSVersErrsStrArrayID, eContinueButtonTextIndex);
00154     ::GetIndString(quitButtonLabel, kNSOSVersErrsStrArrayID, eQuitButtonTextIndex);
00155     if (StrLength(str1) && StrLength(str1) && StrLength(continueButtonLabel) && StrLength(quitButtonLabel))
00156     {
00157       AlertStdAlertParamRec pRec;
00158       
00159       pRec.movable      = nil;
00160       pRec.filterProc       = nil;
00161       pRec.defaultText  = continueButtonLabel;
00162       pRec.cancelText   = quitButtonLabel;
00163       pRec.otherText    = nil;
00164       pRec.helpButton   = nil;
00165       pRec.defaultButton = kAlertStdAlertOKButton;
00166       pRec.cancelButton  = kAlertStdAlertCancelButton;
00167       pRec.position      = 0;
00168       
00169       ::StandardAlert(kAlertNoteAlert, str1, str2, &pRec, &outItemHit);
00170       if (outItemHit == kAlertStdAlertCancelButton)
00171         return PR_FALSE;
00172     }
00173     else
00174       return PR_FALSE;
00175   }
00176   
00177   // We also check for CarbonLib version >= 1.4 if OS vers < 10.0
00178   // which is always cause for the app to quit
00179   if (response < 0x00001000)
00180   {
00181     err = ::Gestalt (gestaltCarbonVersion, &response);
00182     if (err || response < 0x00000140)
00183     {
00184       // put up error dialog
00185       ::GetIndString(str1, kNSOSVersErrsStrArrayID, eCarbonLibVersTooOldIndex);
00186       ::GetIndString(str2, kNSOSVersErrsStrArrayID, eCarbonLibVersTooOldExplanationIndex);
00187       if (StrLength(str1) && StrLength(str1))
00188       {
00189         ::StandardAlert(kAlertStopAlert, str1, str2, nil, &outItemHit);
00190       }
00191       return PR_FALSE;
00192     }
00193   }
00194 #endif
00195 
00196   *_retval = PR_TRUE;
00197   return NS_OK;
00198 }
00199 
00200 /* boolean stop (); */
00201 NS_IMETHODIMP nsNativeAppSupportMac::Stop(PRBool *_retval)
00202 {
00203   *_retval = PR_TRUE;
00204   return NS_OK;
00205 }
00206 
00207 /* void quit (); */
00208 NS_IMETHODIMP nsNativeAppSupportMac::Quit()
00209 {
00210   return NS_OK;
00211 }
00212 
00213 /* [noscript] void ensureProfile (in nsICmdLineService aCmdService); */
00214 NS_IMETHODIMP nsNativeAppSupportMac::EnsureProfile(nsICmdLineService *aCmdService)
00215 {
00216   return NS_OK;
00217 }
00218 
00219 /* void showSplashScreen (); */
00220 NS_IMETHODIMP nsNativeAppSupportMac::ShowSplashScreen()
00221 {
00222   mDialog = ::GetNewDialog(rSplashDialog, nil, (WindowPtr)-1L);
00223   if (!mDialog) return NS_ERROR_FAILURE;
00224 
00225 #if TARGET_CARBON
00226   ::ShowWindow(GetDialogWindow(mDialog));
00227   ::SetPortDialogPort(mDialog);
00228 #else 
00229   ::ShowWindow(mDialog);
00230   ::SetPort(mDialog);
00231 #endif
00232 
00233   ::DrawDialog(mDialog);    // we don't handle events for this dialog, so we
00234                             // need to draw explicitly. Yuck.
00235   return NS_OK;
00236 }
00237 
00238 /* void hideSplashScreen (); */
00239 NS_IMETHODIMP nsNativeAppSupportMac::HideSplashScreen()
00240 {
00241   if (mDialog)
00242   {
00243     ::DisposeDialog( mDialog );
00244     mDialog = nsnull;
00245   }
00246   return NS_OK;
00247 }
00248 
00249 /* attribute boolean isServerMode; */
00250 NS_IMETHODIMP nsNativeAppSupportMac::GetIsServerMode(PRBool *aIsServerMode)
00251 {
00252   *aIsServerMode = PR_FALSE;
00253   return NS_OK;;
00254 }
00255 NS_IMETHODIMP nsNativeAppSupportMac::SetIsServerMode(PRBool aIsServerMode)
00256 {
00257   return NS_ERROR_NOT_IMPLEMENTED;
00258 }
00259 
00260 /* attribute boolean shouldShowUI; */
00261 NS_IMETHODIMP nsNativeAppSupportMac::GetShouldShowUI(PRBool *aShouldShowUI)
00262 {
00263   *aShouldShowUI = PR_TRUE;
00264   return NS_OK;;
00265 }
00266 
00267 NS_IMETHODIMP nsNativeAppSupportMac::SetShouldShowUI(PRBool aShouldShowUI)
00268 {
00269   return NS_ERROR_NOT_IMPLEMENTED;
00270 }
00271 
00272 /* void startServerMode (); */
00273 NS_IMETHODIMP nsNativeAppSupportMac::StartServerMode()
00274 {
00275   return NS_ERROR_NOT_IMPLEMENTED;
00276 }
00277 
00278 /* void onLastWindowClosing (in nsIXULWindow aWindow); */
00279 NS_IMETHODIMP nsNativeAppSupportMac::OnLastWindowClosing()
00280 {
00281   return NS_OK;
00282 }
00283 
00284 NS_IMETHODIMP
00285 nsNativeAppSupportMac::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
00286 {
00287   // update a string in the dialog
00288   
00289   nsCAutoString statusString;
00290   statusString.AssignWithConversion(someData);
00291 
00292   ControlHandle   staticTextControl;
00293   OSErr err = ::GetDialogItemAsControl(mDialog, eSplashStatusTextItem, &staticTextControl);
00294   if (err != noErr) return NS_OK;
00295   
00296   PRInt32   maxLen = statusString.Length();
00297   if (maxLen > 254)
00298     maxLen = 254;
00299   
00300   ::SetControlData(staticTextControl, 0, kControlStaticTextTextTag, maxLen, statusString.get());
00301   ::DrawOneControl(staticTextControl);
00302       
00303   //::DrawDialog(mDialog);
00304   return NS_OK;
00305 }
00306 
00307 NS_IMETHODIMP
00308 nsNativeAppSupportMac::ReOpen()
00309 {
00310 
00311   PRBool haveUncollapsed = PR_FALSE;
00312   PRBool haveOpenWindows = PR_FALSE;
00313   PRBool done = PR_FALSE;
00314   
00315   nsCOMPtr<nsIWindowMediator> 
00316     wm(do_GetService(NS_WINDOWMEDIATOR_CONTRACTID));
00317   if (!wm)
00318   {
00319     return NS_ERROR_FAILURE;
00320   } 
00321   else
00322   {
00323     nsCOMPtr<nsISimpleEnumerator> windowList;
00324     wm->GetXULWindowEnumerator(nsnull, getter_AddRefs(windowList));
00325     PRBool more;
00326     windowList->HasMoreElements(&more);
00327     while (more)
00328     {
00329       nsCOMPtr<nsISupports> nextWindow = nsnull;
00330       windowList->GetNext(getter_AddRefs(nextWindow));
00331       nsCOMPtr<nsIBaseWindow> baseWindow(do_QueryInterface(nextWindow));
00332                 if (!baseWindow)
00333                 {
00334         windowList->HasMoreElements(&more);
00335         continue;
00336       }
00337       else
00338       {
00339         haveOpenWindows = PR_TRUE;
00340       }
00341 
00342       nsCOMPtr<nsIWidget> widget = nsnull;
00343       baseWindow->GetMainWidget(getter_AddRefs(widget));
00344       if (!widget)
00345       {
00346         windowList->HasMoreElements(&more);
00347         continue;
00348       }
00349       WindowRef windowRef = (WindowRef)widget->GetNativeData(NS_NATIVE_DISPLAY);
00350       if (!::IsWindowCollapsed(windowRef))
00351       {
00352         haveUncollapsed = PR_TRUE;
00353         break;  //have un-minimized windows, nothing to do
00354       } 
00355       windowList->HasMoreElements(&more);
00356     } // end while
00357         
00358     if (!haveUncollapsed)
00359     {
00360       //uncollapse the most recenty used window
00361       nsCOMPtr<nsIDOMWindowInternal> mru = nsnull;
00362       wm->GetMostRecentWindow(nsnull, getter_AddRefs(mru));
00363             
00364       if (mru) 
00365       {        
00366         WindowRef mruRef = nil;
00367         GetNativeWindowPointerFromDOMWindow(mru, &mruRef);
00368         if (mruRef)
00369         {
00370           ::CollapseWindow(mruRef, FALSE);
00371           ::SelectWindow(mruRef);
00372           done = PR_TRUE;
00373         }
00374       }
00375       
00376     } // end if have uncollapsed 
00377     
00378     if (!haveOpenWindows && !done)
00379     {    
00380     
00381       NS_WARNING("trying to open new window");
00382       //use the bootstrap helpers to make the right kind(s) of window open        
00383       nsresult rv = PR_FALSE;
00384       nsCOMPtr<nsIAppStartup> appStartup(do_GetService(NS_APPSTARTUP_CONTRACTID));
00385       if (appStartup)
00386       {
00387         PRBool openedAWindow = PR_FALSE;
00388         appStartup->CreateStartupState(nsIAppShellService::SIZE_TO_CONTENT,
00389                                        nsIAppShellService::SIZE_TO_CONTENT,
00390                                        &openedAWindow);
00391       }
00392     }
00393     
00394   } // got window mediator
00395   return NS_OK;
00396 }
00397 
00398 nsresult
00399 GetNativeWindowPointerFromDOMWindow(nsIDOMWindowInternal *a_window, WindowRef *a_nativeWindow)
00400 {
00401     *a_nativeWindow = nil;
00402     if (!a_window) return NS_ERROR_INVALID_ARG;
00403     
00404     nsCOMPtr<nsIWebNavigation> mruWebNav(do_GetInterface(a_window));
00405     if (mruWebNav)
00406     {
00407       nsCOMPtr<nsIDocShellTreeItem> mruTreeItem(do_QueryInterface(mruWebNav));
00408       nsCOMPtr<nsIDocShellTreeOwner> mruTreeOwner = nsnull;
00409       mruTreeItem->GetTreeOwner(getter_AddRefs(mruTreeOwner));
00410       if(mruTreeOwner)
00411       {
00412         nsCOMPtr<nsIBaseWindow> mruBaseWindow(do_QueryInterface(mruTreeOwner));
00413         if (mruBaseWindow)
00414         {
00415           nsCOMPtr<nsIWidget> mruWidget = nsnull;
00416           mruBaseWindow->GetMainWidget(getter_AddRefs(mruWidget));
00417           if (mruWidget)
00418           {
00419             *a_nativeWindow = (WindowRef)mruWidget->GetNativeData(NS_NATIVE_DISPLAY);
00420           }
00421         }
00422       }
00423     }
00424     return NS_OK;
00425 }
00426 
00427 #pragma mark -
00428 
00429 // Create and return an instance of class nsNativeAppSupportMac.
00430 nsresult NS_CreateNativeAppSupport(nsINativeAppSupport**aResult)
00431 {
00432   if ( aResult )
00433   {  
00434       *aResult = new nsNativeAppSupportMac;
00435       if ( *aResult )
00436       {
00437           NS_ADDREF( *aResult );
00438           return NS_OK;
00439       } 
00440       else
00441       {
00442           return NS_ERROR_OUT_OF_MEMORY;
00443       }
00444   } 
00445   else
00446   {
00447       return NS_ERROR_NULL_POINTER;
00448   }
00449 }
00450 
00451 // Snagged from mozilla/xpinstall/wizrd/mac/src/SetupTypeWin.c
00452 // VersGreaterThan4 - utility function to test if it's >4.x running
00453 static Boolean VersGreaterThan4(const FSSpec *fSpec)
00454 {
00455   Boolean result = false;
00456   short fRefNum = 0;
00457   
00458   ::SetResLoad(false);
00459   fRefNum = ::FSpOpenResFile(fSpec, fsRdPerm);
00460   ::SetResLoad(true);
00461   if (fRefNum != -1)
00462   {
00463     Handle  h;
00464     h = ::Get1Resource('vers', 2);
00465     if (h && **(unsigned short**)h >= 0x0500)
00466       result = true;
00467     ::CloseResFile(fRefNum);
00468   }
00469     
00470   return result;
00471 }