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

/**
 * SECTION:nmt-newt-component
 * @short_description: Base class for widgets that wrap #newtComponents
 *
 * #NmtNewtComponent is the abstract class for #NmtNewtWidgets that
 * wrap a (single) #newtComponent.
 */

#include "nm-default.h"

#include "nmt-newt-component.h"
#include "nmt-newt-form.h"
#include "nmt-newt-hacks.h"

G_DEFINE_ABSTRACT_TYPE(NmtNewtComponent, nmt_newt_component, NMT_TYPE_NEWT_WIDGET)

#define NMT_NEWT_COMPONENT_GET_PRIVATE(o) \
    (G_TYPE_INSTANCE_GET_PRIVATE((o), NMT_TYPE_NEWT_COMPONENT, NmtNewtComponentPrivate))

typedef struct {
    newtComponent co;
    gboolean      own_component;
    gboolean      sensitive;
} NmtNewtComponentPrivate;

enum {
    PROP_0,

    PROP_COMPONENT,
    PROP_SENSITIVE,

    LAST_PROP
};

static void
nmt_newt_component_init(NmtNewtComponent *component)
{
    NmtNewtComponentPrivate *priv = NMT_NEWT_COMPONENT_GET_PRIVATE(component);

    priv->sensitive = TRUE;
}

static void
nmt_newt_component_unrealize(NmtNewtWidget *widget)
{
    NmtNewtComponentPrivate *priv = NMT_NEWT_COMPONENT_GET_PRIVATE(widget);

    if (!priv->co)
        return;

    newtComponentAddCallback(priv->co, NULL, NULL);
    newtComponentAddDestroyCallback(priv->co, NULL, NULL);

    if (priv->own_component)
        newtComponentDestroy(priv->co);
    priv->co = NULL;
}

static void
component_destroy_callback(newtComponent co, void *component)
{
    NmtNewtComponentPrivate *priv = NMT_NEWT_COMPONENT_GET_PRIVATE(component);

    priv->own_component = FALSE;
    nmt_newt_widget_unrealize(component);
    nmt_newt_widget_needs_rebuild(component);
}

static void
nmt_newt_component_realize(NmtNewtWidget *widget)
{
    NmtNewtComponentPrivate *priv = NMT_NEWT_COMPONENT_GET_PRIVATE(widget);

    nmt_newt_component_unrealize(widget);
    priv->co = NMT_NEWT_COMPONENT_GET_CLASS(widget)->build_component(NMT_NEWT_COMPONENT(widget),
                                                                     priv->sensitive);
    priv->own_component = TRUE;
    if (!priv->sensitive)
        newtComponentTakesFocus(priv->co, FALSE);
    newtComponentAddDestroyCallback(priv->co, component_destroy_callback, widget);
}

static newtComponent *
nmt_newt_component_get_components(NmtNewtWidget *widget)
{
    NmtNewtComponentPrivate *priv = NMT_NEWT_COMPONENT_GET_PRIVATE(widget);
    newtComponent *          cos;

    priv->own_component = FALSE;
    cos                 = g_new0(newtComponent, 2);
    cos[0]              = priv->co;
    return cos;
}

static NmtNewtWidget *
nmt_newt_component_find_component(NmtNewtWidget *widget, newtComponent co)
{
    NmtNewtComponentPrivate *priv = NMT_NEWT_COMPONENT_GET_PRIVATE(widget);

    if (co == priv->co)
        return widget;
    else
        return NULL;
}

static void
nmt_newt_component_size_request(NmtNewtWidget *widget, int *width, int *height)
{
    NmtNewtComponentPrivate *priv = NMT_NEWT_COMPONENT_GET_PRIVATE(widget);

    newtComponentGetSize(priv->co, width, height);
}

static void
nmt_newt_component_size_allocate(NmtNewtWidget *widget, int x, int y, int width, int height)
{
    NmtNewtComponentPrivate *priv = NMT_NEWT_COMPONENT_GET_PRIVATE(widget);
    newtGrid                 grid;

    /* You can't directly place a newtComponent, so we create a newtGrid,
	 * position the component within that, and then place the grid.
	 */
    grid = newtCreateGrid(1, 1);
    newtGridSetField(grid,
                     0,
                     0,
                     NEWT_GRID_COMPONENT,
                     priv->co,
                     x,
                     y,
                     0,
                     0,
                     NEWT_ANCHOR_LEFT | NEWT_ANCHOR_TOP,
                     0);
    newtGridPlace(grid, 0, 0);
    newtGridFree(grid, FALSE);
}

static newtComponent
nmt_newt_component_get_focus_component(NmtNewtWidget *widget)
{
    NmtNewtComponentPrivate *priv = NMT_NEWT_COMPONENT_GET_PRIVATE(widget);

    return priv->co;
}

/**
 * nmt_newt_component_get_component:
 * @component: an #NmtNewtComponent
 *
 * A simpler version of nmt_newt_widget_get_components() for the
 * single-component case. Also, unlike
 * nmt_newt_widget_get_component(), this does not realize the widget
 * if it isn't already realized. FIXME: why?
 *
 * Returns: @component's #newtComponent
 */
newtComponent
nmt_newt_component_get_component(NmtNewtComponent *component)
{
    NmtNewtComponentPrivate *priv = NMT_NEWT_COMPONENT_GET_PRIVATE(component);

    return priv->co;
}

/**
 * nmt_newt_component_get_sensitive:
 * @component: an #NmtNewtComponent
 *
 * Gets @component's #NmtNewtComponent:sensitive property, indicating
 * whether the widget is available for manipulation. Insensitive
 * components will be skipped over in the keyboard tab chain, and may
 * be displayed differently.
 *
 * Returns: @component's #NmtNewtComponent:sensitive property
 */
gboolean
nmt_newt_component_get_sensitive(NmtNewtComponent *component)
{
    NmtNewtComponentPrivate *priv = NMT_NEWT_COMPONENT_GET_PRIVATE(component);

    return priv->sensitive;
}

/**
 * nmt_newt_component_set_sensitive:
 * @component: an #NmtNewtComponent
 * @sensitive: whether @component should be sensitive
 *
 * Sets @component's #NmtNewtComponent:sensitive property.
 */
void
nmt_newt_component_set_sensitive(NmtNewtComponent *component, gboolean sensitive)
{
    NmtNewtComponentPrivate *priv = NMT_NEWT_COMPONENT_GET_PRIVATE(component);

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

    priv->sensitive = sensitive;
    g_object_notify(G_OBJECT(component), "sensitive");
    nmt_newt_widget_needs_rebuild(NMT_NEWT_WIDGET(component));
}

static void
nmt_newt_component_set_property(GObject *     object,
                                guint         prop_id,
                                const GValue *value,
                                GParamSpec *  pspec)
{
    NmtNewtComponent *component = NMT_NEWT_COMPONENT(object);

    switch (prop_id) {
    case PROP_SENSITIVE:
        nmt_newt_component_set_sensitive(component, g_value_get_boolean(value));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
nmt_newt_component_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
    NmtNewtComponent *component = NMT_NEWT_COMPONENT(object);

    switch (prop_id) {
    case PROP_COMPONENT:
        g_value_set_pointer(value, nmt_newt_component_get_component(component));
        break;
    case PROP_SENSITIVE:
        g_value_set_boolean(value, nmt_newt_component_get_sensitive(component));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
nmt_newt_component_class_init(NmtNewtComponentClass *component_class)
{
    GObjectClass *      object_class = G_OBJECT_CLASS(component_class);
    NmtNewtWidgetClass *widget_class = NMT_NEWT_WIDGET_CLASS(component_class);

    g_type_class_add_private(component_class, sizeof(NmtNewtComponentPrivate));

    /* virtual methods */
    object_class->set_property = nmt_newt_component_set_property;
    object_class->get_property = nmt_newt_component_get_property;

    widget_class->realize             = nmt_newt_component_realize;
    widget_class->unrealize           = nmt_newt_component_unrealize;
    widget_class->get_components      = nmt_newt_component_get_components;
    widget_class->find_component      = nmt_newt_component_find_component;
    widget_class->size_request        = nmt_newt_component_size_request;
    widget_class->size_allocate       = nmt_newt_component_size_allocate;
    widget_class->get_focus_component = nmt_newt_component_get_focus_component;

    /* properties */

    /**
	 * NmtNewtComponent:component:
	 *
	 * The component's #newtComponent
	 */
    g_object_class_install_property(
        object_class,
        PROP_COMPONENT,
        g_param_spec_pointer("component", "", "", G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
    /**
	 * NmtNewtComponent:sensitive:
	 *
	 * Whether the component is sensitive. Insensitive components will
	 * be skipped over in the keyboard tab chain, and may be displayed
	 * differently.
	 */
    g_object_class_install_property(
        object_class,
        PROP_SENSITIVE,
        g_param_spec_boolean("sensitive",
                             "",
                             "",
                             TRUE,
                             G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}