Backed out changeset 3fe07c50c854 (bug 946316) for bustage. a=backout
[gecko.git] / widget / os2 / nsAppShell.cpp
blob13d26f31bdb8b2012b0844c86510310284e2cc1b
1 /* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsAppShell.h"
7 #include "nsThreadUtils.h"
9 static UINT sMsgId;
11 //-------------------------------------------------------------------------
13 MRESULT EXPENTRY EventWindowProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
15 if (msg == sMsgId) {
16 nsAppShell *as = reinterpret_cast<nsAppShell *>(mp2);
17 as->NativeEventCallback();
18 NS_RELEASE(as);
19 return (MRESULT)TRUE;
21 return WinDefWindowProc(hwnd, msg, mp1, mp2);
24 nsAppShell::~nsAppShell()
26 if (mEventWnd) {
27 // DestroyWindow doesn't do anything when called from a non UI thread.
28 // Since mEventWnd was created on the UI thread, it must be destroyed on
29 // the UI thread.
30 WinSendMsg(mEventWnd, WM_CLOSE, 0, 0);
34 nsresult
35 nsAppShell::Init()
37 // a message queue is required to create a window but
38 // it is not necessarily created yet
39 if (WinQueryQueueInfo(HMQ_CURRENT, nullptr, 0) == FALSE) {
40 // Set our app to be a PM app before attempting Win calls
41 PPIB ppib;
42 PTIB ptib;
43 DosGetInfoBlocks(&ptib, &ppib);
44 ppib->pib_ultype = 3;
46 HAB hab = WinInitialize(0);
47 WinCreateMsgQueue(hab, 0);
50 if (!sMsgId) {
51 sMsgId = WinAddAtom( WinQuerySystemAtomTable(), "nsAppShell:EventID");
52 WinRegisterClass((HAB)0, "nsAppShell:EventWindowClass", EventWindowProc,
53 nullptr, 0);
56 mEventWnd = ::WinCreateWindow(HWND_DESKTOP,
57 "nsAppShell:EventWindowClass",
58 "nsAppShell:EventWindow",
60 0, 0,
61 10, 10,
62 HWND_DESKTOP,
63 HWND_BOTTOM,
64 0, 0, 0);
65 NS_ENSURE_STATE(mEventWnd);
67 return nsBaseAppShell::Init();
70 void
71 nsAppShell::ScheduleNativeEventCallback()
73 // post a message to the native event queue...
74 NS_ADDREF_THIS();
75 WinPostMsg(mEventWnd, sMsgId, 0, reinterpret_cast<MPARAM>(this));
78 bool
79 nsAppShell::ProcessNextNativeEvent(bool mayWait)
81 bool gotMessage = false;
83 do {
84 QMSG qmsg;
85 // Give priority to system messages (in particular keyboard, mouse, timer,
86 // and paint messages).
87 if (WinPeekMsg((HAB)0, &qmsg, nullptr, WM_CHAR, WM_VIOCHAR, PM_REMOVE) ||
88 WinPeekMsg((HAB)0, &qmsg, nullptr, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE) ||
89 WinPeekMsg((HAB)0, &qmsg, nullptr, 0, WM_USER-1, PM_REMOVE) ||
90 WinPeekMsg((HAB)0, &qmsg, nullptr, 0, 0, PM_REMOVE)) {
91 gotMessage = true;
92 ::WinDispatchMsg((HAB)0, &qmsg);
93 } else if (mayWait) {
94 // Block and wait for any posted application message
95 ::WinWaitMsg((HAB)0, 0, 0);
97 } while (!gotMessage && mayWait);
99 return gotMessage;