• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/infobars/confirm_infobar_gtk.h"
6 
7 #include <gtk/gtk.h>
8 
9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/ui/gtk/gtk_chrome_link_button.h"
11 #include "chrome/browser/ui/gtk/gtk_chrome_shrinkable_hbox.h"
12 #include "chrome/browser/ui/gtk/gtk_util.h"
13 
14 // ConfirmInfoBarDelegate ------------------------------------------------------
15 
CreateInfoBar()16 InfoBar* ConfirmInfoBarDelegate::CreateInfoBar() {
17   return new ConfirmInfoBarGtk(this);
18 }
19 
20 // ConfirmInfoBarGtk -----------------------------------------------------------
21 
ConfirmInfoBarGtk(ConfirmInfoBarDelegate * delegate)22 ConfirmInfoBarGtk::ConfirmInfoBarGtk(ConfirmInfoBarDelegate* delegate)
23     : InfoBar(delegate) {
24   confirm_hbox_ = gtk_chrome_shrinkable_hbox_new(FALSE, FALSE,
25                                                  kEndOfLabelSpacing);
26   // This alignment allocates the confirm hbox only as much space as it
27   // requests, and less if there is not enough available.
28   GtkWidget* align = gtk_alignment_new(0, 0, 0, 1);
29   gtk_container_add(GTK_CONTAINER(align), confirm_hbox_);
30   gtk_box_pack_start(GTK_BOX(hbox_), align, TRUE, TRUE, 0);
31 
32   // We add the buttons in reverse order and pack end instead of start so
33   // that the first widget to get shrunk is the label rather than the button(s).
34   AddButton(ConfirmInfoBarDelegate::BUTTON_OK);
35   AddButton(ConfirmInfoBarDelegate::BUTTON_CANCEL);
36 
37   std::string label_text = UTF16ToUTF8(delegate->GetMessageText());
38   GtkWidget* label = gtk_label_new(label_text.c_str());
39   gtk_util::ForceFontSizePixels(label, 13.4);
40   gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
41   gtk_util::CenterWidgetInHBox(confirm_hbox_, label, true, 0);
42   gtk_widget_modify_fg(label, GTK_STATE_NORMAL, &gtk_util::kGdkBlack);
43   g_signal_connect(label, "map",
44                    G_CALLBACK(gtk_util::InitLabelSizeRequestAndEllipsizeMode),
45                    NULL);
46 
47   std::string link_text = UTF16ToUTF8(delegate->GetLinkText());
48   if (link_text.empty())
49     return;
50 
51   GtkWidget* link = gtk_chrome_link_button_new(link_text.c_str());
52   gtk_misc_set_alignment(GTK_MISC(GTK_CHROME_LINK_BUTTON(link)->label), 0, 0.5);
53   g_signal_connect(link, "clicked", G_CALLBACK(OnLinkClickedThunk), this);
54   gtk_util::SetButtonTriggersNavigation(link);
55   // Until we switch to vector graphics, force the font size.
56   // 13.4px == 10pt @ 96dpi
57   gtk_util::ForceFontSizePixels(GTK_CHROME_LINK_BUTTON(link)->label, 13.4);
58   gtk_util::CenterWidgetInHBox(hbox_, link, true, kEndOfLabelSpacing);
59 }
60 
~ConfirmInfoBarGtk()61 ConfirmInfoBarGtk::~ConfirmInfoBarGtk() {
62 }
63 
AddButton(ConfirmInfoBarDelegate::InfoBarButton type)64 void ConfirmInfoBarGtk::AddButton(ConfirmInfoBarDelegate::InfoBarButton type) {
65   if (delegate_->AsConfirmInfoBarDelegate()->GetButtons() & type) {
66     GtkWidget* button = gtk_button_new_with_label(UTF16ToUTF8(
67         delegate_->AsConfirmInfoBarDelegate()->GetButtonLabel(type)).c_str());
68     gtk_util::CenterWidgetInHBox(confirm_hbox_, button, true, 0);
69     g_signal_connect(button, "clicked",
70                      G_CALLBACK(type == ConfirmInfoBarDelegate::BUTTON_OK ?
71                                 OnOkButtonThunk : OnCancelButtonThunk),
72                      this);
73   }
74 }
75 
OnOkButton(GtkWidget * widget)76 void ConfirmInfoBarGtk::OnOkButton(GtkWidget* widget) {
77   if (delegate_->AsConfirmInfoBarDelegate()->Accept())
78     RemoveInfoBar();
79 }
80 
OnCancelButton(GtkWidget * widget)81 void ConfirmInfoBarGtk::OnCancelButton(GtkWidget* widget) {
82   if (delegate_->AsConfirmInfoBarDelegate()->Cancel())
83     RemoveInfoBar();
84 }
85 
OnLinkClicked(GtkWidget * widget)86 void ConfirmInfoBarGtk::OnLinkClicked(GtkWidget* widget) {
87   if (delegate_->AsConfirmInfoBarDelegate()->LinkClicked(
88         gtk_util::DispositionForCurrentButtonPressEvent())) {
89     RemoveInfoBar();
90   }
91 }
92