summaryrefslogtreecommitdiffstats
path: root/accessible/windows/msaa/nsWinUtils.cpp
blob: 42d3d374f7383f0ede8c0d9d6a55992411d6fc66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:expandtab:shiftwidth=2:tabstop=2:
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsWinUtils.h"

#include "Compatibility.h"
#include "DocAccessible.h"
#include "MsaaAccessible.h"
#include "nsAccessibilityService.h"
#include "nsCoreUtils.h"

#include "mozilla/a11y/DocAccessibleParent.h"
#include "mozilla/Preferences.h"
#include "nsArrayUtils.h"
#include "nsICSSDeclaration.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/Element.h"
#include "nsXULAppAPI.h"

using namespace mozilla;
using namespace mozilla::a11y;
using mozilla::dom::Element;

// Window property used by ipc related code in identifying accessible
// tab windows.
const wchar_t* kPropNameTabContent = L"AccessibleTabWindow";

/**
 * WindowProc to process WM_GETOBJECT messages, used in windows emulation mode.
 */
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
                                   LPARAM lParam);

bool nsWinUtils::sWindowEmulationStarted = false;

already_AddRefed<nsICSSDeclaration> nsWinUtils::GetComputedStyleDeclaration(
    nsIContent* aContent) {
  nsIContent* elm = nsCoreUtils::GetDOMElementFor(aContent);
  if (!elm) return nullptr;

  // Returns number of items in style declaration
  nsCOMPtr<nsPIDOMWindowInner> window = elm->OwnerDoc()->GetInnerWindow();
  if (!window) return nullptr;

  ErrorResult dummy;
  nsCOMPtr<Element> domElement(do_QueryInterface(elm));
  nsCOMPtr<nsICSSDeclaration> cssDecl =
      window->GetComputedStyle(*domElement, u""_ns, dummy);
  dummy.SuppressException();
  return cssDecl.forget();
}

bool nsWinUtils::MaybeStartWindowEmulation() {
  // Register window class that'll be used for document accessibles associated
  // with tabs.
  if (IPCAccessibilityActive()) return false;

  if (Compatibility::IsJAWS() || Compatibility::IsWE() ||
      Compatibility::IsDolphin() || Compatibility::IsVisperoShared()) {
    RegisterNativeWindow(kClassNameTabContent);
    sWindowEmulationStarted = true;
    return true;
  }

  return false;
}

void nsWinUtils::ShutdownWindowEmulation() {
  // Unregister window call that's used for document accessibles associated
  // with tabs.
  if (IsWindowEmulationStarted()) {
    ::UnregisterClassW(kClassNameTabContent, GetModuleHandle(nullptr));
    sWindowEmulationStarted = false;
  }
}

void nsWinUtils::RegisterNativeWindow(LPCWSTR aWindowClass) {
  WNDCLASSW wc;
  wc.style = CS_GLOBALCLASS;
  wc.lpfnWndProc = WindowProc;
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.hInstance = GetModuleHandle(nullptr);
  wc.hIcon = nullptr;
  wc.hCursor = nullptr;
  wc.hbrBackground = nullptr;
  wc.lpszMenuName = nullptr;
  wc.lpszClassName = aWindowClass;
  ::RegisterClassW(&wc);
}

HWND nsWinUtils::CreateNativeWindow(LPCWSTR aWindowClass, HWND aParentWnd,
                                    int aX, int aY, int aWidth, int aHeight,
                                    bool aIsActive,
                                    NativeWindowCreateProc* aOnCreateProc) {
  return ::CreateWindowExW(
      WS_EX_TRANSPARENT, aWindowClass, L"NetscapeDispatchWnd",
      WS_CHILD | (aIsActive ? WS_VISIBLE : 0), aX, aY, aWidth, aHeight,
      aParentWnd, nullptr, GetModuleHandle(nullptr), aOnCreateProc);
}

void nsWinUtils::ShowNativeWindow(HWND aWnd) { ::ShowWindow(aWnd, SW_SHOW); }

void nsWinUtils::HideNativeWindow(HWND aWnd) {
  ::SetWindowPos(
      aWnd, nullptr, 0, 0, 0, 0,
      SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  // Note, this window's message handling should not invoke any call that
  // may result in a cross-process ipc call. Doing so may violate RPC
  // message semantics.

  switch (msg) {
    case WM_CREATE: {
      // Mark this window so that ipc related code can identify it.
      ::SetPropW(hWnd, kPropNameTabContent, reinterpret_cast<HANDLE>(1));

      auto createStruct = reinterpret_cast<CREATESTRUCT*>(lParam);
      auto createProc = reinterpret_cast<nsWinUtils::NativeWindowCreateProc*>(
          createStruct->lpCreateParams);

      if (createProc && *createProc) {
        (*createProc)(hWnd);
      }

      return 0;
    }
    case WM_GETOBJECT: {
      // Do explicit casting to make it working on 64bit systems (see bug 649236
      // for details).
      int32_t objId = static_cast<DWORD>(lParam);
      if (objId == OBJID_CLIENT) {
        RefPtr<IAccessible> msaaAccessible;
        DocAccessible* document =
            reinterpret_cast<DocAccessible*>(::GetPropW(hWnd, kPropNameDocAcc));
        if (document) {
          document->GetNativeInterface(getter_AddRefs(msaaAccessible));
        } else {
          DocAccessibleParent* docParent = static_cast<DocAccessibleParent*>(
              ::GetPropW(hWnd, kPropNameDocAccParent));
          if (docParent) {
            msaaAccessible = MsaaAccessible::GetFrom(docParent);
          }
        }
        if (msaaAccessible) {
          LRESULT result =
              ::LresultFromObject(IID_IAccessible, wParam,
                                  msaaAccessible);  // does an addref
          return result;
        }
      }
      return 0;
    }
    case WM_NCHITTEST: {
      LRESULT lRet = ::DefWindowProc(hWnd, msg, wParam, lParam);
      if (HTCLIENT == lRet) lRet = HTTRANSPARENT;
      return lRet;
    }
  }

  return ::DefWindowProcW(hWnd, msg, wParam, lParam);
}