summaryrefslogtreecommitdiff
path: root/libmetacity/meta-style-info.c
blob: d4af8730cbf2debf8e930e2e8d6198ac1de01598 (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Copyright (C) 2001 Havoc Pennington
 * Copyright (C) 2016 Alberts Muktupāvels
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "meta-frame-enums.h"
#include "meta-style-info-private.h"
#include "meta-theme-impl-private.h"

static void
add_toplevel_class (GtkStyleContext *style,
                    const gchar     *class_name)
{
  if (gtk_style_context_get_parent (style))
    {
      GtkWidgetPath *path;

      path = gtk_widget_path_copy (gtk_style_context_get_path (style));
      gtk_widget_path_iter_add_class (path, 0, class_name);
      gtk_style_context_set_path (style, path);
      gtk_widget_path_unref (path);
    }
  else
    gtk_style_context_add_class (style, class_name);
}

static void
remove_toplevel_class (GtkStyleContext *style,
                       const gchar     *class_name)
{
  if (gtk_style_context_get_parent (style))
    {
      GtkWidgetPath *path;

      path = gtk_widget_path_copy (gtk_style_context_get_path (style));
      gtk_widget_path_iter_remove_class (path, 0, class_name);
      gtk_style_context_set_path (style, path);
      gtk_widget_path_unref (path);
    }
  else
    gtk_style_context_remove_class (style, class_name);
}

static GtkStyleContext *
create_style_context (GtkStyleContext *parent,
                      GtkCssProvider  *provider,
                      const char      *object_name,
                      const char      *first_class,
                      ...)
{
  GtkWidgetPath *path;
  GtkStyleContext *context;
  const char *name;
  va_list ap;

  if (parent)
    path = gtk_widget_path_copy (gtk_style_context_get_path (parent));
  else
    path = gtk_widget_path_new ();

  gtk_widget_path_append_type (path, G_TYPE_NONE);
  gtk_widget_path_iter_set_object_name (path, -1, object_name);

  va_start (ap, first_class);
  for (name = first_class; name; name = va_arg (ap, const char *))
    gtk_widget_path_iter_add_class (path, -1, name);
  va_end (ap);

  context = gtk_style_context_new ();
  gtk_style_context_set_path (context, path);
  gtk_style_context_set_parent (context, parent);
  gtk_style_context_set_scale (context, get_window_scaling_factor ());
  gtk_widget_path_unref (path);

  gtk_style_context_add_provider (context, GTK_STYLE_PROVIDER (provider),
                                  GTK_STYLE_PROVIDER_PRIORITY_SETTINGS);

  return context;
}

MetaStyleInfo *
meta_style_info_new (const gchar *theme_name,
                     const gchar *variant,
                     gboolean     composited)
{
  MetaStyleInfo *style_info;
  GtkCssProvider *provider;

  if (theme_name && *theme_name)
    provider = gtk_css_provider_get_named (theme_name, variant);
  else
    provider = gtk_css_provider_get_default ();

  style_info = g_new0 (MetaStyleInfo, 1);
  style_info->refcount = 1;

  style_info->styles[META_STYLE_ELEMENT_WINDOW] =
    create_style_context (NULL,
                          provider,
                          "window",
                          GTK_STYLE_CLASS_BACKGROUND,
                          composited == TRUE ? "ssd" : "solid-csd",
                          NULL);
  style_info->styles[META_STYLE_ELEMENT_DECORATION] =
    create_style_context (style_info->styles[META_STYLE_ELEMENT_WINDOW],
                          provider,
                          "decoration",
                          NULL);
  style_info->styles[META_STYLE_ELEMENT_TITLEBAR] =
    create_style_context (style_info->styles[META_STYLE_ELEMENT_DECORATION],
                          provider,
                          "headerbar",
                          GTK_STYLE_CLASS_TITLEBAR,
                          GTK_STYLE_CLASS_HORIZONTAL,
                          "default-decoration",
                          NULL);
  style_info->styles[META_STYLE_ELEMENT_TITLE] =
    create_style_context (style_info->styles[META_STYLE_ELEMENT_TITLEBAR],
                          provider,
                          "label",
                          GTK_STYLE_CLASS_TITLE,
                          NULL);
  style_info->styles[META_STYLE_ELEMENT_BUTTON] =
    create_style_context (style_info->styles[META_STYLE_ELEMENT_TITLEBAR],
                          provider,
                          "button",
                          "titlebutton",
                          NULL);
  style_info->styles[META_STYLE_ELEMENT_IMAGE] =
    create_style_context (style_info->styles[META_STYLE_ELEMENT_BUTTON],
                          provider,
                          "image",
                          NULL);

  return style_info;
}

MetaStyleInfo *
meta_style_info_ref (MetaStyleInfo *style_info)
{
  g_return_val_if_fail (style_info != NULL, NULL);
  g_return_val_if_fail (style_info->refcount > 0, NULL);

  g_atomic_int_inc ((volatile int *)&style_info->refcount);
  return style_info;
}

void
meta_style_info_unref (MetaStyleInfo *style_info)
{
  g_return_if_fail (style_info != NULL);
  g_return_if_fail (style_info->refcount > 0);

  if (g_atomic_int_dec_and_test ((volatile int *)&style_info->refcount))
    {
      int i;
      for (i = 0; i < META_STYLE_ELEMENT_LAST; i++)
        g_object_unref (style_info->styles[i]);
      g_free (style_info);
    }
}

void
meta_style_info_set_flags (MetaStyleInfo  *style_info,
                           MetaFrameFlags  flags)
{
  GtkStyleContext *style;
  gboolean backdrop;
  int i;

  backdrop = !(flags & META_FRAME_HAS_FOCUS);
  if (flags & META_FRAME_IS_FLASHING)
    backdrop = !backdrop;

  for (i = 0; i < META_STYLE_ELEMENT_LAST; i++)
    {
      GtkStateFlags state;

      style = style_info->styles[i];

      state = gtk_style_context_get_state (style);
      if (backdrop)
        gtk_style_context_set_state (style, state | GTK_STATE_FLAG_BACKDROP);
      else
        gtk_style_context_set_state (style, state & ~GTK_STATE_FLAG_BACKDROP);

      if (flags & META_FRAME_TILED_LEFT || flags & META_FRAME_TILED_RIGHT)
        add_toplevel_class (style, "tiled");
      else
        remove_toplevel_class (style, "tiled");

      if (flags & META_FRAME_MAXIMIZED)
        add_toplevel_class (style, "maximized");
      else
        remove_toplevel_class (style, "maximized");

      if (flags & META_FRAME_FULLSCREEN)
        add_toplevel_class (style, "fullscreen");
      else
        remove_toplevel_class (style, "fullscreen");
    }
}