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

/**
 * SECTION:nmt-newt-stack
 * @short_description: A stack of alternative widgets
 *
 * #NmtNewtStack implements a stack of widgets, only one of which is
 * visible at any time.
 *
 * The height and width of the widget is determined only by its
 * visible child. Likewise, the widget's #NmtNewtWidget:valid is
 * determined only by the validity of its visible child, not its other
 * children.
 */

#include "nm-default.h"

#include "nmt-newt-stack.h"

G_DEFINE_TYPE(NmtNewtStack, nmt_newt_stack, NMT_TYPE_NEWT_CONTAINER)

#define NMT_NEWT_STACK_GET_PRIVATE(o) \
    (G_TYPE_INSTANCE_GET_PRIVATE((o), NMT_TYPE_NEWT_STACK, NmtNewtStackPrivate))

typedef struct {
    GPtrArray *children;
    GPtrArray *ids;

    guint active;
} NmtNewtStackPrivate;

enum {
    PROP_0,
    PROP_ACTIVE,
    PROP_ACTIVE_ID,

    LAST_PROP
};

/**
 * nmt_newt_stack_new:
 *
 * Creates a new #NmtNewtStack
 *
 * Returns: a new #NmtNewtStack
 */
NmtNewtWidget *
nmt_newt_stack_new(void)
{
    return g_object_new(NMT_TYPE_NEWT_STACK, NULL);
}

static void
nmt_newt_stack_init(NmtNewtStack *stack)
{
    NmtNewtStackPrivate *priv = NMT_NEWT_STACK_GET_PRIVATE(stack);

    priv->children = g_ptr_array_new();
    priv->ids      = g_ptr_array_new_with_free_func(g_free);
}

static void
nmt_newt_stack_finalize(GObject *object)
{
    NmtNewtStackPrivate *priv = NMT_NEWT_STACK_GET_PRIVATE(object);

    g_ptr_array_unref(priv->children);
    g_ptr_array_unref(priv->ids);

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

static newtComponent *
nmt_newt_stack_get_components(NmtNewtWidget *widget)
{
    NmtNewtStackPrivate *priv = NMT_NEWT_STACK_GET_PRIVATE(widget);

    if (priv->active > priv->children->len)
        return NULL;

    return nmt_newt_widget_get_components(priv->children->pdata[priv->active]);
}

static void
nmt_newt_stack_size_request(NmtNewtWidget *widget, int *width, int *height)
{
    NmtNewtStack *       stack = NMT_NEWT_STACK(widget);
    NmtNewtStackPrivate *priv  = NMT_NEWT_STACK_GET_PRIVATE(stack);
    int                  i, child_width, child_height;

    if (priv->active > priv->children->len) {
        *width = *height = 0;
        return;
    }

    /* We size-request all pages so that embedded NmtPageGrids will
	 * participate in their size-grouping (so that switching pages
	 * won't result in the column widths changing).
	 */
    for (i = 0; i < priv->children->len; i++) {
        nmt_newt_widget_size_request(priv->children->pdata[i], &child_width, &child_height);
        if (i == priv->active) {
            *width  = child_width;
            *height = child_height;
        }
    }
}

static void
nmt_newt_stack_size_allocate(NmtNewtWidget *widget, int x, int y, int width, int height)
{
    NmtNewtStackPrivate *priv = NMT_NEWT_STACK_GET_PRIVATE(widget);

    if (priv->active > priv->children->len)
        return;

    nmt_newt_widget_size_allocate(priv->children->pdata[priv->active], x, y, width, height);
}

/**
 * nmt_newt_stack_add:
 * @stack: an #NmtNewtStack
 * @id: the ID for the new page
 * @widget: the widget to add
 *
 * Adds @widget to @stack with the given @id.
 */
void
nmt_newt_stack_add(NmtNewtStack *stack, const char *id, NmtNewtWidget *widget)
{
    NmtNewtStackPrivate *priv = NMT_NEWT_STACK_GET_PRIVATE(stack);

    g_ptr_array_add(priv->children, widget);
    g_ptr_array_add(priv->ids, g_strdup(id));

    NMT_NEWT_CONTAINER_CLASS(nmt_newt_stack_parent_class)->add(NMT_NEWT_CONTAINER(stack), widget);
}

static void
nmt_newt_stack_remove(NmtNewtContainer *container, NmtNewtWidget *widget)
{
    NmtNewtStack *       stack = NMT_NEWT_STACK(container);
    NmtNewtStackPrivate *priv  = NMT_NEWT_STACK_GET_PRIVATE(stack);
    int                  i;

    NMT_NEWT_CONTAINER_CLASS(nmt_newt_stack_parent_class)->remove(container, widget);

    for (i = 0; i < priv->children->len; i++) {
        if (priv->children->pdata[i] == widget) {
            g_ptr_array_remove_index(priv->children, i);
            g_ptr_array_remove_index(priv->ids, i);
            return;
        }
    }
}

static void
nmt_newt_stack_child_validity_changed(NmtNewtContainer *container, NmtNewtWidget *widget)
{
    NmtNewtStackPrivate *priv = NMT_NEWT_STACK_GET_PRIVATE(container);

    if (priv->active > priv->children->len)
        return;

    if (priv->children->pdata[priv->active] == (gpointer) widget) {
        NMT_NEWT_CONTAINER_CLASS(nmt_newt_stack_parent_class)
            ->child_validity_changed(container, widget);
    }
}

/**
 * nmt_newt_stack_set_active:
 * @stack: an #NmtNewtStack
 * @active: the index of the new active page
 *
 * Sets the active page on @stack to @active.
 */
void
nmt_newt_stack_set_active(NmtNewtStack *stack, guint active)
{
    NmtNewtStackPrivate *priv = NMT_NEWT_STACK_GET_PRIVATE(stack);

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

    priv->active = active;
    g_object_notify(G_OBJECT(stack), "active");
    g_object_notify(G_OBJECT(stack), "active-id");
    nmt_newt_widget_needs_rebuild(NMT_NEWT_WIDGET(stack));
}

/**
 * nmt_newt_stack_get_active:
 * @stack: an #NmtNewtStack
 *
 * Gets the index of the active page on @stack
 *
 * Returns: the index of the active page on @stack
 */
guint
nmt_newt_stack_get_active(NmtNewtStack *stack)
{
    NmtNewtStackPrivate *priv = NMT_NEWT_STACK_GET_PRIVATE(stack);

    return priv->active;
}

/**
 * nmt_newt_stack_set_active_id:
 * @stack: an #NmtNewtStack
 * @active_id: the ID of the new active page
 *
 * Sets the active page on @stack to @active_id.
 */
void
nmt_newt_stack_set_active_id(NmtNewtStack *stack, const char *id)
{
    NmtNewtStackPrivate *priv = NMT_NEWT_STACK_GET_PRIVATE(stack);
    int                  i;

    if (!g_strcmp0(priv->ids->pdata[priv->active], id))
        return;

    for (i = 0; i < priv->ids->len; i++) {
        if (!g_strcmp0(priv->ids->pdata[i], id)) {
            priv->active = i;
            g_object_notify(G_OBJECT(stack), "active");
            g_object_notify(G_OBJECT(stack), "active-id");
            nmt_newt_widget_needs_rebuild(NMT_NEWT_WIDGET(stack));
            return;
        }
    }
}

/**
 * nmt_newt_stack_get_active_id:
 * @stack: an #NmtNewtStack
 *
 * Gets the ID of the active page on @stack
 *
 * Returns: the ID of the active page on @stack
 */
const char *
nmt_newt_stack_get_active_id(NmtNewtStack *stack)
{
    NmtNewtStackPrivate *priv = NMT_NEWT_STACK_GET_PRIVATE(stack);

    if (priv->active > priv->children->len)
        return NULL;

    return priv->ids->pdata[priv->active];
}

static void
nmt_newt_stack_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
    NmtNewtStack *stack = NMT_NEWT_STACK(object);

    switch (prop_id) {
    case PROP_ACTIVE:
        nmt_newt_stack_set_active(stack, g_value_get_uint(value));
        break;
    case PROP_ACTIVE_ID:
        nmt_newt_stack_set_active_id(stack, g_value_get_string(value));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
nmt_newt_stack_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
    NmtNewtStack *stack = NMT_NEWT_STACK(object);

    switch (prop_id) {
    case PROP_ACTIVE:
        g_value_set_uint(value, nmt_newt_stack_get_active(stack));
        break;
    case PROP_ACTIVE_ID:
        g_value_set_string(value, nmt_newt_stack_get_active_id(stack));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
nmt_newt_stack_class_init(NmtNewtStackClass *stack_class)
{
    GObjectClass *         object_class    = G_OBJECT_CLASS(stack_class);
    NmtNewtWidgetClass *   widget_class    = NMT_NEWT_WIDGET_CLASS(stack_class);
    NmtNewtContainerClass *container_class = NMT_NEWT_CONTAINER_CLASS(stack_class);

    g_type_class_add_private(stack_class, sizeof(NmtNewtStackPrivate));

    /* virtual methods */
    object_class->set_property = nmt_newt_stack_set_property;
    object_class->get_property = nmt_newt_stack_get_property;
    object_class->finalize     = nmt_newt_stack_finalize;

    widget_class->get_components = nmt_newt_stack_get_components;
    widget_class->size_request   = nmt_newt_stack_size_request;
    widget_class->size_allocate  = nmt_newt_stack_size_allocate;

    container_class->remove                 = nmt_newt_stack_remove;
    container_class->child_validity_changed = nmt_newt_stack_child_validity_changed;

    /**
	 * NmtNewtStack:active:
	 *
	 * The index of the active page
	 */
    g_object_class_install_property(object_class,
                                    PROP_ACTIVE,
                                    g_param_spec_uint("active",
                                                      "",
                                                      "",
                                                      0,
                                                      G_MAXUINT,
                                                      0,
                                                      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
    /**
	 * NmtNewtStack:active-id:
	 *
	 * The ID of the active page
	 */
    g_object_class_install_property(
        object_class,
        PROP_ACTIVE_ID,
        g_param_spec_string("active-id", "", "", NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}