adw-message-dialog.c 69.1 KB
Newer Older
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
1
2
3
4
5
6
7
8
9
10
11
/*
 * Copyright (C) 2022 Purism SPC
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * Author: Alexander Mikhaylenko <alexander.mikhaylenko@puri.sm>
 */

#include "config.h"
#include "adw-message-dialog.h"

12
#include "adw-gizmo-private.h"
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
13
#include "adw-gtkbuilder-utils-private.h"
14
#include "adw-widget-utils-private.h"
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
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

/**
 * AdwMessageDialog:
 *
 * A dialog presenting a message or a question.
 *
 * <picture>
 *   <source srcset="message-dialog-dark.png" media="(prefers-color-scheme: dark)">
 *   <img src="message-dialog.png" alt="message-dialog">
 * </picture>
 *
 * Message dialogs have a heading, a body, an optional child widget, and one or
 * multiple responses, each presented as a button.
 *
 * Each response has a unique string ID, and a button label. Additionally, each
 * response can be enabled or disabled, and can have a suggested or destructive
 * appearance.
 *
 * When one of the responses is activated, or the dialog is closed, the
 * [signal@MessageDialog::response] signal will be emitted. This signal is
 * detailed, and the detail, as well as the `response` parameter will be set to
 * the ID of the activated response, or to the value of the
 * [property@MessageDialog:close-response] property if the dialog had been
 * closed without activating any of the responses.
 *
 * Response buttons can be presented horizontally or vertically depending on
 * available space.
 *
 * When a response is activated, `AdwMessageDialog` is closed automatically.
 *
 * An example of using a message dialog:
 *
 * ```c
 * GtkWidget *dialog;
 *
 * dialog = adw_message_dialog_new (parent, _("Replace File?"), NULL);
 *
 * adw_message_dialog_format_body (ADW_MESSAGE_DIALOG (dialog),
 *                                 _("A file named “%s” already exists. Do you want to replace it?"),
 *                                 filename);
 *
 * adw_message_dialog_add_responses (ADW_MESSAGE_DIALOG (dialog),
 *                                   "cancel",  _("_Cancel"),
 *                                   "replace", _("_Replace"),
 *                                   NULL);
 *
 * adw_message_dialog_set_response_appearance (ADW_MESSAGE_DIALOG (dialog), "replace", ADW_RESPONSE_DESTRUCTIVE);
 *
 * adw_message_dialog_set_default_response (ADW_MESSAGE_DIALOG (dialog), "cancel");
 * adw_message_dialog_set_close_response (ADW_MESSAGE_DIALOG (dialog), "cancel");
 *
 * g_signal_connect (dialog, "response", G_CALLBACK (response_cb), self);
 *
 * gtk_window_present (GTK_WINDOW (dialog));
 * ```
 *
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
 * ## Async API
 *
 * `AdwMessageDialog` can also be used via the [method@MessageDialog.choose]
 * method. This API follows the GIO async pattern, and the result can be
 * obtained by calling [method@MessageDialog.choose_finish], for example:
 *
 * ```c
 * static void
 * dialog_cb (AdwMessageDialog *dialog,
 *            GAsyncResult     *result,
 *            MyWindow         *self)
 * {
 *   const char *response = adw_message_dialog_choose_finish (dialog, result);
 *
 *   ...
 * }
 *
 * static void
 * show_dialog (MyWindow *self)
 * {
 *   GtkWidget *dialog;
 *
 *   dialog = adw_message_dialog_new (GTK_WINDOW (self), _("Replace File?"), NULL);
 *
 *   adw_message_dialog_format_body (ADW_MESSAGE_DIALOG (dialog),
 *                                   _("A file named “%s” already exists. Do you want to replace it?"),
 *                                   filename);
 *
 *   adw_message_dialog_add_responses (ADW_MESSAGE_DIALOG (dialog),
 *                                     "cancel",  _("_Cancel"),
 *                                     "replace", _("_Replace"),
 *                                     NULL);
 *
 *   adw_message_dialog_set_response_appearance (ADW_MESSAGE_DIALOG (dialog), "replace", ADW_RESPONSE_DESTRUCTIVE);
 *
 *   adw_message_dialog_set_default_response (ADW_MESSAGE_DIALOG (dialog), "cancel");
 *   adw_message_dialog_set_close_response (ADW_MESSAGE_DIALOG (dialog), "cancel");
 *
 *   adw_message_dialog_choose (ADW_MESSAGE_DIALOG (dialog), NULL, (GAsyncReadyCallback) dialog_cb, self);
 * }
 * ```
 *
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
 * ## AdwMessageDialog as GtkBuildable
 *
 * `AdwMessageDialog` supports adding responses in UI definitions by via the
 * `<responses>` element that may contain multiple `<response>` elements, each
 * respresenting a response.
 *
 * Each of the `<response>` elements must have the `id` attribute specifying the
 * response ID. The contents of the element are used as the response label.
 *
 * Response labels can be translated with the usual `translatable`, `context`
 * and `comments` attributes.
 *
 * The `<response>` elements can also have `enabled` and/or `appearance`
 * attributes. See [method@MessageDialog.set_response_enabled] and
 * [method@MessageDialog.set_response_appearance] for details.
 *
 * Example of an `AdwMessageDialog` UI definition:
 *
 * ```xml
 * <object class="AdwMessageDialog" id="dialog">
 *   <property name="heading" translatable="yes">Save Changes?</property>
 *   <property name="body" translatable="yes">Open documents contain unsaved changes. Changes which are not saved will be permanently lost.</property>
 *   <property name="default-response">save</property>
 *   <property name="close-response">cancel</property>
137
 *   <signal name="response" handler="response_cb"/>
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
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
 *   <responses>
 *     <response id="cancel" translatable="yes">_Cancel</response>
 *     <response id="discard" translatable="yes" appearance="destructive">_Discard</response>
 *     <response id="save" translatable="yes" appearance="suggested" enabled="false">_Save</response>
 *   </responses>
 * </object>
 * ```
 *
 * ## Accessibility
 *
 * `AdwMessageDialog` uses the `GTK_ACCESSIBLE_ROLE_DIALOG` role.
 *
 * Since: 1.2
 */

/**
 * AdwResponseAppearance:
 * @ADW_RESPONSE_DEFAULT: the default appearance.
 * @ADW_RESPONSE_SUGGESTED: used to denote important responses such as the
 *     affirmative action.
 * @ADW_RESPONSE_DESTRUCTIVE: used to draw attention to the potentially damaging
 *     consequences of using the response. This appearance acts as a warning to
 *     the user.
 *
 * Describes the possible styles of [class@MessageDialog] response buttons.
 *
 * See [method@MessageDialog.set_response_appearance].
 *
 * Since: 1.2
 */

#define DIALOG_MARGIN 30
170
171
#define DIALOG_MAX_WIDTH 550
#define DIALOG_MIN_WIDTH 300
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
172
173
174
175
176
177
178
179

typedef struct {
  AdwMessageDialog *dialog;
  GQuark id;
  char *label;
  AdwResponseAppearance appearance;
  gboolean enabled;

180
181
  GtkWidget *button;
  GtkWidget *separator;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
182
183
184
185
186
187
188
} ResponseInfo;

typedef struct
{
  GtkWidget *heading_label;
  GtkWidget *body_label;
  GtkBox *message_area;
189
  GtkWidget *response_area;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
190
191
192
193
194
195
196
197

  char *heading;
  gboolean heading_use_markup;
  char *body;
  gboolean body_use_markup;
  GtkWidget *child;

  GList *responses;
198
  GHashTable *id_to_response;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
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
  GQuark default_response;
  GQuark close_response;

  gboolean block_close_response;

  GtkWindow *parent_window;
  int parent_width;
  int parent_height;
} AdwMessageDialogPrivate;

static void adw_message_dialog_buildable_init (GtkBuildableIface *iface);

G_DEFINE_TYPE_WITH_CODE (AdwMessageDialog, adw_message_dialog, GTK_TYPE_WINDOW,
                         G_ADD_PRIVATE (AdwMessageDialog)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, adw_message_dialog_buildable_init))

static GtkBuildableIface *parent_buildable_iface;

enum {
  PROP_0,
  PROP_HEADING,
  PROP_HEADING_USE_MARKUP,
  PROP_BODY,
  PROP_BODY_USE_MARKUP,
  PROP_EXTRA_CHILD,
  PROP_DEFAULT_RESPONSE,
  PROP_CLOSE_RESPONSE,
  LAST_PROP,
};

static GParamSpec *props[LAST_PROP];

enum {
  SIGNAL_RESPONSE,
  SIGNAL_LAST_SIGNAL,
};
static guint signals[SIGNAL_LAST_SIGNAL];

static void
response_info_free (ResponseInfo *info)
{
  g_free (info->label);
  g_free (info);
}

static inline ResponseInfo *
find_response (AdwMessageDialog *self,
               const char       *id)
{
248
249
250
  AdwMessageDialogPrivate *priv = adw_message_dialog_get_instance_private (self);

  return g_hash_table_lookup (priv->id_to_response, id);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
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
}

static void
parent_size_cb (AdwMessageDialog *self)
{
  AdwMessageDialogPrivate *priv = adw_message_dialog_get_instance_private (self);
  int w = gtk_widget_get_allocated_width (GTK_WIDGET (priv->parent_window));
  int h = gtk_widget_get_allocated_height (GTK_WIDGET (priv->parent_window));

  if (w == priv->parent_width && h == priv->parent_height)
      return;

  priv->parent_width = w;
  priv->parent_height = h;
  gtk_widget_queue_resize (GTK_WIDGET (self));
}

static void
parent_realize_cb (AdwMessageDialog *self)
{
  AdwMessageDialogPrivate *priv = adw_message_dialog_get_instance_private (self);
  GdkSurface *surface;

  g_assert (GTK_IS_NATIVE (priv->parent_window));

  surface = gtk_native_get_surface (GTK_NATIVE (priv->parent_window));

  g_signal_connect_swapped (surface, "compute-size",
                            G_CALLBACK (parent_size_cb), self);
  g_signal_connect_swapped (surface, "notify::width",
                            G_CALLBACK (parent_size_cb), self);
  g_signal_connect_swapped (surface, "notify::height",
                            G_CALLBACK (parent_size_cb), self);

  parent_size_cb (self);
}

static void
parent_unrealize_cb (AdwMessageDialog *self)
{
  AdwMessageDialogPrivate *priv = adw_message_dialog_get_instance_private (self);
  GdkSurface *surface;

  g_assert (GTK_IS_NATIVE (priv->parent_window));

  surface = gtk_native_get_surface (GTK_NATIVE (priv->parent_window));

  g_signal_handlers_disconnect_by_func (surface,
                                        G_CALLBACK (parent_size_cb),
                                        self);

  priv->parent_width = -1;
  priv->parent_height = -1;
}

306
307
308
309
310
311
312
313
314
315
static void
parent_window_notify_cb (AdwMessageDialog *self)
{
  AdwMessageDialogPrivate *priv = adw_message_dialog_get_instance_private (self);

  priv->parent_window = NULL;
  priv->parent_width = -1;
  priv->parent_height = -1;
}

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
static void
set_parent (AdwMessageDialog *self,
            GtkWindow        *parent)
{
  AdwMessageDialogPrivate *priv = adw_message_dialog_get_instance_private (self);

  if (priv->parent_window == parent)
    return;

  if (priv->parent_window) {
    g_signal_handlers_disconnect_by_func (priv->parent_window,
                                          G_CALLBACK (parent_realize_cb),
                                          self);
    g_signal_handlers_disconnect_by_func (priv->parent_window,
                                          G_CALLBACK (parent_unrealize_cb),
                                          self);

    if (gtk_widget_get_realized (GTK_WIDGET (priv->parent_window)))
      parent_unrealize_cb (self);
335
336
337
338

    g_object_weak_unref (G_OBJECT (priv->parent_window),
                         (GWeakNotify) parent_window_notify_cb,
                         self);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
339
340
341
342
343
  }

  priv->parent_window = parent;

  if (priv->parent_window) {
344
345
346
347
    g_object_weak_ref (G_OBJECT (priv->parent_window),
                       (GWeakNotify) parent_window_notify_cb,
                       self);

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
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
    if (gtk_widget_get_realized (GTK_WIDGET (priv->parent_window)))
      parent_realize_cb (self);

    g_signal_connect_swapped (priv->parent_window, "realize",
                              G_CALLBACK (parent_realize_cb), self);
    g_signal_connect_swapped (priv->parent_window, "unrealize",
                              G_CALLBACK (parent_unrealize_cb), self);
  }
}

static void
parent_changed_cb (AdwMessageDialog *self)
{
  GtkWindow *transient_for = gtk_window_get_transient_for (GTK_WINDOW (self));

  set_parent (self, transient_for);
}

static void
button_clicked_cb (ResponseInfo *info)
{
  AdwMessageDialog *self = info->dialog;
  AdwMessageDialogPrivate *priv = adw_message_dialog_get_instance_private (self);

  g_object_ref (self);
  priv->block_close_response = TRUE;

  gtk_window_close (GTK_WINDOW (self));
  g_signal_emit (self, signals[SIGNAL_RESPONSE], info->id, g_quark_to_string (info->id));

  priv->block_close_response = FALSE;
  g_object_unref (self);
}

static GtkWidget *
create_response_button (AdwMessageDialog *self,
                        ResponseInfo     *info)
{
  GtkWidget *button = gtk_button_new_with_mnemonic (info->label);

  gtk_widget_add_css_class (button, "flat");

  switch (info->appearance) {
  case ADW_RESPONSE_SUGGESTED:
    gtk_widget_add_css_class (button, "suggested");
    break;
  case ADW_RESPONSE_DESTRUCTIVE:
    gtk_widget_add_css_class (button, "destructive");
    break;
  case ADW_RESPONSE_DEFAULT:
  default:
    break;
  }

  gtk_widget_set_sensitive (button, info->enabled);

  g_signal_connect_swapped (button, "clicked", G_CALLBACK (button_clicked_cb), info);

  return button;
}

static void
update_window_title (AdwMessageDialog *self)
{
  AdwMessageDialogPrivate *priv = adw_message_dialog_get_instance_private (self);

  if (priv->heading_use_markup) {
    char *heading = NULL;
    GError *error = NULL;

    pango_parse_markup (priv->heading, -1, 0, NULL, &heading, NULL, &error);

    if (error) {
      g_critical ("Couldn't parse markup: %s", error->message);
      g_clear_error (&error);

      heading = g_strdup (priv->heading);
    }

    gtk_window_set_title (GTK_WINDOW (self), heading);

    g_free (heading);
  } else {
    gtk_window_set_title (GTK_WINDOW (self), priv->heading);
  }
}

static gboolean
adw_message_dialog_close_request (GtkWindow *window)
{
  AdwMessageDialog *self = ADW_MESSAGE_DIALOG (window);
  AdwMessageDialogPrivate *priv = adw_message_dialog_get_instance_private (self);

  if (!priv->block_close_response)
    g_signal_emit (self, signals[SIGNAL_RESPONSE],
                   priv->close_response,
                   g_quark_to_string (priv->close_response));

  return GTK_WINDOW_CLASS (adw_message_dialog_parent_class)->close_request (window);
}

static void
adw_message_dialog_map (GtkWidget *widget)
{
  AdwMessageDialog *self = ADW_MESSAGE_DIALOG (widget);
  AdwMessageDialogPrivate *priv = adw_message_dialog_get_instance_private (self);
  GtkWidget *focus, *default_widget;

  if (gtk_window_get_transient_for (GTK_WINDOW (self)) == NULL)
    g_message ("AdwMessageDialog mapped without a transient parent. This is discouraged.");

  GTK_WIDGET_CLASS (adw_message_dialog_parent_class)->map (widget);

  /* The rest of the function was copied from gtkdialog.c */
  focus = gtk_window_get_focus (GTK_WINDOW (self));
  if (!focus) {
    GtkWidget *first_focus = NULL;
    GList *l;

    do {
      g_signal_emit_by_name (self, "move_focus", GTK_DIR_TAB_FORWARD);

      focus = gtk_window_get_focus (GTK_WINDOW (self));
      if (GTK_IS_LABEL (focus) &&
          !gtk_label_get_current_uri (GTK_LABEL (focus)))
        gtk_label_select_region (GTK_LABEL (focus), 0, 0);

      if (first_focus == NULL)
        first_focus = focus;
      else if (first_focus == focus)
        break;

      if (!GTK_IS_LABEL (focus))
        break;
    } while (TRUE);

    default_widget = gtk_window_get_default_widget (GTK_WINDOW (self));
    for (l = priv->responses; l; l = l->next) {
      ResponseInfo *response = l->data;

488
489
      if ((focus == NULL || response->button == focus) &&
           response->button != default_widget &&
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
490
491
492
493
           default_widget) {
        gtk_widget_grab_focus (default_widget);
        break;
      }
494
495
496
    }
  }
}
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
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
static void
measure_responses_do (AdwMessageDialog *self,
                      gboolean          compact,
                      GtkOrientation    orientation,
                      int              *minimum,
                      int              *natural)
{
  AdwMessageDialogPrivate *priv = adw_message_dialog_get_instance_private (self);
  GList *l;
  int min = 0, nat = 0;
  int button_min = 0, button_nat = 0;
  int n_buttons = 0;
  gboolean horiz = (orientation == GTK_ORIENTATION_HORIZONTAL);

  for (l = priv->responses; l; l = l->next) {
    ResponseInfo *response = l->data;
    int child_min, child_nat;

    gtk_widget_measure (response->button, orientation, -1,
                        &child_min, &child_nat, NULL, NULL);

    if (horiz == compact) {
      min = MAX (min, child_min);
      nat = MAX (nat, child_min);
    } else if (horiz) {
      button_min = MAX (button_min, child_min);
      button_nat = MAX (button_nat, child_min);
      n_buttons++;
    } else {
      min += child_min;
      nat += child_nat;
    }

    if (response->separator) {
      gtk_widget_measure (response->separator, orientation, -1,
                          &child_min, &child_nat, NULL, NULL);

    if (horiz == compact) {
        min = MAX (min, child_min);
        nat = MAX (nat, child_min);
      } else {
        min += child_min;
        nat += child_nat;
      }
    }
  }

  if (horiz && !compact) {
    min += button_min * n_buttons;
    nat += button_nat * n_buttons;
  }

  if (minimum)
    *minimum = min;
  if (natural)
    *natural = nat;
}

static GtkSizeRequestMode
get_responses_request_mode (GtkWidget *widget)
{
  return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
}

static void
measure_responses (GtkWidget      *widget,
                   GtkOrientation  orientation,
                   int             for_size,
                   int            *minimum,
                   int            *natural,
                   int            *minimum_baseline,
                   int            *natural_baseline)
{
  AdwMessageDialog *self = ADW_MESSAGE_DIALOG (gtk_widget_get_root (widget));

  if (orientation == GTK_ORIENTATION_HORIZONTAL) {
    measure_responses_do (self, TRUE, orientation, minimum, NULL);
    measure_responses_do (self, FALSE, orientation, NULL, natural);
  } else {
    int wide_min = 0;

    if (for_size >= 0)
      measure_responses_do (self, FALSE, GTK_ORIENTATION_HORIZONTAL, &wide_min, NULL);

    measure_responses_do (self, for_size >= 0 && for_size < wide_min,
                          orientation, minimum, natural);
  }

  if (minimum_baseline)
    *minimum_baseline = -1;
  if (natural_baseline)
    *natural_baseline = -1;
}

static void
allocate_responses (GtkWidget *widget,
                    int        width,
                    int        height,
                    int        baseline)
{
  AdwMessageDialog *self = ADW_MESSAGE_DIALOG (gtk_widget_get_root (widget));
  AdwMessageDialogPrivate *priv = adw_message_dialog_get_instance_private (self);
  gboolean compact;
  int wide_min;

  measure_responses_do (self, FALSE, GTK_ORIENTATION_HORIZONTAL, &wide_min, NULL);

  compact = wide_min > width;

  if (compact)
    gtk_widget_add_css_class (widget, "compact");
  else
    gtk_widget_remove_css_class (widget, "compact");

  if (compact) {
    int pos = height;
    GList *l;

    for (l = priv->responses; l; l = l->next) {
      ResponseInfo *response = l->data;
      int child_height;

      if (response->separator) {
        gtk_widget_measure (response->separator, GTK_ORIENTATION_VERTICAL, -1,
                            &child_height, NULL, NULL, NULL);

        pos -= child_height;

        gtk_widget_allocate (response->separator, width, child_height, -1,
                             gsk_transform_translate (NULL, &GRAPHENE_POINT_INIT (0, pos)));
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
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

      gtk_widget_measure (response->button, GTK_ORIENTATION_VERTICAL, -1,
                          &child_height, NULL, NULL, NULL);

      pos -= child_height;

      gtk_widget_allocate (response->button, width, child_height, -1,
                           gsk_transform_translate (NULL, &GRAPHENE_POINT_INIT (0, pos)));
    }
  } else {
    gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
    int pos = is_rtl ? width : 0;
    int n_buttons = g_list_length (priv->responses);
    int total_width = width;
    int button_width;
    GList *l;

    for (l = priv->responses; l; l = l->next) {
      ResponseInfo *response = l->data;
      int separator_width;

      if (!response->separator)
        continue;

      gtk_widget_measure (response->separator, GTK_ORIENTATION_HORIZONTAL, -1,
                          &separator_width, NULL, NULL, NULL);

      total_width -= separator_width;
    }

    button_width = (int) ceil ((double) total_width / n_buttons);

    for (l = priv->responses; l; l = l->next) {
      ResponseInfo *response = l->data;

      if (response->separator) {
        int separator_width;

        gtk_widget_measure (response->separator, GTK_ORIENTATION_HORIZONTAL, -1,
                            &separator_width, NULL, NULL, NULL);

        if (is_rtl)
          pos -= separator_width;

        gtk_widget_allocate (response->separator, separator_width, height, -1,
                             gsk_transform_translate (NULL, &GRAPHENE_POINT_INIT (pos, 0)));

        if (!is_rtl)
          pos += separator_width;
      }

      button_width = MIN (button_width, total_width);

      total_width -= button_width;

      if (is_rtl)
        pos -= button_width;

      gtk_widget_allocate (response->button, button_width, height, -1,
                           gsk_transform_translate (NULL, &GRAPHENE_POINT_INIT (pos, 0)));

      if (!is_rtl)
        pos += button_width;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
    }
  }
}

static void
adw_message_dialog_measure (GtkWidget      *widget,
                            GtkOrientation  orientation,
                            int             for_size,
                            int            *min,
                            int            *nat,
                            int            *min_baseline,
                            int            *nat_baseline)
{
  AdwMessageDialog *self = ADW_MESSAGE_DIALOG (widget);
  AdwMessageDialogPrivate *priv = adw_message_dialog_get_instance_private (self);
707
  int max_size, min_size, base_nat;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
708
709
710
711

  GTK_WIDGET_CLASS (adw_message_dialog_parent_class)->measure (widget,
                                                               orientation,
                                                               for_size,
712
                                                               &min_size,
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
713
714
715
                                                               &base_nat,
                                                               NULL, NULL);

716
717
  if (orientation == GTK_ORIENTATION_HORIZONTAL) {
    int wide_min, narrow_nat;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
718

719
720
    min_size = MAX (min_size, DIALOG_MIN_WIDTH);

721
722
723
724
725
726
    if (priv->parent_window) {
      max_size = priv->parent_width - DIALOG_MARGIN * 2;
      max_size = MIN (max_size, DIALOG_MAX_WIDTH);
    } else {
      max_size = DIALOG_MAX_WIDTH;
    }
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
727

728
729
    measure_responses_do (self, FALSE, GTK_ORIENTATION_HORIZONTAL, &wide_min, NULL);
    measure_responses_do (self, TRUE, GTK_ORIENTATION_HORIZONTAL, NULL, &narrow_nat);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
730

731
732
733
734
735
736
    narrow_nat = MAX (narrow_nat, DIALOG_MIN_WIDTH);

    if (max_size < wide_min)
      max_size = MIN (max_size, narrow_nat);
  } else {
    max_size = priv->parent_height - DIALOG_MARGIN * 2;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
737
738
  }

739
  max_size = MAX (min_size, max_size);
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
740

741
  if (min)
742
    *min = min_size;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
743
  if (nat)
744
    *nat = CLAMP (base_nat, min_size, max_size);
745
746
747
748
  if (min_baseline)
    *min_baseline = -1;
  if (nat_baseline)
    *nat_baseline = -1;
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
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
}

static void
adw_message_dialog_get_property (GObject    *object,
                                 guint       prop_id,
                                 GValue     *value,
                                 GParamSpec *pspec)
{
  AdwMessageDialog *self = ADW_MESSAGE_DIALOG (object);

  switch (prop_id) {
  case PROP_HEADING:
    g_value_set_string (value, adw_message_dialog_get_heading (self));
    break;
  case PROP_HEADING_USE_MARKUP:
    g_value_set_boolean (value, adw_message_dialog_get_heading_use_markup (self));
    break;
  case PROP_BODY:
    g_value_set_string (value, adw_message_dialog_get_body (self));
    break;
  case PROP_BODY_USE_MARKUP:
    g_value_set_boolean (value, adw_message_dialog_get_body_use_markup (self));
    break;
  case PROP_EXTRA_CHILD:
    g_value_set_object (value, adw_message_dialog_get_extra_child (self));
    break;
  case PROP_DEFAULT_RESPONSE:
    g_value_set_string (value, adw_message_dialog_get_default_response (self));
    break;
  case PROP_CLOSE_RESPONSE:
    g_value_set_string (value, adw_message_dialog_get_close_response (self));
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
adw_message_dialog_set_property (GObject      *object,
                                 guint         prop_id,
                                 const GValue *value,
                                 GParamSpec   *pspec)
{
  AdwMessageDialog *self = ADW_MESSAGE_DIALOG (object);

  switch (prop_id) {
  case PROP_HEADING:
    adw_message_dialog_set_heading (self, g_value_get_string (value));
    break;
  case PROP_HEADING_USE_MARKUP:
    adw_message_dialog_set_heading_use_markup (self, g_value_get_boolean (value));
    break;
  case PROP_BODY:
    adw_message_dialog_set_body (self, g_value_get_string (value));
    break;
  case PROP_BODY_USE_MARKUP:
    adw_message_dialog_set_body_use_markup (self, g_value_get_boolean (value));
    break;
  case PROP_EXTRA_CHILD:
    adw_message_dialog_set_extra_child (self, g_value_get_object (value));
    break;
  case PROP_DEFAULT_RESPONSE:
    adw_message_dialog_set_default_response (self, g_value_get_string (value));
    break;
  case PROP_CLOSE_RESPONSE:
    adw_message_dialog_set_close_response (self, g_value_get_string (value));
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
adw_message_dialog_dispose (GObject *object)
{
  AdwMessageDialog *self = ADW_MESSAGE_DIALOG (object);
  AdwMessageDialogPrivate *priv = adw_message_dialog_get_instance_private (self);

  set_parent (self, NULL);

  if (priv->child) {
    gtk_box_remove (priv->message_area, priv->child);
    priv->child = NULL;
  }

834
835
836
837
  if (priv->responses) {
    g_list_free_full (priv->responses, (GDestroyNotify) response_info_free);
    priv->responses = NULL;
  }
Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
838

839
840
  g_clear_pointer (&priv->id_to_response, g_hash_table_unref);

Alexander Mikhaylenko's avatar
Alexander Mikhaylenko committed
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
  G_OBJECT_CLASS (adw_message_dialog_parent_class)->dispose (object);
}

static void
adw_message_dialog_finalize (GObject *object)
{
  AdwMessageDialog *self = ADW_MESSAGE_DIALOG (object);
  AdwMessageDialogPrivate *priv = adw_message_dialog_get_instance_private (self);

  g_clear_pointer (&priv->heading, g_free);
  g_clear_pointer (&priv->body, g_free);

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

static void
adw_message_dialog_class_init (AdwMessageDialogClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GtkWindowClass *window_class = GTK_WINDOW_CLASS (klass);

  object_class->get_property = adw_message_dialog_get_property;
  object_class->set_property = adw_message_dialog_set_property;
  object_class->dispose = adw_message_dialog_dispose;
  object_class->finalize = adw_message_dialog_finalize;

  widget_class->map = adw_message_dialog_map;
  widget_class->measure = adw_message_dialog_measure;

  window_class->close_request = adw_message_dialog_close_request;

  /**
   * AdwMessageDialog:heading: (attributes org.gtk.Property.get=adw_message_dialog_get_heading org.gtk.Property.set=adw_message_dialog_set_heading)
   *
   * The heading of the dialog.
   *
   * Since: 1.2
   */
  props[PROP_HEADING] =
    g_param_spec_string ("heading", NULL, NULL,
                         "",
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * AdwMessageDialog:heading-use-markup: (attributes org.gtk.Property.get=adw_message_dialog_get_heading_use_markup org.gtk.Property.set=adw_message_dialog_set_heading_use_markup)
   *
   * Whether the heading includes Pango markup.
   *
   * See [func@Pango.parse_markup].
   *
   * Since: 1.2
   */
  props[PROP_HEADING_USE_MARKUP] =
    g_param_spec_boolean ("heading-use-markup", NULL, NULL,
                          FALSE,
                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * AdwMessageDialog:body: (attributes org.gtk.Property.get=adw_message_dialog_get_body org.gtk.Property.set=adw_message_dialog_set_body)
   *
   * The body text of the dialog.
   *
   * Since: 1.2
   */
  props[PROP_BODY] =
    g_param_spec_string ("body", NULL, NULL,
                         "",
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * AdwMessageDialog:body-use-markup: (attributes org.gtk.Property.get=adw_message_dialog_get_body_use_markup org.gtk.Property.set=adw_message_dialog_set_body_use_markup)
   *
   * Whether the body text includes Pango markup.
   *
   * See [func@Pango.parse_markup].
   *
   * Since: 1.2
   */
  props[PROP_BODY_USE_MARKUP] =
    g_param_spec_boolean ("body-use-markup", NULL, NULL,
                          FALSE,
                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * AdwMessageDialog:extra-child: (attributes org.gtk.Property.get=adw_message_dialog_get_extra_child org.gtk.Property.set=adw_message_dialog_set_extra_child)
   *
   * The child widget.
   *
   * Displayed below the heading and body.
   *
   * Since: 1.2
   */
  props[PROP_EXTRA_CHILD] =
    g_param_spec_object ("extra-child", NULL, NULL,
                         GTK_TYPE_WIDGET,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * AdwMessageDialog:default-response: (attributes org.gtk.Property.get=adw_message_dialog_get_default_response org.gtk.Property.set=adw_message_dialog_set_default_response)
   *
   * The response ID of the default response.
   *
   * If set, pressing <kbd>Enter</kbd> will activate the corresponding button.
   *
   * If set to `NULL` or a non-existent response ID, pressing <kbd>Enter</kbd>
   * will do nothing.
   *
   * Since: 1.2
   */
  props[PROP_DEFAULT_RESPONSE] =
    g_param_spec_string ("default-response", NULL, NULL,
                          NULL,
                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * AdwMessageDialog:close-response: (attributes org.gtk.Property.get=adw_message_dialog_get_close_response org.gtk.Property.set=adw_message_dialog_set_close_response)
   *
   * The ID of the close response.
   *
   * It will be passed to [signal@MessageDialog::response] if the window is
   * closed by pressing <kbd>Escape</kbd> or with a system action.
   *
   * It doesn't have to correspond to any of the responses in the dialog.
   *
   * The default close response is `close`.
   *
   * Since: 1.2
   */
  props[PROP_CLOSE_RESPONSE] =
    g_param_spec_string ("close-response", NULL, NULL,
                          "close",
                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (object_class, LAST_PROP, props);

  /**
   * AdwMessageDialog::response:
   * @self: a message dialog
   * @response: the response ID
   *
   * This signal is emitted when the dialog is closed.
   *
   * @response will be set to the response ID of the button that had been
   * activated.
   *
   * if the dialog was closed by pressing <kbd>Escape</kbd> or with a system
   * action, @response will be set to the value of
   * [property@MessageDialog:close-response].
   *
   * Since: 1.2
   */
  signals[SIGNAL_RESPONSE] =
    g_signal_new ("response",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
                  G_STRUCT_OFFSET (AdwMessageDialogClass, response),
                  NULL, NULL, NULL,
                  G_TYPE_NONE,
                  1,
For faster browsing, not all history is shown. View entire blame