glade-design-layout.c 27.7 KB
Newer Older
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3
4
/*
 * glade-design-layout.c
 *
5
 * Copyright (C) 2006-2007 Vincent Geddes
6
7
 *
 * Authors:
8
 *   Vincent Geddes <vgeddes@gnome.org>
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

Christian Persch's avatar
Christian Persch committed
26
27
#include "config.h"

28
#include "glade.h"
29
#include "glade-design-layout.h"
30
31
#include "glade-accumulators.h"
#include "glade-marshallers.h"
32
33
34

#include <gtk/gtk.h>

35
36
37
#define GLADE_DESIGN_LAYOUT_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object),  \
						 GLADE_TYPE_DESIGN_LAYOUT,               \
						 GladeDesignLayoutPrivate))
38

39
40
#define OUTLINE_WIDTH     4
#define PADDING          12
41
42
43

typedef enum 
{
44
45
46
47
48
	ACTIVITY_NONE,
	ACTIVITY_RESIZE_WIDTH,
	ACTIVITY_RESIZE_HEIGHT,
	ACTIVITY_RESIZE_WIDTH_AND_HEIGHT
} Activity;
49
50
51

typedef enum 
{
52
53
54
55
56
57
58
59
	REGION_INSIDE,
	REGION_EAST,
	REGION_SOUTH,
	REGION_SOUTH_EAST,
	REGION_WEST_OF_SOUTH_EAST,
	REGION_NORTH_OF_SOUTH_EAST,
	REGION_OUTSIDE
} PointerRegion;
60

61
62
63
64
65
66
enum
{
	WIDGET_EVENT,
	LAST_SIGNAL
};

67
68
69
70
71
72
73
74
75
struct _GladeDesignLayoutPrivate
{
	GdkWindow *event_window;
	
	GdkCursor *cursor_resize_bottom;
	GdkCursor *cursor_resize_right;
	GdkCursor *cursor_resize_bottom_right;

	/* state machine */
76
	Activity        activity;   /* the current activity */
77
	GtkRequisition *current_size_request;
78
79
80
81
	gint dx;                    /* child.width - event.pointer.x   */
	gint dy;                    /* child.height - event.pointer.y  */
	gint new_width;             /* user's new requested width */
	gint new_height;            /* user's new requested height */
82
83
};

84
85
static guint              glade_design_layout_signals[LAST_SIGNAL] = {0};

86
G_DEFINE_TYPE (GladeDesignLayout, glade_design_layout, GTK_TYPE_BIN)
87

88
static PointerRegion
89
glade_design_layout_get_pointer_region (GladeDesignLayout *layout, gint x, gint y)
90
91
{
	GladeDesignLayoutPrivate  *priv;
92
	GtkAllocation              child_allocation;
93
	PointerRegion              region = REGION_INSIDE;
94
95
96

	priv  = GLADE_DESIGN_LAYOUT_GET_PRIVATE (layout);

97
	gtk_widget_get_allocation (gtk_bin_get_child (GTK_BIN (layout)), &child_allocation);
98

99
100
	if ((x >= (child_allocation.x + child_allocation.width)) &&
	    (x < (child_allocation.x + child_allocation.width + OUTLINE_WIDTH)))
101
	{
102
103
		if ((y < (child_allocation.y + child_allocation.height - OUTLINE_WIDTH)) &&
		    (y >= child_allocation.y - OUTLINE_WIDTH))
104
			region = REGION_EAST;
105

106
107
		else if ((y < (child_allocation.y + child_allocation.height)) &&
			 (y >= (child_allocation.y + child_allocation.height - OUTLINE_WIDTH)))
108
			region = REGION_NORTH_OF_SOUTH_EAST;
109

110
111
		else if ((y < (child_allocation.y + child_allocation.height + OUTLINE_WIDTH)) &&
			 (y >= (child_allocation.y + child_allocation.height)))
112
			region = REGION_SOUTH_EAST;
113
	}
114
115
	else if ((y >= (child_allocation.y + child_allocation.height)) &&
		 (y < (child_allocation.y + child_allocation.height + OUTLINE_WIDTH)))
116
	{
117
118
		if ((x < (child_allocation.x + child_allocation.width - OUTLINE_WIDTH)) &&
		    (x >= child_allocation.x - OUTLINE_WIDTH))
119
			region = REGION_SOUTH;
120

121
122
		else if ((x < (child_allocation.x + child_allocation.width)) &&
			 (x >= (child_allocation.x + child_allocation.width - OUTLINE_WIDTH)))
123
			region = REGION_WEST_OF_SOUTH_EAST;
124

125
126
		else if ((x < (child_allocation.x + child_allocation.width + OUTLINE_WIDTH)) &&
			 (x >= (child_allocation.x + child_allocation.width)))
127
			region = REGION_SOUTH_EAST;
128
129
	}

130
	if (x < PADDING || y < PADDING ||
131
132
	    x >= (child_allocation.x + child_allocation.width + OUTLINE_WIDTH) ||
	    y >= (child_allocation.y + child_allocation.height + OUTLINE_WIDTH))
133
		region = REGION_OUTSIDE;
134

135
136
137
138
	return region;
}


139
140
/* this handler ensures that the user cannot
 * resize a widget below it minimum acceptable size */
141
142
143
144
145
146
static void
child_size_request_handler (GtkWidget         *widget,
			    GtkRequisition    *req,
			    GladeDesignLayout *layout)
{
	GladeDesignLayoutPrivate *priv;
147
148
	GtkAllocation allocation;
	GtkRequisition requisition;
149
150
	gint new_width, new_height;
	gint old_width, old_height;
151
152
153
154
155
156
		
	priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (layout);

	priv->current_size_request->width = req->width;
	priv->current_size_request->height = req->height;

157
158
159
	gtk_widget_get_allocation (widget, &allocation);
	new_width = allocation.width;
	new_height = allocation.height;
160
161
162
163
164
165
166

	if (req->width > new_width)
		new_width = req->width;

	if (req->height > new_height)
		new_height = req->height;

167
168
	if ((new_width != allocation.width) ||
	    (new_height != allocation.height))
169
	{
170
171
172
		gtk_widget_size_request (widget, &requisition);
		old_width = requisition.width;
		old_height = requisition.height;
173

174
175
176
		gtk_widget_set_size_request (widget,
					     (old_width > new_width) ? old_width : new_width,
					     (old_height > new_height) ? old_height : new_height);
177
178
	}
	
179
	gtk_widget_queue_draw (GTK_WIDGET (layout));
180
181
182
	
}

183
184
static gboolean
glade_design_layout_leave_notify_event (GtkWidget *widget, GdkEventCrossing *ev)
185
{
186
	GtkWidget *child;
187
188
	GladeDesignLayoutPrivate *priv;

189
	if ((child = gtk_bin_get_child (GTK_BIN (widget))) == NULL)
190
		return FALSE;
191

192
	priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (widget);
193

194
	if (priv->activity == ACTIVITY_NONE)
195
		gdk_window_set_cursor (priv->event_window, NULL);
196
197
198
	
	return FALSE;
}
199
200


201
202
203
204
205
206
207
static void
glade_design_layout_update_child (GladeDesignLayout *layout, 
				  GtkWidget         *child,
				  GtkAllocation     *allocation)
{
	GladeDesignLayoutPrivate *priv;
	GladeWidget *gchild;
208

209
	priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (layout);
210

211
212
213
214
215
216
	/* Update GladeWidget metadata */
	gchild = glade_widget_get_from_gobject (child);
	g_object_set (gchild, 
		      "toplevel-width", allocation->width,
		      "toplevel-height", allocation->height,
		      NULL);
217

218
219
	gtk_widget_size_allocate (child, allocation);
	gtk_widget_queue_resize (GTK_WIDGET (layout));
220
221
}

222

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239



/* A temp data struct that we use when looking for a widget inside a container
 * we need a struct, because the forall can only pass one pointer
 */
typedef struct {
	gint x;
	gint y;
	gboolean   any;
	GtkWidget *found;
	GtkWidget *toplevel;
} GladeFindInContainerData;

static void
glade_design_layout_find_inside_container (GtkWidget *widget, GladeFindInContainerData *data)
{
240
	GtkAllocation allocation;
241
242
243
244
	gint x;
	gint y;

	gtk_widget_translate_coordinates (data->toplevel, widget, data->x, data->y, &x, &y);
245
	gtk_widget_get_allocation (widget, &allocation);
246
247

	if (GTK_WIDGET_MAPPED(widget) &&
248
	    x >= 0 && x < allocation.width && y >= 0 && y < allocation.height)
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
	{
		if (glade_widget_get_from_gobject (widget) || data->any)
			data->found = widget;
		else if (GTK_IS_CONTAINER (widget))
		{
			/* Recurse and see if any project objects exist
			 * under this container that is not in the project
			 * (mostly for dialog buttons).
			 */
			GladeFindInContainerData search;
			search.x = data->x;
			search.y = data->y;
			search.toplevel = data->toplevel;
			search.found = NULL;

			gtk_container_forall (GTK_CONTAINER (widget), (GtkCallback)
					      glade_design_layout_find_inside_container, &search);

			data->found = search.found;
		}
	}
}

static GladeWidget *
glade_design_layout_deepest_gwidget_at_position (GtkContainer *toplevel,
						 GtkContainer *container,
						 gint top_x, gint top_y)
{
	GladeFindInContainerData data;
	GladeWidget *ret_widget = NULL;

	data.x = top_x;
	data.y = top_y;
	data.any = FALSE;
	data.toplevel = GTK_WIDGET (toplevel);
	data.found = NULL;

	gtk_container_forall (container, (GtkCallback)
			      glade_design_layout_find_inside_container, &data);

289
290
291
292
293
294
295
296
297
298
	if (data.found)
	{
		if (GTK_IS_CONTAINER (data.found))
			ret_widget = glade_design_layout_deepest_gwidget_at_position
				(toplevel, GTK_CONTAINER (data.found), top_x, top_y);
		else
			ret_widget = glade_widget_get_from_gobject (data.found);
	}

	if (!ret_widget)
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
		ret_widget = glade_widget_get_from_gobject (container);

	return ret_widget;
}


static GtkWidget *
glade_design_layout_deepest_widget_at_position (GtkContainer *toplevel,
						GtkContainer *container,
						gint top_x, gint top_y)
{
	GladeFindInContainerData data;
	GtkWidget *ret_widget = NULL;

	data.x = top_x;
	data.y = top_y;
	data.any = TRUE;
	data.toplevel = GTK_WIDGET (toplevel);
	data.found = NULL;

	gtk_container_forall (container, (GtkCallback)
			      glade_design_layout_find_inside_container, &data);

	if (data.found && GTK_IS_CONTAINER (data.found))
		ret_widget = glade_design_layout_deepest_widget_at_position
			(toplevel, GTK_CONTAINER (data.found), top_x, top_y);
	else if (data.found)
		ret_widget = data.found;
	else
		ret_widget = GTK_WIDGET (container);

	return ret_widget;
}

333
static gboolean
334
glade_design_layout_motion_notify_event (GtkWidget *widget, GdkEventMotion *ev)
335
{
336
337
338
	GtkWidget                *child;
	GladeDesignLayoutPrivate *priv;
	GladeWidget              *child_glade_widget;
339
	PointerRegion        region;
340
341
342
343
	GtkAllocation             allocation;
	gint                      x, y;
	gint                      new_width, new_height;

344
	if ((child = gtk_bin_get_child (GTK_BIN (widget))) == NULL)
345
346
347
		return FALSE;
	
	priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (widget);
348

349
	gdk_window_get_pointer (priv->event_window, &x, &y, NULL);
350
	
351
	child_glade_widget = glade_widget_get_from_gobject (child);
352
	gtk_widget_get_allocation (child, &allocation);
353

354
	if (priv->activity == ACTIVITY_RESIZE_WIDTH)
355
356
357
358
359
360
361
	{
		new_width = x - priv->dx - PADDING - OUTLINE_WIDTH;
			
		if (new_width < priv->current_size_request->width)
			new_width = priv->current_size_request->width;
		
		allocation.width = new_width;
362

363
364
365
		glade_design_layout_update_child (GLADE_DESIGN_LAYOUT (widget), 
						  child, &allocation);
	}
366
	else if (priv->activity == ACTIVITY_RESIZE_HEIGHT)
367
368
369
370
371
372
373
	{
		new_height = y - priv->dy - PADDING - OUTLINE_WIDTH;
		
		if (new_height < priv->current_size_request->height)
			new_height = priv->current_size_request->height;
		
		allocation.height = new_height;
374

375
376
377
		glade_design_layout_update_child (GLADE_DESIGN_LAYOUT (widget), 
						  child, &allocation);
	}
378
	else if (priv->activity == ACTIVITY_RESIZE_WIDTH_AND_HEIGHT)
379
380
381
382
383
384
385
386
387
388
389
390
	{
		new_width =  x - priv->dx - PADDING - OUTLINE_WIDTH;
		new_height = y - priv->dy - PADDING - OUTLINE_WIDTH;
		
		if (new_width < priv->current_size_request->width)
			new_width = priv->current_size_request->width;
		if (new_height < priv->current_size_request->height)
			new_height = priv->current_size_request->height;
		
		
		allocation.height = new_height;
		allocation.width  = new_width;
391

392
393
394
395
396
397
398
		glade_design_layout_update_child (GLADE_DESIGN_LAYOUT (widget), 
						  child, &allocation);
	}
	else
	{
		region = glade_design_layout_get_pointer_region (GLADE_DESIGN_LAYOUT (widget), x, y);
		
399
		if (region == REGION_EAST)
400
			gdk_window_set_cursor (priv->event_window, priv->cursor_resize_right);
401
		
402
		else if (region == REGION_SOUTH)
403
			gdk_window_set_cursor (priv->event_window, priv->cursor_resize_bottom);
404
		
405
406
407
		else if (region == REGION_SOUTH_EAST ||
			 region == REGION_WEST_OF_SOUTH_EAST ||
			 region == REGION_NORTH_OF_SOUTH_EAST)
408
409
			gdk_window_set_cursor (priv->event_window, priv->cursor_resize_bottom_right);

410
		else if (region == REGION_OUTSIDE)
411
			gdk_window_set_cursor (priv->event_window, NULL);
412
	}
413
	
414
415
416
417
418
419
	return FALSE;
}

static gboolean
glade_design_layout_button_press_event (GtkWidget *widget, GdkEventButton *ev)
{
420
	GtkWidget                *child;
421
422
	GtkAllocation             child_allocation;
	PointerRegion             region;
423
424
425
	GladeDesignLayoutPrivate *priv;
	gint                      x, y;

426
	if ((child = gtk_bin_get_child (GTK_BIN (widget))) == NULL)
427
		return FALSE;
428

429
430
431
	priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (widget);

	gdk_window_get_pointer (priv->event_window, &x, &y, NULL);
432
	region = glade_design_layout_get_pointer_region (GLADE_DESIGN_LAYOUT (widget), x, y);
433

434
435
	if (((GdkEventButton *) ev)->button == 1) 
	{
436
437
438
		gtk_widget_get_allocation (child, &child_allocation);
		priv->dx = x - (child_allocation.x + child_allocation.width);
		priv->dy = y - (child_allocation.y + child_allocation.height);
439
		
440
		if (region == REGION_EAST) 
441
		{
442
			priv->activity = ACTIVITY_RESIZE_WIDTH;
443
			gdk_window_set_cursor (priv->event_window, priv->cursor_resize_right);
444
		} 
445
		if (region == REGION_SOUTH) 
446
		{
447
			priv->activity = ACTIVITY_RESIZE_HEIGHT;
448
			gdk_window_set_cursor (priv->event_window, priv->cursor_resize_bottom);
449
		} 
450
		if (region == REGION_SOUTH_EAST) 
451
		{
452
			priv->activity = ACTIVITY_RESIZE_WIDTH_AND_HEIGHT;
453
			gdk_window_set_cursor (priv->event_window, priv->cursor_resize_bottom_right);
454
		} 
455
		if (region == REGION_WEST_OF_SOUTH_EAST) 
456
		{
457
			priv->activity = ACTIVITY_RESIZE_WIDTH_AND_HEIGHT;
458
			gdk_window_set_cursor (priv->event_window, priv->cursor_resize_bottom_right);
459
		} 
460
		if (region == REGION_NORTH_OF_SOUTH_EAST) 
461
		{
462
			priv->activity = ACTIVITY_RESIZE_WIDTH_AND_HEIGHT;
463
			gdk_window_set_cursor (priv->event_window, priv->cursor_resize_bottom_right);
464
465
466
		}
	}
		
467
468
469
470
471
472
	return FALSE;
}

static gboolean
glade_design_layout_button_release_event (GtkWidget *widget, GdkEventButton *ev)
{
473
474
	GladeDesignLayoutPrivate *priv;
	GtkWidget                *child;
475

476
	if ((child = gtk_bin_get_child (GTK_BIN (widget))) == NULL)
477
478
479
480
		return FALSE;

	priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (widget);

481
	priv->activity = ACTIVITY_NONE;
482
	gdk_window_set_cursor (priv->event_window, NULL);
483
484
485
486
487
488
489
490
491
492

	return FALSE;
}

static void
glade_design_layout_size_request (GtkWidget *widget, GtkRequisition *requisition)
{
	GladeDesignLayoutPrivate *priv;
	GtkRequisition child_requisition;
	GtkWidget *child;
493
	GladeWidget *gchild;
494
495
	gint child_width = 0;
	gint child_height = 0;
496
	guint border_width = 0;
497
498
499
500
501
502

	priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (widget);

	requisition->width = 0;
	requisition->height = 0;

503
	child = gtk_bin_get_child (GTK_BIN (widget));
504

505
	if (child && gtk_widget_get_visible (child))
506
	{
507
508
509
		gchild = glade_widget_get_from_gobject (child);
		g_assert (gchild);

510
511
		gtk_widget_size_request (child, &child_requisition);

512
513
514
515
		g_object_get (gchild, 
			      "toplevel-width", &child_width,
			      "toplevel-height", &child_height,
			      NULL);
516
517
			
		child_width = MAX (child_width, child_requisition.width);
518
		child_height = MAX (child_height, child_requisition.height);
519
520
521
522
523
524
525
526

		requisition->width = MAX (requisition->width,
		                          2 * PADDING + child_width + 2 * OUTLINE_WIDTH);

		requisition->height = MAX (requisition->height,
		                          2 * PADDING + child_height + 2 * OUTLINE_WIDTH);
	}

527
528
529
	border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
	requisition->width += border_width * 2;
	requisition->height += border_width * 2;
530
531
532
533
534
535
}

static void
glade_design_layout_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
{
	GladeDesignLayoutPrivate *priv;
536
	GladeWidget *gchild;
537
	GtkRequisition child_requisition;
538
	GtkAllocation child_allocation, widget_allocation;
539
540
541
542
543
544
545
	GtkWidget *child;
	gint border_width;
	gint child_width = 0;
	gint child_height = 0;

	priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (widget);

546
547
	gtk_widget_set_allocation (widget, allocation);
	border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
548
549
550
551
552

	if (GTK_WIDGET_REALIZED (widget))
	{
		if (priv->event_window)
			gdk_window_move_resize (priv->event_window,
553
554
555
556
						allocation->x,
						allocation->y,
						allocation->width,
						allocation->height);
557
558
559

	}

560
	child = gtk_bin_get_child (GTK_BIN (widget));
561

562
	if (child && gtk_widget_get_visible (child))
563
	{
564
565
566
		gchild = glade_widget_get_from_gobject (child);
		g_assert (gchild);

567
568
		gtk_widget_get_child_requisition (child, &child_requisition);

569
570
571
572
573
574
575
		g_object_get (gchild, 
			      "toplevel-width", &child_width,
			      "toplevel-height", &child_height,
			      NULL);

		child_width = MAX (child_width, child_requisition.width);
		child_height = MAX (child_height, child_requisition.height);
576

577
578
579
		gtk_widget_get_allocation (widget, &widget_allocation);
		child_allocation.x = widget_allocation.x + border_width + PADDING + OUTLINE_WIDTH;
		child_allocation.y = widget_allocation.y + border_width + PADDING + OUTLINE_WIDTH;
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597

		child_allocation.width = child_width - 2 * border_width;
		child_allocation.height = child_height - 2 * border_width;

		gtk_widget_size_allocate (child, &child_allocation);
	}
}

static void
glade_design_layout_map (GtkWidget *widget)
{
	GladeDesignLayoutPrivate *priv;

	priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (widget);

	if (priv->event_window)
		gdk_window_show (priv->event_window);

598
	GTK_WIDGET_CLASS (glade_design_layout_parent_class)->map (widget);
599
600
601
602
603
604
605
606
607
608

}

static void
glade_design_layout_unmap (GtkWidget *widget)
{
	GladeDesignLayoutPrivate *priv;	

	priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (widget);

609
	GTK_WIDGET_CLASS (glade_design_layout_parent_class)->unmap (widget);
610
611
612
613
614
615
616
617
618

	if (priv->event_window)
		gdk_window_hide (priv->event_window);
}

static void
glade_design_layout_realize (GtkWidget *widget)
{
	GladeDesignLayoutPrivate *priv;
619
620
621
	GtkAllocation allocation;
	GtkStyle *style;
	GdkWindow *window;
622
623
	GdkWindowAttr attributes;
	gint attributes_mask;
624
	guint border_width;
625
626
627
628
629

	priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (widget);

	GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);

630
	border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
631

632
633
634
635
636
	gtk_widget_get_allocation (widget, &allocation);
	attributes.x = allocation.x;
	attributes.y = allocation.y;
	attributes.width = allocation.width;
	attributes.height = allocation.height;
637
638
639

	attributes.window_type = GDK_WINDOW_CHILD;
	attributes.wclass = GDK_INPUT_ONLY;
640
641
642
643
644
645
646
647
648
	attributes.event_mask = 
		gtk_widget_get_events (widget) |
		GDK_POINTER_MOTION_MASK        |
		GDK_POINTER_MOTION_HINT_MASK   |
		GDK_BUTTON_PRESS_MASK          |
		GDK_BUTTON_RELEASE_MASK        |
		GDK_EXPOSURE_MASK              |
		GDK_ENTER_NOTIFY_MASK          |
		GDK_LEAVE_NOTIFY_MASK;
649
650
	attributes_mask = GDK_WA_X | GDK_WA_Y;

651
652
	window = gtk_widget_get_parent_window (widget);
	gtk_widget_set_window (widget, window);
653
654
655
656
657

	priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
					     &attributes, attributes_mask);
	gdk_window_set_user_data (priv->event_window, widget);

658
659
660
	style = gtk_style_attach (gtk_widget_get_style (widget), window);
	gtk_widget_set_style (widget, style);
	gtk_style_set_background (style, window, GTK_STATE_NORMAL);
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
}

static void
glade_design_layout_unrealize (GtkWidget *widget)
{
	GladeDesignLayoutPrivate *priv;

	priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (widget);

	if (priv->event_window)
	{
		gdk_window_set_user_data (priv->event_window, NULL);
		gdk_window_destroy (priv->event_window);
		priv->event_window = NULL;
	}

677
	GTK_WIDGET_CLASS (glade_design_layout_parent_class)->unrealize (widget);
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694

}

static void
glade_design_layout_add (GtkContainer *container, GtkWidget *widget)
{
	GladeDesignLayout *layout;

	layout = GLADE_DESIGN_LAYOUT (container);

	layout->priv->current_size_request->width = 0;
	layout->priv->current_size_request->height = 0;

	g_signal_connect (G_OBJECT (widget), "size-request", 
			  G_CALLBACK (child_size_request_handler),
			  container);

695
	GTK_CONTAINER_CLASS (glade_design_layout_parent_class)->add (container, widget);
696
697

	gdk_window_lower (layout->priv->event_window);
698
699
700
701
702
703
704
705
706
}

static void
glade_design_layout_remove (GtkContainer *container, GtkWidget *widget)
{
	g_signal_handlers_disconnect_by_func(G_OBJECT (widget), 
					     G_CALLBACK (child_size_request_handler), 
					     container);	

707
	GTK_CONTAINER_CLASS (glade_design_layout_parent_class)->remove (container, widget);
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
}

static void
glade_design_layout_dispose (GObject *object)
{
	GladeDesignLayoutPrivate *priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (object);

	if (priv->cursor_resize_bottom != NULL) {
		gdk_cursor_unref (priv->cursor_resize_bottom);
		priv->cursor_resize_bottom = NULL;
	}
	if (priv->cursor_resize_right != NULL) {
		gdk_cursor_unref (priv->cursor_resize_right);	
		priv->cursor_resize_right = NULL;
	}
	if (priv->cursor_resize_bottom_right != NULL) {	
		gdk_cursor_unref (priv->cursor_resize_bottom_right);
		priv->cursor_resize_bottom_right = NULL;
	}

728
	G_OBJECT_CLASS (glade_design_layout_parent_class)->dispose (object);
729
730
731
732
733
734
735
}

static void
glade_design_layout_finalize (GObject *object)
{
	GladeDesignLayoutPrivate *priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (object);

736
	g_slice_free (GtkRequisition, priv->current_size_request);
737
	
738
	G_OBJECT_CLASS (glade_design_layout_parent_class)->finalize (object);
739
740
}

741
742
743
744
745
746
747
748
749
static inline void
draw_frame (GtkWidget *widget,
	    cairo_t   *cr,
	    int x, int y, int w, int h)
{	
	cairo_set_line_width (cr, OUTLINE_WIDTH);
	cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
	cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);

750
	gdk_cairo_set_source_color (cr, &gtk_widget_get_style (widget)->bg[GTK_STATE_SELECTED]);
751
752
753
754
755
756
757
758

	/* rectangle */
	cairo_move_to (cr, x, y);
	cairo_rel_line_to (cr, 0, h);
	cairo_rel_line_to (cr, w, 0);
	cairo_rel_line_to (cr, 0, -h);
	cairo_close_path (cr);	
	cairo_stroke (cr);
759
	
Vincent Geddes's avatar
Vincent Geddes committed
760
761
#if 0
	/* shadow effect */
762
763
764
765
766
767
	cairo_set_source_rgba (cr, 0, 0, 0, 0.04);
		
	cairo_move_to (cr, x + OUTLINE_WIDTH, y + h + OUTLINE_WIDTH);
	cairo_rel_line_to (cr, w, 0);
	cairo_rel_line_to (cr, 0, -h);
	cairo_stroke (cr);  	      
Vincent Geddes's avatar
Vincent Geddes committed
768
#endif
769
770
}

771
772
773
774
static gboolean
glade_design_layout_expose_event (GtkWidget *widget, GdkEventExpose *ev)
{
	GladeDesignLayout *layout;
775
776
	GtkStyle *style;
	GtkAllocation allocation, child_allocation;
777
	GtkWidget *child;
778
	GdkWindow *window;
779
780
	gint x, y, w, h;
	gint border_width;
781
	cairo_t *cr;
782
783
784

	layout = GLADE_DESIGN_LAYOUT (widget);

785
	border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
786

787
788
789
	window = gtk_widget_get_window (widget);
	style = gtk_widget_get_style (widget);
	gtk_widget_get_allocation (widget, &allocation);
790
	/* draw a white widget background */
791
792
	gdk_draw_rectangle (window,
			    style->base_gc [gtk_widget_get_state (widget)],
793
			    TRUE,
794
795
796
797
			    allocation.x + border_width,
			    allocation.y + border_width,
			    allocation.width - 2 * border_width,
			    allocation.height - 2 * border_width);
798

799
	child = gtk_bin_get_child (GTK_BIN (widget));
800

801
	if (child && gtk_widget_get_visible (child))
802
	{
803
804
805
806
807
		gtk_widget_get_allocation (child, &child_allocation);
		x = child_allocation.x - OUTLINE_WIDTH / 2;
		y = child_allocation.y - OUTLINE_WIDTH / 2;
		w = child_allocation.width + OUTLINE_WIDTH;
		h = child_allocation.height + OUTLINE_WIDTH;
808

809
		/* draw frame */
810
		cr = gdk_cairo_create (window);
811
812
813
814
		
		draw_frame (widget, cr, x, y, w, h);
		
		cairo_destroy (cr);
815

816
817
		/* draw a filled rectangle in case child does not draw 
		 * it's own background (a GTK_WIDGET_NO_WINDOW child). */
818
819
		gdk_draw_rectangle (window,
				    style->bg_gc[GTK_STATE_NORMAL],
820
				    TRUE,
821
				    x + OUTLINE_WIDTH / 2, y + OUTLINE_WIDTH / 2,
822
				    w - OUTLINE_WIDTH, h - OUTLINE_WIDTH);
823
824

		GTK_WIDGET_CLASS (glade_design_layout_parent_class)->expose_event (widget, ev);
825
826
	}	

827
828
829
	return TRUE;
}

830
831
832
833
834
835
836
837
838
839
840
841
842
843
/**
 * glade_design_layout_widget_event:
 * @layout:        A #GladeDesignLayout
 * @event_gwidget: the #GladeWidget that recieved event notification
 * @event:         the #GdkEvent
 *
 * This is called internally by a #GladeWidget recieving an event,
 * it will marshall the event to the proper #GladeWidget according
 * to its position in @layout.
 *
 * Returns: Whether or not the event was handled by the retrieved #GladeWidget
 */
gboolean
glade_design_layout_widget_event (GladeDesignLayout *layout,
844
				  GladeWidget       *event_gwidget,
845
				  GdkEvent          *event)
846
{
847
848
849
850
	GladeWidget *gwidget;
	GtkWidget   *child;
	gboolean     retval;
	gint         x, y;
851
	
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
	gtk_widget_get_pointer (GTK_WIDGET (layout), &x, &y);
	gwidget = glade_design_layout_deepest_gwidget_at_position
		(GTK_CONTAINER (layout), GTK_CONTAINER (layout), x, y);

	child = glade_design_layout_deepest_widget_at_position 
		(GTK_CONTAINER (layout), GTK_CONTAINER (layout), x, y);

	/* First try a placeholder */
	if (GLADE_IS_PLACEHOLDER (child) && event->type != GDK_EXPOSE)
	{
		retval = gtk_widget_event (child, event);

		if (retval)
			return retval;
	}

	/* Then we try a GladeWidget */
	if (gwidget) 
	{
871
872
		g_signal_emit_by_name (layout, "widget-event",
				       gwidget, event, &retval);
873
874
875
876
877
878
879
880

		if (retval)
			return retval;
	}

	return FALSE;
}

881
882
883
884
885
886
887
888
889
890
891
892
893
static gboolean
glade_design_layout_widget_event_impl (GladeProject *project,
				       GladeWidget *gwidget,
				       GdkEvent *event)
{

	if (glade_widget_event (gwidget, event))
		return GLADE_WIDGET_EVENT_STOP_EMISSION |
			GLADE_WIDGET_EVENT_RETURN_TRUE;
	else
		return 0;
}

894
895
896
897
898
899
900
static void
glade_design_layout_init (GladeDesignLayout *layout)
{
	GladeDesignLayoutPrivate *priv;

	layout->priv = priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (layout);

901
	gtk_widget_set_has_window (GTK_WIDGET (layout), FALSE);
902
903

	priv->event_window = NULL;
904
	priv->activity = ACTIVITY_NONE;
905

906
	priv->current_size_request = g_slice_new0 (GtkRequisition);
907
908
909
910
911
912
913
914
915
916

	priv->cursor_resize_bottom       = gdk_cursor_new (GDK_BOTTOM_SIDE);
	priv->cursor_resize_right        = gdk_cursor_new (GDK_RIGHT_SIDE);
	priv->cursor_resize_bottom_right = gdk_cursor_new (GDK_BOTTOM_RIGHT_CORNER);

	priv->new_width = -1;
	priv->new_height = -1;
}

static void
917
glade_design_layout_class_init (GladeDesignLayoutClass *klass)
918
919
920
921
922
{
	GObjectClass       *object_class;
	GtkWidgetClass     *widget_class;
	GtkContainerClass  *container_class;

923
924
925
	object_class = G_OBJECT_CLASS (klass);
	widget_class = GTK_WIDGET_CLASS (klass);
	container_class = GTK_CONTAINER_CLASS (klass);
926
927
928
929
930
931

	object_class->dispose               = glade_design_layout_dispose;	
	object_class->finalize              = glade_design_layout_finalize;
	
	container_class->add                = glade_design_layout_add;
	container_class->remove             = glade_design_layout_remove;
932

933
934
935
936
937
938
939
940
941
942
943
944
	widget_class->motion_notify_event   = glade_design_layout_motion_notify_event;
	widget_class->leave_notify_event    = glade_design_layout_leave_notify_event;
	widget_class->button_press_event    = glade_design_layout_button_press_event;
	widget_class->button_release_event  = glade_design_layout_button_release_event;
	widget_class->realize               = glade_design_layout_realize;
	widget_class->unrealize             = glade_design_layout_unrealize;
	widget_class->map                   = glade_design_layout_map;
	widget_class->unmap                 = glade_design_layout_unmap;
	widget_class->expose_event          = glade_design_layout_expose_event;
	widget_class->size_request          = glade_design_layout_size_request;
	widget_class->size_allocate         = glade_design_layout_size_allocate;

945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
	klass->widget_event          = glade_design_layout_widget_event_impl;

	/**
	 * GladeDesignLayout::widget-event:
	 * @glade_design_layout: the #GladeDesignLayout containing the #GladeWidget.
	 * @signal_editor: the #GladeWidget which received the signal.
	 * @event: the #GdkEvent
	 *
	 * Emitted when a widget event received.
	 * Connect before the default handler to block glade logic,
	 * such as selecting or resizing, by returning #GLADE_WIDGET_EVENT_STOP_EMISSION.
	 * If you want to handle the event after it passed the glade logic,
	 * but before it reaches the widget, you should connect after the default handler.
	 *
	 * Returns: #GLADE_WIDGET_EVENT_STOP_EMISSION flag to abort futher emission of the signal.
	 * #GLADE_WIDGET_EVENT_RETURN_TRUE flag in the last handler
	 * to stop propagating of the event to the appropriate widget.
	 */
	glade_design_layout_signals[WIDGET_EVENT] =
		g_signal_new ("widget-event",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (GladeDesignLayoutClass, widget_event),
			      glade_integer_handled_accumulator, NULL,
			      glade_marshal_INT__OBJECT_BOXED,
			      G_TYPE_INT,
			      2,
			      GLADE_TYPE_WIDGET,
			      GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);

975
976
977
978
979
980
	g_type_class_add_private (object_class, sizeof (GladeDesignLayoutPrivate));
}

GtkWidget*
glade_design_layout_new (void)
{
981
	return g_object_new (GLADE_TYPE_DESIGN_LAYOUT, NULL);
982
}
983