1
/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
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
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with 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.org code.
17
* The Initial Developer of the Original Code is
19
* Portions created by the Initial Developer are Copyright (C) 2003
20
* the Initial Developer. All Rights Reserved.
23
* Michael Lowe <michael.lowe@bigfoot.com>
24
* John Fairhurst <john_fairhurst@iname.com>
26
* Alternatively, the contents of this file may be used under the terms of
27
* either the GNU General Public License Version 2 or later (the "GPL"), or
28
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the NPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the NPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
40
#include "nsAppShell.h"
41
#include "nsToolkit.h"
42
#include "nsIWidget.h"
43
#include "nsIEventQueueService.h"
44
#include "nsIServiceManager.h"
46
#include "nsWidgetsCID.h"
48
NS_IMPL_ISUPPORTS1(nsAppShell, nsIAppShell)
50
static int gKeepGoing = 1;
51
//-------------------------------------------------------------------------
53
// nsAppShell constructor
55
//-------------------------------------------------------------------------
56
nsAppShell::nsAppShell()
62
//-------------------------------------------------------------------------
64
// Create the application shell
66
//-------------------------------------------------------------------------
68
NS_METHOD nsAppShell::Create(int* argc, char ** argv)
73
//-------------------------------------------------------------------------
75
// Enter a message handler loop
77
//-------------------------------------------------------------------------
79
#include "nsITimerManager.h"
81
NS_METHOD nsAppShell::Run(void)
88
nsCOMPtr<nsITimerManager> timerManager(do_GetService("@mozilla.org/timer/manager;1", &rv));
89
if (NS_FAILED(rv)) return rv;
91
// Using idle timers breaks drag drop, so turn it off for now
92
timerManager->SetUseIdleTimers(PR_FALSE);
97
// Give priority to system messages (in particular keyboard, mouse,
98
// timer, and paint messages).
99
if (WinPeekMsg((HAB)0, &qmsg, NULL, WM_CHAR, WM_VIOCHAR, PM_REMOVE) ||
100
WinPeekMsg((HAB)0, &qmsg, NULL, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE) ||
101
WinPeekMsg((HAB)0, &qmsg, NULL, 0, WM_USER-1, PM_REMOVE) ||
102
WinPeekMsg((HAB)0, &qmsg, NULL, 0, 0, PM_REMOVE)) {
103
keepGoing = (qmsg.msg != WM_QUIT);
105
if (keepGoing != 0) {
106
WinDispatchMsg((HAB)0, &qmsg);
108
if ((qmsg.hwnd) && (qmsg.mp1 || qmsg.mp2)) {
109
// If close is selected from the task list, only close
110
// that window, not the entire application
111
WinSendMsg(qmsg.hwnd, WM_SYSCOMMAND, MPFROMSHORT(SC_CLOSE), 0);
118
timerManager->HasIdleTimers(&hasTimers);
121
timerManager->FireNextIdleTimer();
122
timerManager->HasIdleTimers(&hasTimers);
123
} while (hasTimers && WinPeekMsg((HAB)0, &qmsg, NULL, 0, 0, PM_NOREMOVE));
127
// In this situation, PostQuitMessage() was called, but the WM_QUIT
128
// message was removed from the event queue by someone else -
129
// (see bug #54725). So, just exit the loop as if WM_QUIT had been
134
// Block and wait for any posted application message
135
::WinWaitMsg((HAB)0, 0, 0);
140
} while (keepGoing != 0);
145
inline NS_METHOD nsAppShell::Spinup(void)
148
inline NS_METHOD nsAppShell::Spindown(void)
151
inline NS_METHOD nsAppShell::ListenToEventQueue(nsIEventQueue * aQueue, PRBool aListen)
155
nsAppShell::GetNativeEvent(PRBool &aRealEvent, void *&aEvent)
159
PRBool gotMessage = PR_FALSE;
162
nsCOMPtr<nsITimerManager> timerManager(do_GetService("@mozilla.org/timer/manager;1", &rv));
163
if (NS_FAILED(rv)) return rv;
166
// Give priority to system messages (in particular keyboard, mouse,
167
// timer, and paint messages).
168
if (WinPeekMsg((HAB)0, &qmsg, NULL, WM_CHAR, WM_VIOCHAR, PM_REMOVE) ||
169
WinPeekMsg((HAB)0, &qmsg, NULL, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE) ||
170
WinPeekMsg((HAB)0, &qmsg, NULL, 0, WM_USER-1, PM_REMOVE) ||
171
WinPeekMsg((HAB)0, &qmsg, NULL, 0, 0, PM_REMOVE)) {
172
gotMessage = PR_TRUE;
175
timerManager->HasIdleTimers(&hasTimers);
178
timerManager->FireNextIdleTimer();
179
timerManager->HasIdleTimers(&hasTimers);
180
} while (hasTimers && WinPeekMsg((HAB)0, &qmsg, NULL, 0, 0, PM_NOREMOVE));
182
// Block and wait for any posted application message
183
::WinWaitMsg((HAB)0, 0, 0);
187
} while (gotMessage == PR_FALSE);
190
if (qmsg.msg != WM_TIMER)
191
printf("-> %d", qmsg.msg);
195
aRealEvent = PR_TRUE;
199
nsresult nsAppShell::DispatchNativeEvent(PRBool aRealEvent, void *aEvent)
201
WinDispatchMsg((HAB)0, (QMSG *)aEvent);
205
//-------------------------------------------------------------------------
207
// Exit a message handler loop
209
//-------------------------------------------------------------------------
211
NS_METHOD nsAppShell::Exit(void)
213
WinPostQueueMsg(HMQ_CURRENT, WM_QUIT, 0, 0);
215
// Also, set a global flag, just in case someone eats the WM_QUIT message.
222
//-------------------------------------------------------------------------
224
// nsAppShell destructor
226
//-------------------------------------------------------------------------
227
nsAppShell::~nsAppShell()