summaryrefslogtreecommitdiff
path: root/src/terminal-screen-container.c
blob: 290685c3808f519b1fee709002b4a2c5f895e244 (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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * Copyright © 2008, 2010 Christian Persch
 *
 * This 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 3 of the License, or (at your option) any later version.
 *
 * This 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 this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <config.h>

#include "terminal-screen-container.h"
#include "terminal-debug.h"
#include "terminal-intl.h"

#include <gtk/gtk.h>

#define TERMINAL_SCREEN_CONTAINER_GET_PRIVATE(screen_container)(G_TYPE_INSTANCE_GET_PRIVATE ((screen_container), TERMINAL_TYPE_SCREEN_CONTAINER, TerminalScreenContainerPrivate))

struct _TerminalScreenContainerPrivate
{
	TerminalScreen *screen;
#ifdef USE_SCROLLED_WINDOW
	GtkWidget *scrolled_window;
#else
	GtkWidget *hbox;
	GtkWidget *vscrollbar;
#endif
	GtkPolicyType hscrollbar_policy;
	GtkPolicyType vscrollbar_policy;
	GtkCornerType window_placement;
	guint window_placement_set : 1;
};

enum
{
    PROP_0,
    PROP_SCREEN,
    PROP_HSCROLLBAR_POLICY,
    PROP_VSCROLLBAR_POLICY,
    PROP_WINDOW_PLACEMENT,
    PROP_WINDOW_PLACEMENT_SET
};

G_DEFINE_TYPE (TerminalScreenContainer, terminal_screen_container, GTK_TYPE_BOX)

/* helper functions */

static void
terminal_screen_container_set_placement_internal (TerminalScreenContainer *container,
        GtkCornerType corner)
{
	TerminalScreenContainerPrivate *priv = container->priv;

#ifdef USE_SCROLLED_WINDOW
	gtk_scrolled_window_set_placement (GTK_SCROLLED_WINDOW (priv->scrolled_window), corner);
#else
	switch (corner)
	{
	case GTK_CORNER_TOP_LEFT:
	case GTK_CORNER_BOTTOM_LEFT:
		gtk_box_reorder_child (GTK_BOX (priv->hbox), priv->vscrollbar, 1);
		break;
	case GTK_CORNER_TOP_RIGHT:
	case GTK_CORNER_BOTTOM_RIGHT:
		gtk_box_reorder_child (GTK_BOX (priv->hbox), priv->vscrollbar, 0);
		break;
	default:
		g_assert_not_reached ();
	}
#endif

	priv->window_placement = corner;
	g_object_notify (G_OBJECT (container), "window-placement");
}

static void
terminal_screen_container_set_placement_set (TerminalScreenContainer *container,
        gboolean set)
{
	TerminalScreenContainerPrivate *priv = container->priv;

#ifdef USE_SCROLLED_WINDOW
	g_object_set (priv->scrolled_window, "window-placement-set", set, NULL);
#endif

	priv->window_placement_set = set != FALSE;
	g_object_notify (G_OBJECT (container), "window-placement-set");
}

#if defined(USE_SCROLLED_WINDOW) && defined(MATE_ENABLE_DEBUG)
static void
size_allocate_cb (GtkWidget *widget,
                  GdkRectangle *rect,
                  TerminalScreenContainer *container)
{
	_terminal_debug_print (TERMINAL_DEBUG_GEOMETRY,
	                       "[screen %p] scrolled-window size alloc %d : %d\n",
	                       container->priv->screen, rect->width, rect->height);
}
#endif

/* Class implementation */

static void
terminal_screen_container_init (TerminalScreenContainer *container)
{
	TerminalScreenContainerPrivate *priv;

	priv = container->priv = TERMINAL_SCREEN_CONTAINER_GET_PRIVATE (container);

	priv->hscrollbar_policy = GTK_POLICY_AUTOMATIC;
	priv->vscrollbar_policy = GTK_POLICY_AUTOMATIC;
	priv->window_placement = GTK_CORNER_BOTTOM_RIGHT;
	priv->window_placement_set = FALSE;

	gtk_orientable_set_orientation (GTK_ORIENTABLE (container), GTK_ORIENTATION_VERTICAL);
}

static GObject *
terminal_screen_container_constructor (GType type,
                                       guint n_construct_properties,
                                       GObjectConstructParam *construct_params)
{
	GObject *object;
	TerminalScreenContainer *container;
	TerminalScreenContainerPrivate *priv;

	object = G_OBJECT_CLASS (terminal_screen_container_parent_class)->constructor
	         (type, n_construct_properties, construct_params);

	container = TERMINAL_SCREEN_CONTAINER (object);
	priv = container->priv;

	g_assert (priv->screen != NULL);

#ifdef USE_SCROLLED_WINDOW
#if VTE_CHECK_VERSION (0, 38, 0)
	priv->scrolled_window = gtk_scrolled_window_new (NULL, gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (priv->screen)));
#else
	priv->scrolled_window = gtk_scrolled_window_new (NULL, vte_terminal_get_adjustment (VTE_TERMINAL (priv->screen)));
#endif

	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
	                                priv->hscrollbar_policy,
	                                priv->vscrollbar_policy);
	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window),
	                                     GTK_SHADOW_NONE);
	gtk_container_add (GTK_CONTAINER (priv->scrolled_window), GTK_WIDGET (priv->screen));
	gtk_widget_show (GTK_WIDGET (priv->screen));
	gtk_box_pack_end (GTK_BOX (container), priv->scrolled_window, TRUE, TRUE, 0);
	gtk_widget_show (priv->scrolled_window);

#ifdef MATE_ENABLE_DEBUG
	g_signal_connect (priv->scrolled_window, "size-allocate", G_CALLBACK (size_allocate_cb), container);
#endif

#else

#if GTK_CHECK_VERSION (3, 0, 0)
	priv->hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
#else
	priv->hbox = gtk_hbox_new (FALSE, 0);
#endif

#if VTE_CHECK_VERSION (0, 38, 0)
	priv->vscrollbar = gtk_vscrollbar_new (gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (priv->screen)));
#else
	priv->vscrollbar = gtk_vscrollbar_new (vte_terminal_get_adjustment (VTE_TERMINAL (priv->screen)));
#endif

	gtk_box_pack_start (GTK_BOX (priv->hbox), GTK_WIDGET (priv->screen), TRUE, TRUE, 0);
	gtk_box_pack_start (GTK_BOX (priv->hbox), priv->vscrollbar, FALSE, FALSE, 0);

	gtk_box_pack_end (GTK_BOX (container), priv->hbox, TRUE, TRUE, 0);
	gtk_widget_show_all (priv->hbox);
#endif /* USE_SCROLLED_WINDOW */

	_terminal_screen_update_scrollbar (priv->screen);

	return object;
}

static void
terminal_screen_container_get_property (GObject *object,
                                        guint prop_id,
                                        GValue *value,
                                        GParamSpec *pspec)
{
	TerminalScreenContainer *container = TERMINAL_SCREEN_CONTAINER (object);
	TerminalScreenContainerPrivate *priv = container->priv;

	switch (prop_id)
	{
	case PROP_SCREEN:
		break;
	case PROP_HSCROLLBAR_POLICY:
		g_value_set_enum (value, priv->hscrollbar_policy);
		break;
	case PROP_VSCROLLBAR_POLICY:
		g_value_set_enum (value, priv->vscrollbar_policy);
		break;
	case PROP_WINDOW_PLACEMENT:
		g_value_set_enum (value, priv->window_placement);
		break;
	case PROP_WINDOW_PLACEMENT_SET:
		g_value_set_boolean (value, priv->window_placement_set);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
terminal_screen_container_set_property (GObject *object,
                                        guint prop_id,
                                        const GValue *value,
                                        GParamSpec *pspec)
{
	TerminalScreenContainer *container = TERMINAL_SCREEN_CONTAINER (object);
	TerminalScreenContainerPrivate *priv = container->priv;

	switch (prop_id)
	{
	case PROP_SCREEN:
		priv->screen = g_value_get_object (value);
		break;
	case PROP_HSCROLLBAR_POLICY:
		terminal_screen_container_set_policy (container,
		                                      g_value_get_enum (value),
		                                      priv->vscrollbar_policy);
		break;
	case PROP_VSCROLLBAR_POLICY:
		terminal_screen_container_set_policy (container,
		                                      priv->hscrollbar_policy,
		                                      g_value_get_enum (value));
		break;
	case PROP_WINDOW_PLACEMENT:
		terminal_screen_container_set_placement_internal (container, g_value_get_enum (value));
		break;
	case PROP_WINDOW_PLACEMENT_SET:
		terminal_screen_container_set_placement_set (container, g_value_get_boolean (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
terminal_screen_container_class_init (TerminalScreenContainerClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

	g_type_class_add_private (gobject_class, sizeof (TerminalScreenContainerPrivate));

	gobject_class->constructor = terminal_screen_container_constructor;
	gobject_class->get_property = terminal_screen_container_get_property;
	gobject_class->set_property = terminal_screen_container_set_property;

	g_object_class_install_property
	(gobject_class,
	 PROP_SCREEN,
	 g_param_spec_object ("screen", NULL, NULL,
	                      TERMINAL_TYPE_SCREEN,
	                      G_PARAM_READWRITE |
	                      G_PARAM_CONSTRUCT_ONLY |
	                      G_PARAM_STATIC_STRINGS));

	g_object_class_install_property
	(gobject_class,
	 PROP_HSCROLLBAR_POLICY,
	 g_param_spec_enum ("hscrollbar-policy", NULL, NULL,
	                    GTK_TYPE_POLICY_TYPE,
	                    GTK_POLICY_AUTOMATIC,
	                    G_PARAM_READWRITE |
	                    G_PARAM_STATIC_STRINGS));
	g_object_class_install_property
	(gobject_class,
	 PROP_VSCROLLBAR_POLICY,
	 g_param_spec_enum ("vscrollbar-policy", NULL, NULL,
	                    GTK_TYPE_POLICY_TYPE,
	                    GTK_POLICY_AUTOMATIC,
	                    G_PARAM_READWRITE |
	                    G_PARAM_STATIC_STRINGS));

	g_object_class_install_property
	(gobject_class,
	 PROP_WINDOW_PLACEMENT,
	 g_param_spec_enum ("window-placement", NULL, NULL,
	                    GTK_TYPE_CORNER_TYPE,
	                    GTK_CORNER_TOP_LEFT,
	                    G_PARAM_READWRITE |
	                    G_PARAM_STATIC_STRINGS));

	g_object_class_install_property
	(gobject_class,
	 PROP_WINDOW_PLACEMENT_SET,
	 g_param_spec_boolean ("window-placement-set", NULL, NULL,
	                       FALSE,
	                       G_PARAM_READWRITE |
	                       G_PARAM_STATIC_STRINGS));
}

/* public API */

/**
 * terminal_screen_container_new:
 * @screen: a #TerminalScreen
 *
 * Returns: a new #TerminalScreenContainer for @screen
 */
GtkWidget *
terminal_screen_container_new (TerminalScreen *screen)
{
	return g_object_new (TERMINAL_TYPE_SCREEN_CONTAINER,
	                     "screen", screen,
	                     NULL);
}

/**
 * terminal_screen_container_get_screen:
 * @container: a #TerminalScreenContainer
 *
 * Returns: @container's #TerminalScreen
 */
TerminalScreen *
terminal_screen_container_get_screen (TerminalScreenContainer *container)
{
	g_return_val_if_fail (TERMINAL_IS_SCREEN_CONTAINER (container), NULL);

	return container->priv->screen;
}

/**
 * terminal_screen_container_get_from_screen:
 * @screen: a #TerminalScreenContainerPrivate
 *
 * Returns the #TerminalScreenContainer containing @screen.
 */
TerminalScreenContainer *
terminal_screen_container_get_from_screen (TerminalScreen *screen)
{
	g_return_val_if_fail (TERMINAL_IS_SCREEN (screen), NULL);

	return TERMINAL_SCREEN_CONTAINER (gtk_widget_get_ancestor (GTK_WIDGET (screen), TERMINAL_TYPE_SCREEN_CONTAINER));
}

/**
 * terminal_screen_container_set_policy:
 * @container: a #TerminalScreenContainer
 * @hpolicy: a #GtkPolicyType
 * @vpolicy: a #GtkPolicyType
 *
 * Sets @container's scrollbar policy.
 */
void
terminal_screen_container_set_policy (TerminalScreenContainer *container,
                                      GtkPolicyType hpolicy G_GNUC_UNUSED,
                                      GtkPolicyType vpolicy)
{
	TerminalScreenContainerPrivate *priv;
	GObject *object;

	g_return_if_fail (TERMINAL_IS_SCREEN_CONTAINER (container));

	object = G_OBJECT (container);
	priv = container->priv;

	g_object_freeze_notify (object);

	if (priv->hscrollbar_policy != hpolicy)
	{
		priv->hscrollbar_policy = hpolicy;
		g_object_notify (object, "hscrollbar-policy");
	}
	if (priv->vscrollbar_policy != vpolicy)
	{
		priv->vscrollbar_policy = vpolicy;
		g_object_notify (object, "vscrollbar-policy");
	}

#ifdef USE_SCROLLED_WINDOW
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), hpolicy, vpolicy);
#else
	switch (vpolicy)
	{
	case GTK_POLICY_NEVER:
		gtk_widget_hide (priv->vscrollbar);
		break;
	case GTK_POLICY_AUTOMATIC:
	case GTK_POLICY_ALWAYS:
		gtk_widget_show (priv->vscrollbar);
		break;
	default:
		g_assert_not_reached ();
	}
#endif

	g_object_thaw_notify (object);
}

/**
 * terminal_screen_container_set_placement:
 * @container: a #TerminalScreenContainer
 * @corner: a #GtkCornerType
 *
 * Sets @container's window placement.
 */
void
terminal_screen_container_set_placement (TerminalScreenContainer *container,
        GtkCornerType corner)
{
	g_return_if_fail (TERMINAL_IS_SCREEN_CONTAINER (container));

	terminal_screen_container_set_placement_internal (container, corner);
	terminal_screen_container_set_placement_set (container, TRUE);
}