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/bookmark_bar_gtk.h" | 5 #include "chrome/browser/gtk/bookmark_bar_gtk.h" |
6 | 6 |
7 #include "app/gfx/gtk_util.h" | 7 #include "app/gfx/gtk_util.h" |
8 #include "app/gfx/text_elider.h" | 8 #include "app/gfx/text_elider.h" |
9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
10 #include "app/resource_bundle.h" | 10 #include "app/resource_bundle.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "chrome/browser/profile.h" | 23 #include "chrome/browser/profile.h" |
24 #include "chrome/common/gtk_util.h" | 24 #include "chrome/common/gtk_util.h" |
25 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
26 #include "chrome/common/pref_service.h" | 26 #include "chrome/common/pref_service.h" |
27 #include "grit/app_resources.h" | 27 #include "grit/app_resources.h" |
28 #include "grit/generated_resources.h" | 28 #include "grit/generated_resources.h" |
29 #include "grit/theme_resources.h" | 29 #include "grit/theme_resources.h" |
30 | 30 |
31 namespace { | 31 namespace { |
32 | 32 |
| 33 // The height of the bar. |
| 34 const int kBookmarkBarHeight = 33; |
| 35 |
33 // Maximum number of characters on a bookmark button. | 36 // Maximum number of characters on a bookmark button. |
34 const size_t kMaxCharsOnAButton = 15; | 37 const size_t kMaxCharsOnAButton = 15; |
35 | 38 |
36 // Used in gtk_selection_data_set(). (I assume from this parameter that gtk has | 39 // Used in gtk_selection_data_set(). (I assume from this parameter that gtk has |
37 // to some really exotic hardware...) | 40 // to some really exotic hardware...) |
38 const int kBitsInAByte = 8; | 41 const int kBitsInAByte = 8; |
39 | 42 |
40 // Dictionary key used to store a BookmarkNode* on a GtkWidget. | 43 // Dictionary key used to store a BookmarkNode* on a GtkWidget. |
41 const char kBookmarkNode[] = "bookmark-node"; | 44 const char kBookmarkNode[] = "bookmark-node"; |
42 | 45 |
43 // Mime types for DnD. Used to synchronize across applications. | 46 // Mime types for DnD. Used to synchronize across applications. |
44 const char kInternalURIType[] = "application/x-chrome-bookmark-item"; | 47 const char kInternalURIType[] = "application/x-chrome-bookmark-item"; |
45 | 48 |
| 49 // Left-padding for the instructional text. |
| 50 const int kInstructionsPadding = 6; |
| 51 |
| 52 // Color of the instructional text. |
| 53 const GdkColor kInstructionsColor = GDK_COLOR_RGB(128, 128, 142); |
| 54 |
46 // Table of the mime types that we accept with their options. | 55 // Table of the mime types that we accept with their options. |
47 const GtkTargetEntry kTargetTable[] = { | 56 const GtkTargetEntry kTargetTable[] = { |
48 { const_cast<char*>(kInternalURIType), GTK_TARGET_SAME_APP, | 57 { const_cast<char*>(kInternalURIType), GTK_TARGET_SAME_APP, |
49 dnd::X_CHROME_BOOKMARK_ITEM } | 58 dnd::X_CHROME_BOOKMARK_ITEM } |
50 // TODO(erg): Add "text/uri-list" support. | 59 // TODO(erg): Add "text/uri-list" support. |
51 }; | 60 }; |
52 | 61 |
53 const int kTargetTableSize = G_N_ELEMENTS(kTargetTable); | 62 const int kTargetTableSize = G_N_ELEMENTS(kTargetTable); |
54 | 63 |
55 } // namespace | 64 } // namespace |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 page_navigator_ = navigator; | 112 page_navigator_ = navigator; |
104 } | 113 } |
105 | 114 |
106 void BookmarkBarGtk::Init(Profile* profile) { | 115 void BookmarkBarGtk::Init(Profile* profile) { |
107 // Load the default images from the resource bundle. | 116 // Load the default images from the resource bundle. |
108 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 117 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
109 static GdkPixbuf* folder_icon = rb.GetPixbufNamed(IDR_BOOKMARK_BAR_FOLDER); | 118 static GdkPixbuf* folder_icon = rb.GetPixbufNamed(IDR_BOOKMARK_BAR_FOLDER); |
110 | 119 |
111 bookmark_hbox_.Own(gtk_hbox_new(FALSE, 0)); | 120 bookmark_hbox_.Own(gtk_hbox_new(FALSE, 0)); |
112 | 121 |
113 instructions_ = gtk_label_new( | 122 instructions_ = gtk_alignment_new(0.0, 0.0, 1.0, 1.0); |
| 123 gtk_alignment_set_padding(GTK_ALIGNMENT(instructions_), 0, 0, |
| 124 kInstructionsPadding, 0); |
| 125 GtkWidget* instructions_label = gtk_label_new( |
114 l10n_util::GetStringUTF8(IDS_BOOKMARKS_NO_ITEMS).c_str()); | 126 l10n_util::GetStringUTF8(IDS_BOOKMARKS_NO_ITEMS).c_str()); |
| 127 gtk_widget_modify_fg(instructions_label, GTK_STATE_NORMAL, |
| 128 &kInstructionsColor); |
| 129 gtk_container_add(GTK_CONTAINER(instructions_), instructions_label); |
115 gtk_box_pack_start(GTK_BOX(bookmark_hbox_.get()), instructions_, | 130 gtk_box_pack_start(GTK_BOX(bookmark_hbox_.get()), instructions_, |
116 FALSE, FALSE, 0); | 131 FALSE, FALSE, 0); |
| 132 |
117 gtk_widget_set_app_paintable(bookmark_hbox_.get(), TRUE); | 133 gtk_widget_set_app_paintable(bookmark_hbox_.get(), TRUE); |
118 g_signal_connect(G_OBJECT(bookmark_hbox_.get()), "expose-event", | 134 g_signal_connect(G_OBJECT(bookmark_hbox_.get()), "expose-event", |
119 G_CALLBACK(&OnHBoxExpose), this); | 135 G_CALLBACK(&OnHBoxExpose), this); |
120 | 136 |
121 bookmark_toolbar_.Own(gtk_toolbar_new()); | 137 bookmark_toolbar_.Own(gtk_toolbar_new()); |
122 gtk_widget_set_app_paintable(bookmark_toolbar_.get(), TRUE); | 138 gtk_widget_set_app_paintable(bookmark_toolbar_.get(), TRUE); |
123 g_signal_connect(G_OBJECT(bookmark_toolbar_.get()), "expose-event", | 139 g_signal_connect(G_OBJECT(bookmark_toolbar_.get()), "expose-event", |
124 G_CALLBACK(&OnToolbarExpose), this); | 140 G_CALLBACK(&OnToolbarExpose), this); |
125 gtk_box_pack_start(GTK_BOX(bookmark_hbox_.get()), bookmark_toolbar_.get(), | 141 gtk_box_pack_start(GTK_BOX(bookmark_hbox_.get()), bookmark_toolbar_.get(), |
126 TRUE, TRUE, 0); | 142 TRUE, TRUE, 0); |
(...skipping 18 matching lines...) Expand all Loading... |
145 other_bookmarks_button_ = gtk_chrome_button_new(); | 161 other_bookmarks_button_ = gtk_chrome_button_new(); |
146 ConnectFolderButtonEvents(other_bookmarks_button_); | 162 ConnectFolderButtonEvents(other_bookmarks_button_); |
147 gtk_button_set_label( | 163 gtk_button_set_label( |
148 GTK_BUTTON(other_bookmarks_button_), | 164 GTK_BUTTON(other_bookmarks_button_), |
149 l10n_util::GetStringUTF8(IDS_BOOMARK_BAR_OTHER_BOOKMARKED).c_str()); | 165 l10n_util::GetStringUTF8(IDS_BOOMARK_BAR_OTHER_BOOKMARKED).c_str()); |
150 gtk_button_set_image(GTK_BUTTON(other_bookmarks_button_), | 166 gtk_button_set_image(GTK_BUTTON(other_bookmarks_button_), |
151 gtk_image_new_from_pixbuf(folder_icon)); | 167 gtk_image_new_from_pixbuf(folder_icon)); |
152 | 168 |
153 gtk_box_pack_start(GTK_BOX(bookmark_hbox_.get()), other_bookmarks_button_, | 169 gtk_box_pack_start(GTK_BOX(bookmark_hbox_.get()), other_bookmarks_button_, |
154 FALSE, FALSE, 0); | 170 FALSE, FALSE, 0); |
| 171 gtk_widget_set_size_request(bookmark_hbox_.get(), -1, 0); |
| 172 |
| 173 slide_animation_.reset(new SlideAnimation(this)); |
155 } | 174 } |
156 | 175 |
157 void BookmarkBarGtk::AddBookmarkbarToBox(GtkWidget* box) { | 176 void BookmarkBarGtk::AddBookmarkbarToBox(GtkWidget* box) { |
158 gtk_box_pack_start(GTK_BOX(box), bookmark_hbox_.get(), FALSE, FALSE, 0); | 177 gtk_box_pack_start(GTK_BOX(box), bookmark_hbox_.get(), FALSE, FALSE, 0); |
159 } | 178 } |
160 | 179 |
161 void BookmarkBarGtk::Show() { | 180 void BookmarkBarGtk::Show() { |
162 gtk_widget_show_all(bookmark_hbox_.get()); | 181 gtk_widget_show_all(bookmark_hbox_.get()); |
| 182 slide_animation_->Show(); |
163 | 183 |
164 // Maybe show the instructions | 184 // Maybe show the instructions |
165 if (show_instructions_) { | 185 if (show_instructions_) { |
166 gtk_widget_show(instructions_); | 186 gtk_widget_show(instructions_); |
167 } else { | 187 } else { |
168 gtk_widget_hide(instructions_); | 188 gtk_widget_hide(instructions_); |
169 } | 189 } |
170 } | 190 } |
171 | 191 |
172 void BookmarkBarGtk::Hide() { | 192 void BookmarkBarGtk::Hide() { |
173 gtk_widget_hide_all(bookmark_hbox_.get()); | 193 // Sometimes we get called without a matching call to open. If that happens |
| 194 // then force hide. |
| 195 if (slide_animation_->IsShowing()) |
| 196 slide_animation_->Hide(); |
| 197 else |
| 198 gtk_widget_hide(bookmark_hbox_.get()); |
174 } | 199 } |
175 | 200 |
176 bool BookmarkBarGtk::OnNewTabPage() { | 201 bool BookmarkBarGtk::OnNewTabPage() { |
177 return (browser_ && browser_->GetSelectedTabContents() && | 202 return (browser_ && browser_->GetSelectedTabContents() && |
178 browser_->GetSelectedTabContents()->IsBookmarkBarAlwaysVisible()); | 203 browser_->GetSelectedTabContents()->IsBookmarkBarAlwaysVisible()); |
179 } | 204 } |
180 | 205 |
181 void BookmarkBarGtk::Loaded(BookmarkModel* model) { | 206 void BookmarkBarGtk::Loaded(BookmarkModel* model) { |
182 RemoveAllBookmarkButtons(); | 207 RemoveAllBookmarkButtons(); |
183 | 208 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 GtkToolItem* item = CreateBookmarkToolItem(node->GetChild(i)); | 301 GtkToolItem* item = CreateBookmarkToolItem(node->GetChild(i)); |
277 gtk_toolbar_insert(GTK_TOOLBAR(bookmark_toolbar_.get()), item, -1); | 302 gtk_toolbar_insert(GTK_TOOLBAR(bookmark_toolbar_.get()), item, -1); |
278 } | 303 } |
279 | 304 |
280 SetInstructionState(node); | 305 SetInstructionState(node); |
281 } | 306 } |
282 | 307 |
283 void BookmarkBarGtk::SetInstructionState(BookmarkNode* boomarks_bar_node) { | 308 void BookmarkBarGtk::SetInstructionState(BookmarkNode* boomarks_bar_node) { |
284 show_instructions_ = (boomarks_bar_node->GetChildCount() == 0); | 309 show_instructions_ = (boomarks_bar_node->GetChildCount() == 0); |
285 if (show_instructions_) { | 310 if (show_instructions_) { |
286 gtk_widget_show(instructions_); | 311 gtk_widget_show_all(instructions_); |
287 } else { | 312 } else { |
288 gtk_widget_hide(instructions_); | 313 gtk_widget_hide(instructions_); |
289 } | 314 } |
290 } | 315 } |
291 | 316 |
292 void BookmarkBarGtk::RemoveAllBookmarkButtons() { | 317 void BookmarkBarGtk::RemoveAllBookmarkButtons() { |
293 gtk_util::RemoveAllChildren(bookmark_toolbar_.get()); | 318 gtk_util::RemoveAllChildren(bookmark_toolbar_.get()); |
294 } | 319 } |
295 | 320 |
296 int BookmarkBarGtk::GetBookmarkButtonCount() { | 321 int BookmarkBarGtk::GetBookmarkButtonCount() { |
297 GList* children = gtk_container_get_children( | 322 GList* children = gtk_container_get_children( |
298 GTK_CONTAINER(bookmark_toolbar_.get())); | 323 GTK_CONTAINER(bookmark_toolbar_.get())); |
299 int count = g_list_length(children); | 324 int count = g_list_length(children); |
300 g_list_free(children); | 325 g_list_free(children); |
301 return count; | 326 return count; |
302 } | 327 } |
303 | 328 |
304 bool BookmarkBarGtk::IsAlwaysShown() { | 329 bool BookmarkBarGtk::IsAlwaysShown() { |
305 return profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); | 330 return profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); |
306 } | 331 } |
307 | 332 |
| 333 void BookmarkBarGtk::AnimationProgressed(const Animation* animation) { |
| 334 DCHECK_EQ(animation, slide_animation_.get()); |
| 335 |
| 336 gtk_widget_set_size_request(bookmark_hbox_.get(), -1, |
| 337 animation->GetCurrentValue() * |
| 338 kBookmarkBarHeight); |
| 339 } |
| 340 |
| 341 void BookmarkBarGtk::AnimationEnded(const Animation* animation) { |
| 342 DCHECK_EQ(animation, slide_animation_.get()); |
| 343 |
| 344 if (!slide_animation_->IsShowing()) |
| 345 gtk_widget_hide(bookmark_hbox_.get()); |
| 346 } |
| 347 |
308 void BookmarkBarGtk::ConfigureButtonForNode(BookmarkNode* node, | 348 void BookmarkBarGtk::ConfigureButtonForNode(BookmarkNode* node, |
309 GtkWidget* button) { | 349 GtkWidget* button) { |
310 std::string tooltip = BuildTooltip(node); | 350 std::string tooltip = BuildTooltip(node); |
311 if (!tooltip.empty()) | 351 if (!tooltip.empty()) |
312 gtk_widget_set_tooltip_text(button, tooltip.c_str()); | 352 gtk_widget_set_tooltip_text(button, tooltip.c_str()); |
313 | 353 |
314 // TODO(erg): Consider a soft maximum instead of this hard 15. | 354 // TODO(erg): Consider a soft maximum instead of this hard 15. |
315 std::wstring title = node->GetTitle(); | 355 std::wstring title = node->GetTitle(); |
316 title = title.substr(0, std::min(title.size(), kMaxCharsOnAButton)); | 356 title = title.substr(0, std::min(title.size(), kMaxCharsOnAButton)); |
317 gtk_button_set_label(GTK_BUTTON(button), WideToUTF8(title).c_str()); | 357 gtk_button_set_label(GTK_BUTTON(button), WideToUTF8(title).c_str()); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 // button. | 406 // button. |
367 g_signal_connect(G_OBJECT(button), "button-press-event", | 407 g_signal_connect(G_OBJECT(button), "button-press-event", |
368 G_CALLBACK(OnButtonPressed), this); | 408 G_CALLBACK(OnButtonPressed), this); |
369 g_signal_connect(G_OBJECT(button), "button-release-event", | 409 g_signal_connect(G_OBJECT(button), "button-release-event", |
370 G_CALLBACK(OnButtonReleased), this); | 410 G_CALLBACK(OnButtonReleased), this); |
371 } else { | 411 } else { |
372 // TODO(erg): This button can also be a drop target. | 412 // TODO(erg): This button can also be a drop target. |
373 ConnectFolderButtonEvents(button); | 413 ConnectFolderButtonEvents(button); |
374 } | 414 } |
375 | 415 |
376 GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS); | |
377 | |
378 return button; | 416 return button; |
379 } | 417 } |
380 | 418 |
381 GtkToolItem* BookmarkBarGtk::CreateBookmarkToolItem(BookmarkNode* node) { | 419 GtkToolItem* BookmarkBarGtk::CreateBookmarkToolItem(BookmarkNode* node) { |
382 GtkWidget* button = CreateBookmarkButton(node); | 420 GtkWidget* button = CreateBookmarkButton(node); |
383 g_object_set_data(G_OBJECT(button), "left-align-popup", | 421 g_object_set_data(G_OBJECT(button), "left-align-popup", |
384 reinterpret_cast<void*>(true)); | 422 reinterpret_cast<void*>(true)); |
385 | 423 |
386 GtkToolItem* item = gtk_tool_item_new(); | 424 GtkToolItem* item = gtk_tool_item_new(); |
387 gtk_container_add(GTK_CONTAINER(item), button); | 425 gtk_container_add(GTK_CONTAINER(item), button); |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
754 cairo_rectangle(cr, event->area.x, event->area.y, | 792 cairo_rectangle(cr, event->area.x, event->area.y, |
755 event->area.width, event->area.height); | 793 event->area.width, event->area.height); |
756 cairo_clip(cr); | 794 cairo_clip(cr); |
757 bar->InitBackground(); | 795 bar->InitBackground(); |
758 bar->background_ninebox_->RenderTopCenterStrip(cr, event->area.x, | 796 bar->background_ninebox_->RenderTopCenterStrip(cr, event->area.x, |
759 0, event->area.width); | 797 0, event->area.width); |
760 cairo_destroy(cr); | 798 cairo_destroy(cr); |
761 | 799 |
762 return FALSE; // Propagate expose to children. | 800 return FALSE; // Propagate expose to children. |
763 } | 801 } |
OLD | NEW |