OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/color_chooser.h" |
| 6 #include "chrome/browser/ui/cocoa/color_utils.h" |
| 7 |
| 8 #if defined(ENABLE_INPUT_COLOR) |
| 9 |
| 10 #import "base/mac/cocoa_protocols.h" |
| 11 |
| 12 #import <Cocoa/Cocoa.h> |
| 13 |
| 14 // A Listener class to act as a event target for NSColorPanel and send |
| 15 // the results to the C++ class, ColorChooserImpl. |
| 16 @interface ColorPanelBridge : NSObject<NSWindowDelegate> |
| 17 |
| 18 + (ColorPanelBridge*)sharedBridge; |
| 19 |
| 20 // Called from NSColorPanel |
| 21 - (void)didChooseColor:(NSColorPanel *)panel; |
| 22 |
| 23 @end |
| 24 |
| 25 class ColorChooserImpl : public ColorChooser { |
| 26 public: |
| 27 // Get the singleton instance of this class. |
| 28 static ColorChooserImpl* GetInstance(); |
| 29 |
| 30 virtual ~ColorChooserImpl(); |
| 31 |
| 32 // Called from ColorPanelBridge |
| 33 void didChooseColor(WebKit::WebColor color); |
| 34 void cleanup(); |
| 35 |
| 36 protected: |
| 37 virtual void openImpl(Listener* listener, WebKit::WebColor initial_color); |
| 38 virtual void cleanupImpl(Listener* listener); |
| 39 void setSelectedColorImpl(WebKit::WebColor color); |
| 40 |
| 41 private: |
| 42 ColorChooser::Listener* listener_; |
| 43 }; |
| 44 |
| 45 ColorChooser* ColorChooser::GetInstance() { |
| 46 return ColorChooserImpl::GetInstance(); |
| 47 } |
| 48 |
| 49 ColorChooserImpl* ColorChooserImpl::GetInstance() { |
| 50 return Singleton<ColorChooserImpl>::get(); |
| 51 } |
| 52 |
| 53 ColorChooserImpl::~ColorChooserImpl() |
| 54 { |
| 55 cleanupImpl(listener_); |
| 56 } |
| 57 |
| 58 void ColorChooserImpl::didChooseColor(WebKit::WebColor color) { |
| 59 if (!listener_) |
| 60 return; |
| 61 listener_->DidChooseColor(color); |
| 62 } |
| 63 |
| 64 void ColorChooserImpl::openImpl(Listener* listener, |
| 65 WebKit::WebColor initial_color) { |
| 66 listener_ = listener; |
| 67 NSColorPanel* panel = [NSColorPanel sharedColorPanel]; |
| 68 [panel setColor:NSColorFromWebColor(initial_color)]; |
| 69 [panel setShowsAlpha:NO]; |
| 70 [panel setDelegate:[ColorPanelBridge sharedBridge]]; |
| 71 [panel setTarget:[ColorPanelBridge sharedBridge]]; |
| 72 [panel setAction:@selector(didChooseColor:)]; |
| 73 [panel makeKeyAndOrderFront:NULL]; |
| 74 } |
| 75 |
| 76 void ColorChooserImpl::cleanup() { |
| 77 NSColorPanel* panel = [NSColorPanel sharedColorPanel]; |
| 78 [panel setDelegate:nil]; |
| 79 [panel setTarget:nil]; |
| 80 [panel setAction:NULL]; |
| 81 if (!listener_) |
| 82 return; |
| 83 listener_->DidCleanup(); |
| 84 listener_ = 0; |
| 85 } |
| 86 |
| 87 void ColorChooserImpl::cleanupImpl(Listener* listener) { |
| 88 if (listener_ != listener) |
| 89 return; |
| 90 cleanup(); |
| 91 } |
| 92 |
| 93 void ColorChooserImpl::setSelectedColorImpl(WebKit::WebColor color) { |
| 94 NSColorPanel* panel = [NSColorPanel sharedColorPanel]; |
| 95 [panel setColor:NSColorFromWebColor(color)]; |
| 96 } |
| 97 |
| 98 @implementation ColorPanelBridge |
| 99 |
| 100 static ColorPanelBridge *sharedColorPanelBridge = nil; |
| 101 |
| 102 + (ColorPanelBridge*)sharedBridge { |
| 103 if (!sharedColorPanelBridge) |
| 104 sharedColorPanelBridge = [[ColorPanelBridge alloc] init]; |
| 105 return sharedColorPanelBridge; |
| 106 } |
| 107 |
| 108 - (void)windowWillClose:(NSNotification *)notification { |
| 109 ColorChooserImpl::GetInstance()->cleanup(); |
| 110 } |
| 111 |
| 112 - (void)didChooseColor:(NSColorPanel *)panel { |
| 113 ColorChooserImpl::GetInstance()->didChooseColor(WebColorFromNSColor( |
| 114 [[panel color] colorUsingColorSpaceName:NSDeviceRGBColorSpace])); |
| 115 } |
| 116 |
| 117 @end |
| 118 |
| 119 #endif // defined(ENABLE_INPUT_COLOR) |
OLD | NEW |