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  *   John Fairhurst <john_fairhurst@iname.com>
00024  *   Pierre Phaneuf <pp@ludusdesign.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsToolkit.h"
00041 #include "nsSwitchToUIThread.h"
00042 
00043 // Window procedure for the internal window
00044 static MRESULT EXPENTRY nsToolkitWindowProc(HWND, ULONG, MPARAM, MPARAM);
00045 
00046 NS_IMPL_ISUPPORTS1(nsToolkit, nsIToolkit)
00047 
00048 //
00049 // Static thread local storage index of the Toolkit 
00050 // object associated with a given thread...
00051 //
00052 static PRUintn gToolkitTLSIndex = 0;
00053 
00054 //
00055 // main for the message pump thread
00056 //
00057 PRBool gThreadState = PR_FALSE;
00058 
00059 struct ThreadInitInfo {
00060     PRMonitor *monitor;
00061     nsToolkit *toolkit;
00062 };
00063 
00064 void PR_CALLBACK RunPump(void* arg)
00065 {
00066     ThreadInitInfo *info = (ThreadInitInfo*)arg;
00067     ::PR_EnterMonitor(info->monitor);
00068 
00069     // do registration and creation in this thread
00070     info->toolkit->CreateInternalWindow(PR_GetCurrentThread());
00071 
00072     gThreadState = PR_TRUE;
00073 
00074     ::PR_Notify(info->monitor);
00075     ::PR_ExitMonitor(info->monitor);
00076 
00077     delete info;
00078 
00079     // Process messages
00080     QMSG qmsg;
00081     while (WinGetMsg((HAB)0, &qmsg, 0, 0, 0)) {
00082         WinDispatchMsg((HAB)0, &qmsg);
00083     }
00084 }
00085 
00086 //-------------------------------------------------------------------------
00087 //
00088 // constructor
00089 //
00090 //-------------------------------------------------------------------------
00091 nsToolkit::nsToolkit()  
00092 {
00093     mGuiThread  = NULL;
00094     mDispatchWnd = 0;
00095     mMonitor = PR_NewMonitor();
00096 }
00097 
00098 
00099 //-------------------------------------------------------------------------
00100 //
00101 // destructor
00102 //
00103 //-------------------------------------------------------------------------
00104 nsToolkit::~nsToolkit()
00105 {
00106     NS_PRECONDITION(::WinIsWindow((HAB)0, mDispatchWnd), "Invalid window handle");
00107 
00108     // Destroy the Dispatch Window
00109     ::WinDestroyWindow(mDispatchWnd);
00110     mDispatchWnd = NULL;
00111 
00112     // Remove the TLS reference to the toolkit...
00113     PR_SetThreadPrivate(gToolkitTLSIndex, nsnull);
00114     PR_DestroyMonitor( mMonitor);
00115 }
00116 
00117 
00118 //-------------------------------------------------------------------------
00119 //
00120 // Register the window class for the internal window and create the window
00121 //
00122 //-------------------------------------------------------------------------
00123 void nsToolkit::CreateInternalWindow(PRThread *aThread)
00124 {
00125     
00126     NS_PRECONDITION(aThread, "null thread");
00127     mGuiThread  = aThread;
00128 
00129     //
00130     // create the internal window
00131     //
00132     WinRegisterClass((HAB)0, "nsToolkitClass", nsToolkitWindowProc, NULL, 0);
00133     mDispatchWnd = ::WinCreateWindow(HWND_DESKTOP,
00134                                      "nsToolkitClass",
00135                                      "NetscapeDispatchWnd",
00136                                      WS_DISABLED,
00137                                      -50, -50,
00138                                      10, 10,
00139                                      HWND_DESKTOP,
00140                                      HWND_BOTTOM,
00141                                      0, 0, 0);
00142     VERIFY(mDispatchWnd);
00143 }
00144 
00145 
00146 //-------------------------------------------------------------------------
00147 //
00148 // Create a new thread and run the message pump in there
00149 //
00150 //-------------------------------------------------------------------------
00151 void nsToolkit::CreateUIThread()
00152 {
00153     PRMonitor *monitor = ::PR_NewMonitor();
00154 
00155     ::PR_EnterMonitor(monitor);
00156 
00157     ThreadInitInfo *ti = new ThreadInitInfo();
00158     ti->monitor = monitor;
00159     ti->toolkit = this;
00160 
00161     // create a gui thread
00162     mGuiThread = ::PR_CreateThread(PR_SYSTEM_THREAD,
00163                                     RunPump,
00164                                     (void*)ti,
00165                                     PR_PRIORITY_NORMAL,
00166                                     PR_LOCAL_THREAD,
00167                                     PR_UNJOINABLE_THREAD,
00168                                     0);
00169 
00170     // wait for the gui thread to start
00171     while(gThreadState == PR_FALSE) {
00172         ::PR_Wait(monitor, PR_INTERVAL_NO_TIMEOUT);
00173     }
00174 
00175     // at this point the thread is running
00176     ::PR_ExitMonitor(monitor);
00177     ::PR_DestroyMonitor(monitor);
00178 }
00179 
00180 
00181 //-------------------------------------------------------------------------
00182 //
00183 //
00184 //-------------------------------------------------------------------------
00185 NS_METHOD nsToolkit::Init(PRThread *aThread)
00186 {
00187     // Store the thread ID of the thread containing the message pump.  
00188     // If no thread is provided create one
00189     if (NULL != aThread) {
00190         CreateInternalWindow(aThread);
00191     } else {
00192         // create a thread where the message pump will run
00193         CreateUIThread();
00194     }
00195     return NS_OK;
00196 }
00197 
00198 
00199 //-------------------------------------------------------------------------
00200 //
00201 // nsToolkit WindowProc. Used to call methods on the "main GUI thread"...
00202 //
00203 //-------------------------------------------------------------------------
00204 MRESULT EXPENTRY nsToolkitWindowProc(HWND hWnd, ULONG msg, MPARAM mp1,
00205                                      MPARAM mp2)
00206 {
00207     switch (msg) {
00208         case WM_CALLMETHOD:
00209         {
00210             MethodInfo *info = (MethodInfo *)mp2;
00211             return (MRESULT)info->Invoke();
00212         }
00213     }
00214     return ::WinDefWindowProc(hWnd, msg, mp1, mp2);
00215 }
00216 
00217 
00218 
00219 //-------------------------------------------------------------------------
00220 //
00221 // Return the nsIToolkit for the current thread.  If a toolkit does not
00222 // yet exist, then one will be created...
00223 //
00224 //-------------------------------------------------------------------------
00225 NS_METHOD NS_GetCurrentToolkit(nsIToolkit* *aResult)
00226 {
00227   nsIToolkit* toolkit = nsnull;
00228   nsresult rv = NS_OK;
00229   PRStatus status;
00230 
00231   // Create the TLS index the first time through...
00232   if (0 == gToolkitTLSIndex) {
00233     status = PR_NewThreadPrivateIndex(&gToolkitTLSIndex, NULL);
00234     if (PR_FAILURE == status) {
00235       rv = NS_ERROR_FAILURE;
00236     }
00237   }
00238 
00239   if (NS_SUCCEEDED(rv)) {
00240     toolkit = (nsIToolkit*)PR_GetThreadPrivate(gToolkitTLSIndex);
00241 
00242     //
00243     // Create a new toolkit for this thread...
00244     //
00245     if (!toolkit) {
00246       toolkit = new nsToolkit();
00247 
00248       if (!toolkit) {
00249         rv = NS_ERROR_OUT_OF_MEMORY;
00250       } else {
00251         NS_ADDREF(toolkit);
00252         toolkit->Init(PR_GetCurrentThread());
00253         //
00254         // The reference stored in the TLS is weak.  It is removed in the
00255         // nsToolkit destructor...
00256         //
00257         PR_SetThreadPrivate(gToolkitTLSIndex, (void*)toolkit);
00258       }
00259     } else {
00260       NS_ADDREF(toolkit);
00261     }
00262     *aResult = toolkit;
00263   }
00264 
00265   return rv;
00266 }