Home | History | Annotate | Download | only in gtk
      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/status_bubble_gtk.h"
      6 
      7 #include <gtk/gtk.h>
      8 
      9 #include <algorithm>
     10 
     11 #include "base/i18n/rtl.h"
     12 #include "base/message_loop/message_loop.h"
     13 #include "base/strings/utf_string_conversions.h"
     14 #include "chrome/browser/chrome_notification_types.h"
     15 #include "chrome/browser/themes/theme_properties.h"
     16 #include "chrome/browser/ui/gtk/gtk_theme_service.h"
     17 #include "chrome/browser/ui/gtk/gtk_util.h"
     18 #include "chrome/browser/ui/gtk/rounded_window.h"
     19 #include "content/public/browser/notification_source.h"
     20 #include "ui/base/animation/slide_animation.h"
     21 #include "ui/base/gtk/gtk_compat.h"
     22 #include "ui/base/gtk/gtk_hig_constants.h"
     23 #include "ui/base/text/text_elider.h"
     24 
     25 namespace {
     26 
     27 // Inner padding between the border and the text label.
     28 const int kInternalTopBottomPadding = 1;
     29 const int kInternalLeftRightPadding = 2;
     30 
     31 // The radius of the edges of our bubble.
     32 const int kCornerSize = 3;
     33 
     34 // Milliseconds before we hide the status bubble widget when you mouseout.
     35 const int kHideDelay = 250;
     36 
     37 // How close the mouse can get to the infobubble before it starts sliding
     38 // off-screen.
     39 const int kMousePadding = 20;
     40 
     41 }  // namespace
     42 
     43 StatusBubbleGtk::StatusBubbleGtk(Profile* profile)
     44     : theme_service_(GtkThemeService::GetFrom(profile)),
     45       padding_(NULL),
     46       start_width_(0),
     47       desired_width_(0),
     48       flip_horizontally_(false),
     49       y_offset_(0),
     50       download_shelf_is_visible_(false),
     51       last_mouse_left_content_(false),
     52       ignore_next_left_content_(false) {
     53   InitWidgets();
     54 
     55   theme_service_->InitThemesFor(this);
     56   registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
     57                  content::Source<ThemeService>(theme_service_));
     58 }
     59 
     60 StatusBubbleGtk::~StatusBubbleGtk() {
     61   label_.Destroy();
     62   container_.Destroy();
     63 }
     64 
     65 void StatusBubbleGtk::SetStatus(const string16& status_text_wide) {
     66   std::string status_text = UTF16ToUTF8(status_text_wide);
     67   if (status_text_ == status_text)
     68     return;
     69 
     70   status_text_ = status_text;
     71   if (!status_text_.empty())
     72     SetStatusTextTo(status_text_);
     73   else if (!url_text_.empty())
     74     SetStatusTextTo(url_text_);
     75   else
     76     SetStatusTextTo(std::string());
     77 }
     78 
     79 void StatusBubbleGtk::SetURL(const GURL& url, const std::string& languages) {
     80   url_ = url;
     81   languages_ = languages;
     82 
     83   // If we want to clear a displayed URL but there is a status still to
     84   // display, display that status instead.
     85   if (url.is_empty() && !status_text_.empty()) {
     86     url_text_ = std::string();
     87     SetStatusTextTo(status_text_);
     88     return;
     89   }
     90 
     91   SetStatusTextToURL();
     92 }
     93 
     94 void StatusBubbleGtk::SetStatusTextToURL() {
     95   GtkWidget* parent = gtk_widget_get_parent(container_.get());
     96 
     97   // It appears that parent can be NULL (probably only during shutdown).
     98   if (!parent || !gtk_widget_get_realized(parent))
     99     return;
    100 
    101   GtkAllocation allocation;
    102   gtk_widget_get_allocation(parent, &allocation);
    103   int desired_width = allocation.width;
    104   if (!expanded()) {
    105     expand_timer_.Stop();
    106     expand_timer_.Start(FROM_HERE,
    107                         base::TimeDelta::FromMilliseconds(kExpandHoverDelay),
    108                         this, &StatusBubbleGtk::ExpandURL);
    109     // When not expanded, we limit the size to one third the browser's
    110     // width.
    111     desired_width /= 3;
    112   }
    113 
    114   // TODO(tc): We don't actually use gfx::Font as the font in the status
    115   // bubble.  We should extend ui::ElideUrl to take some sort of pango font.
    116   url_text_ = UTF16ToUTF8(
    117       ui::ElideUrl(url_, gfx::Font(), desired_width, languages_));
    118   SetStatusTextTo(url_text_);
    119 }
    120 
    121 void StatusBubbleGtk::Show() {
    122   // If we were going to hide, stop.
    123   hide_timer_.Stop();
    124 
    125   gtk_widget_show(container_.get());
    126   GdkWindow* gdk_window = gtk_widget_get_window(container_.get());
    127   if (gdk_window)
    128     gdk_window_raise(gdk_window);
    129 }
    130 
    131 void StatusBubbleGtk::Hide() {
    132   // If we were going to expand the bubble, stop.
    133   expand_timer_.Stop();
    134   expand_animation_.reset();
    135 
    136   gtk_widget_hide(container_.get());
    137 }
    138 
    139 void StatusBubbleGtk::SetStatusTextTo(const std::string& status_utf8) {
    140   if (status_utf8.empty()) {
    141     hide_timer_.Stop();
    142     hide_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kHideDelay),
    143                       this, &StatusBubbleGtk::Hide);
    144   } else {
    145     gtk_label_set_text(GTK_LABEL(label_.get()), status_utf8.c_str());
    146     GtkRequisition req;
    147     gtk_widget_size_request(label_.get(), &req);
    148     desired_width_ = req.width;
    149 
    150     UpdateLabelSizeRequest();
    151 
    152     if (!last_mouse_left_content_) {
    153       // Show the padding and label to update our requisition and then
    154       // re-process the last mouse event -- if the label was empty before or the
    155       // text changed, our size will have changed and we may need to move
    156       // ourselves away from the pointer now.
    157       gtk_widget_show_all(padding_);
    158       MouseMoved(last_mouse_location_, false);
    159     }
    160     Show();
    161   }
    162 }
    163 
    164 void StatusBubbleGtk::MouseMoved(
    165     const gfx::Point& location, bool left_content) {
    166   if (left_content && ignore_next_left_content_) {
    167     ignore_next_left_content_ = false;
    168     return;
    169   }
    170 
    171   last_mouse_location_ = location;
    172   last_mouse_left_content_ = left_content;
    173 
    174   if (!gtk_widget_get_realized(container_.get()))
    175     return;
    176 
    177   GtkWidget* parent = gtk_widget_get_parent(container_.get());
    178   if (!parent || !gtk_widget_get_realized(parent))
    179     return;
    180 
    181   int old_y_offset = y_offset_;
    182   bool old_flip_horizontally = flip_horizontally_;
    183 
    184   if (left_content) {
    185     SetFlipHorizontally(false);
    186     y_offset_ = 0;
    187   } else {
    188     GtkWidget* toplevel = gtk_widget_get_toplevel(container_.get());
    189     if (!toplevel || !gtk_widget_get_realized(toplevel))
    190       return;
    191 
    192     bool ltr = !base::i18n::IsRTL();
    193 
    194     GtkRequisition requisition;
    195     gtk_widget_size_request(container_.get(), &requisition);
    196 
    197     GtkAllocation parent_allocation;
    198     gtk_widget_get_allocation(parent, &parent_allocation);
    199 
    200     // Get our base position (that is, not including the current offset)
    201     // relative to the origin of the root window.
    202     gint toplevel_x = 0, toplevel_y = 0;
    203     GdkWindow* gdk_window = gtk_widget_get_window(toplevel);
    204     gdk_window_get_position(gdk_window, &toplevel_x, &toplevel_y);
    205     gfx::Rect parent_rect =
    206         gtk_util::GetWidgetRectRelativeToToplevel(parent);
    207     gfx::Rect bubble_rect(
    208         toplevel_x + parent_rect.x() +
    209             (ltr ? 0 : parent_allocation.width - requisition.width),
    210         toplevel_y + parent_rect.y() +
    211             parent_allocation.height - requisition.height,
    212         requisition.width,
    213         requisition.height);
    214 
    215     int left_threshold =
    216         bubble_rect.x() - bubble_rect.height() - kMousePadding;
    217     int right_threshold =
    218         bubble_rect.right() + bubble_rect.height() + kMousePadding;
    219     int top_threshold = bubble_rect.y() - kMousePadding;
    220 
    221     if (((ltr && location.x() < right_threshold) ||
    222          (!ltr && location.x() > left_threshold)) &&
    223         location.y() > top_threshold) {
    224       if (download_shelf_is_visible_) {
    225         SetFlipHorizontally(true);
    226         y_offset_ = 0;
    227       } else {
    228         SetFlipHorizontally(false);
    229         int distance = std::max(ltr ?
    230                                     location.x() - right_threshold :
    231                                     left_threshold - location.x(),
    232                                 top_threshold - location.y());
    233         y_offset_ = std::min(-1 * distance, requisition.height);
    234       }
    235     } else {
    236       SetFlipHorizontally(false);
    237       y_offset_ = 0;
    238     }
    239   }
    240 
    241   if (y_offset_ != old_y_offset || flip_horizontally_ != old_flip_horizontally)
    242     gtk_widget_queue_resize_no_redraw(parent);
    243 }
    244 
    245 void StatusBubbleGtk::UpdateDownloadShelfVisibility(bool visible) {
    246   download_shelf_is_visible_ = visible;
    247 }
    248 
    249 void StatusBubbleGtk::Observe(int type,
    250                               const content::NotificationSource& source,
    251                               const content::NotificationDetails& details) {
    252   if (type == chrome::NOTIFICATION_BROWSER_THEME_CHANGED) {
    253     UserChangedTheme();
    254   }
    255 }
    256 
    257 void StatusBubbleGtk::InitWidgets() {
    258   bool ltr = !base::i18n::IsRTL();
    259 
    260   label_.Own(gtk_label_new(NULL));
    261 
    262   padding_ = gtk_alignment_new(0, 0, 1, 1);
    263   gtk_alignment_set_padding(GTK_ALIGNMENT(padding_),
    264       kInternalTopBottomPadding, kInternalTopBottomPadding,
    265       kInternalLeftRightPadding + (ltr ? 0 : kCornerSize),
    266       kInternalLeftRightPadding + (ltr ? kCornerSize : 0));
    267   gtk_container_add(GTK_CONTAINER(padding_), label_.get());
    268   gtk_widget_show_all(padding_);
    269 
    270   container_.Own(gtk_event_box_new());
    271   gtk_widget_set_no_show_all(container_.get(), TRUE);
    272   gtk_util::ActAsRoundedWindow(
    273       container_.get(), ui::kGdkWhite, kCornerSize,
    274       gtk_util::ROUNDED_TOP_RIGHT,
    275       gtk_util::BORDER_TOP | gtk_util::BORDER_RIGHT);
    276   gtk_widget_set_name(container_.get(), "status-bubble");
    277   gtk_container_add(GTK_CONTAINER(container_.get()), padding_);
    278 
    279   // We need to listen for mouse motion events, since a fast-moving pointer may
    280   // enter our window without us getting any motion events on the browser near
    281   // enough for us to run away.
    282   gtk_widget_add_events(container_.get(), GDK_POINTER_MOTION_MASK |
    283                                           GDK_ENTER_NOTIFY_MASK);
    284   g_signal_connect(container_.get(), "motion-notify-event",
    285                    G_CALLBACK(HandleMotionNotifyThunk), this);
    286   g_signal_connect(container_.get(), "enter-notify-event",
    287                    G_CALLBACK(HandleEnterNotifyThunk), this);
    288 
    289   UserChangedTheme();
    290 }
    291 
    292 void StatusBubbleGtk::UserChangedTheme() {
    293   if (theme_service_->UsingNativeTheme()) {
    294     gtk_widget_modify_fg(label_.get(), GTK_STATE_NORMAL, NULL);
    295     gtk_widget_modify_bg(container_.get(), GTK_STATE_NORMAL, NULL);
    296   } else {
    297     // TODO(erg): This is the closest to "text that will look good on a
    298     // toolbar" that I can find. Maybe in later iterations of the theme system,
    299     // there will be a better color to pick.
    300     GdkColor bookmark_text =
    301         theme_service_->GetGdkColor(ThemeProperties::COLOR_BOOKMARK_TEXT);
    302     gtk_widget_modify_fg(label_.get(), GTK_STATE_NORMAL, &bookmark_text);
    303 
    304     GdkColor toolbar_color =
    305         theme_service_->GetGdkColor(ThemeProperties::COLOR_TOOLBAR);
    306     gtk_widget_modify_bg(container_.get(), GTK_STATE_NORMAL, &toolbar_color);
    307   }
    308 
    309   gtk_util::SetRoundedWindowBorderColor(container_.get(),
    310                                         theme_service_->GetBorderColor());
    311 }
    312 
    313 void StatusBubbleGtk::SetFlipHorizontally(bool flip_horizontally) {
    314   if (flip_horizontally == flip_horizontally_)
    315     return;
    316 
    317   flip_horizontally_ = flip_horizontally;
    318 
    319   bool ltr = !base::i18n::IsRTL();
    320   bool on_left = (ltr && !flip_horizontally) || (!ltr && flip_horizontally);
    321 
    322   gtk_alignment_set_padding(GTK_ALIGNMENT(padding_),
    323       kInternalTopBottomPadding, kInternalTopBottomPadding,
    324       kInternalLeftRightPadding + (on_left ? 0 : kCornerSize),
    325       kInternalLeftRightPadding + (on_left ? kCornerSize : 0));
    326   // The rounded window code flips these arguments if we're RTL.
    327   gtk_util::SetRoundedWindowEdgesAndBorders(
    328       container_.get(),
    329       kCornerSize,
    330       flip_horizontally ?
    331           gtk_util::ROUNDED_TOP_LEFT :
    332           gtk_util::ROUNDED_TOP_RIGHT,
    333       gtk_util::BORDER_TOP |
    334           (flip_horizontally ? gtk_util::BORDER_LEFT : gtk_util::BORDER_RIGHT));
    335   gtk_widget_queue_draw(container_.get());
    336 }
    337 
    338 void StatusBubbleGtk::ExpandURL() {
    339   GtkAllocation allocation;
    340   gtk_widget_get_allocation(label_.get(), &allocation);
    341   start_width_ = allocation.width;
    342   expand_animation_.reset(new ui::SlideAnimation(this));
    343   expand_animation_->SetTweenType(ui::Tween::LINEAR);
    344   expand_animation_->Show();
    345 
    346   SetStatusTextToURL();
    347 }
    348 
    349 void StatusBubbleGtk::UpdateLabelSizeRequest() {
    350   if (!expanded() || !expand_animation_->is_animating()) {
    351     gtk_widget_set_size_request(label_.get(), -1, -1);
    352     return;
    353   }
    354 
    355   int new_width = start_width_ +
    356       (desired_width_ - start_width_) * expand_animation_->GetCurrentValue();
    357   gtk_widget_set_size_request(label_.get(), new_width, -1);
    358 }
    359 
    360 // See http://crbug.com/68897 for why we have to handle this event.
    361 gboolean StatusBubbleGtk::HandleEnterNotify(GtkWidget* sender,
    362                                             GdkEventCrossing* event) {
    363   ignore_next_left_content_ = true;
    364   MouseMoved(gfx::Point(event->x_root, event->y_root), false);
    365   return FALSE;
    366 }
    367 
    368 gboolean StatusBubbleGtk::HandleMotionNotify(GtkWidget* sender,
    369                                              GdkEventMotion* event) {
    370   MouseMoved(gfx::Point(event->x_root, event->y_root), false);
    371   return FALSE;
    372 }
    373 
    374 void StatusBubbleGtk::AnimationEnded(const ui::Animation* animation) {
    375   UpdateLabelSizeRequest();
    376 }
    377 
    378 void StatusBubbleGtk::AnimationProgressed(const ui::Animation* animation) {
    379   UpdateLabelSizeRequest();
    380 }
    381