OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/gtk/status_bubble_gtk.h" | 5 #include "chrome/browser/gtk/status_bubble_gtk.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 #include "base/gfx/gtk_util.h" | 9 #include "base/gfx/gtk_util.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 // Border of color kFrameBorderColor around the status bubble. | 26 // Border of color kFrameBorderColor around the status bubble. |
27 const int kBorderPadding = 1; | 27 const int kBorderPadding = 1; |
28 | 28 |
29 // Milliseconds before we hide the status bubble widget when you mouseout. | 29 // Milliseconds before we hide the status bubble widget when you mouseout. |
30 static const int kHideDelay = 250; | 30 static const int kHideDelay = 250; |
31 | 31 |
32 } // namespace | 32 } // namespace |
33 | 33 |
34 StatusBubbleGtk::StatusBubbleGtk() | 34 StatusBubbleGtk::StatusBubbleGtk() |
35 : parent_(NULL), | 35 : timer_factory_(this) { |
36 timer_factory_(this) { | |
37 InitWidgets(); | 36 InitWidgets(); |
38 } | 37 } |
39 | 38 |
40 StatusBubbleGtk::~StatusBubbleGtk() { | 39 StatusBubbleGtk::~StatusBubbleGtk() { |
41 container_.Destroy(); | 40 container_.Destroy(); |
42 } | 41 } |
43 | 42 |
44 void StatusBubbleGtk::SetStatus(const std::string& status) { | 43 void StatusBubbleGtk::SetStatus(const std::string& status) { |
45 if (status.empty()) { | 44 if (status.empty()) { |
46 HideInASecond(); | 45 HideInASecond(); |
47 return; | 46 return; |
48 } | 47 } |
49 | 48 |
50 gtk_label_set_text(GTK_LABEL(label_), status.c_str()); | 49 gtk_label_set_text(GTK_LABEL(label_), status.c_str()); |
51 | 50 |
52 Show(); | 51 Show(); |
53 } | 52 } |
54 | 53 |
55 void StatusBubbleGtk::SetStatus(const std::wstring& status) { | 54 void StatusBubbleGtk::SetStatus(const std::wstring& status) { |
56 SetStatus(WideToUTF8(status)); | 55 SetStatus(WideToUTF8(status)); |
57 } | 56 } |
58 | 57 |
59 void StatusBubbleGtk::SetParentAllocation( | |
60 GtkWidget* parent, GtkAllocation* allocation) { | |
61 parent_ = parent; | |
62 parent_allocation_ = *allocation; | |
63 SetStatusBubbleSize(); | |
64 } | |
65 | |
66 void StatusBubbleGtk::SetURL(const GURL& url, const std::wstring& languages) { | 58 void StatusBubbleGtk::SetURL(const GURL& url, const std::wstring& languages) { |
67 SetStatus(url.possibly_invalid_spec()); | 59 SetStatus(url.possibly_invalid_spec()); |
68 } | 60 } |
69 | 61 |
70 void StatusBubbleGtk::Show() { | 62 void StatusBubbleGtk::Show() { |
71 // If we were going to hide, stop. | 63 // If we were going to hide, stop. |
72 timer_factory_.RevokeAll(); | 64 timer_factory_.RevokeAll(); |
73 | 65 |
74 SetStatusBubbleSize(); | |
75 gtk_widget_show_all(container_.get()); | 66 gtk_widget_show_all(container_.get()); |
76 | 67 |
77 if (container_.get()->window) | 68 if (container_.get()->window) |
78 gdk_window_raise(container_.get()->window); | 69 gdk_window_raise(container_.get()->window); |
79 } | 70 } |
80 | 71 |
81 void StatusBubbleGtk::Hide() { | 72 void StatusBubbleGtk::Hide() { |
82 gtk_widget_hide_all(container_.get()); | 73 gtk_widget_hide_all(container_.get()); |
83 } | 74 } |
84 | 75 |
85 void StatusBubbleGtk::HideInASecond() { | 76 void StatusBubbleGtk::HideInASecond() { |
86 if (!timer_factory_.empty()) | 77 if (!timer_factory_.empty()) |
87 timer_factory_.RevokeAll(); | 78 timer_factory_.RevokeAll(); |
88 | 79 |
89 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 80 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
90 timer_factory_.NewRunnableMethod(&StatusBubbleGtk::Hide), | 81 timer_factory_.NewRunnableMethod(&StatusBubbleGtk::Hide), |
91 kHideDelay); | 82 kHideDelay); |
92 } | 83 } |
93 | 84 |
94 void StatusBubbleGtk::SetStatusBubbleSize() { | |
95 if (parent_) { | |
96 GtkRequisition requisition; | |
97 gtk_widget_size_request(container_.get(), &requisition); | |
98 | |
99 // TODO(erg): Previously, I put a call to gtk_fixed_put() here. It appears | |
100 // that doing this sets off a size-allocate storm, since gtk_fixed_put() | |
101 // calls gtk_widget_queue_resize on the GtkFixed that caused this message. | |
102 // The real solution may be creating a subclass of GtkVBox that has extra | |
103 // code to deal with floating widgets, but this hack is good enough for | |
104 // Friday. evanm says that there's a a GtkFixed subclass in test_shell that | |
105 // we'll be stealing for plugin support anyway that should also do the same | |
106 // task. | |
107 | |
108 GtkAllocation widget_allocation; | |
109 int child_y = std::max( | |
110 parent_allocation_.y + parent_allocation_.height - requisition.height, | |
111 0); | |
112 widget_allocation.x = 0; | |
113 widget_allocation.y = child_y; | |
114 widget_allocation.width = std::max(1, std::min(requisition.width, | |
115 parent_allocation_.width)); | |
116 widget_allocation.height = std::max(1, requisition.height); | |
117 | |
118 if (memcmp(&widget_allocation, &container_.get()->allocation, | |
119 sizeof widget_allocation) != 0) { | |
120 // Only do something when we are actually changing sizes. | |
121 gtk_widget_size_allocate(container_.get(), &widget_allocation); | |
122 } | |
123 } | |
124 } | |
125 | |
126 void StatusBubbleGtk::MouseMoved() { | 85 void StatusBubbleGtk::MouseMoved() { |
127 // We can't do that fancy sliding behaviour where the status bubble slides | 86 // We can't do that fancy sliding behaviour where the status bubble slides |
128 // out of the window because the window manager gets in the way. So totally | 87 // out of the window because the window manager gets in the way. So totally |
129 // ignore this message for now. | 88 // ignore this message for now. |
130 // | 89 // |
131 // TODO(erg): At least get some sliding behaviour so that it slides out of | 90 // TODO(erg): At least get some sliding behaviour so that it slides out of |
132 // the way to hide the status bubble on mouseover. | 91 // the way to hide the status bubble on mouseover. |
133 } | 92 } |
134 | 93 |
135 void StatusBubbleGtk::InitWidgets() { | 94 void StatusBubbleGtk::InitWidgets() { |
136 label_ = gtk_label_new(NULL); | 95 label_ = gtk_label_new(NULL); |
137 gtk_widget_modify_fg(label_, GTK_STATE_NORMAL, &kTextColor); | 96 gtk_widget_modify_fg(label_, GTK_STATE_NORMAL, &kTextColor); |
138 | 97 |
139 GtkWidget* padding = gtk_alignment_new(0, 0, 1, 1); | 98 GtkWidget* padding = gtk_alignment_new(0, 0, 1, 1); |
140 gtk_alignment_set_padding(GTK_ALIGNMENT(padding), | 99 gtk_alignment_set_padding(GTK_ALIGNMENT(padding), |
141 kInternalTopBottomPadding, kInternalTopBottomPadding, | 100 kInternalTopBottomPadding, kInternalTopBottomPadding, |
142 kInternalLeftRightPadding, kInternalLeftRightPadding); | 101 kInternalLeftRightPadding, kInternalLeftRightPadding); |
143 gtk_container_add(GTK_CONTAINER(padding), label_); | 102 gtk_container_add(GTK_CONTAINER(padding), label_); |
144 | 103 |
145 GtkWidget* bg_box = gtk_event_box_new(); | 104 GtkWidget* bg_box = gtk_event_box_new(); |
146 gtk_container_add(GTK_CONTAINER(bg_box), padding); | 105 gtk_container_add(GTK_CONTAINER(bg_box), padding); |
147 gtk_widget_modify_bg(bg_box, GTK_STATE_NORMAL, &kBackgroundColor); | 106 gtk_widget_modify_bg(bg_box, GTK_STATE_NORMAL, &kBackgroundColor); |
148 | 107 |
149 container_.Own(gtk_util::CreateGtkBorderBin(bg_box, &kFrameBorderColor, | 108 container_.Own(gtk_util::CreateGtkBorderBin(bg_box, &kFrameBorderColor, |
150 kBorderPadding, kBorderPadding, kBorderPadding, kBorderPadding)); | 109 kBorderPadding, kBorderPadding, kBorderPadding, kBorderPadding)); |
151 gtk_widget_set_name(container_.get(), "status-bubble"); | 110 gtk_widget_set_name(container_.get(), "status-bubble"); |
152 gtk_widget_set_app_paintable(container_.get(), TRUE); | 111 gtk_widget_set_app_paintable(container_.get(), TRUE); |
153 } | 112 } |
OLD | NEW |