1 // Copyright 2013 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.
5 #include "chrome/browser/ui/screen_capture_notification_ui.h"
10 #include "base/compiler_specific.h"
11 #include "base/logging.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "grit/generated_resources.h"
15 #include "ui/base/gtk/gtk_signal.h"
16 #include "ui/base/l10n/l10n_util.h"
18 class ScreenCaptureNotificationUIGtk : public ScreenCaptureNotificationUI {
20 explicit ScreenCaptureNotificationUIGtk(const base::string16& text);
21 virtual ~ScreenCaptureNotificationUIGtk();
23 // ScreenCaptureNotificationUI interface
24 virtual gfx::NativeViewId OnStarted(const base::Closure& stop_callback)
28 CHROMEGTK_CALLBACK_1(ScreenCaptureNotificationUIGtk, gboolean, OnDelete,
30 CHROMEGTK_CALLBACK_0(ScreenCaptureNotificationUIGtk, void, OnClicked);
31 CHROMEGTK_CALLBACK_1(ScreenCaptureNotificationUIGtk, gboolean, OnConfigure,
33 CHROMEGTK_CALLBACK_1(ScreenCaptureNotificationUIGtk, gboolean, OnButtonPress,
39 const std::string text_;
41 base::Closure stop_callback_;
44 // Used to distinguish resize events from other types of "configure-event"
49 DISALLOW_COPY_AND_ASSIGN(ScreenCaptureNotificationUIGtk);
52 ScreenCaptureNotificationUIGtk::ScreenCaptureNotificationUIGtk(
53 const base::string16& text)
54 : text_(base::UTF16ToUTF8(text)),
60 ScreenCaptureNotificationUIGtk::~ScreenCaptureNotificationUIGtk() {
64 void ScreenCaptureNotificationUIGtk::CreateWindow() {
68 window_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
69 GtkWindow* window = GTK_WINDOW(window_);
71 g_signal_connect(window_, "delete-event", G_CALLBACK(OnDeleteThunk), this);
72 gtk_window_set_title(window, text_.c_str());
73 gtk_window_set_resizable(window, FALSE);
75 // Try to keep the window always visible.
76 gtk_window_stick(window);
77 gtk_window_set_keep_above(window, TRUE);
79 // Remove window titlebar.
80 gtk_window_set_decorated(window, FALSE);
82 // In case the titlebar is still there, try to remove some of the buttons.
83 // Utility windows have no minimize button or taskbar presence.
84 gtk_window_set_type_hint(window, GDK_WINDOW_TYPE_HINT_UTILITY);
85 gtk_window_set_deletable(window, FALSE);
87 // Allow custom rendering of the background pixmap.
88 gtk_widget_set_app_paintable(window_, TRUE);
90 // Handle window resizing, to regenerate the background pixmap and window
91 // shape bitmap. The stored width & height need to be initialized here
92 // in case the window is created a second time (the size of the previous
93 // window would be remembered, preventing the generation of bitmaps for the
95 current_height_ = current_width_ = 0;
96 g_signal_connect(window_, "configure-event",
97 G_CALLBACK(OnConfigureThunk), this);
99 // Handle mouse events to allow the user to drag the window around.
100 gtk_widget_set_events(window_, GDK_BUTTON_PRESS_MASK);
101 g_signal_connect(window_, "button-press-event",
102 G_CALLBACK(OnButtonPressThunk), this);
104 // All magic numbers taken from screen shots provided by UX.
105 // The alignment sets narrow margins at the top and bottom, compared with
106 // left and right. The left margin is made larger to accommodate the
107 // window movement gripper.
108 GtkWidget* align = gtk_alignment_new(0, 0, 1, 1);
109 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 8, 8, 24, 12);
110 gtk_container_add(GTK_CONTAINER(window), align);
112 GtkWidget* button_row = gtk_hbox_new(FALSE, 12);
113 gtk_container_add(GTK_CONTAINER(align), button_row);
115 std::string button_label =
116 l10n_util::GetStringUTF8(IDS_MEDIA_SCREEN_CAPTURE_NOTIFICATION_STOP);
117 GtkWidget* button = gtk_button_new_with_label(button_label.c_str());
118 gtk_box_pack_end(GTK_BOX(button_row), button, FALSE, FALSE, 0);
120 g_signal_connect(button, "clicked", G_CALLBACK(OnClickedThunk), this);
122 GtkWidget* message = gtk_label_new(NULL);
123 gtk_box_pack_end(GTK_BOX(button_row), message, FALSE, FALSE, 0);
125 // Override any theme setting for the text color, so that the text is
126 // readable against the window's background pixmap.
127 PangoAttrList* attributes = pango_attr_list_new();
128 PangoAttribute* text_color = pango_attr_foreground_new(0, 0, 0);
129 pango_attr_list_insert(attributes, text_color);
130 gtk_label_set_attributes(GTK_LABEL(message), attributes);
131 pango_attr_list_unref(attributes);
133 gtk_label_set_text(GTK_LABEL(message), text_.c_str());
135 gtk_widget_show_all(window_);
136 gtk_window_present(GTK_WINDOW(window_));
139 gfx::NativeViewId ScreenCaptureNotificationUIGtk::OnStarted(
140 const base::Closure& stop_callback) {
141 DCHECK(stop_callback_.is_null());
142 DCHECK(!stop_callback.is_null());
145 stop_callback_ = stop_callback;
151 void ScreenCaptureNotificationUIGtk::HideWindow() {
153 gtk_widget_destroy(window_);
157 stop_callback_.Reset();
160 void ScreenCaptureNotificationUIGtk::OnClicked(GtkWidget* button) {
161 stop_callback_.Run();
165 gboolean ScreenCaptureNotificationUIGtk::OnDelete(
166 GtkWidget* window, GdkEvent* event) {
167 stop_callback_.Run();
174 // Helper function for creating a rectangular path with rounded corners, as
175 // Cairo doesn't have this facility. |radius| is the arc-radius of each
176 // corner. The bounding rectangle extends from (0, 0) to (width, height).
177 void AddRoundRectPath(cairo_t* cairo_context, int width, int height,
179 cairo_new_sub_path(cairo_context);
180 cairo_arc(cairo_context, width - radius, radius, radius, -M_PI_2, 0);
181 cairo_arc(cairo_context, width - radius, height - radius, radius, 0, M_PI_2);
182 cairo_arc(cairo_context, radius, height - radius, radius, M_PI_2, 2 * M_PI_2);
183 cairo_arc(cairo_context, radius, radius, radius, 2 * M_PI_2, 3 * M_PI_2);
184 cairo_close_path(cairo_context);
189 gboolean ScreenCaptureNotificationUIGtk::OnConfigure(GtkWidget* widget,
190 GdkEventConfigure* event) {
191 // Only generate bitmaps if the size has actually changed.
192 if (event->width == current_width_ && event->height == current_height_)
195 current_width_ = event->width;
196 current_height_ = event->height;
198 // Create the depth 1 pixmap for the window shape.
199 GdkPixmap* shape_mask =
200 gdk_pixmap_new(NULL, current_width_, current_height_, 1);
201 cairo_t* cairo_context = gdk_cairo_create(shape_mask);
203 // Set the arc radius for the corners.
204 const int kCornerRadius = 6;
206 // Initialize the whole bitmap to be transparent.
207 cairo_set_source_rgba(cairo_context, 0, 0, 0, 0);
208 cairo_set_operator(cairo_context, CAIRO_OPERATOR_SOURCE);
209 cairo_paint(cairo_context);
211 // Paint an opaque round rect covering the whole area (leaving the extreme
212 // corners transparent).
213 cairo_set_source_rgba(cairo_context, 1, 1, 1, 1);
214 cairo_set_operator(cairo_context, CAIRO_OPERATOR_SOURCE);
215 AddRoundRectPath(cairo_context, current_width_, current_height_,
217 cairo_fill(cairo_context);
219 cairo_destroy(cairo_context);
220 gdk_window_shape_combine_mask(widget->window, shape_mask, 0, 0);
221 g_object_unref(shape_mask);
223 // Create a full-color pixmap for the window background image.
224 GdkPixmap* background =
225 gdk_pixmap_new(NULL, current_width_, current_height_, 24);
226 cairo_context = gdk_cairo_create(background);
228 // Paint the whole bitmap one color.
229 cairo_set_source_rgb(cairo_context, 0.91, 0.91, 0.91);
230 cairo_paint(cairo_context);
232 // Paint the round-rectangle edge.
233 cairo_set_source_rgb(cairo_context, 0.13, 0.69, 0.11);
234 cairo_set_line_width(cairo_context, 6);
235 AddRoundRectPath(cairo_context, current_width_, current_height_,
237 cairo_stroke(cairo_context);
239 // Render the window-gripper. In order for a straight line to light up
240 // single pixels, Cairo requires the coordinates to have fractional
241 // components of 0.5 (so the "/ 2" is a deliberate integer division).
242 double gripper_top = current_height_ / 2 - 10.5;
243 double gripper_bottom = current_height_ / 2 + 10.5;
244 cairo_set_line_width(cairo_context, 1);
247 cairo_set_source_rgb(cairo_context, 0.70, 0.70, 0.70);
248 cairo_move_to(cairo_context, x, gripper_top);
249 cairo_line_to(cairo_context, x, gripper_bottom);
250 cairo_stroke(cairo_context);
252 cairo_move_to(cairo_context, x, gripper_top);
253 cairo_line_to(cairo_context, x, gripper_bottom);
254 cairo_stroke(cairo_context);
257 cairo_set_source_rgb(cairo_context, 0.97, 0.97, 0.97);
258 cairo_move_to(cairo_context, x, gripper_top);
259 cairo_line_to(cairo_context, x, gripper_bottom);
260 cairo_stroke(cairo_context);
262 cairo_move_to(cairo_context, x, gripper_top);
263 cairo_line_to(cairo_context, x, gripper_bottom);
264 cairo_stroke(cairo_context);
266 cairo_destroy(cairo_context);
268 gdk_window_set_back_pixmap(widget->window, background, FALSE);
269 g_object_unref(background);
270 gdk_window_invalidate_rect(widget->window, NULL, TRUE);
275 gboolean ScreenCaptureNotificationUIGtk::OnButtonPress(GtkWidget* widget,
276 GdkEventButton* event) {
277 gtk_window_begin_move_drag(GTK_WINDOW(window_),
285 scoped_ptr<ScreenCaptureNotificationUI> ScreenCaptureNotificationUI::Create(
286 const base::string16& text) {
287 return scoped_ptr<ScreenCaptureNotificationUI>(
288 new ScreenCaptureNotificationUIGtk(text));