1 // Copyright (c) 2012 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 #ifndef UI_VIEWS_COLOR_CHOOSER_COLOR_CHOOSER_VIEW_H_ 6 #define UI_VIEWS_COLOR_CHOOSER_COLOR_CHOOSER_VIEW_H_ 7 8 #include "base/basictypes.h" 9 #include "base/compiler_specific.h" 10 #include "third_party/skia/include/core/SkColor.h" 11 #include "third_party/skia/include/core/SkScalar.h" 12 #include "ui/views/controls/textfield/textfield_controller.h" 13 #include "ui/views/views_export.h" 14 #include "ui/views/widget/widget_delegate.h" 15 16 namespace views { 17 18 class ColorChooserListener; 19 class Textfield; 20 21 // ColorChooserView provides the UI to choose a color by mouse and/or keyboard. 22 // It is typically used for <input type="color">. Currently the user can 23 // choose a color by dragging over the bar for hue and the area for saturation 24 // and value. 25 class VIEWS_EXPORT ColorChooserView : public WidgetDelegateView, 26 public TextfieldController { 27 public: 28 ColorChooserView(ColorChooserListener* listener, SkColor initial_color); 29 virtual ~ColorChooserView(); 30 31 // Called when its color value is changed in the web contents. 32 void OnColorChanged(SkColor color); 33 34 // Called when the user chooses a hue from the UI. 35 void OnHueChosen(SkScalar hue); 36 37 // Called when the user chooses saturation/value from the UI. 38 void OnSaturationValueChosen(SkScalar saturation, SkScalar value); 39 40 float hue() const { return hsv_[0]; } 41 float saturation() const { return hsv_[1]; } 42 float value() const { return hsv_[2]; } 43 void set_listener(ColorChooserListener* listener) { listener_ = listener; } 44 45 private: 46 class HueView; 47 class SaturationValueView; 48 class SelectedColorPatchView; 49 50 // WidgetDelegate overrides: 51 virtual View* GetInitiallyFocusedView() OVERRIDE; 52 virtual ui::ModalType GetModalType() const OVERRIDE; 53 virtual void WindowClosing() OVERRIDE; 54 virtual View* GetContentsView() OVERRIDE; 55 56 // TextfieldController overrides: 57 virtual void ContentsChanged(Textfield* sender, 58 const base::string16& new_contents) OVERRIDE; 59 virtual bool HandleKeyEvent(Textfield* sender, 60 const ui::KeyEvent& key_event) OVERRIDE; 61 62 // The current color in HSV coordinate. 63 SkScalar hsv_[3]; 64 65 // The pointer to the current color chooser for callbacks. It doesn't take 66 // ownership on |listener_| so the user of this class should take care of 67 // its lifetime. See chrome/browser/ui/browser.cc for example. 68 ColorChooserListener* listener_; 69 70 // Child views. These are owned as part of the normal views hierarchy. 71 // The view of hue chooser. 72 HueView* hue_; 73 74 // The view of saturation/value choosing area. 75 SaturationValueView* saturation_value_; 76 77 // The textfield to write the color explicitly. 78 Textfield* textfield_; 79 80 // The rectangle to denote the selected color. 81 SelectedColorPatchView* selected_color_patch_; 82 83 DISALLOW_COPY_AND_ASSIGN(ColorChooserView); 84 }; 85 86 } // namespace views 87 88 #endif // UI_VIEWS_COLOR_CHOOSER_COLOR_CHOOSER_VIEW_H_ 89