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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/gtk/tab_modal_confirm_dialog_gtk.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_dialogs.h"
12 #include "chrome/browser/ui/browser_finder.h"
13 #include "chrome/browser/ui/gtk/event_utils.h"
14 #include "chrome/browser/ui/gtk/gtk_theme_service.h"
15 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h"
16 #include "components/web_modal/web_contents_modal_dialog_manager.h"
17 #include "content/public/browser/notification_types.h"
18 #include "grit/generated_resources.h"
19 #include "ui/base/gtk/gtk_hig_constants.h"
20 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/gfx/image/image.h"
23 using web_modal::WebContentsModalDialogManager
;
25 TabModalConfirmDialog
* TabModalConfirmDialog::Create(
26 TabModalConfirmDialogDelegate
* delegate
,
27 content::WebContents
* web_contents
) {
28 return new TabModalConfirmDialogGtk(delegate
, web_contents
);
31 TabModalConfirmDialogGtk::TabModalConfirmDialogGtk(
32 TabModalConfirmDialogDelegate
* delegate
,
33 content::WebContents
* web_contents
)
34 : delegate_(delegate
),
37 dialog_
= gtk_vbox_new(FALSE
, ui::kContentAreaSpacing
);
38 GtkWidget
* label
= gtk_label_new(
39 base::UTF16ToUTF8(delegate
->GetMessage()).c_str());
40 gfx::Image
* icon
= delegate
->GetIcon();
41 GtkWidget
* image
= icon
? gtk_image_new_from_pixbuf(icon
->ToGdkPixbuf())
42 : gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION
,
43 GTK_ICON_SIZE_DIALOG
);
44 gtk_misc_set_alignment(GTK_MISC(image
), 0.5, 0.0);
46 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.0);
47 gtk_label_set_line_wrap(GTK_LABEL(label
), TRUE
);
48 gtk_label_set_selectable(GTK_LABEL(label
), TRUE
);
50 GtkWidget
* hbox
= gtk_hbox_new(FALSE
, ui::kControlSpacing
);
52 gtk_box_pack_start(GTK_BOX(hbox
), image
, FALSE
, FALSE
, 0);
54 GtkWidget
* vbox
= gtk_vbox_new(FALSE
, ui::kContentAreaSpacing
);
56 gtk_box_pack_start(GTK_BOX(vbox
), label
, TRUE
, TRUE
, 0);
58 base::string16 link_text
= delegate
->GetLinkText();
59 if (!link_text
.empty()) {
60 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents
);
61 GtkThemeService
* theme_service
= GtkThemeService::GetFrom(
64 GtkWidget
* link
= theme_service
->BuildChromeLinkButton(base::UTF16ToUTF8(
66 g_signal_connect(link
, "clicked", G_CALLBACK(OnLinkClickedThunk
), this);
67 GtkWidget
* link_align
= gtk_alignment_new(0, 0, 0, 0);
68 gtk_container_add(GTK_CONTAINER(link_align
), link
);
70 gtk_box_pack_end(GTK_BOX(vbox
), link_align
, FALSE
, FALSE
, 0);
73 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, TRUE
, TRUE
, 0);
74 gtk_box_pack_start(GTK_BOX(dialog_
), hbox
, FALSE
, FALSE
, 0);
76 GtkWidget
* buttonBox
= gtk_hbutton_box_new();
77 gtk_button_box_set_layout(GTK_BUTTON_BOX(buttonBox
), GTK_BUTTONBOX_END
);
78 gtk_box_set_spacing(GTK_BOX(buttonBox
), ui::kControlSpacing
);
79 gtk_box_pack_end(GTK_BOX(dialog_
), buttonBox
, FALSE
, TRUE
, 0);
81 cancel_
= gtk_button_new_with_label(
82 base::UTF16ToUTF8(delegate
->GetCancelButtonTitle()).c_str());
83 const char* cancel_button_icon_id
= delegate
->GetCancelButtonIcon();
84 if (cancel_button_icon_id
) {
85 gtk_button_set_image(GTK_BUTTON(cancel_
), gtk_image_new_from_stock(
86 cancel_button_icon_id
, GTK_ICON_SIZE_BUTTON
));
88 g_signal_connect(cancel_
, "clicked", G_CALLBACK(OnCancelThunk
), this);
89 gtk_box_pack_end(GTK_BOX(buttonBox
), cancel_
, FALSE
, TRUE
, 0);
91 ok_
= gtk_button_new_with_label(
92 base::UTF16ToUTF8(delegate
->GetAcceptButtonTitle()).c_str());
93 const char* accept_button_icon_id
= delegate
->GetAcceptButtonIcon();
94 if (accept_button_icon_id
) {
95 gtk_button_set_image(GTK_BUTTON(ok_
), gtk_image_new_from_stock(
96 accept_button_icon_id
, GTK_ICON_SIZE_BUTTON
));
98 g_signal_connect(ok_
, "clicked", G_CALLBACK(OnAcceptThunk
), this);
99 gtk_box_pack_end(GTK_BOX(buttonBox
), ok_
, FALSE
, TRUE
, 0);
101 g_signal_connect(dialog_
, "destroy", G_CALLBACK(OnDestroyThunk
), this);
103 window_
= CreateWebContentsModalDialogGtk(dialog_
, cancel_
);
104 delegate_
->set_close_delegate(this);
106 WebContentsModalDialogManager
* web_contents_modal_dialog_manager
=
107 WebContentsModalDialogManager::FromWebContents(web_contents
);
108 web_contents_modal_dialog_manager
->ShowDialog(window_
);
111 TabModalConfirmDialogGtk::~TabModalConfirmDialogGtk() {
112 // Provide a disposition in case the dialog was closed without accepting or
116 gtk_widget_destroy(dialog_
);
119 void TabModalConfirmDialogGtk::AcceptTabModalDialog() {
123 void TabModalConfirmDialogGtk::CancelTabModalDialog() {
127 void TabModalConfirmDialogGtk::CloseDialog() {
130 gtk_widget_destroy(window_
);
134 void TabModalConfirmDialogGtk::OnAccept(GtkWidget
* widget
) {
138 void TabModalConfirmDialogGtk::OnCancel(GtkWidget
* widget
) {
142 void TabModalConfirmDialogGtk::OnLinkClicked(GtkWidget
* widget
) {
143 delegate_
->LinkClicked(event_utils::DispositionForCurrentButtonPressEvent());
146 void TabModalConfirmDialogGtk::OnDestroy(GtkWidget
* widget
) {