gtkwindowcontrols.c 20.9 KB
Newer Older
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * Copyright (c) 2020 Alexander Mikhaylenko <alexm@gnome.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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 Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"

#include "gtkwindowcontrols.h"

23
#include "gtkaccessible.h"
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
24
25
26
27
28
29
30
31
32
33
34
35
#include "gtkactionable.h"
#include "gtkboxlayout.h"
#include "gtkbutton.h"
#include "gtkenums.h"
#include "gtkicontheme.h"
#include "gtkimage.h"
#include "gtkintl.h"
#include "gtkprivate.h"
#include "gtktypebuiltins.h"
#include "gtkwindowprivate.h"

/**
Matthias Clasen's avatar
Matthias Clasen committed
36
 * GtkWindowControls:
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
37
 *
Matthias Clasen's avatar
Matthias Clasen committed
38
39
40
41
 * `GtkWindowControls` shows window frame controls.
 *
 * Typical window frame controls are minimize, maximize and close buttons,
 * and the window icon.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
42
 *
Matthias Clasen's avatar
Matthias Clasen committed
43
 * ![An example GtkWindowControls](windowcontrols.png)
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
44
 *
Matthias Clasen's avatar
Matthias Clasen committed
45
46
47
48
49
 * `GtkWindowControls` only displays start or end side of the controls (see
 * [property@Gtk.WindowControls:side]), so it's intended to be always used
 * in pair with another `GtkWindowControls` for the opposite side, for example:
 *
 * ```xml
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 * <object class="GtkBox">
 *   <child>
 *     <object class="GtkWindowControls">
 *       <property name="side">start</property>
 *     </object>
 *   </child>
 *
 *   ...
 *
 *   <child>
 *     <object class="GtkWindowControls">
 *       <property name="side">end</property>
 *     </object>
 *   </child>
 * </object>
Matthias Clasen's avatar
Matthias Clasen committed
65
 * ```
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
66
67
68
 *
 * # CSS nodes
 *
69
 * ```
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
70
71
72
73
74
 * windowcontrols
 * ├── [image.icon]
 * ├── [button.minimize]
 * ├── [button.maximize]
 * ╰── [button.close]
75
 * ```
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
76
 *
Matthias Clasen's avatar
Matthias Clasen committed
77
 * A `GtkWindowControls`' CSS node is called windowcontrols. It contains
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
78
79
 * subnodes corresponding to each title button. Which of the title buttons
 * exist and where they are placed exactly depends on the desktop environment
Matthias Clasen's avatar
Matthias Clasen committed
80
 * and [property@Gtk.WindowControls:decoration-layout] value.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
81
 *
Matthias Clasen's avatar
Matthias Clasen committed
82
83
 * When [property@Gtk.WindowControls:empty] is %TRUE, it gets the .empty
 * style class.
84
85
86
 *
 * # Accessibility
 *
Matthias Clasen's avatar
Matthias Clasen committed
87
 * `GtkWindowControls` uses the %GTK_ACCESSIBLE_ROLE_GROUP role.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
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
 */

struct _GtkWindowControls {
  GtkWidget parent_instance;

  GtkPackType side;
  char *decoration_layout;

  gboolean empty;
};

enum {
  PROP_0,
  PROP_SIDE,
  PROP_DECORATION_LAYOUT,
  PROP_EMPTY,
  LAST_PROP
};

static GParamSpec *props[LAST_PROP] = { NULL, };

#define WINDOW_ICON_SIZE 16

G_DEFINE_TYPE (GtkWindowControls, gtk_window_controls, GTK_TYPE_WIDGET)

static char *
get_layout (GtkWindowControls *self)
{
  GtkWidget *widget = GTK_WIDGET (self);
117
  GtkRoot *root;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
118
119
120
  char *layout_desc, *layout_half;
  char **tokens;

121
122
  root = gtk_widget_get_root (widget);
  if (!root || !GTK_IS_WINDOW (root))
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
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
    return NULL;

  if (self->decoration_layout)
    layout_desc = g_strdup (self->decoration_layout);
  else
    g_object_get (gtk_widget_get_settings (widget),
                  "gtk-decoration-layout", &layout_desc,
                  NULL);

  tokens = g_strsplit (layout_desc, ":", 2);

  switch (self->side)
    {
    case GTK_PACK_START:
      layout_half = g_strdup (tokens[0]);
      break;

    case GTK_PACK_END:
      layout_half = g_strdup (tokens[1]);
      break;

    default:
      g_assert_not_reached ();
    }

  g_free (layout_desc);
  g_strfreev (tokens);

  return layout_half;
}

static GdkPaintable *
get_default_icon (GtkWidget *widget)
{
  GdkDisplay *display = gtk_widget_get_display (widget);
  GtkIconPaintable *info;
  int scale = gtk_widget_get_scale_factor (widget);

  info = gtk_icon_theme_lookup_icon (gtk_icon_theme_get_for_display (display),
                                     gtk_window_get_default_icon_name (),
                                     NULL,
                                     WINDOW_ICON_SIZE,
                                     scale,
                                     gtk_widget_get_direction (widget),
                                     0);

  return GDK_PAINTABLE (info);
}

static gboolean
update_window_icon (GtkWindow *window,
                    GtkWidget *icon)
{
  GdkPaintable *paintable;

  if (window)
    paintable = gtk_window_get_icon_for_size (window, WINDOW_ICON_SIZE);
  else
    paintable = get_default_icon (icon);

  if (paintable)
    {
      gtk_image_set_from_paintable (GTK_IMAGE (icon), paintable);
      g_object_unref (paintable);
      gtk_widget_show (icon);

      return TRUE;
    }

  return FALSE;
}

static void
set_empty (GtkWindowControls *self,
           gboolean           empty)
{
  if (empty == self->empty)
    return;

  self->empty = empty;

  if (empty)
    gtk_widget_add_css_class (GTK_WIDGET (self), "empty");
  else
    gtk_widget_remove_css_class (GTK_WIDGET (self), "empty");

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_EMPTY]);
}

static void
clear_controls (GtkWindowControls *self)
{
  GtkWidget *child = gtk_widget_get_first_child (GTK_WIDGET (self));

  while (child)
    {
      GtkWidget *next = gtk_widget_get_next_sibling (child);

      gtk_widget_unparent (child);

      child = next;
    }
}

static void
update_window_buttons (GtkWindowControls *self)
{
  GtkWidget *widget = GTK_WIDGET (self);
  char *layout;
  char **tokens;
  int i;
  gboolean is_sovereign_window;
  gboolean maximized;
  gboolean resizable;
  gboolean deletable;
  gboolean empty = TRUE;
239
  GtkRoot *root;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
240
241
  GtkWindow *window = NULL;

242
243
  root = gtk_widget_get_root (widget);
  if (!root || !GTK_IS_WINDOW (root))
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
244
245
246
247
248
249
250
251
    {
      set_empty (self, TRUE);

      return;
    }

  clear_controls (self);

252
253
254
255
256
257
  window = GTK_WINDOW (root);
  is_sovereign_window = !gtk_window_get_modal (window) &&
                         gtk_window_get_transient_for (window) == NULL;
  maximized = gtk_window_is_maximized (window);
  resizable = gtk_window_get_resizable (window);
  deletable = gtk_window_get_deletable (window);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

  layout = get_layout (self);

  if (!layout)
    {
      set_empty (self, TRUE);

      return;
    }

  tokens = g_strsplit (layout, ",", -1);

  for (i = 0; tokens[i]; i++)
    {
      GtkWidget *button = NULL;
      GtkWidget *image = NULL;

      if (strcmp (tokens[i], "icon") == 0 &&
          is_sovereign_window)
        {
278
279
280
281
          /* The icon is not relevant for accessibility purposes */
          button = g_object_new (GTK_TYPE_IMAGE,
                                 "accessible-role", GTK_ACCESSIBLE_ROLE_PRESENTATION,
                                 NULL);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
          gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
          gtk_widget_add_css_class (button, "icon");

          if (!update_window_icon (window, button))
            {
              g_object_ref_sink (button);
              g_object_unref (button);
              button = NULL;
            }
        }
      else if (strcmp (tokens[i], "minimize") == 0 &&
               is_sovereign_window)
        {
          button = gtk_button_new ();
          gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
          gtk_widget_add_css_class (button, "minimize");
298
299
300
301
302
          /* The icon is not relevant for accessibility purposes */
          image = g_object_new (GTK_TYPE_IMAGE,
                                "accessible-role", GTK_ACCESSIBLE_ROLE_PRESENTATION,
                                "icon-name", "window-minimize-symbolic",
                                NULL);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
303
          g_object_set (image, "use-fallback", TRUE, NULL);
304
          gtk_button_set_child (GTK_BUTTON (button), image);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
305
306
307
          gtk_widget_set_can_focus (button, FALSE);
          gtk_actionable_set_action_name (GTK_ACTIONABLE (button),
                                          "window.minimize");
308
309
310
311
312
          gtk_accessible_update_property (GTK_ACCESSIBLE (button),
                                          GTK_ACCESSIBLE_PROPERTY_LABEL, _("Minimize"),
                                          GTK_ACCESSIBLE_PROPERTY_DESCRIPTION,
                                            _("Minimize the window"),
                                          -1);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
313
314
315
316
317
318
319
320
321
322
323
        }
      else if (strcmp (tokens[i], "maximize") == 0 &&
               resizable &&
               is_sovereign_window)
        {
          const char *icon_name;

          icon_name = maximized ? "window-restore-symbolic" : "window-maximize-symbolic";
          button = gtk_button_new ();
          gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
          gtk_widget_add_css_class (button, "maximize");
324
325
326
327
328
          /* The icon is not relevant for accessibility purposes */
          image = g_object_new (GTK_TYPE_IMAGE,
                                "accessible-role", GTK_ACCESSIBLE_ROLE_PRESENTATION,
                                "icon-name", icon_name,
                                NULL);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
329
          g_object_set (image, "use-fallback", TRUE, NULL);
330
          gtk_button_set_child (GTK_BUTTON (button), image);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
331
332
333
          gtk_widget_set_can_focus (button, FALSE);
          gtk_actionable_set_action_name (GTK_ACTIONABLE (button),
                                          "window.toggle-maximized");
334
335
336
337
338
          gtk_accessible_update_property (GTK_ACCESSIBLE (button),
                                          GTK_ACCESSIBLE_PROPERTY_LABEL, _("Maximize"),
                                          GTK_ACCESSIBLE_PROPERTY_DESCRIPTION,
                                            _("Maximize the window"),
                                          -1);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
339
340
341
342
343
344
        }
      else if (strcmp (tokens[i], "close") == 0 &&
               deletable)
        {
          button = gtk_button_new ();
          gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
345
346
347
348
349
          /* The icon is not relevant for accessibility purposes */
          image = g_object_new (GTK_TYPE_IMAGE,
                                "accessible-role", GTK_ACCESSIBLE_ROLE_PRESENTATION,
                                "icon-name", "window-close-symbolic",
                                NULL);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
350
351
          gtk_widget_add_css_class (button, "close");
          g_object_set (image, "use-fallback", TRUE, NULL);
352
          gtk_button_set_child (GTK_BUTTON (button), image);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
353
354
355
          gtk_widget_set_can_focus (button, FALSE);
          gtk_actionable_set_action_name (GTK_ACTIONABLE (button),
                                          "window.close");
356
357
358
359
360
          gtk_accessible_update_property (GTK_ACCESSIBLE (button),
                                          GTK_ACCESSIBLE_PROPERTY_LABEL, _("Close"),
                                          GTK_ACCESSIBLE_PROPERTY_DESCRIPTION,
                                            _("Close the window"),
                                          -1);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
        }

      if (button)
        {
          gtk_widget_set_parent (button, widget);
          empty = FALSE;
        }
    }
  g_free (layout);
  g_strfreev (tokens);

  set_empty (self, empty);
}

static void
window_notify_cb (GtkWindowControls *self,
                  GParamSpec        *pspec,
                  GtkWindow         *window)
{
  if (pspec->name == I_("deletable") ||
      pspec->name == I_("icon-name") ||
382
      pspec->name == I_("maximized") ||
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
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
      pspec->name == I_("modal") ||
      pspec->name == I_("resizable") ||
      pspec->name == I_("scale-factor") ||
      pspec->name == I_("transient-for"))
    update_window_buttons (self);
}

static void
gtk_window_controls_root (GtkWidget *widget)
{
  GtkSettings *settings;
  GtkWidget *root;

  GTK_WIDGET_CLASS (gtk_window_controls_parent_class)->root (widget);

  settings = gtk_widget_get_settings (widget);
  g_signal_connect_swapped (settings, "notify::gtk-decoration-layout",
                            G_CALLBACK (update_window_buttons), widget);

  root = GTK_WIDGET (gtk_widget_get_root (widget));

  if (GTK_IS_WINDOW (root))
    g_signal_connect_swapped (root, "notify",
                              G_CALLBACK (window_notify_cb), widget);

  update_window_buttons (GTK_WINDOW_CONTROLS (widget));
}

static void
gtk_window_controls_unroot (GtkWidget *widget)
{
  GtkSettings *settings;

  settings = gtk_widget_get_settings (widget);

  g_signal_handlers_disconnect_by_func (settings, update_window_buttons, widget);
  g_signal_handlers_disconnect_by_func (gtk_widget_get_root (widget), window_notify_cb, widget);

  GTK_WIDGET_CLASS (gtk_window_controls_parent_class)->unroot (widget);
}

static void
425
gtk_window_controls_dispose (GObject *object)
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
426
427
428
429
430
{
  GtkWindowControls *self = GTK_WINDOW_CONTROLS (object);

  clear_controls (self);

431
432
433
434
435
436
437
438
  G_OBJECT_CLASS (gtk_window_controls_parent_class)->dispose (object);
}

static void
gtk_window_controls_finalize (GObject *object)
{
  GtkWindowControls *self = GTK_WINDOW_CONTROLS (object);

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
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
  g_free (self->decoration_layout);

  G_OBJECT_CLASS (gtk_window_controls_parent_class)->finalize (object);
}

static void
gtk_window_controls_get_property (GObject    *object,
                                  guint       prop_id,
                                  GValue     *value,
                                  GParamSpec *pspec)
{
  GtkWindowControls *self = GTK_WINDOW_CONTROLS (object);

  switch (prop_id)
    {
    case PROP_SIDE:
      g_value_set_enum (value, gtk_window_controls_get_side (self));
      break;

    case PROP_DECORATION_LAYOUT:
      g_value_set_string (value, gtk_window_controls_get_decoration_layout (self));
      break;

    case PROP_EMPTY:
      g_value_set_boolean (value, gtk_window_controls_get_empty (self));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_window_controls_set_property (GObject      *object,
                                  guint         prop_id,
                                  const GValue *value,
                                  GParamSpec   *pspec)
{
  GtkWindowControls *self = GTK_WINDOW_CONTROLS (object);

  switch (prop_id)
    {
    case PROP_SIDE:
      gtk_window_controls_set_side (self, g_value_get_enum (value));
      break;

    case PROP_DECORATION_LAYOUT:
      gtk_window_controls_set_decoration_layout (self, g_value_get_string (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_window_controls_class_init (GtkWindowControlsClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

502
  object_class->dispose = gtk_window_controls_dispose;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
503
504
505
506
507
508
509
510
  object_class->finalize = gtk_window_controls_finalize;
  object_class->get_property = gtk_window_controls_get_property;
  object_class->set_property = gtk_window_controls_set_property;

  widget_class->root = gtk_window_controls_root;
  widget_class->unroot = gtk_window_controls_unroot;

  /**
Matthias Clasen's avatar
Matthias Clasen committed
511
   * GtkWindowControls:side: (attributes org.gtk.Property.get=gtk_window_controls_get_side org.gtk.Property.set=gtk_window_controls_set_side)
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
512
513
514
   *
   * Whether the widget shows start or end side of the decoration layout.
   *
Matthias Clasen's avatar
Matthias Clasen committed
515
   * See [property@Gtk.WindowControls:decoration_layout].
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
516
517
518
519
520
521
522
523
524
525
   */
  props[PROP_SIDE] =
      g_param_spec_enum ("side",
                         P_("Side"),
                         P_("Whether the widget shows start or end portion of the decoration layout"),
                         GTK_TYPE_PACK_TYPE,
                         GTK_PACK_START,
                         GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

  /**
Matthias Clasen's avatar
Matthias Clasen committed
526
   * GtkWindowControls:decoration-layout: (attributes org.gtk.Property.get=gtk_window_controls_get_decoration_layout org.gtk.Property.set=gtk_window_controls_set_decoration_layout)
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
527
   *
Matthias Clasen's avatar
Matthias Clasen committed
528
   * The decoration layout for window buttons.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
529
   *
Matthias Clasen's avatar
Matthias Clasen committed
530
531
   * If this property is not set, the
   * [property@Gtk.Settings:gtk-decoration-layout] setting is used.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
532
533
534
535
536
537
538
539
540
   */
  props[PROP_DECORATION_LAYOUT] =
      g_param_spec_string ("decoration-layout",
                           P_("Decoration Layout"),
                           P_("The layout for window decorations"),
                           NULL,
                           GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

  /**
Matthias Clasen's avatar
Matthias Clasen committed
541
   * GtkWindowControls:empty: (attributes org.gtk.Property.get=gtk_window_controls_get_empty)
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
542
543
544
545
546
547
548
549
550
551
552
553
554
555
   *
   * Whether the widget has any window buttons.
   */
  props[PROP_EMPTY] =
    g_param_spec_boolean ("empty",
                          P_("Empty"),
                          P_("Whether the widget has any window buttons"),
                          TRUE,
                          GTK_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (object_class, LAST_PROP, props);

  gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT);
  gtk_widget_class_set_css_name (widget_class, I_("windowcontrols"));
556
  gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_GROUP);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
}

static void
gtk_window_controls_init (GtkWindowControls *self)
{
  self->decoration_layout = NULL;
  self->side = GTK_PACK_START;
  self->empty = TRUE;

  gtk_widget_add_css_class (GTK_WIDGET (self), "empty");
  gtk_widget_add_css_class (GTK_WIDGET (self), "start");

  gtk_widget_set_can_focus (GTK_WIDGET (self), FALSE);
}

/**
 * gtk_window_controls_new:
 * @side: the side
 *
Matthias Clasen's avatar
Matthias Clasen committed
576
 * Creates a new `GtkWindowControls`.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
577
 *
Matthias Clasen's avatar
Matthias Clasen committed
578
 * Returns: a new `GtkWindowControls`.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
579
580
581
582
583
584
585
586
587
588
 **/
GtkWidget *
gtk_window_controls_new (GtkPackType side)
{
  return g_object_new (GTK_TYPE_WINDOW_CONTROLS,
                       "side", side,
                       NULL);
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
589
590
 * gtk_window_controls_get_side: (attributes org.gtk.Method.get_property=side)
 * @self: a `GtkWindowControls`
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
591
 *
Matthias Clasen's avatar
Matthias Clasen committed
592
 * Gets the side to which this `GtkWindowControls` instance belongs.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
593
594
595
596
597
598
599
600
601
602
603
604
 *
 * Returns: the side
 */
GtkPackType
gtk_window_controls_get_side (GtkWindowControls *self)
{
  g_return_val_if_fail (GTK_IS_WINDOW_CONTROLS (self), GTK_PACK_START);

  return self->side;
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
605
606
 * gtk_window_controls_set_side: (attributes org.gtk.Method.set_property=side)
 * @self: a `GtkWindowControls`
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
607
608
 * @side: a side
 *
Matthias Clasen's avatar
Matthias Clasen committed
609
 * Determines which part of decoration layout the `GtkWindowControls` uses.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
610
 *
Matthias Clasen's avatar
Matthias Clasen committed
611
 * See [property@Gtk.WindowControls:decoration-layout].
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
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
 */
void
gtk_window_controls_set_side (GtkWindowControls *self,
                              GtkPackType        side)
{
  g_return_if_fail (GTK_IS_WINDOW_CONTROLS (self));

  if (self->side == side)
    return;

  self->side = side;

  switch (side)
    {
    case GTK_PACK_START:
      gtk_widget_add_css_class (GTK_WIDGET (self), "start");
      gtk_widget_remove_css_class (GTK_WIDGET (self), "end");
      break;

    case GTK_PACK_END:
      gtk_widget_add_css_class (GTK_WIDGET (self), "end");
      gtk_widget_remove_css_class (GTK_WIDGET (self), "start");
      break;

    default:
      g_warning ("Unexpected side: %d", side);
      break;
    }

  update_window_buttons (self);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_SIDE]);
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
647
648
 * gtk_window_controls_get_decoration_layout: (attributes org.gtk.Method.get_property=decoration-layout)
 * @self: a `GtkWindowControls`
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
649
 *
Matthias Clasen's avatar
Matthias Clasen committed
650
 * Gets the decoration layout of this `GtkWindowControls`.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
651
 *
652
 * Returns: (nullable): the decoration layout or %NULL if it is unset
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
653
654
655
656
657
658
659
660
661
662
 */
const char *
gtk_window_controls_get_decoration_layout (GtkWindowControls *self)
{
  g_return_val_if_fail (GTK_IS_WINDOW_CONTROLS (self), NULL);

  return self->decoration_layout;
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
663
664
 * gtk_window_controls_set_decoration_layout: (attributes org.gtk.Method.set_property=decoration-layout)
 * @self: a `GtkWindowControls`
Matthias Clasen's avatar
Matthias Clasen committed
665
 * @layout: (nullable): a decoration layout, or %NULL to unset the layout
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
666
 *
Matthias Clasen's avatar
Matthias Clasen committed
667
668
669
670
 * Sets the decoration layout for the title buttons.
 *
 * This overrides the [property@Gtk.Settings:gtk-decoration-layout]
 * setting.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
671
672
673
674
675
676
677
678
679
 *
 * The format of the string is button names, separated by commas.
 * A colon separates the buttons that should appear on the left
 * from those on the right. Recognized button names are minimize,
 * maximize, close and icon (the window icon).
 *
 * For example, “icon:minimize,maximize,close” specifies a icon
 * on the left, and minimize, maximize and close buttons on the right.
 *
Matthias Clasen's avatar
Matthias Clasen committed
680
681
 * If [property@Gtk.WindowControls:side] value is @GTK_PACK_START, @self
 * will display the part before the colon, otherwise after that.
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
 */
void
gtk_window_controls_set_decoration_layout (GtkWindowControls *self,
                                           const char        *layout)
{
  g_return_if_fail (GTK_IS_WINDOW_CONTROLS (self));

  g_free (self->decoration_layout);
  self->decoration_layout = g_strdup (layout);

  update_window_buttons (self);

  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_DECORATION_LAYOUT]);
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
698
699
 * gtk_window_controls_get_empty: (attributes org.gtk.Method.get_property=empty)
 * @self: a `GtkWindowControls`
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
700
701
702
703
704
705
706
707
708
709
710
711
 *
 * Gets whether the widget has any window buttons.
 *
 * Returns: %TRUE if the widget has window buttons, otherwise %FALSE
 */
gboolean
gtk_window_controls_get_empty (GtkWindowControls *self)
{
  g_return_val_if_fail (GTK_IS_WINDOW_CONTROLS (self), FALSE);

  return self->empty;
}