| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/gtk/download_shelf_gtk.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "app/l10n_util.h" | |
| 10 #include "app/resource_bundle.h" | |
| 11 #include "chrome/browser/download/download_item.h" | |
| 12 #include "chrome/browser/download/download_item_model.h" | |
| 13 #include "chrome/browser/download/download_util.h" | |
| 14 #include "chrome/browser/gtk/browser_window_gtk.h" | |
| 15 #include "chrome/browser/gtk/custom_button.h" | |
| 16 #include "chrome/browser/gtk/download_item_gtk.h" | |
| 17 #include "chrome/browser/gtk/gtk_chrome_link_button.h" | |
| 18 #include "chrome/browser/gtk/gtk_chrome_shrinkable_hbox.h" | |
| 19 #include "chrome/browser/gtk/gtk_theme_provider.h" | |
| 20 #include "chrome/browser/gtk/gtk_util.h" | |
| 21 #include "chrome/browser/ui/browser.h" | |
| 22 #include "chrome/common/notification_service.h" | |
| 23 #include "gfx/gtk_util.h" | |
| 24 #include "grit/generated_resources.h" | |
| 25 #include "grit/theme_resources.h" | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 // The height of the download items. | |
| 30 const int kDownloadItemHeight = download_util::kSmallProgressIconSize; | |
| 31 | |
| 32 // Padding between the download widgets. | |
| 33 const int kDownloadItemPadding = 10; | |
| 34 | |
| 35 // Padding between the top/bottom of the download widgets and the edge of the | |
| 36 // shelf. | |
| 37 const int kTopBottomPadding = 4; | |
| 38 | |
| 39 // Padding between the left side of the shelf and the first download item. | |
| 40 const int kLeftPadding = 2; | |
| 41 | |
| 42 // Padding between the right side of the shelf and the close button. | |
| 43 const int kRightPadding = 10; | |
| 44 | |
| 45 // Speed of the shelf show/hide animation. | |
| 46 const int kShelfAnimationDurationMs = 120; | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 DownloadShelfGtk::DownloadShelfGtk(Browser* browser, GtkWidget* parent) | |
| 51 : browser_(browser), | |
| 52 is_showing_(false), | |
| 53 theme_provider_(GtkThemeProvider::GetFrom(browser->profile())) { | |
| 54 // Logically, the shelf is a vbox that contains two children: a one pixel | |
| 55 // tall event box, which serves as the top border, and an hbox, which holds | |
| 56 // the download items and other shelf widgets (close button, show-all- | |
| 57 // downloads link). | |
| 58 // To make things pretty, we have to add a few more widgets. To get padding | |
| 59 // right, we stick the hbox in an alignment. We put that alignment in an | |
| 60 // event box so we can color the background. | |
| 61 | |
| 62 // Create the top border. | |
| 63 top_border_ = gtk_event_box_new(); | |
| 64 gtk_widget_set_size_request(GTK_WIDGET(top_border_), 0, 1); | |
| 65 | |
| 66 // Create |items_hbox_|. We use GtkChromeShrinkableHBox, so that download | |
| 67 // items can be hid automatically when there is no enough space to show them. | |
| 68 items_hbox_.Own(gtk_chrome_shrinkable_hbox_new( | |
| 69 TRUE, FALSE, kDownloadItemPadding)); | |
| 70 // We want the download shelf to be horizontally shrinkable, so that the | |
| 71 // Chrome window can be resized freely even with many download items. | |
| 72 gtk_widget_set_size_request(items_hbox_.get(), 0, kDownloadItemHeight); | |
| 73 | |
| 74 // Create a hbox that holds |items_hbox_| and other shelf widgets. | |
| 75 GtkWidget* outer_hbox = gtk_hbox_new(FALSE, kDownloadItemPadding); | |
| 76 | |
| 77 // Pack the |items_hbox_| in the outer hbox. | |
| 78 gtk_box_pack_start(GTK_BOX(outer_hbox), items_hbox_.get(), TRUE, TRUE, 0); | |
| 79 | |
| 80 // Get the padding and background color for |outer_hbox| right. | |
| 81 GtkWidget* padding = gtk_alignment_new(0, 0, 1, 1); | |
| 82 // Subtract 1 from top spacing to account for top border. | |
| 83 gtk_alignment_set_padding(GTK_ALIGNMENT(padding), | |
| 84 kTopBottomPadding - 1, kTopBottomPadding, kLeftPadding, kRightPadding); | |
| 85 padding_bg_ = gtk_event_box_new(); | |
| 86 gtk_container_add(GTK_CONTAINER(padding_bg_), padding); | |
| 87 gtk_container_add(GTK_CONTAINER(padding), outer_hbox); | |
| 88 | |
| 89 GtkWidget* vbox = gtk_vbox_new(FALSE, 0); | |
| 90 gtk_box_pack_start(GTK_BOX(vbox), top_border_, FALSE, FALSE, 0); | |
| 91 gtk_box_pack_start(GTK_BOX(vbox), padding_bg_, FALSE, FALSE, 0); | |
| 92 | |
| 93 // Put the shelf in an event box so it gets its own window, which makes it | |
| 94 // easier to get z-ordering right. | |
| 95 shelf_.Own(gtk_event_box_new()); | |
| 96 gtk_container_add(GTK_CONTAINER(shelf_.get()), vbox); | |
| 97 | |
| 98 // Create and pack the close button. | |
| 99 close_button_.reset(CustomDrawButton::CloseButton(theme_provider_)); | |
| 100 gtk_util::CenterWidgetInHBox(outer_hbox, close_button_->widget(), true, 0); | |
| 101 g_signal_connect(close_button_->widget(), "clicked", | |
| 102 G_CALLBACK(OnButtonClickThunk), this); | |
| 103 | |
| 104 // Create the "Show all downloads..." link and connect to the click event. | |
| 105 std::string link_text = | |
| 106 l10n_util::GetStringUTF8(IDS_SHOW_ALL_DOWNLOADS); | |
| 107 link_button_ = gtk_chrome_link_button_new(link_text.c_str()); | |
| 108 g_signal_connect(link_button_, "clicked", | |
| 109 G_CALLBACK(OnButtonClickThunk), this); | |
| 110 gtk_util::SetButtonTriggersNavigation(link_button_); | |
| 111 // Until we switch to vector graphics, force the font size. | |
| 112 // 13.4px == 10pt @ 96dpi | |
| 113 gtk_util::ForceFontSizePixels(GTK_CHROME_LINK_BUTTON(link_button_)->label, | |
| 114 13.4); | |
| 115 | |
| 116 // Make the download arrow icon. | |
| 117 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 118 GdkPixbuf* download_pixbuf = rb.GetPixbufNamed(IDR_DOWNLOADS_FAVICON); | |
| 119 GtkWidget* download_image = gtk_image_new_from_pixbuf(download_pixbuf); | |
| 120 | |
| 121 // Pack the link and the icon in outer hbox. | |
| 122 gtk_util::CenterWidgetInHBox(outer_hbox, link_button_, true, 0); | |
| 123 gtk_util::CenterWidgetInHBox(outer_hbox, download_image, true, 0); | |
| 124 | |
| 125 slide_widget_.reset(new SlideAnimatorGtk(shelf_.get(), | |
| 126 SlideAnimatorGtk::UP, | |
| 127 kShelfAnimationDurationMs, | |
| 128 false, true, this)); | |
| 129 | |
| 130 theme_provider_->InitThemesFor(this); | |
| 131 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED, | |
| 132 NotificationService::AllSources()); | |
| 133 | |
| 134 gtk_widget_show_all(shelf_.get()); | |
| 135 | |
| 136 // Stick ourselves at the bottom of the parent browser. | |
| 137 gtk_box_pack_end(GTK_BOX(parent), slide_widget_->widget(), | |
| 138 FALSE, FALSE, 0); | |
| 139 // Make sure we are at the very end. | |
| 140 gtk_box_reorder_child(GTK_BOX(parent), slide_widget_->widget(), 0); | |
| 141 Show(); | |
| 142 } | |
| 143 | |
| 144 DownloadShelfGtk::~DownloadShelfGtk() { | |
| 145 for (std::vector<DownloadItemGtk*>::iterator iter = download_items_.begin(); | |
| 146 iter != download_items_.end(); ++iter) { | |
| 147 delete *iter; | |
| 148 } | |
| 149 | |
| 150 shelf_.Destroy(); | |
| 151 items_hbox_.Destroy(); | |
| 152 } | |
| 153 | |
| 154 void DownloadShelfGtk::AddDownload(BaseDownloadItemModel* download_model_) { | |
| 155 download_items_.push_back(new DownloadItemGtk(this, download_model_)); | |
| 156 Show(); | |
| 157 } | |
| 158 | |
| 159 bool DownloadShelfGtk::IsShowing() const { | |
| 160 return slide_widget_->IsShowing(); | |
| 161 } | |
| 162 | |
| 163 bool DownloadShelfGtk::IsClosing() const { | |
| 164 return slide_widget_->IsClosing(); | |
| 165 } | |
| 166 | |
| 167 void DownloadShelfGtk::Show() { | |
| 168 slide_widget_->Open(); | |
| 169 browser_->UpdateDownloadShelfVisibility(true); | |
| 170 } | |
| 171 | |
| 172 void DownloadShelfGtk::Close() { | |
| 173 // When we are closing, we can vertically overlap the render view. Make sure | |
| 174 // we are on top. | |
| 175 gdk_window_raise(shelf_.get()->window); | |
| 176 slide_widget_->Close(); | |
| 177 browser_->UpdateDownloadShelfVisibility(false); | |
| 178 } | |
| 179 | |
| 180 Browser* DownloadShelfGtk::browser() const { | |
| 181 return browser_; | |
| 182 } | |
| 183 | |
| 184 void DownloadShelfGtk::Closed() { | |
| 185 // When the close animation is complete, remove all completed downloads. | |
| 186 size_t i = 0; | |
| 187 while (i < download_items_.size()) { | |
| 188 DownloadItem* download = download_items_[i]->get_download(); | |
| 189 bool is_transfer_done = download->state() == DownloadItem::COMPLETE || | |
| 190 download->state() == DownloadItem::CANCELLED; | |
| 191 if (is_transfer_done && | |
| 192 download->safety_state() != DownloadItem::DANGEROUS) { | |
| 193 RemoveDownloadItem(download_items_[i]); | |
| 194 } else { | |
| 195 ++i; | |
| 196 } | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 void DownloadShelfGtk::Observe(NotificationType type, | |
| 201 const NotificationSource& source, | |
| 202 const NotificationDetails& details) { | |
| 203 if (type == NotificationType::BROWSER_THEME_CHANGED) { | |
| 204 GdkColor color = theme_provider_->GetGdkColor( | |
| 205 BrowserThemeProvider::COLOR_TOOLBAR); | |
| 206 gtk_widget_modify_bg(padding_bg_, GTK_STATE_NORMAL, &color); | |
| 207 | |
| 208 color = theme_provider_->GetBorderColor(); | |
| 209 gtk_widget_modify_bg(top_border_, GTK_STATE_NORMAL, &color); | |
| 210 | |
| 211 gtk_chrome_link_button_set_use_gtk_theme( | |
| 212 GTK_CHROME_LINK_BUTTON(link_button_), theme_provider_->UseGtkTheme()); | |
| 213 | |
| 214 // When using a non-standard, non-gtk theme, we make the link color match | |
| 215 // the bookmark text color. Otherwise, standard link blue can look very | |
| 216 // bad for some dark themes. | |
| 217 bool use_default_color = theme_provider_->GetColor( | |
| 218 BrowserThemeProvider::COLOR_BOOKMARK_TEXT) == | |
| 219 BrowserThemeProvider::GetDefaultColor( | |
| 220 BrowserThemeProvider::COLOR_BOOKMARK_TEXT); | |
| 221 GdkColor bookmark_color = theme_provider_->GetGdkColor( | |
| 222 BrowserThemeProvider::COLOR_BOOKMARK_TEXT); | |
| 223 gtk_chrome_link_button_set_normal_color( | |
| 224 GTK_CHROME_LINK_BUTTON(link_button_), | |
| 225 use_default_color ? NULL : &bookmark_color); | |
| 226 | |
| 227 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 228 close_button_->SetBackground( | |
| 229 theme_provider_->GetColor(BrowserThemeProvider::COLOR_TAB_TEXT), | |
| 230 rb.GetBitmapNamed(IDR_CLOSE_BAR), | |
| 231 rb.GetBitmapNamed(IDR_CLOSE_BAR_MASK)); | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 int DownloadShelfGtk::GetHeight() const { | |
| 236 return slide_widget_->widget()->allocation.height; | |
| 237 } | |
| 238 | |
| 239 void DownloadShelfGtk::RemoveDownloadItem(DownloadItemGtk* download_item) { | |
| 240 DCHECK(download_item); | |
| 241 std::vector<DownloadItemGtk*>::iterator i = | |
| 242 find(download_items_.begin(), download_items_.end(), download_item); | |
| 243 DCHECK(i != download_items_.end()); | |
| 244 download_items_.erase(i); | |
| 245 delete download_item; | |
| 246 if (download_items_.empty()) { | |
| 247 slide_widget_->CloseWithoutAnimation(); | |
| 248 browser_->UpdateDownloadShelfVisibility(false); | |
| 249 } | |
| 250 } | |
| 251 | |
| 252 GtkWidget* DownloadShelfGtk::GetHBox() const { | |
| 253 return items_hbox_.get(); | |
| 254 } | |
| 255 | |
| 256 void DownloadShelfGtk::MaybeShowMoreDownloadItems() { | |
| 257 // Show all existing download items. It'll trigger "size-allocate" signal, | |
| 258 // which will hide download items that don't have enough space to show. | |
| 259 gtk_widget_show_all(items_hbox_.get()); | |
| 260 } | |
| 261 | |
| 262 void DownloadShelfGtk::OnButtonClick(GtkWidget* button) { | |
| 263 if (button == close_button_->widget()) { | |
| 264 Close(); | |
| 265 } else { | |
| 266 // The link button was clicked. | |
| 267 browser_->ShowDownloadsTab(); | |
| 268 } | |
| 269 } | |
| OLD | NEW |