aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-paned.c
blob: b363d0aa0eda7f0acbaf0f429afa7ebe380d28b2 (plain) (blame)
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 * e-paned.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#include "e-paned.h"

#include <config.h>
#include <glib/gi18n-lib.h>

#define E_PANED_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_PANED, EPanedPrivate))

struct _EPanedPrivate {
    gint hposition;
    gint vposition;

    gulong wse_handler_id;

    guint sync_position : 1;
    guint toplevel_ready    : 1;
};

enum {
    PROP_0,
    PROP_HPOSITION,
    PROP_VPOSITION
};

static gpointer parent_class;

static gboolean
paned_window_state_event_cb (EPaned *paned,
                             GdkEventWindowState *event,
                             GtkWidget *toplevel)
{
    /* Wait for WITHDRAWN to change from 1 to 0. */
    if (!(event->changed_mask & GDK_WINDOW_STATE_WITHDRAWN))
        return FALSE;

    /* The whole point of this hack is to trap a point where if
     * the window were to be maximized initially, the maximized
     * allocation would already be negotiated.  We're there now.
     * Set a flag so we know it's safe to set GtkPaned position. */
    paned->priv->toplevel_ready = TRUE;

    paned->priv->sync_position = TRUE;
    gtk_widget_queue_resize (GTK_WIDGET (paned));

    /* We don't need to listen for window state events anymore. */
    g_signal_handler_disconnect (toplevel, paned->priv->wse_handler_id);
    paned->priv->wse_handler_id = 0;

    return FALSE;
}

static void
paned_notify_orientation_cb (EPaned *paned)
{
    /* Ignore the next "notify::position" emission. */
    paned->priv->sync_position = TRUE;
    gtk_widget_queue_resize (GTK_WIDGET (paned));
}

static void
paned_notify_position_cb (EPaned *paned)
{
    GtkAllocation *allocation;
    GtkOrientable *orientable;
    GtkOrientation orientation;
    gint position;

    if (paned->priv->sync_position)
        return;

    orientable = GTK_ORIENTABLE (paned);
    orientation = gtk_orientable_get_orientation (orientable);

    allocation = &GTK_WIDGET (paned)->allocation;
    position = gtk_paned_get_position (GTK_PANED (paned));

    if (orientation == GTK_ORIENTATION_HORIZONTAL) {
        position = MAX (0, allocation->width - position);
        e_paned_set_hposition (paned, position);
    } else {
        position = MAX (0, allocation->height - position);
        e_paned_set_vposition (paned, position);
    }
}

static void
paned_set_property (GObject *object,
                    guint property_id,
                    const GValue *value,
                    GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_HPOSITION:
            e_paned_set_hposition (
                E_PANED (object),
                g_value_get_int (value));
            return;

        case PROP_VPOSITION:
            e_paned_set_vposition (
                E_PANED (object),
                g_value_get_int (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
paned_get_property (GObject *object,
                    guint property_id,
                    GValue *value,
                    GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_HPOSITION:
            g_value_set_int (
                value, e_paned_get_hposition (
                E_PANED (object)));
            return;

        case PROP_VPOSITION:
            g_value_set_int (
                value, e_paned_get_vposition (
                E_PANED (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
paned_realize (GtkWidget *widget)
{
    EPanedPrivate *priv;
    GtkWidget *toplevel;
    GdkWindowState state;
    GdkWindow *window;

    priv = E_PANED_GET_PRIVATE (widget);

    /* Chain up to parent's realize() method. */
    GTK_WIDGET_CLASS (parent_class)->realize (widget);

    /* XXX This would be easier if we could be notified of
     *     window state events directly, but I can't seem
     *     to make that happen. */

    toplevel = gtk_widget_get_toplevel (widget);
    window = gtk_widget_get_window (toplevel);
    state = gdk_window_get_state (window);

    /* If the window is withdrawn, wait for it to be shown before
     * setting the pane position.  If the window is already shown,
     * it's safe to set the pane position immediately. */
    if (state & GDK_WINDOW_STATE_WITHDRAWN)
        priv->wse_handler_id = g_signal_connect_swapped (
            toplevel, "window-state-event",
            G_CALLBACK (paned_window_state_event_cb), widget);
    else
        priv->toplevel_ready = TRUE;
}

static void
paned_size_allocate (GtkWidget *widget,
                     GtkAllocation *allocation)
{
    EPaned *paned = E_PANED (widget);
    GtkOrientable *orientable;
    GtkOrientation orientation;
    gint allocated;
    gint position;

    /* Chain up to parent's size_allocate() method. */
    GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);

    if (!paned->priv->toplevel_ready)
        return;

    if (!paned->priv->sync_position)
        return;

    orientable = GTK_ORIENTABLE (paned);
    orientation = gtk_orientable_get_orientation (orientable);

    if (orientation == GTK_ORIENTATION_HORIZONTAL) {
        allocated = allocation->width;
        position = e_paned_get_hposition (paned);
    } else {
        allocated = allocation->height;
        position = e_paned_get_vposition (paned);
    }

    position = MAX (0, allocated - position);
    gtk_paned_set_position (GTK_PANED (paned), position);

    paned->priv->sync_position = FALSE;
}

static void
paned_class_init (EPanedClass *class)
{
    GObjectClass *object_class;
    GtkWidgetClass *widget_class;

    parent_class = g_type_class_peek_parent (class);
    g_type_class_add_private (class, sizeof (EPanedPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = paned_set_property;
    object_class->get_property = paned_get_property;

    widget_class = GTK_WIDGET_CLASS (class);
    widget_class->realize = paned_realize;
    widget_class->size_allocate = paned_size_allocate;

    g_object_class_install_property (
        object_class,
        PROP_HPOSITION,
        g_param_spec_int (
            "hposition",
            _("Horizontal Position"),
            _("Pane position when oriented horizontally"),
            G_MININT,
            G_MAXINT,
            0,
            G_PARAM_READWRITE));

    g_object_class_install_property (
        object_class,
        PROP_VPOSITION,
        g_param_spec_int (
            "vposition",
            _("Vertical Position"),
            _("Pane position when oriented vertically"),
            G_MININT,
            G_MAXINT,
            0,
            G_PARAM_READWRITE));
}

static void
paned_init (EPaned *paned)
{
    paned->priv = E_PANED_GET_PRIVATE (paned);

    g_signal_connect (
        paned, "notify::orientation",
        G_CALLBACK (paned_notify_orientation_cb), NULL);

    g_signal_connect (
        paned, "notify::position",
        G_CALLBACK (paned_notify_position_cb), NULL);
}

GType
e_paned_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0)) {
        static const GTypeInfo type_info = {
            sizeof (EPanedClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) paned_class_init,
            (GClassFinalizeFunc) NULL,
            NULL,  /* class_data */
            sizeof (EPaned),
            0,     /* n_preallocs */
            (GInstanceInitFunc) paned_init,
            NULL   /* value_table */
        };

        type = g_type_register_static (
            GTK_TYPE_PANED, "EPaned", &type_info, 0);
    }

    return type;
}

GtkWidget *
e_paned_new (GtkOrientation orientation)
{
    return g_object_new (E_TYPE_PANED, "orientation", orientation, NULL);
}

gint
e_paned_get_hposition (EPaned *paned)
{
    g_return_val_if_fail (E_IS_PANED (paned), 0);

    return paned->priv->hposition;
}

void
e_paned_set_hposition (EPaned *paned,
                       gint hposition)
{
    GtkOrientable *orientable;
    GtkOrientation orientation;

    g_return_if_fail (E_IS_PANED (paned));

    if (hposition == paned->priv->hposition)
        return;

    paned->priv->hposition = hposition;

    g_object_notify (G_OBJECT (paned), "hposition");

    orientable = GTK_ORIENTABLE (paned);
    orientation = gtk_orientable_get_orientation (orientable);

    if (orientation == GTK_ORIENTATION_HORIZONTAL) {
        paned->priv->sync_position = TRUE;
        gtk_widget_queue_resize (GTK_WIDGET (paned));
    }
}

gint
e_paned_get_vposition (EPaned *paned)
{
    g_return_val_if_fail (E_IS_PANED (paned), 0);

    return paned->priv->vposition;
}

void
e_paned_set_vposition (EPaned *paned,
                       gint vposition)
{
    GtkOrientable *orientable;
    GtkOrientation orientation;

    g_return_if_fail (E_IS_PANED (paned));

    if (vposition == paned->priv->vposition)
        return;

    paned->priv->vposition = vposition;

    g_object_notify (G_OBJECT (paned), "vposition");

    orientable = GTK_ORIENTABLE (paned);
    orientation = gtk_orientable_get_orientation (orientable);

    if (orientation == GTK_ORIENTATION_VERTICAL) {
        paned->priv->sync_position = TRUE;
        gtk_widget_queue_resize (GTK_WIDGET (paned));
    }
}