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..add8ff5e8509b45e726cd89bf80957155ac8a61e |
--- /dev/null |
+++ b/chrome/browser/ui/gtk/color_chooser_gtk.cc |
@@ -0,0 +1,131 @@ |
+// 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/browser/renderer_host/render_view_host.h" |
+#include "content/public/browser/render_view_host_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::RenderViewHostObserver { |
+ public: |
+ ColorChooserGtk( |
+ int identifier, RenderViewHost* rvh, SkColor initial_color); |
+ virtual ~ColorChooserGtk(); |
+ |
+ virtual void End() OVERRIDE; |
+ virtual void SetSelectedColor(SkColor color) OVERRIDE; |
+ |
+ virtual RenderViewHost* GetRenderViewHost() const OVERRIDE; |
+ virtual int GetIdentifier() const OVERRIDE { return identifier_; } |
+ |
+ private: |
+ virtual void RenderViewHostDestroyed( |
+ RenderViewHost* render_view_host) 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, RenderViewHost* rvh, SkColor initial_color) { |
+ return new ColorChooserGtk(identifier, rvh, initial_color); |
+} |
+ |
+ColorChooserGtk::ColorChooserGtk( |
+ int identifier, RenderViewHost* rvh, SkColor initial_color) |
+ : content::RenderViewHostObserver(rvh), |
+ 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); |
+ render_view_host()->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 (render_view_host()) |
+ render_view_host()->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); |
+} |
+ |
+RenderViewHost* ColorChooserGtk::GetRenderViewHost() const { |
+ return content::RenderViewHostObserver::render_view_host(); |
+} |
+ |
+void ColorChooserGtk::RenderViewHostDestroyed( |
+ RenderViewHost* render_view_host) { |
+ if (color_selection_dialog_) |
+ gtk_widget_destroy(color_selection_dialog_); |
+} |