1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Steven Michaud <smichaud@pobox.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
41 #import <Carbon/Carbon.h>
42 #import <Cocoa/Cocoa.h>
45 #include "nsObjCExceptions.h"
46 #include "nsNativeAppSupportBase.h"
48 #include "nsIAppShellService.h"
49 #include "nsIAppStartup.h"
50 #include "nsIBaseWindow.h"
51 #include "nsICommandLineRunner.h"
52 #include "nsIDOMWindowInternal.h"
53 #include "nsIDocShellTreeItem.h"
54 #include "nsIDocShellTreeOwner.h"
55 #include "nsIInterfaceRequestorUtils.h"
56 #include "nsIObserver.h"
57 #include "nsIServiceManager.h"
58 #include "nsIWebNavigation.h"
59 #include "nsIWidget.h"
60 #include "nsIWindowMediator.h"
62 #include "nsXPFEComponentsCID.h"
64 const OSType kNSCreator = 'MOSS';
65 const OSType kMozCreator = 'MOZZ';
66 const SInt16 kNSCanRunStrArrayID = 1000;
67 const SInt16 kAnotherVersionStrIndex = 1;
70 GetNativeWindowPointerFromDOMWindow(nsIDOMWindowInternal *window, NSWindow **nativeWindow);
72 const SInt16 kNSOSVersErrsStrArrayID = 1001;
75 eOSXVersTooOldErrIndex = 1,
76 eOSXVersTooOldExplanationIndex,
77 eContinueButtonTextIndex,
79 eCarbonLibVersTooOldIndex,
80 eCarbonLibVersTooOldExplanationIndex
83 class nsNativeAppSupportCocoa : public nsNativeAppSupportBase
86 nsNativeAppSupportCocoa() :
87 mCanShowUI(PR_FALSE) { }
89 NS_IMETHOD Start(PRBool* aRetVal);
99 nsNativeAppSupportCocoa::Enable()
101 mCanShowUI = PR_TRUE;
105 /* boolean start (); */
106 NS_IMETHODIMP nsNativeAppSupportCocoa::Start(PRBool *_retval)
108 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
111 OSErr err = ::Gestalt (gestaltSystemVersion, &response);
112 response &= 0xFFFF; // The system version is in the low order word
114 // Check for at least Mac OS X 10.4, and if that fails return PR_FALSE,
115 // which will make the browser quit. In principle we could display an
116 // alert here. But the alert's message and buttons would require custom
117 // localization. So (for now at least) we just log an English message
118 // to the console before quitting.
119 if ((err != noErr) || response < 0x00001040)
121 NSLog(@"Requires Mac OS X version 10.4 or newer");
128 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
132 nsNativeAppSupportCocoa::ReOpen()
134 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
137 return NS_ERROR_FAILURE;
139 PRBool haveNonMiniaturized = PR_FALSE;
140 PRBool haveOpenWindows = PR_FALSE;
141 PRBool done = PR_FALSE;
143 nsCOMPtr<nsIWindowMediator>
144 wm(do_GetService(NS_WINDOWMEDIATOR_CONTRACTID));
147 return NS_ERROR_FAILURE;
151 nsCOMPtr<nsISimpleEnumerator> windowList;
152 wm->GetXULWindowEnumerator(nsnull, getter_AddRefs(windowList));
154 windowList->HasMoreElements(&more);
157 nsCOMPtr<nsISupports> nextWindow = nsnull;
158 windowList->GetNext(getter_AddRefs(nextWindow));
159 nsCOMPtr<nsIBaseWindow> baseWindow(do_QueryInterface(nextWindow));
162 windowList->HasMoreElements(&more);
167 haveOpenWindows = PR_TRUE;
170 nsCOMPtr<nsIWidget> widget = nsnull;
171 baseWindow->GetMainWidget(getter_AddRefs(widget));
174 windowList->HasMoreElements(&more);
177 NSWindow *cocoaWindow = (NSWindow*)widget->GetNativeData(NS_NATIVE_WINDOW);
178 if (![cocoaWindow isMiniaturized]) {
179 haveNonMiniaturized = PR_TRUE;
180 break; //have un-minimized windows, nothing to do
182 windowList->HasMoreElements(&more);
185 if (!haveNonMiniaturized)
187 // Deminiaturize the most recenty used window
188 nsCOMPtr<nsIDOMWindowInternal> mru = nsnull;
189 wm->GetMostRecentWindow(nsnull, getter_AddRefs(mru));
193 NSWindow *cocoaMru = nil;
194 GetNativeWindowPointerFromDOMWindow(mru, &cocoaMru);
196 [cocoaMru deminiaturize:nil];
201 } // end if have non miniaturized
203 if (!haveOpenWindows && !done)
205 char* argv[] = { nsnull };
207 // use an empty command line to make the right kind(s) of window open
208 nsCOMPtr<nsICommandLineRunner> cmdLine
209 (do_CreateInstance("@mozilla.org/toolkit/command-line;1"));
210 NS_ENSURE_TRUE(cmdLine, NS_ERROR_FAILURE);
213 rv = cmdLine->Init(0, argv, nsnull,
214 nsICommandLine::STATE_REMOTE_EXPLICIT);
215 NS_ENSURE_SUCCESS(rv, rv);
217 return cmdLine->Run();
220 } // got window mediator
223 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
227 GetNativeWindowPointerFromDOMWindow(nsIDOMWindowInternal *a_window, NSWindow **a_nativeWindow)
229 *a_nativeWindow = nil;
230 if (!a_window) return NS_ERROR_INVALID_ARG;
232 nsCOMPtr<nsIWebNavigation> mruWebNav(do_GetInterface(a_window));
235 nsCOMPtr<nsIDocShellTreeItem> mruTreeItem(do_QueryInterface(mruWebNav));
236 nsCOMPtr<nsIDocShellTreeOwner> mruTreeOwner = nsnull;
237 mruTreeItem->GetTreeOwner(getter_AddRefs(mruTreeOwner));
240 nsCOMPtr<nsIBaseWindow> mruBaseWindow(do_QueryInterface(mruTreeOwner));
243 nsCOMPtr<nsIWidget> mruWidget = nsnull;
244 mruBaseWindow->GetMainWidget(getter_AddRefs(mruWidget));
247 *a_nativeWindow = (NSWindow*)mruWidget->GetNativeData(NS_NATIVE_WINDOW);
257 // Create and return an instance of class nsNativeAppSupportCocoa.
258 nsresult NS_CreateNativeAppSupport(nsINativeAppSupport**aResult)
260 *aResult = new nsNativeAppSupportCocoa;
261 if (!*aResult) return NS_ERROR_OUT_OF_MEMORY;