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 <gtk/gtk.h> |
| 6 |
| 7 #include "chrome/browser/ui/color_chooser.h" |
| 8 |
| 9 #include "grit/generated_resources.h" |
| 10 #include "ui/base/gtk/gtk_signal.h" |
| 11 #include "ui/base/l10n/l10n_util.h" |
| 12 |
| 13 #if defined(ENABLE_INPUT_COLOR) |
| 14 |
| 15 GdkColor GdkColorFromWebColor(WebKit::WebColor color) { |
| 16 uint8 r = (color >> 16) & 0xFF; |
| 17 uint8 g = (color >> 8) & 0xFF; |
| 18 uint8 b = (color >> 0) & 0xFF; |
| 19 GdkColor gdk_color = {1, r * 257, g * 257, b * 257}; |
| 20 return gdk_color; |
| 21 } |
| 22 |
| 23 WebKit::WebColor WebColorFromGdkColor(GdkColor color) { |
| 24 uint8 r = color.red / 257; |
| 25 uint8 g = color.green / 257; |
| 26 uint8 b = color.blue / 257; |
| 27 return ((255 << 24) | (r << 16) | (g << 8) | b); |
| 28 } |
| 29 |
| 30 class ColorChooserImpl : public ColorChooser { |
| 31 public: |
| 32 // Get the single instance of this class. |
| 33 static ColorChooserImpl* GetInstance(); |
| 34 |
| 35 protected: |
| 36 virtual void openImpl(Listener* listener, WebKit::WebColor initial_color); |
| 37 virtual void cleanupImpl(Listener* listener); |
| 38 virtual void setSelectedColorImpl(WebKit::WebColor color); |
| 39 |
| 40 private: |
| 41 GtkColorSelection* colorSelection(); |
| 42 |
| 43 CHROMEGTK_CALLBACK_0(ColorChooserImpl, void, OnColorChooserOk); |
| 44 CHROMEGTK_CALLBACK_0(ColorChooserImpl, void, OnColorChooserCancel); |
| 45 CHROMEGTK_CALLBACK_0(ColorChooserImpl, void, OnColorChooserDestroy); |
| 46 |
| 47 ColorChooser::Listener* listener_; |
| 48 GtkWidget* color_selecting_dialog_; |
| 49 }; |
| 50 |
| 51 ColorChooser* ColorChooser::GetInstance() { |
| 52 return ColorChooserImpl::GetInstance(); |
| 53 } |
| 54 |
| 55 ColorChooserImpl* ColorChooserImpl::GetInstance() { |
| 56 return Singleton<ColorChooserImpl>::get(); |
| 57 } |
| 58 |
| 59 GtkColorSelection* ColorChooserImpl::colorSelection() { |
| 60 if (!color_selecting_dialog_) |
| 61 return 0; |
| 62 return GTK_COLOR_SELECTION( |
| 63 GTK_COLOR_SELECTION_DIALOG(color_selecting_dialog_)->colorsel); |
| 64 } |
| 65 |
| 66 void ColorChooserImpl::OnColorChooserOk(GtkWidget* widget) { |
| 67 GdkColor color; |
| 68 gtk_color_selection_get_current_color(colorSelection(), &color); |
| 69 listener_->DidChooseColor(WebColorFromGdkColor(color)); |
| 70 |
| 71 gtk_widget_destroy(color_selecting_dialog_); |
| 72 listener_ = 0; |
| 73 color_selecting_dialog_ = 0; |
| 74 } |
| 75 |
| 76 void ColorChooserImpl::OnColorChooserCancel(GtkWidget* widget) { |
| 77 gtk_widget_destroy(color_selecting_dialog_); |
| 78 listener_ = 0; |
| 79 color_selecting_dialog_ = 0; |
| 80 } |
| 81 |
| 82 void ColorChooserImpl::OnColorChooserDestroy(GtkWidget* widget) { |
| 83 listener_ = 0; |
| 84 color_selecting_dialog_ = 0; |
| 85 } |
| 86 |
| 87 void ColorChooserImpl::openImpl(Listener* listener, |
| 88 WebKit::WebColor initial_color) { |
| 89 listener_ = listener; |
| 90 if (!color_selecting_dialog_) { |
| 91 color_selecting_dialog_ = gtk_color_selection_dialog_new( |
| 92 l10n_util::GetStringUTF8(IDS_SELECT_COLOR_DIALOG_TITLE).c_str()); |
| 93 gtk_color_selection_set_has_opacity_control(colorSelection(), FALSE); |
| 94 g_signal_connect(G_OBJECT( |
| 95 GTK_COLOR_SELECTION_DIALOG(color_selecting_dialog_)->ok_button), |
| 96 "clicked", G_CALLBACK(OnColorChooserOkThunk), this); |
| 97 g_signal_connect(G_OBJECT( |
| 98 GTK_COLOR_SELECTION_DIALOG(color_selecting_dialog_)->cancel_button), |
| 99 "clicked", G_CALLBACK(OnColorChooserCancelThunk), this); |
| 100 g_signal_connect(G_OBJECT(color_selecting_dialog_), "destroy", |
| 101 G_CALLBACK(OnColorChooserDestroyThunk), this); |
| 102 } |
| 103 GdkColor gdk_color = GdkColorFromWebColor(initial_color); |
| 104 gtk_color_selection_set_previous_color(colorSelection(), &gdk_color); |
| 105 gtk_color_selection_set_current_color(colorSelection(), &gdk_color); |
| 106 if (color_selecting_dialog_) |
| 107 gtk_window_present(GTK_WINDOW(color_selecting_dialog_)); |
| 108 else |
| 109 gtk_widget_show(color_selecting_dialog_); |
| 110 } |
| 111 |
| 112 void ColorChooserImpl::cleanupImpl(Listener* listener) { |
| 113 if (!color_selecting_dialog_) |
| 114 return; |
| 115 |
| 116 if (listener_ != listener) |
| 117 return; |
| 118 |
| 119 gtk_widget_destroy(color_selecting_dialog_); |
| 120 listener_ = 0; |
| 121 color_selecting_dialog_ = 0; |
| 122 } |
| 123 |
| 124 void ColorChooserImpl::setSelectedColorImpl(WebKit::WebColor color) { |
| 125 if (!color_selecting_dialog_) |
| 126 return; |
| 127 GdkColor gdk_color = GdkColorFromWebColor(color); |
| 128 gtk_color_selection_set_previous_color(colorSelection(), &gdk_color); |
| 129 gtk_color_selection_set_current_color(colorSelection(), &gdk_color); |
| 130 } |
| 131 |
| 132 #endif // defined(ENABLE_INPUT_COLOR) |
| 133 |
OLD | NEW |