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 |
(...skipping 19 matching lines...) Expand all Loading... |
30 " ythickness = 0" | 30 " ythickness = 0" |
31 "}" | 31 "}" |
32 "widget \"*chrome-link-button\" style \"chrome-link-button\""); | 32 "widget \"*chrome-link-button\" style \"chrome-link-button\""); |
33 } | 33 } |
34 | 34 |
35 } // namespace | 35 } // namespace |
36 | 36 |
37 G_BEGIN_DECLS | 37 G_BEGIN_DECLS |
38 G_DEFINE_TYPE(GtkChromeLinkButton, gtk_chrome_link_button, GTK_TYPE_BUTTON) | 38 G_DEFINE_TYPE(GtkChromeLinkButton, gtk_chrome_link_button, GTK_TYPE_BUTTON) |
39 | 39 |
40 // Should be called after we are realized so that the "link-color" property | |
41 // can be read. | |
42 static void gtk_chrome_link_button_set_text(GtkChromeLinkButton* button) { | |
43 // We only set the markup once. | |
44 if (button->blue_markup) | |
45 return; | |
46 | |
47 gchar* text = button->text; | |
48 gboolean uses_markup = button->uses_markup; | |
49 | |
50 if (!uses_markup) { | |
51 button->blue_markup = g_markup_printf_escaped(kLinkMarkup, "blue", text); | |
52 button->red_markup = g_markup_printf_escaped(kLinkMarkup, "red", text); | |
53 } else { | |
54 button->blue_markup = static_cast<gchar*>( | |
55 g_malloc(strlen(kLinkMarkup) + strlen("blue") + strlen(text) + 1)); | |
56 sprintf(button->blue_markup, kLinkMarkup, "blue", text); | |
57 | |
58 button->red_markup = static_cast<gchar*>( | |
59 g_malloc(strlen(kLinkMarkup) + strlen("red") + strlen(text) + 1)); | |
60 sprintf(button->red_markup, kLinkMarkup, "red", text); | |
61 } | |
62 | |
63 // Get the current GTK theme's link button text color. | |
64 GdkColor* native_color = NULL; | |
65 gtk_widget_style_get(GTK_WIDGET(button), "link-color", &native_color, NULL); | |
66 | |
67 if (native_color) { | |
68 gchar color_spec[9]; | |
69 sprintf(color_spec, "#%02X%02X%02X", native_color->red / 257, | |
70 native_color->green / 257, native_color->blue / 257); | |
71 gdk_color_free(native_color); | |
72 | |
73 if (!uses_markup) { | |
74 button->native_markup = g_markup_printf_escaped(kLinkMarkup, | |
75 color_spec, text); | |
76 } else { | |
77 button->native_markup = static_cast<gchar*>( | |
78 g_malloc(strlen(kLinkMarkup) + strlen(color_spec) + strlen(text) + | |
79 1)); | |
80 sprintf(button->native_markup, kLinkMarkup, color_spec, text); | |
81 } | |
82 } else { | |
83 // If the theme doesn't have a link color, just use blue. This matches the | |
84 // default for GtkLinkButton. | |
85 button->native_markup = button->blue_markup; | |
86 } | |
87 | |
88 gtk_label_set_markup(GTK_LABEL(button->label), | |
89 button->using_native_theme ? button->native_markup : button->blue_markup); | |
90 } | |
91 | |
92 static gboolean gtk_chrome_link_button_expose(GtkWidget* widget, | 40 static gboolean gtk_chrome_link_button_expose(GtkWidget* widget, |
93 GdkEventExpose* event) { | 41 GdkEventExpose* event) { |
94 GtkChromeLinkButton* button = GTK_CHROME_LINK_BUTTON(widget); | 42 GtkChromeLinkButton* button = GTK_CHROME_LINK_BUTTON(widget); |
95 GtkWidget* label = button->label; | 43 GtkWidget* label = button->label; |
96 | 44 |
97 gtk_chrome_link_button_set_text(button); | |
98 | |
99 if (GTK_WIDGET_STATE(widget) == GTK_STATE_ACTIVE && button->is_blue) { | 45 if (GTK_WIDGET_STATE(widget) == GTK_STATE_ACTIVE && button->is_blue) { |
100 gtk_label_set_markup(GTK_LABEL(label), button->red_markup); | 46 gtk_label_set_markup(GTK_LABEL(label), button->red_markup); |
101 button->is_blue = FALSE; | 47 button->is_blue = FALSE; |
102 } else if (GTK_WIDGET_STATE(widget) != GTK_STATE_ACTIVE && !button->is_blue) { | 48 } else if (GTK_WIDGET_STATE(widget) != GTK_STATE_ACTIVE && !button->is_blue) { |
103 gtk_label_set_markup(GTK_LABEL(label), | 49 gtk_label_set_markup(GTK_LABEL(label), button->blue_markup); |
104 button->using_native_theme ? button->native_markup : button->blue_markup
); | |
105 button->is_blue = TRUE; | 50 button->is_blue = TRUE; |
106 } | 51 } |
107 | 52 |
108 // Draw the link inside the button. | 53 // Draw the link inside the button. |
109 gtk_container_propagate_expose(GTK_CONTAINER(widget), label, event); | 54 gtk_container_propagate_expose(GTK_CONTAINER(widget), label, event); |
110 | 55 |
111 // Draw the focus rectangle. | 56 // Draw the focus rectangle. |
112 if (GTK_WIDGET_HAS_FOCUS(widget)) { | 57 if (GTK_WIDGET_HAS_FOCUS(widget)) { |
113 gtk_paint_focus(widget->style, widget->window, | 58 gtk_paint_focus(widget->style, widget->window, |
114 static_cast<GtkStateType>(GTK_WIDGET_STATE(widget)), | 59 static_cast<GtkStateType>(GTK_WIDGET_STATE(widget)), |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 static void gtk_chrome_link_button_leave(GtkButton* button) { | 107 static void gtk_chrome_link_button_leave(GtkButton* button) { |
163 GtkWidget* widget = GTK_WIDGET(button); | 108 GtkWidget* widget = GTK_WIDGET(button); |
164 GtkChromeLinkButton* link_button = GTK_CHROME_LINK_BUTTON(button); | 109 GtkChromeLinkButton* link_button = GTK_CHROME_LINK_BUTTON(button); |
165 gdk_window_set_cursor(widget->window, NULL); | 110 gdk_window_set_cursor(widget->window, NULL); |
166 free(link_button->click_button_event); | 111 free(link_button->click_button_event); |
167 link_button->click_button_event = NULL; | 112 link_button->click_button_event = NULL; |
168 } | 113 } |
169 | 114 |
170 static void gtk_chrome_link_button_destroy(GtkObject* object) { | 115 static void gtk_chrome_link_button_destroy(GtkObject* object) { |
171 GtkChromeLinkButton* button = GTK_CHROME_LINK_BUTTON(object); | 116 GtkChromeLinkButton* button = GTK_CHROME_LINK_BUTTON(object); |
172 if (button->native_markup && (button->native_markup != button->blue_markup)) { | |
173 g_free(button->native_markup); | |
174 button->native_markup = NULL; | |
175 } | |
176 if (button->blue_markup) { | 117 if (button->blue_markup) { |
177 g_free(button->blue_markup); | 118 g_free(button->blue_markup); |
178 button->blue_markup = NULL; | 119 button->blue_markup = NULL; |
179 } | 120 } |
180 if (button->red_markup) { | 121 if (button->red_markup) { |
181 g_free(button->red_markup); | 122 g_free(button->red_markup); |
182 button->red_markup = NULL; | 123 button->red_markup = NULL; |
183 } | 124 } |
184 if (button->hand_cursor) { | 125 if (button->hand_cursor) { |
185 gdk_cursor_unref(button->hand_cursor); | 126 gdk_cursor_unref(button->hand_cursor); |
186 button->hand_cursor = NULL; | 127 button->hand_cursor = NULL; |
187 } | 128 } |
188 | |
189 free(button->click_button_event); | 129 free(button->click_button_event); |
190 button->click_button_event = NULL; | 130 button->click_button_event = NULL; |
191 | 131 |
192 free(button->text); | |
193 button->text = NULL; | |
194 | |
195 GTK_OBJECT_CLASS(gtk_chrome_link_button_parent_class)->destroy(object); | 132 GTK_OBJECT_CLASS(gtk_chrome_link_button_parent_class)->destroy(object); |
196 } | 133 } |
197 | 134 |
198 static void gtk_chrome_link_button_class_init( | 135 static void gtk_chrome_link_button_class_init( |
199 GtkChromeLinkButtonClass* link_button_class) { | 136 GtkChromeLinkButtonClass* link_button_class) { |
200 GtkWidgetClass* widget_class = | 137 GtkWidgetClass* widget_class = |
201 reinterpret_cast<GtkWidgetClass*>(link_button_class); | 138 reinterpret_cast<GtkWidgetClass*>(link_button_class); |
202 GtkButtonClass* button_class = | 139 GtkButtonClass* button_class = |
203 reinterpret_cast<GtkButtonClass*>(link_button_class); | 140 reinterpret_cast<GtkButtonClass*>(link_button_class); |
204 GtkObjectClass* object_class = | 141 GtkObjectClass* object_class = |
205 reinterpret_cast<GtkObjectClass*>(link_button_class); | 142 reinterpret_cast<GtkObjectClass*>(link_button_class); |
206 widget_class->expose_event = >k_chrome_link_button_expose; | 143 widget_class->expose_event = >k_chrome_link_button_expose; |
207 widget_class->button_press_event = >k_chrome_link_button_button_press; | 144 widget_class->button_press_event = >k_chrome_link_button_button_press; |
208 widget_class->button_release_event = >k_chrome_link_button_button_release; | 145 widget_class->button_release_event = >k_chrome_link_button_button_release; |
209 button_class->enter = >k_chrome_link_button_enter; | 146 button_class->enter = >k_chrome_link_button_enter; |
210 button_class->leave = >k_chrome_link_button_leave; | 147 button_class->leave = >k_chrome_link_button_leave; |
211 object_class->destroy = >k_chrome_link_button_destroy; | 148 object_class->destroy = >k_chrome_link_button_destroy; |
212 } | 149 } |
213 | 150 |
214 static void gtk_chrome_link_button_init(GtkChromeLinkButton* button) { | 151 static void gtk_chrome_link_button_init(GtkChromeLinkButton* button) { |
215 SetLinkButtonStyle(); | 152 SetLinkButtonStyle(); |
216 | 153 |
217 // We put a label in a button so we can connect to the click event. We don't | 154 // We put a label in a button so we can connect to the click event. We don't |
218 // let the button draw itself; catch all expose events to the button and pass | 155 // let the button draw itself; catch all expose events to the button and pass |
219 // them through to the label. | 156 // them through to the label. |
220 button->label = gtk_label_new(NULL); | 157 button->label = gtk_label_new(NULL); |
221 button->blue_markup = NULL; | 158 button->blue_markup = NULL; |
222 button->red_markup = NULL; | 159 button->red_markup = NULL; |
223 button->is_blue = TRUE; | 160 button->is_blue = TRUE; |
224 button->native_markup = NULL; | |
225 button->using_native_theme = TRUE; | |
226 button->hand_cursor = gdk_cursor_new(GDK_HAND2); | 161 button->hand_cursor = gdk_cursor_new(GDK_HAND2); |
227 button->click_button_event = NULL; | 162 button->click_button_event = NULL; |
228 button->text = NULL; | |
229 | 163 |
230 gtk_container_add(GTK_CONTAINER(button), button->label); | 164 gtk_container_add(GTK_CONTAINER(button), button->label); |
231 gtk_widget_set_name(GTK_WIDGET(button), "chrome-link-button"); | 165 gtk_widget_set_name(GTK_WIDGET(button), "chrome-link-button"); |
232 gtk_widget_set_app_paintable(GTK_WIDGET(button), TRUE); | 166 gtk_widget_set_app_paintable(GTK_WIDGET(button), TRUE); |
233 } | 167 } |
234 | 168 |
| 169 static void gtk_chrome_link_button_set_text(GtkChromeLinkButton* button, |
| 170 const char* text, |
| 171 bool contains_markup) { |
| 172 // We should have only been called once or we'd leak the markups. |
| 173 DCHECK(!button->blue_markup && !button->red_markup); |
| 174 |
| 175 if (!contains_markup) { |
| 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 |
| 188 gtk_label_set_markup(GTK_LABEL(button->label), button->blue_markup); |
| 189 button->is_blue = TRUE; |
| 190 } |
| 191 |
235 GtkWidget* gtk_chrome_link_button_new(const char* text) { | 192 GtkWidget* gtk_chrome_link_button_new(const char* text) { |
236 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)); |
237 GTK_CHROME_LINK_BUTTON(lb)->text = strdup(text); | 194 gtk_chrome_link_button_set_text(GTK_CHROME_LINK_BUTTON(lb), text, false); |
238 GTK_CHROME_LINK_BUTTON(lb)->uses_markup = FALSE; | |
239 return lb; | 195 return lb; |
240 } | 196 } |
241 | 197 |
242 GtkWidget* gtk_chrome_link_button_new_with_markup(const char* markup) { | 198 GtkWidget* gtk_chrome_link_button_new_with_markup(const char* markup) { |
243 GtkWidget* lb = GTK_WIDGET(g_object_new(GTK_TYPE_CHROME_LINK_BUTTON, NULL)); | 199 GtkWidget* lb = GTK_WIDGET(g_object_new(GTK_TYPE_CHROME_LINK_BUTTON, NULL)); |
244 GTK_CHROME_LINK_BUTTON(lb)->text = strdup(markup); | 200 gtk_chrome_link_button_set_text(GTK_CHROME_LINK_BUTTON(lb), markup, true); |
245 GTK_CHROME_LINK_BUTTON(lb)->uses_markup = TRUE; | |
246 return lb; | 201 return lb; |
247 } | 202 } |
248 | 203 |
249 void gtk_chrome_link_button_set_use_gtk_theme(GtkChromeLinkButton* button, | |
250 gboolean use_gtk) { | |
251 if (use_gtk != button->using_native_theme) { | |
252 button->using_native_theme = use_gtk; | |
253 if (GTK_WIDGET_VISIBLE(button)) | |
254 gtk_widget_queue_draw(GTK_WIDGET(button)); | |
255 } | |
256 } | |
257 | |
258 const GdkEventButton* gtk_chrome_link_button_get_event_for_click( | 204 const GdkEventButton* gtk_chrome_link_button_get_event_for_click( |
259 GtkChromeLinkButton* button) { | 205 GtkChromeLinkButton* button) { |
260 return button->click_button_event; | 206 return button->click_button_event; |
261 } | 207 } |
262 | 208 |
263 G_END_DECLS | 209 G_END_DECLS |
OLD | NEW |