Back to index

lightning-sunbird  0.9+nobinonly
nsAppShell.cpp
Go to the documentation of this file.
00001 /* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
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.org 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  *  Mark Mentovai <mark@moxienet.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * 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  * Runs the main native Carbon run loop.
00041  */
00042 
00043 #include "nsAppShell.h"
00044 #include "nsToolkit.h"
00045 #include "nsMacMessagePump.h"
00046 
00047 #include <Carbon/Carbon.h>
00048 
00049 NS_IMPL_THREADSAFE_ISUPPORTS1(nsAppShell, nsIAppShell)
00050 
00051 nsMacMessagePump* nsAppShell::sMacPump;
00052 nsAppShell* nsAppShell::sMacPumpOwner;
00053 
00054 nsAppShell::nsAppShell()
00055 {
00056 }
00057 
00058 nsAppShell::~nsAppShell()
00059 {
00060   if (sMacPumpOwner == this) {
00061     delete sMacPump;
00062     sMacPump = NULL;
00063   }
00064 }
00065 
00066 NS_IMETHODIMP
00067 nsAppShell::Create(int* argc, char** argv)
00068 {
00069   nsresult rv = NS_GetCurrentToolkit(getter_AddRefs(mToolkit));
00070   if (NS_FAILED(rv))
00071    return rv;
00072 
00073   nsIToolkit* toolkit = mToolkit.get();
00074 
00075   if (!sMacPump) {
00076     sMacPump = new nsMacMessagePump(NS_STATIC_CAST(nsToolkit*, toolkit));
00077     if (sMacPump)
00078       sMacPumpOwner = this;
00079     else
00080       return NS_ERROR_OUT_OF_MEMORY;
00081   }
00082 
00083   return NS_OK;
00084 }
00085 
00086 NS_IMETHODIMP
00087 nsAppShell::Run()
00088 {
00089   if (!sMacPump)
00090     return NS_ERROR_NOT_INITIALIZED;
00091 
00092   PRBool wasProcessing = sMacPump->ProcessEvents(PR_TRUE);
00093   ::RunApplicationEventLoop();
00094   sMacPump->ProcessEvents(wasProcessing);
00095 
00096   return NS_OK;
00097 }
00098 
00099 NS_IMETHODIMP
00100 nsAppShell::Exit()
00101 {
00102   ::QuitApplicationEventLoop();
00103   return NS_OK;
00104 }
00105 
00106 NS_IMETHODIMP
00107 nsAppShell::Spinup()
00108 {
00109   // Nothing to do
00110   return NS_OK;
00111 }
00112 
00113 NS_IMETHODIMP
00114 nsAppShell::Spindown()
00115 {
00116   // Nothing to do
00117   return NS_OK;
00118 }
00119 
00120 NS_IMETHODIMP
00121 nsAppShell::ListenToEventQueue(nsIEventQueue* aQueue, PRBool aListen)
00122 {
00123   // Nothing to do
00124   return NS_OK;
00125 }
00126 
00127 NS_IMETHODIMP
00128 nsAppShell::GetNativeEvent(PRBool& aRealEvent, void*& aEvent)
00129 {
00130   aRealEvent = PR_FALSE;
00131   aEvent = nsnull;
00132 
00133   EventRef carbonEvent;
00134   OSStatus err =
00135    ::ReceiveNextEvent(0, nsnull, 0.1, PR_TRUE, &carbonEvent);
00136   if (err == noErr && carbonEvent) {
00137     aRealEvent = PR_TRUE;
00138     aEvent = carbonEvent;
00139   }
00140 
00141   return NS_OK;
00142 }
00143 
00144 NS_IMETHODIMP
00145 nsAppShell::DispatchNativeEvent(PRBool aRealEvent, void* aEvent)
00146 {
00147   if (aRealEvent) {
00148     EventRef carbonEvent = NS_STATIC_CAST(EventRef, aEvent);
00149 
00150     if (!sMacPump)
00151       return NS_ERROR_NOT_INITIALIZED;
00152 
00153     PRBool wasProcessing = sMacPump->ProcessEvents(PR_TRUE);
00154     ::SendEventToEventTarget(carbonEvent, ::GetEventDispatcherTarget());
00155     sMacPump->ProcessEvents(wasProcessing);
00156 
00157     // This can be bad, but the only way DispatchNativeEvent is ever
00158     // used, it's tightly coupled to GetNativeEvent and is only called
00159     // once.  This is really the only good place to release it and
00160     // avoid leaking.
00161     ::ReleaseEvent(carbonEvent);
00162   }
00163 
00164   return NS_OK;
00165 }