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. 4 5 #include "chrome/browser/ui/gtk/avatar_menu_button_gtk.h" 6 7 #include "base/i18n/rtl.h" 8 #include "chrome/app/chrome_command_ids.h" 9 #include "chrome/browser/command_updater.h" 10 #include "chrome/browser/profiles/profile_metrics.h" 11 #include "chrome/browser/profiles/profile_info_util.h" 12 #include "chrome/browser/ui/browser.h" 13 #include "chrome/browser/ui/browser_commands.h" 14 #include "chrome/browser/ui/gtk/avatar_menu_bubble_gtk.h" 15 #include "chrome/browser/ui/gtk/bubble/bubble_gtk.h" 16 #include "ui/gfx/gtk_util.h" 17 18 AvatarMenuButtonGtk::AvatarMenuButtonGtk(Browser* browser) 19 : image_(NULL), 20 browser_(browser), 21 frame_style_(BubbleGtk::ANCHOR_TOP_LEFT), 22 is_gaia_picture_(false), 23 old_height_(0) { 24 GtkWidget* event_box = gtk_event_box_new(); 25 image_ = gtk_image_new(); 26 gtk_container_add(GTK_CONTAINER(event_box), image_); 27 28 g_signal_connect(event_box, "button-press-event", 29 G_CALLBACK(OnButtonPressedThunk), this); 30 g_signal_connect(event_box, "size-allocate", 31 G_CALLBACK(OnSizeAllocateThunk), this); 32 33 gtk_event_box_set_visible_window(GTK_EVENT_BOX(event_box), FALSE); 34 35 widget_.Own(event_box); 36 } 37 38 AvatarMenuButtonGtk::~AvatarMenuButtonGtk() { 39 } 40 41 void AvatarMenuButtonGtk::SetIcon(const gfx::Image& image, 42 bool is_gaia_picture) { 43 icon_.reset(new gfx::Image(image)); 44 is_gaia_picture_ = is_gaia_picture; 45 UpdateButtonIcon(); 46 } 47 48 gboolean AvatarMenuButtonGtk::OnButtonPressed(GtkWidget* widget, 49 GdkEventButton* event) { 50 if (event->button != 1) 51 return FALSE; 52 53 ShowAvatarBubble(); 54 ProfileMetrics::LogProfileOpenMethod(ProfileMetrics::ICON_AVATAR_BUBBLE); 55 return TRUE; 56 } 57 58 void AvatarMenuButtonGtk::OnSizeAllocate(GtkWidget* widget, 59 GtkAllocation* allocation) { 60 if (allocation->height != old_height_) 61 UpdateButtonIcon(); 62 } 63 64 void AvatarMenuButtonGtk::ShowAvatarBubble() { 65 DCHECK(chrome::IsCommandEnabled(browser_, IDC_SHOW_AVATAR_MENU)); 66 // Only show the avatar bubble if the avatar button is in the title bar. 67 if (gtk_widget_get_parent_window(widget_.get())) 68 new AvatarMenuBubbleGtk(browser_, widget_.get(), frame_style_, NULL); 69 } 70 71 void AvatarMenuButtonGtk::UpdateButtonIcon() { 72 if (!icon_.get()) 73 return; 74 75 GtkAllocation allocation; 76 gtk_widget_get_allocation(widget(), &allocation); 77 old_height_ = allocation.height; 78 79 // GAIA images are square; use kWidth for both destination height and width 80 // since old_height_ is often not usable (typically a value of 1 which, after 81 // subtracting border, tries to resize the profile image to a size of -1). 82 gfx::Image icon = profiles::GetAvatarIconForTitleBar( 83 *icon_, is_gaia_picture_, 84 profiles::kAvatarIconWidth, profiles::kAvatarIconWidth); 85 gtk_image_set_from_pixbuf(GTK_IMAGE(image_), icon.ToGdkPixbuf()); 86 gtk_misc_set_alignment(GTK_MISC(image_), 0.0, 1.0); 87 gtk_widget_set_size_request(image_, -1, 0); 88 } 89