| 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/download_item_gtk.h" | 5 #include "chrome/browser/gtk/download_item_gtk.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/gfx/canvas_paint.h" | 8 #include "app/gfx/canvas_paint.h" |
| 9 #include "app/gfx/font.h" | 9 #include "app/gfx/font.h" |
| 10 #include "app/gfx/text_elider.h" | 10 #include "app/gfx/text_elider.h" |
| 11 #include "app/resource_bundle.h" | 11 #include "app/resource_bundle.h" |
| 12 #include "app/slide_animation.h" | 12 #include "app/slide_animation.h" |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "base/time.h" | 15 #include "base/time.h" |
| 16 #include "chrome/browser/browser.h" |
| 16 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/download/download_item_model.h" | 18 #include "chrome/browser/download/download_item_model.h" |
| 18 #include "chrome/browser/download/download_manager.h" | 19 #include "chrome/browser/download/download_manager.h" |
| 19 #include "chrome/browser/download/download_shelf.h" | 20 #include "chrome/browser/download/download_shelf.h" |
| 20 #include "chrome/browser/download/download_util.h" | 21 #include "chrome/browser/download/download_util.h" |
| 21 #include "chrome/browser/gtk/download_shelf_gtk.h" | 22 #include "chrome/browser/gtk/download_shelf_gtk.h" |
| 23 #include "chrome/browser/gtk/gtk_theme_provider.h" |
| 22 #include "chrome/browser/gtk/menu_gtk.h" | 24 #include "chrome/browser/gtk/menu_gtk.h" |
| 23 #include "chrome/browser/gtk/nine_box.h" | 25 #include "chrome/browser/gtk/nine_box.h" |
| 24 #include "chrome/browser/gtk/standard_menus.h" | 26 #include "chrome/browser/gtk/standard_menus.h" |
| 25 #include "chrome/common/gtk_util.h" | 27 #include "chrome/common/gtk_util.h" |
| 26 #include "grit/generated_resources.h" | 28 #include "grit/generated_resources.h" |
| 27 #include "grit/theme_resources.h" | 29 #include "grit/theme_resources.h" |
| 28 #include "third_party/skia/include/core/SkBitmap.h" | 30 #include "third_party/skia/include/core/SkBitmap.h" |
| 29 | 31 |
| 30 namespace { | 32 namespace { |
| 31 | 33 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 NineBox* DownloadItemGtk::menu_nine_box_normal_ = NULL; | 162 NineBox* DownloadItemGtk::menu_nine_box_normal_ = NULL; |
| 161 NineBox* DownloadItemGtk::menu_nine_box_prelight_ = NULL; | 163 NineBox* DownloadItemGtk::menu_nine_box_prelight_ = NULL; |
| 162 NineBox* DownloadItemGtk::menu_nine_box_active_ = NULL; | 164 NineBox* DownloadItemGtk::menu_nine_box_active_ = NULL; |
| 163 | 165 |
| 164 NineBox* DownloadItemGtk::dangerous_nine_box_ = NULL; | 166 NineBox* DownloadItemGtk::dangerous_nine_box_ = NULL; |
| 165 | 167 |
| 166 DownloadItemGtk::DownloadItemGtk(DownloadShelfGtk* parent_shelf, | 168 DownloadItemGtk::DownloadItemGtk(DownloadShelfGtk* parent_shelf, |
| 167 BaseDownloadItemModel* download_model) | 169 BaseDownloadItemModel* download_model) |
| 168 : parent_shelf_(parent_shelf), | 170 : parent_shelf_(parent_shelf), |
| 169 menu_showing_(false), | 171 menu_showing_(false), |
| 172 use_gtk_colors_(GtkThemeProvider::UseSystemThemeGraphics( |
| 173 parent_shelf->browser()->profile())), |
| 170 progress_angle_(download_util::kStartAngleDegrees), | 174 progress_angle_(download_util::kStartAngleDegrees), |
| 171 download_model_(download_model), | 175 download_model_(download_model), |
| 172 bounding_widget_(parent_shelf->GetRightBoundingWidget()), | 176 bounding_widget_(parent_shelf->GetRightBoundingWidget()), |
| 173 dangerous_prompt_(NULL), | 177 dangerous_prompt_(NULL), |
| 174 icon_(NULL) { | 178 icon_(NULL) { |
| 175 InitNineBoxes(); | 179 InitNineBoxes(); |
| 176 LoadIcon(); | 180 LoadIcon(); |
| 177 | 181 |
| 178 body_.Own(gtk_button_new()); | 182 body_.Own(gtk_button_new()); |
| 179 gtk_widget_set_app_paintable(body_.get(), TRUE); | 183 gtk_widget_set_app_paintable(body_.get(), TRUE); |
| 180 g_signal_connect(body_.get(), "expose-event", | 184 g_signal_connect(body_.get(), "expose-event", |
| 181 G_CALLBACK(OnExpose), this); | 185 G_CALLBACK(OnExpose), this); |
| 182 g_signal_connect(body_.get(), "clicked", | 186 g_signal_connect(body_.get(), "clicked", |
| 183 G_CALLBACK(OnClick), this); | 187 G_CALLBACK(OnClick), this); |
| 184 GTK_WIDGET_UNSET_FLAGS(body_.get(), GTK_CAN_FOCUS); | 188 GTK_WIDGET_UNSET_FLAGS(body_.get(), GTK_CAN_FOCUS); |
| 185 // Remove internal padding on the button. | 189 // Remove internal padding on the button. |
| 186 GtkRcStyle* no_padding_style = gtk_rc_style_new(); | 190 GtkRcStyle* no_padding_style = gtk_rc_style_new(); |
| 187 no_padding_style->xthickness = 0; | 191 no_padding_style->xthickness = 0; |
| 188 no_padding_style->ythickness = 0; | 192 no_padding_style->ythickness = 0; |
| 189 gtk_widget_modify_style(body_.get(), no_padding_style); | 193 gtk_widget_modify_style(body_.get(), no_padding_style); |
| 190 g_object_unref(no_padding_style); | 194 g_object_unref(no_padding_style); |
| 191 | 195 |
| 192 name_label_ = gtk_label_new(NULL); | 196 name_label_ = gtk_label_new(NULL); |
| 193 | 197 |
| 194 // TODO(estade): This is at best an educated guess, since we don't actually | 198 // TODO(estade): This is at best an educated guess, since we don't actually |
| 195 // use gfx::Font() to draw the text. This is why we need to add so | 199 // use gfx::Font() to draw the text. This is why we need to add so |
| 196 // much padding when we set the size request. We need to either use gfx::Font | 200 // much padding when we set the size request. We need to either use gfx::Font |
| 197 // or somehow extend TextElider. | 201 // or somehow extend TextElider. |
| 198 std::wstring elided_filename = gfx::ElideFilename( | 202 UpdateNameLabel(); |
| 199 get_download()->GetFileName(), | |
| 200 gfx::Font(), kTextWidth); | |
| 201 gchar* label_markup = | |
| 202 g_markup_printf_escaped(kLabelColorMarkup, kFilenameColor, | |
| 203 WideToUTF8(elided_filename).c_str()); | |
| 204 gtk_label_set_markup(GTK_LABEL(name_label_), label_markup); | |
| 205 g_free(label_markup); | |
| 206 | 203 |
| 207 status_label_ = gtk_label_new(NULL); | 204 status_label_ = gtk_label_new(NULL); |
| 208 // Left align and vertically center the labels. | 205 // Left align and vertically center the labels. |
| 209 gtk_misc_set_alignment(GTK_MISC(name_label_), 0, 0.5); | 206 gtk_misc_set_alignment(GTK_MISC(name_label_), 0, 0.5); |
| 210 gtk_misc_set_alignment(GTK_MISC(status_label_), 0, 0.5); | 207 gtk_misc_set_alignment(GTK_MISC(status_label_), 0, 0.5); |
| 211 // Until we switch to vector graphics, force the font size. | 208 // Until we switch to vector graphics, force the font size. |
| 212 gtk_util::ForceFontSizePixels(name_label_, 13.4); // 13.4px == 10pt @ 96dpi | 209 gtk_util::ForceFontSizePixels(name_label_, 13.4); // 13.4px == 10pt @ 96dpi |
| 213 gtk_util::ForceFontSizePixels(status_label_, 13.4); // 13.4px == 10pt @ 96dpi | 210 gtk_util::ForceFontSizePixels(status_label_, 13.4); // 13.4px == 10pt @ 96dpi |
| 214 | 211 |
| 215 // Stack the labels on top of one another. | 212 // Stack the labels on top of one another. |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 NOTREACHED(); | 384 NOTREACHED(); |
| 388 } | 385 } |
| 389 | 386 |
| 390 // Now update the status label. We may have already removed it; if so, we | 387 // Now update the status label. We may have already removed it; if so, we |
| 391 // do nothing. | 388 // do nothing. |
| 392 if (!status_label_) { | 389 if (!status_label_) { |
| 393 return; | 390 return; |
| 394 } | 391 } |
| 395 | 392 |
| 396 std::wstring status_text = download_model_->GetStatusText(); | 393 std::wstring status_text = download_model_->GetStatusText(); |
| 394 status_text_ = WideToUTF8(status_text); |
| 397 // Remove the status text label. | 395 // Remove the status text label. |
| 398 if (status_text.empty()) { | 396 if (status_text.empty()) { |
| 399 gtk_widget_destroy(status_label_); | 397 gtk_widget_destroy(status_label_); |
| 400 status_label_ = NULL; | 398 status_label_ = NULL; |
| 401 return; | 399 return; |
| 402 } | 400 } |
| 403 | 401 |
| 404 gchar* label_markup = | 402 UpdateStatusLabel(status_label_, status_text_); |
| 405 g_markup_printf_escaped(kLabelColorMarkup, kStatusColor, | |
| 406 WideToUTF8(status_text).c_str()); | |
| 407 gtk_label_set_markup(GTK_LABEL(status_label_), label_markup); | |
| 408 g_free(label_markup); | |
| 409 } | 403 } |
| 410 | 404 |
| 411 void DownloadItemGtk::AnimationProgressed(const Animation* animation) { | 405 void DownloadItemGtk::AnimationProgressed(const Animation* animation) { |
| 412 if (animation == complete_animation_.get()) { | 406 if (animation == complete_animation_.get()) { |
| 413 gtk_widget_queue_draw(progress_area_.get()); | 407 gtk_widget_queue_draw(progress_area_.get()); |
| 414 } else { | 408 } else { |
| 415 if (IsDangerous()) { | 409 if (IsDangerous()) { |
| 416 int progress = (dangerous_hbox_full_width_ - | 410 int progress = (dangerous_hbox_full_width_ - |
| 417 dangerous_hbox_start_width_) * | 411 dangerous_hbox_start_width_) * |
| 418 new_item_animation_->GetCurrentValue(); | 412 new_item_animation_->GetCurrentValue(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 return; | 447 return; |
| 454 progress_timer_.Start( | 448 progress_timer_.Start( |
| 455 base::TimeDelta::FromMilliseconds(download_util::kProgressRateMs), this, | 449 base::TimeDelta::FromMilliseconds(download_util::kProgressRateMs), this, |
| 456 &DownloadItemGtk::UpdateDownloadProgress); | 450 &DownloadItemGtk::UpdateDownloadProgress); |
| 457 } | 451 } |
| 458 | 452 |
| 459 void DownloadItemGtk::StopDownloadProgress() { | 453 void DownloadItemGtk::StopDownloadProgress() { |
| 460 progress_timer_.Stop(); | 454 progress_timer_.Stop(); |
| 461 } | 455 } |
| 462 | 456 |
| 457 void DownloadItemGtk::UserChangedTheme(GtkThemeProperties* properties) { |
| 458 use_gtk_colors_ = properties->use_gtk_rendering; |
| 459 |
| 460 UpdateNameLabel(); |
| 461 UpdateStatusLabel(status_label_, status_text_); |
| 462 } |
| 463 |
| 463 // Icon loading functions. | 464 // Icon loading functions. |
| 464 | 465 |
| 465 void DownloadItemGtk::OnLoadIconComplete(IconManager::Handle handle, | 466 void DownloadItemGtk::OnLoadIconComplete(IconManager::Handle handle, |
| 466 SkBitmap* icon_bitmap) { | 467 SkBitmap* icon_bitmap) { |
| 467 icon_ = icon_bitmap; | 468 icon_ = icon_bitmap; |
| 468 gtk_widget_queue_draw(progress_area_.get()); | 469 gtk_widget_queue_draw(progress_area_.get()); |
| 469 } | 470 } |
| 470 | 471 |
| 471 void DownloadItemGtk::LoadIcon() { | 472 void DownloadItemGtk::LoadIcon() { |
| 472 IconManager* im = g_browser_process->icon_manager(); | 473 IconManager* im = g_browser_process->icon_manager(); |
| 473 im->LoadIcon(get_download()->full_path(), | 474 im->LoadIcon(get_download()->full_path(), |
| 474 IconLoader::SMALL, &icon_consumer_, | 475 IconLoader::SMALL, &icon_consumer_, |
| 475 NewCallback(this, &DownloadItemGtk::OnLoadIconComplete)); | 476 NewCallback(this, &DownloadItemGtk::OnLoadIconComplete)); |
| 476 } | 477 } |
| 477 | 478 |
| 479 void DownloadItemGtk::UpdateNameLabel() { |
| 480 std::wstring elided_filename = gfx::ElideFilename( |
| 481 get_download()->GetFileName(), |
| 482 gfx::Font(), kTextWidth); |
| 483 if (use_gtk_colors_) { |
| 484 gtk_label_set_markup(GTK_LABEL(name_label_), |
| 485 WideToUTF8(elided_filename).c_str()); |
| 486 } else { |
| 487 gchar* label_markup = |
| 488 g_markup_printf_escaped(kLabelColorMarkup, kFilenameColor, |
| 489 WideToUTF8(elided_filename).c_str()); |
| 490 gtk_label_set_markup(GTK_LABEL(name_label_), label_markup); |
| 491 g_free(label_markup); |
| 492 } |
| 493 } |
| 494 |
| 495 void DownloadItemGtk::UpdateStatusLabel(GtkWidget* status_label, |
| 496 const std::string& status_text) { |
| 497 if (status_label) { |
| 498 if (use_gtk_colors_) { |
| 499 gtk_label_set_label(GTK_LABEL(status_label), status_text.c_str()); |
| 500 } else { |
| 501 // TODO(erg): I am not sure which ThemeProvider color I'm supposed to use |
| 502 // here. I am also not sure if using set_markup is the correct course of |
| 503 // action compared to modifying the GtkStyle->text[]. |
| 504 gchar* label_markup = |
| 505 g_markup_printf_escaped(kLabelColorMarkup, kStatusColor, |
| 506 status_text.c_str()); |
| 507 gtk_label_set_markup(GTK_LABEL(status_label), label_markup); |
| 508 g_free(label_markup); |
| 509 } |
| 510 } |
| 511 } |
| 512 |
| 478 // static | 513 // static |
| 479 void DownloadItemGtk::InitNineBoxes() { | 514 void DownloadItemGtk::InitNineBoxes() { |
| 480 if (body_nine_box_normal_) | 515 if (body_nine_box_normal_) |
| 481 return; | 516 return; |
| 482 | 517 |
| 483 body_nine_box_normal_ = new NineBox( | 518 body_nine_box_normal_ = new NineBox( |
| 484 IDR_DOWNLOAD_BUTTON_LEFT_TOP, | 519 IDR_DOWNLOAD_BUTTON_LEFT_TOP, |
| 485 IDR_DOWNLOAD_BUTTON_CENTER_TOP, | 520 IDR_DOWNLOAD_BUTTON_CENTER_TOP, |
| 486 IDR_DOWNLOAD_BUTTON_RIGHT_TOP, | 521 IDR_DOWNLOAD_BUTTON_RIGHT_TOP, |
| 487 IDR_DOWNLOAD_BUTTON_LEFT_MIDDLE, | 522 IDR_DOWNLOAD_BUTTON_LEFT_MIDDLE, |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 668 item->get_download()); | 703 item->get_download()); |
| 669 } | 704 } |
| 670 | 705 |
| 671 // static | 706 // static |
| 672 void DownloadItemGtk::OnDangerousDecline(GtkWidget* button, | 707 void DownloadItemGtk::OnDangerousDecline(GtkWidget* button, |
| 673 DownloadItemGtk* item) { | 708 DownloadItemGtk* item) { |
| 674 if (item->get_download()->state() == DownloadItem::IN_PROGRESS) | 709 if (item->get_download()->state() == DownloadItem::IN_PROGRESS) |
| 675 item->get_download()->Cancel(true); | 710 item->get_download()->Cancel(true); |
| 676 item->get_download()->Remove(true); | 711 item->get_download()->Remove(true); |
| 677 } | 712 } |
| OLD | NEW |