summaryrefslogtreecommitdiffstats
path: root/panels/common/cc-list-row.c
blob: 20ee00260e8819639de64f28db9c17d3b9e2c82c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* cc-list-row.c
 *
 * Copyright 2019 Purism SPC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author(s):
 *   Mohammed Sadiq <sadiq@sadiqpk.org>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "cc-list-row"

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "cc-list-row.h"

struct _CcListRow
{
  GtkListBoxRow parent_instance;

  GtkBox       *box;
  GtkLabel     *title;
  GtkLabel     *subtitle;
  GtkLabel     *secondary_label;
  GtkImage     *icon;

  GtkSwitch    *enable_switch;
  gboolean      show_switch;

  gboolean      switch_active;
};

G_DEFINE_TYPE (CcListRow, cc_list_row, GTK_TYPE_LIST_BOX_ROW)


enum {
  PROP_0,
  PROP_TITLE,
  PROP_SUBTITLE,
  PROP_SECONDARY_LABEL,
  PROP_ICON_NAME,
  PROP_SHOW_SWITCH,
  PROP_ACTIVE,
  PROP_USE_UNDERLINE,
  N_PROPS
};

static GParamSpec *properties[N_PROPS];

static void
cc_list_row_activated_cb (CcListRow     *self,
                          GtkListBoxRow *row)
{
  g_assert (CC_IS_LIST_ROW (self));

  if (!self->show_switch || row != GTK_LIST_BOX_ROW (self))
    return;

  cc_list_row_activate (self);
}

static void
cc_list_row_parent_changed_cb (CcListRow *self)
{
  GtkWidget *parent;

  g_assert (CC_IS_LIST_ROW (self));

  parent = gtk_widget_get_parent (GTK_WIDGET (self));

  if (!parent)
    return;

  g_return_if_fail (GTK_IS_LIST_BOX (parent));
  g_signal_connect_object (parent, "row-activated",
                           G_CALLBACK (cc_list_row_activated_cb),
                           self, G_CONNECT_SWAPPED);
}

static void
cc_list_row_switch_active_cb (CcListRow *self)
{
  gboolean switch_active;

  g_assert (CC_IS_LIST_ROW (self));
  g_assert (self->show_switch);

  switch_active = gtk_switch_get_active (self->enable_switch);

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

  self->switch_active = switch_active;
  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACTIVE]);
}

static void
cc_list_row_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  CcListRow *self = (CcListRow *)object;

  switch (prop_id)
    {
    case PROP_SECONDARY_LABEL:
      g_value_set_string (value, gtk_label_get_label (self->secondary_label));
      break;

    case PROP_ACTIVE:
      g_value_set_boolean (value, self->switch_active);
      break;

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

static void
cc_list_row_set_property (GObject      *object,
                          guint         prop_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  CcListRow *self = (CcListRow *)object;
  gint margin;

  switch (prop_id)
    {
    case PROP_TITLE:
      gtk_label_set_label (self->title, g_value_get_string (value));
      break;

    case PROP_SUBTITLE:
      gtk_widget_set_visible (GTK_WIDGET (self->subtitle),
                              g_value_get_string (value) != NULL);
      gtk_label_set_label (self->subtitle, g_value_get_string (value));
      if (g_value_get_string (value) != NULL)
        margin = 6;
      else
        margin = 12;
      g_object_set (self->box,
                    "margin-top", margin,
                    "margin-bottom", margin,
                    NULL);
      break;

    case PROP_SECONDARY_LABEL:
      gtk_label_set_label (self->secondary_label, g_value_get_string (value));
      break;

    case PROP_ICON_NAME:
      cc_list_row_set_icon_name (self, g_value_get_string (value));
      break;

    case PROP_SHOW_SWITCH:
      cc_list_row_set_show_switch (self, g_value_get_boolean (value));
      break;

    case PROP_USE_UNDERLINE:
      gtk_label_set_use_underline (self->title, g_value_get_boolean (value));
      gtk_label_set_use_underline (self->subtitle, g_value_get_boolean (value));
      gtk_label_set_mnemonic_widget (self->title, GTK_WIDGET (self));
      gtk_label_set_mnemonic_widget (self->subtitle, GTK_WIDGET (self));
      break;

    case PROP_ACTIVE:
      g_signal_handlers_block_by_func (self->enable_switch,
                                       cc_list_row_switch_active_cb, self);
      gtk_switch_set_active (self->enable_switch,
                             g_value_get_boolean (value));
      self->switch_active = g_value_get_boolean (value);
      g_signal_handlers_unblock_by_func (self->enable_switch,
                                         cc_list_row_switch_active_cb, self);
      break;

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

static void
cc_list_row_class_init (CcListRowClass *klass)
{
  GObjectClass   *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->get_property = cc_list_row_get_property;
  object_class->set_property = cc_list_row_set_property;

  properties[PROP_TITLE] =
    g_param_spec_string ("title",
                         "Title",
                         "List row primary title",
                         NULL,
                         G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);

  properties[PROP_SUBTITLE] =
    g_param_spec_string ("subtitle",
                         "Subtitle",
                         "List row primary subtitle",
                         NULL,
                         G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);

  properties[PROP_SECONDARY_LABEL] =
    g_param_spec_string ("secondary-label",
                         "Secondary Label",
                         "Set Secondary Label",
                         NULL,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  properties[PROP_ICON_NAME] =
    g_param_spec_string ("icon-name",
                         "Icon Name",
                         "Secondary Icon name",
                         NULL,
                         G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);

  properties[PROP_SHOW_SWITCH] =
    g_param_spec_boolean ("show-switch",
                          "Show Switch",
                          "Whether to show a switch at the end of row",
                          FALSE,
                          G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);

  properties[PROP_ACTIVE] =
    g_param_spec_boolean ("active",
                          "Active",
                          "The active state of the switch",
                          FALSE,
                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  properties[PROP_USE_UNDERLINE] =
    g_param_spec_boolean ("use-underline",
                          "Use underline",
                          "If set, text prefixed with underline shall be used as mnemonic",
                          FALSE,
                          G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, N_PROPS, properties);

  gtk_widget_class_set_template_from_resource (widget_class,
                                               "/org/gnome/control-center/"
                                               "common/cc-list-row.ui");

  gtk_widget_class_bind_template_child (widget_class, CcListRow, box);
  gtk_widget_class_bind_template_child (widget_class, CcListRow, title);
  gtk_widget_class_bind_template_child (widget_class, CcListRow, subtitle);
  gtk_widget_class_bind_template_child (widget_class, CcListRow, secondary_label);
  gtk_widget_class_bind_template_child (widget_class, CcListRow, icon);
  gtk_widget_class_bind_template_child (widget_class, CcListRow, enable_switch);

  gtk_widget_class_bind_template_callback (widget_class, cc_list_row_switch_active_cb);
}

static void
cc_list_row_init (CcListRow *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));
  g_signal_connect_object (self, "notify::parent",
                           G_CALLBACK (cc_list_row_parent_changed_cb),
                           self, G_CONNECT_SWAPPED);
}

void
cc_list_row_set_icon_name (CcListRow   *self,
                           const gchar *icon_name)
{
  g_return_if_fail (CC_IS_LIST_ROW (self));
  g_return_if_fail (!self->show_switch);

  if (icon_name)
    g_object_set (self->icon, "icon-name", icon_name, NULL);

  gtk_widget_set_visible (GTK_WIDGET (self->icon), icon_name != NULL);
}

void
cc_list_row_set_show_switch (CcListRow *self,
                             gboolean   show_switch)
{
  g_return_if_fail (CC_IS_LIST_ROW (self));

  self->show_switch = !!show_switch;

  gtk_widget_set_visible (GTK_WIDGET (self->enable_switch), self->show_switch);
  gtk_widget_set_visible (GTK_WIDGET (self->icon), !self->show_switch);
  gtk_widget_set_visible (GTK_WIDGET (self->secondary_label), !self->show_switch);
}

gboolean
cc_list_row_get_active (CcListRow *self)
{
  g_return_val_if_fail (CC_IS_LIST_ROW (self), FALSE);
  g_return_val_if_fail (self->show_switch, FALSE);

  return self->switch_active;
}

void
cc_list_row_activate (CcListRow *self)
{
  g_return_if_fail (CC_IS_LIST_ROW (self));
  g_return_if_fail (self->show_switch);

  self->switch_active = !self->switch_active;
  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACTIVE]);

  gtk_widget_activate (GTK_WIDGET (self->enable_switch));
}

void
cc_list_row_set_secondary_label (CcListRow   *self,
                                 const gchar *label)
{
  g_return_if_fail (CC_IS_LIST_ROW (self));
  g_return_if_fail (!self->show_switch);

  if (!label)
    label = "";

  if (g_str_equal (label, gtk_label_get_label (self->secondary_label)))
    return;

  gtk_label_set_text (self->secondary_label, label);
  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SECONDARY_LABEL]);
}

void
cc_list_row_set_secondary_markup (CcListRow   *self,
                                  const gchar *markup)
{
  g_return_if_fail (CC_IS_LIST_ROW (self));
  g_return_if_fail (!self->show_switch);

  if (!markup)
    markup = "";

  if (g_str_equal (markup, gtk_label_get_label (self->secondary_label)))
    return;

  gtk_label_set_markup (self->secondary_label, markup);
  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SECONDARY_LABEL]);
}