Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / widget / src / windows / nsAppShell.cpp
blobcddfb843fabdd103333478f6ec7f2cdca08ee0a0
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
13 * License.
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.
22 * Contributor(s):
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"
44 static UINT sMsgId;
46 //-------------------------------------------------------------------------
48 static BOOL PeekKeyAndIMEMessage(LPMSG msg, HWND hwnd)
50 MSG msg1, msg2, *lpMsg;
51 BOOL b1, b2;
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);
54 if (b1 || b2) {
55 if (b1 && b2) {
56 if (msg1.time < msg2.time)
57 lpMsg = &msg1;
58 else
59 lpMsg = &msg2;
60 } else if (b1)
61 lpMsg = &msg1;
62 else
63 lpMsg = &msg2;
64 return ::PeekMessageW(msg, hwnd, lpMsg->message, lpMsg->message, PM_REMOVE);
67 return false;
70 /*static*/ LRESULT CALLBACK
71 nsAppShell::EventWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
73 if (uMsg == sMsgId) {
74 nsAppShell *as = reinterpret_cast<nsAppShell *>(lParam);
75 as->NativeEventCallback();
76 NS_RELEASE(as);
77 return TRUE;
79 return DefWindowProc(hwnd, uMsg, wParam, lParam);
82 nsAppShell::~nsAppShell()
84 if (mEventWnd) {
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
87 // the UI thread.
88 SendMessage(mEventWnd, WM_CLOSE, 0, 0);
92 nsresult
93 nsAppShell::Init()
95 if (!sMsgId)
96 sMsgId = RegisterWindowMessageW(L"nsAppShell:EventID");
98 WNDCLASSW wc;
99 HINSTANCE module = GetModuleHandle(NULL);
101 const PRUnichar *const kWindowClass = L"nsAppShell:EventWindowClass";
102 if (!GetClassInfoW(module, kWindowClass, &wc)) {
103 wc.style = 0;
104 wc.lpfnWndProc = EventWindowProc;
105 wc.cbClsExtra = 0;
106 wc.cbWndExtra = 0;
107 wc.hInstance = module;
108 wc.hIcon = NULL;
109 wc.hCursor = NULL;
110 wc.hbrBackground = (HBRUSH) NULL;
111 wc.lpszMenuName = (LPCWSTR) NULL;
112 wc.lpszClassName = kWindowClass;
113 RegisterClassW(&wc);
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();
123 void
124 nsAppShell::ScheduleNativeEventCallback()
126 // post a message to the native event queue...
127 NS_ADDREF_THIS();
128 ::PostMessage(mEventWnd, sMsgId, 0, reinterpret_cast<LPARAM>(this));
131 PRBool
132 nsAppShell::ProcessNextNativeEvent(PRBool mayWait)
134 PRBool gotMessage = PR_FALSE;
136 do {
137 MSG msg;
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);
146 Exit();
147 } else {
148 ::TranslateMessage(&msg);
149 ::DispatchMessageW(&msg);
151 } else if (mayWait) {
152 // Block and wait for any posted application message
153 ::WaitMessage();
155 } while (!gotMessage && mayWait);
157 return gotMessage;