summaryrefslogtreecommitdiff
path: root/libnautilus-extensions/nautilus-viewport.c
blob: 5ea54c17af3647d106b27c956ca2bdf5f75a7eed (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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* nautilus-viewport.c - A subclass of GtkViewport with non broken drawing.

   Copyright (C) 1999, 2000 Eazel, Inc.

   The Gnome 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 Gnome 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 Gnome 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 "nautilus-viewport.h"

#include "nautilus-gtk-macros.h"

#include <gtk/gtksignal.h>

/* Detail member struct */
struct NautilusViewportDetails
{
	gboolean is_smooth;
	gboolean never_smooth;
       
	gboolean constrain_width;
	gboolean constrain_height;
};

/* GtkObjectClass methods */
static void nautilus_viewport_initialize_class     (NautilusViewportClass *viewport_class);
static void nautilus_viewport_initialize           (NautilusViewport      *viewport);
static void nautilus_viewport_destroy              (GtkObject             *object);

/* GtkWidgetClass methods */
static void nautilus_viewport_realize              (GtkWidget             *widget);
static void nautilus_viewport_draw                 (GtkWidget             *widget,
						    GdkRectangle          *area);
static void nautilus_viewport_size_allocate        (GtkWidget             *widget,
						    GtkAllocation         *allocation);
static gint nautilus_viewport_expose_event         (GtkWidget             *widget,
						    GdkEventExpose        *event);
static void nautilus_viewport_paint                (GtkWidget             *widget,
						    GdkRectangle          *area);

/* NautilusViewport signals */
static void nautilus_viewport_set_is_smooth_signal (GtkWidget             *widget,
						    gboolean               is_smooth);

/* Signals */
typedef enum
{
	SET_IS_SMOOTH,
	LAST_SIGNAL
} NautilusViewportSignal;

/* Signals */
static guint nautilus_viewport_signals[LAST_SIGNAL] = { 0 };

NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusViewport, nautilus_viewport, GTK_TYPE_VIEWPORT)

/* GtkObjectClass methods */
static void
nautilus_viewport_initialize_class (NautilusViewportClass *nautilus_viewport_class)
{
	GtkObjectClass *object_class = GTK_OBJECT_CLASS (nautilus_viewport_class);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (nautilus_viewport_class);

	/* GtkObjectClass */
	object_class->destroy = nautilus_viewport_destroy;
	
	/* GtkWidgetClass */
	widget_class->realize = nautilus_viewport_realize;
	widget_class->expose_event = nautilus_viewport_expose_event;
	widget_class->draw = nautilus_viewport_draw;
	widget_class->size_allocate = nautilus_viewport_size_allocate;
	
	/* NautilusViewportClass */
	nautilus_viewport_class->set_is_smooth = nautilus_viewport_set_is_smooth_signal;
	
	nautilus_viewport_signals[SET_IS_SMOOTH] = 
		gtk_signal_new ("set_is_smooth",
				GTK_RUN_LAST,
				object_class->type,
				GTK_SIGNAL_OFFSET (NautilusViewportClass, set_is_smooth),
				gtk_marshal_NONE__BOOL,
				GTK_TYPE_NONE, 
				1,
				GTK_TYPE_BOOL);
	
	gtk_object_class_add_signals (object_class, nautilus_viewport_signals, LAST_SIGNAL);

	/* Let the smooth widget machinery know that our class can be smooth */
	nautilus_smooth_widget_register_type (NAUTILUS_TYPE_VIEWPORT);
}

void
nautilus_viewport_initialize (NautilusViewport *nautilus_viewport)
{
	nautilus_viewport->details = g_new0 (NautilusViewportDetails, 1);
	
	nautilus_smooth_widget_register (GTK_WIDGET (nautilus_viewport));
	nautilus_viewport->details->never_smooth = TRUE;
}

void
nautilus_viewport_destroy (GtkObject *object)
{
	NautilusViewport *viewport;

	g_return_if_fail (NAUTILUS_IS_VIEWPORT (object));

	viewport = NAUTILUS_VIEWPORT (object);
	
	g_free (viewport->details);

	/* Chain destroy */
	NAUTILUS_CALL_PARENT (GTK_OBJECT_CLASS, destroy, (object));
}

/* GtkWidgetClass methods */
static void
nautilus_viewport_draw (GtkWidget *widget,
			GdkRectangle *area)
{
	NautilusViewport *nautilus_viewport;
	GtkViewport *viewport;
	GtkBin *bin;
	GdkRectangle tmp_area;
	GdkRectangle child_area;
	gint border_width;
	
	g_return_if_fail (NAUTILUS_IS_VIEWPORT (widget));
	g_return_if_fail (area != NULL);
	
	if (!GTK_WIDGET_DRAWABLE (widget)) {
		return;
	}

	nautilus_viewport = NAUTILUS_VIEWPORT (widget);
	viewport = GTK_VIEWPORT (widget);
	bin = GTK_BIN (widget);
	
	border_width = GTK_CONTAINER (widget)->border_width;
	
	tmp_area = *area;
	tmp_area.x -= border_width;
	tmp_area.y -= border_width;
	
	nautilus_viewport_paint (widget, &tmp_area);
	
	tmp_area.x += viewport->hadjustment->value - widget->style->klass->xthickness;
	tmp_area.y += viewport->vadjustment->value - widget->style->klass->ythickness;
	
	/* The gtk_viewport_draw() version does not adjust the width
	 * and height of the tmp_area for the class x/y thickness.  This
	 * causes some drawing to be clipped on the bottom.  This is a bug
	 * in GTK+.
	 */
	
	/* FIXME bugzilla.eazel.com xxxx: 
	 * Remove this widget once the fix makes it to GTK+.
	 */
	tmp_area.width += 2 * widget->style->klass->xthickness;
	tmp_area.height += 2 * widget->style->klass->ythickness;
	
	if (!nautilus_viewport_get_is_smooth (nautilus_viewport)) {
		gtk_paint_flat_box (widget->style, viewport->bin_window, 
				    GTK_STATE_NORMAL, GTK_SHADOW_NONE,
				    &tmp_area, widget, "viewportbin",
				    0, 0, -1, -1);
	}
	
	if (bin->child) {
		if (gtk_widget_intersect (bin->child, &tmp_area, &child_area)) {
			gtk_widget_draw (bin->child, &child_area);
		}
	}
}

static void
nautilus_viewport_size_allocate (GtkWidget *widget,
				 GtkAllocation *allocation)
{
	NautilusViewport *nautilus_viewport;
	GtkViewport *viewport;
	GtkBin *bin;
	GtkAllocation child_allocation;
	gint hval, vval;
	gint border_width;
	
	g_return_if_fail (widget != NULL);
	g_return_if_fail (GTK_IS_VIEWPORT (widget));
	g_return_if_fail (allocation != NULL);
	
	widget->allocation = *allocation;
	nautilus_viewport = NAUTILUS_VIEWPORT (widget);
	viewport = GTK_VIEWPORT (widget);
	bin = GTK_BIN (widget);
	  
	border_width = GTK_CONTAINER (widget)->border_width;
	
	child_allocation.x = 0;
	child_allocation.y = 0;
	
	if (viewport->shadow_type != GTK_SHADOW_NONE)
	{
		child_allocation.x = GTK_WIDGET (viewport)->style->klass->xthickness;
		child_allocation.y = GTK_WIDGET (viewport)->style->klass->ythickness;
	}
	
	child_allocation.width = MAX (1, allocation->width - child_allocation.x * 2 - border_width * 2);
	child_allocation.height = MAX (1, allocation->height - child_allocation.y * 2 - border_width * 2);
	  
	if (GTK_WIDGET_REALIZED (widget))
	{
		gdk_window_move_resize (widget->window,
					allocation->x + border_width,
					allocation->y + border_width,
					allocation->width - border_width * 2,
					allocation->height - border_width * 2);
		
		gdk_window_move_resize (viewport->view_window,
					child_allocation.x,
					child_allocation.y,
					child_allocation.width,
					child_allocation.height);
	}
	
	viewport->hadjustment->page_size = child_allocation.width;
	viewport->hadjustment->page_increment = viewport->hadjustment->page_size / 2;
	viewport->hadjustment->step_increment = 10;
	
	viewport->vadjustment->page_size = child_allocation.height;
	viewport->vadjustment->page_increment = viewport->vadjustment->page_size / 2;
	viewport->vadjustment->step_increment = 10;
	
	hval = viewport->hadjustment->value;
	vval = viewport->vadjustment->value;
	
	if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
	{
		GtkRequisition child_requisition;
		gtk_widget_get_child_requisition (bin->child, &child_requisition);
		
		viewport->hadjustment->lower = 0;
		viewport->hadjustment->upper = MAX (child_requisition.width,
						    child_allocation.width);
		
		hval = CLAMP (hval, 0,
			      viewport->hadjustment->upper -
			      viewport->hadjustment->page_size);
		
		viewport->vadjustment->lower = 0;
		viewport->vadjustment->upper = MAX (child_requisition.height,
						    child_allocation.height);
		
		vval = CLAMP (vval, 0,
			      viewport->vadjustment->upper -
			      viewport->vadjustment->page_size);
	}
	
	if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
	{
		child_allocation.x = 0;
		child_allocation.y = 0;
		
		child_allocation.width = viewport->hadjustment->upper;
		child_allocation.height = viewport->vadjustment->upper;
		
		if (nautilus_viewport_get_constrain_width (nautilus_viewport)) {
			child_allocation.width = widget->allocation.width;
		}
		if (nautilus_viewport_get_constrain_height (nautilus_viewport)) {
			child_allocation.height = widget->allocation.height;
		}
		
		if (GTK_WIDGET_REALIZED (widget))
			gdk_window_resize (viewport->bin_window,
					   child_allocation.width,
					   child_allocation.height);
		
		child_allocation.x = 0;
		child_allocation.y = 0;
		gtk_widget_size_allocate (bin->child, &child_allocation);
	}
	
	gtk_signal_emit_by_name (GTK_OBJECT (viewport->hadjustment), "changed");
	gtk_signal_emit_by_name (GTK_OBJECT (viewport->vadjustment), "changed");
	if (viewport->hadjustment->value != hval)
	{
		viewport->hadjustment->value = hval;
		gtk_signal_emit_by_name (GTK_OBJECT (viewport->hadjustment), "value_changed");
	}
	if (viewport->vadjustment->value != vval)
	{
		viewport->vadjustment->value = vval;
		gtk_signal_emit_by_name (GTK_OBJECT (viewport->vadjustment), "value_changed");
	}
}

static gint
nautilus_viewport_expose_event (GtkWidget *widget,
				GdkEventExpose *event)
{
	NautilusViewport *nautilus_viewport;
	GtkViewport *viewport;
	GtkBin *bin;
	
	g_return_val_if_fail (NAUTILUS_IS_VIEWPORT (widget), FALSE);
	g_return_val_if_fail (event != NULL, FALSE);

	if (!GTK_WIDGET_DRAWABLE (widget)) {
		return FALSE;
	}

	nautilus_viewport = NAUTILUS_VIEWPORT (widget);
	viewport = GTK_VIEWPORT (widget);
	bin = GTK_BIN (widget);
	
	if (event->window == widget->window) {
		nautilus_viewport_paint (widget, &event->area);
	} else if (event->window == viewport->bin_window) {
		GdkEventExpose child_event;

		child_event = *event;
		
		if (!nautilus_viewport_get_is_smooth (nautilus_viewport)) {
			gtk_paint_flat_box (widget->style, viewport->bin_window, 
					    GTK_STATE_NORMAL, GTK_SHADOW_NONE,
					    &event->area, widget, "viewportbin",
					    0, 0, -1, -1);
		}
		
		if ((bin->child != NULL) &&
		    GTK_WIDGET_NO_WINDOW (bin->child) &&
		    gtk_widget_intersect (bin->child, &event->area, &child_event.area))
			gtk_widget_event (bin->child, (GdkEvent*) &child_event);
	}
	
	return FALSE;
}

static void
nautilus_viewport_realize (GtkWidget *widget)
{
	NautilusViewport *nautilus_viewport;
	
	g_return_if_fail (NAUTILUS_IS_VIEWPORT (widget));
	
	nautilus_viewport = NAUTILUS_VIEWPORT (widget);

	/* GtkViewport does the actual realization */
	NAUTILUS_CALL_PARENT (GTK_WIDGET_CLASS, realize, (widget));

 	gdk_window_set_static_gravities (GTK_VIEWPORT (nautilus_viewport)->bin_window,
					 nautilus_viewport_get_is_smooth (nautilus_viewport));
}

static void
nautilus_viewport_paint (GtkWidget *widget,
			 GdkRectangle *area)
{
	GtkViewport *viewport;
	
	g_return_if_fail (widget != NULL);
	g_return_if_fail (GTK_IS_VIEWPORT (widget));
	g_return_if_fail (area != NULL);
	
	if (!GTK_WIDGET_DRAWABLE (widget)) {
		return;
	}

	viewport = GTK_VIEWPORT (widget);
	
	gtk_draw_shadow (widget->style, widget->window,
			 GTK_STATE_NORMAL, viewport->shadow_type,
			 0, 0, -1, -1);
}

/* NautilusViewport signals */
static void
nautilus_viewport_set_is_smooth_signal (GtkWidget *widget,
					gboolean is_smooth)
{
	g_return_if_fail (NAUTILUS_IS_VIEWPORT (widget));
	
	nautilus_viewport_set_is_smooth (NAUTILUS_VIEWPORT (widget), is_smooth);
}

/* Public NautilusViewport methods */
GtkWidget*
nautilus_viewport_new (GtkAdjustment *hadjustment,
		       GtkAdjustment *vadjustment)
{
	NautilusViewport *nautilus_viewport;

	nautilus_viewport = NAUTILUS_VIEWPORT (gtk_widget_new (nautilus_viewport_get_type (), NULL));
	
	gtk_viewport_set_hadjustment (GTK_VIEWPORT (nautilus_viewport), hadjustment);
	gtk_viewport_set_vadjustment (GTK_VIEWPORT (nautilus_viewport), vadjustment);

	return GTK_WIDGET (nautilus_viewport);
}

void
nautilus_viewport_set_is_smooth (NautilusViewport *nautilus_viewport,
				 gboolean is_smooth)
{
	g_return_if_fail (NAUTILUS_IS_VIEWPORT (nautilus_viewport));

	if (nautilus_viewport->details->is_smooth == is_smooth) {
		return;
	}

	nautilus_viewport->details->is_smooth = is_smooth;
	
	if (!GTK_WIDGET_REALIZED (nautilus_viewport)) {
		return;
	}
	
 	gdk_window_set_static_gravities (GTK_VIEWPORT (nautilus_viewport)->bin_window,
					 nautilus_viewport->details->is_smooth);
}

gboolean
nautilus_viewport_get_is_smooth (const NautilusViewport *nautilus_viewport)
{
	g_return_val_if_fail (NAUTILUS_IS_VIEWPORT (nautilus_viewport), FALSE);
	
	return !nautilus_viewport->details->never_smooth && nautilus_viewport->details->is_smooth;
}

void
nautilus_viewport_set_constrain_width (NautilusViewport *nautilus_viewport,
				       gboolean constrain_width)
{
	g_return_if_fail (NAUTILUS_IS_VIEWPORT (nautilus_viewport));

	nautilus_viewport->details->constrain_width = constrain_width;
}

gboolean
nautilus_viewport_get_constrain_width (const NautilusViewport *nautilus_viewport)
{
	g_return_val_if_fail (NAUTILUS_IS_VIEWPORT (nautilus_viewport), FALSE);

	return nautilus_viewport->details->constrain_width;
}

void nautilus_viewport_set_constrain_height (NautilusViewport *nautilus_viewport,
					     gboolean constrain_height)
{
	g_return_if_fail (NAUTILUS_IS_VIEWPORT (nautilus_viewport));
	
	nautilus_viewport->details->constrain_height = constrain_height;
}

gboolean
nautilus_viewport_get_constrain_height (const NautilusViewport *nautilus_viewport)
{
	g_return_val_if_fail (NAUTILUS_IS_VIEWPORT (nautilus_viewport), FALSE);

	return nautilus_viewport->details->constrain_height;
}

void
nautilus_viewport_set_never_smooth (NautilusViewport *nautilus_viewport,
				    gboolean never_smooth)
{
	g_return_if_fail (NAUTILUS_IS_VIEWPORT (nautilus_viewport));

	nautilus_viewport->details->never_smooth = never_smooth;

	if (!GTK_WIDGET_REALIZED (nautilus_viewport)) {
		return;
	}

 	gdk_window_set_static_gravities (GTK_VIEWPORT (nautilus_viewport)->bin_window,
					 nautilus_viewport_get_is_smooth (nautilus_viewport));
}

NautilusArtIPoint
nautilus_viewport_get_scroll_offset (const NautilusViewport *nautilus_viewport)
{
	NautilusArtIPoint scroll_offset;
	
	g_return_val_if_fail (NAUTILUS_IS_VIEWPORT (nautilus_viewport), NAUTILUS_ART_IPOINT_ZERO);

	if (!GTK_WIDGET_REALIZED (nautilus_viewport)) {
		return NAUTILUS_ART_IPOINT_ZERO;
	}

	gdk_window_get_position (GTK_VIEWPORT (nautilus_viewport)->bin_window,
				 &scroll_offset.x,
				 &scroll_offset.y);

	return scroll_offset;
}