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

/**
 * SECTION:nmt-newt-textbox
 * @short_description: Multi-line text box
 *
 * #NmtNewtTextbox implements a multi-line text, optionally with
 * word-wrapping.
 */

#include "nm-default.h"

#include "nmt-newt-textbox.h"

#include "nmt-newt-utils.h"

G_DEFINE_TYPE(NmtNewtTextbox, nmt_newt_textbox, NMT_TYPE_NEWT_COMPONENT)

#define NMT_NEWT_TEXTBOX_GET_PRIVATE(o) \
    (G_TYPE_INSTANCE_GET_PRIVATE((o), NMT_TYPE_NEWT_TEXTBOX, NmtNewtTextboxPrivate))

typedef struct {
    int                 wrap_width;
    NmtNewtTextboxFlags flags;

    char *text;
    int   width, height;
} NmtNewtTextboxPrivate;

enum {
    PROP_0,
    PROP_TEXT,
    PROP_FLAGS,
    PROP_WRAP_WIDTH,

    LAST_PROP
};

/**
 * NmtNewtTextboxFlags:
 * @NMT_NEWT_TEXTBOX_SCROLLABLE: the textbox should be scrollable.
 * @NMT_NEWT_TEXTBOX_SET_BACKGROUND: the textbox should have a
 *   white background
 *
 * Flags for an #NmtNewtTextbox
 */

/**
 * nmt_newt_textbox_new:
 * @flags: the textbox's flags
 * @wrap_width: width in characters at which to word-wrap, or
 *   0 to not wrap.
 *
 * Creates a new #NmtNewtTextbox.
 *
 * Returns: a new #NmtNewtTextbox
 */
NmtNewtWidget *
nmt_newt_textbox_new(NmtNewtTextboxFlags flags, int wrap_width)
{
    return g_object_new(NMT_TYPE_NEWT_TEXTBOX, "flags", flags, "wrap-width", wrap_width, NULL);
}

/**
 * nmt_newt_textbox_get_text:
 * @textbox: an #NmtNewtTextbox
 *
 * Gets @textbox's text
 *
 * Returns: @textbox's text
 */
void
nmt_newt_textbox_set_text(NmtNewtTextbox *textbox, const char *text)
{
    NmtNewtTextboxPrivate *priv = NMT_NEWT_TEXTBOX_GET_PRIVATE(textbox);
    char **                lines;
    int                    i, width;

    if (!text)
        text = "";
    if (!strcmp(priv->text, text))
        return;

    g_free(priv->text);
    priv->text = g_strdup(text);

    priv->width = priv->height = 0;
    lines                      = g_strsplit(priv->text, "\n", -1);
    for (i = 0; lines[i]; i++) {
        width = nmt_newt_text_width(lines[i]);
        if (width > priv->width)
            priv->width = width;
    }
    g_free(lines);
    priv->height = MIN(i, 1);

    g_object_notify(G_OBJECT(textbox), "text");
    nmt_newt_widget_needs_rebuild(NMT_NEWT_WIDGET(textbox));
}

/**
 * nmt_newt_textbox_get_text:
 * @textbox: an #NmtNewtTextbox
 *
 * Gets @textbox's text
 *
 * Returns: @textbox's text
 */
const char *
nmt_newt_textbox_get_text(NmtNewtTextbox *textbox)
{
    NmtNewtTextboxPrivate *priv = NMT_NEWT_TEXTBOX_GET_PRIVATE(textbox);

    return priv->text;
}

static void
nmt_newt_textbox_init(NmtNewtTextbox *textbox)
{
    NmtNewtTextboxPrivate *priv = NMT_NEWT_TEXTBOX_GET_PRIVATE(textbox);

    priv->text = g_strdup("");
}

static void
nmt_newt_textbox_finalize(GObject *object)
{
    NmtNewtTextboxPrivate *priv = NMT_NEWT_TEXTBOX_GET_PRIVATE(object);

    g_free(priv->text);

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

static guint
convert_flags(NmtNewtTextboxFlags flags)
{
    guint newt_flags = 0;

    if (flags & NMT_NEWT_TEXTBOX_SCROLLABLE)
        newt_flags |= NEWT_FLAG_SCROLL;

    return newt_flags;
}

static newtComponent
nmt_newt_textbox_build_component(NmtNewtComponent *component, gboolean sensitive)
{
    NmtNewtTextboxPrivate *priv = NMT_NEWT_TEXTBOX_GET_PRIVATE(component);
    newtComponent          co;
    const char *           text;
    char *                 text_lc;

    text = priv->text;
    if (!*text)
        text = "\n";

    text_lc = nmt_newt_locale_from_utf8(text);
    if (priv->wrap_width > 0) {
        co = newtTextboxReflowed(-1, -1, text_lc, priv->wrap_width, 0, 0, 0);
    } else {
        co = newtTextbox(-1, -1, priv->width, priv->height, convert_flags(priv->flags));
        newtTextboxSetText(co, text_lc);
    }
    g_free(text_lc);

    if (priv->flags & NMT_NEWT_TEXTBOX_SET_BACKGROUND)
        newtTextboxSetColors(co,
                             NMT_NEWT_COLORSET_TEXTBOX_WITH_BACKGROUND,
                             NEWT_COLORSET_ACTTEXTBOX);

    return co;
}

static void
nmt_newt_textbox_set_property(GObject *     object,
                              guint         prop_id,
                              const GValue *value,
                              GParamSpec *  pspec)
{
    NmtNewtTextbox *       textbox = NMT_NEWT_TEXTBOX(object);
    NmtNewtTextboxPrivate *priv    = NMT_NEWT_TEXTBOX_GET_PRIVATE(textbox);

    switch (prop_id) {
    case PROP_TEXT:
        nmt_newt_textbox_set_text(textbox, g_value_get_string(value));
        break;
    case PROP_FLAGS:
        priv->flags = g_value_get_uint(value);
        break;
    case PROP_WRAP_WIDTH:
        priv->wrap_width = g_value_get_int(value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
nmt_newt_textbox_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
    NmtNewtTextboxPrivate *priv = NMT_NEWT_TEXTBOX_GET_PRIVATE(object);

    switch (prop_id) {
    case PROP_TEXT:
        g_value_set_string(value, priv->text);
        break;
    case PROP_FLAGS:
        g_value_set_uint(value, priv->flags);
        break;
    case PROP_WRAP_WIDTH:
        g_value_set_int(value, priv->wrap_width);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
nmt_newt_textbox_class_init(NmtNewtTextboxClass *textbox_class)
{
    GObjectClass *         object_class    = G_OBJECT_CLASS(textbox_class);
    NmtNewtComponentClass *component_class = NMT_NEWT_COMPONENT_CLASS(textbox_class);

    g_type_class_add_private(textbox_class, sizeof(NmtNewtTextboxPrivate));

    /* virtual methods */
    object_class->set_property = nmt_newt_textbox_set_property;
    object_class->get_property = nmt_newt_textbox_get_property;
    object_class->finalize     = nmt_newt_textbox_finalize;

    component_class->build_component = nmt_newt_textbox_build_component;

    /**
	 * NmtNewtTextbox:text:
	 *
	 * The textbox's text
	 */
    g_object_class_install_property(
        object_class,
        PROP_TEXT,
        g_param_spec_string("text", "", "", "", G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
    /**
	 * NmtNewtTextbox:flags:
	 *
	 * The textbox's flags
	 */
    g_object_class_install_property(
        object_class,
        PROP_FLAGS,
        g_param_spec_uint("flags",
                          "",
                          "",
                          0,
                          G_MAXUINT,
                          0,
                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
    /**
	 * NmtNewtTextbox:wrap-width:
	 *
	 * The width in characters at which the textbox's text
	 * will wrap, or 0 if it does not wrap.
	 */
    g_object_class_install_property(
        object_class,
        PROP_WRAP_WIDTH,
        g_param_spec_int("wrap-width",
                         "",
                         "",
                         0,
                         G_MAXINT,
                         0,
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
}