1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-2007 Imendio AB
4 * Copyright (C) 2008 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Xavier Claessens <xclaesse@gmail.com>
28 #include <glib/gi18n-lib.h>
31 #include <telepathy-glib/util.h>
32 #include <libempathy/empathy-utils.h>
34 #include "empathy-theme-manager.h"
35 #include "empathy-chat-view.h"
36 #include "empathy-conf.h"
37 #include "empathy-chat-text-view.h"
38 #include "empathy-theme-boxes.h"
39 #include "empathy-theme-irc.h"
41 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
42 #include <libempathy/empathy-debug.h>
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyThemeManager)
48 GtkSettings *settings;
50 } EmpathyThemeManagerPriv;
57 static guint signals[LAST_SIGNAL] = { 0 };
59 static const gchar *themes[] = {
60 "classic", N_("Classic"),
61 "simple", N_("Simple"),
67 G_DEFINE_TYPE (EmpathyThemeManager, empathy_theme_manager, G_TYPE_OBJECT);
70 theme_manager_gdk_color_to_hex (GdkColor *gdk_color, gchar *str_color)
72 g_snprintf (str_color, 10,
75 gdk_color->green >> 8,
76 gdk_color->blue >> 8);
79 static EmpathyThemeIrc *
80 theme_manager_create_irc_view (EmpathyThemeManager *manager)
82 EmpathyChatTextView *view;
83 EmpathyThemeIrc *theme;
85 theme = empathy_theme_irc_new ();
86 view = EMPATHY_CHAT_TEXT_VIEW (theme);
88 /* Define base tags */
89 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
92 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
93 "foreground", "darkgrey",
94 "justification", GTK_JUSTIFY_CENTER,
96 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION,
97 "foreground", "brown4",
98 "style", PANGO_STYLE_ITALIC,
100 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY,
101 "foreground-set", FALSE,
103 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
104 "foreground", "PeachPuff4",
105 "justification", GTK_JUSTIFY_LEFT,
107 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
108 "foreground", "steelblue",
109 "underline", PANGO_UNDERLINE_SINGLE,
112 /* Define IRC tags */
113 empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_SELF,
114 "foreground", "sea green",
116 empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_OTHER,
117 "foreground", "skyblue4",
119 empathy_chat_text_view_tag_set (view, EMPATHY_THEME_IRC_TAG_NICK_HIGHLIGHT,
120 "foreground", "indian red",
121 "weight", PANGO_WEIGHT_BOLD,
128 theme_manager_boxes_weak_notify_cb (gpointer data,
129 GObject *where_the_object_was)
131 EmpathyThemeManagerPriv *priv = GET_PRIV (data);
133 priv->boxes_views = g_list_remove (priv->boxes_views, where_the_object_was);
136 static EmpathyThemeBoxes *
137 theme_manager_create_boxes_view (EmpathyThemeManager *manager)
139 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
140 EmpathyThemeBoxes *theme;
142 theme = empathy_theme_boxes_new ();
143 priv->boxes_views = g_list_prepend (priv->boxes_views, theme);
144 g_object_weak_ref (G_OBJECT (theme),
145 theme_manager_boxes_weak_notify_cb,
152 theme_manager_update_boxes_tags (EmpathyThemeBoxes *theme,
153 const gchar *header_foreground,
154 const gchar *header_background,
155 const gchar *header_line_background,
156 const gchar *action_foreground,
157 const gchar *time_foreground,
158 const gchar *event_foreground,
159 const gchar *link_foreground,
160 const gchar *text_foreground,
161 const gchar *text_background,
162 const gchar *highlight_foreground)
165 EmpathyChatTextView *view = EMPATHY_CHAT_TEXT_VIEW (theme);
168 DEBUG ("Update view with new colors:\n"
169 "header_foreground = %s\n"
170 "header_background = %s\n"
171 "header_line_background = %s\n"
172 "action_foreground = %s\n"
173 "time_foreground = %s\n"
174 "event_foreground = %s\n"
175 "link_foreground = %s\n"
176 "text_foreground = %s\n"
177 "text_background = %s\n"
178 "highlight_foreground = %s\n",
179 header_foreground, header_background, header_line_background,
180 action_foreground, time_foreground, event_foreground,
181 link_foreground, text_foreground, text_background,
182 highlight_foreground);
185 /* FIXME: GtkTextTag don't support to set color properties to NULL.
188 #define TAG_SET(prop, prop_set, value) \
189 if (value != NULL) { \
190 g_object_set (tag, prop, value, NULL); \
192 g_object_set (tag, prop_set, FALSE, NULL); \
195 /* Define base tags */
196 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_HIGHLIGHT,
197 "weight", PANGO_WEIGHT_BOLD,
198 "pixels-above-lines", 4,
200 TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
201 TAG_SET ("foreground", "foreground-set",highlight_foreground);
203 empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_SPACING,
205 "pixels-above-lines", 8,
207 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
208 "justification", GTK_JUSTIFY_CENTER,
210 TAG_SET ("foreground", "foreground-set", time_foreground);
211 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_ACTION,
212 "style", PANGO_STYLE_ITALIC,
213 "pixels-above-lines", 4,
215 TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
216 TAG_SET ("foreground", "foreground-set", action_foreground);
217 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_BODY,
218 "pixels-above-lines", 4,
220 TAG_SET ("paragraph-background", "paragraph-background-set", text_background);
221 TAG_SET ("foreground", "foreground-set", text_foreground);
222 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_EVENT,
223 "justification", GTK_JUSTIFY_LEFT,
225 TAG_SET ("foreground", "foreground-set", event_foreground);
226 tag = empathy_chat_text_view_tag_set (view, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
227 "underline", PANGO_UNDERLINE_SINGLE,
229 TAG_SET ("foreground", "foreground-set", link_foreground);
231 /* Define BOXES tags */
232 tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER,
233 "weight", PANGO_WEIGHT_BOLD,
234 "foreground", header_foreground,
235 "paragraph-background", header_background,
237 TAG_SET ("foreground", "foreground-set", header_foreground);
238 TAG_SET ("paragraph-background", "paragraph-background-set", header_background);
239 tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER_LINE,
241 "paragraph-background", header_line_background,
243 TAG_SET ("paragraph-background", "paragraph-background-set", header_line_background);
249 theme_manager_update_simple_tags (EmpathyThemeBoxes *theme)
257 style = gtk_widget_get_default_style ();
259 theme_manager_gdk_color_to_hex (&style->base[GTK_STATE_SELECTED], color1);
260 theme_manager_gdk_color_to_hex (&style->bg[GTK_STATE_SELECTED], color2);
261 theme_manager_gdk_color_to_hex (&style->dark[GTK_STATE_SELECTED], color3);
262 theme_manager_gdk_color_to_hex (&style->fg[GTK_STATE_SELECTED], color4);
264 theme_manager_update_boxes_tags (theme,
265 color4, /* header_foreground */
266 color2, /* header_background */
267 color3, /* header_line_background */
268 color1, /* action_foreground */
269 "darkgrey", /* time_foreground */
270 "darkgrey", /* event_foreground */
271 color1, /* link_foreground */
272 NULL, /* text_foreground */
273 NULL, /* text_background */
274 NULL); /* highlight_foreground */
278 theme_manager_update_boxes_theme (EmpathyThemeManager *manager,
279 EmpathyThemeBoxes *theme)
281 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
283 if (strcmp (priv->name, "simple") == 0) {
284 theme_manager_update_simple_tags (theme);
286 else if (strcmp (priv->name, "clean") == 0) {
287 theme_manager_update_boxes_tags (theme,
288 "black", /* header_foreground */
289 "#efefdf", /* header_background */
290 "#e3e3d3", /* header_line_background */
291 "brown4", /* action_foreground */
292 "darkgrey", /* time_foreground */
293 "darkgrey", /* event_foreground */
294 "#49789e", /* link_foreground */
295 NULL, /* text_foreground */
296 NULL, /* text_background */
297 NULL); /* highlight_foreground */
299 else if (strcmp (priv->name, "blue") == 0) {
300 theme_manager_update_boxes_tags (theme,
301 "black", /* header_foreground */
302 "#88a2b4", /* header_background */
303 "#7f96a4", /* header_line_background */
304 "brown4", /* action_foreground */
305 "darkgrey", /* time_foreground */
306 "#7f96a4", /* event_foreground */
307 "#49789e", /* link_foreground */
308 "black", /* text_foreground */
309 "#adbdc8", /* text_background */
310 "black"); /* highlight_foreground */
315 empathy_theme_manager_create_view (EmpathyThemeManager *manager)
317 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
318 EmpathyThemeBoxes *theme;
320 g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (manager), NULL);
322 DEBUG ("Using theme %s", priv->name);
324 if (strcmp (priv->name, "classic") == 0) {
325 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
328 theme = theme_manager_create_boxes_view (manager);
329 theme_manager_update_boxes_theme (manager, theme);
331 return EMPATHY_CHAT_VIEW (theme);
335 theme_manager_color_hash_notify_cb (EmpathyThemeManager *manager)
337 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
339 /* FIXME: Make that work, it should update color when theme changes but
340 * it doesnt seems to work with all themes. */
342 if (strcmp (priv->name, "simple") == 0) {
345 /* We are using the simple theme which use the GTK theme color,
346 * Update views to use the new color. */
347 for (l = priv->boxes_views; l; l = l->next) {
348 theme_manager_update_simple_tags (EMPATHY_THEME_BOXES (l->data));
354 theme_manager_ensure_theme_exists (const gchar *name)
358 if (EMP_STR_EMPTY (name)) {
362 for (i = 0; themes[i]; i += 2) {
363 if (strcmp (themes[i], name) == 0) {
372 theme_manager_notify_name_cb (EmpathyConf *conf,
376 EmpathyThemeManager *manager = EMPATHY_THEME_MANAGER (user_data);
377 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
380 if (!empathy_conf_get_string (conf, key, &name) ||
381 !theme_manager_ensure_theme_exists (name) ||
382 !tp_strdiff (priv->name, name)) {
384 priv->name = g_strdup ("classic");
394 if (!tp_strdiff (priv->name, "simple") ||
395 !tp_strdiff (priv->name, "clean") ||
396 !tp_strdiff (priv->name, "blue")) {
399 /* The theme changes to a boxed one, we can update boxed views */
400 for (l = priv->boxes_views; l; l = l->next) {
401 theme_manager_update_boxes_theme (manager,
402 EMPATHY_THEME_BOXES (l->data));
406 g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
410 theme_manager_finalize (GObject *object)
412 EmpathyThemeManagerPriv *priv = GET_PRIV (object);
415 empathy_conf_notify_remove (empathy_conf_get (), priv->name_notify_id);
418 for (l = priv->boxes_views; l; l = l->next) {
419 g_object_weak_unref (G_OBJECT (l->data),
420 theme_manager_boxes_weak_notify_cb,
423 g_list_free (priv->boxes_views);
425 G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
429 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
431 GObjectClass *object_class = G_OBJECT_CLASS (klass);
433 signals[THEME_CHANGED] =
434 g_signal_new ("theme-changed",
435 G_OBJECT_CLASS_TYPE (object_class),
439 g_cclosure_marshal_VOID__VOID,
443 g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
445 object_class->finalize = theme_manager_finalize;
449 empathy_theme_manager_init (EmpathyThemeManager *manager)
451 EmpathyThemeManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
452 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
454 manager->priv = priv;
456 /* Take the theme name and track changes */
457 priv->name_notify_id =
458 empathy_conf_notify_add (empathy_conf_get (),
459 EMPATHY_PREFS_CHAT_THEME,
460 theme_manager_notify_name_cb,
462 theme_manager_notify_name_cb (empathy_conf_get (),
463 EMPATHY_PREFS_CHAT_THEME,
466 /* Track GTK color changes */
467 priv->settings = gtk_settings_get_default ();
468 g_signal_connect_swapped (priv->settings, "notify::color-hash",
469 G_CALLBACK (theme_manager_color_hash_notify_cb),
473 EmpathyThemeManager *
474 empathy_theme_manager_get (void)
476 static EmpathyThemeManager *manager = NULL;
479 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
486 empathy_theme_manager_get_themes (void)