2013-06-23 20:48:24 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
#include "nsColorPicker.h"
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
#include <shlwapi.h>
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2014-01-11 21:46:20 +04:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2013-06-23 20:48:24 +04:00
|
|
|
#include "nsIWidget.h"
|
2014-08-25 23:17:41 +04:00
|
|
|
#include "nsString.h"
|
2013-06-23 20:48:24 +04:00
|
|
|
#include "WidgetUtils.h"
|
2017-10-06 17:01:22 +03:00
|
|
|
#include "nsPIDOMWindow.h"
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
using namespace mozilla::widget;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
namespace {
|
|
|
|
// Manages NS_NATIVE_TMP_WINDOW child windows. NS_NATIVE_TMP_WINDOWs are
|
|
|
|
// temporary child windows of mParentWidget created to address RTL issues
|
|
|
|
// in picker dialogs. We are responsible for destroying these.
|
|
|
|
class AutoDestroyTmpWindow {
|
|
|
|
public:
|
|
|
|
explicit AutoDestroyTmpWindow(HWND aTmpWnd) : mWnd(aTmpWnd) {}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
~AutoDestroyTmpWindow() {
|
|
|
|
if (mWnd) DestroyWindow(mWnd);
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
inline HWND get() const { return mWnd; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
private:
|
|
|
|
HWND mWnd;
|
|
|
|
};
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
static DWORD ColorStringToRGB(const nsAString& aColor) {
|
|
|
|
DWORD result = 0;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
for (uint32_t i = 1; i < aColor.Length(); ++i) {
|
|
|
|
result *= 16;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t c = aColor[i];
|
2013-06-23 20:48:24 +04:00
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
result += c - '0';
|
|
|
|
} else if (c >= 'a' && c <= 'f') {
|
|
|
|
result += 10 + (c - 'a');
|
|
|
|
} else {
|
|
|
|
result += 10 + (c - 'A');
|
|
|
|
}
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
DWORD r = result & 0x00FF0000;
|
|
|
|
DWORD g = result & 0x0000FF00;
|
|
|
|
DWORD b = result & 0x000000FF;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
r = r >> 16;
|
|
|
|
b = b << 16;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
result = r | g | b;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
return result;
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
static nsString ToHexString(BYTE n) {
|
|
|
|
nsString result;
|
|
|
|
if (n <= 0x0F) {
|
|
|
|
result.Append('0');
|
|
|
|
}
|
|
|
|
result.AppendInt(n, 16);
|
|
|
|
return result;
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
static void BGRIntToRGBString(DWORD color, nsAString& aResult) {
|
|
|
|
BYTE r = GetRValue(color);
|
|
|
|
BYTE g = GetGValue(color);
|
|
|
|
BYTE b = GetBValue(color);
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2014-05-26 22:55:08 +04:00
|
|
|
aResult.Assign('#');
|
2013-06-23 20:48:24 +04:00
|
|
|
aResult.Append(ToHexString(r));
|
|
|
|
aResult.Append(ToHexString(g));
|
|
|
|
aResult.Append(ToHexString(b));
|
|
|
|
}
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2014-08-25 23:17:45 +04:00
|
|
|
static AsyncColorChooser* gColorChooser;
|
|
|
|
|
2014-08-25 23:17:41 +04:00
|
|
|
AsyncColorChooser::AsyncColorChooser(COLORREF aInitialColor,
|
2014-01-11 21:46:20 +04:00
|
|
|
nsIWidget* aParentWidget,
|
|
|
|
nsIColorPickerShownCallback* aCallback)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("AsyncColorChooser"),
|
|
|
|
mInitialColor(aInitialColor),
|
2014-08-25 23:17:45 +04:00
|
|
|
mColor(aInitialColor),
|
2013-06-23 20:48:24 +04:00
|
|
|
mParentWidget(aParentWidget),
|
|
|
|
mCallback(aCallback) {}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
AsyncColorChooser::Run() {
|
2014-01-11 21:46:20 +04:00
|
|
|
static COLORREF sCustomColors[16] = {0};
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2014-01-11 21:46:20 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
|
|
|
"Color pickers can only be opened from main thread currently");
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2014-01-11 21:46:20 +04:00
|
|
|
// Allow only one color picker to be opened at a time, to workaround bug
|
|
|
|
// 944737
|
2014-08-25 23:17:45 +04:00
|
|
|
if (!gColorChooser) {
|
|
|
|
mozilla::AutoRestore<AsyncColorChooser*> restoreColorChooser(gColorChooser);
|
|
|
|
gColorChooser = this;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2014-01-11 21:46:20 +04:00
|
|
|
AutoDestroyTmpWindow adtw((HWND)(
|
|
|
|
mParentWidget.get() ? mParentWidget->GetNativeData(NS_NATIVE_TMP_WINDOW)
|
|
|
|
: nullptr));
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2014-01-11 21:46:20 +04:00
|
|
|
CHOOSECOLOR options;
|
|
|
|
options.lStructSize = sizeof(options);
|
|
|
|
options.hwndOwner = adtw.get();
|
2014-08-25 23:17:45 +04:00
|
|
|
options.Flags = CC_RGBINIT | CC_FULLOPEN | CC_ENABLEHOOK;
|
2014-08-25 23:17:41 +04:00
|
|
|
options.rgbResult = mInitialColor;
|
2014-01-11 21:46:20 +04:00
|
|
|
options.lpCustColors = sCustomColors;
|
2014-08-25 23:17:45 +04:00
|
|
|
options.lpfnHook = HookProc;
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2014-08-25 23:17:45 +04:00
|
|
|
mColor = ChooseColor(&options) ? options.rgbResult : mInitialColor;
|
2014-01-11 21:46:20 +04:00
|
|
|
} else {
|
|
|
|
NS_WARNING(
|
|
|
|
"Currently, it's not possible to open more than one color "
|
|
|
|
"picker at a time");
|
|
|
|
mColor = mInitialColor;
|
2013-06-23 20:48:24 +04:00
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
if (mCallback) {
|
2014-08-25 23:17:41 +04:00
|
|
|
nsAutoString colorStr;
|
|
|
|
BGRIntToRGBString(mColor, colorStr);
|
|
|
|
mCallback->Done(colorStr);
|
2013-06-23 20:48:24 +04:00
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2014-08-25 23:17:45 +04:00
|
|
|
void AsyncColorChooser::Update(COLORREF aColor) {
|
|
|
|
if (mColor != aColor) {
|
|
|
|
mColor = aColor;
|
|
|
|
|
|
|
|
nsAutoString colorStr;
|
|
|
|
BGRIntToRGBString(mColor, colorStr);
|
|
|
|
mCallback->Update(colorStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ UINT_PTR CALLBACK AsyncColorChooser::HookProc(HWND aDialog,
|
|
|
|
UINT aMsg,
|
|
|
|
WPARAM aWParam,
|
|
|
|
LPARAM aLParam) {
|
|
|
|
if (!gColorChooser) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aMsg == WM_CTLCOLORSTATIC) {
|
|
|
|
// The color picker does not expose a proper way to retrieve the current
|
|
|
|
// color, so we need to obtain it from the static control displaying the
|
|
|
|
// current color instead.
|
|
|
|
const int kCurrentColorBoxID = 709;
|
|
|
|
if ((HWND)aLParam == GetDlgItem(aDialog, kCurrentColorBoxID)) {
|
|
|
|
gColorChooser->Update(GetPixel((HDC)aWParam, 0, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIColorPicker
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
nsColorPicker::nsColorPicker() {}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
nsColorPicker::~nsColorPicker() {}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsColorPicker, nsIColorPicker)
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
NS_IMETHODIMP
|
2016-01-30 20:05:36 +03:00
|
|
|
nsColorPicker::Init(mozIDOMWindowProxy* parent, const nsAString& title,
|
2014-01-11 21:46:20 +04:00
|
|
|
const nsAString& aInitialColor) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(parent,
|
2013-06-23 20:48:24 +04:00
|
|
|
"Null parent passed to colorpicker, no color picker for you!");
|
2016-01-30 20:05:36 +03:00
|
|
|
mParentWidget =
|
|
|
|
WidgetUtils::DOMWindowToWidget(nsPIDOMWindowOuter::From(parent));
|
2014-08-25 23:17:41 +04:00
|
|
|
mInitialColor = ColorStringToRGB(aInitialColor);
|
2013-06-23 20:48:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-06-19 04:56:02 +04:00
|
|
|
|
2013-06-23 20:48:24 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsColorPicker::Open(nsIColorPickerShownCallback* aCallback) {
|
|
|
|
NS_ENSURE_ARG(aCallback);
|
2014-08-25 23:17:41 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
new AsyncColorChooser(mInitialColor, mParentWidget, aCallback);
|
2013-06-23 20:48:24 +04:00
|
|
|
return NS_DispatchToMainThread(event);
|
|
|
|
}
|