OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/ui/gtk/global_error_bubble.h" |
| 6 |
| 7 #include "base/i18n/rtl.h" |
| 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/global_error_delegate.h" |
| 11 #include "chrome/browser/ui/gtk/browser_toolbar_gtk.h" |
| 12 #include "chrome/browser/ui/gtk/browser_window_gtk.h" |
| 13 #include "chrome/browser/ui/gtk/gtk_theme_service.h" |
| 14 #include "chrome/browser/ui/gtk/gtk_util.h" |
| 15 #include "ui/base/gtk/gtk_hig_constants.h" |
| 16 #include "ui/base/resource/resource_bundle.h" |
| 17 #include "ui/gfx/gtk_util.h" |
| 18 #include "ui/gfx/image/image.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 // Padding between content and edge of bubble. |
| 23 const int kContentBorder = 7; |
| 24 |
| 25 // Horizontal spacing between the image view and the label. |
| 26 const int kImageViewSpacing = 5; |
| 27 |
| 28 // Vertical spacing between labels. |
| 29 const int kInterLineSpacing = 5; |
| 30 |
| 31 // Text size of the message label. 12.1px = 9pt @ 96dpi. |
| 32 const double kMessageTextSize = 12.1; |
| 33 |
| 34 // Width of the message label. |
| 35 const int kMessageLabelWidth = 250; |
| 36 |
| 37 } // namespace |
| 38 |
| 39 GlobalErrorBubble::GlobalErrorBubble(Profile* profile, |
| 40 GlobalErrorDelegate* delegate, |
| 41 GtkWidget* anchor) |
| 42 : bubble_(NULL), |
| 43 delegate_(delegate) { |
| 44 GtkWidget* content = gtk_vbox_new(FALSE, kInterLineSpacing); |
| 45 gtk_container_set_border_width(GTK_CONTAINER(content), kContentBorder); |
| 46 g_signal_connect(content, "destroy", |
| 47 G_CALLBACK(&HandleDestroyThunk), this); |
| 48 |
| 49 GtkThemeService* theme_service_ = GtkThemeService::GetFrom(profile); |
| 50 |
| 51 int resource_id = delegate->GetBubbleViewIconResourceID(); |
| 52 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 53 const SkBitmap* bitmap = rb.GetImageNamed(resource_id).ToSkBitmap(); |
| 54 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(bitmap); |
| 55 GtkWidget* image_view = gtk_image_new_from_pixbuf(pixbuf); |
| 56 |
| 57 GtkWidget* title_label = theme_service_->BuildLabel( |
| 58 UTF16ToUTF8(delegate->GetBubbleViewTitle()), |
| 59 ui::kGdkBlack); |
| 60 GtkWidget* message_label = theme_service_->BuildLabel( |
| 61 UTF16ToUTF8(delegate->GetBubbleViewMessage()), |
| 62 ui::kGdkBlack); |
| 63 gtk_util::ForceFontSizePixels(message_label, kMessageTextSize); |
| 64 gtk_util::SetLabelWidth(message_label, kMessageLabelWidth); |
| 65 GtkWidget* accept_button = gtk_button_new_with_label( |
| 66 UTF16ToUTF8(delegate->GetBubbleViewAcceptButtonLabel()).c_str()); |
| 67 string16 cancel_string = delegate->GetBubbleViewCancelButtonLabel(); |
| 68 GtkWidget* cancel_button = NULL; |
| 69 if (!cancel_string.empty()) { |
| 70 cancel_button = |
| 71 gtk_button_new_with_label(UTF16ToUTF8(cancel_string).c_str()); |
| 72 } |
| 73 |
| 74 // Top, icon and title. |
| 75 GtkWidget* top = gtk_hbox_new(FALSE, kImageViewSpacing); |
| 76 gtk_box_pack_start(GTK_BOX(top), image_view, FALSE, FALSE, 0); |
| 77 gtk_box_pack_start(GTK_BOX(top), title_label, FALSE, FALSE, 0); |
| 78 gtk_box_pack_start(GTK_BOX(content), top, FALSE, FALSE, 0); |
| 79 |
| 80 // Middle, message. |
| 81 gtk_box_pack_start(GTK_BOX(content), message_label, FALSE, FALSE, 0); |
| 82 gtk_box_pack_start(GTK_BOX(content), gtk_hbox_new(FALSE, 0), FALSE, FALSE, 0); |
| 83 |
| 84 // Bottom, accept and cancel button. |
| 85 // We want the buttons on the right, so just use an expanding label to fill |
| 86 // all of the extra space on the left. |
| 87 GtkWidget* bottom = gtk_hbox_new(FALSE, ui::kControlSpacing); |
| 88 gtk_box_pack_start(GTK_BOX(bottom), gtk_label_new(NULL), TRUE, TRUE, 0); |
| 89 if (cancel_button) |
| 90 gtk_box_pack_start(GTK_BOX(bottom), cancel_button, FALSE, FALSE, 0); |
| 91 gtk_box_pack_start(GTK_BOX(bottom), accept_button, FALSE, FALSE, 0); |
| 92 gtk_box_pack_start(GTK_BOX(content), bottom, FALSE, FALSE, 0); |
| 93 |
| 94 gtk_widget_grab_focus(accept_button); |
| 95 |
| 96 g_signal_connect(accept_button, "clicked", |
| 97 G_CALLBACK(&HandleAcceptButtonThunk), this); |
| 98 if (cancel_button) { |
| 99 g_signal_connect(cancel_button, "clicked", |
| 100 G_CALLBACK(&HandleCancelButtonThunk), this); |
| 101 } |
| 102 |
| 103 BubbleGtk::ArrowLocationGtk arrow_location = |
| 104 base::i18n::IsRTL() ? |
| 105 BubbleGtk::ARROW_LOCATION_TOP_LEFT : |
| 106 BubbleGtk::ARROW_LOCATION_TOP_RIGHT; |
| 107 bubble_ = BubbleGtk::Show(anchor, |
| 108 NULL, |
| 109 content, |
| 110 arrow_location, |
| 111 true, // match_system_theme |
| 112 true, // grab_input |
| 113 theme_service_, |
| 114 this); // delegate |
| 115 } |
| 116 |
| 117 GlobalErrorBubble::~GlobalErrorBubble() { |
| 118 } |
| 119 |
| 120 void GlobalErrorBubble::BubbleClosing(BubbleGtk* bubble, |
| 121 bool closed_by_escape) { |
| 122 } |
| 123 |
| 124 void GlobalErrorBubble::HandleDestroy(GtkWidget* sender) { |
| 125 delete this; |
| 126 } |
| 127 |
| 128 void GlobalErrorBubble::HandleAcceptButton(GtkWidget* sender) { |
| 129 delegate_->BubbleViewAcceptButtonPressed(); |
| 130 bubble_->Close(); |
| 131 } |
| 132 |
| 133 void GlobalErrorBubble::HandleCancelButton(GtkWidget* sender) { |
| 134 delegate_->BubbleViewCancelButtonPressed(); |
| 135 bubble_->Close(); |
| 136 } |
| 137 |
| 138 void GlobalErrorDelegate::ShowBubbleView( |
| 139 GlobalErrorDelegate* delegate, |
| 140 Browser* browser, |
| 141 const gfx::Rect& position_relative_to) { |
| 142 BrowserWindowGtk* browser_window = |
| 143 BrowserWindowGtk::GetBrowserWindowForNativeWindow( |
| 144 browser->window()->GetNativeHandle()); |
| 145 GtkWidget* anchor = browser_window->GetToolbar()->GetAppMenuButton(); |
| 146 |
| 147 // The bubble will be automatically deleted when it's closed. |
| 148 new GlobalErrorBubble(browser->profile(), delegate, anchor); |
| 149 } |
OLD | NEW |