| 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/bookmark_utils_gtk.h" | 5 #include "chrome/browser/gtk/bookmark_utils_gtk.h" |
| 6 | 6 |
| 7 #include "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
| 8 #include "base/gfx/gtk_util.h" | 8 #include "base/gfx/gtk_util.h" |
| 9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 const GdkColor kBackgroundColor = GDK_COLOR_RGB(0xe6, 0xed, 0xf4); | 30 const GdkColor kBackgroundColor = GDK_COLOR_RGB(0xe6, 0xed, 0xf4); |
| 31 | 31 |
| 32 // Color of the button text, taken from TextButtonView. | 32 // Color of the button text, taken from TextButtonView. |
| 33 const GdkColor kEnabledColor = GDK_COLOR_RGB(6, 45, 117); | 33 const GdkColor kEnabledColor = GDK_COLOR_RGB(6, 45, 117); |
| 34 const GdkColor kDisabledColor = GDK_COLOR_RGB(161, 161, 146); | 34 const GdkColor kDisabledColor = GDK_COLOR_RGB(161, 161, 146); |
| 35 // TextButtonView uses 255, 255, 255 with opacity of 200. We don't support | 35 // TextButtonView uses 255, 255, 255 with opacity of 200. We don't support |
| 36 // transparent text though, so just use a slightly lighter version of | 36 // transparent text though, so just use a slightly lighter version of |
| 37 // kEnabledColor. | 37 // kEnabledColor. |
| 38 const GdkColor kHighlightColor = GDK_COLOR_RGB(56, 95, 167); | 38 const GdkColor kHighlightColor = GDK_COLOR_RGB(56, 95, 167); |
| 39 | 39 |
| 40 std::string DoubleUnderscores(const std::string& text) { | |
| 41 std::string ret; | |
| 42 ret.reserve(text.length() * 2); | |
| 43 for (size_t i = 0; i < text.length(); ++i) { | |
| 44 if ('_' == text[i]) { | |
| 45 ret.push_back('_'); | |
| 46 ret.push_back('_'); | |
| 47 } else { | |
| 48 ret.push_back(text[i]); | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 return ret; | |
| 53 } | |
| 54 | |
| 55 // Recursively search for label among the children of |widget|. | |
| 56 void SearchForLabel(GtkWidget* widget, gpointer data) { | |
| 57 if (GTK_IS_LABEL(widget)) { | |
| 58 *reinterpret_cast<GtkWidget**>(data) = widget; | |
| 59 } else if (GTK_IS_CONTAINER(widget)) { | |
| 60 gtk_container_foreach(GTK_CONTAINER(widget), SearchForLabel, data); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 void* AsVoid(const BookmarkNode* node) { | 40 void* AsVoid(const BookmarkNode* node) { |
| 65 return const_cast<BookmarkNode*>(node); | 41 return const_cast<BookmarkNode*>(node); |
| 66 } | 42 } |
| 67 | 43 |
| 68 } // namespace | 44 } // namespace |
| 69 | 45 |
| 70 namespace bookmark_utils { | 46 namespace bookmark_utils { |
| 71 | 47 |
| 72 const char kBookmarkNode[] = "bookmark-node"; | 48 const char kBookmarkNode[] = "bookmark-node"; |
| 73 | 49 |
| 50 const int kBarButtonPadding = 2; |
| 51 |
| 74 GdkPixbuf* GetFolderIcon() { | 52 GdkPixbuf* GetFolderIcon() { |
| 75 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 53 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 76 static GdkPixbuf* default_folder_icon = rb.GetPixbufNamed( | 54 static GdkPixbuf* default_folder_icon = rb.GetPixbufNamed( |
| 77 IDR_BOOKMARK_BAR_FOLDER); | 55 IDR_BOOKMARK_BAR_FOLDER); |
| 78 return default_folder_icon; | 56 return default_folder_icon; |
| 79 } | 57 } |
| 80 | 58 |
| 81 GdkPixbuf* GetDefaultFavicon() { | 59 GdkPixbuf* GetDefaultFavicon() { |
| 82 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 60 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 83 static GdkPixbuf* default_bookmark_icon = rb.GetPixbufNamed( | 61 static GdkPixbuf* default_bookmark_icon = rb.GetPixbufNamed( |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 100 |
| 123 return window; | 101 return window; |
| 124 } | 102 } |
| 125 | 103 |
| 126 void ConfigureButtonForNode(const BookmarkNode* node, BookmarkModel* model, | 104 void ConfigureButtonForNode(const BookmarkNode* node, BookmarkModel* model, |
| 127 GtkWidget* button) { | 105 GtkWidget* button) { |
| 128 std::string tooltip = BuildTooltipFor(node); | 106 std::string tooltip = BuildTooltipFor(node); |
| 129 if (!tooltip.empty()) | 107 if (!tooltip.empty()) |
| 130 gtk_widget_set_tooltip_text(button, tooltip.c_str()); | 108 gtk_widget_set_tooltip_text(button, tooltip.c_str()); |
| 131 | 109 |
| 110 // We pack the button manually (rather than using gtk_button_set_*) so that |
| 111 // we can have finer control over its label. |
| 132 GdkPixbuf* pixbuf = bookmark_utils::GetPixbufForNode(node, model); | 112 GdkPixbuf* pixbuf = bookmark_utils::GetPixbufForNode(node, model); |
| 133 gtk_button_set_image(GTK_BUTTON(button), gtk_image_new_from_pixbuf(pixbuf)); | 113 GtkWidget* image = gtk_image_new_from_pixbuf(pixbuf); |
| 134 g_object_unref(pixbuf); | 114 g_object_unref(pixbuf); |
| 135 | 115 |
| 136 // TODO(erg): Consider a soft maximum instead of this hard 15. | 116 GtkWidget* label = gtk_label_new(WideToUTF8(node->GetTitle()).c_str()); |
| 137 std::wstring title = node->GetTitle(); | 117 gtk_label_set_max_width_chars(GTK_LABEL(label), kMaxCharsOnAButton); |
| 138 // Don't treat underscores as mnemonics. | 118 gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END); |
| 139 // O, that we could just use gtk_button_set_use_underline()! | |
| 140 // See http://bugzilla.gnome.org/show_bug.cgi?id=586330 | |
| 141 std::string text = DoubleUnderscores(WideToUTF8(title)); | |
| 142 gtk_button_set_label(GTK_BUTTON(button), text.c_str()); | |
| 143 GtkWidget* label = NULL; | |
| 144 gtk_container_foreach(GTK_CONTAINER(button), SearchForLabel, &label); | |
| 145 if (label) { | |
| 146 gtk_label_set_max_width_chars(GTK_LABEL(label), kMaxCharsOnAButton); | |
| 147 gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END); | |
| 148 } | |
| 149 | 119 |
| 150 SetButtonTextColors(button); | 120 GtkWidget* box = gtk_hbox_new(FALSE, kBarButtonPadding); |
| 121 gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0); |
| 122 gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0); |
| 123 gtk_container_add(GTK_CONTAINER(button), box); |
| 124 |
| 125 SetButtonTextColors(label); |
| 151 g_object_set_data(G_OBJECT(button), bookmark_utils::kBookmarkNode, | 126 g_object_set_data(G_OBJECT(button), bookmark_utils::kBookmarkNode, |
| 152 AsVoid(node)); | 127 AsVoid(node)); |
| 153 } | 128 } |
| 154 | 129 |
| 155 std::string BuildTooltipFor(const BookmarkNode* node) { | 130 std::string BuildTooltipFor(const BookmarkNode* node) { |
| 156 // TODO(erg): Actually build the tooltip. For now, we punt and just return | 131 // TODO(erg): Actually build the tooltip. For now, we punt and just return |
| 157 // the URL. | 132 // the URL. |
| 158 return node->GetURL().possibly_invalid_spec(); | 133 return node->GetURL().possibly_invalid_spec(); |
| 159 } | 134 } |
| 160 | 135 |
| 161 const BookmarkNode* BookmarkNodeForWidget(GtkWidget* widget) { | 136 const BookmarkNode* BookmarkNodeForWidget(GtkWidget* widget) { |
| 162 return reinterpret_cast<const BookmarkNode*>( | 137 return reinterpret_cast<const BookmarkNode*>( |
| 163 g_object_get_data(G_OBJECT(widget), bookmark_utils::kBookmarkNode)); | 138 g_object_get_data(G_OBJECT(widget), bookmark_utils::kBookmarkNode)); |
| 164 } | 139 } |
| 165 | 140 |
| 166 void SetButtonTextColors(GtkWidget* button) { | 141 void SetButtonTextColors(GtkWidget* label) { |
| 167 GtkWidget* label = NULL; | 142 gtk_widget_modify_fg(label, GTK_STATE_NORMAL, &kEnabledColor); |
| 168 gtk_container_foreach(GTK_CONTAINER(button), SearchForLabel, &label); | 143 gtk_widget_modify_fg(label, GTK_STATE_ACTIVE, &kEnabledColor); |
| 169 if (label) { | 144 gtk_widget_modify_fg(label, GTK_STATE_PRELIGHT, &kHighlightColor); |
| 170 gtk_widget_modify_fg(label, GTK_STATE_NORMAL, &kEnabledColor); | 145 gtk_widget_modify_fg(label, GTK_STATE_INSENSITIVE, &kDisabledColor); |
| 171 gtk_widget_modify_fg(label, GTK_STATE_ACTIVE, &kEnabledColor); | |
| 172 gtk_widget_modify_fg(label, GTK_STATE_PRELIGHT, &kHighlightColor); | |
| 173 gtk_widget_modify_fg(label, GTK_STATE_INSENSITIVE, &kDisabledColor); | |
| 174 } | |
| 175 } | 146 } |
| 176 | 147 |
| 177 // DnD-related ----------------------------------------------------------------- | 148 // DnD-related ----------------------------------------------------------------- |
| 178 | 149 |
| 179 void WriteBookmarkToSelection(const BookmarkNode* node, | 150 void WriteBookmarkToSelection(const BookmarkNode* node, |
| 180 GtkSelectionData* selection_data, | 151 GtkSelectionData* selection_data, |
| 181 guint target_type, | 152 guint target_type, |
| 182 Profile* profile) { | 153 Profile* profile) { |
| 183 DCHECK(node); | 154 DCHECK(node); |
| 184 std::vector<const BookmarkNode*> nodes; | 155 std::vector<const BookmarkNode*> nodes; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 default: { | 206 default: { |
| 236 DLOG(ERROR) << "Unsupported drag received type: " << target_type; | 207 DLOG(ERROR) << "Unsupported drag received type: " << target_type; |
| 237 } | 208 } |
| 238 } | 209 } |
| 239 } | 210 } |
| 240 | 211 |
| 241 return std::vector<const BookmarkNode*>(); | 212 return std::vector<const BookmarkNode*>(); |
| 242 } | 213 } |
| 243 | 214 |
| 244 } // namespace bookmark_utils | 215 } // namespace bookmark_utils |
| OLD | NEW |