summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/status_bubble_gtk.cc
blob: 5195135765504508c952090f2a717286310d1374 (plain)
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
// Copyright (c) 2009 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/gtk/status_bubble_gtk.h"

#include <gtk/gtk.h>

#include "base/gfx/gtk_util.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "chrome/browser/gtk/slide_animator_gtk.h"
#include "chrome/common/gtk_util.h"
#include "googleurl/src/gurl.h"

namespace {

const GdkColor kBackgroundColor = GDK_COLOR_RGB(0xe6, 0xed, 0xf4);
const GdkColor kFrameBorderColor = GDK_COLOR_RGB(0xbe, 0xc8, 0xd4);

// Inner padding between the border and the text label.
const int kInternalTopBottomPadding = 1;
const int kInternalLeftRightPadding = 2;

// Border of color kFrameBorderColor around the status bubble.
const int kBorderPadding = 1;

// Milliseconds before we hide the status bubble widget when you mouseout.
static const int kHideDelay = 250;

}  // namespace

StatusBubbleGtk::StatusBubbleGtk()
    : parent_(NULL),
      timer_factory_(this) {
  InitWidgets();
}

StatusBubbleGtk::~StatusBubbleGtk() {
  container_.Destroy();
}

void StatusBubbleGtk::SetStatus(const std::string& status) {
  if (status.empty()) {
    HideInASecond();
    return;
  }

  gtk_label_set_text(GTK_LABEL(label_), status.c_str());

  Show();
}

void StatusBubbleGtk::SetStatus(const std::wstring& status) {
  SetStatus(WideToUTF8(status));
}

void StatusBubbleGtk::SetParentAllocation(
    GtkWidget* parent, GtkAllocation* allocation) {
  parent_ = parent;
  parent_allocation_ = *allocation;
  SetStatusBubbleSize();
}

void StatusBubbleGtk::SetURL(const GURL& url, const std::wstring& languages) {
  SetStatus(url.possibly_invalid_spec());
}

void StatusBubbleGtk::Show() {
  // If we were going to hide, stop.
  timer_factory_.RevokeAll();

  SetStatusBubbleSize();
  gtk_widget_show_all(container_.get());

  if (container_.get()->window)
    gdk_window_raise(container_.get()->window);
}

void StatusBubbleGtk::Hide() {
  gtk_widget_hide_all(container_.get());
}

void StatusBubbleGtk::HideInASecond() {
  if (!timer_factory_.empty())
    timer_factory_.RevokeAll();

  MessageLoop::current()->PostDelayedTask(FROM_HERE,
      timer_factory_.NewRunnableMethod(&StatusBubbleGtk::Hide),
      kHideDelay);
}

void StatusBubbleGtk::SetStatusBubbleSize() {
  if (parent_) {
    GtkRequisition requisition;
    gtk_widget_size_request(container_.get(), &requisition);

    // TODO(erg): Previously, I put a call to gtk_fixed_put() here. It appears
    // that doing this sets off a size-allocate storm, since gtk_fixed_put()
    // calls gtk_widget_queue_resize on the GtkFixed that caused this message.
    // The real solution may be creating a subclass of GtkVBox that has extra
    // code to deal with floating widgets, but this hack is good enough for
    // Friday. evanm says that there's a a GtkFixed subclass in test_shell that
    // we'll be stealing for plugin support anyway that should also do the same
    // task.

    GtkAllocation widget_allocation;
    int child_y = std::max(
        parent_allocation_.y + parent_allocation_.height - requisition.height,
        0);
    widget_allocation.x = 0;
    widget_allocation.y = child_y;
    widget_allocation.width = std::max(1, std::min(requisition.width,
                                                   parent_allocation_.width));
    widget_allocation.height = std::max(1, requisition.height);

    if (memcmp(&widget_allocation, &container_.get()->allocation,
        sizeof widget_allocation) != 0) {
      // Only do something when we are actually changing sizes.
      gtk_widget_size_allocate(container_.get(), &widget_allocation);
    }
  }
}

void StatusBubbleGtk::MouseMoved() {
  // We can't do that fancy sliding behaviour where the status bubble slides
  // out of the window because the window manager gets in the way. So totally
  // ignore this message for now.
  //
  // TODO(erg): At least get some sliding behaviour so that it slides out of
  // the way to hide the status bubble on mouseover.
}

void StatusBubbleGtk::InitWidgets() {
  label_ = gtk_label_new(NULL);

  GtkWidget* padding = gtk_alignment_new(0, 0, 1, 1);
  gtk_alignment_set_padding(GTK_ALIGNMENT(padding),
      kInternalTopBottomPadding, kInternalTopBottomPadding,
      kInternalLeftRightPadding, kInternalLeftRightPadding);
  gtk_container_add(GTK_CONTAINER(padding), label_);

  GtkWidget* bg_box = gtk_event_box_new();
  gtk_container_add(GTK_CONTAINER(bg_box), padding);
  gtk_widget_modify_bg(bg_box, GTK_STATE_NORMAL, &kBackgroundColor);

  container_.Own(gtk_util::CreateGtkBorderBin(bg_box, &kFrameBorderColor,
          kBorderPadding, kBorderPadding, kBorderPadding, kBorderPadding));
  gtk_widget_set_name(container_.get(), "status-bubble");
  gtk_widget_set_app_paintable(container_.get(), TRUE);
}