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 void OnStarted(const base::Closure
& stop_callback
) OVERRIDE
;
27 CHROMEGTK_CALLBACK_1(ScreenCaptureNotificationUIGtk
, gboolean
, OnDelete
,
29 CHROMEGTK_CALLBACK_0(ScreenCaptureNotificationUIGtk
, void, OnClicked
);
30 CHROMEGTK_CALLBACK_1(ScreenCaptureNotificationUIGtk
, gboolean
, OnConfigure
,
32 CHROMEGTK_CALLBACK_1(ScreenCaptureNotificationUIGtk
, gboolean
, OnButtonPress
,
38 const std::string text_
;
40 base::Closure stop_callback_
;
43 // Used to distinguish resize events from other types of "configure-event"
48 DISALLOW_COPY_AND_ASSIGN(ScreenCaptureNotificationUIGtk
);
51 ScreenCaptureNotificationUIGtk::ScreenCaptureNotificationUIGtk(
52 const base::string16
& text
)
53 : text_(base::UTF16ToUTF8(text
)),
59 ScreenCaptureNotificationUIGtk::~ScreenCaptureNotificationUIGtk() {
63 void ScreenCaptureNotificationUIGtk::CreateWindow() {
67 window_
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
68 GtkWindow
* window
= GTK_WINDOW(window_
);
70 g_signal_connect(window_
, "delete-event", G_CALLBACK(OnDeleteThunk
), this);
71 gtk_window_set_title(window
, text_
.c_str());
72 gtk_window_set_resizable(window
, FALSE
);
74 // Try to keep the window always visible.
75 gtk_window_stick(window
);
76 gtk_window_set_keep_above(window
, TRUE
);
78 // Remove window titlebar.
79 gtk_window_set_decorated(window
, FALSE
);
81 // In case the titlebar is still there, try to remove some of the buttons.
82 // Utility windows have no minimize button or taskbar presence.
83 gtk_window_set_type_hint(window
, GDK_WINDOW_TYPE_HINT_UTILITY
);
84 gtk_window_set_deletable(window
, FALSE
);
86 // Allow custom rendering of the background pixmap.
87 gtk_widget_set_app_paintable(window_
, TRUE
);
89 // Handle window resizing, to regenerate the background pixmap and window
90 // shape bitmap. The stored width & height need to be initialized here
91 // in case the window is created a second time (the size of the previous
92 // window would be remembered, preventing the generation of bitmaps for the
94 current_height_
= current_width_
= 0;
95 g_signal_connect(window_
, "configure-event",
96 G_CALLBACK(OnConfigureThunk
), this);
98 // Handle mouse events to allow the user to drag the window around.
99 gtk_widget_set_events(window_
, GDK_BUTTON_PRESS_MASK
);
100 g_signal_connect(window_
, "button-press-event",
101 G_CALLBACK(OnButtonPressThunk
), this);
103 // All magic numbers taken from screen shots provided by UX.
104 // The alignment sets narrow margins at the top and bottom, compared with
105 // left and right. The left margin is made larger to accommodate the
106 // window movement gripper.
107 GtkWidget
* align
= gtk_alignment_new(0, 0, 1, 1);
108 gtk_alignment_set_padding(GTK_ALIGNMENT(align
), 8, 8, 24, 12);
109 gtk_container_add(GTK_CONTAINER(window
), align
);
111 GtkWidget
* button_row
= gtk_hbox_new(FALSE
, 12);
112 gtk_container_add(GTK_CONTAINER(align
), button_row
);
114 std::string button_label
=
115 l10n_util::GetStringUTF8(IDS_MEDIA_SCREEN_CAPTURE_NOTIFICATION_STOP
);
116 GtkWidget
* button
= gtk_button_new_with_label(button_label
.c_str());
117 gtk_box_pack_end(GTK_BOX(button_row
), button
, FALSE
, FALSE
, 0);
119 g_signal_connect(button
, "clicked", G_CALLBACK(OnClickedThunk
), this);
121 GtkWidget
* message
= gtk_label_new(NULL
);
122 gtk_box_pack_end(GTK_BOX(button_row
), message
, FALSE
, FALSE
, 0);
124 // Override any theme setting for the text color, so that the text is
125 // readable against the window's background pixmap.
126 PangoAttrList
* attributes
= pango_attr_list_new();
127 PangoAttribute
* text_color
= pango_attr_foreground_new(0, 0, 0);
128 pango_attr_list_insert(attributes
, text_color
);
129 gtk_label_set_attributes(GTK_LABEL(message
), attributes
);
130 pango_attr_list_unref(attributes
);
132 gtk_label_set_text(GTK_LABEL(message
), text_
.c_str());
134 gtk_widget_show_all(window_
);
135 gtk_window_present(GTK_WINDOW(window_
));
138 void ScreenCaptureNotificationUIGtk::OnStarted(
139 const base::Closure
& stop_callback
) {
140 DCHECK(stop_callback_
.is_null());
141 DCHECK(!stop_callback
.is_null());
144 stop_callback_
= stop_callback
;
148 void ScreenCaptureNotificationUIGtk::HideWindow() {
150 gtk_widget_destroy(window_
);
154 stop_callback_
.Reset();
157 void ScreenCaptureNotificationUIGtk::OnClicked(GtkWidget
* button
) {
158 stop_callback_
.Run();
162 gboolean
ScreenCaptureNotificationUIGtk::OnDelete(
163 GtkWidget
* window
, GdkEvent
* event
) {
164 stop_callback_
.Run();
171 // Helper function for creating a rectangular path with rounded corners, as
172 // Cairo doesn't have this facility. |radius| is the arc-radius of each
173 // corner. The bounding rectangle extends from (0, 0) to (width, height).
174 void AddRoundRectPath(cairo_t
* cairo_context
, int width
, int height
,
176 cairo_new_sub_path(cairo_context
);
177 cairo_arc(cairo_context
, width
- radius
, radius
, radius
, -M_PI_2
, 0);
178 cairo_arc(cairo_context
, width
- radius
, height
- radius
, radius
, 0, M_PI_2
);
179 cairo_arc(cairo_context
, radius
, height
- radius
, radius
, M_PI_2
, 2 * M_PI_2
);
180 cairo_arc(cairo_context
, radius
, radius
, radius
, 2 * M_PI_2
, 3 * M_PI_2
);
181 cairo_close_path(cairo_context
);
186 gboolean
ScreenCaptureNotificationUIGtk::OnConfigure(GtkWidget
* widget
,
187 GdkEventConfigure
* event
) {
188 // Only generate bitmaps if the size has actually changed.
189 if (event
->width
== current_width_
&& event
->height
== current_height_
)
192 current_width_
= event
->width
;
193 current_height_
= event
->height
;
195 // Create the depth 1 pixmap for the window shape.
196 GdkPixmap
* shape_mask
=
197 gdk_pixmap_new(NULL
, current_width_
, current_height_
, 1);
198 cairo_t
* cairo_context
= gdk_cairo_create(shape_mask
);
200 // Set the arc radius for the corners.
201 const int kCornerRadius
= 6;
203 // Initialize the whole bitmap to be transparent.
204 cairo_set_source_rgba(cairo_context
, 0, 0, 0, 0);
205 cairo_set_operator(cairo_context
, CAIRO_OPERATOR_SOURCE
);
206 cairo_paint(cairo_context
);
208 // Paint an opaque round rect covering the whole area (leaving the extreme
209 // corners transparent).
210 cairo_set_source_rgba(cairo_context
, 1, 1, 1, 1);
211 cairo_set_operator(cairo_context
, CAIRO_OPERATOR_SOURCE
);
212 AddRoundRectPath(cairo_context
, current_width_
, current_height_
,
214 cairo_fill(cairo_context
);
216 cairo_destroy(cairo_context
);
217 gdk_window_shape_combine_mask(widget
->window
, shape_mask
, 0, 0);
218 g_object_unref(shape_mask
);
220 // Create a full-color pixmap for the window background image.
221 GdkPixmap
* background
=
222 gdk_pixmap_new(NULL
, current_width_
, current_height_
, 24);
223 cairo_context
= gdk_cairo_create(background
);
225 // Paint the whole bitmap one color.
226 cairo_set_source_rgb(cairo_context
, 0.91, 0.91, 0.91);
227 cairo_paint(cairo_context
);
229 // Paint the round-rectangle edge.
230 cairo_set_source_rgb(cairo_context
, 0.13, 0.69, 0.11);
231 cairo_set_line_width(cairo_context
, 6);
232 AddRoundRectPath(cairo_context
, current_width_
, current_height_
,
234 cairo_stroke(cairo_context
);
236 // Render the window-gripper. In order for a straight line to light up
237 // single pixels, Cairo requires the coordinates to have fractional
238 // components of 0.5 (so the "/ 2" is a deliberate integer division).
239 double gripper_top
= current_height_
/ 2 - 10.5;
240 double gripper_bottom
= current_height_
/ 2 + 10.5;
241 cairo_set_line_width(cairo_context
, 1);
244 cairo_set_source_rgb(cairo_context
, 0.70, 0.70, 0.70);
245 cairo_move_to(cairo_context
, x
, gripper_top
);
246 cairo_line_to(cairo_context
, x
, gripper_bottom
);
247 cairo_stroke(cairo_context
);
249 cairo_move_to(cairo_context
, x
, gripper_top
);
250 cairo_line_to(cairo_context
, x
, gripper_bottom
);
251 cairo_stroke(cairo_context
);
254 cairo_set_source_rgb(cairo_context
, 0.97, 0.97, 0.97);
255 cairo_move_to(cairo_context
, x
, gripper_top
);
256 cairo_line_to(cairo_context
, x
, gripper_bottom
);
257 cairo_stroke(cairo_context
);
259 cairo_move_to(cairo_context
, x
, gripper_top
);
260 cairo_line_to(cairo_context
, x
, gripper_bottom
);
261 cairo_stroke(cairo_context
);
263 cairo_destroy(cairo_context
);
265 gdk_window_set_back_pixmap(widget
->window
, background
, FALSE
);
266 g_object_unref(background
);
267 gdk_window_invalidate_rect(widget
->window
, NULL
, TRUE
);
272 gboolean
ScreenCaptureNotificationUIGtk::OnButtonPress(GtkWidget
* widget
,
273 GdkEventButton
* event
) {
274 gtk_window_begin_move_drag(GTK_WINDOW(window_
),
282 scoped_ptr
<ScreenCaptureNotificationUI
> ScreenCaptureNotificationUI::Create(
283 const base::string16
& text
) {
284 return scoped_ptr
<ScreenCaptureNotificationUI
>(
285 new ScreenCaptureNotificationUIGtk(text
));