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"
11 #include "mozilla/AutoRestore.h"
12 #include "nsIWidget.h"
14 #include "WidgetUtils.h"
16 using namespace mozilla::widget
;
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
26 explicit AutoDestroyTmpWindow(HWND aTmpWnd
) :
30 ~AutoDestroyTmpWindow() {
35 inline HWND
get() const { return mWnd
; }
40 static DWORD
ColorStringToRGB(const nsAString
& aColor
)
44 for (uint32_t i
= 1; i
< aColor
.Length(); ++i
) {
47 char16_t c
= aColor
[i
];
48 if (c
>= '0' && c
<= '9') {
50 } else if (c
>= 'a' && c
<= 'f') {
51 result
+= 10 + (c
- 'a');
53 result
+= 10 + (c
- 'A');
57 DWORD r
= result
& 0x00FF0000;
58 DWORD g
= result
& 0x0000FF00;
59 DWORD b
= result
& 0x000000FF;
69 static nsString
ToHexString(BYTE n
)
75 result
.AppendInt(n
, 16);
81 BGRIntToRGBString(DWORD color
, nsAString
& aResult
)
83 BYTE r
= GetRValue(color
);
84 BYTE g
= GetGValue(color
);
85 BYTE b
= GetBValue(color
);
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
)
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));
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
;
132 NS_WARNING("Currently, it's not possible to open more than one color "
134 mColor
= mInitialColor
;
138 nsAutoString colorStr
;
139 BGRIntToRGBString(mColor
, colorStr
);
140 mCallback
->Done(colorStr
);
147 AsyncColorChooser::Update(COLORREF aColor
)
149 if (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
) {
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));
179 ///////////////////////////////////////////////////////////////////////////////
182 nsColorPicker::nsColorPicker()
186 nsColorPicker::~nsColorPicker()
190 NS_IMPL_ISUPPORTS(nsColorPicker
, nsIColorPicker
)
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
);
205 nsColorPicker::Open(nsIColorPickerShownCallback
* aCallback
)
207 NS_ENSURE_ARG(aCallback
);
208 nsCOMPtr
<nsIRunnable
> event
= new AsyncColorChooser(mInitialColor
,
211 return NS_DispatchToMainThread(event
);