summaryrefslogtreecommitdiff
path: root/clients/tui/newt/nmt-newt-checkbox.c
blob: 9e67dbd2f5820f1b2f3234a3326e9fb05d218f3f (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2013 Red Hat, Inc.
 */

/**
 * SECTION:nmt-newt-checkbox
 * @short_description: Checkboxes
 *
 * #NmtNewtCheckbox implements a checkbox widget.
 */

#include "nm-default.h"

#include "nmt-newt-checkbox.h"
#include "nmt-newt-utils.h"

G_DEFINE_TYPE(NmtNewtCheckbox, nmt_newt_checkbox, NMT_TYPE_NEWT_COMPONENT)

#define NMT_NEWT_CHECKBOX_GET_PRIVATE(o) \
    (G_TYPE_INSTANCE_GET_PRIVATE((o), NMT_TYPE_NEWT_CHECKBOX, NmtNewtCheckboxPrivate))

typedef struct {
    char *   label_lc;
    gboolean active;
} NmtNewtCheckboxPrivate;

enum {
    PROP_0,
    PROP_LABEL,
    PROP_ACTIVE,

    LAST_PROP
};

#define CHECKBOX_INACTIVE ' '
#define CHECKBOX_ACTIVE   'X'
#define CHECKBOX_STATES   " X"

/**
 * nmt_newt_checkbox_new:
 * @label: the (initial) checkbox label
 *
 * Creates a new checkbox.
 *
 * Returns: a new #NmtNewtCheckbox
 */
NmtNewtWidget *
nmt_newt_checkbox_new(const char *label)
{
    return g_object_new(NMT_TYPE_NEWT_CHECKBOX, "label", label, NULL);
}

/**
 * nmt_newt_checkbox_set_active:
 * @checkbox: an #NmtNewtCheckbox
 * @active: whether @checkbox should be checked
 *
 * Updates @checkbox's checked state
 */
void
nmt_newt_checkbox_set_active(NmtNewtCheckbox *checkbox, gboolean active)
{
    NmtNewtCheckboxPrivate *priv = NMT_NEWT_CHECKBOX_GET_PRIVATE(checkbox);
    newtComponent           co;

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

    priv->active = active;

    co = nmt_newt_component_get_component(NMT_NEWT_COMPONENT(checkbox));
    if (co)
        newtCheckboxSetValue(co, priv->active ? CHECKBOX_ACTIVE : CHECKBOX_INACTIVE);

    g_object_notify(G_OBJECT(checkbox), "active");
}

/**
 * nmt_newt_checkbox_get_active:
 * @checkbox: an #NmtNewtCheckbox
 *
 * Gets @checkbox's checked state
 *
 * Returns: @checkbox's checked state
 */
gboolean
nmt_newt_checkbox_get_active(NmtNewtCheckbox *checkbox)
{
    NmtNewtCheckboxPrivate *priv = NMT_NEWT_CHECKBOX_GET_PRIVATE(checkbox);

    return priv->active;
}

static void
nmt_newt_checkbox_init(NmtNewtCheckbox *checkbox)
{}

static void
nmt_newt_checkbox_finalize(GObject *object)
{
    NmtNewtCheckboxPrivate *priv = NMT_NEWT_CHECKBOX_GET_PRIVATE(object);

    g_free(priv->label_lc);

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

static void
checkbox_toggled_callback(newtComponent co, void *checkbox)
{
    NmtNewtCheckboxPrivate *priv = NMT_NEWT_CHECKBOX_GET_PRIVATE(checkbox);
    gboolean                active;

    active = (newtCheckboxGetValue(co) == CHECKBOX_ACTIVE);
    if (active != priv->active) {
        priv->active = active;
        g_object_notify(checkbox, "active");
    }
}

static newtComponent
nmt_newt_checkbox_build_component(NmtNewtComponent *component, gboolean sensitive)
{
    NmtNewtCheckboxPrivate *priv = NMT_NEWT_CHECKBOX_GET_PRIVATE(component);
    newtComponent           co;

    co = newtCheckbox(-1,
                      -1,
                      priv->label_lc,
                      priv->active ? CHECKBOX_ACTIVE : CHECKBOX_INACTIVE,
                      CHECKBOX_STATES,
                      NULL);
    if (!sensitive)
        newtCheckboxSetFlags(co, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);
    newtComponentAddCallback(co, checkbox_toggled_callback, component);
    return co;
}

static void
nmt_newt_checkbox_set_property(GObject *     object,
                               guint         prop_id,
                               const GValue *value,
                               GParamSpec *  pspec)
{
    NmtNewtCheckbox *       checkbox = NMT_NEWT_CHECKBOX(object);
    NmtNewtCheckboxPrivate *priv     = NMT_NEWT_CHECKBOX_GET_PRIVATE(object);

    switch (prop_id) {
    case PROP_LABEL:
        g_free(priv->label_lc);
        priv->label_lc = nmt_newt_locale_from_utf8(g_value_get_string(value));
        break;
    case PROP_ACTIVE:
        nmt_newt_checkbox_set_active(checkbox, g_value_get_boolean(value));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
nmt_newt_checkbox_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
    NmtNewtCheckboxPrivate *priv = NMT_NEWT_CHECKBOX_GET_PRIVATE(object);

    switch (prop_id) {
    case PROP_LABEL:
        g_value_take_string(value, nmt_newt_locale_to_utf8(priv->label_lc));
        break;
    case PROP_ACTIVE:
        g_value_set_boolean(value, priv->active);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
nmt_newt_checkbox_class_init(NmtNewtCheckboxClass *checkbox_class)
{
    GObjectClass *         object_class    = G_OBJECT_CLASS(checkbox_class);
    NmtNewtComponentClass *component_class = NMT_NEWT_COMPONENT_CLASS(checkbox_class);

    g_type_class_add_private(checkbox_class, sizeof(NmtNewtCheckboxPrivate));

    /* virtual methods */
    object_class->set_property = nmt_newt_checkbox_set_property;
    object_class->get_property = nmt_newt_checkbox_get_property;
    object_class->finalize     = nmt_newt_checkbox_finalize;

    component_class->build_component = nmt_newt_checkbox_build_component;

    /**
	 * NmtNewtCheckbox:label:
	 *
	 * The checkbox's label
	 */
    g_object_class_install_property(
        object_class,
        PROP_LABEL,
        g_param_spec_string("label", "", "", NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
    /**
	 * NmtNewtCheckbox:active:
	 *
	 * The checkbox's checked state
	 */
    g_object_class_install_property(
        object_class,
        PROP_ACTIVE,
        g_param_spec_boolean("active", "", "", FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}