OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/gtk/infobar_gtk.h" | 5 #include "chrome/browser/gtk/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/gtk/custom_button.h" | 10 #include "chrome/browser/gtk/custom_button.h" |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 | 141 |
142 void InfoBar::Observe(NotificationType type, | 142 void InfoBar::Observe(NotificationType type, |
143 const NotificationSource& source, | 143 const NotificationSource& source, |
144 const NotificationDetails& details) { | 144 const NotificationDetails& details) { |
145 UpdateBorderColor(); | 145 UpdateBorderColor(); |
146 } | 146 } |
147 | 147 |
148 // TODO(joth): This method factors out some common functionality between the | 148 // TODO(joth): This method factors out some common functionality between the |
149 // various derived infobar classes, however the class hierarchy itself could | 149 // various derived infobar classes, however the class hierarchy itself could |
150 // use refactoring to reduce this duplication. http://crbug.com/38924 | 150 // use refactoring to reduce this duplication. http://crbug.com/38924 |
151 void InfoBar::AddLabelAndLink(const std::wstring& display_text, | 151 void InfoBar::AddLabelAndLink(const string16& display_text, |
152 const std::wstring& link_text, | 152 const string16& link_text, |
153 size_t link_offset, | 153 size_t link_offset, |
154 guint link_padding, | 154 guint link_padding, |
155 GCallback callback) { | 155 GCallback callback) { |
156 GtkWidget* link_button = NULL; | 156 GtkWidget* link_button = NULL; |
157 if (link_text.empty()) { | 157 if (link_text.empty()) { |
158 // No link text, so skip creating the link and splitting display_text. | 158 // No link text, so skip creating the link and splitting display_text. |
159 link_offset = std::wstring::npos; | 159 link_offset = std::wstring::npos; |
160 } else { | 160 } else { |
161 // If we have some link text, create the link button. | 161 // If we have some link text, create the link button. |
162 link_button = gtk_chrome_link_button_new(WideToUTF8(link_text).c_str()); | 162 link_button = gtk_chrome_link_button_new(UTF16ToUTF8(link_text).c_str()); |
163 gtk_chrome_link_button_set_use_gtk_theme( | 163 gtk_chrome_link_button_set_use_gtk_theme( |
164 GTK_CHROME_LINK_BUTTON(link_button), FALSE); | 164 GTK_CHROME_LINK_BUTTON(link_button), FALSE); |
165 DCHECK(callback); | 165 DCHECK(callback); |
166 g_signal_connect(link_button, "clicked", callback, this); | 166 g_signal_connect(link_button, "clicked", callback, this); |
167 gtk_util::SetButtonTriggersNavigation(link_button); | 167 gtk_util::SetButtonTriggersNavigation(link_button); |
168 } | 168 } |
169 | 169 |
170 GtkWidget* hbox = gtk_hbox_new(FALSE, 0); | 170 GtkWidget* hbox = gtk_hbox_new(FALSE, 0); |
171 // We want the link to be horizontally shrinkable, so that the Chrome | 171 // We want the link to be horizontally shrinkable, so that the Chrome |
172 // window can be resized freely even with a very long link. | 172 // window can be resized freely even with a very long link. |
173 gtk_widget_set_size_request(hbox, 0, -1); | 173 gtk_widget_set_size_request(hbox, 0, -1); |
174 gtk_box_pack_start(GTK_BOX(hbox_), hbox, TRUE, TRUE, 0); | 174 gtk_box_pack_start(GTK_BOX(hbox_), hbox, TRUE, TRUE, 0); |
175 | 175 |
176 // If link_offset is npos, we right-align the link instead of embedding it | 176 // If link_offset is npos, we right-align the link instead of embedding it |
177 // in the text. | 177 // in the text. |
178 if (link_offset == std::wstring::npos) { | 178 if (link_offset == std::wstring::npos) { |
179 if (link_button) | 179 if (link_button) |
180 gtk_box_pack_end(GTK_BOX(hbox), link_button, FALSE, FALSE, 0); | 180 gtk_box_pack_end(GTK_BOX(hbox), link_button, FALSE, FALSE, 0); |
181 GtkWidget* label = gtk_label_new(WideToUTF8(display_text).c_str()); | 181 GtkWidget* label = gtk_label_new(UTF16ToUTF8(display_text).c_str()); |
182 // In order to avoid the link_button and the label overlapping with each | 182 // In order to avoid the link_button and the label overlapping with each |
183 // other, we make the label shrinkable. | 183 // other, we make the label shrinkable. |
184 gtk_widget_set_size_request(label, 0, -1); | 184 gtk_widget_set_size_request(label, 0, -1); |
185 gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END); | 185 gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END); |
186 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); | 186 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); |
187 gtk_widget_modify_fg(label, GTK_STATE_NORMAL, &gfx::kGdkBlack); | 187 gtk_widget_modify_fg(label, GTK_STATE_NORMAL, &gfx::kGdkBlack); |
188 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0); | 188 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0); |
189 } else { | 189 } else { |
190 DCHECK(link_button); | 190 DCHECK(link_button); |
191 // Need to insert the link inside the display text. | 191 // Need to insert the link inside the display text. |
192 GtkWidget* initial_label = gtk_label_new( | 192 GtkWidget* initial_label = gtk_label_new( |
193 WideToUTF8(display_text.substr(0, link_offset)).c_str()); | 193 UTF16ToUTF8(display_text.substr(0, link_offset)).c_str()); |
194 GtkWidget* trailing_label = gtk_label_new( | 194 GtkWidget* trailing_label = gtk_label_new( |
195 WideToUTF8(display_text.substr(link_offset)).c_str()); | 195 UTF16ToUTF8(display_text.substr(link_offset)).c_str()); |
196 | 196 |
197 // TODO(joth): Unlike the right-align case above, none of the label widgets | 197 // TODO(joth): Unlike the right-align case above, none of the label widgets |
198 // are set as shrinkable here, meaning the text will run under the close | 198 // are set as shrinkable here, meaning the text will run under the close |
199 // button etc. when the width is restricted, rather than eliding. | 199 // button etc. when the width is restricted, rather than eliding. |
200 gtk_widget_modify_fg(initial_label, GTK_STATE_NORMAL, &gfx::kGdkBlack); | 200 gtk_widget_modify_fg(initial_label, GTK_STATE_NORMAL, &gfx::kGdkBlack); |
201 gtk_widget_modify_fg(trailing_label, GTK_STATE_NORMAL, &gfx::kGdkBlack); | 201 gtk_widget_modify_fg(trailing_label, GTK_STATE_NORMAL, &gfx::kGdkBlack); |
202 | 202 |
203 // We don't want any spacing between the elements, so we pack them into | 203 // We don't want any spacing between the elements, so we pack them into |
204 // this hbox that doesn't use kElementPadding. | 204 // this hbox that doesn't use kElementPadding. |
205 gtk_box_pack_start(GTK_BOX(hbox), initial_label, FALSE, FALSE, 0); | 205 gtk_box_pack_start(GTK_BOX(hbox), initial_label, FALSE, FALSE, 0); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 | 282 |
283 return FALSE; | 283 return FALSE; |
284 } | 284 } |
285 | 285 |
286 // AlertInfoBar ---------------------------------------------------------------- | 286 // AlertInfoBar ---------------------------------------------------------------- |
287 | 287 |
288 class AlertInfoBar : public InfoBar { | 288 class AlertInfoBar : public InfoBar { |
289 public: | 289 public: |
290 explicit AlertInfoBar(AlertInfoBarDelegate* delegate) | 290 explicit AlertInfoBar(AlertInfoBarDelegate* delegate) |
291 : InfoBar(delegate) { | 291 : InfoBar(delegate) { |
292 AddLabelAndLink(delegate->GetMessageText(), std::wstring(), 0, 0, NULL); | 292 AddLabelAndLink(delegate->GetMessageText(), string16(), 0, 0, NULL); |
293 gtk_widget_show_all(border_bin_.get()); | 293 gtk_widget_show_all(border_bin_.get()); |
294 } | 294 } |
295 }; | 295 }; |
296 | 296 |
297 // LinkInfoBar ----------------------------------------------------------------- | 297 // LinkInfoBar ----------------------------------------------------------------- |
298 | 298 |
299 class LinkInfoBar : public InfoBar { | 299 class LinkInfoBar : public InfoBar { |
300 public: | 300 public: |
301 explicit LinkInfoBar(LinkInfoBarDelegate* delegate) | 301 explicit LinkInfoBar(LinkInfoBarDelegate* delegate) |
302 : InfoBar(delegate) { | 302 : InfoBar(delegate) { |
303 size_t link_offset; | 303 size_t link_offset; |
304 std::wstring display_text = | 304 string16 display_text = |
305 delegate->GetMessageTextWithOffset(&link_offset); | 305 delegate->GetMessageTextWithOffset(&link_offset); |
306 std::wstring link_text = delegate->GetLinkText(); | 306 string16 link_text = delegate->GetLinkText(); |
307 AddLabelAndLink(display_text, link_text, link_offset, 0, | 307 AddLabelAndLink(display_text, link_text, link_offset, 0, |
308 G_CALLBACK(OnLinkClick)); | 308 G_CALLBACK(OnLinkClick)); |
309 gtk_widget_show_all(border_bin_.get()); | 309 gtk_widget_show_all(border_bin_.get()); |
310 } | 310 } |
311 | 311 |
312 private: | 312 private: |
313 static void OnLinkClick(GtkWidget* button, LinkInfoBar* link_info_bar) { | 313 static void OnLinkClick(GtkWidget* button, LinkInfoBar* link_info_bar) { |
314 if (link_info_bar->delegate_->AsLinkInfoBarDelegate()-> | 314 if (link_info_bar->delegate_->AsLinkInfoBarDelegate()-> |
315 LinkClicked(gtk_util::DispositionForCurrentButtonPressEvent())) { | 315 LinkClicked(gtk_util::DispositionForCurrentButtonPressEvent())) { |
316 link_info_bar->RemoveInfoBar(); | 316 link_info_bar->RemoveInfoBar(); |
317 } | 317 } |
318 } | 318 } |
319 }; | 319 }; |
320 | 320 |
321 // ConfirmInfoBar -------------------------------------------------------------- | 321 // ConfirmInfoBar -------------------------------------------------------------- |
322 | 322 |
323 class ConfirmInfoBar : public InfoBar { | 323 class ConfirmInfoBar : public InfoBar { |
324 public: | 324 public: |
325 explicit ConfirmInfoBar(ConfirmInfoBarDelegate* delegate) | 325 explicit ConfirmInfoBar(ConfirmInfoBarDelegate* delegate) |
326 : InfoBar(delegate) { | 326 : InfoBar(delegate) { |
327 AddConfirmButton(ConfirmInfoBarDelegate::BUTTON_CANCEL); | 327 AddConfirmButton(ConfirmInfoBarDelegate::BUTTON_CANCEL); |
328 AddConfirmButton(ConfirmInfoBarDelegate::BUTTON_OK); | 328 AddConfirmButton(ConfirmInfoBarDelegate::BUTTON_OK); |
329 std::wstring display_text = delegate->GetMessageText(); | 329 string16 display_text = delegate->GetMessageText(); |
330 std::wstring link_text = delegate->GetLinkText(); | 330 string16 link_text = delegate->GetLinkText(); |
331 AddLabelAndLink(display_text, link_text, display_text.size(), | 331 AddLabelAndLink(display_text, link_text, display_text.size(), |
332 kElementPadding, G_CALLBACK(OnLinkClick)); | 332 kElementPadding, G_CALLBACK(OnLinkClick)); |
333 gtk_widget_show_all(border_bin_.get()); | 333 gtk_widget_show_all(border_bin_.get()); |
334 } | 334 } |
335 | 335 |
336 private: | 336 private: |
337 // Adds a button to the info bar by type. It will do nothing if the delegate | 337 // Adds a button to the info bar by type. It will do nothing if the delegate |
338 // doesn't specify a button of the given type. | 338 // doesn't specify a button of the given type. |
339 void AddConfirmButton(ConfirmInfoBarDelegate::InfoBarButton type) { | 339 void AddConfirmButton(ConfirmInfoBarDelegate::InfoBarButton type) { |
340 if (delegate_->AsConfirmInfoBarDelegate()->GetButtons() & type) { | 340 if (delegate_->AsConfirmInfoBarDelegate()->GetButtons() & type) { |
341 GtkWidget* button = gtk_button_new_with_label(WideToUTF8( | 341 GtkWidget* button = gtk_button_new_with_label(UTF16ToUTF8( |
342 delegate_->AsConfirmInfoBarDelegate()->GetButtonLabel(type)).c_str()); | 342 delegate_->AsConfirmInfoBarDelegate()->GetButtonLabel(type)).c_str()); |
343 gtk_util::CenterWidgetInHBox(hbox_, button, true, 0); | 343 gtk_util::CenterWidgetInHBox(hbox_, button, true, 0); |
344 g_signal_connect(button, "clicked", | 344 g_signal_connect(button, "clicked", |
345 G_CALLBACK(type == ConfirmInfoBarDelegate::BUTTON_OK ? | 345 G_CALLBACK(type == ConfirmInfoBarDelegate::BUTTON_OK ? |
346 OnOkButton : OnCancelButton), | 346 OnOkButton : OnCancelButton), |
347 this); | 347 this); |
348 } | 348 } |
349 } | 349 } |
350 | 350 |
351 static void OnCancelButton(GtkWidget* button, ConfirmInfoBar* info_bar) { | 351 static void OnCancelButton(GtkWidget* button, ConfirmInfoBar* info_bar) { |
(...skipping 24 matching lines...) Expand all Loading... |
376 | 376 |
377 InfoBar* LinkInfoBarDelegate::CreateInfoBar() { | 377 InfoBar* LinkInfoBarDelegate::CreateInfoBar() { |
378 return new LinkInfoBar(this); | 378 return new LinkInfoBar(this); |
379 } | 379 } |
380 | 380 |
381 // ConfirmInfoBarDelegate, InfoBarDelegate overrides: -------------------------- | 381 // ConfirmInfoBarDelegate, InfoBarDelegate overrides: -------------------------- |
382 | 382 |
383 InfoBar* ConfirmInfoBarDelegate::CreateInfoBar() { | 383 InfoBar* ConfirmInfoBarDelegate::CreateInfoBar() { |
384 return new ConfirmInfoBar(this); | 384 return new ConfirmInfoBar(this); |
385 } | 385 } |
OLD | NEW |