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,
235 TAG_SET ("foreground", "foreground-set", header_foreground);
236 TAG_SET ("paragraph-background", "paragraph-background-set", header_background);
237 tag = empathy_chat_text_view_tag_set (view, EMPATHY_THEME_BOXES_TAG_HEADER_LINE,
240 TAG_SET ("paragraph-background", "paragraph-background-set", header_line_background);
246 theme_manager_update_simple_tags (EmpathyThemeBoxes *theme)
254 style = gtk_widget_get_default_style ();
256 theme_manager_gdk_color_to_hex (&style->base[GTK_STATE_SELECTED], color1);
257 theme_manager_gdk_color_to_hex (&style->bg[GTK_STATE_SELECTED], color2);
258 theme_manager_gdk_color_to_hex (&style->dark[GTK_STATE_SELECTED], color3);
259 theme_manager_gdk_color_to_hex (&style->fg[GTK_STATE_SELECTED], color4);
261 theme_manager_update_boxes_tags (theme,
262 color4, /* header_foreground */
263 color2, /* header_background */
264 color3, /* header_line_background */
265 color1, /* action_foreground */
266 "darkgrey", /* time_foreground */
267 "darkgrey", /* event_foreground */
268 color1, /* link_foreground */
269 NULL, /* text_foreground */
270 NULL, /* text_background */
271 NULL); /* highlight_foreground */
275 theme_manager_update_boxes_theme (EmpathyThemeManager *manager,
276 EmpathyThemeBoxes *theme)
278 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
280 if (strcmp (priv->name, "simple") == 0) {
281 theme_manager_update_simple_tags (theme);
283 else if (strcmp (priv->name, "clean") == 0) {
284 theme_manager_update_boxes_tags (theme,
285 "black", /* header_foreground */
286 "#efefdf", /* header_background */
287 "#e3e3d3", /* header_line_background */
288 "brown4", /* action_foreground */
289 "darkgrey", /* time_foreground */
290 "darkgrey", /* event_foreground */
291 "#49789e", /* link_foreground */
292 NULL, /* text_foreground */
293 NULL, /* text_background */
294 NULL); /* highlight_foreground */
296 else if (strcmp (priv->name, "blue") == 0) {
297 theme_manager_update_boxes_tags (theme,
298 "black", /* header_foreground */
299 "#88a2b4", /* header_background */
300 "#7f96a4", /* header_line_background */
301 "brown4", /* action_foreground */
302 "darkgrey", /* time_foreground */
303 "#7f96a4", /* event_foreground */
304 "#49789e", /* link_foreground */
305 "black", /* text_foreground */
306 "#adbdc8", /* text_background */
307 "black"); /* highlight_foreground */
312 empathy_theme_manager_create_view (EmpathyThemeManager *manager)
314 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
315 EmpathyThemeBoxes *theme;
317 g_return_val_if_fail (EMPATHY_IS_THEME_MANAGER (manager), NULL);
319 DEBUG ("Using theme %s", priv->name);
321 if (strcmp (priv->name, "classic") == 0) {
322 return EMPATHY_CHAT_VIEW (theme_manager_create_irc_view (manager));
325 theme = theme_manager_create_boxes_view (manager);
326 theme_manager_update_boxes_theme (manager, theme);
328 return EMPATHY_CHAT_VIEW (theme);
332 theme_manager_color_hash_notify_cb (EmpathyThemeManager *manager)
334 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
336 /* FIXME: Make that work, it should update color when theme changes but
337 * it doesnt seems to work with all themes. */
339 if (strcmp (priv->name, "simple") == 0) {
342 /* We are using the simple theme which use the GTK theme color,
343 * Update views to use the new color. */
344 for (l = priv->boxes_views; l; l = l->next) {
345 theme_manager_update_simple_tags (EMPATHY_THEME_BOXES (l->data));
351 theme_manager_ensure_theme_exists (const gchar *name)
355 if (EMP_STR_EMPTY (name)) {
359 for (i = 0; themes[i]; i += 2) {
360 if (strcmp (themes[i], name) == 0) {
369 theme_manager_notify_name_cb (EmpathyConf *conf,
373 EmpathyThemeManager *manager = EMPATHY_THEME_MANAGER (user_data);
374 EmpathyThemeManagerPriv *priv = GET_PRIV (manager);
377 if (!empathy_conf_get_string (conf, key, &name) ||
378 !theme_manager_ensure_theme_exists (name) ||
379 !tp_strdiff (priv->name, name)) {
381 priv->name = g_strdup ("classic");
391 if (!tp_strdiff (priv->name, "simple") ||
392 !tp_strdiff (priv->name, "clean") ||
393 !tp_strdiff (priv->name, "blue")) {
396 /* The theme changes to a boxed one, we can update boxed views */
397 for (l = priv->boxes_views; l; l = l->next) {
398 theme_manager_update_boxes_theme (manager,
399 EMPATHY_THEME_BOXES (l->data));
403 g_signal_emit (manager, signals[THEME_CHANGED], 0, NULL);
407 theme_manager_finalize (GObject *object)
409 EmpathyThemeManagerPriv *priv = GET_PRIV (object);
412 empathy_conf_notify_remove (empathy_conf_get (), priv->name_notify_id);
415 for (l = priv->boxes_views; l; l = l->next) {
416 g_object_weak_unref (G_OBJECT (l->data),
417 theme_manager_boxes_weak_notify_cb,
420 g_list_free (priv->boxes_views);
422 G_OBJECT_CLASS (empathy_theme_manager_parent_class)->finalize (object);
426 empathy_theme_manager_class_init (EmpathyThemeManagerClass *klass)
428 GObjectClass *object_class = G_OBJECT_CLASS (klass);
430 signals[THEME_CHANGED] =
431 g_signal_new ("theme-changed",
432 G_OBJECT_CLASS_TYPE (object_class),
436 g_cclosure_marshal_VOID__VOID,
440 g_type_class_add_private (object_class, sizeof (EmpathyThemeManagerPriv));
442 object_class->finalize = theme_manager_finalize;
446 empathy_theme_manager_init (EmpathyThemeManager *manager)
448 EmpathyThemeManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
449 EMPATHY_TYPE_THEME_MANAGER, EmpathyThemeManagerPriv);
451 manager->priv = priv;
453 /* Take the theme name and track changes */
454 priv->name_notify_id =
455 empathy_conf_notify_add (empathy_conf_get (),
456 EMPATHY_PREFS_CHAT_THEME,
457 theme_manager_notify_name_cb,
459 theme_manager_notify_name_cb (empathy_conf_get (),
460 EMPATHY_PREFS_CHAT_THEME,
463 /* Track GTK color changes */
464 priv->settings = gtk_settings_get_default ();
465 g_signal_connect_swapped (priv->settings, "notify::color-hash",
466 G_CALLBACK (theme_manager_color_hash_notify_cb),
470 EmpathyThemeManager *
471 empathy_theme_manager_get (void)
473 static EmpathyThemeManager *manager = NULL;
476 manager = g_object_new (EMPATHY_TYPE_THEME_MANAGER, NULL);
483 empathy_theme_manager_get_themes (void)