- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / gtk / screen_capture_notification_ui_gtk.cc
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 string16& text);
21   virtual ~ScreenCaptureNotificationUIGtk();
22
23   // ScreenCaptureNotificationUI interface
24   virtual void OnStarted(const base::Closure& stop_callback) OVERRIDE;
25
26  private:
27   CHROMEGTK_CALLBACK_1(ScreenCaptureNotificationUIGtk, gboolean, OnDelete,
28                        GdkEvent*);
29   CHROMEGTK_CALLBACK_0(ScreenCaptureNotificationUIGtk, void, OnClicked);
30   CHROMEGTK_CALLBACK_1(ScreenCaptureNotificationUIGtk, gboolean, OnConfigure,
31                        GdkEventConfigure*);
32   CHROMEGTK_CALLBACK_1(ScreenCaptureNotificationUIGtk, gboolean, OnButtonPress,
33                        GdkEventButton*);
34
35   void CreateWindow();
36   void HideWindow();
37
38   const std::string text_;
39
40   base::Closure stop_callback_;
41   GtkWidget* window_;
42
43   // Used to distinguish resize events from other types of "configure-event"
44   // notifications.
45   int current_width_;
46   int current_height_;
47
48   DISALLOW_COPY_AND_ASSIGN(ScreenCaptureNotificationUIGtk);
49 };
50
51 ScreenCaptureNotificationUIGtk::ScreenCaptureNotificationUIGtk(
52     const string16& text)
53     : text_(UTF16ToUTF8(text)),
54       window_(NULL),
55       current_width_(0),
56       current_height_(0) {
57 }
58
59 ScreenCaptureNotificationUIGtk::~ScreenCaptureNotificationUIGtk() {
60   HideWindow();
61 }
62
63 void ScreenCaptureNotificationUIGtk::CreateWindow() {
64   if (window_)
65     return;
66
67   window_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
68   GtkWindow* window = GTK_WINDOW(window_);
69
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);
73
74   // Try to keep the window always visible.
75   gtk_window_stick(window);
76   gtk_window_set_keep_above(window, TRUE);
77
78   // Remove window titlebar.
79   gtk_window_set_decorated(window, FALSE);
80
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);
85
86   // Allow custom rendering of the background pixmap.
87   gtk_widget_set_app_paintable(window_, TRUE);
88
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
93   // new window).
94   current_height_ = current_width_ = 0;
95   g_signal_connect(window_, "configure-event",
96                    G_CALLBACK(OnConfigureThunk), this);
97
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);
102
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);
110
111   GtkWidget* button_row = gtk_hbox_new(FALSE, 12);
112   gtk_container_add(GTK_CONTAINER(align), button_row);
113
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);
118
119   g_signal_connect(button, "clicked", G_CALLBACK(OnClickedThunk), this);
120
121   GtkWidget* message = gtk_label_new(NULL);
122   gtk_box_pack_end(GTK_BOX(button_row), message, FALSE, FALSE, 0);
123
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);
131
132   gtk_label_set_text(GTK_LABEL(message), text_.c_str());
133
134   gtk_widget_show_all(window_);
135   gtk_window_present(GTK_WINDOW(window_));
136 }
137
138 void ScreenCaptureNotificationUIGtk::OnStarted(
139     const base::Closure& stop_callback) {
140   DCHECK(stop_callback_.is_null());
141   DCHECK(!stop_callback.is_null());
142   DCHECK(!window_);
143
144   stop_callback_ = stop_callback;
145   CreateWindow();
146 }
147
148 void ScreenCaptureNotificationUIGtk::HideWindow() {
149   if (window_) {
150     gtk_widget_destroy(window_);
151     window_ = NULL;
152   }
153
154   stop_callback_.Reset();
155 }
156
157 void ScreenCaptureNotificationUIGtk::OnClicked(GtkWidget* button) {
158   stop_callback_.Run();
159   HideWindow();
160 }
161
162 gboolean ScreenCaptureNotificationUIGtk::OnDelete(
163     GtkWidget* window, GdkEvent* event) {
164   stop_callback_.Run();
165   HideWindow();
166
167   return TRUE;
168 }
169
170 namespace {
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,
175                       int radius) {
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);
182 }
183
184 }  // namespace
185
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_)
190     return FALSE;
191
192   current_width_ = event->width;
193   current_height_ = event->height;
194
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);
199
200   // Set the arc radius for the corners.
201   const int kCornerRadius = 6;
202
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);
207
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_,
213                    kCornerRadius);
214   cairo_fill(cairo_context);
215
216   cairo_destroy(cairo_context);
217   gdk_window_shape_combine_mask(widget->window, shape_mask, 0, 0);
218   g_object_unref(shape_mask);
219
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);
224
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);
228
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_,
233                    kCornerRadius);
234   cairo_stroke(cairo_context);
235
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);
242
243   double x = 12.5;
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);
248   x += 3;
249   cairo_move_to(cairo_context, x, gripper_top);
250   cairo_line_to(cairo_context, x, gripper_bottom);
251   cairo_stroke(cairo_context);
252
253   x -= 2;
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);
258   x += 3;
259   cairo_move_to(cairo_context, x, gripper_top);
260   cairo_line_to(cairo_context, x, gripper_bottom);
261   cairo_stroke(cairo_context);
262
263   cairo_destroy(cairo_context);
264
265   gdk_window_set_back_pixmap(widget->window, background, FALSE);
266   g_object_unref(background);
267   gdk_window_invalidate_rect(widget->window, NULL, TRUE);
268
269   return FALSE;
270 }
271
272 gboolean ScreenCaptureNotificationUIGtk::OnButtonPress(GtkWidget* widget,
273                                               GdkEventButton* event) {
274   gtk_window_begin_move_drag(GTK_WINDOW(window_),
275                              event->button,
276                              event->x_root,
277                              event->y_root,
278                              event->time);
279   return FALSE;
280 }
281
282 scoped_ptr<ScreenCaptureNotificationUI> ScreenCaptureNotificationUI::Create(
283     const string16& text) {
284   return scoped_ptr<ScreenCaptureNotificationUI>(
285       new ScreenCaptureNotificationUIGtk(text));
286 }