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/gtk_chrome_link_button.h" | 5 #include "chrome/browser/gtk/gtk_chrome_link_button.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 | 10 |
11 static const char* kLinkMarkup = "<u><span color=\"%s\">%s</span></u>"; | 11 static const gchar* kLinkMarkup = "<u><span color=\"%s\">%s</span></u>"; |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // Set the GTK style on our custom link button. We don't want any border around | 15 // Set the GTK style on our custom link button. We don't want any border around |
16 // the link text. | 16 // the link text. |
17 void SetLinkButtonStyle() { | 17 void SetLinkButtonStyle() { |
18 static bool style_was_set = false; | 18 static bool style_was_set = false; |
19 | 19 |
20 if (style_was_set) | 20 if (style_was_set) |
21 return; | 21 return; |
22 style_was_set = true; | 22 style_was_set = true; |
23 | 23 |
24 gtk_rc_parse_string( | 24 gtk_rc_parse_string( |
25 "style \"chrome-link-button\" {" | 25 "style \"chrome-link-button\" {" |
26 " GtkButton::inner-border = {0, 0, 0, 0}" | 26 " GtkButton::inner-border = {0, 0, 0, 0}" |
| 27 " GtkButton::child-displacement-x = 0" |
| 28 " GtkButton::child-displacement-y = 0" |
27 " xthickness = 0" | 29 " xthickness = 0" |
28 " ythickness = 0" | 30 " ythickness = 0" |
29 "}" | 31 "}" |
30 "widget \"*chrome-link-button\" style \"chrome-link-button\""); | 32 "widget \"*chrome-link-button\" style \"chrome-link-button\""); |
31 } | 33 } |
32 | 34 |
33 } // namespace | 35 } // namespace |
34 | 36 |
35 G_BEGIN_DECLS | 37 G_BEGIN_DECLS |
36 G_DEFINE_TYPE(GtkChromeLinkButton, gtk_chrome_link_button, GTK_TYPE_BUTTON) | 38 G_DEFINE_TYPE(GtkChromeLinkButton, gtk_chrome_link_button, GTK_TYPE_BUTTON) |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 button->is_blue = TRUE; | 160 button->is_blue = TRUE; |
159 button->hand_cursor = gdk_cursor_new(GDK_HAND2); | 161 button->hand_cursor = gdk_cursor_new(GDK_HAND2); |
160 button->click_button_event = NULL; | 162 button->click_button_event = NULL; |
161 | 163 |
162 gtk_container_add(GTK_CONTAINER(button), button->label); | 164 gtk_container_add(GTK_CONTAINER(button), button->label); |
163 gtk_widget_set_name(GTK_WIDGET(button), "chrome-link-button"); | 165 gtk_widget_set_name(GTK_WIDGET(button), "chrome-link-button"); |
164 gtk_widget_set_app_paintable(GTK_WIDGET(button), TRUE); | 166 gtk_widget_set_app_paintable(GTK_WIDGET(button), TRUE); |
165 } | 167 } |
166 | 168 |
167 static void gtk_chrome_link_button_set_text(GtkChromeLinkButton* button, | 169 static void gtk_chrome_link_button_set_text(GtkChromeLinkButton* button, |
168 const char* text) { | 170 const char* text, |
| 171 bool contains_markup) { |
169 // We should have only been called once or we'd leak the markups. | 172 // We should have only been called once or we'd leak the markups. |
170 DCHECK(!button->blue_markup && !button->red_markup); | 173 DCHECK(!button->blue_markup && !button->red_markup); |
171 | 174 |
172 button->blue_markup = g_markup_printf_escaped(kLinkMarkup, "blue", text); | 175 if (!contains_markup) { |
173 button->red_markup = g_markup_printf_escaped(kLinkMarkup, "red", text); | 176 button->blue_markup = g_markup_printf_escaped(kLinkMarkup, "blue", text); |
| 177 button->red_markup = g_markup_printf_escaped(kLinkMarkup, "red", text); |
| 178 } else { |
| 179 button->blue_markup = static_cast<gchar*>( |
| 180 g_malloc(strlen(kLinkMarkup) + strlen("blue") + strlen(text) + 1)); |
| 181 sprintf(button->blue_markup, kLinkMarkup, "blue", text); |
| 182 |
| 183 button->red_markup = static_cast<gchar*>( |
| 184 g_malloc(strlen(kLinkMarkup) + strlen("red") + strlen(text) + 1)); |
| 185 sprintf(button->red_markup, kLinkMarkup, "red", text); |
| 186 } |
| 187 |
174 gtk_label_set_markup(GTK_LABEL(button->label), button->blue_markup); | 188 gtk_label_set_markup(GTK_LABEL(button->label), button->blue_markup); |
175 button->is_blue = TRUE; | 189 button->is_blue = TRUE; |
176 } | 190 } |
177 | 191 |
178 GtkWidget* gtk_chrome_link_button_new(const char* text) { | 192 GtkWidget* gtk_chrome_link_button_new(const char* text) { |
179 GtkWidget* lb = GTK_WIDGET(g_object_new(GTK_TYPE_CHROME_LINK_BUTTON, NULL)); | 193 GtkWidget* lb = GTK_WIDGET(g_object_new(GTK_TYPE_CHROME_LINK_BUTTON, NULL)); |
180 gtk_chrome_link_button_set_text(GTK_CHROME_LINK_BUTTON(lb), text); | 194 gtk_chrome_link_button_set_text(GTK_CHROME_LINK_BUTTON(lb), text, false); |
| 195 return lb; |
| 196 } |
| 197 |
| 198 GtkWidget* gtk_chrome_link_button_new_with_markup(const char* markup) { |
| 199 GtkWidget* lb = GTK_WIDGET(g_object_new(GTK_TYPE_CHROME_LINK_BUTTON, NULL)); |
| 200 gtk_chrome_link_button_set_text(GTK_CHROME_LINK_BUTTON(lb), markup, true); |
181 return lb; | 201 return lb; |
182 } | 202 } |
183 | 203 |
184 const GdkEventButton* gtk_chrome_link_button_get_event_for_click( | 204 const GdkEventButton* gtk_chrome_link_button_get_event_for_click( |
185 GtkChromeLinkButton* button) { | 205 GtkChromeLinkButton* button) { |
186 return button->click_button_event; | 206 return button->click_button_event; |
187 } | 207 } |
188 | 208 |
189 G_END_DECLS | 209 G_END_DECLS |
OLD | NEW |