123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- /* $Id$
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Copyright (C) 2013 Cedric Leporcq <cedl38@gmail.com>
- *
- * Parts of this code is derived from xfwm4 sources (setting.c)
- *
- */
- #include <common/mypixmap.h>
- #include <common/theme.h>
- #include "wckbuttons-theme.h"
- #define XPM_COLOR_SYMBOL_SIZE 22
- static gboolean
- set_g_value (const gchar * lvalue, const GValue *rvalue, Settings *rc)
- {
- gint i;
- TRACE ("entering setValue");
- g_return_val_if_fail (lvalue != NULL, FALSE);
- g_return_val_if_fail (rvalue != NULL, FALSE);
- for (i = 0; rc[i].option; i++)
- {
- if (!g_ascii_strcasecmp (lvalue, rc[i].option))
- {
- if (rvalue)
- {
- if (rc[i].value)
- {
- g_value_unset (rc[i].value);
- g_value_init (rc[i].value, G_VALUE_TYPE(rvalue));
- }
- else
- {
- rc[i].value = g_new0(GValue, 1);
- g_value_init (rc[i].value, G_VALUE_TYPE(rvalue));
- }
- g_value_copy (rvalue, rc[i].value);
- return TRUE;
- }
- }
- }
- return FALSE;
- }
- static gboolean
- set_string_value (const gchar * lvalue, const gchar *value, Settings *rc)
- {
- GValue tmp_val = {0, };
- g_value_init(&tmp_val, G_TYPE_STRING);
- g_value_set_static_string(&tmp_val, value);
- return set_g_value (lvalue, &tmp_val, rc);
- }
- static void get_unity_pixbuf (const gchar *themedir, WBPlugin *wb) {
- gint i,j;
- gchar imagename[40];
- static const char *button_names[] = {
- "minimize",
- "unmaximize",
- "maximize",
- "close"
- };
- static const char *button_state_names[] = {
- "unfocused",
- "focused_normal",
- "focused_prelight",
- "focused_pressed"
- };
- for (i = 0; i < IMAGES_BUTTONS; i++)
- {
- for (j = 0; j < IMAGES_STATES; j++)
- {
- g_snprintf(imagename, sizeof (imagename), "%s_%s", button_names[i], button_state_names[j]);
- wb->pixbufs[i][j] = pixbuf_alpha_load (themedir, imagename);
- }
- }
- }
- gchar *button_layout_filter (const gchar *string, const gchar *default_layout)
- {
- guint i, j;
- gchar layout[BUTTONS] = {0};
- /* WARNING : beware of bluffer overflow !!! */
- j = 0;
- for (i = 0; i < strlen (string) && j < BUTTONS; i++)
- {
- switch (string[i])
- {
- case 'H':
- layout[j++] = 'H';
- break;
- case 'M':
- layout[j++] = 'M';
- break;
- case 'C':
- layout[j++] = 'C';
- }
- }
- layout[j] = '\0';
- if (layout[0] == '\0')
- return g_strdup (default_layout);
- return g_strdup (layout);
- }
- gchar *opposite_layout_filter (const gchar *string)
- {
- guint i, j;
- gchar layout[8] = {0};
- /* WARNING : beware of bluffer overflow !!! */
- j = 0;
- for (i = 0; i < strlen (string) && j < 8; i++)
- {
- switch (string[i])
- {
- case 'H':
- case 'M':
- case 'C':
- break;
- default:
- layout[j] = string[i];
- j++;
- }
- }
- layout[j] = '\0';
- return g_strdup (layout);
- }
- static int get_button_from_letter (char chr)
- {
- TRACE ("entering get_button_from_letter");
- switch (chr)
- {
- case 'H':
- return MINIMIZE_BUTTON;
- case 'M':
- return MAXIMIZE_BUTTON;
- case 'C':
- return CLOSE_BUTTON;
- default:
- return -1;
- }
- }
- /* Replace buttons accordingly to button_layout and visible state */
- void replace_buttons (const gchar *button_layout, WBPlugin *wb)
- {
- guint i, j;
- gint button;
- for (i = 0; i < BUTTONS; i++)
- gtk_widget_hide_all(GTK_WIDGET(wb->button[i]->eventbox));
- j = 0;
- for (i = 0; i < strlen (button_layout); i++)
- {
- button = get_button_from_letter (button_layout[i]);
- if (button >= 0 && wb->button[button]->image)
- {
- gtk_box_reorder_child (GTK_BOX (wb->hvbox), GTK_WIDGET(wb->button[button]->eventbox), j);
- gtk_widget_show_all(GTK_WIDGET(wb->button[button]->eventbox));
- j++;
- }
- }
- }
- gchar *get_rc_button_layout (const gchar *theme)
- {
- gchar *wm_themedir;
- wm_themedir = test_theme_dir(theme, "xfwm4", THEMERC);
- if (G_LIKELY(wm_themedir))
- {
- const gchar *rc_button_layout;
- gchar *filename;
- XfceRc *rc;
- /* check in the rc if the theme supports a custom button layout */
- filename = g_build_filename (wm_themedir, THEMERC, NULL);
- rc = xfce_rc_simple_open (filename, TRUE);
- g_free (filename);
- if (G_LIKELY (rc))
- {
- rc_button_layout = xfce_rc_read_entry (rc, "button_layout", NULL);
- if (rc_button_layout)
- return button_layout_filter (rc_button_layout, NULL);
- }
- }
- g_free (wm_themedir);
- return NULL;
- }
- /* load the theme according to xfwm4 theme format */
- void load_theme (const gchar *theme, WBPlugin *wb)
- {
- gint i;
- gchar *themedir;
- /* get theme dir */
- themedir = get_unity_theme_dir (wb->prefs->theme, DEFAULT_THEME);
- if (themedir)
- {
- get_unity_pixbuf (themedir, wb);
- g_free (themedir);
- /* try to replace missing images */
-
- for (i = 0; i < IMAGES_STATES; i++)
- {
- if (!wb->pixbufs[IMAGE_UNMAXIMIZE][i])
- wb->pixbufs[IMAGE_UNMAXIMIZE][i] = wb->pixbufs[IMAGE_MAXIMIZE][i];
- }
- for (i = 0; i < IMAGES_BUTTONS; i++)
- {
- if (!wb->pixbufs[i][IMAGE_UNFOCUSED] || !wb->pixbufs[i][IMAGE_PRESSED])
- wb->pixbufs[i][IMAGE_UNFOCUSED] = wb->pixbufs[i][IMAGE_FOCUSED];
-
- if (!wb->pixbufs[i][IMAGE_PRELIGHT])
- wb->pixbufs[i][IMAGE_PRELIGHT] = wb->pixbufs[i][IMAGE_PRESSED];
- }
- }
- }
- static void apply_wm_theme (WBPlugin *wb)
- {
- const gchar *wm_theme = xfconf_channel_get_string (wb->wm_channel, "/general/theme", NULL);
- if (G_LIKELY(wm_theme))
- {
- gchar *button_layout;
- wb->prefs->theme = g_strdup (wm_theme);
- load_theme (wb->prefs->theme, wb);
- button_layout = get_rc_button_layout (wm_theme);
- if (button_layout)
- {
- replace_buttons (button_layout, wb);
- }
- else
- {
- const gchar *wm_buttons_layout = xfconf_channel_get_string (wb->wm_channel, "/general/button_layout", wb->prefs->button_layout);
- wb->prefs->button_layout = button_layout_filter (wm_buttons_layout, wb->prefs->button_layout);
- replace_buttons (wb->prefs->button_layout, wb);
- }
- g_free (button_layout);
- }
- on_wck_state_changed (wb->win->controlwindow, wb);
- }
- static void
- on_x_chanel_property_changed (XfconfChannel *x_channel, const gchar *property_name, const GValue *value, WBPlugin *wb)
- {
- if (g_str_has_prefix(property_name, "/Net/") == TRUE)
- {
- const gchar *name = &property_name[5];
- switch (G_VALUE_TYPE(value))
- {
- case G_TYPE_STRING:
- if (!strcmp (name, "ThemeName"))
- {
- apply_wm_theme (wb);
- }
- break;
- default:
- g_warning("The property '%s' is not supported", property_name);
- break;
- }
- }
- }
- static void
- on_xfwm_channel_property_changed (XfconfChannel *wm_channel, const gchar *property_name, const GValue *value, WBPlugin *wb)
- {
- if (g_str_has_prefix(property_name, "/general/") == TRUE)
- {
- const gchar *name = &property_name[9];
- switch (G_VALUE_TYPE(value))
- {
- case G_TYPE_STRING:
- if (!strcmp (name, "theme")
- || !strcmp (name, "button_layout"))
- {
- apply_wm_theme (wb);
- }
- break;
- default:
- g_warning("The property '%s' is not supported", property_name);
- break;
- }
- }
- }
- void init_theme (WBPlugin *wb)
- {
- /* get the xfwm4 chanel */
- wb->wm_channel = wck_properties_get_channel (G_OBJECT (wb->plugin), "xfwm4");
- /* try to use the xfwm4 theme */
- if (wb->wm_channel && wb->prefs->sync_wm_theme)
- {
- apply_wm_theme (wb);
- g_signal_connect (wb->wm_channel, "property-changed", G_CALLBACK (on_xfwm_channel_property_changed), wb);
- }
- else
- {
- load_theme (wb->prefs->theme, wb);
- replace_buttons (wb->prefs->button_layout, wb);
- }
- /* get the xsettings chanel to update the gtk theme */
- wb->x_channel = wck_properties_get_channel (G_OBJECT (wb->plugin), "xsettings");
- if (wb->x_channel)
- g_signal_connect (wb->x_channel, "property-changed", G_CALLBACK (on_x_chanel_property_changed), wb);
- }
|