Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: chrome/browser/input_window_dialog_gtk.cc

Issue 5582002: Move:... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/input_window_dialog.h ('k') | chrome/browser/input_window_dialog_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 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/input_window_dialog.h"
6
7 #include <gtk/gtk.h>
8
9 #include "app/gtk_signal.h"
10 #include "base/message_loop.h"
11 #include "base/scoped_ptr.h"
12 #include "base/string_piece.h"
13 #include "base/utf_string_conversions.h"
14 #include "chrome/browser/gtk/gtk_util.h"
15
16 class InputWindowDialogGtk : public InputWindowDialog {
17 public:
18 // Creates a dialog. Takes ownership of |delegate|.
19 InputWindowDialogGtk(GtkWindow* parent,
20 const std::string& window_title,
21 const std::string& label,
22 const std::string& contents,
23 Delegate* delegate);
24 virtual ~InputWindowDialogGtk();
25
26 virtual void Show();
27 virtual void Close();
28
29 private:
30 CHROMEG_CALLBACK_0(InputWindowDialogGtk, void, OnEntryChanged, GtkEditable*);
31 CHROMEGTK_CALLBACK_1(InputWindowDialogGtk, void, OnResponse, int);
32 CHROMEGTK_CALLBACK_1(InputWindowDialogGtk, gboolean,
33 OnWindowDeleteEvent, GdkEvent*);
34 CHROMEGTK_CALLBACK_0(InputWindowDialogGtk, void, OnWindowDestroy);
35
36 // The underlying gtk dialog window.
37 GtkWidget* dialog_;
38
39 // The GtkEntry in this form.
40 GtkWidget* input_;
41
42 // Our delegate. Consumes the window's output.
43 scoped_ptr<InputWindowDialog::Delegate> delegate_;
44 };
45
46
47 InputWindowDialogGtk::InputWindowDialogGtk(GtkWindow* parent,
48 const std::string& window_title,
49 const std::string& label,
50 const std::string& contents,
51 Delegate* delegate)
52 : dialog_(gtk_dialog_new_with_buttons(
53 window_title.c_str(),
54 parent,
55 GTK_DIALOG_MODAL,
56 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
57 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
58 NULL)),
59 delegate_(delegate) {
60 gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_ACCEPT);
61 gtk_dialog_set_has_separator(GTK_DIALOG(dialog_), FALSE);
62
63 GtkWidget* content_area = GTK_DIALOG(dialog_)->vbox;
64 gtk_box_set_spacing(GTK_BOX(content_area), 18);
65
66 GtkWidget* hbox = gtk_hbox_new(FALSE, 6);
67 GtkWidget* label_widget = gtk_label_new(label.c_str());
68 gtk_box_pack_start(GTK_BOX(hbox), label_widget, FALSE, FALSE, 0);
69
70 input_ = gtk_entry_new();
71 gtk_entry_set_text(GTK_ENTRY(input_), contents.c_str());
72 g_signal_connect(input_, "changed",
73 G_CALLBACK(OnEntryChangedThunk), this);
74 g_object_set(G_OBJECT(input_), "activates-default", TRUE, NULL);
75 gtk_box_pack_start(GTK_BOX(hbox), input_, TRUE, TRUE, 0);
76
77 gtk_widget_show_all(hbox);
78
79 gtk_box_pack_start(GTK_BOX(content_area), hbox, FALSE, FALSE, 0);
80
81 g_signal_connect(dialog_, "response",
82 G_CALLBACK(OnResponseThunk), this);
83 g_signal_connect(dialog_, "delete-event",
84 G_CALLBACK(OnWindowDeleteEventThunk), this);
85 g_signal_connect(dialog_, "destroy",
86 G_CALLBACK(OnWindowDestroyThunk), this);
87 }
88
89 InputWindowDialogGtk::~InputWindowDialogGtk() {
90 }
91
92 void InputWindowDialogGtk::Show() {
93 gtk_util::ShowDialog(dialog_);
94 }
95
96 void InputWindowDialogGtk::Close() {
97 // Under the model that we've inherited from Windows, dialogs can receive
98 // more than one Close() call inside the current message loop event.
99 if (dialog_) {
100 gtk_widget_destroy(GTK_WIDGET(dialog_));
101 dialog_ = NULL;
102 }
103 }
104
105 void InputWindowDialogGtk::OnEntryChanged(GtkEditable* entry) {
106 std::wstring value(UTF8ToWide(gtk_entry_get_text(GTK_ENTRY(entry))));
107 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog_),
108 GTK_RESPONSE_ACCEPT,
109 delegate_->IsValid(value));
110 }
111
112 void InputWindowDialogGtk::OnResponse(GtkWidget* dialog, int response_id) {
113 if (response_id == GTK_RESPONSE_ACCEPT) {
114 std::wstring value(UTF8ToWide(gtk_entry_get_text(GTK_ENTRY(input_))));
115 delegate_->InputAccepted(value);
116 } else {
117 delegate_->InputCanceled();
118 }
119 Close();
120 }
121
122 gboolean InputWindowDialogGtk::OnWindowDeleteEvent(GtkWidget* widget,
123 GdkEvent* event) {
124 Close();
125
126 // Return true to prevent the gtk dialog from being destroyed. Close will
127 // destroy it for us and the default gtk_dialog_delete_event_handler() will
128 // force the destruction without us being able to stop it.
129 return TRUE;
130 }
131
132 void InputWindowDialogGtk::OnWindowDestroy(GtkWidget* widget) {
133 MessageLoop::current()->DeleteSoon(FROM_HERE, this);
134 }
135
136 InputWindowDialog* InputWindowDialog::Create(gfx::NativeWindow parent,
137 const std::wstring& window_title,
138 const std::wstring& label,
139 const std::wstring& contents,
140 Delegate* delegate) {
141 return new InputWindowDialogGtk(parent,
142 WideToUTF8(window_title),
143 WideToUTF8(label),
144 WideToUTF8(contents),
145 delegate);
146 }
OLDNEW
« no previous file with comments | « chrome/browser/input_window_dialog.h ('k') | chrome/browser/input_window_dialog_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698