1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/gtk/avatar_menu_item_gtk.h"
#include <gdk/gdkkeysyms.h>
#include "base/bind.h"
#include "base/message_loop.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/ui/gtk/gtk_chrome_link_button.h"
#include "chrome/browser/ui/gtk/gtk_theme_service.h"
#include "chrome/browser/ui/gtk/gtk_util.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/notification_source.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/gtk/gtk_hig_constants.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/text/text_elider.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/gtk_util.h"
#include "ui/gfx/image/image.h"
namespace {
// A checkmark is drawn in the lower-right corner of the active avatar image.
// This value is the x offset, in pixels, from that position.
const int kCheckMarkXOffset = 2;
// The maximum width of a user name in pixels. Anything longer than this will be
// elided.
const int kUserNameMaxWidth = 200;
// The color of the item highlight when we're in chrome-theme mode.
const GdkColor kHighlightColor = GDK_COLOR_RGB(0xe3, 0xed, 0xf6);
// The color of the background when we're in chrome-theme mode.
const GdkColor kBackgroundColor = GDK_COLOR_RGB(0xff, 0xff, 0xff);
} // namespace
AvatarMenuItemGtk::AvatarMenuItemGtk(Delegate* delegate,
const AvatarMenuModel::Item& item,
size_t item_index,
GtkThemeService* theme_service)
: delegate_(delegate),
item_(item),
item_index_(item_index),
theme_service_(theme_service),
status_label_(NULL),
link_alignment_(NULL),
edit_profile_link_(NULL) {
Init(theme_service_);
registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
content::Source<ThemeService>(theme_service_));
theme_service_->InitThemesFor(this);
}
AvatarMenuItemGtk::~AvatarMenuItemGtk() {
widget_.Destroy();
}
gboolean AvatarMenuItemGtk::OnProfileClick(GtkWidget* widget,
GdkEventButton* event) {
delegate_->OpenProfile(item_index_);
return FALSE;
}
gboolean AvatarMenuItemGtk::OnProfileKeyPress(GtkWidget* widget,
GdkEventKey* event) {
if (event->keyval == GDK_Return ||
event->keyval == GDK_ISO_Enter ||
event->keyval == GDK_KP_Enter) {
if (item_.active)
delegate_->EditProfile(item_index_);
else
delegate_->OpenProfile(item_index_);
}
return FALSE;
}
void AvatarMenuItemGtk::ShowStatusLabel() {
gtk_widget_show(status_label_);
gtk_widget_hide(link_alignment_);
}
void AvatarMenuItemGtk::ShowEditLink() {
gtk_widget_hide(status_label_);
gtk_widget_show(link_alignment_);
}
gboolean AvatarMenuItemGtk::OnProfileFocusIn(GtkWidget* widget,
GdkEventFocus* event) {
if (item_.active)
ShowEditLink();
return FALSE;
}
gboolean AvatarMenuItemGtk::OnProfileFocusOut(GtkWidget* widget,
GdkEventFocus* event) {
if (item_.active)
ShowStatusLabel();
return FALSE;
}
gboolean AvatarMenuItemGtk::OnProfileEnter(GtkWidget* widget,
GdkEventCrossing* event) {
if (event->detail == GDK_NOTIFY_INFERIOR)
return FALSE;
gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, &highlighted_color_);
if (item_.active)
ShowEditLink();
return FALSE;
}
gboolean AvatarMenuItemGtk::OnProfileLeave(GtkWidget* widget,
GdkEventCrossing* event) {
if (event->detail == GDK_NOTIFY_INFERIOR)
return FALSE;
gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, unhighlighted_color_);
if (item_.active)
ShowStatusLabel();
return FALSE;
}
void AvatarMenuItemGtk::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK_EQ(type, chrome::NOTIFICATION_BROWSER_THEME_CHANGED);
bool using_native = theme_service_->UsingNativeTheme();
if (using_native) {
GtkStyle* style = gtk_rc_get_style(widget_.get());
highlighted_color_ = style->bg[GTK_STATE_SELECTED];
unhighlighted_color_ = NULL;
} else {
highlighted_color_ = kHighlightColor;
unhighlighted_color_ = &kBackgroundColor;
}
// Assume that the widget isn't highlighted since theme changes will almost
// never happen while we're up.
gtk_widget_modify_bg(widget_.get(), GTK_STATE_NORMAL, unhighlighted_color_);
}
void AvatarMenuItemGtk::OnEditProfileLinkClicked(GtkWidget* link) {
delegate_->EditProfile(item_index_);
}
gboolean AvatarMenuItemGtk::OnEventBoxExpose(GtkWidget* widget,
GdkEventExpose* event) {
// Draw the focus rectangle.
if (gtk_widget_has_focus(widget)) {
GtkAllocation allocation;
gtk_widget_get_allocation(widget, &allocation);
gtk_paint_focus(gtk_widget_get_style(widget),
gtk_widget_get_window(widget),
gtk_widget_get_state(widget),
&event->area, widget, NULL,
0, 0,
allocation.width, allocation.height);
}
return TRUE;
}
void AvatarMenuItemGtk::Init(GtkThemeService* theme_service) {
widget_.Own(gtk_event_box_new());
g_signal_connect(widget_.get(), "button-press-event",
G_CALLBACK(OnProfileClickThunk), this);
g_signal_connect(widget_.get(), "enter-notify-event",
G_CALLBACK(OnProfileEnterThunk), this);
g_signal_connect(widget_.get(), "leave-notify-event",
G_CALLBACK(OnProfileLeaveThunk), this);
g_signal_connect(widget_.get(), "focus-in-event",
G_CALLBACK(OnProfileFocusInThunk), this);
g_signal_connect(widget_.get(), "focus-out-event",
G_CALLBACK(OnProfileFocusOutThunk), this);
g_signal_connect(widget_.get(), "key-press-event",
G_CALLBACK(OnProfileKeyPressThunk), this);
g_signal_connect_after(widget_.get(), "expose-event",
G_CALLBACK(OnEventBoxExposeThunk), this);
GtkWidget* item_hbox = gtk_hbox_new(FALSE, ui::kControlSpacing);
GdkPixbuf* avatar_pixbuf = NULL;
// If this profile is active then draw a check mark on the bottom right
// of the profile icon.
if (item_.active) {
const SkBitmap* avatar_image = item_.icon.ToSkBitmap();
gfx::ImageSkiaRep avatar_image_rep =
gfx::ImageSkiaRep(*avatar_image, ui::SCALE_FACTOR_100P);
gfx::Canvas canvas(avatar_image_rep, /* is_opaque */ true);
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
const SkBitmap* check_image = rb.GetImageNamed(
IDR_PROFILE_SELECTED).ToSkBitmap();
gfx::Rect check_rect(0, 0, check_image->width(), check_image->height());
int y = avatar_image->height() - check_image->height();
int x = avatar_image->width() - check_image->width() + kCheckMarkXOffset;
canvas.DrawImageInt(*check_image, x, y);
SkBitmap final_image = canvas.ExtractImageRep().sk_bitmap();
avatar_pixbuf = gfx::GdkPixbufFromSkBitmap(final_image);
} else {
avatar_pixbuf = gfx::GdkPixbufFromSkBitmap(*item_.icon.ToSkBitmap());
}
GtkWidget* avatar_image = gtk_image_new_from_pixbuf(avatar_pixbuf);
g_object_unref(avatar_pixbuf);
gtk_misc_set_alignment(GTK_MISC(avatar_image), 0, 0);
gtk_box_pack_start(GTK_BOX(item_hbox), avatar_image, FALSE, FALSE, 0);
// The user name label.
GtkWidget* item_vbox = gtk_vbox_new(FALSE, 0);
GtkWidget* name_label = NULL;
string16 elided_name = ui::ElideText(item_.name,
gfx::Font(),
kUserNameMaxWidth,
ui::ELIDE_AT_END);
name_label = theme_service->BuildLabel(UTF16ToUTF8(elided_name),
ui::kGdkBlack);
if (item_.active) {
char* markup = g_markup_printf_escaped(
"<span weight='bold'>%s</span>", UTF16ToUTF8(elided_name).c_str());
gtk_label_set_markup(GTK_LABEL(name_label), markup);
}
gtk_misc_set_alignment(GTK_MISC(name_label), 0, 0);
gtk_box_pack_start(GTK_BOX(item_vbox), name_label, TRUE, TRUE, 0);
// The sync status label.
status_label_ = theme_service_->BuildLabel(std::string(), ui::kGdkBlack);
char* markup = g_markup_printf_escaped(
"<span size='small'>%s</span>", UTF16ToUTF8(item_.sync_state).c_str());
gtk_label_set_markup(GTK_LABEL(status_label_), markup);
g_free(markup);
gtk_misc_set_alignment(GTK_MISC(status_label_), 0, 0);
gtk_widget_set_no_show_all(status_label_, TRUE);
gtk_box_pack_start(GTK_BOX(item_vbox), status_label_, FALSE, FALSE, 0);
gtk_widget_show(status_label_);
if (item_.active) {
// The "edit your profile" link.
edit_profile_link_ = theme_service_->BuildChromeLinkButton(
l10n_util::GetStringUTF8(IDS_PROFILES_EDIT_PROFILE_LINK));
// Fix for bug#107348. edit link steals focus from menu item which
// hides edit link button in focus-out-event handler,
// so, it misses the click event.
gtk_widget_set_can_focus(edit_profile_link_, FALSE);
link_alignment_ = gtk_alignment_new(0, 0, 0, 0);
gtk_container_add(GTK_CONTAINER(link_alignment_), edit_profile_link_);
// The chrome link button contains a label that won't be shown if the button
// is set to "no show all", so show all first.
gtk_widget_show_all(link_alignment_);
gtk_widget_set_no_show_all(link_alignment_, TRUE);
gtk_widget_hide(link_alignment_);
gtk_box_pack_start(GTK_BOX(item_vbox), link_alignment_, FALSE, FALSE, 0);
g_signal_connect(edit_profile_link_, "clicked",
G_CALLBACK(OnEditProfileLinkClickedThunk), this);
GtkSizeGroup* size_group = gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
gtk_size_group_add_widget(size_group, status_label_);
gtk_size_group_add_widget(size_group, link_alignment_);
g_object_unref(size_group);
}
gtk_box_pack_start(GTK_BOX(item_hbox), item_vbox, TRUE, TRUE, 0);
gtk_container_add(GTK_CONTAINER(widget_.get()), item_hbox);
}
|