 Chromium Code Reviews
 Chromium Code Reviews Issue 1745024:
  Make a new yes/no messagebox wrapper function, use it in the bookmark alert....  (Closed) 
  Base URL: svn://chrome-svn/chrome/trunk/src/
    
  
    Issue 1745024:
  Make a new yes/no messagebox wrapper function, use it in the bookmark alert....  (Closed) 
  Base URL: svn://chrome-svn/chrome/trunk/src/| OLD | NEW | 
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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/platform_util.h" | 5 #include "chrome/browser/platform_util.h" | 
| 6 | 6 | 
| 7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> | 
| 8 | 8 | 
| 9 #include "app/gtk_util.h" | 9 #include "app/gtk_util.h" | 
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" | 
| 11 #include "base/message_loop.h" | |
| 11 #include "base/process_util.h" | 12 #include "base/process_util.h" | 
| 12 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" | 
| 13 #include "chrome/common/process_watcher.h" | 14 #include "chrome/common/process_watcher.h" | 
| 14 #include "chrome/browser/gtk/gtk_util.h" | 15 #include "chrome/browser/gtk/gtk_util.h" | 
| 15 #include "gfx/native_widget_types.h" | 16 #include "gfx/native_widget_types.h" | 
| 16 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" | 
| 17 | 18 | 
| 19 namespace { | |
| 20 | |
| 21 void SetDialogTitle(GtkWidget* dialog, const string16& title) { | |
| 22 gtk_window_set_title(GTK_WINDOW(dialog), UTF16ToUTF8(title).c_str()); | |
| 23 | |
| 24 #if !defined(OS_CHROMEOS) | |
| 25 // The following code requires the dialog to be realized. However, we host | |
| 26 // dialog's content in a Chrome window without really realize the dialog | |
| 27 // on ChromeOS. Thus, skip the following code for ChromeOS. | |
| 28 gtk_widget_realize(dialog); | |
| 29 | |
| 30 // Make sure it's big enough to show the title. | |
| 31 GtkRequisition req; | |
| 32 gtk_widget_size_request(dialog, &req); | |
| 33 int width; | |
| 34 gtk_util::GetWidgetSizeFromCharacters(dialog, title.length(), 0, | |
| 35 &width, NULL); | |
| 36 // The fudge factor accounts for extra space needed by the frame | |
| 37 // decorations as well as width differences between average text and the | |
| 38 // actual title text. | |
| 39 width = width * 1.2 + 50; | |
| 40 | |
| 41 if (width > req.width) | |
| 42 gtk_widget_set_size_request(dialog, width, -1); | |
| 43 #endif // !defined(OS_CHROMEOS) | |
| 44 } | |
| 45 | |
| 46 int g_dialog_response; | |
| 47 | |
| 48 void HandleOnResponseDialog(GtkWidget* widget, | |
| 49 int response, | |
| 50 void* user_data) { | |
| 51 g_dialog_response = response; | |
| 52 gtk_widget_destroy(widget); | |
| 53 MessageLoop::current()->QuitNow(); | |
| 54 } | |
| 55 | |
| 56 } // namespace | |
| 57 | |
| 18 namespace platform_util { | 58 namespace platform_util { | 
| 19 | 59 | 
| 20 gfx::NativeWindow GetTopLevel(gfx::NativeView view) { | 60 gfx::NativeWindow GetTopLevel(gfx::NativeView view) { | 
| 21 // A detached widget won't have a toplevel window as an ancestor, so we can't | 61 // A detached widget won't have a toplevel window as an ancestor, so we can't | 
| 22 // assume that the query for toplevel will return a window. | 62 // assume that the query for toplevel will return a window. | 
| 23 GtkWidget* toplevel = gtk_widget_get_ancestor(view, GTK_TYPE_WINDOW); | 63 GtkWidget* toplevel = gtk_widget_get_ancestor(view, GTK_TYPE_WINDOW); | 
| 24 return GTK_IS_WINDOW(toplevel) ? GTK_WINDOW(toplevel) : NULL; | 64 return GTK_IS_WINDOW(toplevel) ? GTK_WINDOW(toplevel) : NULL; | 
| 25 } | 65 } | 
| 26 | 66 | 
| 27 bool IsWindowActive(gfx::NativeWindow window) { | 67 bool IsWindowActive(gfx::NativeWindow window) { | 
| 28 return gtk_window_is_active(window); | 68 return gtk_window_is_active(window); | 
| 29 } | 69 } | 
| 30 | 70 | 
| 31 void ActivateWindow(gfx::NativeWindow window) { | 71 void ActivateWindow(gfx::NativeWindow window) { | 
| 32 gtk_window_present(window); | 72 gtk_window_present(window); | 
| 33 } | 73 } | 
| 34 | 74 | 
| 35 bool IsVisible(gfx::NativeView view) { | 75 bool IsVisible(gfx::NativeView view) { | 
| 36 return GTK_WIDGET_VISIBLE(view); | 76 return GTK_WIDGET_VISIBLE(view); | 
| 37 } | 77 } | 
| 38 | 78 | 
| 39 void SimpleErrorBox(gfx::NativeWindow parent, | 79 void SimpleErrorBox(gfx::NativeWindow parent, | 
| 40 const string16& title, | 80 const string16& title, | 
| 41 const string16& message) { | 81 const string16& message) { | 
| 42 GtkWidget* dialog = gtk_message_dialog_new(parent, GTK_DIALOG_MODAL, | 82 GtkWidget* dialog = gtk_message_dialog_new(parent, GTK_DIALOG_MODAL, | 
| 43 GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", UTF16ToUTF8(message).c_str()); | 83 GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", UTF16ToUTF8(message).c_str()); | 
| 44 gtk_util::ApplyMessageDialogQuirks(dialog); | 84 gtk_util::ApplyMessageDialogQuirks(dialog); | 
| 45 gtk_window_set_title(GTK_WINDOW(dialog), UTF16ToUTF8(title).c_str()); | 85 SetDialogTitle(dialog, title); | 
| 46 | 86 | 
| 47 g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), NULL); | 87 g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), NULL); | 
| 48 gtk_util::ShowDialog(dialog); | 88 gtk_util::ShowDialog(dialog); | 
| 89 } | |
| 49 | 90 | 
| 50 #if !defined(OS_CHROMEOS) | 91 bool SimpleYesNoBox(gfx::NativeWindow parent, | 
| 51 // The following code requires the dialog to be realized. However, we host | 92 const string16& title, | 
| 52 // dialog's content in a Chrome window without really realize the dialog | 93 const string16& message) { | 
| 53 // on ChromeOS. Thus, skip the following code for ChromeOS. | 94 GtkWidget* dialog = gtk_message_dialog_new(parent, GTK_DIALOG_MODAL, | 
| 95 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, "%s", | |
| 96 UTF16ToUTF8(message).c_str()); | |
| 97 gtk_util::ApplyMessageDialogQuirks(dialog); | |
| 98 SetDialogTitle(dialog, title); | |
| 54 | 99 | 
| 55 // Make sure it's big enough to show the title. | 100 g_signal_connect(dialog, | 
| 56 GtkRequisition req; | 101 "response", | 
| 57 gtk_widget_size_request(dialog, &req); | 102 G_CALLBACK(HandleOnResponseDialog), | 
| 58 int width; | 103 NULL); | 
| 59 gtk_util::GetWidgetSizeFromCharacters(dialog, title.length(), 0, | 104 gtk_util::ShowDialog(dialog); | 
| 60 &width, NULL); | 105 // Not gtk_dialog_run as it prevents timers from running in the unit tests. | 
| 61 // The fudge factor accounts for extra space needed by the frame | 106 MessageLoop::current()->Run(); | 
| 62 // decorations as well as width differences between average text and the | 107 return (g_dialog_response == GTK_RESPONSE_YES); | 
| 
Evan Stade
2010/06/25 01:02:35
nit: no ()
 | |
| 63 // actual title text. | |
| 64 width = width * 1.2 + 50; | |
| 65 | |
| 66 if (width > req.width) | |
| 67 gtk_widget_set_size_request(dialog, width, -1); | |
| 68 #endif // !defined(OS_CHROMEOS) | |
| 69 } | 108 } | 
| 70 | 109 | 
| 71 /* Warning: this may be either Linux or ChromeOS */ | 110 /* Warning: this may be either Linux or ChromeOS */ | 
| 72 string16 GetVersionStringModifier() { | 111 string16 GetVersionStringModifier() { | 
| 73 char* env = getenv("CHROME_VERSION_EXTRA"); | 112 char* env = getenv("CHROME_VERSION_EXTRA"); | 
| 74 if (!env) | 113 if (!env) | 
| 75 return string16(); | 114 return string16(); | 
| 76 std::string modifier(env); | 115 std::string modifier(env); | 
| 77 | 116 | 
| 78 #if defined(GOOGLE_CHROME_BUILD) | 117 #if defined(GOOGLE_CHROME_BUILD) | 
| 79 // Only ever return "", "unknown", "dev" or "beta" in a branded build. | 118 // Only ever return "", "unknown", "dev" or "beta" in a branded build. | 
| 80 if (modifier == "unstable") // linux version of "dev" | 119 if (modifier == "unstable") // linux version of "dev" | 
| 81 modifier = "dev"; | 120 modifier = "dev"; | 
| 82 if (modifier == "stable") { | 121 if (modifier == "stable") { | 
| 83 modifier = ""; | 122 modifier = ""; | 
| 84 } else if ((modifier == "dev") || (modifier == "beta")) { | 123 } else if ((modifier == "dev") || (modifier == "beta")) { | 
| 85 // do nothing. | 124 // do nothing. | 
| 86 } else { | 125 } else { | 
| 87 modifier = "unknown"; | 126 modifier = "unknown"; | 
| 88 } | 127 } | 
| 89 #endif | 128 #endif | 
| 90 | 129 | 
| 91 return ASCIIToUTF16(modifier); | 130 return ASCIIToUTF16(modifier); | 
| 92 } | 131 } | 
| 93 | 132 | 
| 94 } // namespace platform_util | 133 } // namespace platform_util | 
| OLD | NEW |