summaryrefslogtreecommitdiff
path: root/libcaja-private/caja-horizontal-splitter.c
blob: e31d8a6a81c63b6528f76cfa5d93e0fbd8afdc5e (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* caja-horizontal-splitter.c - A horizontal splitter with a semi gradient look

   Copyright (C) 1999, 2000 Eazel, Inc.

   The Mate Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Mate Library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Mate Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Authors: Ramiro Estrugo <ramiro@eazel.com>
*/

#include <config.h>
#include "caja-horizontal-splitter.h"

#include <eel/eel-gtk-macros.h>
#include <stdlib.h>

struct CajaHorizontalSplitterDetails
{
    guint32 press_time;
    int press_position;
    int saved_size;
};

#define CLOSED_THRESHOLD 4
#define NOMINAL_SIZE 148
#define SPLITTER_CLICK_SLOP 4
#define SPLITTER_CLICK_TIMEOUT	400

static void caja_horizontal_splitter_class_init (CajaHorizontalSplitterClass *horizontal_splitter_class);
static void caja_horizontal_splitter_init       (CajaHorizontalSplitter      *horizontal_splitter);

EEL_CLASS_BOILERPLATE (CajaHorizontalSplitter,
                       caja_horizontal_splitter,
                       GTK_TYPE_HPANED)

static void
caja_horizontal_splitter_init (CajaHorizontalSplitter *horizontal_splitter)
{
    horizontal_splitter->details = g_new0 (CajaHorizontalSplitterDetails, 1);
}

static void
caja_horizontal_splitter_finalize (GObject *object)
{
    CajaHorizontalSplitter *horizontal_splitter;

    horizontal_splitter = CAJA_HORIZONTAL_SPLITTER (object);

    g_free (horizontal_splitter->details);

    EEL_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
}

static void
splitter_expand (CajaHorizontalSplitter *splitter, int position)
{
    g_assert (CAJA_IS_HORIZONTAL_SPLITTER (splitter));

    if (position >= CLOSED_THRESHOLD)
    {
        return;
    }

    position = splitter->details->saved_size;
    if (position < CLOSED_THRESHOLD)
    {
        position = NOMINAL_SIZE;
    }

    gtk_paned_set_position (GTK_PANED (splitter), position);
}

static void
splitter_collapse (CajaHorizontalSplitter *splitter, int position)
{
    g_assert (CAJA_IS_HORIZONTAL_SPLITTER (splitter));

    splitter->details->saved_size = position;
    gtk_paned_set_position (GTK_PANED (splitter), 0);
}

static void
splitter_toggle (CajaHorizontalSplitter *splitter, int position)
{
    g_assert (CAJA_IS_HORIZONTAL_SPLITTER (splitter));

    if (gtk_paned_get_position (GTK_PANED (splitter)) >= CLOSED_THRESHOLD)
    {
        caja_horizontal_splitter_collapse (splitter);
    }
    else
    {
        caja_horizontal_splitter_expand (splitter);
    }
}

static void
splitter_hide (CajaHorizontalSplitter *splitter)
{
    GtkPaned *parent;

    parent = GTK_PANED (splitter);

    gtk_widget_hide (gtk_paned_get_child1 (parent));
}

static void
splitter_show (CajaHorizontalSplitter *splitter)
{
    GtkPaned *parent;

    parent = GTK_PANED (splitter);

    gtk_widget_show (gtk_paned_get_child1 (parent));
}

static gboolean
splitter_is_hidden (CajaHorizontalSplitter *splitter)
{
    GtkPaned *parent;

    parent = GTK_PANED (splitter);

    return gtk_widget_get_visible (gtk_paned_get_child1 (parent));
}

void
caja_horizontal_splitter_expand (CajaHorizontalSplitter *splitter)
{
    splitter_expand (splitter, gtk_paned_get_position (GTK_PANED (splitter)));
}

void
caja_horizontal_splitter_hide (CajaHorizontalSplitter *splitter)
{
    splitter_hide (splitter);
}

void
caja_horizontal_splitter_show (CajaHorizontalSplitter *splitter)
{
    splitter_show (splitter);
}

gboolean
caja_horizontal_splitter_is_hidden (CajaHorizontalSplitter *splitter)
{
    return splitter_is_hidden (splitter);
}

void
caja_horizontal_splitter_collapse (CajaHorizontalSplitter *splitter)
{
    splitter_collapse (splitter, gtk_paned_get_position (GTK_PANED (splitter)));
}

/* routine to toggle the open/closed state of the splitter */
void
caja_horizontal_splitter_toggle_position (CajaHorizontalSplitter *splitter)
{
    splitter_toggle (splitter, gtk_paned_get_position (GTK_PANED (splitter)));
}

/* CajaHorizontalSplitter public methods */
GtkWidget *
caja_horizontal_splitter_new (void)
{
    return gtk_widget_new (caja_horizontal_splitter_get_type (), NULL);
}

/* handle mouse downs by remembering the position and the time */
static gboolean
caja_horizontal_splitter_button_press (GtkWidget *widget, GdkEventButton *event)
{
    gboolean result;
    CajaHorizontalSplitter *splitter;
    int position;

    splitter = CAJA_HORIZONTAL_SPLITTER (widget);

    position = gtk_paned_get_position (GTK_PANED (widget));

    result = EEL_CALL_PARENT_WITH_RETURN_VALUE
             (GTK_WIDGET_CLASS, button_press_event, (widget, event));

    if (result)
    {
        splitter->details->press_time = event->time;
        splitter->details->press_position = position;
    }

    return result;
}

/* handle mouse ups by seeing if it was a tap and toggling the open state accordingly */
static gboolean
caja_horizontal_splitter_button_release (GtkWidget *widget, GdkEventButton *event)
{
    gboolean result;
    CajaHorizontalSplitter *splitter;
    int position, delta, delta_time;
    splitter = CAJA_HORIZONTAL_SPLITTER (widget);

    position = gtk_paned_get_position (GTK_PANED (widget));

    result = EEL_CALL_PARENT_WITH_RETURN_VALUE
             (GTK_WIDGET_CLASS, button_release_event, (widget, event));

    if (result)
    {
        delta = abs (position - splitter->details->press_position);
        delta_time = event->time - splitter->details->press_time;
        if (delta < SPLITTER_CLICK_SLOP && delta_time < SPLITTER_CLICK_TIMEOUT)
        {
            caja_horizontal_splitter_toggle_position (splitter);
        }
    }

    return result;
}

static void
caja_horizontal_splitter_size_allocate (GtkWidget     *widget,
                                        GtkAllocation *allocation)
{
    gint border_width;
    GtkPaned *paned;
    GtkAllocation child_allocation;
    GtkRequisition child_requisition;

    paned = GTK_PANED (widget);
    border_width = gtk_container_get_border_width (GTK_CONTAINER (paned));

    gtk_widget_set_allocation (widget, allocation);

    if (gtk_paned_get_child2 (paned) != NULL && gtk_widget_get_visible (gtk_paned_get_child2 (paned)))
    {
        EEL_CALL_PARENT (GTK_WIDGET_CLASS, size_allocate,
                         (widget, allocation));
    }
    else if (gtk_paned_get_child1 (paned) && gtk_widget_get_visible (gtk_paned_get_child1 (paned)))
    {

        if (gtk_widget_get_realized (widget))
        {
            gdk_window_hide (gtk_paned_get_handle_window (paned));
        }

        gtk_widget_get_child_requisition (gtk_paned_get_child1 (paned),
                                          &child_requisition);

        child_allocation.x = allocation->x + border_width;
        child_allocation.y = allocation->y + border_width;
        child_allocation.width = MIN (child_requisition.width,
                                      allocation->width - 2 * border_width);
        child_allocation.height = MIN (child_requisition.height,
                                       allocation->height - 2 * border_width);

        gtk_widget_size_allocate (gtk_paned_get_child1 (paned), &child_allocation);
    }
    else if (gtk_widget_get_realized (widget))
    {
        gdk_window_hide (gtk_paned_get_handle_window (paned));
    }

}

static void
caja_horizontal_splitter_class_init (CajaHorizontalSplitterClass *class)
{
    GtkWidgetClass *widget_class;

    widget_class = GTK_WIDGET_CLASS (class);

    G_OBJECT_CLASS (class)->finalize = caja_horizontal_splitter_finalize;

    widget_class->size_allocate = caja_horizontal_splitter_size_allocate;
    widget_class->button_press_event = caja_horizontal_splitter_button_press;
    widget_class->button_release_event = caja_horizontal_splitter_button_release;
}

void
caja_horizontal_splitter_pack2 (CajaHorizontalSplitter *splitter,
                                GtkWidget                  *child2)
{
    GtkPaned *paned;

    g_return_if_fail (GTK_IS_WIDGET (child2));
    g_return_if_fail (CAJA_IS_HORIZONTAL_SPLITTER (splitter));

    paned = GTK_PANED (splitter);
    gtk_paned_pack2 (paned, child2, TRUE, FALSE);
}