hdy-action-row.c 23.1 KB
Newer Older
Adrien Plazas's avatar
Adrien Plazas committed
1
2
3
4
5
6
/*
 * Copyright (C) 2018 Purism SPC
 *
 * SPDX-License-Identifier: LGPL-2.1+
 */

Bastien Nocera's avatar
Bastien Nocera committed
7
#include "config.h"
Adrien Plazas's avatar
Adrien Plazas committed
8
9
#include "hdy-action-row.h"

Bastien Nocera's avatar
Bastien Nocera committed
10
#include <glib/gi18n-lib.h>
Adrien Plazas's avatar
Adrien Plazas committed
11
12
13

/**
 * SECTION:hdy-action-row
14
 * @short_description: A #GtkListBox row used to present actions.
Adrien Plazas's avatar
Adrien Plazas committed
15
16
17
 * @Title: HdyActionRow
 *
 * The #HdyActionRow widget can have a title, a subtitle and an icon. The row
18
 * can receive additional widgets at its end, or prefix widgets at its start.
Adrien Plazas's avatar
Adrien Plazas committed
19
 *
20
 * It is convenient to present a preference and its related actions.
Adrien Plazas's avatar
Adrien Plazas committed
21
 *
22
23
24
25
 * #HdyActionRow is unactivatable by default, giving it an activatable widget
 * will automatically make it activatable, but unsetting it won't change the
 * row's activatability.
 *
Adrien Plazas's avatar
Adrien Plazas committed
26
27
28
 * # HdyActionRow as GtkBuildable
 *
 * The GtkWindow implementation of the GtkBuildable interface supports setting a
29
 * child at its end by omitting the “type” attribute of a &lt;child&gt; element.
Adrien Plazas's avatar
Adrien Plazas committed
30
31
32
 *
 * It also supports setting a child as a prefix widget by specifying “prefix” as
 * the “type” attribute of a &lt;child&gt; element.
Guido Günther's avatar
Guido Günther committed
33
 *
34
35
36
37
38
39
40
41
42
43
 * # CSS nodes
 *
 * #HdyActionRow has a main CSS node with name row.
 *
 * It contains the subnode box.header for its main horizontal box, and box.title
 * for the vertical box containing the title and subtitle labels.
 *
 * It contains subnodes label.title and label.subtitle representing respectively
 * the title label and subtitle label.
 *
Guido Günther's avatar
Guido Günther committed
44
 * Since: 0.0.6
Adrien Plazas's avatar
Adrien Plazas committed
45
46
47
48
49
50
51
52
 */

typedef struct
{
  GtkBox *header;
  GtkImage *image;
  GtkBox *prefixes;
  GtkLabel *subtitle;
53
  GtkBox *suffixes;
Adrien Plazas's avatar
Adrien Plazas committed
54
55
56
57
58
59
  GtkLabel *title;
  GtkBox *title_box;

  GtkWidget *previous_parent;

  gboolean use_underline;
60
  GtkWidget *activatable_widget;
Adrien Plazas's avatar
Adrien Plazas committed
61
62
63
64
} HdyActionRowPrivate;

static void hdy_action_row_buildable_init (GtkBuildableIface *iface);

65
G_DEFINE_TYPE_WITH_CODE (HdyActionRow, hdy_action_row, HDY_TYPE_PREFERENCES_ROW,
Adrien Plazas's avatar
Adrien Plazas committed
66
67
68
69
70
71
72
73
74
                         G_ADD_PRIVATE (HdyActionRow)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                         hdy_action_row_buildable_init))

static GtkBuildableIface *parent_buildable_iface;

enum {
  PROP_0,
  PROP_ICON_NAME,
75
  PROP_ACTIVATABLE_WIDGET,
Adrien Plazas's avatar
Adrien Plazas committed
76
77
78
79
80
81
82
83
  PROP_SUBTITLE,
  PROP_TITLE,
  PROP_USE_UNDERLINE,
  LAST_PROP,
};

static GParamSpec *props[LAST_PROP];

84
85
86
87
88
89
90
enum {
  SIGNAL_ACTIVATED,
  SIGNAL_LAST_SIGNAL,
};

static guint signals[SIGNAL_LAST_SIGNAL];

Adrien Plazas's avatar
Adrien Plazas committed
91
static void
92
row_activated_cb (HdyActionRow  *self,
Adrien Plazas's avatar
Adrien Plazas committed
93
94
                  GtkListBoxRow *row)
{
95
96
  /* No need to use GTK_LIST_BOX_ROW() for a pointer comparison. */
  if ((GtkListBoxRow *) self == row)
Adrien Plazas's avatar
Adrien Plazas committed
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
    hdy_action_row_activate (self);
}

static void
parent_cb (HdyActionRow *self)
{
  HdyActionRowPrivate *priv = hdy_action_row_get_instance_private (self);
  GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (self));

  if (priv->previous_parent != NULL) {
    g_signal_handlers_disconnect_by_func (priv->previous_parent, G_CALLBACK (row_activated_cb), self);
    priv->previous_parent = NULL;
  }

  if (parent == NULL || !GTK_IS_LIST_BOX (parent))
    return;

  priv->previous_parent = parent;
  g_signal_connect_swapped (parent, "row-activated", G_CALLBACK (row_activated_cb), self);
}

static void
update_subtitle_visibility (HdyActionRow *self)
{
  HdyActionRowPrivate *priv = hdy_action_row_get_instance_private (self);

  gtk_widget_set_visible (GTK_WIDGET (priv->subtitle),
                          gtk_label_get_text (priv->subtitle) != NULL &&
                          g_strcmp0 (gtk_label_get_text (priv->subtitle), "") != 0);
}

static void
hdy_action_row_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  HdyActionRow *self = HDY_ACTION_ROW (object);

  switch (prop_id) {
  case PROP_ICON_NAME:
    g_value_set_string (value, hdy_action_row_get_icon_name (self));
    break;
140
141
142
  case PROP_ACTIVATABLE_WIDGET:
    g_value_set_object (value, (GObject *) hdy_action_row_get_activatable_widget (self));
    break;
Adrien Plazas's avatar
Adrien Plazas committed
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
  case PROP_SUBTITLE:
    g_value_set_string (value, hdy_action_row_get_subtitle (self));
    break;
  case PROP_TITLE:
    g_value_set_string (value, hdy_action_row_get_title (self));
    break;
  case PROP_USE_UNDERLINE:
    g_value_set_boolean (value, hdy_action_row_get_use_underline (self));
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
hdy_action_row_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  HdyActionRow *self = HDY_ACTION_ROW (object);

  switch (prop_id) {
  case PROP_ICON_NAME:
    hdy_action_row_set_icon_name (self, g_value_get_string (value));
    break;
169
170
171
  case PROP_ACTIVATABLE_WIDGET:
    hdy_action_row_set_activatable_widget (self, (GtkWidget*) g_value_get_object (value));
    break;
Adrien Plazas's avatar
Adrien Plazas committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  case PROP_SUBTITLE:
    hdy_action_row_set_subtitle (self, g_value_get_string (value));
    break;
  case PROP_TITLE:
    hdy_action_row_set_title (self, g_value_get_string (value));
    break;
  case PROP_USE_UNDERLINE:
    hdy_action_row_set_use_underline (self, g_value_get_boolean (value));
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
hdy_action_row_dispose (GObject *object)
{
  HdyActionRow *self = HDY_ACTION_ROW (object);
  HdyActionRowPrivate *priv = hdy_action_row_get_instance_private (self);

192
193
194
195
  if (priv->previous_parent != NULL) {
    g_signal_handlers_disconnect_by_func (priv->previous_parent, G_CALLBACK (row_activated_cb), self);
    priv->previous_parent = NULL;
  }
Adrien Plazas's avatar
Adrien Plazas committed
196

197
  G_OBJECT_CLASS (hdy_action_row_parent_class)->dispose (object);
Adrien Plazas's avatar
Adrien Plazas committed
198
199
}

Adrien Plazas's avatar
Adrien Plazas committed
200
201
202
203
204
205
206
207
208
209
210
211
212
static void
hdy_action_row_show_all (GtkWidget *widget)
{
  HdyActionRow *self = HDY_ACTION_ROW (widget);
  HdyActionRowPrivate *priv;

  g_return_if_fail (HDY_IS_ACTION_ROW (self));

  priv = hdy_action_row_get_instance_private (self);

  gtk_container_foreach (GTK_CONTAINER (priv->prefixes),
                         (GtkCallback) gtk_widget_show_all,
                         NULL);
213
214
215
216
217

  gtk_container_foreach (GTK_CONTAINER (priv->suffixes),
                         (GtkCallback) gtk_widget_show_all,
                         NULL);

Adrien Plazas's avatar
Adrien Plazas committed
218
219
220
  GTK_WIDGET_CLASS (hdy_action_row_parent_class)->show_all (widget);
}

221
222
223
224
static void
hdy_action_row_destroy (GtkWidget *widget)
{
  HdyActionRow *self = HDY_ACTION_ROW (widget);
225
  HdyActionRowPrivate *priv = hdy_action_row_get_instance_private (self);
226

227
228
229
  if (priv->header) {
    gtk_widget_destroy (GTK_WIDGET (priv->header));
    priv->header = NULL;
230
231
  }

232
233
  hdy_action_row_set_activatable_widget (self, NULL);

234
  priv->prefixes = NULL;
235
  priv->suffixes = NULL;
236

237
238
239
  GTK_WIDGET_CLASS (hdy_action_row_parent_class)->destroy (widget);
}

Adrien Plazas's avatar
Adrien Plazas committed
240
241
242
243
244
245
246
247
248
249
static void
hdy_action_row_add (GtkContainer *container,
                    GtkWidget    *child)
{
  HdyActionRow *self = HDY_ACTION_ROW (container);
  HdyActionRowPrivate *priv = hdy_action_row_get_instance_private (self);

  /* When constructing the widget, we want the box to be added as the child of
   * the GtkListBoxRow, as an implementation detail.
   */
250
  if (priv->header == NULL)
Adrien Plazas's avatar
Adrien Plazas committed
251
    GTK_CONTAINER_CLASS (hdy_action_row_parent_class)->add (container, child);
252
  else {
253
    gtk_container_add (GTK_CONTAINER (priv->suffixes), child);
254
255
    gtk_widget_show (GTK_WIDGET (priv->suffixes));
  }
Adrien Plazas's avatar
Adrien Plazas committed
256
257
}

258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
static void
hdy_action_row_remove (GtkContainer *container,
                       GtkWidget    *child)
{
  HdyActionRow *self = HDY_ACTION_ROW (container);
  HdyActionRowPrivate *priv = hdy_action_row_get_instance_private (self);

  if (child == GTK_WIDGET (priv->header))
    GTK_CONTAINER_CLASS (hdy_action_row_parent_class)->remove (container, child);
  else if (gtk_widget_get_parent (child) == GTK_WIDGET (priv->prefixes))
    gtk_container_remove (GTK_CONTAINER (priv->prefixes), child);
  else
    gtk_container_remove (GTK_CONTAINER (priv->suffixes), child);
}

Adrien Plazas's avatar
Adrien Plazas committed
273
274
275
276
277
278
279
280
281
282
283
284
285
typedef struct {
  HdyActionRow *row;
  GtkCallback callback;
  gpointer callback_data;
} ForallData;

static void
for_non_internal_child (GtkWidget *widget,
                        gpointer   callback_data)
{
  ForallData *data = callback_data;
  HdyActionRowPrivate *priv = hdy_action_row_get_instance_private (data->row);

286
  if (widget != (GtkWidget *) priv->image &&
Adrien Plazas's avatar
Adrien Plazas committed
287
      widget != (GtkWidget *) priv->prefixes &&
288
      widget != (GtkWidget *) priv->suffixes &&
Adrien Plazas's avatar
Adrien Plazas committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
      widget != (GtkWidget *) priv->title_box)
    data->callback (widget, data->callback_data);
}

static void
hdy_action_row_forall (GtkContainer *container,
                       gboolean      include_internals,
                       GtkCallback   callback,
                       gpointer      callback_data)
{
  HdyActionRow *self = HDY_ACTION_ROW (container);
  HdyActionRowPrivate *priv = hdy_action_row_get_instance_private (self);
  ForallData data;

  if (include_internals) {
    GTK_CONTAINER_CLASS (hdy_action_row_parent_class)->forall (GTK_CONTAINER (self), include_internals, callback, callback_data);

    return;
  }

  data.row = self;
  data.callback = callback;
  data.callback_data = callback_data;

313
314
  if (priv->prefixes)
    GTK_CONTAINER_GET_CLASS (priv->prefixes)->forall (GTK_CONTAINER (priv->prefixes), include_internals, for_non_internal_child, &data);
315
316
  if (priv->suffixes)
    GTK_CONTAINER_GET_CLASS (priv->suffixes)->forall (GTK_CONTAINER (priv->suffixes), include_internals, for_non_internal_child, &data);
317
318
  if (priv->header)
    GTK_CONTAINER_GET_CLASS (priv->header)->forall (GTK_CONTAINER (priv->header), include_internals, for_non_internal_child, &data);
Adrien Plazas's avatar
Adrien Plazas committed
319
320
321
322
323
}

static void
hdy_action_row_activate_real (HdyActionRow *self)
{
324
325
326
327
  HdyActionRowPrivate *priv = hdy_action_row_get_instance_private (self);

  if (priv->activatable_widget)
    gtk_widget_mnemonic_activate (priv->activatable_widget, FALSE);
328
329

  g_signal_emit (self, signals[SIGNAL_ACTIVATED], 0);
Adrien Plazas's avatar
Adrien Plazas committed
330
331
332
333
334
335
336
337
338
339
340
341
342
}

static void
hdy_action_row_class_init (HdyActionRowClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);

  object_class->get_property = hdy_action_row_get_property;
  object_class->set_property = hdy_action_row_set_property;
  object_class->dispose = hdy_action_row_dispose;

343
  widget_class->destroy = hdy_action_row_destroy;
Adrien Plazas's avatar
Adrien Plazas committed
344
345
  widget_class->show_all = hdy_action_row_show_all;

Adrien Plazas's avatar
Adrien Plazas committed
346
  container_class->add = hdy_action_row_add;
347
  container_class->remove = hdy_action_row_remove;
Adrien Plazas's avatar
Adrien Plazas committed
348
349
350
351
352
353
354
355
  container_class->forall = hdy_action_row_forall;

  klass->activate = hdy_action_row_activate_real;

  /**
   * HdyActionRow:icon-name:
   *
   * The icon name for this row.
356
357
   *
   * Since: 0.0.6
Adrien Plazas's avatar
Adrien Plazas committed
358
359
360
361
362
363
364
365
   */
  props[PROP_ICON_NAME] =
    g_param_spec_string ("icon-name",
                         _("Icon name"),
                         _("Icon name"),
                         "",
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

366
367
368
369
370
371
372
373
374
375
376
377
378
379
  /**
   * HdyActionRow:activatable-widget:
   *
   * The activatable widget for this row.
   *
   * Since: 0.0.7
   */
  props[PROP_ACTIVATABLE_WIDGET] =
      g_param_spec_object ("activatable-widget",
                           _("Activatable widget"),
                           _("The widget to be activated when the row is activated"),
                           GTK_TYPE_WIDGET,
                           G_PARAM_READWRITE);

Adrien Plazas's avatar
Adrien Plazas committed
380
381
382
383
  /**
   * HdyActionRow:subtitle:
   *
   * The subtitle for this row.
384
385
   *
   * Since: 0.0.6
Adrien Plazas's avatar
Adrien Plazas committed
386
387
388
389
390
391
392
393
394
395
396
397
   */
  props[PROP_SUBTITLE] =
    g_param_spec_string ("subtitle",
                         _("Subtitle"),
                         _("Subtitle"),
                         "",
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * HdyActionRow:title:
   *
   * The title for this row.
398
399
   *
   * Since: 0.0.6
Adrien Plazas's avatar
Adrien Plazas committed
400
401
402
403
404
405
406
407
   */
  props[PROP_TITLE] =
    g_param_spec_string ("title",
                         _("Title"),
                         _("Title"),
                         "",
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

408
409
410
411
412
413
414
415
  /**
   * HdyActionRow:use-underline:
   *
   * Whether an embedded underline in the text of the title and subtitle labels
   * indicates a mnemonic.
   *
   * Since: 0.0.6
   */
Adrien Plazas's avatar
Adrien Plazas committed
416
417
418
419
420
421
422
423
424
  props[PROP_USE_UNDERLINE] =
    g_param_spec_boolean ("use-underline",
                          _("Use underline"),
                          _("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
                          FALSE,
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (object_class, LAST_PROP, props);

425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
  /**
   * HdyActionRow::activated:
   * @self: The #HdyActionRow instance
   *
   * This signal is emitted after the row has been activated.
   *
   * Since: 1.0
   */
  signals[SIGNAL_ACTIVATED] =
    g_signal_new ("activated",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL, NULL,
                  G_TYPE_NONE,
                  0);

Adrien Plazas's avatar
Adrien Plazas committed
442
  gtk_widget_class_set_template_from_resource (widget_class,
443
                                               "/sm/puri/handy/ui/hdy-action-row.ui");
Adrien Plazas's avatar
Adrien Plazas committed
444
445
446
447
  gtk_widget_class_bind_template_child_private (widget_class, HdyActionRow, header);
  gtk_widget_class_bind_template_child_private (widget_class, HdyActionRow, image);
  gtk_widget_class_bind_template_child_private (widget_class, HdyActionRow, prefixes);
  gtk_widget_class_bind_template_child_private (widget_class, HdyActionRow, subtitle);
448
  gtk_widget_class_bind_template_child_private (widget_class, HdyActionRow, suffixes);
Adrien Plazas's avatar
Adrien Plazas committed
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
  gtk_widget_class_bind_template_child_private (widget_class, HdyActionRow, title);
  gtk_widget_class_bind_template_child_private (widget_class, HdyActionRow, title_box);
}

static void
hdy_action_row_init (HdyActionRow *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));

  update_subtitle_visibility (self);

  g_signal_connect (self, "notify::parent", G_CALLBACK (parent_cb), NULL);

}

static void
hdy_action_row_buildable_add_child (GtkBuildable *buildable,
                                    GtkBuilder   *builder,
                                    GObject      *child,
                                    const gchar  *type)
{
470
471
472
  HdyActionRow *self = HDY_ACTION_ROW (buildable);
  HdyActionRowPrivate *priv = hdy_action_row_get_instance_private (self);

473
  if (priv->header == NULL || !type)
474
    gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (child));
Adrien Plazas's avatar
Adrien Plazas committed
475
  else if (type && strcmp (type, "prefix") == 0)
476
    hdy_action_row_add_prefix (self, GTK_WIDGET (child));
Adrien Plazas's avatar
Adrien Plazas committed
477
  else
478
    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (self, type);
Adrien Plazas's avatar
Adrien Plazas committed
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
}

static void
hdy_action_row_buildable_init (GtkBuildableIface *iface)
{
  parent_buildable_iface = g_type_interface_peek_parent (iface);
  iface->add_child = hdy_action_row_buildable_add_child;
}

/**
 * hdy_action_row_new:
 *
 * Creates a new #HdyActionRow.
 *
 * Returns: a new #HdyActionRow
Guido Günther's avatar
Guido Günther committed
494
495
 *
 * Since: 0.0.6
Adrien Plazas's avatar
Adrien Plazas committed
496
 */
Ujjwal Kumar's avatar
Ujjwal Kumar committed
497
GtkWidget *
Adrien Plazas's avatar
Adrien Plazas committed
498
499
500
501
502
503
504
505
506
507
508
509
hdy_action_row_new (void)
{
  return g_object_new (HDY_TYPE_ACTION_ROW, NULL);
}

/**
 * hdy_action_row_get_title:
 * @self: a #HdyActionRow
 *
 * Gets the title for @self.
 *
 * Returns: the title for @self.
Guido Günther's avatar
Guido Günther committed
510
511
 *
 * Since: 0.0.6
Adrien Plazas's avatar
Adrien Plazas committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
 */
const gchar *
hdy_action_row_get_title (HdyActionRow *self)
{
  HdyActionRowPrivate *priv;

  g_return_val_if_fail (HDY_IS_ACTION_ROW (self), NULL);

  priv = hdy_action_row_get_instance_private (self);

  return gtk_label_get_text (priv->title);
}

/**
 * hdy_action_row_set_title:
 * @self: a #HdyActionRow
 * @title: the title
 *
 * Sets the title for @self.
Guido Günther's avatar
Guido Günther committed
531
532
 *
 * Since: 0.0.6
Adrien Plazas's avatar
Adrien Plazas committed
533
534
535
536
537
538
539
540
541
542
 */
void
hdy_action_row_set_title (HdyActionRow *self,
                          const gchar  *title)
{
  HdyActionRowPrivate *priv;

  g_return_if_fail (HDY_IS_ACTION_ROW (self));

  priv = hdy_action_row_get_instance_private (self);
543
  hdy_preferences_row_set_title (HDY_PREFERENCES_ROW (self), title);
Adrien Plazas's avatar
Adrien Plazas committed
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561

  if (g_strcmp0 (gtk_label_get_text (priv->title), title) == 0)
    return;

  gtk_label_set_text (priv->title, title);
  gtk_widget_set_visible (GTK_WIDGET (priv->title),
                          title != NULL && g_strcmp0 (title, "") != 0);

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

/**
 * hdy_action_row_get_subtitle:
 * @self: a #HdyActionRow
 *
 * Gets the subtitle for @self.
 *
 * Returns: the subtitle for @self.
Guido Günther's avatar
Guido Günther committed
562
563
 *
 * Since: 0.0.6
Adrien Plazas's avatar
Adrien Plazas committed
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
 */
const gchar *
hdy_action_row_get_subtitle (HdyActionRow *self)
{
  HdyActionRowPrivate *priv;

  g_return_val_if_fail (HDY_IS_ACTION_ROW (self), NULL);

  priv = hdy_action_row_get_instance_private (self);

  return gtk_label_get_text (priv->subtitle);
}

/**
 * hdy_action_row_set_subtitle:
 * @self: a #HdyActionRow
 * @subtitle: the subtitle
 *
 * Sets the subtitle for @self.
Guido Günther's avatar
Guido Günther committed
583
584
 *
 * Since: 0.0.6
Adrien Plazas's avatar
Adrien Plazas committed
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
 */
void
hdy_action_row_set_subtitle (HdyActionRow *self,
                             const gchar  *subtitle)
{
  HdyActionRowPrivate *priv;

  g_return_if_fail (HDY_IS_ACTION_ROW (self));

  priv = hdy_action_row_get_instance_private (self);

  if (g_strcmp0 (gtk_label_get_text (priv->subtitle), subtitle) == 0)
    return;

  gtk_label_set_text (priv->subtitle, subtitle);
600
  gtk_widget_set_tooltip_text (GTK_WIDGET (priv->subtitle), subtitle);
Adrien Plazas's avatar
Adrien Plazas committed
601
602
603
604
605
606
607
608
609
610
611
612
613
  gtk_widget_set_visible (GTK_WIDGET (priv->subtitle),
                          subtitle != NULL && g_strcmp0 (subtitle, "") != 0);

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

/**
 * hdy_action_row_get_icon_name:
 * @self: a #HdyActionRow
 *
 * Gets the icon name for @self.
 *
 * Returns: the icon name for @self.
Guido Günther's avatar
Guido Günther committed
614
615
 *
 * Since: 0.0.6
Adrien Plazas's avatar
Adrien Plazas committed
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
 */
const gchar *
hdy_action_row_get_icon_name (HdyActionRow *self)
{
  HdyActionRowPrivate *priv;
  const gchar *icon_name;

  g_return_val_if_fail (HDY_IS_ACTION_ROW (self), NULL);

  priv = hdy_action_row_get_instance_private (self);

  gtk_image_get_icon_name (priv->image, &icon_name, NULL);

  return icon_name;
}

/**
 * hdy_action_row_set_icon_name:
 * @self: a #HdyActionRow
 * @icon_name: the icon name
 *
 * Sets the icon name for @self.
Guido Günther's avatar
Guido Günther committed
638
639
 *
 * Since: 0.0.6
Adrien Plazas's avatar
Adrien Plazas committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
 */
void
hdy_action_row_set_icon_name (HdyActionRow *self,
                              const gchar  *icon_name)
{
  HdyActionRowPrivate *priv;
  const gchar *old_icon_name;

  g_return_if_fail (HDY_IS_ACTION_ROW (self));

  priv = hdy_action_row_get_instance_private (self);

  gtk_image_get_icon_name (priv->image, &old_icon_name, NULL);
  if (g_strcmp0 (old_icon_name, icon_name) == 0)
    return;

  gtk_image_set_from_icon_name (priv->image, icon_name, GTK_ICON_SIZE_INVALID);
  gtk_widget_set_visible (GTK_WIDGET (priv->image),
                          icon_name != NULL && g_strcmp0 (icon_name, "") != 0);

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

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
/**
 * hdy_action_row_get_activatable_widget:
 * @self: a #HdyActionRow
 *
 * Gets the widget activated when @self is activated.
 *
 * Returns: (nullable) (transfer none): the widget activated when @self is
 *          activated, or %NULL if none has been set.
 *
 * Since: 0.0.7
 */
GtkWidget *
hdy_action_row_get_activatable_widget (HdyActionRow *self)
{
  HdyActionRowPrivate *priv;

  g_return_val_if_fail (HDY_IS_ACTION_ROW (self), NULL);

  priv = hdy_action_row_get_instance_private (self);

  return priv->activatable_widget;
}

static void
activatable_widget_weak_notify (gpointer  data,
                                GObject  *where_the_object_was)
{
  HdyActionRow *self = HDY_ACTION_ROW (data);
  HdyActionRowPrivate *priv = hdy_action_row_get_instance_private (self);

  priv->activatable_widget = NULL;

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

/**
 * hdy_action_row_set_activatable_widget:
 * @self: a #HdyActionRow
 * @widget: (nullable): the target #GtkWidget, or %NULL to unset
 *
 * Sets the widget to activate when @self is activated, either by clicking
 * on it, by calling hdy_action_row_activate(), or via mnemonics in the title or
 * the subtitle. See the “use_underline” property to enable mnemonics.
 *
 * The target widget will be activated by emitting the
 * GtkWidget::mnemonic-activate signal on it.
 *
 * Since: 0.0.7
 */
void
hdy_action_row_set_activatable_widget (HdyActionRow *self,
                                       GtkWidget    *widget)
{
  HdyActionRowPrivate *priv;

  g_return_if_fail (HDY_IS_ACTION_ROW (self));
719
  g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
720
721
722
723
724
725
726
727
728
729
730
731
732

  priv = hdy_action_row_get_instance_private (self);

  if (priv->activatable_widget == widget)
    return;

  if (priv->activatable_widget)
    g_object_weak_unref (G_OBJECT (priv->activatable_widget),
                         activatable_widget_weak_notify,
                         self);

  priv->activatable_widget = widget;

733
  if (priv->activatable_widget != NULL) {
734
735
736
    g_object_weak_ref (G_OBJECT (priv->activatable_widget),
                       activatable_widget_weak_notify,
                       self);
737
738
    gtk_list_box_row_set_activatable (GTK_LIST_BOX_ROW (self), TRUE);
  }
739
740
741
742

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

Adrien Plazas's avatar
Adrien Plazas committed
743
744
745
746
747
748
749
750
751
/**
 * hdy_action_row_get_use_underline:
 * @self: a #HdyActionRow
 *
 * Gets whether an embedded underline in the text of the title and subtitle
 * labels indicates a mnemonic. See hdy_action_row_set_use_underline().
 *
 * Returns: %TRUE if an embedded underline in the title and subtitle labels
 *          indicates the mnemonic accelerator keys.
Guido Günther's avatar
Guido Günther committed
752
753
 *
 * Since: 0.0.6
Adrien Plazas's avatar
Adrien Plazas committed
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
 */
gboolean
hdy_action_row_get_use_underline (HdyActionRow *self)
{
  HdyActionRowPrivate *priv;

  g_return_val_if_fail (HDY_IS_ACTION_ROW (self), FALSE);

  priv = hdy_action_row_get_instance_private (self);

  return priv->use_underline;
}

/**
 * hdy_action_row_set_use_underline:
 * @self: a #HdyActionRow
 * @use_underline: %TRUE if underlines in the text indicate mnemonics
 *
 * If true, an underline in the text of the title and subtitle labels indicates
 * the next character should be used for the mnemonic accelerator key.
Guido Günther's avatar
Guido Günther committed
774
775
 *
 * Since: 0.0.6
Adrien Plazas's avatar
Adrien Plazas committed
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
 */
void
hdy_action_row_set_use_underline (HdyActionRow *self,
                                  gboolean      use_underline)
{
  HdyActionRowPrivate *priv;

  g_return_if_fail (HDY_IS_ACTION_ROW (self));

  priv = hdy_action_row_get_instance_private (self);

  if (priv->use_underline == !!use_underline)
    return;

  priv->use_underline = !!use_underline;
791
  hdy_preferences_row_set_use_underline (HDY_PREFERENCES_ROW (self), priv->use_underline);
Adrien Plazas's avatar
Adrien Plazas committed
792
793
794
795
796
797
798
799
800
801
802
  gtk_label_set_use_underline (priv->title, priv->use_underline);
  gtk_label_set_use_underline (priv->subtitle, priv->use_underline);
  gtk_label_set_mnemonic_widget (priv->title, GTK_WIDGET (self));
  gtk_label_set_mnemonic_widget (priv->subtitle, GTK_WIDGET (self));

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

/**
 * hdy_action_row_add_prefix:
 * @self: a #HdyActionRow
803
 * @widget: the prefix widget
Adrien Plazas's avatar
Adrien Plazas committed
804
805
 *
 * Adds a prefix widget to @self.
Guido Günther's avatar
Guido Günther committed
806
807
 *
 * Since: 0.0.6
Adrien Plazas's avatar
Adrien Plazas committed
808
809
810
811
812
813
814
815
 */
void
hdy_action_row_add_prefix (HdyActionRow *self,
                           GtkWidget    *widget)
{
  HdyActionRowPrivate *priv;

  g_return_if_fail (HDY_IS_ACTION_ROW (self));
816
  g_return_if_fail (GTK_IS_WIDGET (self));
Adrien Plazas's avatar
Adrien Plazas committed
817
818
819
820
821
822
823
824
825
826
827
828
829
830

  priv = hdy_action_row_get_instance_private (self);

  gtk_box_pack_start (priv->prefixes, widget, FALSE, TRUE, 0);
  gtk_widget_show (GTK_WIDGET (priv->prefixes));
}

void
hdy_action_row_activate (HdyActionRow *self)
{
  g_return_if_fail (HDY_IS_ACTION_ROW (self));

  HDY_ACTION_ROW_GET_CLASS (self)->activate (self);
}