Bug 875756 - Implement nsIColorPicker for Cocoa. r=mstange

This commit is contained in:
Mounir Lamouri 2013-08-16 12:09:39 +01:00
parent 05d99e99a0
commit 36583db62a
4 changed files with 205 additions and 0 deletions

View File

@ -53,6 +53,7 @@ CMMSRCS = \
GfxInfo.mm \
WidgetTraceEvent.mm \
NativeKeyBindings.mm \
nsColorPicker.mm \
$(NULL)
ifeq (x86_64,$(TARGET_CPU))

View File

@ -0,0 +1,42 @@
/* -*- 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/. */
#ifndef nsColorPicker_h_
#define nsColorPicker_h_
#include "nsIColorPicker.h"
#include "nsString.h"
#include "nsCOMPtr.h"
class nsIColorPickerShownCallback;
class nsIDOMWindow;
@class NSColorPanelWrapper;
@class NSColor;
class nsColorPicker MOZ_FINAL : public nsIColorPicker
{
public:
NS_DECL_ISUPPORTS
NS_IMETHOD Init(nsIDOMWindow* aParent, const nsAString& aTitle,
const nsAString& aInitialColor);
NS_IMETHOD Open(nsIColorPickerShownCallback* aCallback);
// For NSColorPanelWrapper.
void Update(NSColor* aColor);
void Done();
private:
static NSColor* GetNSColorFromHexString(const nsAString& aColor);
static void GetHexStringFromNSColor(NSColor* aColor, nsAString& aResult);
nsString mTitle;
nsString mColor;
NSColorPanelWrapper* mColorPanel;
nsCOMPtr<nsIColorPickerShownCallback> mCallback;
};
#endif // nsColorPicker_h_

View File

@ -0,0 +1,157 @@
/* -*- 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"
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;
@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 setTitle:aTitle];
[mColorPanel setColor:aInitialColor];
[mColorPanel setTarget:self];
[mColorPanel setAction:@selector(colorChanged:)];
[mColorPanel setDelegate:self];
[mColorPanel makeKeyAndOrderFront:nil];
}
- (void)colorChanged:(NSColorPanel*)aPanel
{
mColorPicker->Update([mColorPanel color]);
}
- (void)windowWillClose:(NSNotification*)aNotification
{
mColorPicker->Done();
}
- (void)dealloc
{
if ([mColorPanel delegate] == self) {
[mColorPanel setTarget:nil];
[mColorPanel setAction:nil];
[mColorPanel setDelegate:nil];
}
mColorPanel = nil;
mColorPicker = nullptr;
[super dealloc];
}
@end
NS_IMPL_ISUPPORTS1(nsColorPicker, nsIColorPicker)
NS_IMETHODIMP
nsColorPicker::Init(nsIDOMWindow* aParent, const nsAString& aTitle,
const nsAString& aInitialColor)
{
mTitle = aTitle;
mColor = aInitialColor;
mColorPanel = [[NSColorPanelWrapper alloc] initWithPicker:this];
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;
[aColor getRed: &redFloat green: &greenFloat blue: &blueFloat alpha: nil];
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;
[mColorPanel open:GetNSColorFromHexString(mColor)
title:nsCocoaUtils::ToNSString(mTitle)];
NS_ADDREF_THIS();
return NS_OK;
}
void
nsColorPicker::Update(NSColor* aColor)
{
GetHexStringFromNSColor(aColor, mColor);
mCallback->Update(mColor);
}
void
nsColorPicker::Done()
{
mCallback->Done(EmptyString());
mCallback = nullptr;
[mColorPanel release];
NS_RELEASE_THIS();
}

View File

@ -15,6 +15,7 @@
#include "nsAppShell.h"
#include "nsAppShellSingleton.h"
#include "nsFilePicker.h"
#include "nsColorPicker.h"
#include "nsClipboard.h"
#include "nsClipboardHelper.h"
@ -37,6 +38,7 @@
NS_GENERIC_FACTORY_CONSTRUCTOR(nsCocoaWindow)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsChildView)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsFilePicker)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsColorPicker)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsSound)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsTransferable)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsHTMLFormatConverter)
@ -133,6 +135,7 @@ NS_DEFINE_NAMED_CID(NS_WINDOW_CID);
NS_DEFINE_NAMED_CID(NS_POPUP_CID);
NS_DEFINE_NAMED_CID(NS_CHILD_CID);
NS_DEFINE_NAMED_CID(NS_FILEPICKER_CID);
NS_DEFINE_NAMED_CID(NS_COLORPICKER_CID);
NS_DEFINE_NAMED_CID(NS_APPSHELL_CID);
NS_DEFINE_NAMED_CID(NS_SOUND_CID);
NS_DEFINE_NAMED_CID(NS_TRANSFERABLE_CID);
@ -163,6 +166,7 @@ static const mozilla::Module::CIDEntry kWidgetCIDs[] = {
{ &kNS_POPUP_CID, false, NULL, nsCocoaWindowConstructor },
{ &kNS_CHILD_CID, false, NULL, nsChildViewConstructor },
{ &kNS_FILEPICKER_CID, false, NULL, nsFilePickerConstructor },
{ &kNS_COLORPICKER_CID, false, NULL, nsColorPickerConstructor },
{ &kNS_APPSHELL_CID, false, NULL, nsAppShellConstructor },
{ &kNS_SOUND_CID, false, NULL, nsSoundConstructor },
{ &kNS_TRANSFERABLE_CID, false, NULL, nsTransferableConstructor },
@ -197,6 +201,7 @@ static const mozilla::Module::ContractIDEntry kWidgetContracts[] = {
{ "@mozilla.org/widgets/popup/mac;1", &kNS_POPUP_CID },
{ "@mozilla.org/widgets/childwindow/mac;1", &kNS_CHILD_CID },
{ "@mozilla.org/filepicker;1", &kNS_FILEPICKER_CID },
{ "@mozilla.org/colorpicker;1", &kNS_COLORPICKER_CID },
{ "@mozilla.org/widget/appshell/mac;1", &kNS_APPSHELL_CID },
{ "@mozilla.org/sound;1", &kNS_SOUND_CID },
{ "@mozilla.org/widget/transferable;1", &kNS_TRANSFERABLE_CID },