| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/ui/screen_capture_notification_ui.h" | |
| 6 | |
| 7 #include <gtk/gtk.h> | |
| 8 #include <math.h> | |
| 9 | |
| 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" | |
| 17 | |
| 18 class ScreenCaptureNotificationUIGtk : public ScreenCaptureNotificationUI { | |
| 19 public: | |
| 20 explicit ScreenCaptureNotificationUIGtk(const base::string16& text); | |
| 21 virtual ~ScreenCaptureNotificationUIGtk(); | |
| 22 | |
| 23 // ScreenCaptureNotificationUI interface | |
| 24 virtual gfx::NativeViewId OnStarted(const base::Closure& stop_callback) | |
| 25 OVERRIDE; | |
| 26 | |
| 27 private: | |
| 28 CHROMEGTK_CALLBACK_1(ScreenCaptureNotificationUIGtk, gboolean, OnDelete, | |
| 29 GdkEvent*); | |
| 30 CHROMEGTK_CALLBACK_0(ScreenCaptureNotificationUIGtk, void, OnClicked); | |
| 31 CHROMEGTK_CALLBACK_1(ScreenCaptureNotificationUIGtk, gboolean, OnConfigure, | |
| 32 GdkEventConfigure*); | |
| 33 CHROMEGTK_CALLBACK_1(ScreenCaptureNotificationUIGtk, gboolean, OnButtonPress, | |
| 34 GdkEventButton*); | |
| 35 | |
| 36 void CreateWindow(); | |
| 37 void HideWindow(); | |
| 38 | |
| 39 const std::string text_; | |
| 40 | |
| 41 base::Closure stop_callback_; | |
| 42 GtkWidget* window_; | |
| 43 | |
| 44 // Used to distinguish resize events from other types of "configure-event" | |
| 45 // notifications. | |
| 46 int current_width_; | |
| 47 int current_height_; | |
| 48 | |
| 49 DISALLOW_COPY_AND_ASSIGN(ScreenCaptureNotificationUIGtk); | |
| 50 }; | |
| 51 | |
| 52 ScreenCaptureNotificationUIGtk::ScreenCaptureNotificationUIGtk( | |
| 53 const base::string16& text) | |
| 54 : text_(base::UTF16ToUTF8(text)), | |
| 55 window_(NULL), | |
| 56 current_width_(0), | |
| 57 current_height_(0) { | |
| 58 } | |
| 59 | |
| 60 ScreenCaptureNotificationUIGtk::~ScreenCaptureNotificationUIGtk() { | |
| 61 HideWindow(); | |
| 62 } | |
| 63 | |
| 64 void ScreenCaptureNotificationUIGtk::CreateWindow() { | |
| 65 if (window_) | |
| 66 return; | |
| 67 | |
| 68 window_ = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
| 69 GtkWindow* window = GTK_WINDOW(window_); | |
| 70 | |
| 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); | |
| 74 | |
| 75 // Try to keep the window always visible. | |
| 76 gtk_window_stick(window); | |
| 77 gtk_window_set_keep_above(window, TRUE); | |
| 78 | |
| 79 // Remove window titlebar. | |
| 80 gtk_window_set_decorated(window, FALSE); | |
| 81 | |
| 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); | |
| 86 | |
| 87 // Allow custom rendering of the background pixmap. | |
| 88 gtk_widget_set_app_paintable(window_, TRUE); | |
| 89 | |
| 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 | |
| 94 // new window). | |
| 95 current_height_ = current_width_ = 0; | |
| 96 g_signal_connect(window_, "configure-event", | |
| 97 G_CALLBACK(OnConfigureThunk), this); | |
| 98 | |
| 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); | |
| 103 | |
| 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); | |
| 111 | |
| 112 GtkWidget* button_row = gtk_hbox_new(FALSE, 12); | |
| 113 gtk_container_add(GTK_CONTAINER(align), button_row); | |
| 114 | |
| 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); | |
| 119 | |
| 120 g_signal_connect(button, "clicked", G_CALLBACK(OnClickedThunk), this); | |
| 121 | |
| 122 GtkWidget* message = gtk_label_new(NULL); | |
| 123 gtk_box_pack_end(GTK_BOX(button_row), message, FALSE, FALSE, 0); | |
| 124 | |
| 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); | |
| 132 | |
| 133 gtk_label_set_text(GTK_LABEL(message), text_.c_str()); | |
| 134 | |
| 135 gtk_widget_show_all(window_); | |
| 136 gtk_window_present(GTK_WINDOW(window_)); | |
| 137 } | |
| 138 | |
| 139 gfx::NativeViewId ScreenCaptureNotificationUIGtk::OnStarted( | |
| 140 const base::Closure& stop_callback) { | |
| 141 DCHECK(stop_callback_.is_null()); | |
| 142 DCHECK(!stop_callback.is_null()); | |
| 143 DCHECK(!window_); | |
| 144 | |
| 145 stop_callback_ = stop_callback; | |
| 146 CreateWindow(); | |
| 147 | |
| 148 return 0; | |
| 149 } | |
| 150 | |
| 151 void ScreenCaptureNotificationUIGtk::HideWindow() { | |
| 152 if (window_) { | |
| 153 gtk_widget_destroy(window_); | |
| 154 window_ = NULL; | |
| 155 } | |
| 156 | |
| 157 stop_callback_.Reset(); | |
| 158 } | |
| 159 | |
| 160 void ScreenCaptureNotificationUIGtk::OnClicked(GtkWidget* button) { | |
| 161 stop_callback_.Run(); | |
| 162 HideWindow(); | |
| 163 } | |
| 164 | |
| 165 gboolean ScreenCaptureNotificationUIGtk::OnDelete( | |
| 166 GtkWidget* window, GdkEvent* event) { | |
| 167 stop_callback_.Run(); | |
| 168 HideWindow(); | |
| 169 | |
| 170 return TRUE; | |
| 171 } | |
| 172 | |
| 173 namespace { | |
| 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, | |
| 178 int radius) { | |
| 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); | |
| 185 } | |
| 186 | |
| 187 } // namespace | |
| 188 | |
| 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_) | |
| 193 return FALSE; | |
| 194 | |
| 195 current_width_ = event->width; | |
| 196 current_height_ = event->height; | |
| 197 | |
| 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); | |
| 202 | |
| 203 // Set the arc radius for the corners. | |
| 204 const int kCornerRadius = 6; | |
| 205 | |
| 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); | |
| 210 | |
| 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_, | |
| 216 kCornerRadius); | |
| 217 cairo_fill(cairo_context); | |
| 218 | |
| 219 cairo_destroy(cairo_context); | |
| 220 gdk_window_shape_combine_mask(widget->window, shape_mask, 0, 0); | |
| 221 g_object_unref(shape_mask); | |
| 222 | |
| 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); | |
| 227 | |
| 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); | |
| 231 | |
| 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_, | |
| 236 kCornerRadius); | |
| 237 cairo_stroke(cairo_context); | |
| 238 | |
| 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); | |
| 245 | |
| 246 double x = 12.5; | |
| 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); | |
| 251 x += 3; | |
| 252 cairo_move_to(cairo_context, x, gripper_top); | |
| 253 cairo_line_to(cairo_context, x, gripper_bottom); | |
| 254 cairo_stroke(cairo_context); | |
| 255 | |
| 256 x -= 2; | |
| 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); | |
| 261 x += 3; | |
| 262 cairo_move_to(cairo_context, x, gripper_top); | |
| 263 cairo_line_to(cairo_context, x, gripper_bottom); | |
| 264 cairo_stroke(cairo_context); | |
| 265 | |
| 266 cairo_destroy(cairo_context); | |
| 267 | |
| 268 gdk_window_set_back_pixmap(widget->window, background, FALSE); | |
| 269 g_object_unref(background); | |
| 270 gdk_window_invalidate_rect(widget->window, NULL, TRUE); | |
| 271 | |
| 272 return FALSE; | |
| 273 } | |
| 274 | |
| 275 gboolean ScreenCaptureNotificationUIGtk::OnButtonPress(GtkWidget* widget, | |
| 276 GdkEventButton* event) { | |
| 277 gtk_window_begin_move_drag(GTK_WINDOW(window_), | |
| 278 event->button, | |
| 279 event->x_root, | |
| 280 event->y_root, | |
| 281 event->time); | |
| 282 return FALSE; | |
| 283 } | |
| 284 | |
| 285 scoped_ptr<ScreenCaptureNotificationUI> ScreenCaptureNotificationUI::Create( | |
| 286 const base::string16& text) { | |
| 287 return scoped_ptr<ScreenCaptureNotificationUI>( | |
| 288 new ScreenCaptureNotificationUIGtk(text)); | |
| 289 } | |
| OLD | NEW |