| 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" |
| 11 #include "chrome/browser/bookmarks/bookmark_drag_data.h" | 11 #include "chrome/browser/bookmarks/bookmark_drag_data.h" |
| 12 #include "chrome/browser/bookmarks/bookmark_model.h" | 12 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 13 #include "chrome/browser/gtk/gtk_chrome_button.h" | 13 #include "chrome/browser/gtk/gtk_chrome_button.h" |
| 14 #include "chrome/browser/gtk/gtk_dnd_util.h" | 14 #include "chrome/browser/gtk/gtk_dnd_util.h" |
| 15 #include "chrome/browser/gtk/gtk_theme_provider.h" |
| 15 #include "chrome/browser/profile.h" | 16 #include "chrome/browser/profile.h" |
| 16 #include "grit/app_resources.h" | 17 #include "grit/app_resources.h" |
| 17 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
| 18 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // Used in gtk_selection_data_set(). (I assume from this parameter that gtk has | 23 // Used in gtk_selection_data_set(). (I assume from this parameter that gtk has |
| 23 // to some really exotic hardware...) | 24 // to some really exotic hardware...) |
| 24 const int kBitsInAByte = 8; | 25 const int kBitsInAByte = 8; |
| 25 | 26 |
| 26 // Maximum number of characters on a bookmark button. | 27 // Maximum number of characters on a bookmark button. |
| 27 const size_t kMaxCharsOnAButton = 15; | 28 const size_t kMaxCharsOnAButton = 15; |
| 28 | 29 |
| 29 // Only used for the background of the drag widget. | 30 // Only used for the background of the drag widget. |
| 30 const GdkColor kBackgroundColor = GDK_COLOR_RGB(0xe6, 0xed, 0xf4); | 31 const GdkColor kBackgroundColor = GDK_COLOR_RGB(0xe6, 0xed, 0xf4); |
| 31 | 32 |
| 32 // Color of the button text, taken from TextButtonView. | |
| 33 const GdkColor kEnabledColor = GDK_COLOR_RGB(6, 45, 117); | |
| 34 const GdkColor kDisabledColor = GDK_COLOR_RGB(161, 161, 146); | |
| 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 | |
| 37 // kEnabledColor. | |
| 38 const GdkColor kHighlightColor = GDK_COLOR_RGB(56, 95, 167); | |
| 39 | |
| 40 void* AsVoid(const BookmarkNode* node) { | 33 void* AsVoid(const BookmarkNode* node) { |
| 41 return const_cast<BookmarkNode*>(node); | 34 return const_cast<BookmarkNode*>(node); |
| 42 } | 35 } |
| 43 | 36 |
| 44 } // namespace | 37 } // namespace |
| 45 | 38 |
| 46 namespace bookmark_utils { | 39 namespace bookmark_utils { |
| 47 | 40 |
| 48 const char kBookmarkNode[] = "bookmark-node"; | 41 const char kBookmarkNode[] = "bookmark-node"; |
| 49 | 42 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 75 } | 68 } |
| 76 } else { | 69 } else { |
| 77 pixbuf = GetFolderIcon(); | 70 pixbuf = GetFolderIcon(); |
| 78 g_object_ref(pixbuf); | 71 g_object_ref(pixbuf); |
| 79 } | 72 } |
| 80 | 73 |
| 81 return pixbuf; | 74 return pixbuf; |
| 82 } | 75 } |
| 83 | 76 |
| 84 GtkWidget* GetDragRepresentation(const BookmarkNode* node, | 77 GtkWidget* GetDragRepresentation(const BookmarkNode* node, |
| 85 BookmarkModel* model) { | 78 BookmarkModel* model, |
| 79 GtkThemeProperties* properties) { |
| 86 // Build a windowed representation for our button. | 80 // Build a windowed representation for our button. |
| 87 GtkWidget* window = gtk_window_new(GTK_WINDOW_POPUP); | 81 GtkWidget* window = gtk_window_new(GTK_WINDOW_POPUP); |
| 88 gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &kBackgroundColor); | 82 if (!properties->use_gtk_rendering) { |
| 83 // TODO(erg): Theme wise, which color should I be picking here? |
| 84 // COLOR_BUTTON_BACKGROUND doesn't match the default theme! |
| 85 gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &kBackgroundColor); |
| 86 } |
| 89 gtk_widget_realize(window); | 87 gtk_widget_realize(window); |
| 90 | 88 |
| 91 GtkWidget* frame = gtk_frame_new(NULL); | 89 GtkWidget* frame = gtk_frame_new(NULL); |
| 92 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT); | 90 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT); |
| 93 gtk_container_add(GTK_CONTAINER(window), frame); | 91 gtk_container_add(GTK_CONTAINER(window), frame); |
| 94 gtk_widget_show(frame); | 92 gtk_widget_show(frame); |
| 95 | 93 |
| 96 GtkWidget* floating_button = gtk_chrome_button_new(); | 94 GtkWidget* floating_button = gtk_chrome_button_new(); |
| 97 bookmark_utils::ConfigureButtonForNode(node, model, floating_button); | 95 bookmark_utils::ConfigureButtonForNode(node, model, floating_button, |
| 96 properties); |
| 98 gtk_container_add(GTK_CONTAINER(frame), floating_button); | 97 gtk_container_add(GTK_CONTAINER(frame), floating_button); |
| 99 gtk_widget_show(floating_button); | 98 gtk_widget_show(floating_button); |
| 100 | 99 |
| 101 return window; | 100 return window; |
| 102 } | 101 } |
| 103 | 102 |
| 104 void ConfigureButtonForNode(const BookmarkNode* node, BookmarkModel* model, | 103 void ConfigureButtonForNode(const BookmarkNode* node, BookmarkModel* model, |
| 105 GtkWidget* button) { | 104 GtkWidget* button, GtkThemeProperties* properties) { |
| 106 GtkWidget* former_child = gtk_bin_get_child(GTK_BIN(button)); | 105 GtkWidget* former_child = gtk_bin_get_child(GTK_BIN(button)); |
| 107 if (former_child) | 106 if (former_child) |
| 108 gtk_container_remove(GTK_CONTAINER(button), former_child); | 107 gtk_container_remove(GTK_CONTAINER(button), former_child); |
| 109 | 108 |
| 110 std::string tooltip = BuildTooltipFor(node); | 109 std::string tooltip = BuildTooltipFor(node); |
| 111 if (!tooltip.empty()) | 110 if (!tooltip.empty()) |
| 112 gtk_widget_set_tooltip_text(button, tooltip.c_str()); | 111 gtk_widget_set_tooltip_text(button, tooltip.c_str()); |
| 113 | 112 |
| 114 // We pack the button manually (rather than using gtk_button_set_*) so that | 113 // We pack the button manually (rather than using gtk_button_set_*) so that |
| 115 // we can have finer control over its label. | 114 // we can have finer control over its label. |
| 116 GdkPixbuf* pixbuf = bookmark_utils::GetPixbufForNode(node, model); | 115 GdkPixbuf* pixbuf = bookmark_utils::GetPixbufForNode(node, model); |
| 117 GtkWidget* image = gtk_image_new_from_pixbuf(pixbuf); | 116 GtkWidget* image = gtk_image_new_from_pixbuf(pixbuf); |
| 118 g_object_unref(pixbuf); | 117 g_object_unref(pixbuf); |
| 119 | 118 |
| 120 GtkWidget* label = gtk_label_new(WideToUTF8(node->GetTitle()).c_str()); | 119 GtkWidget* label = gtk_label_new(WideToUTF8(node->GetTitle()).c_str()); |
| 121 gtk_label_set_max_width_chars(GTK_LABEL(label), kMaxCharsOnAButton); | 120 gtk_label_set_max_width_chars(GTK_LABEL(label), kMaxCharsOnAButton); |
| 122 gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END); | 121 gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END); |
| 123 | 122 |
| 124 GtkWidget* box = gtk_hbox_new(FALSE, kBarButtonPadding); | 123 GtkWidget* box = gtk_hbox_new(FALSE, kBarButtonPadding); |
| 125 gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0); | 124 gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0); |
| 126 gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0); | 125 gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0); |
| 127 gtk_container_add(GTK_CONTAINER(button), box); | 126 gtk_container_add(GTK_CONTAINER(button), box); |
| 128 | 127 |
| 129 SetButtonTextColors(label); | 128 SetButtonTextColors(label, properties); |
| 130 g_object_set_data(G_OBJECT(button), bookmark_utils::kBookmarkNode, | 129 g_object_set_data(G_OBJECT(button), bookmark_utils::kBookmarkNode, |
| 131 AsVoid(node)); | 130 AsVoid(node)); |
| 132 | 131 |
| 132 gtk_chrome_button_set_use_gtk_rendering(GTK_CHROME_BUTTON(button), |
| 133 properties->use_gtk_rendering); |
| 134 |
| 133 gtk_widget_show_all(box); | 135 gtk_widget_show_all(box); |
| 134 } | 136 } |
| 135 | 137 |
| 136 std::string BuildTooltipFor(const BookmarkNode* node) { | 138 std::string BuildTooltipFor(const BookmarkNode* node) { |
| 137 // TODO(erg): Actually build the tooltip. For now, we punt and just return | 139 // TODO(erg): Actually build the tooltip. For now, we punt and just return |
| 138 // the URL. | 140 // the URL. |
| 139 return node->GetURL().possibly_invalid_spec(); | 141 return node->GetURL().possibly_invalid_spec(); |
| 140 } | 142 } |
| 141 | 143 |
| 142 const BookmarkNode* BookmarkNodeForWidget(GtkWidget* widget) { | 144 const BookmarkNode* BookmarkNodeForWidget(GtkWidget* widget) { |
| 143 return reinterpret_cast<const BookmarkNode*>( | 145 return reinterpret_cast<const BookmarkNode*>( |
| 144 g_object_get_data(G_OBJECT(widget), bookmark_utils::kBookmarkNode)); | 146 g_object_get_data(G_OBJECT(widget), bookmark_utils::kBookmarkNode)); |
| 145 } | 147 } |
| 146 | 148 |
| 147 void SetButtonTextColors(GtkWidget* label) { | 149 void SetButtonTextColors(GtkWidget* label, GtkThemeProperties* properties) { |
| 148 gtk_widget_modify_fg(label, GTK_STATE_NORMAL, &kEnabledColor); | 150 if (properties->use_gtk_rendering) { |
| 149 gtk_widget_modify_fg(label, GTK_STATE_ACTIVE, &kEnabledColor); | 151 gtk_widget_modify_fg(label, GTK_STATE_NORMAL, NULL); |
| 150 gtk_widget_modify_fg(label, GTK_STATE_PRELIGHT, &kHighlightColor); | 152 gtk_widget_modify_fg(label, GTK_STATE_ACTIVE, NULL); |
| 151 gtk_widget_modify_fg(label, GTK_STATE_INSENSITIVE, &kDisabledColor); | 153 gtk_widget_modify_fg(label, GTK_STATE_PRELIGHT, NULL); |
| 154 gtk_widget_modify_fg(label, GTK_STATE_INSENSITIVE, NULL); |
| 155 } else { |
| 156 GdkColor color = properties->GetGdkColor( |
| 157 BrowserThemeProvider::COLOR_BOOKMARK_TEXT); |
| 158 gtk_widget_modify_fg(label, GTK_STATE_NORMAL, &color); |
| 159 gtk_widget_modify_fg(label, GTK_STATE_ACTIVE, &color); |
| 160 gtk_widget_modify_fg(label, GTK_STATE_PRELIGHT, &color); |
| 161 gtk_widget_modify_fg(label, GTK_STATE_INSENSITIVE, &color); |
| 162 } |
| 152 } | 163 } |
| 153 | 164 |
| 154 // DnD-related ----------------------------------------------------------------- | 165 // DnD-related ----------------------------------------------------------------- |
| 155 | 166 |
| 156 void WriteBookmarkToSelection(const BookmarkNode* node, | 167 void WriteBookmarkToSelection(const BookmarkNode* node, |
| 157 GtkSelectionData* selection_data, | 168 GtkSelectionData* selection_data, |
| 158 guint target_type, | 169 guint target_type, |
| 159 Profile* profile) { | 170 Profile* profile) { |
| 160 DCHECK(node); | 171 DCHECK(node); |
| 161 std::vector<const BookmarkNode*> nodes; | 172 std::vector<const BookmarkNode*> nodes; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 default: { | 223 default: { |
| 213 DLOG(ERROR) << "Unsupported drag received type: " << target_type; | 224 DLOG(ERROR) << "Unsupported drag received type: " << target_type; |
| 214 } | 225 } |
| 215 } | 226 } |
| 216 } | 227 } |
| 217 | 228 |
| 218 return std::vector<const BookmarkNode*>(); | 229 return std::vector<const BookmarkNode*>(); |
| 219 } | 230 } |
| 220 | 231 |
| 221 } // namespace bookmark_utils | 232 } // namespace bookmark_utils |
| OLD | NEW |