File: gtk-decorations.c

package info (click to toggle)
xfce4-settings 4.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,244 kB
  • sloc: ansic: 28,113; sh: 4,365; makefile: 996; python: 111; xml: 43
file content (156 lines) | stat: -rw-r--r-- 4,780 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 *  Copyright (c) 2014 Olivier Fourdan <fourdan@xfce.org>
 *
 *  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 2 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 Library 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.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <xfconf/xfconf.h>
#include <libxfce4util/libxfce4util.h>
#include "gtk-decorations.h"

#define DEFAULT_LAYOUT "O|HMC"

static void xfce_decorations_helper_finalize                  (GObject                  *object);
static void xfce_decorations_helper_channel_property_changed  (XfconfChannel            *channel,
                                                               const gchar              *property_name,
                                                               const GValue             *value,
                                                               XfceDecorationsHelper    *helper);

struct _XfceDecorationsHelperClass
{
    GObjectClass __parent__;
};

struct _XfceDecorationsHelper
{
    GObject  __parent__;

    /* xfconf channel */
    XfconfChannel *wm_channel;
    XfconfChannel *xsettings_channel;
};

G_DEFINE_TYPE (XfceDecorationsHelper, xfce_decorations_helper, G_TYPE_OBJECT)

static void
xfce_decorations_helper_class_init (XfceDecorationsHelperClass *klass)
{
    GObjectClass *gobject_class;

    gobject_class = G_OBJECT_CLASS (klass);
    gobject_class->finalize = xfce_decorations_helper_finalize;
}

static const gchar *
xfce_decorations_button_layout_xlate (char c)
{
    switch (c)
    {
        case 'O':
            return "menu";
            break;
        case 'H':
            return "minimize";
            break;
        case 'M':
            return "maximize";
            break;
        case 'C':
            return "close";
            break;
        case '|':
            return ":";
            break;
        default:
            return NULL;
    }
    return NULL;
}

static void
xfce_decorations_set_decoration_layout (XfceDecorationsHelper *helper,
                                        const gchar *value)
{
    GString *join;
    const gchar *gtk_name;
    gchar *gtk_decoration_layout;
    gboolean add_comma;
    int len, i;

    add_comma = FALSE;
    len = strlen (value);
    join = g_string_new (NULL);
    for (i = 0; i < len; i++)
    {
        gtk_name = xfce_decorations_button_layout_xlate(value[i]);
        if (gtk_name)
        {
            if (add_comma && value[i] != '|')
                join = g_string_append (join, ",");
            join = g_string_append (join, gtk_name);
            add_comma = (value[i] != '|');
        }
    }

    gtk_decoration_layout = g_string_free (join, FALSE);
    xfconf_channel_set_string (helper->xsettings_channel,
                             "/Gtk/DecorationLayout", gtk_decoration_layout);
    g_free (gtk_decoration_layout);
}

static void
xfce_decorations_helper_channel_property_changed (XfconfChannel         *channel,
                                                  const gchar           *property_name,
                                                  const GValue          *value,
                                                  XfceDecorationsHelper *helper)
{
    if (strcmp (property_name, "/general/button_layout") == 0)
    {
        xfce_decorations_set_decoration_layout (helper, g_value_get_string (value));
    }
}

static void
xfce_decorations_helper_init (XfceDecorationsHelper *helper)
{
    gchar *layout;

    helper->wm_channel = xfconf_channel_get ("xfwm4");
    helper->xsettings_channel = xfconf_channel_get ("xsettings");

    layout = xfconf_channel_get_string  (helper->wm_channel,
                                         "/general/button_layout", DEFAULT_LAYOUT);
    xfce_decorations_set_decoration_layout (helper, layout);
    g_free (layout);

    /* monitor WM channel changes */
    g_signal_connect (G_OBJECT (helper->wm_channel), "property-changed",
        G_CALLBACK (xfce_decorations_helper_channel_property_changed), helper);
}

static void
xfce_decorations_helper_finalize (GObject *object)
{
    (*G_OBJECT_CLASS (xfce_decorations_helper_parent_class)->finalize) (object);
}