Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsColorPicker.h" |
michael@0 | 8 | |
michael@0 | 9 | #include <shlwapi.h> |
michael@0 | 10 | |
michael@0 | 11 | #include "mozilla/AutoRestore.h" |
michael@0 | 12 | #include "nsIWidget.h" |
michael@0 | 13 | #include "WidgetUtils.h" |
michael@0 | 14 | |
michael@0 | 15 | using namespace mozilla::widget; |
michael@0 | 16 | |
michael@0 | 17 | namespace |
michael@0 | 18 | { |
michael@0 | 19 | // Manages NS_NATIVE_TMP_WINDOW child windows. NS_NATIVE_TMP_WINDOWs are |
michael@0 | 20 | // temporary child windows of mParentWidget created to address RTL issues |
michael@0 | 21 | // in picker dialogs. We are responsible for destroying these. |
michael@0 | 22 | class AutoDestroyTmpWindow |
michael@0 | 23 | { |
michael@0 | 24 | public: |
michael@0 | 25 | explicit AutoDestroyTmpWindow(HWND aTmpWnd) : |
michael@0 | 26 | mWnd(aTmpWnd) { |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | ~AutoDestroyTmpWindow() { |
michael@0 | 30 | if (mWnd) |
michael@0 | 31 | DestroyWindow(mWnd); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | inline HWND get() const { return mWnd; } |
michael@0 | 35 | private: |
michael@0 | 36 | HWND mWnd; |
michael@0 | 37 | }; |
michael@0 | 38 | |
michael@0 | 39 | static DWORD ColorStringToRGB(const nsAString& aColor) |
michael@0 | 40 | { |
michael@0 | 41 | DWORD result = 0; |
michael@0 | 42 | |
michael@0 | 43 | for (uint32_t i = 1; i < aColor.Length(); ++i) { |
michael@0 | 44 | result *= 16; |
michael@0 | 45 | |
michael@0 | 46 | char16_t c = aColor[i]; |
michael@0 | 47 | if (c >= '0' && c <= '9') { |
michael@0 | 48 | result += c - '0'; |
michael@0 | 49 | } else if (c >= 'a' && c <= 'f') { |
michael@0 | 50 | result += 10 + (c - 'a'); |
michael@0 | 51 | } else { |
michael@0 | 52 | result += 10 + (c - 'A'); |
michael@0 | 53 | } |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | DWORD r = result & 0x00FF0000; |
michael@0 | 57 | DWORD g = result & 0x0000FF00; |
michael@0 | 58 | DWORD b = result & 0x000000FF; |
michael@0 | 59 | |
michael@0 | 60 | r = r >> 16; |
michael@0 | 61 | b = b << 16; |
michael@0 | 62 | |
michael@0 | 63 | result = r | g | b; |
michael@0 | 64 | |
michael@0 | 65 | return result; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | static nsString ToHexString(BYTE n) |
michael@0 | 69 | { |
michael@0 | 70 | nsString result; |
michael@0 | 71 | if (n <= 0x0F) { |
michael@0 | 72 | result.Append('0'); |
michael@0 | 73 | } |
michael@0 | 74 | result.AppendInt(n, 16); |
michael@0 | 75 | return result; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | |
michael@0 | 79 | static void |
michael@0 | 80 | BGRIntToRGBString(DWORD color, nsAString& aResult) |
michael@0 | 81 | { |
michael@0 | 82 | BYTE r = GetRValue(color); |
michael@0 | 83 | BYTE g = GetGValue(color); |
michael@0 | 84 | BYTE b = GetBValue(color); |
michael@0 | 85 | |
michael@0 | 86 | aResult.AssignLiteral("#"); |
michael@0 | 87 | aResult.Append(ToHexString(r)); |
michael@0 | 88 | aResult.Append(ToHexString(g)); |
michael@0 | 89 | aResult.Append(ToHexString(b)); |
michael@0 | 90 | } |
michael@0 | 91 | } // anonymous namespace |
michael@0 | 92 | |
michael@0 | 93 | AsyncColorChooser::AsyncColorChooser(const nsAString& aInitialColor, |
michael@0 | 94 | nsIWidget* aParentWidget, |
michael@0 | 95 | nsIColorPickerShownCallback* aCallback) |
michael@0 | 96 | : mInitialColor(aInitialColor) |
michael@0 | 97 | , mParentWidget(aParentWidget) |
michael@0 | 98 | , mCallback(aCallback) |
michael@0 | 99 | { |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | NS_IMETHODIMP |
michael@0 | 103 | AsyncColorChooser::Run() |
michael@0 | 104 | { |
michael@0 | 105 | static COLORREF sCustomColors[16] = {0} ; |
michael@0 | 106 | |
michael@0 | 107 | MOZ_ASSERT(NS_IsMainThread(), |
michael@0 | 108 | "Color pickers can only be opened from main thread currently"); |
michael@0 | 109 | |
michael@0 | 110 | static bool sColorPickerOpen = false; |
michael@0 | 111 | // Allow only one color picker to be opened at a time, to workaround bug 944737 |
michael@0 | 112 | if (!sColorPickerOpen) { |
michael@0 | 113 | mozilla::AutoRestore<bool> autoRestoreColorPickerOpen(sColorPickerOpen); |
michael@0 | 114 | sColorPickerOpen = true; |
michael@0 | 115 | |
michael@0 | 116 | AutoDestroyTmpWindow adtw((HWND) (mParentWidget.get() ? |
michael@0 | 117 | mParentWidget->GetNativeData(NS_NATIVE_TMP_WINDOW) : nullptr)); |
michael@0 | 118 | |
michael@0 | 119 | CHOOSECOLOR options; |
michael@0 | 120 | options.lStructSize = sizeof(options); |
michael@0 | 121 | options.hwndOwner = adtw.get(); |
michael@0 | 122 | options.Flags = CC_RGBINIT | CC_FULLOPEN; |
michael@0 | 123 | options.rgbResult = ColorStringToRGB(mInitialColor); |
michael@0 | 124 | options.lpCustColors = sCustomColors; |
michael@0 | 125 | |
michael@0 | 126 | if (ChooseColor(&options)) { |
michael@0 | 127 | BGRIntToRGBString(options.rgbResult, mColor); |
michael@0 | 128 | } |
michael@0 | 129 | } else { |
michael@0 | 130 | NS_WARNING("Currently, it's not possible to open more than one color " |
michael@0 | 131 | "picker at a time"); |
michael@0 | 132 | mColor = mInitialColor; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | if (mCallback) { |
michael@0 | 136 | mCallback->Done(mColor); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | return NS_OK; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 143 | // nsIColorPicker |
michael@0 | 144 | |
michael@0 | 145 | nsColorPicker::nsColorPicker() |
michael@0 | 146 | { |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | nsColorPicker::~nsColorPicker() |
michael@0 | 150 | { |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | NS_IMPL_ISUPPORTS(nsColorPicker, nsIColorPicker) |
michael@0 | 154 | |
michael@0 | 155 | NS_IMETHODIMP |
michael@0 | 156 | nsColorPicker::Init(nsIDOMWindow* parent, |
michael@0 | 157 | const nsAString& title, |
michael@0 | 158 | const nsAString& aInitialColor) |
michael@0 | 159 | { |
michael@0 | 160 | NS_PRECONDITION(parent, |
michael@0 | 161 | "Null parent passed to colorpicker, no color picker for you!"); |
michael@0 | 162 | mParentWidget = WidgetUtils::DOMWindowToWidget(parent); |
michael@0 | 163 | mInitialColor = aInitialColor; |
michael@0 | 164 | return NS_OK; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | NS_IMETHODIMP |
michael@0 | 168 | nsColorPicker::Open(nsIColorPickerShownCallback* aCallback) |
michael@0 | 169 | { |
michael@0 | 170 | NS_ENSURE_ARG(aCallback); |
michael@0 | 171 | nsCOMPtr<nsIRunnable> event = new AsyncColorChooser(mInitialColor, mParentWidget, aCallback); |
michael@0 | 172 | return NS_DispatchToMainThread(event); |
michael@0 | 173 | } |