summaryrefslogtreecommitdiff
path: root/libmetacity/meta-theme-gtk.c
blob: f80c553576ecdaca6dedd68e8548b5db6963b0b1 (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
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
/*
 * 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 <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>

#include "meta-rectangle-private.h"
#include "meta-frame-layout-private.h"
#include "meta-frame-style-private.h"
#include "meta-theme-gtk-private.h"
#include "meta-theme.h"

struct _MetaThemeGtk
{
  MetaThemeImpl parent;
};

G_DEFINE_TYPE (MetaThemeGtk, meta_theme_gtk, META_TYPE_THEME_IMPL)

static gboolean
meta_theme_gtk_load (MetaThemeImpl  *impl,
                     const gchar    *name,
                     GError        **error)
{
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  return TRUE;
}

static void
get_margin (GtkStyleContext *style,
            GtkBorder       *border)
{
  GtkStateFlags state;

  state = gtk_style_context_get_state (style);

  gtk_style_context_get_margin (style, state, border);
}

static void
get_padding_and_border (GtkStyleContext *style,
                        GtkBorder       *border)
{
  GtkBorder tmp;
  GtkStateFlags state = gtk_style_context_get_state (style);

  gtk_style_context_get_border (style, state, border);
  gtk_style_context_get_padding (style, state, &tmp);

  border->left += tmp.left;
  border->top += tmp.top;
  border->right += tmp.right;
  border->bottom += tmp.bottom;
}

static void
get_min_size (GtkStyleContext *style,
              GtkRequisition  *requisition)
{
  gtk_style_context_get (style, gtk_style_context_get_state (style),
                         "min-width", &requisition->width,
                         "min-height", &requisition->height,
                         NULL);
}

static void
get_shadow_extents (GtkStyleContext *style,
                    GtkBorder       *border)
{
  GdkRectangle clip;

  gtk_render_background_get_clip (style, 0, 0, 0, 0, &clip);

  border->left = abs (clip.x);
  border->top = abs (clip.y);
  border->right = clip.width - border->left;
  border->bottom = clip.height - border->top;
}

static void
frame_layout_sync_with_style (MetaFrameLayout *layout,
                              MetaStyleInfo   *style_info,
                              gboolean         composited,
                              MetaFrameFlags   flags)
{
  GtkStyleContext *style;
  GtkBorder border;
  GtkRequisition requisition;
  int border_radius, max_radius;

  meta_style_info_set_flags (style_info, flags);

  style = meta_style_info_get_style (style_info, META_STYLE_ELEMENT_DECORATION);
  get_padding_and_border (style, &layout->gtk.frame_border);
  scale_border (&layout->gtk.frame_border, layout->title_scale);

  if (composited)
    {
      /* With compositing manager: margin is resize area */
      get_margin (style, &layout->invisible_resize_border);
      get_shadow_extents (style, &layout->gtk.shadow_border);

      gtk_style_context_get (style, gtk_style_context_get_state (style),
                             "border-radius", &border_radius,
                             NULL);
      /* GTK+ currently does not allow us to look up radii of individual
       * corners; however we don't clip the client area, so with the
       * current trend of using small/no visible frame borders, most
       * themes should work fine with this.
       */
      layout->top_left_corner_rounded_radius = border_radius;
      layout->top_right_corner_rounded_radius = border_radius;
      max_radius = MIN (layout->gtk.frame_border.bottom, layout->gtk.frame_border.left);
      layout->bottom_left_corner_rounded_radius = MAX (border_radius, max_radius);
      max_radius = MIN (layout->gtk.frame_border.bottom, layout->gtk.frame_border.left);
      layout->bottom_right_corner_rounded_radius = MAX (border_radius, max_radius);
    }
  else
    {
      /* Without compositing manager we can not have invisible border */
      layout->invisible_resize_border.top = 0;
      layout->invisible_resize_border.bottom = 0;
      layout->invisible_resize_border.left = 0;
      layout->invisible_resize_border.right = 0;

      layout->gtk.shadow_border.top = 0;
      layout->gtk.shadow_border.bottom = 0;
      layout->gtk.shadow_border.left = 0;
      layout->gtk.shadow_border.right = 0;

      layout->top_left_corner_rounded_radius = 0;
      layout->top_right_corner_rounded_radius = 0;
      layout->bottom_left_corner_rounded_radius = 0;
      layout->bottom_right_corner_rounded_radius = 0;

      /* Without compositing manager: margin is part of border */
      get_margin (style, &border);

      layout->gtk.frame_border.left += border.left;
      layout->gtk.frame_border.right += border.right;
      layout->gtk.frame_border.top += border.top;
      layout->gtk.frame_border.bottom += border.bottom;
    }

  if (layout->hide_buttons)
    layout->gtk.icon_size = 0;

  if (!layout->has_title && layout->hide_buttons)
    return; /* border-only - be done */

  style = meta_style_info_get_style (style_info, META_STYLE_ELEMENT_TITLEBAR);
  get_min_size (style, &layout->gtk.titlebar_min_size);
  get_padding_and_border (style, &layout->gtk.titlebar_border);
  scale_border (&layout->gtk.titlebar_border, layout->title_scale);

  style = meta_style_info_get_style (style_info, META_STYLE_ELEMENT_TITLE);
  get_margin (style, &layout->gtk.title_margin);
  scale_border (&layout->gtk.title_margin, layout->title_scale);

  style = meta_style_info_get_style (style_info, META_STYLE_ELEMENT_BUTTON);
  get_min_size (style, &layout->gtk.button_min_size);
  get_padding_and_border (style, &layout->button_border);
  scale_border (&layout->button_border, layout->title_scale);

  get_margin (style, &layout->gtk.button_margin);
  scale_border (&layout->gtk.button_margin, layout->title_scale);

  style = meta_style_info_get_style (style_info, META_STYLE_ELEMENT_IMAGE);
  get_min_size (style, &requisition);
  get_padding_and_border (style, &border);
  scale_border (&border, layout->title_scale);

  layout->button_border.left += border.left;
  layout->button_border.right += border.right;
  layout->button_border.top += border.top;
  layout->button_border.bottom += border.bottom;

  get_margin (style, &border);
  layout->button_border.left += border.left;
  layout->button_border.right += border.right;
  layout->button_border.top += border.top;
  layout->button_border.bottom += border.bottom;

  layout->gtk.button_min_size.width = MAX (layout->gtk.button_min_size.width,
                                           requisition.width);
  layout->gtk.button_min_size.height = MAX (layout->gtk.button_min_size.height,
                                            requisition.height);
}

static void
meta_theme_gtk_get_frame_borders (MetaThemeImpl    *impl,
                                  MetaFrameLayout  *layout,
                                  MetaStyleInfo    *style_info,
                                  gint              text_height,
                                  MetaFrameFlags    flags,
                                  MetaFrameType     type,
                                  MetaFrameBorders *borders)
{
  gboolean composited;
  gint scale;
  gint title_height;
  gint buttons_height;
  gint content_height;

  composited = meta_theme_impl_get_composited (impl);
  frame_layout_sync_with_style (layout, style_info, composited, flags);

  meta_frame_borders_clear (borders);

  /* For a full-screen window, we don't have any borders, visible or not. */
  if (flags & META_FRAME_FULLSCREEN)
    return;

  g_return_if_fail (layout != NULL);

  if (!layout->has_title)
    text_height = 0;

  /* Scale geometry for HiDPI, see comment in meta_theme_gtk_draw_frame () */
  scale = meta_theme_impl_get_scale (impl);

  title_height = layout->gtk.title_margin.top +
                 text_height / scale +
                 layout->gtk.title_margin.bottom;

  buttons_height = MAX ((gint) layout->gtk.icon_size, layout->gtk.button_min_size.height) +
                   layout->gtk.button_margin.top + layout->button_border.top +
                   layout->gtk.button_margin.bottom + layout->button_border.bottom;

  content_height = MAX (title_height, buttons_height);
  content_height = MAX (content_height, layout->gtk.titlebar_min_size.height) +
                   layout->gtk.titlebar_border.top +
                   layout->gtk.titlebar_border.bottom;

  borders->visible.top = layout->gtk.frame_border.top + content_height;
  borders->visible.left = layout->gtk.frame_border.left;
  borders->visible.right = layout->gtk.frame_border.right;
  borders->visible.bottom = layout->gtk.frame_border.bottom;

  borders->shadow = layout->gtk.shadow_border;

  if (flags & META_FRAME_ALLOWS_HORIZONTAL_RESIZE)
    {
      borders->resize.left = layout->invisible_resize_border.left;
      borders->resize.right = layout->invisible_resize_border.right;
    }

  if (flags & META_FRAME_ALLOWS_VERTICAL_RESIZE)
    {
      borders->resize.bottom = layout->invisible_resize_border.bottom;

      if (type != META_FRAME_TYPE_ATTACHED)
        borders->resize.top = layout->invisible_resize_border.top;
    }

  borders->invisible.left = MAX (borders->shadow.left, borders->resize.left);
  borders->invisible.right = MAX (borders->shadow.right, borders->resize.right);
  borders->invisible.bottom = MAX (borders->shadow.bottom, borders->resize.bottom);
  borders->invisible.top = MAX (borders->shadow.top, borders->resize.top);

  borders->total.left = borders->invisible.left + borders->visible.left;
  borders->total.right = borders->invisible.right + borders->visible.right;
  borders->total.bottom = borders->invisible.bottom + borders->visible.bottom;
  borders->total.top = borders->invisible.top + borders->visible.top;

  scale_border (&borders->visible, scale);
  scale_border (&borders->shadow, scale);
  scale_border (&borders->resize, scale);
  scale_border (&borders->invisible, scale);
  scale_border (&borders->total, scale);
}

static void
meta_theme_gtk_calc_geometry (MetaThemeImpl     *impl,
                              MetaFrameLayout   *layout,
                              MetaStyleInfo     *style_info,
                              gint               text_height,
                              MetaFrameFlags     flags,
                              gint               client_width,
                              gint               client_height,
                              MetaButtonLayout  *button_layout,
                              MetaFrameType      type,
                              MetaFrameGeometry *fgeom)
{
  MetaFrameBorders borders;
  int i, n_left, n_right, n_left_spacers, n_right_spacers;
  int x;
  int button_y;
  int title_right_edge;
  int width, height;
  int scale;
  int content_width, content_height;
  int button_width, button_height;
  int min_size_for_rounding;

  META_THEME_IMPL_GET_CLASS (impl)->get_frame_borders (impl, layout,
                                                       style_info, text_height,
                                                       flags, type, &borders);

  fgeom->borders = borders;

  width = client_width + borders.total.left + borders.total.right;

  height = borders.total.top + borders.total.bottom;
  if (!(flags & META_FRAME_SHADED))
    height += client_height;

  fgeom->width = width;
  fgeom->height = height;

  /* Scale geometry for HiDPI, see comment in meta_theme_gtk_draw_frame () */
  scale = meta_theme_impl_get_scale (impl);

  content_width = width -
                  borders.invisible.left - layout->gtk.frame_border.left * scale -
                  borders.invisible.right - layout->gtk.frame_border.right * scale;
  content_height = borders.visible.top - layout->gtk.frame_border.top * scale;

  button_width = MAX ((gint) layout->gtk.icon_size, layout->gtk.button_min_size.width) +
                 layout->button_border.left + layout->button_border.right;
  button_height = MAX ((gint) layout->gtk.icon_size, layout->gtk.button_min_size.height) +
                  layout->button_border.top + layout->button_border.bottom;

  button_width *= scale;
  button_height *= scale;

  n_left = 0;
  n_right = 0;
  n_left_spacers = 0;
  n_right_spacers = 0;

  if (!layout->hide_buttons)
    {
      MetaButton *button;

      for (i = 0; i < button_layout->n_left_buttons; i++)
        {
          button = &button_layout->left_buttons[i];
          button->visible = is_button_visible (button, flags);

          if (button->visible)
            {
              if (button->type != META_BUTTON_TYPE_SPACER)
                n_left++;
              else
                n_left_spacers++;
            }
        }

      for (i = 0; i < button_layout->n_right_buttons; i++)
        {
          button = &button_layout->right_buttons[i];
          button->visible = is_button_visible (button, flags);

          if (button->visible)
            {
              if (button->type != META_BUTTON_TYPE_SPACER)
                n_left++;
              else
                n_left_spacers++;
            }
        }
    }
  else
    {
      for (i = 0; i < button_layout->n_left_buttons; i++)
        button_layout->left_buttons[i].visible = FALSE;

      for (i = 0; i < button_layout->n_right_buttons; i++)
        button_layout->right_buttons[i].visible = FALSE;
    }

  /* Be sure buttons fit */
  while (n_left > 0 || n_right > 0)
    {
      int space_used_by_buttons;

      space_used_by_buttons = 0;

      space_used_by_buttons += layout->gtk.button_margin.left * scale * n_left;
      space_used_by_buttons += button_width * n_left;
      space_used_by_buttons += layout->gtk.button_margin.right * scale * n_left;
      space_used_by_buttons += (button_width * 0.75) * n_left_spacers;
      space_used_by_buttons += layout->gtk.titlebar_spacing * scale * MAX (n_left - 1, 0);

      space_used_by_buttons += layout->gtk.button_margin.left * scale * n_right;
      space_used_by_buttons += button_width * n_right;
      space_used_by_buttons += layout->gtk.button_margin.right * scale * n_right;
      space_used_by_buttons += (button_width * 0.75) * n_right_spacers;
      space_used_by_buttons += layout->gtk.titlebar_spacing * scale * MAX (n_right - 1, 0);

      if (space_used_by_buttons <= content_width)
        break; /* Everything fits, bail out */

      /* First try to remove separators */
      if (n_left_spacers > 0)
        {
          if (strip_button (button_layout->left_buttons,
                            button_layout->n_left_buttons,
                            META_BUTTON_TYPE_SPACER))
            {
              n_left_spacers--;
              continue;
            }
          else
            {
              g_assert_not_reached ();
            }
        }
      else if (n_right_spacers > 0)
        {
          if (strip_button (button_layout->right_buttons,
                            button_layout->n_right_buttons,
                            META_BUTTON_TYPE_SPACER))
            {
              n_right_spacers--;
              continue;
            }
          else
            {
              g_assert_not_reached ();
            }
        }

      /* Otherwise we need to shave out a button. Shave
       * above, stick, shade, min, max, close, then menu (menu is most useful);
       * prefer the default button locations.
       */
      if (strip_buttons (button_layout, &n_left, &n_right))
        {
          continue;
        }
      else
        {
          g_error ("Could not find a button to strip. n_left = %d n_right = %d",
                   n_left, n_right);
        }
    }

  /* center buttons vertically */
  button_y = borders.invisible.top + layout->gtk.frame_border.top * scale +
             (content_height - button_height) / 2;

  /* right edge of farthest-right button */
  x = width - borders.invisible.right - layout->gtk.frame_border.right * scale -
      layout->gtk.titlebar_border.right * scale;

  for (i = button_layout->n_right_buttons - 1; i >= 0; i--)
    {
      MetaButton *button;
      GdkRectangle rect;

      button = &button_layout->right_buttons[i];

      if (button->visible == FALSE)
        continue;

      /* if we go negative, leave the buttons we don't get to as 0 - width */
      if (x < 0)
        break;

      x -= layout->gtk.button_margin.right * scale;

      rect.y = button_y;
      rect.width = button_width;
      rect.height = button_height;

      if (button->type == META_BUTTON_TYPE_SPACER)
        {
          rect.x = x - button_width * 0.75;
          rect.width *= 0.75;
        }
      else
        {
          rect.x = x - button_width;
        }

      button->rect.visible = rect;
      button->rect.clickable = rect;

      if ((flags & META_FRAME_MAXIMIZED || flags & META_FRAME_TILED_RIGHT) &&
          i == button_layout->n_right_buttons - 1)
        {
          gint extra_width;
          gint extra_height;

          extra_width = layout->gtk.button_margin.right * scale +
                        layout->gtk.frame_border.right * scale +
                        layout->gtk.titlebar_border.right * scale;

          /* FIXME: */
          extra_height = 0;

          button->rect.clickable.y -= extra_height;
          button->rect.clickable.width += extra_width;
          button->rect.clickable.height += extra_height;
        }

      x = rect.x - layout->gtk.button_margin.left * scale;

      if (i > 0)
        x -= layout->gtk.titlebar_spacing * scale;
    }

  /* save right edge of titlebar for later use */
  title_right_edge = x;

  /* Now x changes to be position from the left and we go through
   * the left-side buttons
   */
  x = borders.invisible.left + layout->gtk.frame_border.left * scale +
      layout->gtk.titlebar_border.left * scale;

  for (i = 0; i < button_layout->n_left_buttons; i++)
    {
      MetaButton *button;
      GdkRectangle rect;

      button = &button_layout->left_buttons[i];

      if (button->visible == FALSE)
        continue;

      rect.x = x + layout->gtk.button_margin.left * scale;
      rect.y = button_y;
      rect.width = button_width;
      rect.height = button_height;

      if (button->type == META_BUTTON_TYPE_SPACER)
        rect.width *= 0.75;

      button->rect.visible = rect;
      button->rect.clickable = rect;

      if ((flags & META_FRAME_MAXIMIZED || flags & META_FRAME_TILED_LEFT) &&
          i == 0)
        {
          gint extra_width;
          gint extra_height;

          extra_width = layout->gtk.button_margin.left * scale +
                        layout->gtk.frame_border.left * scale +
                        layout->gtk.titlebar_border.left * scale;

          /* FIXME: */
          extra_height = 0;

          button->rect.clickable.x -= extra_width;
          button->rect.clickable.y -= extra_height;
          button->rect.clickable.width += extra_width;
          button->rect.clickable.height += extra_height;
        }

      x = rect.x + rect.width + layout->gtk.button_margin.right * scale;

      if (i < button_layout->n_left_buttons - 1)
        x += layout->gtk.titlebar_spacing * scale;
    }

  /* Center vertically in the available content area */
  fgeom->title_rect.x = x;
  fgeom->title_rect.y = borders.invisible.top + layout->gtk.frame_border.top * scale +
                        (content_height - text_height) / 2;
  fgeom->title_rect.width = title_right_edge - fgeom->title_rect.x;
  fgeom->title_rect.height = text_height;

  /* Nuke title if it won't fit */
  if (fgeom->title_rect.width < 0 ||
      fgeom->title_rect.height < 0)
    {
      fgeom->title_rect.width = 0;
      fgeom->title_rect.height = 0;
    }

  if (flags & META_FRAME_SHADED)
    min_size_for_rounding = 0;
  else
    min_size_for_rounding = 5 * scale;

  fgeom->top_left_corner_rounded_radius = 0;
  fgeom->top_right_corner_rounded_radius = 0;
  fgeom->bottom_left_corner_rounded_radius = 0;
  fgeom->bottom_right_corner_rounded_radius = 0;

  if (borders.visible.top + borders.visible.left >= min_size_for_rounding)
    fgeom->top_left_corner_rounded_radius = layout->top_left_corner_rounded_radius * scale;
  if (borders.visible.top + borders.visible.right >= min_size_for_rounding)
    fgeom->top_right_corner_rounded_radius = layout->top_right_corner_rounded_radius * scale;

  if (borders.visible.bottom + borders.visible.left >= min_size_for_rounding)
    fgeom->bottom_left_corner_rounded_radius = layout->bottom_left_corner_rounded_radius * scale;
  if (borders.visible.bottom + borders.visible.right >= min_size_for_rounding)
    fgeom->bottom_right_corner_rounded_radius = layout->bottom_right_corner_rounded_radius * scale;
}

static const char *
get_class_from_button_type (MetaButtonType type)
{
  if (type == META_BUTTON_TYPE_CLOSE)
    return "close";
  else if (type == META_BUTTON_TYPE_MAXIMIZE)
    return "maximize";
  else if (type == META_BUTTON_TYPE_MINIMIZE)
    return "minimize";

  return NULL;
}

static void
meta_theme_gtk_draw_frame (MetaThemeImpl           *impl,
                           MetaFrameStyle          *style,
                           MetaStyleInfo           *style_info,
                           cairo_t                 *cr,
                           const MetaFrameGeometry *fgeom,
                           PangoLayout             *title_layout,
                           MetaFrameFlags           flags,
                           const MetaButtonLayout  *button_layout,
                           GdkPixbuf               *mini_icon,
                           GdkPixbuf               *icon)
{
  gdouble scale;
  GtkStyleContext *context;
  GtkStateFlags state;
  MetaRectangleDouble visible_rect;
  MetaRectangleDouble titlebar_rect;
  const MetaFrameBorders *borders;
  gint side;

  /* We opt out of GTK+ HiDPI handling, so we have to do the scaling
   * ourselves; the nitty-gritty is a bit confusing, so here is an overview:
   *  - the values in MetaFrameLayout are always as they appear in the theme,
   *    i.e. unscaled
   *  - calculated values (borders, MetaFrameGeometry) include the scale - as
   *    the geometry is comprised of scaled decorations and the client size
   *    which we must not scale, we don't have another option
   *  - for drawing, we scale the canvas to have GTK+ render elements (borders,
   *    radii, ...) at the correct scale - as a result, we have to "unscale"
   *    the geometry again to not apply the scaling twice
   */
  scale = meta_theme_impl_get_scale (impl);
  cairo_scale (cr, scale, scale);

  borders = &fgeom->borders;

  visible_rect.x = borders->invisible.left / scale;
  visible_rect.y = borders->invisible.top / scale;
  visible_rect.width = (fgeom->width - borders->invisible.left - borders->invisible.right) / scale;
  visible_rect.height = (fgeom->height - borders->invisible.top - borders->invisible.bottom) / scale;

  meta_style_info_set_flags (style_info, flags);

  context = meta_style_info_get_style (style_info, META_STYLE_ELEMENT_DECORATION);
  gtk_render_background (context, cr,
                         visible_rect.x, visible_rect.y,
                         visible_rect.width, visible_rect.height);
  gtk_render_frame (context, cr,
                    visible_rect.x, visible_rect.y,
                    visible_rect.width, visible_rect.height);

  if (flags & META_FRAME_FULLSCREEN)
    return;

  titlebar_rect.x = visible_rect.x + borders->visible.left / scale;
  titlebar_rect.y = visible_rect.y + style->layout->gtk.frame_border.top;
  titlebar_rect.width = visible_rect.width - (borders->visible.left + borders->visible.right) / scale;
  titlebar_rect.height = (borders->visible.top / scale) - style->layout->gtk.frame_border.top;

  context = meta_style_info_get_style (style_info, META_STYLE_ELEMENT_TITLEBAR);
  gtk_render_background (context, cr,
                         titlebar_rect.x, titlebar_rect.y,
                         titlebar_rect.width, titlebar_rect.height);
  gtk_render_frame (context, cr,
                    titlebar_rect.x, titlebar_rect.y,
                    titlebar_rect.width, titlebar_rect.height);

  if (style->layout->has_title && title_layout)
    {
      PangoRectangle logical;
      gdouble text_width, x, y;

      pango_layout_set_width (title_layout, -1);
      pango_layout_get_pixel_extents (title_layout, NULL, &logical);

      text_width = MIN(fgeom->title_rect.width / scale, logical.width);

      if (text_width < logical.width)
        pango_layout_set_width (title_layout, PANGO_SCALE * text_width);

      /* Center within the frame if possible */
      x = titlebar_rect.x + (titlebar_rect.width - text_width) / 2;
      y = titlebar_rect.y + (titlebar_rect.height - logical.height) / 2;

      if (x < fgeom->title_rect.x / scale)
        x = fgeom->title_rect.x / scale;
      else if (x + text_width > (fgeom->title_rect.x + fgeom->title_rect.width) / scale)
        x = (fgeom->title_rect.x + fgeom->title_rect.width) / scale - text_width;

      context = meta_style_info_get_style (style_info, META_STYLE_ELEMENT_TITLE);
      gtk_render_layout (context, cr, x, y, title_layout);
    }

  context = meta_style_info_get_style (style_info, META_STYLE_ELEMENT_BUTTON);
  state = gtk_style_context_get_state (context);

  for (side = 0; side < 2; side++)
    {
      MetaButton *buttons;
      gint n_buttons;
      gint i;

      if (side == 0)
        {
          buttons = button_layout->left_buttons;
          n_buttons = button_layout->n_left_buttons;
        }
      else if (side == 1)
        {
          buttons = button_layout->right_buttons;
          n_buttons = button_layout->n_right_buttons;
        }
      else
        {
          g_assert_not_reached ();
        }

      for (i = 0; i < n_buttons; i++)
        {
          MetaButton *button;
          gdouble x;
          gdouble y;
          gdouble width;
          gdouble height;
          const gchar *button_class;
          const gchar *icon_name;
          GdkPixbuf *pixbuf;

          button = &buttons[i];

          x = button->rect.visible.x / scale;
          y = button->rect.visible.y / scale;
          width = button->rect.visible.width / scale;
          height = button->rect.visible.height / scale;

          if (!button->visible || button->type == META_BUTTON_TYPE_SPACER ||
              width <= 0 || height <= 0)
            {
              continue;
            }

          button_class = get_class_from_button_type (button->type);

          if (button_class)
            gtk_style_context_add_class (context, button_class);

          if (button->state == META_BUTTON_STATE_PRELIGHT)
            gtk_style_context_set_state (context, state | GTK_STATE_PRELIGHT);
          else if (button->state == META_BUTTON_STATE_PRESSED)
            gtk_style_context_set_state (context, state | GTK_STATE_ACTIVE);
          else
            gtk_style_context_set_state (context, state);

          cairo_save (cr);

          gtk_render_background (context, cr, x, y, width, height);
          gtk_render_frame (context, cr, x, y, width, height);

          icon_name = NULL;
          pixbuf = NULL;

          switch (button->type)
            {
              case META_BUTTON_TYPE_CLOSE:
                icon_name = "window-close-symbolic";
                break;
              case META_BUTTON_TYPE_MAXIMIZE:
                if (flags & META_FRAME_MAXIMIZED)
                  icon_name = "window-restore-symbolic";
                else
                  icon_name = "window-maximize-symbolic";
                break;
              case META_BUTTON_TYPE_MINIMIZE:
                icon_name = "window-minimize-symbolic";
                break;
              case META_BUTTON_TYPE_MENU:
                icon_name = "open-menu-symbolic";
                break;
              case META_BUTTON_TYPE_APPMENU:
                pixbuf = g_object_ref (mini_icon);
                break;
              case META_BUTTON_TYPE_SHADE:
              case META_BUTTON_TYPE_ABOVE:
              case META_BUTTON_TYPE_STICK:
              case META_BUTTON_TYPE_UNSHADE:
              case META_BUTTON_TYPE_UNABOVE:
              case META_BUTTON_TYPE_UNSTICK:
              case META_BUTTON_TYPE_SPACER:
              case META_BUTTON_TYPE_LAST:
              default:
                break;
            }

          if (icon_name)
            {
              GtkIconTheme *theme;
              GtkIconInfo *info;

              theme = gtk_icon_theme_get_default ();
              info = gtk_icon_theme_lookup_icon_for_scale (theme, icon_name,
                                                           style->layout->gtk.icon_size,
                                                           scale, 0);

              g_assert (pixbuf == NULL);
              pixbuf = gtk_icon_info_load_symbolic_for_context (info, context, NULL, NULL);
            }

          if (pixbuf)
            {
              gdouble pwidth;
              gdouble pheight;
              gdouble px;
              gdouble py;
              gdouble scale_x;
              gdouble scale_y;

              pwidth = gdk_pixbuf_get_width (pixbuf) / scale;
              pheight = gdk_pixbuf_get_height (pixbuf) / scale;
              px = x + (width - pwidth) / 2;
              py = y + (height - pheight) / 2;

              scale_x = pwidth / style->layout->gtk.icon_size / scale;
              scale_y = pheight / style->layout->gtk.icon_size / scale;

              cairo_translate (cr, px, py);
              cairo_scale (cr, scale_x, scale_y);
              gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
              cairo_paint (cr);

              g_object_unref (pixbuf);
            }

          cairo_restore (cr);

          if (button_class)
            gtk_style_context_remove_class (context, button_class);

          gtk_style_context_set_state (context, state);
        }
    }
}

static void
meta_theme_gtk_class_init (MetaThemeGtkClass *gtk_class)
{
  MetaThemeImplClass *impl_class;

  impl_class = META_THEME_IMPL_CLASS (gtk_class);

  impl_class->load = meta_theme_gtk_load;
  impl_class->get_frame_borders = meta_theme_gtk_get_frame_borders;
  impl_class->calc_geometry = meta_theme_gtk_calc_geometry;
  impl_class->draw_frame = meta_theme_gtk_draw_frame;
}

static void
meta_theme_gtk_init (MetaThemeGtk *gtk)
{
  MetaThemeImpl *impl;
  MetaFrameType type;

  impl = META_THEME_IMPL (gtk);

  for (type = 0; type < META_FRAME_TYPE_LAST; type++)
    {
      MetaFrameStyleSet *style_set;
      MetaFrameStyle *style;
      gint i;
      gint j;

      style_set = meta_frame_style_set_new (NULL);

      style = meta_frame_style_new (NULL);
      style->layout = meta_frame_layout_new ();

      switch (type)
        {
          case META_FRAME_TYPE_NORMAL:
          case META_FRAME_TYPE_DIALOG:
          case META_FRAME_TYPE_MODAL_DIALOG:
          case META_FRAME_TYPE_ATTACHED:
            break;

          case META_FRAME_TYPE_MENU:
          case META_FRAME_TYPE_UTILITY:
            style->layout->title_scale = PANGO_SCALE_SMALL;
            break;

          case META_FRAME_TYPE_BORDER:
            style->layout->has_title = FALSE;
            style->layout->hide_buttons = TRUE;
            break;

          case META_FRAME_TYPE_LAST:
          default:
            g_assert_not_reached ();
        }

      for (i = 0; i < META_FRAME_FOCUS_LAST; i++)
        {
          for (j = 0; j < META_FRAME_RESIZE_LAST; j++)
            {
              meta_frame_style_ref (style);
              style_set->normal_styles[j][i] = style;

              meta_frame_style_ref (style);
              style_set->shaded_styles[j][i] = style;
            }

          meta_frame_style_ref (style);
          style_set->maximized_styles[i] = style;

          meta_frame_style_ref (style);
          style_set->tiled_left_styles[i] = style;

          meta_frame_style_ref (style);
          style_set->tiled_right_styles[i] = style;

          meta_frame_style_ref (style);
          style_set->maximized_and_shaded_styles[i] = style;

          meta_frame_style_ref (style);
          style_set->tiled_left_and_shaded_styles[i] = style;

          meta_frame_style_ref (style);
          style_set->tiled_right_and_shaded_styles[i] = style;
        }

      meta_frame_style_unref (style);
      meta_theme_impl_add_style_set (impl, type, style_set);
    }
}