2013-08-16 15:09:39 +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/. */
|
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
#include "nsColorPicker.h"
|
|
|
|
#include "nsCocoaUtils.h"
|
2014-03-14 00:52:14 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2013-08-16 15:09:39 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
static unsigned int HexStrToInt(NSString* str) {
|
|
|
|
unsigned int result = 0;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < [str length]; ++i) {
|
|
|
|
char c = [str characterAtIndex:i];
|
|
|
|
result *= 16;
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
result += c - '0';
|
|
|
|
} else if (c >= 'A' && c <= 'F') {
|
|
|
|
result += 10 + (c - 'A');
|
|
|
|
} else {
|
|
|
|
result += 10 + (c - 'a');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@interface NSColorPanelWrapper : NSObject <NSWindowDelegate> {
|
|
|
|
NSColorPanel* mColorPanel;
|
|
|
|
nsColorPicker* mColorPicker;
|
|
|
|
}
|
|
|
|
- (id)initWithPicker:(nsColorPicker*)aPicker;
|
|
|
|
- (void)open:(NSColor*)aInitialColor title:(NSString*)aTitle;
|
|
|
|
- (void)colorChanged:(NSColorPanel*)aPanel;
|
2020-05-19 20:27:42 +03:00
|
|
|
- (void)windowWillClose:(NSNotification*)aNotification;
|
2020-05-28 01:07:45 +03:00
|
|
|
- (void)close;
|
2013-08-16 15:09:39 +04:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSColorPanelWrapper
|
|
|
|
- (id)initWithPicker:(nsColorPicker*)aPicker {
|
|
|
|
mColorPicker = aPicker;
|
|
|
|
mColorPanel = [NSColorPanel sharedColorPanel];
|
|
|
|
|
|
|
|
self = [super init];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)open:(NSColor*)aInitialColor title:(NSString*)aTitle {
|
|
|
|
[mColorPanel setTarget:self];
|
|
|
|
[mColorPanel setAction:@selector(colorChanged:)];
|
|
|
|
[mColorPanel setDelegate:self];
|
2017-08-22 02:16:22 +03:00
|
|
|
[mColorPanel setTitle:aTitle];
|
|
|
|
[mColorPanel setColor:aInitialColor];
|
2013-08-16 15:09:39 +04:00
|
|
|
[mColorPanel makeKeyAndOrderFront:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)colorChanged:(NSColorPanel*)aPanel {
|
2020-05-28 01:07:45 +03:00
|
|
|
if (!mColorPicker) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-16 15:09:39 +04:00
|
|
|
mColorPicker->Update([mColorPanel color]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowWillClose:(NSNotification*)aNotification {
|
2020-05-28 01:07:45 +03:00
|
|
|
if (!mColorPicker) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-16 15:09:39 +04:00
|
|
|
mColorPicker->Done();
|
|
|
|
}
|
|
|
|
|
2020-05-28 01:07:45 +03:00
|
|
|
- (void)close {
|
2017-08-22 02:16:22 +03:00
|
|
|
[mColorPanel setTarget:nil];
|
|
|
|
[mColorPanel setAction:nil];
|
|
|
|
[mColorPanel setDelegate:nil];
|
2013-08-16 15:09:39 +04:00
|
|
|
|
|
|
|
mColorPanel = nil;
|
|
|
|
mColorPicker = nullptr;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsColorPicker, nsIColorPicker)
|
2013-08-16 15:09:39 +04:00
|
|
|
|
2020-05-28 01:07:45 +03:00
|
|
|
nsColorPicker::~nsColorPicker() {
|
|
|
|
if (mColorPanelWrapper) {
|
|
|
|
[mColorPanelWrapper close];
|
|
|
|
[mColorPanelWrapper release];
|
|
|
|
mColorPanelWrapper = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2014-07-06 19:25:31 +04:00
|
|
|
|
2013-08-16 15:09:39 +04:00
|
|
|
NS_IMETHODIMP
|
2016-01-30 20:05:36 +03:00
|
|
|
nsColorPicker::Init(mozIDOMWindowProxy* aParent, const nsAString& aTitle,
|
2013-08-16 15:09:39 +04:00
|
|
|
const nsAString& aInitialColor) {
|
2014-03-14 00:52:14 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Color pickers can only be opened from main thread currently");
|
2013-08-16 15:09:39 +04:00
|
|
|
mTitle = aTitle;
|
|
|
|
mColor = aInitialColor;
|
2020-05-19 20:27:42 +03:00
|
|
|
mColorPanelWrapper = [[NSColorPanelWrapper alloc] initWithPicker:this];
|
2013-08-16 15:09:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ NSColor* nsColorPicker::GetNSColorFromHexString(const nsAString& aColor) {
|
|
|
|
NSString* str = nsCocoaUtils::ToNSString(aColor);
|
|
|
|
|
|
|
|
double red = HexStrToInt([str substringWithRange:NSMakeRange(1, 2)]) / 255.0;
|
|
|
|
double green = HexStrToInt([str substringWithRange:NSMakeRange(3, 2)]) / 255.0;
|
|
|
|
double blue = HexStrToInt([str substringWithRange:NSMakeRange(5, 2)]) / 255.0;
|
|
|
|
|
|
|
|
return [NSColor colorWithDeviceRed:red green:green blue:blue alpha:1.0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void nsColorPicker::GetHexStringFromNSColor(NSColor* aColor, nsAString& aResult) {
|
|
|
|
CGFloat redFloat, greenFloat, blueFloat;
|
2016-08-20 06:17:58 +03:00
|
|
|
|
|
|
|
NSColor* color = aColor;
|
|
|
|
@try {
|
|
|
|
[color getRed:&redFloat green:&greenFloat blue:&blueFloat alpha:nil];
|
|
|
|
} @catch (NSException* e) {
|
|
|
|
color = [color colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]];
|
|
|
|
[color getRed:&redFloat green:&greenFloat blue:&blueFloat alpha:nil];
|
|
|
|
}
|
2013-08-16 15:09:39 +04:00
|
|
|
|
|
|
|
nsCocoaUtils::GetStringForNSString(
|
|
|
|
[NSString stringWithFormat:@"#%02x%02x%02x", (int)(redFloat * 255), (int)(greenFloat * 255),
|
|
|
|
(int)(blueFloat * 255)],
|
|
|
|
aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsColorPicker::Open(nsIColorPickerShownCallback* aCallback) {
|
|
|
|
MOZ_ASSERT(aCallback);
|
|
|
|
mCallback = aCallback;
|
|
|
|
|
2020-05-19 20:27:42 +03:00
|
|
|
[mColorPanelWrapper open:GetNSColorFromHexString(mColor) title:nsCocoaUtils::ToNSString(mTitle)];
|
2013-08-16 15:09:39 +04:00
|
|
|
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsColorPicker::Update(NSColor* aColor) {
|
|
|
|
GetHexStringFromNSColor(aColor, mColor);
|
|
|
|
mCallback->Update(mColor);
|
|
|
|
}
|
|
|
|
|
2020-05-19 20:27:42 +03:00
|
|
|
void nsColorPicker::Done() {
|
2020-05-28 01:07:45 +03:00
|
|
|
[mColorPanelWrapper close];
|
2020-05-19 20:27:42 +03:00
|
|
|
[mColorPanelWrapper release];
|
|
|
|
mColorPanelWrapper = nullptr;
|
2020-09-23 18:17:15 +03:00
|
|
|
mCallback->Done(u""_ns);
|
2013-08-16 15:09:39 +04:00
|
|
|
mCallback = nullptr;
|
2014-03-13 22:48:04 +04:00
|
|
|
NS_RELEASE_THIS();
|
2014-03-13 19:04:57 +04:00
|
|
|
}
|