Bumping manifests a=b2g-bump
[gecko.git] / widget / windows / nsColorPicker.cpp
blobf6a4d679c50bed32a4daad53085e0b79eb100bc3
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/AutoRestore.h"
12 #include "nsIWidget.h"
13 #include "nsString.h"
14 #include "WidgetUtils.h"
16 using namespace mozilla::widget;
18 namespace
20 // Manages NS_NATIVE_TMP_WINDOW child windows. NS_NATIVE_TMP_WINDOWs are
21 // temporary child windows of mParentWidget created to address RTL issues
22 // in picker dialogs. We are responsible for destroying these.
23 class AutoDestroyTmpWindow
25 public:
26 explicit AutoDestroyTmpWindow(HWND aTmpWnd) :
27 mWnd(aTmpWnd) {
30 ~AutoDestroyTmpWindow() {
31 if (mWnd)
32 DestroyWindow(mWnd);
35 inline HWND get() const { return mWnd; }
36 private:
37 HWND mWnd;
40 static DWORD ColorStringToRGB(const nsAString& aColor)
42 DWORD result = 0;
44 for (uint32_t i = 1; i < aColor.Length(); ++i) {
45 result *= 16;
47 char16_t c = aColor[i];
48 if (c >= '0' && c <= '9') {
49 result += c - '0';
50 } else if (c >= 'a' && c <= 'f') {
51 result += 10 + (c - 'a');
52 } else {
53 result += 10 + (c - 'A');
57 DWORD r = result & 0x00FF0000;
58 DWORD g = result & 0x0000FF00;
59 DWORD b = result & 0x000000FF;
61 r = r >> 16;
62 b = b << 16;
64 result = r | g | b;
66 return result;
69 static nsString ToHexString(BYTE n)
71 nsString result;
72 if (n <= 0x0F) {
73 result.Append('0');
75 result.AppendInt(n, 16);
76 return result;
80 static void
81 BGRIntToRGBString(DWORD color, nsAString& aResult)
83 BYTE r = GetRValue(color);
84 BYTE g = GetGValue(color);
85 BYTE b = GetBValue(color);
87 aResult.Assign('#');
88 aResult.Append(ToHexString(r));
89 aResult.Append(ToHexString(g));
90 aResult.Append(ToHexString(b));
92 } // anonymous namespace
94 static AsyncColorChooser* gColorChooser;
96 AsyncColorChooser::AsyncColorChooser(COLORREF aInitialColor,
97 nsIWidget* aParentWidget,
98 nsIColorPickerShownCallback* aCallback)
99 : mInitialColor(aInitialColor)
100 , mColor(aInitialColor)
101 , mParentWidget(aParentWidget)
102 , mCallback(aCallback)
106 NS_IMETHODIMP
107 AsyncColorChooser::Run()
109 static COLORREF sCustomColors[16] = {0} ;
111 MOZ_ASSERT(NS_IsMainThread(),
112 "Color pickers can only be opened from main thread currently");
114 // Allow only one color picker to be opened at a time, to workaround bug 944737
115 if (!gColorChooser) {
116 mozilla::AutoRestore<AsyncColorChooser*> restoreColorChooser(gColorChooser);
117 gColorChooser = this;
119 AutoDestroyTmpWindow adtw((HWND) (mParentWidget.get() ?
120 mParentWidget->GetNativeData(NS_NATIVE_TMP_WINDOW) : nullptr));
122 CHOOSECOLOR options;
123 options.lStructSize = sizeof(options);
124 options.hwndOwner = adtw.get();
125 options.Flags = CC_RGBINIT | CC_FULLOPEN | CC_ENABLEHOOK;
126 options.rgbResult = mInitialColor;
127 options.lpCustColors = sCustomColors;
128 options.lpfnHook = HookProc;
130 mColor = ChooseColor(&options) ? options.rgbResult : mInitialColor;
131 } else {
132 NS_WARNING("Currently, it's not possible to open more than one color "
133 "picker at a time");
134 mColor = mInitialColor;
137 if (mCallback) {
138 nsAutoString colorStr;
139 BGRIntToRGBString(mColor, colorStr);
140 mCallback->Done(colorStr);
143 return NS_OK;
146 void
147 AsyncColorChooser::Update(COLORREF aColor)
149 if (mColor != aColor) {
150 mColor = aColor;
152 nsAutoString colorStr;
153 BGRIntToRGBString(mColor, colorStr);
154 mCallback->Update(colorStr);
158 /* static */ UINT_PTR CALLBACK
159 AsyncColorChooser::HookProc(HWND aDialog, UINT aMsg,
160 WPARAM aWParam, LPARAM aLParam)
162 if (!gColorChooser) {
163 return 0;
166 if (aMsg == WM_CTLCOLORSTATIC) {
167 // The color picker does not expose a proper way to retrieve the current
168 // color, so we need to obtain it from the static control displaying the
169 // current color instead.
170 const int kCurrentColorBoxID = 709;
171 if ((HWND)aLParam == GetDlgItem(aDialog, kCurrentColorBoxID)) {
172 gColorChooser->Update(GetPixel((HDC)aWParam, 0, 0));
176 return 0;
179 ///////////////////////////////////////////////////////////////////////////////
180 // nsIColorPicker
182 nsColorPicker::nsColorPicker()
186 nsColorPicker::~nsColorPicker()
190 NS_IMPL_ISUPPORTS(nsColorPicker, nsIColorPicker)
192 NS_IMETHODIMP
193 nsColorPicker::Init(nsIDOMWindow* parent,
194 const nsAString& title,
195 const nsAString& aInitialColor)
197 NS_PRECONDITION(parent,
198 "Null parent passed to colorpicker, no color picker for you!");
199 mParentWidget = WidgetUtils::DOMWindowToWidget(parent);
200 mInitialColor = ColorStringToRGB(aInitialColor);
201 return NS_OK;
204 NS_IMETHODIMP
205 nsColorPicker::Open(nsIColorPickerShownCallback* aCallback)
207 NS_ENSURE_ARG(aCallback);
208 nsCOMPtr<nsIRunnable> event = new AsyncColorChooser(mInitialColor,
209 mParentWidget,
210 aCallback);
211 return NS_DispatchToMainThread(event);