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

/**
 * SECTION:nmt-editor-section:
 * @short_description: A section of the #NmtEditor
 *
 * #NmtEditorSection is the abstract base class for #NmtEditor sections.
 */

#include "nm-default.h"

#include "nmt-editor-section.h"
#include "nmt-newt-toggle-button.h"

G_DEFINE_TYPE(NmtEditorSection, nmt_editor_section, NMT_TYPE_NEWT_SECTION)

#define NMT_EDITOR_SECTION_GET_PRIVATE(o) \
    (G_TYPE_INSTANCE_GET_PRIVATE((o), NMT_TYPE_EDITOR_SECTION, NmtEditorSectionPrivate))

typedef struct {
    NmtEditorGrid *header, *body;
    char *         title;
    NmtNewtWidget *header_widget;
    NmtNewtWidget *toggle;

    gboolean show_by_default;
} NmtEditorSectionPrivate;

enum {
    PROP_0,

    PROP_TITLE,
    PROP_SHOW_BY_DEFAULT,
    PROP_HEADER_WIDGET,

    LAST_PROP
};

/**
 * nmt_editor_section_new:
 * @title: the section title
 * @header_widget: (allow-none): the widget to show next to the title
 * @show_by_default: whether the section should be open by default
 *
 * Creates a new #NmtEditorSection.
 *
 * Returns: a new #NmtEditorSection
 */
NmtEditorSection *
nmt_editor_section_new(const char *title, NmtNewtWidget *header_widget, gboolean show_by_default)
{
    return g_object_new(NMT_TYPE_EDITOR_SECTION,
                        "title",
                        title,
                        "header-widget",
                        header_widget,
                        "show-by-default",
                        show_by_default,
                        NULL);
}

static void
rebuild_header(NmtEditorSection *section)
{
    NmtEditorSectionPrivate *priv = NMT_EDITOR_SECTION_GET_PRIVATE(section);

    /* Removing any widget in an NmtEditorGrid removes its whole row, so we can
     * remove the existing title/widget/toggle by asking to remove toggle.
     */
    nmt_newt_container_remove(NMT_NEWT_CONTAINER(priv->header), priv->toggle);

    nmt_editor_grid_append(priv->header, priv->title, priv->header_widget, priv->toggle);
    nmt_editor_grid_set_row_flags(priv->header,
                                  priv->toggle,
                                  NMT_EDITOR_GRID_ROW_LABEL_ALIGN_LEFT
                                      | NMT_EDITOR_GRID_ROW_EXTRA_ALIGN_RIGHT);
}

static void
nmt_editor_section_init(NmtEditorSection *section)
{
    NmtEditorSectionPrivate *priv = NMT_EDITOR_SECTION_GET_PRIVATE(section);

    priv->header = NMT_EDITOR_GRID(nmt_editor_grid_new());
    priv->body   = NMT_EDITOR_GRID(nmt_editor_grid_new());
    priv->toggle = nmt_newt_toggle_button_new(_("Hide"), _("Show"));
    g_object_ref_sink(priv->toggle);

    nmt_newt_section_set_header(NMT_NEWT_SECTION(section), NMT_NEWT_WIDGET(priv->header));
    nmt_newt_section_set_body(NMT_NEWT_SECTION(section), NMT_NEWT_WIDGET(priv->body));

    g_object_bind_property(priv->toggle, "active", section, "open", G_BINDING_SYNC_CREATE);
}

static void
nmt_editor_section_finalize(GObject *object)
{
    NmtEditorSectionPrivate *priv = NMT_EDITOR_SECTION_GET_PRIVATE(object);

    g_free(priv->title);
    g_clear_object(&priv->header_widget);
    g_clear_object(&priv->toggle);

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

/**
 * nmt_editor_section_get_header_widget:
 * @section: the #NmtEditorSection
 *
 * Gets the section's header widget.
 *
 * Returns: the section's header widget.
 */
NmtNewtWidget *
nmt_editor_section_get_header_widget(NmtEditorSection *section)
{
    NmtEditorSectionPrivate *priv = NMT_EDITOR_SECTION_GET_PRIVATE(section);

    return priv->header_widget;
}

/**
 * nmt_editor_section_get_body:
 * @section: the #NmtEditorSection
 *
 * Gets the section's body grid, so that you can add things to it.
 *
 * Returns: the #NmtEditorGrid used for the section body
 */
NmtEditorGrid *
nmt_editor_section_get_body(NmtEditorSection *section)
{
    NmtEditorSectionPrivate *priv = NMT_EDITOR_SECTION_GET_PRIVATE(section);

    return priv->body;
}

/**
 * nmt_editor_section_get_title:
 * @section: the #NmtEditorSection
 *
 * Gets the section's title.
 *
 * Returns: the section's title
 */
const char *
nmt_editor_section_get_title(NmtEditorSection *section)
{
    NmtEditorSectionPrivate *priv = NMT_EDITOR_SECTION_GET_PRIVATE(section);

    return priv->title;
}

static void
nmt_editor_section_set_property(GObject *     object,
                                guint         prop_id,
                                const GValue *value,
                                GParamSpec *  pspec)
{
    NmtEditorSection *       section = NMT_EDITOR_SECTION(object);
    NmtEditorSectionPrivate *priv    = NMT_EDITOR_SECTION_GET_PRIVATE(section);

    switch (prop_id) {
    case PROP_TITLE:
        priv->title = g_value_dup_string(value);
        rebuild_header(section);
        break;
    case PROP_SHOW_BY_DEFAULT:
        priv->show_by_default = g_value_get_boolean(value);
        nmt_newt_toggle_button_set_active(NMT_NEWT_TOGGLE_BUTTON(priv->toggle),
                                          priv->show_by_default);
        break;
    case PROP_HEADER_WIDGET:
        priv->header_widget = g_value_get_object(value);
        if (priv->header_widget)
            g_object_ref_sink(priv->header_widget);
        rebuild_header(section);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
nmt_editor_section_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
    NmtEditorSectionPrivate *priv = NMT_EDITOR_SECTION_GET_PRIVATE(object);

    switch (prop_id) {
    case PROP_TITLE:
        g_value_set_string(value, priv->title);
        break;
    case PROP_SHOW_BY_DEFAULT:
        g_value_set_boolean(value, priv->show_by_default);
        break;
    case PROP_HEADER_WIDGET:
        g_value_set_object(value, priv->header_widget);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
nmt_editor_section_class_init(NmtEditorSectionClass *section_class)
{
    GObjectClass *object_class = G_OBJECT_CLASS(section_class);

    g_type_class_add_private(section_class, sizeof(NmtEditorSectionPrivate));

    /* virtual methods */
    object_class->set_property = nmt_editor_section_set_property;
    object_class->get_property = nmt_editor_section_get_property;
    object_class->finalize     = nmt_editor_section_finalize;

    /* properties */

    /**
     * NmtEditorSection:title:
     *
     * The section's title.
     */
    g_object_class_install_property(
        object_class,
        PROP_TITLE,
        g_param_spec_string("title",
                            "",
                            "",
                            NULL,
                            G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

    /**
     * NmtEditorSection:show-by-default:
     *
     * Whether the section should be expanded by default.
     */
    g_object_class_install_property(
        object_class,
        PROP_SHOW_BY_DEFAULT,
        g_param_spec_boolean("show-by-default",
                             "",
                             "",
                             TRUE,
                             G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

    /**
     * NmtEditorSection:header-widget:
     *
     * The widget (if any) that appears between the section title and its toggle
     * button.
     */
    g_object_class_install_property(
        object_class,
        PROP_HEADER_WIDGET,
        g_param_spec_object("header-widget",
                            "",
                            "",
                            NMT_TYPE_NEWT_WIDGET,
                            G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}