1 // Copyright (c) 2012 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.
8 #include "base/compiler_specific.h"
9 #include "base/logging.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "remoting/base/string_resources.h"
13 #include "remoting/host/client_session_control.h"
14 #include "remoting/host/host_window.h"
15 #include "ui/base/gtk/gtk_signal.h"
16 #include "ui/base/l10n/l10n_util.h"
22 class DisconnectWindowGtk
: public HostWindow
{
24 DisconnectWindowGtk();
25 virtual ~DisconnectWindowGtk();
27 // HostWindow overrides.
29 const base::WeakPtr
<ClientSessionControl
>& client_session_control
)
33 CHROMEGTK_CALLBACK_1(DisconnectWindowGtk
, gboolean
, OnDelete
, GdkEvent
*);
34 CHROMEGTK_CALLBACK_0(DisconnectWindowGtk
, void, OnClicked
);
35 CHROMEGTK_CALLBACK_1(DisconnectWindowGtk
, gboolean
, OnConfigure
,
37 CHROMEGTK_CALLBACK_1(DisconnectWindowGtk
, gboolean
, OnButtonPress
,
40 // Used to disconnect the client session.
41 base::WeakPtr
<ClientSessionControl
> client_session_control_
;
43 GtkWidget
* disconnect_window_
;
47 // Used to distinguish resize events from other types of "configure-event"
52 DISALLOW_COPY_AND_ASSIGN(DisconnectWindowGtk
);
55 // Helper function for creating a rectangular path with rounded corners, as
56 // Cairo doesn't have this facility. |radius| is the arc-radius of each
57 // corner. The bounding rectangle extends from (0, 0) to (width, height).
58 void AddRoundRectPath(cairo_t
* cairo_context
, int width
, int height
,
60 cairo_new_sub_path(cairo_context
);
61 cairo_arc(cairo_context
, width
- radius
, radius
, radius
, -M_PI_2
, 0);
62 cairo_arc(cairo_context
, width
- radius
, height
- radius
, radius
, 0, M_PI_2
);
63 cairo_arc(cairo_context
, radius
, height
- radius
, radius
, M_PI_2
, 2 * M_PI_2
);
64 cairo_arc(cairo_context
, radius
, radius
, radius
, 2 * M_PI_2
, 3 * M_PI_2
);
65 cairo_close_path(cairo_context
);
68 DisconnectWindowGtk::DisconnectWindowGtk()
69 : disconnect_window_(NULL
),
74 DisconnectWindowGtk::~DisconnectWindowGtk() {
75 DCHECK(CalledOnValidThread());
77 if (disconnect_window_
) {
78 gtk_widget_destroy(disconnect_window_
);
79 disconnect_window_
= NULL
;
83 void DisconnectWindowGtk::Start(
84 const base::WeakPtr
<ClientSessionControl
>& client_session_control
) {
85 DCHECK(CalledOnValidThread());
86 DCHECK(!client_session_control_
.get());
87 DCHECK(client_session_control
.get());
88 DCHECK(!disconnect_window_
);
90 client_session_control_
= client_session_control
;
93 disconnect_window_
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
94 GtkWindow
* window
= GTK_WINDOW(disconnect_window_
);
96 g_signal_connect(disconnect_window_
, "delete-event",
97 G_CALLBACK(OnDeleteThunk
), this);
98 gtk_window_set_title(window
,
99 l10n_util::GetStringUTF8(IDR_PRODUCT_NAME
).c_str());
100 gtk_window_set_resizable(window
, FALSE
);
102 // Try to keep the window always visible.
103 gtk_window_stick(window
);
104 gtk_window_set_keep_above(window
, TRUE
);
106 // Remove window titlebar.
107 gtk_window_set_decorated(window
, FALSE
);
109 // In case the titlebar is still there, try to remove some of the buttons.
110 // Utility windows have no minimize button or taskbar presence.
111 gtk_window_set_type_hint(window
, GDK_WINDOW_TYPE_HINT_UTILITY
);
112 gtk_window_set_deletable(window
, FALSE
);
114 // Allow custom rendering of the background pixmap.
115 gtk_widget_set_app_paintable(disconnect_window_
, TRUE
);
117 // Handle window resizing, to regenerate the background pixmap and window
118 // shape bitmap. The stored width & height need to be initialized here
119 // in case the window is created a second time (the size of the previous
120 // window would be remembered, preventing the generation of bitmaps for the
122 current_height_
= current_width_
= 0;
123 g_signal_connect(disconnect_window_
, "configure-event",
124 G_CALLBACK(OnConfigureThunk
), this);
126 // Handle mouse events to allow the user to drag the window around.
127 gtk_widget_set_events(disconnect_window_
, GDK_BUTTON_PRESS_MASK
);
128 g_signal_connect(disconnect_window_
, "button-press-event",
129 G_CALLBACK(OnButtonPressThunk
), this);
131 // All magic numbers taken from screen shots provided by UX.
132 // The alignment sets narrow margins at the top and bottom, compared with
133 // left and right. The left margin is made larger to accommodate the
134 // window movement gripper.
135 GtkWidget
* align
= gtk_alignment_new(0, 0, 1, 1);
136 gtk_alignment_set_padding(GTK_ALIGNMENT(align
), 8, 8, 24, 12);
137 gtk_container_add(GTK_CONTAINER(window
), align
);
139 GtkWidget
* button_row
= gtk_hbox_new(FALSE
, 12);
140 gtk_container_add(GTK_CONTAINER(align
), button_row
);
142 button_
= gtk_button_new_with_label(
143 l10n_util::GetStringUTF8(IDR_STOP_SHARING_BUTTON
).c_str());
144 gtk_box_pack_end(GTK_BOX(button_row
), button_
, FALSE
, FALSE
, 0);
146 g_signal_connect(button_
, "clicked", G_CALLBACK(OnClickedThunk
), this);
148 message_
= gtk_label_new(NULL
);
149 gtk_box_pack_end(GTK_BOX(button_row
), message_
, FALSE
, FALSE
, 0);
151 // Override any theme setting for the text color, so that the text is
152 // readable against the window's background pixmap.
153 PangoAttrList
* attributes
= pango_attr_list_new();
154 PangoAttribute
* text_color
= pango_attr_foreground_new(0, 0, 0);
155 pango_attr_list_insert(attributes
, text_color
);
156 gtk_label_set_attributes(GTK_LABEL(message_
), attributes
);
157 pango_attr_list_unref(attributes
);
159 gtk_widget_show_all(disconnect_window_
);
161 // Extract the user name from the JID.
162 std::string client_jid
= client_session_control_
->client_jid();
163 string16 username
= UTF8ToUTF16(client_jid
.substr(0, client_jid
.find('/')));
166 l10n_util::GetStringFUTF8(IDR_MESSAGE_SHARED
, username
).c_str());
167 gtk_window_present(window
);
170 void DisconnectWindowGtk::OnClicked(GtkWidget
* button
) {
171 DCHECK(CalledOnValidThread());
173 if (client_session_control_
.get())
174 client_session_control_
->DisconnectSession();
177 gboolean
DisconnectWindowGtk::OnDelete(GtkWidget
* window
,
179 DCHECK(CalledOnValidThread());
181 if (client_session_control_
.get())
182 client_session_control_
->DisconnectSession();
186 gboolean
DisconnectWindowGtk::OnConfigure(GtkWidget
* widget
,
187 GdkEventConfigure
* event
) {
188 DCHECK(CalledOnValidThread());
190 // Only generate bitmaps if the size has actually changed.
191 if (event
->width
== current_width_
&& event
->height
== current_height_
)
194 current_width_
= event
->width
;
195 current_height_
= event
->height
;
197 // Create the depth 1 pixmap for the window shape.
198 GdkPixmap
* shape_mask
= gdk_pixmap_new(NULL
, current_width_
, current_height_
,
200 cairo_t
* cairo_context
= gdk_cairo_create(shape_mask
);
202 // Set the arc radius for the corners.
203 const int kCornerRadius
= 6;
205 // Initialize the whole bitmap to be transparent.
206 cairo_set_source_rgba(cairo_context
, 0, 0, 0, 0);
207 cairo_set_operator(cairo_context
, CAIRO_OPERATOR_SOURCE
);
208 cairo_paint(cairo_context
);
210 // Paint an opaque round rect covering the whole area (leaving the extreme
211 // corners transparent).
212 cairo_set_source_rgba(cairo_context
, 1, 1, 1, 1);
213 cairo_set_operator(cairo_context
, CAIRO_OPERATOR_SOURCE
);
214 AddRoundRectPath(cairo_context
, current_width_
, current_height_
,
216 cairo_fill(cairo_context
);
218 cairo_destroy(cairo_context
);
219 gdk_window_shape_combine_mask(widget
->window
, shape_mask
, 0, 0);
220 g_object_unref(shape_mask
);
222 // Create a full-color pixmap for the window background image.
223 GdkPixmap
* background
= gdk_pixmap_new(NULL
, current_width_
, current_height_
,
225 cairo_context
= gdk_cairo_create(background
);
227 // Paint the whole bitmap one color.
228 cairo_set_source_rgb(cairo_context
, 0.91, 0.91, 0.91);
229 cairo_paint(cairo_context
);
231 // Paint the round-rectangle edge.
232 cairo_set_source_rgb(cairo_context
, 0.13, 0.69, 0.11);
233 cairo_set_line_width(cairo_context
, 6);
234 AddRoundRectPath(cairo_context
, current_width_
, current_height_
,
236 cairo_stroke(cairo_context
);
238 // Render the window-gripper. In order for a straight line to light up
239 // single pixels, Cairo requires the coordinates to have fractional
240 // components of 0.5 (so the "/ 2" is a deliberate integer division).
241 double gripper_top
= current_height_
/ 2 - 10.5;
242 double gripper_bottom
= current_height_
/ 2 + 10.5;
243 cairo_set_line_width(cairo_context
, 1);
246 cairo_set_source_rgb(cairo_context
, 0.70, 0.70, 0.70);
247 cairo_move_to(cairo_context
, x
, gripper_top
);
248 cairo_line_to(cairo_context
, x
, gripper_bottom
);
249 cairo_stroke(cairo_context
);
251 cairo_move_to(cairo_context
, x
, gripper_top
);
252 cairo_line_to(cairo_context
, x
, gripper_bottom
);
253 cairo_stroke(cairo_context
);
256 cairo_set_source_rgb(cairo_context
, 0.97, 0.97, 0.97);
257 cairo_move_to(cairo_context
, x
, gripper_top
);
258 cairo_line_to(cairo_context
, x
, gripper_bottom
);
259 cairo_stroke(cairo_context
);
261 cairo_move_to(cairo_context
, x
, gripper_top
);
262 cairo_line_to(cairo_context
, x
, gripper_bottom
);
263 cairo_stroke(cairo_context
);
265 cairo_destroy(cairo_context
);
267 gdk_window_set_back_pixmap(widget
->window
, background
, FALSE
);
268 g_object_unref(background
);
269 gdk_window_invalidate_rect(widget
->window
, NULL
, TRUE
);
274 gboolean
DisconnectWindowGtk::OnButtonPress(GtkWidget
* widget
,
275 GdkEventButton
* event
) {
276 DCHECK(CalledOnValidThread());
278 gtk_window_begin_move_drag(GTK_WINDOW(disconnect_window_
),
289 scoped_ptr
<HostWindow
> HostWindow::CreateDisconnectWindow() {
290 return scoped_ptr
<HostWindow
>(new DisconnectWindowGtk());
293 } // namespace remoting