Index: chrome/browser/ui/gtk/color_chooser_gtk.cc |
diff --git a/chrome/browser/ui/gtk/color_chooser_gtk.cc b/chrome/browser/ui/gtk/color_chooser_gtk.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2b02d5d06c8bf3d4475918c54d039b3a7bf1dc91 |
--- /dev/null |
+++ b/chrome/browser/ui/gtk/color_chooser_gtk.cc |
@@ -0,0 +1,123 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <gtk/gtk.h> |
+ |
+#include "chrome/browser/ui/color_chooser.h" |
+ |
+#include "content/public/browser/web_contents.h" |
+#include "content/public/browser/web_contents_observer.h" |
+#include "grit/generated_resources.h" |
+#include "ui/base/gtk/gtk_signal.h" |
+#include "ui/base/l10n/l10n_util.h" |
+#include "ui/gfx/skia_utils_gtk.h" |
+ |
+class ColorChooserGtk : public ColorChooser, |
+ public content::WebContentsObserver { |
+ public: |
+ ColorChooserGtk( |
+ int identifier, content::WebContents* tab, SkColor initial_color); |
+ virtual ~ColorChooserGtk(); |
+ |
+ virtual void End() OVERRIDE; |
+ virtual void SetSelectedColor(SkColor color) OVERRIDE; |
+ |
+ virtual int GetIdentifier() const OVERRIDE { return identifier_; } |
+ |
+ private: |
+ virtual void WebContentsDestroyed(content::WebContents* tab) OVERRIDE; |
+ |
+ // Returns the color selection for the color_selection_dialog_. |
+ GtkColorSelection* ColorSelection(); |
+ |
+ CHROMEGTK_CALLBACK_0(ColorChooserGtk, void, OnColorChooserOk); |
+ CHROMEGTK_CALLBACK_0(ColorChooserGtk, void, OnColorChooserCancel); |
+ CHROMEGTK_CALLBACK_0(ColorChooserGtk, void, OnColorChooserDestroy); |
+ |
+ // Identifier to assure the messages are recieved by the corresponding |
+ // RendererWebColorChooserImpl. |
+ int identifier_; |
+ GtkWidget* color_selection_dialog_; |
+}; |
+ |
+ColorChooser* ColorChooser::Create( |
+ int identifier, content::WebContents* tab, SkColor initial_color) { |
+ return new ColorChooserGtk(identifier, tab, initial_color); |
+} |
+ |
+ColorChooserGtk::ColorChooserGtk( |
+ int identifier, content::WebContents* tab, SkColor initial_color) |
+ : content::WebContentsObserver(tab), |
+ identifier_(identifier), |
+ color_selection_dialog_(NULL) { |
+ if (!color_selection_dialog_) { |
+ color_selection_dialog_ = gtk_color_selection_dialog_new( |
+ l10n_util::GetStringUTF8(IDS_SELECT_COLOR_DIALOG_TITLE).c_str()); |
+ gtk_color_selection_set_has_opacity_control(ColorSelection(), FALSE); |
+ g_signal_connect(G_OBJECT( |
+ GTK_COLOR_SELECTION_DIALOG(color_selection_dialog_)->ok_button), |
+ "clicked", G_CALLBACK(OnColorChooserOkThunk), this); |
+ g_signal_connect(G_OBJECT( |
+ GTK_COLOR_SELECTION_DIALOG(color_selection_dialog_)->cancel_button), |
+ "clicked", G_CALLBACK(OnColorChooserCancelThunk), this); |
+ g_signal_connect(G_OBJECT(color_selection_dialog_), "destroy", |
+ G_CALLBACK(OnColorChooserDestroyThunk), this); |
+ } |
+ |
+ GdkColor gdk_color = gfx::SkColorToGdkColor(initial_color); |
+ gtk_color_selection_set_previous_color(ColorSelection(), &gdk_color); |
+ gtk_color_selection_set_current_color(ColorSelection(), &gdk_color); |
+ gtk_window_present(GTK_WINDOW(color_selection_dialog_)); |
+} |
+ |
+ColorChooserGtk::~ColorChooserGtk() { |
+ // Always call End() before destroying. |
+ DCHECK(!color_selection_dialog_); |
+} |
+ |
+GtkColorSelection* ColorChooserGtk::ColorSelection() { |
+ if (!color_selection_dialog_) |
+ return NULL; |
+ |
+ return GTK_COLOR_SELECTION( |
+ GTK_COLOR_SELECTION_DIALOG(color_selection_dialog_)->colorsel); |
+} |
+ |
+void ColorChooserGtk::OnColorChooserOk(GtkWidget* widget) { |
+ GdkColor color; |
+ gtk_color_selection_get_current_color(ColorSelection(), &color); |
+ web_contents()->DidChooseColorInColorChooser(identifier_, |
+ gfx::GdkColorToSkColor(color)); |
+ gtk_widget_destroy(color_selection_dialog_); |
+} |
+ |
+void ColorChooserGtk::OnColorChooserCancel(GtkWidget* widget) { |
+ gtk_widget_destroy(color_selection_dialog_); |
+} |
+ |
+void ColorChooserGtk::OnColorChooserDestroy(GtkWidget* widget) { |
+ color_selection_dialog_ = NULL; |
+ if (web_contents()) |
+ web_contents()->DidEndColorChooser(identifier_); |
+} |
+ |
+void ColorChooserGtk::End() { |
+ if (!color_selection_dialog_) |
+ return; |
+ |
+ gtk_widget_destroy(color_selection_dialog_); |
+} |
+ |
+void ColorChooserGtk::SetSelectedColor(SkColor color) { |
+ if (!color_selection_dialog_) |
+ return; |
+ |
+ GdkColor gdk_color = gfx::SkColorToGdkColor(color); |
+ gtk_color_selection_set_previous_color(ColorSelection(), &gdk_color); |
+ gtk_color_selection_set_current_color(ColorSelection(), &gdk_color); |
+} |
+ |
+void ColorChooserGtk::WebContentsDestroyed(content::WebContents* tab) { |
+ End(); |
+} |