OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/ui/gtk/infobars/confirm_infobar_gtk.h" | 5 #include "chrome/browser/ui/gtk/infobars/confirm_infobar_gtk.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/ui/gtk/event_utils.h" | 10 #include "chrome/browser/ui/gtk/event_utils.h" |
11 #include "chrome/browser/ui/gtk/gtk_chrome_link_button.h" | 11 #include "chrome/browser/ui/gtk/gtk_chrome_link_button.h" |
12 #include "chrome/browser/ui/gtk/gtk_chrome_shrinkable_hbox.h" | 12 #include "chrome/browser/ui/gtk/gtk_chrome_shrinkable_hbox.h" |
13 #include "chrome/browser/ui/gtk/gtk_util.h" | 13 #include "chrome/browser/ui/gtk/gtk_util.h" |
14 #include "ui/base/gtk/gtk_signal_registrar.h" | 14 #include "ui/base/gtk/gtk_signal_registrar.h" |
15 | 15 |
16 // ConfirmInfoBarDelegate ------------------------------------------------------ | 16 // ConfirmInfoBarDelegate ------------------------------------------------------ |
17 | 17 |
18 InfoBar* ConfirmInfoBarDelegate::CreateInfoBar(InfoBarService* owner) { | 18 InfoBar* ConfirmInfoBarDelegate::CreateInfoBar(InfoBarService* owner) { |
19 return new ConfirmInfoBarGtk(owner, this); | 19 return new ConfirmInfoBarGtk(owner, this); |
20 } | 20 } |
21 | 21 |
22 // ConfirmInfoBarGtk ----------------------------------------------------------- | 22 // ConfirmInfoBarGtk ----------------------------------------------------------- |
23 | 23 |
24 ConfirmInfoBarGtk::ConfirmInfoBarGtk(InfoBarService* owner, | 24 ConfirmInfoBarGtk::ConfirmInfoBarGtk(InfoBarService* owner, |
25 ConfirmInfoBarDelegate* delegate) | 25 ConfirmInfoBarDelegate* delegate) |
26 : InfoBarGtk(owner, delegate), | 26 : InfoBarGtk(owner, delegate), |
| 27 confirm_hbox_(NULL), |
27 size_group_(NULL) { | 28 size_group_(NULL) { |
| 29 } |
| 30 |
| 31 ConfirmInfoBarGtk::~ConfirmInfoBarGtk() { |
| 32 if (size_group_) |
| 33 g_object_unref(size_group_); |
| 34 } |
| 35 |
| 36 void ConfirmInfoBarGtk::InitWidgets() { |
| 37 InfoBarGtk::InitWidgets(); |
| 38 |
28 confirm_hbox_ = gtk_chrome_shrinkable_hbox_new(FALSE, FALSE, | 39 confirm_hbox_ = gtk_chrome_shrinkable_hbox_new(FALSE, FALSE, |
29 kEndOfLabelSpacing); | 40 kEndOfLabelSpacing); |
30 // This alignment allocates the confirm hbox only as much space as it | 41 // This alignment allocates the confirm hbox only as much space as it |
31 // requests, and less if there is not enough available. | 42 // requests, and less if there is not enough available. |
32 GtkWidget* align = gtk_alignment_new(0, 0, 0, 1); | 43 GtkWidget* align = gtk_alignment_new(0, 0, 0, 1); |
33 gtk_container_add(GTK_CONTAINER(align), confirm_hbox_); | 44 gtk_container_add(GTK_CONTAINER(align), confirm_hbox_); |
34 gtk_box_pack_start(GTK_BOX(hbox_), align, TRUE, TRUE, 0); | 45 gtk_box_pack_start(GTK_BOX(hbox_), align, TRUE, TRUE, 0); |
35 | 46 |
36 // We add the buttons in reverse order and pack end instead of start so | 47 // We add the buttons in reverse order and pack end instead of start so |
37 // that the first widget to get shrunk is the label rather than the button(s). | 48 // that the first widget to get shrunk is the label rather than the button(s). |
38 AddButton(ConfirmInfoBarDelegate::BUTTON_OK); | 49 AddButton(ConfirmInfoBarDelegate::BUTTON_OK); |
39 AddButton(ConfirmInfoBarDelegate::BUTTON_CANCEL); | 50 AddButton(ConfirmInfoBarDelegate::BUTTON_CANCEL); |
40 | 51 |
41 std::string label_text = UTF16ToUTF8(delegate->GetMessageText()); | 52 std::string label_text = UTF16ToUTF8(GetDelegate()->GetMessageText()); |
42 GtkWidget* label = CreateLabel(label_text); | 53 GtkWidget* label = CreateLabel(label_text); |
43 gtk_util::ForceFontSizePixels(label, 13.4); | 54 gtk_util::ForceFontSizePixels(label, 13.4); |
44 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); | 55 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); |
45 gtk_util::CenterWidgetInHBox(confirm_hbox_, label, true, 0); | 56 gtk_util::CenterWidgetInHBox(confirm_hbox_, label, true, 0); |
46 Signals()->Connect(label, "map", | 57 Signals()->Connect(label, "map", |
47 G_CALLBACK(gtk_util::InitLabelSizeRequestAndEllipsizeMode), | 58 G_CALLBACK(gtk_util::InitLabelSizeRequestAndEllipsizeMode), |
48 NULL); | 59 NULL); |
49 | 60 |
50 std::string link_text = UTF16ToUTF8(delegate->GetLinkText()); | 61 std::string link_text = UTF16ToUTF8(GetDelegate()->GetLinkText()); |
51 if (link_text.empty()) | 62 if (link_text.empty()) |
52 return; | 63 return; |
53 | 64 |
54 GtkWidget* link = CreateLinkButton(link_text); | 65 GtkWidget* link = CreateLinkButton(link_text); |
55 gtk_misc_set_alignment(GTK_MISC(GTK_CHROME_LINK_BUTTON(link)->label), 0, 0.5); | 66 gtk_misc_set_alignment(GTK_MISC(GTK_CHROME_LINK_BUTTON(link)->label), 0, 0.5); |
56 Signals()->Connect(link, "clicked", G_CALLBACK(OnLinkClickedThunk), this); | 67 Signals()->Connect(link, "clicked", G_CALLBACK(OnLinkClickedThunk), this); |
57 gtk_util::SetButtonTriggersNavigation(link); | 68 gtk_util::SetButtonTriggersNavigation(link); |
58 // Until we switch to vector graphics, force the font size. | 69 // Until we switch to vector graphics, force the font size. |
59 // 13.4px == 10pt @ 96dpi | 70 // 13.4px == 10pt @ 96dpi |
60 gtk_util::ForceFontSizePixels(GTK_CHROME_LINK_BUTTON(link)->label, 13.4); | 71 gtk_util::ForceFontSizePixels(GTK_CHROME_LINK_BUTTON(link)->label, 13.4); |
61 gtk_util::CenterWidgetInHBox(hbox_, link, true, kEndOfLabelSpacing); | 72 gtk_util::CenterWidgetInHBox(hbox_, link, true, kEndOfLabelSpacing); |
62 } | 73 } |
63 | 74 |
64 ConfirmInfoBarGtk::~ConfirmInfoBarGtk() { | 75 ConfirmInfoBarDelegate* ConfirmInfoBarGtk::GetDelegate() { |
65 if (size_group_) | 76 return delegate()->AsConfirmInfoBarDelegate(); |
66 g_object_unref(size_group_); | |
67 } | 77 } |
68 | 78 |
69 void ConfirmInfoBarGtk::AddButton(ConfirmInfoBarDelegate::InfoBarButton type) { | 79 void ConfirmInfoBarGtk::AddButton(ConfirmInfoBarDelegate::InfoBarButton type) { |
| 80 DCHECK(confirm_hbox_); |
70 if (delegate()->AsConfirmInfoBarDelegate()->GetButtons() & type) { | 81 if (delegate()->AsConfirmInfoBarDelegate()->GetButtons() & type) { |
71 if (!size_group_) | 82 if (!size_group_) |
72 size_group_ = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL); | 83 size_group_ = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL); |
73 | 84 |
74 GtkWidget* button = gtk_button_new_with_label(UTF16ToUTF8( | 85 GtkWidget* button = gtk_button_new_with_label(UTF16ToUTF8( |
75 delegate()->AsConfirmInfoBarDelegate()->GetButtonLabel(type)).c_str()); | 86 delegate()->AsConfirmInfoBarDelegate()->GetButtonLabel(type)).c_str()); |
76 gtk_size_group_add_widget(size_group_, button); | 87 gtk_size_group_add_widget(size_group_, button); |
77 | 88 |
78 gtk_util::CenterWidgetInHBox(confirm_hbox_, button, true, 0); | 89 gtk_util::CenterWidgetInHBox(confirm_hbox_, button, true, 0); |
79 Signals()->Connect(button, "clicked", | 90 Signals()->Connect(button, "clicked", |
(...skipping 11 matching lines...) Expand all Loading... |
91 void ConfirmInfoBarGtk::OnCancelButton(GtkWidget* widget) { | 102 void ConfirmInfoBarGtk::OnCancelButton(GtkWidget* widget) { |
92 if (delegate()->AsConfirmInfoBarDelegate()->Cancel()) | 103 if (delegate()->AsConfirmInfoBarDelegate()->Cancel()) |
93 RemoveSelf(); | 104 RemoveSelf(); |
94 } | 105 } |
95 | 106 |
96 void ConfirmInfoBarGtk::OnLinkClicked(GtkWidget* widget) { | 107 void ConfirmInfoBarGtk::OnLinkClicked(GtkWidget* widget) { |
97 if (delegate()->AsConfirmInfoBarDelegate()->LinkClicked( | 108 if (delegate()->AsConfirmInfoBarDelegate()->LinkClicked( |
98 event_utils::DispositionForCurrentButtonPressEvent())) | 109 event_utils::DispositionForCurrentButtonPressEvent())) |
99 RemoveSelf(); | 110 RemoveSelf(); |
100 } | 111 } |
OLD | NEW |