michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsColorPicker.h" michael@0: michael@0: #include michael@0: michael@0: #include "mozilla/AutoRestore.h" michael@0: #include "nsIWidget.h" michael@0: #include "WidgetUtils.h" michael@0: michael@0: using namespace mozilla::widget; michael@0: michael@0: namespace michael@0: { michael@0: // Manages NS_NATIVE_TMP_WINDOW child windows. NS_NATIVE_TMP_WINDOWs are michael@0: // temporary child windows of mParentWidget created to address RTL issues michael@0: // in picker dialogs. We are responsible for destroying these. michael@0: class AutoDestroyTmpWindow michael@0: { michael@0: public: michael@0: explicit AutoDestroyTmpWindow(HWND aTmpWnd) : michael@0: mWnd(aTmpWnd) { michael@0: } michael@0: michael@0: ~AutoDestroyTmpWindow() { michael@0: if (mWnd) michael@0: DestroyWindow(mWnd); michael@0: } michael@0: michael@0: inline HWND get() const { return mWnd; } michael@0: private: michael@0: HWND mWnd; michael@0: }; michael@0: michael@0: static DWORD ColorStringToRGB(const nsAString& aColor) michael@0: { michael@0: DWORD result = 0; michael@0: michael@0: for (uint32_t i = 1; i < aColor.Length(); ++i) { michael@0: result *= 16; michael@0: michael@0: char16_t c = aColor[i]; michael@0: if (c >= '0' && c <= '9') { michael@0: result += c - '0'; michael@0: } else if (c >= 'a' && c <= 'f') { michael@0: result += 10 + (c - 'a'); michael@0: } else { michael@0: result += 10 + (c - 'A'); michael@0: } michael@0: } michael@0: michael@0: DWORD r = result & 0x00FF0000; michael@0: DWORD g = result & 0x0000FF00; michael@0: DWORD b = result & 0x000000FF; michael@0: michael@0: r = r >> 16; michael@0: b = b << 16; michael@0: michael@0: result = r | g | b; michael@0: michael@0: return result; michael@0: } michael@0: michael@0: static nsString ToHexString(BYTE n) michael@0: { michael@0: nsString result; michael@0: if (n <= 0x0F) { michael@0: result.Append('0'); michael@0: } michael@0: result.AppendInt(n, 16); michael@0: return result; michael@0: } michael@0: michael@0: michael@0: static void michael@0: BGRIntToRGBString(DWORD color, nsAString& aResult) michael@0: { michael@0: BYTE r = GetRValue(color); michael@0: BYTE g = GetGValue(color); michael@0: BYTE b = GetBValue(color); michael@0: michael@0: aResult.AssignLiteral("#"); michael@0: aResult.Append(ToHexString(r)); michael@0: aResult.Append(ToHexString(g)); michael@0: aResult.Append(ToHexString(b)); michael@0: } michael@0: } // anonymous namespace michael@0: michael@0: AsyncColorChooser::AsyncColorChooser(const nsAString& aInitialColor, michael@0: nsIWidget* aParentWidget, michael@0: nsIColorPickerShownCallback* aCallback) michael@0: : mInitialColor(aInitialColor) michael@0: , mParentWidget(aParentWidget) michael@0: , mCallback(aCallback) michael@0: { michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: AsyncColorChooser::Run() michael@0: { michael@0: static COLORREF sCustomColors[16] = {0} ; michael@0: michael@0: MOZ_ASSERT(NS_IsMainThread(), michael@0: "Color pickers can only be opened from main thread currently"); michael@0: michael@0: static bool sColorPickerOpen = false; michael@0: // Allow only one color picker to be opened at a time, to workaround bug 944737 michael@0: if (!sColorPickerOpen) { michael@0: mozilla::AutoRestore autoRestoreColorPickerOpen(sColorPickerOpen); michael@0: sColorPickerOpen = true; michael@0: michael@0: AutoDestroyTmpWindow adtw((HWND) (mParentWidget.get() ? michael@0: mParentWidget->GetNativeData(NS_NATIVE_TMP_WINDOW) : nullptr)); michael@0: michael@0: CHOOSECOLOR options; michael@0: options.lStructSize = sizeof(options); michael@0: options.hwndOwner = adtw.get(); michael@0: options.Flags = CC_RGBINIT | CC_FULLOPEN; michael@0: options.rgbResult = ColorStringToRGB(mInitialColor); michael@0: options.lpCustColors = sCustomColors; michael@0: michael@0: if (ChooseColor(&options)) { michael@0: BGRIntToRGBString(options.rgbResult, mColor); michael@0: } michael@0: } else { michael@0: NS_WARNING("Currently, it's not possible to open more than one color " michael@0: "picker at a time"); michael@0: mColor = mInitialColor; michael@0: } michael@0: michael@0: if (mCallback) { michael@0: mCallback->Done(mColor); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // nsIColorPicker michael@0: michael@0: nsColorPicker::nsColorPicker() michael@0: { michael@0: } michael@0: michael@0: nsColorPicker::~nsColorPicker() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsColorPicker, nsIColorPicker) michael@0: michael@0: NS_IMETHODIMP michael@0: nsColorPicker::Init(nsIDOMWindow* parent, michael@0: const nsAString& title, michael@0: const nsAString& aInitialColor) michael@0: { michael@0: NS_PRECONDITION(parent, michael@0: "Null parent passed to colorpicker, no color picker for you!"); michael@0: mParentWidget = WidgetUtils::DOMWindowToWidget(parent); michael@0: mInitialColor = aInitialColor; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsColorPicker::Open(nsIColorPickerShownCallback* aCallback) michael@0: { michael@0: NS_ENSURE_ARG(aCallback); michael@0: nsCOMPtr event = new AsyncColorChooser(mInitialColor, mParentWidget, aCallback); michael@0: return NS_DispatchToMainThread(event); michael@0: }