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 |
40 static gboolean gtk_chrome_link_button_expose(GtkWidget* widget, | 92 static gboolean gtk_chrome_link_button_expose(GtkWidget* widget, |
41 GdkEventExpose* event) { | 93 GdkEventExpose* event) { |
42 GtkChromeLinkButton* button = GTK_CHROME_LINK_BUTTON(widget); | 94 GtkChromeLinkButton* button = GTK_CHROME_LINK_BUTTON(widget); |
43 GtkWidget* label = button->label; | 95 GtkWidget* label = button->label; |
44 | 96 |
| 97 gtk_chrome_link_button_set_text(button); |
| 98 |
45 if (GTK_WIDGET_STATE(widget) == GTK_STATE_ACTIVE && button->is_blue) { | 99 if (GTK_WIDGET_STATE(widget) == GTK_STATE_ACTIVE && button->is_blue) { |
46 gtk_label_set_markup(GTK_LABEL(label), button->red_markup); | 100 gtk_label_set_markup(GTK_LABEL(label), button->red_markup); |
47 button->is_blue = FALSE; | 101 button->is_blue = FALSE; |
48 } else if (GTK_WIDGET_STATE(widget) != GTK_STATE_ACTIVE && !button->is_blue) { | 102 } else if (GTK_WIDGET_STATE(widget) != GTK_STATE_ACTIVE && !button->is_blue) { |
49 gtk_label_set_markup(GTK_LABEL(label), button->blue_markup); | 103 gtk_label_set_markup(GTK_LABEL(label), |
| 104 button->using_native_theme ? button->native_markup : button->blue_markup
); |
50 button->is_blue = TRUE; | 105 button->is_blue = TRUE; |
51 } | 106 } |
52 | 107 |
53 // Draw the link inside the button. | 108 // Draw the link inside the button. |
54 gtk_container_propagate_expose(GTK_CONTAINER(widget), label, event); | 109 gtk_container_propagate_expose(GTK_CONTAINER(widget), label, event); |
55 | 110 |
56 // Draw the focus rectangle. | 111 // Draw the focus rectangle. |
57 if (GTK_WIDGET_HAS_FOCUS(widget)) { | 112 if (GTK_WIDGET_HAS_FOCUS(widget)) { |
58 gtk_paint_focus(widget->style, widget->window, | 113 gtk_paint_focus(widget->style, widget->window, |
59 static_cast<GtkStateType>(GTK_WIDGET_STATE(widget)), | 114 static_cast<GtkStateType>(GTK_WIDGET_STATE(widget)), |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 static void gtk_chrome_link_button_leave(GtkButton* button) { | 162 static void gtk_chrome_link_button_leave(GtkButton* button) { |
108 GtkWidget* widget = GTK_WIDGET(button); | 163 GtkWidget* widget = GTK_WIDGET(button); |
109 GtkChromeLinkButton* link_button = GTK_CHROME_LINK_BUTTON(button); | 164 GtkChromeLinkButton* link_button = GTK_CHROME_LINK_BUTTON(button); |
110 gdk_window_set_cursor(widget->window, NULL); | 165 gdk_window_set_cursor(widget->window, NULL); |
111 free(link_button->click_button_event); | 166 free(link_button->click_button_event); |
112 link_button->click_button_event = NULL; | 167 link_button->click_button_event = NULL; |
113 } | 168 } |
114 | 169 |
115 static void gtk_chrome_link_button_destroy(GtkObject* object) { | 170 static void gtk_chrome_link_button_destroy(GtkObject* object) { |
116 GtkChromeLinkButton* button = GTK_CHROME_LINK_BUTTON(object); | 171 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 } |
117 if (button->blue_markup) { | 176 if (button->blue_markup) { |
118 g_free(button->blue_markup); | 177 g_free(button->blue_markup); |
119 button->blue_markup = NULL; | 178 button->blue_markup = NULL; |
120 } | 179 } |
121 if (button->red_markup) { | 180 if (button->red_markup) { |
122 g_free(button->red_markup); | 181 g_free(button->red_markup); |
123 button->red_markup = NULL; | 182 button->red_markup = NULL; |
124 } | 183 } |
125 if (button->hand_cursor) { | 184 if (button->hand_cursor) { |
126 gdk_cursor_unref(button->hand_cursor); | 185 gdk_cursor_unref(button->hand_cursor); |
127 button->hand_cursor = NULL; | 186 button->hand_cursor = NULL; |
128 } | 187 } |
| 188 |
129 free(button->click_button_event); | 189 free(button->click_button_event); |
130 button->click_button_event = NULL; | 190 button->click_button_event = NULL; |
131 | 191 |
| 192 free(button->text); |
| 193 button->text = NULL; |
| 194 |
132 GTK_OBJECT_CLASS(gtk_chrome_link_button_parent_class)->destroy(object); | 195 GTK_OBJECT_CLASS(gtk_chrome_link_button_parent_class)->destroy(object); |
133 } | 196 } |
134 | 197 |
135 static void gtk_chrome_link_button_class_init( | 198 static void gtk_chrome_link_button_class_init( |
136 GtkChromeLinkButtonClass* link_button_class) { | 199 GtkChromeLinkButtonClass* link_button_class) { |
137 GtkWidgetClass* widget_class = | 200 GtkWidgetClass* widget_class = |
138 reinterpret_cast<GtkWidgetClass*>(link_button_class); | 201 reinterpret_cast<GtkWidgetClass*>(link_button_class); |
139 GtkButtonClass* button_class = | 202 GtkButtonClass* button_class = |
140 reinterpret_cast<GtkButtonClass*>(link_button_class); | 203 reinterpret_cast<GtkButtonClass*>(link_button_class); |
141 GtkObjectClass* object_class = | 204 GtkObjectClass* object_class = |
142 reinterpret_cast<GtkObjectClass*>(link_button_class); | 205 reinterpret_cast<GtkObjectClass*>(link_button_class); |
143 widget_class->expose_event = >k_chrome_link_button_expose; | 206 widget_class->expose_event = >k_chrome_link_button_expose; |
144 widget_class->button_press_event = >k_chrome_link_button_button_press; | 207 widget_class->button_press_event = >k_chrome_link_button_button_press; |
145 widget_class->button_release_event = >k_chrome_link_button_button_release; | 208 widget_class->button_release_event = >k_chrome_link_button_button_release; |
146 button_class->enter = >k_chrome_link_button_enter; | 209 button_class->enter = >k_chrome_link_button_enter; |
147 button_class->leave = >k_chrome_link_button_leave; | 210 button_class->leave = >k_chrome_link_button_leave; |
148 object_class->destroy = >k_chrome_link_button_destroy; | 211 object_class->destroy = >k_chrome_link_button_destroy; |
149 } | 212 } |
150 | 213 |
151 static void gtk_chrome_link_button_init(GtkChromeLinkButton* button) { | 214 static void gtk_chrome_link_button_init(GtkChromeLinkButton* button) { |
152 SetLinkButtonStyle(); | 215 SetLinkButtonStyle(); |
153 | 216 |
154 // We put a label in a button so we can connect to the click event. We don't | 217 // We put a label in a button so we can connect to the click event. We don't |
155 // let the button draw itself; catch all expose events to the button and pass | 218 // let the button draw itself; catch all expose events to the button and pass |
156 // them through to the label. | 219 // them through to the label. |
157 button->label = gtk_label_new(NULL); | 220 button->label = gtk_label_new(NULL); |
158 button->blue_markup = NULL; | 221 button->blue_markup = NULL; |
159 button->red_markup = NULL; | 222 button->red_markup = NULL; |
160 button->is_blue = TRUE; | 223 button->is_blue = TRUE; |
| 224 button->native_markup = NULL; |
| 225 button->using_native_theme = TRUE; |
161 button->hand_cursor = gdk_cursor_new(GDK_HAND2); | 226 button->hand_cursor = gdk_cursor_new(GDK_HAND2); |
162 button->click_button_event = NULL; | 227 button->click_button_event = NULL; |
| 228 button->text = NULL; |
163 | 229 |
164 gtk_container_add(GTK_CONTAINER(button), button->label); | 230 gtk_container_add(GTK_CONTAINER(button), button->label); |
165 gtk_widget_set_name(GTK_WIDGET(button), "chrome-link-button"); | 231 gtk_widget_set_name(GTK_WIDGET(button), "chrome-link-button"); |
166 gtk_widget_set_app_paintable(GTK_WIDGET(button), TRUE); | 232 gtk_widget_set_app_paintable(GTK_WIDGET(button), TRUE); |
167 } | 233 } |
168 | 234 |
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 | |
192 GtkWidget* gtk_chrome_link_button_new(const char* text) { | 235 GtkWidget* gtk_chrome_link_button_new(const char* text) { |
193 GtkWidget* lb = GTK_WIDGET(g_object_new(GTK_TYPE_CHROME_LINK_BUTTON, NULL)); | 236 GtkWidget* lb = GTK_WIDGET(g_object_new(GTK_TYPE_CHROME_LINK_BUTTON, NULL)); |
194 gtk_chrome_link_button_set_text(GTK_CHROME_LINK_BUTTON(lb), text, false); | 237 GTK_CHROME_LINK_BUTTON(lb)->text = strdup(text); |
| 238 GTK_CHROME_LINK_BUTTON(lb)->uses_markup = FALSE; |
195 return lb; | 239 return lb; |
196 } | 240 } |
197 | 241 |
198 GtkWidget* gtk_chrome_link_button_new_with_markup(const char* markup) { | 242 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)); | 243 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); | 244 GTK_CHROME_LINK_BUTTON(lb)->text = strdup(markup); |
| 245 GTK_CHROME_LINK_BUTTON(lb)->uses_markup = TRUE; |
201 return lb; | 246 return lb; |
202 } | 247 } |
203 | 248 |
| 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 |
204 const GdkEventButton* gtk_chrome_link_button_get_event_for_click( | 258 const GdkEventButton* gtk_chrome_link_button_get_event_for_click( |
205 GtkChromeLinkButton* button) { | 259 GtkChromeLinkButton* button) { |
206 return button->click_button_event; | 260 return button->click_button_event; |
207 } | 261 } |
208 | 262 |
209 G_END_DECLS | 263 G_END_DECLS |
OLD | NEW |