Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / widget / windows / nsColorPicker.cpp
blob5074b620f54ebe721aab81b5ca8a0e89a5ca803e
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsColorPicker.h"
9 #include <shlwapi.h>
11 #include "mozilla/ArrayUtils.h"
12 #include "mozilla/AutoRestore.h"
13 #include "nsIWidget.h"
14 #include "nsString.h"
15 #include "WidgetUtils.h"
16 #include "WinUtils.h"
17 #include "nsPIDOMWindow.h"
19 using namespace mozilla::widget;
21 namespace {
22 static DWORD ColorStringToRGB(const nsAString& aColor) {
23 DWORD result = 0;
25 for (uint32_t i = 1; i < aColor.Length(); ++i) {
26 result *= 16;
28 char16_t c = aColor[i];
29 if (c >= '0' && c <= '9') {
30 result += c - '0';
31 } else if (c >= 'a' && c <= 'f') {
32 result += 10 + (c - 'a');
33 } else {
34 result += 10 + (c - 'A');
38 DWORD r = result & 0x00FF0000;
39 DWORD g = result & 0x0000FF00;
40 DWORD b = result & 0x000000FF;
42 r = r >> 16;
43 b = b << 16;
45 result = r | g | b;
47 return result;
50 static nsString ToHexString(BYTE n) {
51 nsString result;
52 if (n <= 0x0F) {
53 result.Append('0');
55 result.AppendInt(n, 16);
56 return result;
59 static void BGRIntToRGBString(DWORD color, nsAString& aResult) {
60 BYTE r = GetRValue(color);
61 BYTE g = GetGValue(color);
62 BYTE b = GetBValue(color);
64 aResult.Assign('#');
65 aResult.Append(ToHexString(r));
66 aResult.Append(ToHexString(g));
67 aResult.Append(ToHexString(b));
69 } // namespace
71 static AsyncColorChooser* gColorChooser;
73 AsyncColorChooser::AsyncColorChooser(COLORREF aInitialColor,
74 const nsTArray<nsString>& aDefaultColors,
75 nsIWidget* aParentWidget,
76 nsIColorPickerShownCallback* aCallback)
77 : mozilla::Runnable("AsyncColorChooser"),
78 mInitialColor(aInitialColor),
79 mDefaultColors(aDefaultColors.Clone()),
80 mColor(aInitialColor),
81 mParentWidget(aParentWidget),
82 mCallback(aCallback) {}
84 NS_IMETHODIMP
85 AsyncColorChooser::Run() {
86 MOZ_ASSERT(NS_IsMainThread(),
87 "Color pickers can only be opened from main thread currently");
89 // Allow only one color picker to be opened at a time, to workaround bug
90 // 944737
91 if (!gColorChooser) {
92 mozilla::AutoRestore<AsyncColorChooser*> restoreColorChooser(gColorChooser);
93 gColorChooser = this;
95 ScopedRtlShimWindow shim(mParentWidget.get());
97 COLORREF customColors[16];
98 for (size_t i = 0; i < mozilla::ArrayLength(customColors); i++) {
99 if (i < mDefaultColors.Length()) {
100 customColors[i] = ColorStringToRGB(mDefaultColors[i]);
101 } else {
102 customColors[i] = 0x00FFFFFF;
106 CHOOSECOLOR options;
107 options.lStructSize = sizeof(options);
108 options.hwndOwner = shim.get();
109 options.Flags = CC_RGBINIT | CC_FULLOPEN | CC_ENABLEHOOK;
110 options.rgbResult = mInitialColor;
111 options.lpCustColors = customColors;
112 options.lpfnHook = HookProc;
114 mColor = ChooseColor(&options) ? options.rgbResult : mInitialColor;
115 } else {
116 NS_WARNING(
117 "Currently, it's not possible to open more than one color "
118 "picker at a time");
119 mColor = mInitialColor;
122 if (mCallback) {
123 nsAutoString colorStr;
124 BGRIntToRGBString(mColor, colorStr);
125 mCallback->Done(colorStr);
128 return NS_OK;
131 void AsyncColorChooser::Update(COLORREF aColor) {
132 if (mColor != aColor) {
133 mColor = aColor;
135 nsAutoString colorStr;
136 BGRIntToRGBString(mColor, colorStr);
137 mCallback->Update(colorStr);
141 /* static */ UINT_PTR CALLBACK AsyncColorChooser::HookProc(HWND aDialog,
142 UINT aMsg,
143 WPARAM aWParam,
144 LPARAM aLParam) {
145 if (!gColorChooser) {
146 return 0;
149 if (aMsg == WM_INITDIALOG) {
150 // "The default dialog box procedure processes the WM_INITDIALOG message
151 // before passing it to the hook procedure.
152 // For all other messages, the hook procedure receives the message first."
153 // https://docs.microsoft.com/en-us/windows/win32/api/commdlg/nc-commdlg-lpcchookproc
154 // "The dialog box procedure should return TRUE to direct the system to
155 // set the keyboard focus to the control specified by wParam."
156 // https://docs.microsoft.com/en-us/windows/win32/dlgbox/wm-initdialog
157 return 1;
160 if (aMsg == WM_CTLCOLORSTATIC) {
161 // The color picker does not expose a proper way to retrieve the current
162 // color, so we need to obtain it from the static control displaying the
163 // current color instead.
164 const int kCurrentColorBoxID = 709;
165 if ((HWND)aLParam == GetDlgItem(aDialog, kCurrentColorBoxID)) {
166 gColorChooser->Update(GetPixel((HDC)aWParam, 0, 0));
170 // Let the default dialog box procedure processes the message.
171 return 0;
174 ///////////////////////////////////////////////////////////////////////////////
175 // nsIColorPicker
177 nsColorPicker::nsColorPicker() {}
179 nsColorPicker::~nsColorPicker() {}
181 NS_IMPL_ISUPPORTS(nsColorPicker, nsIColorPicker)
183 NS_IMETHODIMP
184 nsColorPicker::Init(mozIDOMWindowProxy* parent, const nsAString& title,
185 const nsAString& aInitialColor,
186 const nsTArray<nsString>& aDefaultColors) {
187 MOZ_ASSERT(parent,
188 "Null parent passed to colorpicker, no color picker for you!");
189 mParentWidget =
190 WidgetUtils::DOMWindowToWidget(nsPIDOMWindowOuter::From(parent));
191 mInitialColor = ColorStringToRGB(aInitialColor);
192 mDefaultColors.Assign(aDefaultColors);
193 return NS_OK;
196 NS_IMETHODIMP
197 nsColorPicker::Open(nsIColorPickerShownCallback* aCallback) {
198 NS_ENSURE_ARG(aCallback);
199 nsCOMPtr<nsIRunnable> event = new AsyncColorChooser(
200 mInitialColor, mDefaultColors, mParentWidget, aCallback);
201 return NS_DispatchToMainThread(event);