hdy-preferences-row.c 6.81 KB
Newer Older
Adrien Plazas's avatar
Adrien Plazas committed
1
2
3
/*
 * Copyright (C) 2019 Purism SPC
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
Adrien Plazas's avatar
Adrien Plazas committed
5
6
7
8
9
10
11
12
 */

#include "config.h"
#include <glib/gi18n-lib.h>

#include "hdy-preferences-row.h"

/**
13
 * HdyPreferencesRow:
Adrien Plazas's avatar
Adrien Plazas committed
14
 *
15
 * A [class@Gtk.ListBoxRow] used to present preferences.
Adrien Plazas's avatar
Adrien Plazas committed
16
 *
17
18
19
 * The `HdyPreferencesRow` widget has a title that [class@PreferencesWindow]
 * will use to let the user look for a preference. It doesn't present the title
 * in any way and lets you present the preference as you please.
Adrien Plazas's avatar
Adrien Plazas committed
20
 *
21
22
23
24
25
 * [class@ActionRow] and its derivatives are convenient to use as preference
 * rows as they take care of presenting the preference's title while letting you
 * compose the inputs of the preference around it.
 *
 * Since: 1.0
Adrien Plazas's avatar
Adrien Plazas committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
 */

typedef struct
{
  gchar *title;

  gboolean use_underline;
} HdyPreferencesRowPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (HdyPreferencesRow, hdy_preferences_row, GTK_TYPE_LIST_BOX_ROW)

enum {
  PROP_0,
  PROP_TITLE,
  PROP_USE_UNDERLINE,
  LAST_PROP,
};

static GParamSpec *props[LAST_PROP];

static void
hdy_preferences_row_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  HdyPreferencesRow *self = HDY_PREFERENCES_ROW (object);

  switch (prop_id) {
  case PROP_TITLE:
    g_value_set_string (value, hdy_preferences_row_get_title (self));
    break;
  case PROP_USE_UNDERLINE:
    g_value_set_boolean (value, hdy_preferences_row_get_use_underline (self));
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
hdy_preferences_row_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  HdyPreferencesRow *self = HDY_PREFERENCES_ROW (object);

  switch (prop_id) {
  case PROP_TITLE:
    hdy_preferences_row_set_title (self, g_value_get_string (value));
    break;
  case PROP_USE_UNDERLINE:
    hdy_preferences_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_preferences_row_finalize (GObject *object)
{
  HdyPreferencesRow *self = HDY_PREFERENCES_ROW (object);
  HdyPreferencesRowPrivate *priv = hdy_preferences_row_get_instance_private (self);

  g_free (priv->title);

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

static void
hdy_preferences_row_class_init (HdyPreferencesRowClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = hdy_preferences_row_get_property;
  object_class->set_property = hdy_preferences_row_set_property;
  object_class->finalize = hdy_preferences_row_finalize;

  /**
107
   * HdyPreferencesRow:title: (attributes org.gtk.Property.get=hdy_preferences_row_get_title org.gtk.Property.set=hdy_preferences_row_set_title)
Adrien Plazas's avatar
Adrien Plazas committed
108
109
110
   *
   * The title of the preference represented by this row.
   *
111
   * Since: 1.0
Adrien Plazas's avatar
Adrien Plazas committed
112
113
114
115
116
117
118
119
120
   */
  props[PROP_TITLE] =
    g_param_spec_string ("title",
                         _("Title"),
                         _("The title of the preference"),
                         "",
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

  /**
121
   * HdyPreferencesRow:use-underline: (attributes org.gtk.Property.get=hdy_preferences_row_get_use_underline org.gtk.Property.set=hdy_preferences_row_set_use_underline)
Adrien Plazas's avatar
Adrien Plazas committed
122
   *
123
   * Whether an embedded underline in the title indicates a mnemonic.
Adrien Plazas's avatar
Adrien Plazas committed
124
   *
125
   * Since: 1.0
Adrien Plazas's avatar
Adrien Plazas committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
   */
  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);
}

static void
hdy_preferences_row_init (HdyPreferencesRow *self)
{
}

/**
 * hdy_preferences_row_new:
 *
145
 * Creates a new `HdyPreferencesRow`.
Adrien Plazas's avatar
Adrien Plazas committed
146
 *
147
 * Returns: the newly created `HdyPreferencesRow`
Adrien Plazas's avatar
Adrien Plazas committed
148
 *
149
 * Since: 1.0
Adrien Plazas's avatar
Adrien Plazas committed
150
 */
Ujjwal Kumar's avatar
Ujjwal Kumar committed
151
GtkWidget *
Adrien Plazas's avatar
Adrien Plazas committed
152
153
154
155
156
157
hdy_preferences_row_new (void)
{
  return g_object_new (HDY_TYPE_PREFERENCES_ROW, NULL);
}

/**
158
159
 * hdy_preferences_row_get_title: (attributes org.gtk.Method.get_property=title)
 * @self: a preferences row
Adrien Plazas's avatar
Adrien Plazas committed
160
161
162
163
 *
 * Gets the title of the preference represented by @self.
 *
 * Returns: (transfer none) (nullable): the title of the preference represented
164
 *   by @self
Adrien Plazas's avatar
Adrien Plazas committed
165
 *
166
 * Since: 1.0
Adrien Plazas's avatar
Adrien Plazas committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
 */
const gchar *
hdy_preferences_row_get_title (HdyPreferencesRow *self)
{
  HdyPreferencesRowPrivate *priv;

  g_return_val_if_fail (HDY_IS_PREFERENCES_ROW (self), NULL);

  priv = hdy_preferences_row_get_instance_private (self);

  return priv->title;
}

/**
181
182
183
 * hdy_preferences_row_set_title: (attributes org.gtk.Method.set_property=title)
 * @self: a preferences row
 * @title: (nullable): the title
Adrien Plazas's avatar
Adrien Plazas committed
184
185
186
 *
 * Sets the title of the preference represented by @self.
 *
187
 * Since: 1.0
Adrien Plazas's avatar
Adrien Plazas committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
 */
void
hdy_preferences_row_set_title (HdyPreferencesRow *self,
                               const gchar       *title)
{
  HdyPreferencesRowPrivate *priv;

  g_return_if_fail (HDY_IS_PREFERENCES_ROW (self));

  priv = hdy_preferences_row_get_instance_private (self);

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

  g_free (priv->title);
  priv->title = g_strdup (title);

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

/**
209
210
 * hdy_preferences_row_get_use_underline: (attributes org.gtk.Method.get_property=use-underline)
 * @self: a preferences row
Adrien Plazas's avatar
Adrien Plazas committed
211
 *
212
 * Gets whether an embedded underline in the title indicates a mnemonic.
Adrien Plazas's avatar
Adrien Plazas committed
213
 *
214
 * Returns: whether an embedded underline in the title indicates a mnemonic
Adrien Plazas's avatar
Adrien Plazas committed
215
 *
216
 * Since: 1.0
Adrien Plazas's avatar
Adrien Plazas committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
 */
gboolean
hdy_preferences_row_get_use_underline (HdyPreferencesRow *self)
{
  HdyPreferencesRowPrivate *priv;

  g_return_val_if_fail (HDY_IS_PREFERENCES_ROW (self), FALSE);

  priv = hdy_preferences_row_get_instance_private (self);

  return priv->use_underline;
}

/**
231
232
233
 * hdy_preferences_row_set_use_underline: (attributes org.gtk.Method.set_property=use-underline)
 * @self: a preferences row
 * @use_underline: `TRUE` if underlines in the text indicate mnemonics
Adrien Plazas's avatar
Adrien Plazas committed
234
 *
235
 * Sets whether an embedded underline in the title indicates a mnemonic.
Adrien Plazas's avatar
Adrien Plazas committed
236
 *
237
 * Since: 1.0
Adrien Plazas's avatar
Adrien Plazas committed
238
239
240
241
242
243
244
245
246
247
248
 */
void
hdy_preferences_row_set_use_underline (HdyPreferencesRow *self,
                                       gboolean           use_underline)
{
  HdyPreferencesRowPrivate *priv;

  g_return_if_fail (HDY_IS_PREFERENCES_ROW (self));

  priv = hdy_preferences_row_get_instance_private (self);

249
250
251
  use_underline = !!use_underline;

  if (priv->use_underline == use_underline)
Adrien Plazas's avatar
Adrien Plazas committed
252
253
    return;

254
  priv->use_underline = use_underline;
Adrien Plazas's avatar
Adrien Plazas committed
255
256
257

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