summaryrefslogtreecommitdiff
path: root/clients/tui/newt/nmt-newt-button-box.c
blob: d86a154cb25510a14858e7e013f0b8ac4fb6169d (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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2013 Red Hat, Inc.
 */

/**
 * SECTION:nmt-newt-button-box
 * @short_description: A container for #NmtNewtButtons
 *
 * #NmtNewtButtonBox is a container for creating and holding
 * #NmtNewtButtons.
 *
 * A button box can be either horizontally or vertically laid out, and
 * has two sections within it: the "start" (left or top) and "end"
 * (right or bottom). Buttons are added from left to right or top to bottom
 * within each of the two sections.
 */

#include "libnm-client-aux-extern/nm-default-client.h"

#include "nmt-newt-button-box.h"

#include "nmt-newt-button.h"

G_DEFINE_TYPE(NmtNewtButtonBox, nmt_newt_button_box, NMT_TYPE_NEWT_CONTAINER)

#define NMT_NEWT_BUTTON_BOX_GET_PRIVATE(o) \
    (G_TYPE_INSTANCE_GET_PRIVATE((o), NMT_TYPE_NEWT_BUTTON_BOX, NmtNewtButtonBoxPrivate))

typedef struct {
    NmtNewtButtonBoxOrientation orientation;
    GPtrArray *                 start_buttons, *end_buttons;
} NmtNewtButtonBoxPrivate;

enum {
    PROP_0,

    PROP_ORIENTATION,

    LAST_PROP
};

/**
 * NmtNewtButtonBoxOrientation:
 * @NMT_NEWT_BUTTON_BOX_HORIZONTAL: horizontal
 * @NMT_NEWT_BUTTON_BOX_VERTICAL: vertical
 *
 * The orientation of an #NmtNewtButtonBox
 */

/**
 * nmt_newt_button_box_new:
 * @orientation: the orientation
 *
 * Creates a new #NmtNewtButtonBox
 *
 * Returns: a new #NmtNewtButtonBox
 */
NmtNewtWidget *
nmt_newt_button_box_new(NmtNewtButtonBoxOrientation orientation)
{
    return g_object_new(NMT_TYPE_NEWT_BUTTON_BOX, "orientation", orientation, NULL);
}

static void
nmt_newt_button_box_init(NmtNewtButtonBox *bbox)
{
    NmtNewtButtonBoxPrivate *priv = NMT_NEWT_BUTTON_BOX_GET_PRIVATE(bbox);

    priv->start_buttons = g_ptr_array_new();
    priv->end_buttons   = g_ptr_array_new();
}

/**
 * nmt_newt_button_box_add_start:
 * @bbox: an #NmtNewtButtonBox
 * @label: the label for the newt button
 *
 * Creates a new #NmtNewtButton with the given @label, adds it
 * to the "start" section of @bbox, and returns the newly-created
 * button.
 *
 * Returns: the newly-created button, already added to @bbox
 */
NmtNewtWidget *
nmt_newt_button_box_add_start(NmtNewtButtonBox *bbox, const char *label)
{
    NmtNewtWidget *button;

    button = nmt_newt_button_new(label);
    nmt_newt_button_box_add_widget_start(bbox, button);
    return button;
}

/**
 * nmt_newt_button_box_add_widget_start:
 * @bbox: an #NmtNewtButtonBox
 * @widget: the #NmtNewtWidget to add
 *
 * Adds the given widget to the "start" section of @bbox.
 */
void
nmt_newt_button_box_add_widget_start(NmtNewtButtonBox *bbox, NmtNewtWidget *widget)
{
    NmtNewtButtonBoxPrivate *priv = NMT_NEWT_BUTTON_BOX_GET_PRIVATE(bbox);

    NMT_NEWT_CONTAINER_CLASS(nmt_newt_button_box_parent_class)
        ->add(NMT_NEWT_CONTAINER(bbox), widget);
    g_ptr_array_add(priv->start_buttons, widget);
}

/**
 * nmt_newt_button_box_add_end:
 * @bbox: an #NmtNewtButtonBox
 * @label: the label for the newt button
 *
 * Creates a new #NmtNewtButton with the given @label, adds it
 * to the "end" section of @bbox, and returns the newly-created
 * button.
 *
 * Returns: the newly-created button, already added to @bbox
 */
NmtNewtWidget *
nmt_newt_button_box_add_end(NmtNewtButtonBox *bbox, const char *label)
{
    NmtNewtWidget *button;

    button = nmt_newt_button_new(label);
    nmt_newt_button_box_add_widget_end(bbox, button);
    return button;
}

/**
 * nmt_newt_button_box_add_widget_end:
 * @bbox: an #NmtNewtButtonBox
 * @widget: the #NmtNewtWidget to add
 *
 * Adds the given widget to the "end" section of @bbox.
 */
void
nmt_newt_button_box_add_widget_end(NmtNewtButtonBox *bbox, NmtNewtWidget *widget)
{
    NmtNewtButtonBoxPrivate *priv = NMT_NEWT_BUTTON_BOX_GET_PRIVATE(bbox);

    NMT_NEWT_CONTAINER_CLASS(nmt_newt_button_box_parent_class)
        ->add(NMT_NEWT_CONTAINER(bbox), widget);
    g_ptr_array_add(priv->end_buttons, widget);
}

static void
nmt_newt_button_box_remove(NmtNewtContainer *container, NmtNewtWidget *child)
{
    NmtNewtButtonBoxPrivate *priv = NMT_NEWT_BUTTON_BOX_GET_PRIVATE(container);
    int                      i;

    NMT_NEWT_CONTAINER_CLASS(nmt_newt_button_box_parent_class)->remove(container, child);

    for (i = 0; i < priv->start_buttons->len; i++) {
        if (priv->start_buttons->pdata[i] == (gpointer) child) {
            g_ptr_array_remove_index(priv->start_buttons, i);
            return;
        }
    }
    for (i = 0; i < priv->end_buttons->len; i++) {
        if (priv->end_buttons->pdata[i] == (gpointer) child) {
            g_ptr_array_remove_index(priv->end_buttons, i);
            return;
        }
    }
}

static void
add_buttons(GPtrArray *buttons, GPtrArray *cos)
{
    NmtNewtWidget *child;
    newtComponent *child_cos;
    int            i, c;

    for (i = 0; i < buttons->len; i++) {
        child = buttons->pdata[i];

        if (!nmt_newt_widget_get_visible(child))
            continue;

        child_cos = nmt_newt_widget_get_components(child);
        for (c = 0; child_cos[c]; c++)
            g_ptr_array_add(cos, child_cos[c]);
        g_free(child_cos);
    }
}

static newtComponent *
nmt_newt_button_box_get_components(NmtNewtWidget *widget)
{
    NmtNewtButtonBoxPrivate *priv = NMT_NEWT_BUTTON_BOX_GET_PRIVATE(widget);
    GPtrArray *              cos;

    cos = g_ptr_array_new();
    add_buttons(priv->start_buttons, cos);
    add_buttons(priv->end_buttons, cos);
    g_ptr_array_add(cos, NULL);

    return (newtComponent *) g_ptr_array_free(cos, FALSE);
}

static void
size_request_buttons(NmtNewtButtonBox *bbox, GPtrArray *buttons, int *width, int *height)
{
    NmtNewtButtonBoxPrivate *priv = NMT_NEWT_BUTTON_BOX_GET_PRIVATE(bbox);
    int                      child_width, child_height;
    int                      i;

    for (i = 0; i < buttons->len; i++) {
        NmtNewtWidget *child = buttons->pdata[i];

        nmt_newt_widget_size_request(child, &child_width, &child_height);
        if (priv->orientation == NMT_NEWT_BUTTON_BOX_HORIZONTAL) {
            *width += child_width;
            if (i > 0)
                *width += 1;
            *height = MAX(*height, child_height);
        } else {
            *height += child_height;
            if (i > 0)
                *height += 1;
            *width = MAX(*width, child_width);
        }
    }
}

static void
nmt_newt_button_box_size_request(NmtNewtWidget *widget, int *width, int *height)
{
    NmtNewtButtonBox *       bbox = NMT_NEWT_BUTTON_BOX(widget);
    NmtNewtButtonBoxPrivate *priv = NMT_NEWT_BUTTON_BOX_GET_PRIVATE(widget);

    *width = *height = 0;
    size_request_buttons(bbox, priv->start_buttons, width, height);
    size_request_buttons(bbox, priv->end_buttons, width, height);

    if (priv->orientation == NMT_NEWT_BUTTON_BOX_HORIZONTAL)
        *width += 1;
    else
        *height += 1;
}

static void
nmt_newt_button_box_size_allocate(NmtNewtWidget *widget, int x, int y, int width, int height)
{
    NmtNewtButtonBoxPrivate *priv = NMT_NEWT_BUTTON_BOX_GET_PRIVATE(widget);
    NmtNewtWidget *          child;
    int                      child_x, child_y, child_width, child_height;
    int                      i;

    child_x = x;
    child_y = y;
    for (i = 0; i < priv->start_buttons->len; i++) {
        child = priv->start_buttons->pdata[i];
        nmt_newt_widget_size_request(child, &child_width, &child_height);

        if (priv->orientation == NMT_NEWT_BUTTON_BOX_HORIZONTAL) {
            nmt_newt_widget_size_allocate(child, child_x, child_y, child_width, child_height);
            child_x += child_width + 1;
        } else {
            nmt_newt_widget_size_allocate(child, child_x, child_y, child_width, child_height);
            child_y += child_height + 1;
        }
    }

    if (priv->orientation == NMT_NEWT_BUTTON_BOX_HORIZONTAL)
        child_x = x + width;
    else
        child_y = y + height;

    for (i = priv->end_buttons->len - 1; i >= 0; i--) {
        child = priv->end_buttons->pdata[i];
        nmt_newt_widget_size_request(child, &child_width, &child_height);

        if (priv->orientation == NMT_NEWT_BUTTON_BOX_HORIZONTAL) {
            nmt_newt_widget_size_allocate(child,
                                          child_x - child_width,
                                          child_y,
                                          child_width,
                                          child_height);
            child_x -= child_width + 1;
        } else {
            nmt_newt_widget_size_allocate(child,
                                          child_x,
                                          child_y - child_height,
                                          child_width,
                                          child_height);
            child_y -= child_height + 1;
        }
    }
}

static void
nmt_newt_button_box_set_property(GObject *     object,
                                 guint         prop_id,
                                 const GValue *value,
                                 GParamSpec *  pspec)
{
    NmtNewtButtonBoxPrivate *priv = NMT_NEWT_BUTTON_BOX_GET_PRIVATE(object);

    switch (prop_id) {
    case PROP_ORIENTATION:
        priv->orientation = g_value_get_int(value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
nmt_newt_button_box_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
    NmtNewtButtonBoxPrivate *priv = NMT_NEWT_BUTTON_BOX_GET_PRIVATE(object);

    switch (prop_id) {
    case PROP_ORIENTATION:
        g_value_set_int(value, priv->orientation);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
nmt_newt_button_box_class_init(NmtNewtButtonBoxClass *bbox_class)
{
    GObjectClass *         object_class    = G_OBJECT_CLASS(bbox_class);
    NmtNewtWidgetClass *   widget_class    = NMT_NEWT_WIDGET_CLASS(bbox_class);
    NmtNewtContainerClass *container_class = NMT_NEWT_CONTAINER_CLASS(bbox_class);

    g_type_class_add_private(bbox_class, sizeof(NmtNewtButtonBoxPrivate));

    object_class->get_property = nmt_newt_button_box_get_property;
    object_class->set_property = nmt_newt_button_box_set_property;

    widget_class->get_components = nmt_newt_button_box_get_components;
    widget_class->size_request   = nmt_newt_button_box_size_request;
    widget_class->size_allocate  = nmt_newt_button_box_size_allocate;

    container_class->remove = nmt_newt_button_box_remove;

    g_object_class_install_property(
        object_class,
        PROP_ORIENTATION,
        g_param_spec_int("orientation",
                         "",
                         "",
                         0,
                         G_MAXINT,
                         0,
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
}