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 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.org 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 * Michael Lowe <michael.lowe@bigfoot.com>
24 * Darin Fisher <darin@meer.net>
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 MPL, 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 MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsAppShell.h"
41 #include "nsToolkit.h"
42 #include "nsThreadUtils.h"
46 //-------------------------------------------------------------------------
48 static BOOL
PeekKeyAndIMEMessage(LPMSG msg
, HWND hwnd
)
50 MSG msg1
, msg2
, *lpMsg
;
52 b1
= ::PeekMessageW(&msg1
, NULL
, WM_KEYFIRST
, WM_IME_KEYLAST
, PM_NOREMOVE
);
53 b2
= ::PeekMessageW(&msg2
, NULL
, WM_IME_SETCONTEXT
, WM_IME_KEYUP
, PM_NOREMOVE
);
56 if (msg1
.time
< msg2
.time
)
64 return ::PeekMessageW(msg
, hwnd
, lpMsg
->message
, lpMsg
->message
, PM_REMOVE
);
70 /*static*/ LRESULT CALLBACK
71 nsAppShell::EventWindowProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
74 nsAppShell
*as
= reinterpret_cast<nsAppShell
*>(lParam
);
75 as
->NativeEventCallback();
79 return DefWindowProc(hwnd
, uMsg
, wParam
, lParam
);
82 nsAppShell::~nsAppShell()
85 // DestroyWindow doesn't do anything when called from a non UI thread.
86 // Since mEventWnd was created on the UI thread, it must be destroyed on
88 SendMessage(mEventWnd
, WM_CLOSE
, 0, 0);
96 sMsgId
= RegisterWindowMessageW(L
"nsAppShell:EventID");
99 HINSTANCE module
= GetModuleHandle(NULL
);
101 const PRUnichar
*const kWindowClass
= L
"nsAppShell:EventWindowClass";
102 if (!GetClassInfoW(module
, kWindowClass
, &wc
)) {
104 wc
.lpfnWndProc
= EventWindowProc
;
107 wc
.hInstance
= module
;
110 wc
.hbrBackground
= (HBRUSH
) NULL
;
111 wc
.lpszMenuName
= (LPCWSTR
) NULL
;
112 wc
.lpszClassName
= kWindowClass
;
116 mEventWnd
= CreateWindowW(kWindowClass
, L
"nsAppShell:EventWindow",
117 0, 0, 0, 10, 10, NULL
, NULL
, module
, NULL
);
118 NS_ENSURE_STATE(mEventWnd
);
120 return nsBaseAppShell::Init();
124 nsAppShell::ScheduleNativeEventCallback()
126 // post a message to the native event queue...
128 ::PostMessage(mEventWnd
, sMsgId
, 0, reinterpret_cast<LPARAM
>(this));
132 nsAppShell::ProcessNextNativeEvent(PRBool mayWait
)
134 PRBool gotMessage
= PR_FALSE
;
138 // Give priority to system messages (in particular keyboard, mouse, timer,
139 // and paint messages).
140 if (PeekKeyAndIMEMessage(&msg
, NULL
) ||
141 ::PeekMessageW(&msg
, NULL
, WM_MOUSEFIRST
, WM_MOUSELAST
, PM_REMOVE
) ||
142 ::PeekMessageW(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
143 gotMessage
= PR_TRUE
;
144 if (msg
.message
== WM_QUIT
) {
145 ::PostQuitMessage(msg
.wParam
);
148 ::TranslateMessage(&msg
);
149 ::DispatchMessageW(&msg
);
151 } else if (mayWait
) {
152 // Block and wait for any posted application message
155 } while (!gotMessage
&& mayWait
);