Back to index

lightning-sunbird  0.9+nobinonly
nsToolkit.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.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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * 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 
00038 #include "nsToolkit.h"
00039 
00040 #include "nsIEventSink.h"
00041 
00042 #include "nsWidgetSupport.h"
00043 
00044 #include <Gestalt.h>
00045 #include <Movies.h>
00046 
00047 #include "nsIContent.h"
00048 #include "nsIDocument.h"
00049 #include "nsIFrame.h"
00050 #include "nsIView.h"
00051 #include "nsIInterfaceRequestor.h"
00052 
00053 
00054 //-------------------------------------------------------------------------
00055 //
00056 //-------------------------------------------------------------------------
00057 nsToolkit::nsToolkit()
00058 {
00059   ::EnterMovies();   // Required for nsSound to Work! -- fix for bug 194632
00060 }
00061 
00062 //-------------------------------------------------------------------------
00063 //
00064 //-------------------------------------------------------------------------
00065 nsToolkit::~nsToolkit()
00066 { 
00067   ::ExitMovies();    // done with Movie Toolbox -- fix for bug 194632
00068 }
00069 
00070 
00071 //-------------------------------------------------------------------------
00072 //
00073 //-------------------------------------------------------------------------
00074 nsresult
00075 nsToolkit::InitEventQueue(PRThread * aThread)
00076 {
00077   return NS_OK;
00078 }
00079 
00080 
00081 //
00082 // Return the OS X version as returned from Gestalt(gestaltSystemVersion, ...)
00083 //
00084 long
00085 nsToolkit :: OSXVersion()
00086 {
00087   static long gOSXVersion = 0x0;
00088   if (gOSXVersion == 0x0) {
00089     OSErr err = ::Gestalt(gestaltSystemVersion, &gOSXVersion);
00090     if (err != noErr) {
00091       NS_ERROR("Couldn't determine OS X version, assume 10.0");
00092       gOSXVersion = MAC_OS_X_VERSION_10_0_HEX;
00093     }
00094   }
00095   return gOSXVersion;
00096 }
00097 
00098 
00099 //
00100 // GetTopWidget
00101 //
00102 // We've stashed the nsIWidget for the given windowPtr in the data 
00103 // properties of the window. Fetch it.
00104 //
00105 void
00106 nsToolkit::GetTopWidget ( WindowPtr aWindow, nsIWidget** outWidget )
00107 {
00108   nsIWidget* topLevelWidget = nsnull;
00109   ::GetWindowProperty ( aWindow,
00110       kTopLevelWidgetPropertyCreator, kTopLevelWidgetRefPropertyTag,
00111       sizeof(nsIWidget*), nsnull, (void*)&topLevelWidget);
00112   if ( topLevelWidget ) {
00113     *outWidget = topLevelWidget;
00114     NS_ADDREF(*outWidget);
00115   }
00116 }
00117 
00118 
00119 //
00120 // GetWindowEventSink
00121 //
00122 // We've stashed the nsIEventSink for the given windowPtr in the data 
00123 // properties of the window. Fetch it.
00124 //
00125 void
00126 nsToolkit::GetWindowEventSink ( WindowPtr aWindow, nsIEventSink** outSink )
00127 {
00128   *outSink = nsnull;
00129   
00130   nsCOMPtr<nsIWidget> topWidget;
00131   GetTopWidget ( aWindow, getter_AddRefs(topWidget) );
00132   nsCOMPtr<nsIEventSink> sink ( do_QueryInterface(topWidget) );
00133   if ( sink ) {
00134     *outSink = sink;
00135     NS_ADDREF(*outSink);
00136   }
00137 }
00138 
00139 
00140 nsIView *
00141 nsToolkit::GetViewFor(nsIWidget *aWidget)
00142 {
00143   if (!aWidget)
00144     return nsnull;
00145   nsIView *retval = nsnull;
00146   nsISupports *data = nsnull;
00147   aWidget->GetClientData((void*&)data);
00148   nsCOMPtr<nsIInterfaceRequestor> req(do_QueryInterface(data));
00149   if (req)
00150     req->GetInterface(NS_GET_IID(nsIView), (void**) &retval);
00151   return retval;
00152 }
00153 
00154 
00155 // Returns the nsIDocument associated with the first available nsIContent
00156 // at or "below" aWidget.
00157 nsIDocument *
00158 nsToolkit::GetDocumentFor(nsIWidget *aWidget)
00159 {
00160   if (!aWidget)
00161     return nsnull;
00162   nsIFrame *frame = nsnull;
00163   nsIContent *content = nsnull;
00164   nsIDocument *document = nsnull;
00165   nsIView *view = nsToolkit::GetViewFor(aWidget);
00166   if (view)
00167     frame = NS_STATIC_CAST(nsIFrame*, view->GetClientData());
00168   if (frame)
00169     content = frame->GetContent();
00170   if (content)
00171     document = content->GetCurrentDoc();
00172   if (document)
00173     return document;
00174   nsIWidget *widgetChild = aWidget->GetFirstChild();
00175   while (widgetChild) {
00176     if ((document = GetDocumentFor(widgetChild)) != nsnull)
00177       return document;
00178     widgetChild = widgetChild->GetNextSibling();
00179   }
00180   return nsnull;
00181 }
00182 
00183 
00184 //-------------------------------------------------------------------------
00185 //
00186 //-------------------------------------------------------------------------
00187 nsToolkitBase* NS_CreateToolkitInstance()
00188 {
00189   return new nsToolkit();
00190 }