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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
|
/*
* 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 <glib/gi18n-lib.h>
#include <string.h>
#include "meta-enum-types.h"
#include "meta-frame-layout-private.h"
#include "meta-theme.h"
#include "meta-theme-gtk-private.h"
#include "meta-theme-impl-private.h"
#include "meta-theme-metacity-private.h"
#include "meta-style-info-private.h"
struct _MetaTheme
{
GObject parent;
MetaThemeType type;
MetaThemeImpl *impl;
gboolean composited;
PangoFontDescription *titlebar_font;
gchar *gtk_theme_name;
GHashTable *variants;
PangoContext *context;
GHashTable *font_descs;
GHashTable *title_heights;
};
enum
{
PROP_0,
PROP_TYPE,
LAST_PROP
};
static GParamSpec *properties[LAST_PROP] = { NULL };
G_DEFINE_TYPE (MetaTheme, meta_theme, G_TYPE_OBJECT)
static void
ensure_pango_context (MetaTheme *theme)
{
GdkScreen *screen;
PangoFontMap *fontmap;
PangoContext *context;
const cairo_font_options_t *options;
gdouble dpi;
if (theme->context != NULL)
return;
screen = gdk_screen_get_default ();
fontmap = pango_cairo_font_map_get_default ();
context = pango_font_map_create_context (fontmap);
options = gdk_screen_get_font_options (screen);
pango_cairo_context_set_font_options (context, options);
dpi = gdk_screen_get_resolution (screen);
pango_cairo_context_set_resolution (context, dpi);
theme->context = context;
}
static gint
get_title_height (MetaTheme *theme,
const gchar *variant,
MetaFrameType type,
MetaFrameFlags flags)
{
PangoFontDescription *description;
gpointer size;
gpointer height;
gint title_height;
description = meta_theme_get_title_font_desc (theme, variant, type, flags);
g_assert (description != NULL);
size = GINT_TO_POINTER (pango_font_description_get_size (description));
height = g_hash_table_lookup (theme->title_heights, size);
if (height != NULL)
{
title_height = GPOINTER_TO_INT (height);
}
else
{
PangoLanguage *lang;
PangoFontMetrics *metrics;
gint ascent;
gint descent;
gint scale;
ensure_pango_context (theme);
lang = pango_context_get_language (theme->context);
metrics = pango_context_get_metrics (theme->context, description, lang);
ascent = pango_font_metrics_get_ascent (metrics);
descent = pango_font_metrics_get_descent (metrics);
pango_font_metrics_unref (metrics);
title_height = PANGO_PIXELS (ascent + descent);
scale = get_window_scaling_factor ();
title_height *= scale;
height = GINT_TO_POINTER (title_height);
g_hash_table_insert (theme->title_heights, size, height);
}
return title_height;
}
static void
font_desc_apply_scale (PangoFontDescription *font_desc,
MetaTheme *theme,
MetaFrameType type,
MetaFrameFlags flags)
{
gint old_size;
MetaFrameStyle *style;
gdouble scale;
gint new_size;
old_size = pango_font_description_get_size (font_desc);
style = meta_theme_get_frame_style (theme, type, flags);
scale = get_window_scaling_factor ();
new_size = MAX (old_size * (style->layout->title_scale / scale), 1);
pango_font_description_set_size (font_desc, new_size);
}
static MetaStyleInfo *
meta_theme_get_style_info (MetaTheme *theme,
const gchar *variant)
{
const gchar *key;
MetaStyleInfo *style_info;
key = variant;
if (variant == NULL)
key = "default";
style_info = g_hash_table_lookup (theme->variants, key);
if (style_info == NULL)
{
gint window_scale;
window_scale = get_window_scaling_factor ();
style_info = meta_style_info_new (theme->gtk_theme_name, variant,
theme->composited, window_scale);
g_hash_table_insert (theme->variants, g_strdup (key), style_info);
}
return style_info;
}
static void
meta_theme_constructed (GObject *object)
{
MetaTheme *theme;
G_OBJECT_CLASS (meta_theme_parent_class)->constructed (object);
theme = META_THEME (object);
if (theme->type == META_THEME_TYPE_GTK)
theme->impl = g_object_new (META_TYPE_THEME_GTK, NULL);
else if (theme->type == META_THEME_TYPE_METACITY)
theme->impl = g_object_new (META_TYPE_THEME_METACITY, NULL);
else
g_assert_not_reached ();
meta_theme_impl_set_composited (theme->impl, theme->composited);
}
static void
meta_theme_dispose (GObject *object)
{
MetaTheme *theme;
theme = META_THEME (object);
g_clear_object (&theme->impl);
g_clear_pointer (&theme->variants, g_hash_table_destroy);
g_clear_object (&theme->context);
g_clear_pointer (&theme->font_descs, g_hash_table_destroy);
g_clear_pointer (&theme->title_heights, g_hash_table_destroy);
G_OBJECT_CLASS (meta_theme_parent_class)->dispose (object);
}
static void
meta_theme_finalize (GObject *object)
{
MetaTheme *theme;
theme = META_THEME (object);
if (theme->titlebar_font)
{
pango_font_description_free (theme->titlebar_font);
theme->titlebar_font = NULL;
}
g_free (theme->gtk_theme_name);
G_OBJECT_CLASS (meta_theme_parent_class)->finalize (object);
}
static void
meta_theme_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
MetaTheme *theme;
theme = META_THEME (object);
switch (property_id)
{
case PROP_TYPE:
theme->type = g_value_get_enum (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
meta_theme_install_properties (GObjectClass *object_class)
{
properties[PROP_TYPE] =
g_param_spec_enum ("type", "type", "type",
META_TYPE_THEME_TYPE, META_THEME_TYPE_GTK,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE |
G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, LAST_PROP, properties);
}
static void
meta_theme_class_init (MetaThemeClass *theme_class)
{
GObjectClass *object_class;
object_class = G_OBJECT_CLASS (theme_class);
object_class->constructed = meta_theme_constructed;
object_class->dispose = meta_theme_dispose;
object_class->finalize = meta_theme_finalize;
object_class->set_property = meta_theme_set_property;
meta_theme_install_properties (object_class);
}
static void
meta_theme_init (MetaTheme *theme)
{
theme->composited = TRUE;
theme->variants = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, g_object_unref);
theme->font_descs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
(GDestroyNotify) pango_font_description_free);
theme->title_heights = g_hash_table_new (NULL, NULL);
}
/**
* meta_theme_error_quark:
*
* Domain for #MetaThemeError errors.
*
* Returns: the #GQuark identifying the #MetaThemeError domain.
*/
GQuark
meta_theme_error_quark (void)
{
return g_quark_from_static_string ("meta-theme-error-quark");
}
MetaTheme *
meta_theme_new (MetaThemeType type)
{
return g_object_new (META_TYPE_THEME, "type", type, NULL);
}
gboolean
meta_theme_load (MetaTheme *theme,
const gchar *name,
GError **error)
{
g_free (theme->gtk_theme_name);
if (theme->type == META_THEME_TYPE_GTK)
{
theme->gtk_theme_name = g_strdup (name);
}
else if (theme->type == META_THEME_TYPE_METACITY)
{
GtkSettings *settings;
settings = gtk_settings_get_default ();
g_object_get (settings, "gtk-theme-name", &theme->gtk_theme_name, NULL);
}
else
{
g_assert_not_reached ();
}
return META_THEME_IMPL_GET_CLASS (theme->impl)->load (theme->impl, name,
error);
}
void
meta_theme_invalidate (MetaTheme *theme)
{
g_hash_table_remove_all (theme->variants);
g_clear_object (&theme->context);
g_hash_table_remove_all (theme->font_descs);
g_hash_table_remove_all (theme->title_heights);
}
void
meta_theme_set_composited (MetaTheme *theme,
gboolean composited)
{
if (theme->composited == composited)
return;
theme->composited = composited;
meta_theme_impl_set_composited (theme->impl, composited);
meta_theme_invalidate (theme);
}
void
meta_theme_set_titlebar_font (MetaTheme *theme,
const PangoFontDescription *titlebar_font)
{
pango_font_description_free (theme->titlebar_font);
theme->titlebar_font = pango_font_description_copy (titlebar_font);
g_hash_table_remove_all (theme->font_descs);
g_hash_table_remove_all (theme->title_heights);
}
MetaFrameStyle *
meta_theme_get_frame_style (MetaTheme *theme,
MetaFrameType type,
MetaFrameFlags flags)
{
MetaFrameState state;
MetaFrameResize resize;
MetaFrameFocus focus;
MetaFrameStyle *style;
MetaFrameStyleSet *style_set;
g_return_val_if_fail (type < META_FRAME_TYPE_LAST, NULL);
style_set = meta_theme_impl_get_style_set (theme->impl, type);
if (style_set == NULL && type == META_FRAME_TYPE_ATTACHED)
style_set = meta_theme_impl_get_style_set (theme->impl, META_FRAME_TYPE_BORDER);
/* Right now the parser forces a style set for all other types,
* but this fallback code is here in case I take that out.
*/
if (style_set == NULL)
style_set = meta_theme_impl_get_style_set (theme->impl, META_FRAME_TYPE_NORMAL);
if (style_set == NULL)
return NULL;
switch (flags & (META_FRAME_MAXIMIZED | META_FRAME_SHADED | META_FRAME_TILED_LEFT | META_FRAME_TILED_RIGHT))
{
case 0:
state = META_FRAME_STATE_NORMAL;
break;
case META_FRAME_MAXIMIZED:
state = META_FRAME_STATE_MAXIMIZED;
break;
case META_FRAME_TILED_LEFT:
state = META_FRAME_STATE_TILED_LEFT;
break;
case META_FRAME_TILED_RIGHT:
state = META_FRAME_STATE_TILED_RIGHT;
break;
case META_FRAME_SHADED:
state = META_FRAME_STATE_SHADED;
break;
case (META_FRAME_MAXIMIZED | META_FRAME_SHADED):
state = META_FRAME_STATE_MAXIMIZED_AND_SHADED;
break;
case (META_FRAME_TILED_LEFT | META_FRAME_SHADED):
state = META_FRAME_STATE_TILED_LEFT_AND_SHADED;
break;
case (META_FRAME_TILED_RIGHT | META_FRAME_SHADED):
state = META_FRAME_STATE_TILED_RIGHT_AND_SHADED;
break;
default:
g_assert_not_reached ();
state = META_FRAME_STATE_LAST; /* compiler */
break;
}
switch (flags & (META_FRAME_ALLOWS_VERTICAL_RESIZE | META_FRAME_ALLOWS_HORIZONTAL_RESIZE))
{
case 0:
resize = META_FRAME_RESIZE_NONE;
break;
case META_FRAME_ALLOWS_VERTICAL_RESIZE:
resize = META_FRAME_RESIZE_VERTICAL;
break;
case META_FRAME_ALLOWS_HORIZONTAL_RESIZE:
resize = META_FRAME_RESIZE_HORIZONTAL;
break;
case (META_FRAME_ALLOWS_VERTICAL_RESIZE | META_FRAME_ALLOWS_HORIZONTAL_RESIZE):
resize = META_FRAME_RESIZE_BOTH;
break;
default:
g_assert_not_reached ();
resize = META_FRAME_RESIZE_LAST; /* compiler */
break;
}
/* re invert the styles used for focus/unfocussed while flashing a frame */
if (((flags & META_FRAME_HAS_FOCUS) && !(flags & META_FRAME_IS_FLASHING))
|| (!(flags & META_FRAME_HAS_FOCUS) && (flags & META_FRAME_IS_FLASHING)))
focus = META_FRAME_FOCUS_YES;
else
focus = META_FRAME_FOCUS_NO;
style = meta_frame_style_set_get_style (style_set, state, resize, focus);
return style;
}
/**
* meta_theme_create_title_layout:
* @theme: a #MetaTheme
* @title: (nullable): text to set on the layout
*
* Returns: (transfer full): the new #PangoLayout
*/
PangoLayout *
meta_theme_create_title_layout (MetaTheme *theme,
const gchar *title)
{
PangoLayout *layout;
ensure_pango_context (theme);
layout = pango_layout_new (theme->context);
if (title)
pango_layout_set_text (layout, title, -1);
pango_layout_set_auto_dir (layout, FALSE);
pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
pango_layout_set_single_paragraph_mode (layout, TRUE);
return layout;
}
/**
* meta_theme_get_title_font_desc:
* @theme: a #MetaTheme
* @variant: (nullable): theme variant
* @type: frame type
* @flags: frame flags
*
* Returns: (transfer none): the #PangoFontDescription
*/
PangoFontDescription*
meta_theme_get_title_font_desc (MetaTheme *theme,
const gchar *variant,
MetaFrameType type,
MetaFrameFlags flags)
{
gchar *key;
PangoFontDescription *font_desc;
MetaStyleInfo *style_info;
GtkStyleContext *context;
key = g_strdup_printf ("%s_%d_%x", variant ? variant : "default", type, flags);
font_desc = g_hash_table_lookup (theme->font_descs, key);
if (font_desc != NULL)
{
g_free (key);
return font_desc;
}
style_info = meta_theme_get_style_info (theme, variant);
context = meta_style_info_get_style (style_info, META_STYLE_ELEMENT_TITLE);
gtk_style_context_save (context);
gtk_style_context_set_state (context, GTK_STATE_FLAG_NORMAL);
gtk_style_context_get (context, GTK_STATE_FLAG_NORMAL,
"font", &font_desc, NULL);
gtk_style_context_restore (context);
if (theme->titlebar_font)
pango_font_description_merge (font_desc, theme->titlebar_font, TRUE);
font_desc_apply_scale (font_desc, theme, type, flags);
g_hash_table_insert (theme->font_descs, key, font_desc);
return font_desc;
}
MetaFrameType
meta_frame_type_from_string (const gchar *str)
{
if (strcmp ("normal", str) == 0)
return META_FRAME_TYPE_NORMAL;
else if (strcmp ("dialog", str) == 0)
return META_FRAME_TYPE_DIALOG;
else if (strcmp ("modal_dialog", str) == 0)
return META_FRAME_TYPE_MODAL_DIALOG;
else if (strcmp ("utility", str) == 0)
return META_FRAME_TYPE_UTILITY;
else if (strcmp ("menu", str) == 0)
return META_FRAME_TYPE_MENU;
else if (strcmp ("border", str) == 0)
return META_FRAME_TYPE_BORDER;
else if (strcmp ("attached", str) == 0)
return META_FRAME_TYPE_ATTACHED;
else
return META_FRAME_TYPE_LAST;
}
void
meta_theme_get_frame_borders (MetaTheme *theme,
const gchar *theme_variant,
MetaFrameType type,
MetaFrameFlags flags,
MetaFrameBorders *borders)
{
MetaFrameStyle *style;
MetaThemeImplClass *impl_class;
MetaStyleInfo *style_info;
gint title_height;
g_return_if_fail (type < META_FRAME_TYPE_LAST);
meta_frame_borders_clear (borders);
style = meta_theme_get_frame_style (theme, type, flags);
/* Parser is not supposed to allow this currently */
if (style == NULL)
return;
impl_class = META_THEME_IMPL_GET_CLASS (theme->impl);
style_info = meta_theme_get_style_info (theme, theme_variant);
title_height = get_title_height (theme, theme_variant, type, flags);
impl_class->get_frame_borders (theme->impl, style->layout, style_info,
title_height, flags, type, borders);
}
void
meta_theme_calc_geometry (MetaTheme *theme,
const gchar *theme_variant,
MetaFrameType type,
MetaFrameFlags flags,
gint client_width,
gint client_height,
const MetaButtonLayout *button_layout,
MetaFrameGeometry *fgeom)
{
MetaFrameStyle *style;
MetaThemeImplClass *impl_class;
MetaStyleInfo *style_info;
gint title_height;
g_return_if_fail (type < META_FRAME_TYPE_LAST);
style = meta_theme_get_frame_style (theme, type, flags);
/* Parser is not supposed to allow this currently */
if (style == NULL)
return;
impl_class = META_THEME_IMPL_GET_CLASS (theme->impl);
style_info = meta_theme_get_style_info (theme, theme_variant);
title_height = get_title_height (theme, theme_variant, type, flags);
impl_class->calc_geometry (theme->impl, style->layout, style_info,
title_height, flags, client_width, client_height,
button_layout, type, fgeom);
}
void
meta_theme_draw_frame (MetaTheme *theme,
const gchar *theme_variant,
cairo_t *cr,
MetaFrameType type,
MetaFrameFlags flags,
int client_width,
int client_height,
PangoLayout *title_layout,
const MetaButtonLayout *button_layout,
MetaButtonState button_states[META_BUTTON_TYPE_LAST],
GdkPixbuf *mini_icon,
GdkPixbuf *icon)
{
MetaFrameStyle *style;
MetaThemeImplClass *impl_class;
MetaStyleInfo *style_info;
gint title_height;
MetaFrameGeometry fgeom;
g_return_if_fail (type < META_FRAME_TYPE_LAST);
style = meta_theme_get_frame_style (theme, type, flags);
/* Parser is not supposed to allow this currently */
if (style == NULL)
return;
impl_class = META_THEME_IMPL_GET_CLASS (theme->impl);
style_info = meta_theme_get_style_info (theme, theme_variant);
title_height = get_title_height (theme, theme_variant, type, flags);
impl_class->calc_geometry (theme->impl, style->layout, style_info,
title_height, flags, client_width, client_height,
button_layout, type, &fgeom);
impl_class->draw_frame (theme->impl, style, style_info, cr, &fgeom,
title_layout, flags, button_states, mini_icon, icon);
}
|