glade-design-layout.c 66.4 KB
Newer Older
1
2
3
/*
 * glade-design-layout.c
 *
4
 * Copyright (C) 2006-2007 Vincent Geddes
5
 *                    2011 Juan Pablo Ugarte
6
7
 *
 * Authors:
8
 *   Vincent Geddes <vgeddes@gnome.org>
9
 *   Juan Pablo Ugarte <juanpablougarte@gmail.com>
10
 *
11
12
13
14
 * This library 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.1 of
 * the License, or (at your option) any later version.
15
 *
16
17
18
19
 * 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
 * Lesser General Public License for more details.
20
 *
21
22
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this program; if not, write to the Free Software
23
24
25
26
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

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

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

35
#include <glib/gi18n-lib.h>
36
37
#include <gtk/gtk.h>

38
39
40
#define GLADE_DESIGN_LAYOUT_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object),  \
						 GLADE_TYPE_DESIGN_LAYOUT,               \
						 GladeDesignLayoutPrivate))
41

42
#define OUTLINE_WIDTH     4
43
#define PADDING           12
44

45
#define MARGIN_STEP       6
46

47
typedef enum
48
{
49
50
51
  ACTIVITY_NONE,
  ACTIVITY_RESIZE_WIDTH,
  ACTIVITY_RESIZE_HEIGHT,
52
  ACTIVITY_RESIZE_WIDTH_AND_HEIGHT,
53
  ACTIVITY_ALIGNMENTS,
54
55
56
57
58
59
60
61
  ACTIVITY_MARGINS,
  ACTIVITY_MARGINS_VERTICAL, /* These activities are only used to set the cursor */
  ACTIVITY_MARGINS_HORIZONTAL,
  ACTIVITY_MARGINS_TOP_LEFT,
  ACTIVITY_MARGINS_TOP_RIGHT,
  ACTIVITY_MARGINS_BOTTOM_LEFT,
  ACTIVITY_MARGINS_BOTTOM_RIGHT,
  N_ACTIVITY
62
} Activity;
63

64
65
66
67
68
69
70
71
72

typedef enum
{
  MARGIN_TOP    = 1 << 0,
  MARGIN_BOTTOM = 1 << 1,
  MARGIN_LEFT   = 1 << 2,
  MARGIN_RIGHT  = 1 << 3
} Margins;

73
74
struct _GladeDesignLayoutPrivate
{
75
  GdkWindow *window, *offscreen_window;
76

77
78
  gint child_offset;
  GdkRectangle east, south, south_east;
79
80
  GdkCursor *cursor;            /* Current cursor */
  GdkCursor *cursors[N_ACTIVITY];
81
82

  gint current_width, current_height;
83
  PangoLayout *widget_name;
84
  gint layout_width;
85
86

  /* Colors */
87
  GdkRGBA fg_color;
88
89
90
  GdkRGBA frame_color[2];
  GdkRGBA frame_color_active[2];

91
92
  /* Margin edit mode */
  GtkWidget *selection;
93
  gint top, bottom, left, right;
94
  gint m_dy, m_dx;
95
  gint max_width, max_height;
96
  Margins margin;
97
  GtkAlign valign, halign;
98
  Margins node_over;
99

100
101
102
103
104
105
  /* state machine */
  Activity activity;            /* the current activity */
  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 */
106
107
108

  /* Properties */
  GladeDesignView *view;
109
  GladeProject *project;
110
111
112
113
114
115
};

enum
{
  PROP_0,
  PROP_DESIGN_VIEW
116
117
};

118
G_DEFINE_TYPE (GladeDesignLayout, glade_design_layout, GTK_TYPE_BIN)
119

120
#define RECTANGLE_POINT_IN(rect,x,y) (x >= rect.x && x <= (rect.x + rect.width) && y >= rect.y && y <= (rect.y + rect.height))
121

122
static Margins
123
gdl_get_margins_from_pointer (GtkWidget *child, GtkWidget *widget, gint x, gint y)
124
{
125
  gint width, height, xx, yy, top, bottom, left, right;
126
127
  Margins margin = 0;
  GdkRectangle rec;
128
129
130
  
  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);
131

132
133
  gtk_widget_translate_coordinates (widget, child, 0, 0, &xx, &yy);
  
134
135
136
137
138
139
140
  top = gtk_widget_get_margin_top (widget);
  bottom = gtk_widget_get_margin_bottom (widget);
  left = gtk_widget_get_margin_left (widget);
  right = gtk_widget_get_margin_right (widget);

  rec.x = xx - left - OUTLINE_WIDTH;
  rec.y = yy - top - OUTLINE_WIDTH;
141
142
  rec.width = width + left + right + (OUTLINE_WIDTH * 2);
  rec.height = height + top + bottom + (OUTLINE_WIDTH * 2);
143
144
145
146

  if (RECTANGLE_POINT_IN (rec, x, y))
    {      
      if (y <= yy + OUTLINE_WIDTH) margin |= MARGIN_TOP;
147
      else if (y >= yy + height - OUTLINE_WIDTH) margin |= MARGIN_BOTTOM;
148
149
      
      if (x <= xx + OUTLINE_WIDTH) margin |= MARGIN_LEFT;
150
      else if (x >= xx + width - OUTLINE_WIDTH) margin |= MARGIN_RIGHT;
151
152
153
154
155
    }

  return margin;
}

156
static Activity
157
gdl_get_activity_from_pointer (GladeDesignLayout *layout, gint x, gint y)
158
{
159
  GladeDesignLayoutPrivate *priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (layout);
160
161
  
  if (priv->selection)
162
    {
163
164
      priv->margin = gdl_get_margins_from_pointer (gtk_bin_get_child (GTK_BIN (layout)),
                                                   priv->selection,
165
166
167
                                                   x - priv->child_offset,
                                                   y - priv->child_offset);
      
168
169
170
171
172
      if (priv->margin)
        {
          GladePointerMode mode = glade_project_get_pointer_mode (priv->project);
          return (mode == GLADE_POINTER_ALIGN_EDIT) ? ACTIVITY_ALIGNMENTS : ACTIVITY_MARGINS;
        }
173
174
    }
  
175
  if (RECTANGLE_POINT_IN (priv->south_east, x, y)) return ACTIVITY_RESIZE_WIDTH_AND_HEIGHT;
176

177
  if (RECTANGLE_POINT_IN (priv->east, x, y)) return ACTIVITY_RESIZE_WIDTH;
178

179
  if (RECTANGLE_POINT_IN (priv->south, x, y)) return ACTIVITY_RESIZE_HEIGHT;
180

181
  return ACTIVITY_NONE;
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
static void
gdl_set_cursor (GladeDesignLayoutPrivate *priv, GdkCursor *cursor)
{
  if (cursor != priv->cursor)
    {
      priv->cursor = cursor;
      gdk_window_set_cursor (priv->window, cursor);
    }
}

static Activity
gdl_margin_get_activity (Margins margin)
{
  if (margin & MARGIN_TOP)
    {
      if (margin & MARGIN_LEFT)
        return ACTIVITY_MARGINS_TOP_LEFT;
      else if (margin & MARGIN_RIGHT)
        return ACTIVITY_MARGINS_TOP_RIGHT;
      else
        return ACTIVITY_MARGINS_VERTICAL;
    }
  else if (margin & MARGIN_BOTTOM)
    {
      if (margin & MARGIN_LEFT)
        return ACTIVITY_MARGINS_BOTTOM_LEFT;
      else if (margin & MARGIN_RIGHT)
        return ACTIVITY_MARGINS_BOTTOM_RIGHT;
      else
        return ACTIVITY_MARGINS_VERTICAL;
    }
  else if (margin & MARGIN_LEFT || margin & MARGIN_RIGHT)
    return ACTIVITY_MARGINS_HORIZONTAL;

  return ACTIVITY_NONE;
}

221
static gboolean
222
glade_design_layout_leave_notify_event (GtkWidget *widget, GdkEventCrossing *ev)
223
{
224
225
226
  GtkWidget *child;
  GladeDesignLayoutPrivate *priv;

227
228
  if ((child = gtk_bin_get_child (GTK_BIN (widget))) == NULL ||
      ev->window != gtk_widget_get_window (widget))
229
    return FALSE;
230

231
  priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (widget);
232

233
  if (priv->activity == ACTIVITY_NONE)
234
    gdl_set_cursor (priv, NULL);
235

236
  return FALSE;
237
}
238

239
static void
240
gdl_update_max_margins (GladeDesignLayout *layout,
241
242
243
                        GtkWidget *child,
                        gint width, gint height)
{
244
245
  GladeDesignLayoutPrivate *priv = layout->priv;
  gint parent_w, parent_h, layout_w, layout_h;
246
247
248
249
250
251
252
253
254
  gint top, bottom, left, right;
  GtkRequisition req;
  
  gtk_widget_get_preferred_size (child, &req, NULL);

  top = gtk_widget_get_margin_top (priv->selection);
  bottom = gtk_widget_get_margin_bottom (priv->selection);
  left = gtk_widget_get_margin_left (priv->selection);
  right = gtk_widget_get_margin_right (priv->selection);
255
  
256
  priv->max_width = width - (req.width - left - right);
257

258
  parent_w = gtk_widget_get_allocated_width (GTK_WIDGET (priv->view));
259
260
261
  layout_w = gtk_widget_get_allocated_width (GTK_WIDGET (layout));

  if (parent_w > layout_w)
262
    priv->max_width += parent_w - layout_w - (PADDING - OUTLINE_WIDTH);
263
264
265
  
  priv->max_height = height - (req.height - top - bottom) ;

266
  parent_h = gtk_widget_get_allocated_height (GTK_WIDGET (priv->view));
267
268
  layout_h = gtk_widget_get_allocated_height (GTK_WIDGET (layout));
  if (parent_h > layout_h)
269
    priv->max_height += parent_h - layout_h - (PADDING - OUTLINE_WIDTH);
270
271
}

272
static void
273
274
275
glade_design_layout_update_child (GladeDesignLayout *layout,
                                  GtkWidget         *child,
                                  GtkAllocation     *allocation)
276
{
277
  GladeWidget *gchild;
278

279
280
281
282
283
  /* Update GladeWidget metadata */
  gchild = glade_widget_get_from_gobject (child);
  g_object_set (gchild,
                "toplevel-width", allocation->width,
                "toplevel-height", allocation->height, NULL);
284

285
  if (layout->priv->selection)
286
    gdl_update_max_margins (layout, child, allocation->width, allocation->height);
287

288
  gtk_widget_queue_resize (GTK_WIDGET (layout));
289
290
}

291
292
293
294
295
static inline void
gdl_alignments_invalidate (GdkWindow *window,
                           GtkWidget *parent,
                           GtkWidget *selection,
                           Margins nodes)
296
{
297
298
299
  cairo_region_t *region = cairo_region_create ();
  cairo_rectangle_int_t rect = {0, 0, 16, 16};
  gint x1, x2, x3, y1, y2, y3;
300
  GtkAllocation alloc;
301
  gint x, y, w, h;
302

303
  gtk_widget_get_allocation (selection, &alloc);
304

305
306
  w = alloc.width;
  h = alloc.height;
307

308
  gtk_widget_translate_coordinates (selection, parent, 0, 0, &x, &y);
309

310
311
312
313
314
315
  x1 = x - gtk_widget_get_margin_left (selection);
  x2 = x + w/2;
  x3 = x + w + gtk_widget_get_margin_right (selection);
  y1 = y - gtk_widget_get_margin_top (selection);
  y2 = y + h/2;
  y3 = y + h + gtk_widget_get_margin_bottom (selection);
316

317
318
319
  /* Only invalidate node area */
  if (nodes & MARGIN_TOP)
    {
320
      rect.x = x2 - 5;
321
322
      rect.y = y1 - 10;
      cairo_region_union_rectangle (region, &rect);
323
    }
324
  if (nodes & MARGIN_BOTTOM)
325
    {
326
      rect.x = x2 - 8;
327
328
329
      rect.y = y3 - 13;
      cairo_region_union_rectangle (region, &rect);
    }
330

331
332
333
334
335
336
337
338
339
340
  rect.y = y2 - 10;
  if (nodes & MARGIN_LEFT)
    {
      rect.x = x1 - 8;
      cairo_region_union_rectangle (region, &rect);
    }
  if (nodes & MARGIN_RIGHT)
    {
      rect.x = x3 - 5;
      cairo_region_union_rectangle (region, &rect);
341
342
    }

343
344
345
  gdk_window_invalidate_region (window, region, FALSE);

  cairo_region_destroy (region);
346
}
347

348
static gboolean
349
glade_design_layout_motion_notify_event (GtkWidget *widget, GdkEventMotion *ev)
350
{
351
352
  GladeDesignLayoutPrivate *priv;
  GtkAllocation allocation;
353
354
  GtkWidget *child;
  gint x, y;
355

356
  if ((child = gtk_bin_get_child (GTK_BIN (widget))) == NULL)
357
358
359
360
    return FALSE;

  priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (widget);

361
362
  x = ev->x;
  y = ev->y;
363
364
365

  gtk_widget_get_allocation (child, &allocation);

366
367
  allocation.x += priv->child_offset;
  allocation.y += priv->child_offset;
368

369
  switch (priv->activity)
370
    {
371
      case ACTIVITY_RESIZE_WIDTH:
372
373
        allocation.width = MAX (0, x - priv->dx - PADDING - OUTLINE_WIDTH);
      break;
374
      case ACTIVITY_RESIZE_HEIGHT:
375
376
        allocation.height = MAX (0, y - priv->dy - PADDING - OUTLINE_WIDTH);
      break;
377
      case ACTIVITY_RESIZE_WIDTH_AND_HEIGHT:
378
379
380
        allocation.height = MAX (0, y - priv->dy - PADDING - OUTLINE_WIDTH);
        allocation.width = MAX (0, x - priv->dx - PADDING - OUTLINE_WIDTH);
      break;
381
      case ACTIVITY_MARGINS:
382
383
        {
          gboolean shift = ev->state & GDK_SHIFT_MASK;
384
          gboolean snap = ev->state & GDK_CONTROL_MASK;
385
386
          GtkWidget *selection = priv->selection;
          Margins margin = priv->margin;
387

388
389
          if (margin & MARGIN_TOP)
            {
390
391
392
393
              gint max_height = (shift) ? priv->max_height/2 : priv->max_height -
                gtk_widget_get_margin_bottom (selection);
              gint val = MAX (0, MIN (priv->m_dy - y, max_height));
              
394
              if (snap) val = (val/MARGIN_STEP)*MARGIN_STEP;
395
396
397
398
399
              gtk_widget_set_margin_top (selection, val);
              if (shift) gtk_widget_set_margin_bottom (selection, val);
            }
          else if (margin & MARGIN_BOTTOM)
            {
400
401
402
403
              gint max_height = (shift) ? priv->max_height/2 : priv->max_height -
                gtk_widget_get_margin_top (selection);
              gint val = MAX (0, MIN (y - priv->m_dy, max_height));
              
404
              if (snap) val = (val/MARGIN_STEP)*MARGIN_STEP;
405
406
407
              gtk_widget_set_margin_bottom (selection, val);
              if (shift) gtk_widget_set_margin_top (selection, val);
            }
408

409
410
          if (margin & MARGIN_LEFT)
            {
411
412
413
414
              gint max_width = (shift) ? priv->max_width/2 : priv->max_width -
                gtk_widget_get_margin_right (selection);
              gint val = MAX (0, MIN (priv->m_dx - x, max_width));
              
415
              if (snap) val = (val/MARGIN_STEP)*MARGIN_STEP;
416
417
418
419
420
              gtk_widget_set_margin_left (selection, val);
              if (shift) gtk_widget_set_margin_right (selection, val);
            }
          else if (margin & MARGIN_RIGHT)
            {
421
422
423
424
              gint max_width = (shift) ? priv->max_width/2 : priv->max_width -
                gtk_widget_get_margin_left (selection);
              gint val = MAX (0, MIN (x - priv->m_dx, max_width));
              
425
              if (snap) val = (val/MARGIN_STEP)*MARGIN_STEP;
426
427
428
429
430
              gtk_widget_set_margin_right (selection, val);
              if (shift) gtk_widget_set_margin_left (selection, val);
            }
        }
      break;
431
      default:
432
433
434
        {
          Activity activity = gdl_get_activity_from_pointer (GLADE_DESIGN_LAYOUT (widget), x, y);

435
436
          if (priv->node_over != priv->margin && (activity == ACTIVITY_ALIGNMENTS ||
              glade_project_get_pointer_mode (priv->project) == GLADE_POINTER_ALIGN_EDIT))
437
            {
438
439
440
441
442
              if (priv->selection)
                gdl_alignments_invalidate (priv->window, widget, priv->selection,
                                           priv->node_over | priv->margin);
              else
                gdk_window_invalidate_rect (priv->window, NULL, FALSE);
443

444
              priv->node_over = priv->margin;
445
            }
446
           
447
448
449
450
451
452
453
454
          if (activity == ACTIVITY_MARGINS)
            activity = gdl_margin_get_activity (priv->margin);

          /* Only set the cursor if changed */
          gdl_set_cursor (priv, priv->cursors[activity]);
          return TRUE;
        }
      break;
455
456
    }

457
  glade_design_layout_update_child (GLADE_DESIGN_LAYOUT (widget), child, &allocation);
458
  return FALSE;
459
460
}

461
462
463
464
465
typedef struct
{
  GtkWidget *toplevel;
  gint x;
  gint y;
466
  GtkWidget *placeholder;
467
468
469
470
  GladeWidget *gwidget;
} GladeFindInContainerData;

static void
471
472
glade_design_layout_find_inside_container (GtkWidget                *widget,
                                           GladeFindInContainerData *data)
473
{
474
  gint x, y, w, h;
475
476
477
478

  if (data->gwidget || !gtk_widget_get_mapped (widget))
    return;

479
480
  gtk_widget_translate_coordinates (data->toplevel, widget, data->x, data->y,
                                    &x, &y);
481
  
482
  /* Margins are not part of the widget allocation */
483
484
  w = gtk_widget_get_allocated_width (widget) + gtk_widget_get_margin_right (widget);
  h = gtk_widget_get_allocated_height (widget) + gtk_widget_get_margin_bottom (widget);
485

486
487
  if (x >= (0 - gtk_widget_get_margin_left (widget)) && x < w &&
      y >= (0 - gtk_widget_get_margin_top (widget)) && y < h)
488
    {
489
490
491
492
493
494
495
496
      if (GLADE_IS_PLACEHOLDER (widget))
        data->placeholder = widget;
      else
        {
          if (GTK_IS_CONTAINER (widget))
            gtk_container_forall (GTK_CONTAINER (widget), (GtkCallback)
                                  glade_design_layout_find_inside_container,
                                  data);
497

498
499
500
          if (!data->gwidget)
            data->gwidget = glade_widget_get_from_gobject (widget);
        }
501
502
503
    }
}

504
505
506
507
508
509
510
static gboolean
glade_project_is_toplevel_active (GladeProject *project, GtkWidget *toplevel)
{
  GList *l;

  for (l = glade_project_selection_get (project); l; l = g_list_next (l))
    {
511
512
      if (GTK_IS_WIDGET (l->data) && 
	  gtk_widget_is_ancestor (l->data, toplevel)) return TRUE;
513
514
515
516
517
    }

  return FALSE;
}

518
static void
519
520
521
gdl_edit_mode_set_selection (GladeDesignLayout *layout,
                             GladePointerMode mode,
                             GtkWidget *selection)
522
{
523
524
525
526
527
  GladeDesignLayoutPrivate *priv = layout->priv;

  if ((selection && GTK_IS_WIDGET (selection) == FALSE) ||
      gtk_bin_get_child (GTK_BIN (layout)) == selection) selection = NULL;
  
528
529
530
531
532
533
  if (priv->selection == selection) return;

  priv->selection = selection;

  if (selection)
    {
534
535
536
      if (mode == GLADE_POINTER_MARGIN_EDIT)
        {
          GtkWidget *child = gtk_bin_get_child (GTK_BIN (layout));
537

538
539
540
541
542
          /* Save initital margins to know which one where edited */
          priv->top = gtk_widget_get_margin_top (selection);
          priv->bottom = gtk_widget_get_margin_bottom (selection);
          priv->left = gtk_widget_get_margin_left (selection);
          priv->right = gtk_widget_get_margin_right (selection);
543

544
545
546
547
548
549
550
551
552
553
554
          gdl_update_max_margins (layout, child,
                                  gtk_widget_get_allocated_width (child),
                                  gtk_widget_get_allocated_height (child));
        }
      else if (mode == GLADE_POINTER_ALIGN_EDIT)
        {
          priv->valign = gtk_widget_get_valign (selection);
          priv->halign = gtk_widget_get_halign (selection);
        }

      gdk_window_invalidate_rect (priv->window, NULL, FALSE);
555
556
557
558
559
    }
  else
    {
      gdl_set_cursor (priv, NULL);
    }
560
561

  glade_project_set_pointer_mode (priv->project, mode);
562
563
}

564
static gboolean
565
glade_design_layout_button_press_event (GtkWidget *widget, GdkEventButton *ev)
566
{
567
  GladeDesignLayoutPrivate *priv;
568
569
  GtkAllocation child_allocation;
  Activity activity;
570
  GtkWidget *child;
571
  gint x, y;
572

573
  if (ev->button != 1 || ev->type != GDK_BUTTON_PRESS ||
574
      (child = gtk_bin_get_child (GTK_BIN (widget))) == NULL)
575
576
    return FALSE;

577
578
579
580
581
  priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (widget);

  x = ev->x;
  y = ev->y;

582
583
584
585
  priv->activity = activity = gdl_get_activity_from_pointer (GLADE_DESIGN_LAYOUT (widget), x, y);

  /* Check if we are in margin edit mode */
  if (priv->selection)
586
    {
587
      GtkWidget *selection = priv->selection;
588

589
      switch (activity)
590
        {
591
592
593
594
595
          case ACTIVITY_NONE:
            gdl_edit_mode_set_selection (GLADE_DESIGN_LAYOUT (widget), GLADE_POINTER_SELECT, NULL);
            return FALSE;
          break;
          case ACTIVITY_ALIGNMENTS:
596
            {
597
598
599
600
              gboolean top, bottom, left, right;
              Margins node = priv->margin;
              GtkAlign valign, halign;
              GladeWidget *gwidget;
601

602
603
              valign = gtk_widget_get_valign (selection);
              halign = gtk_widget_get_halign (selection);
604

605
606
607
608
609
610
611
              if (valign == GTK_ALIGN_FILL)
                top = bottom = TRUE;
              else
                {
                  top = (valign == GTK_ALIGN_START);
                  bottom = (valign == GTK_ALIGN_END);
                }
612

613
614
615
616
617
618
619
              if (halign == GTK_ALIGN_FILL)
                left = right = TRUE;
              else
                {
                  left = (halign == GTK_ALIGN_START);
                  right = (halign == GTK_ALIGN_END);
                }
620

621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
              if (node & MARGIN_TOP)
                valign = (top) ? ((bottom) ? GTK_ALIGN_END : GTK_ALIGN_CENTER) : 
                                 ((bottom) ? GTK_ALIGN_FILL : GTK_ALIGN_START);
              else if (node & MARGIN_BOTTOM)
                valign = (bottom) ? ((top) ? GTK_ALIGN_START : GTK_ALIGN_CENTER) :
                                    ((top) ? GTK_ALIGN_FILL : GTK_ALIGN_END);

              if (node & MARGIN_LEFT)
                halign = (left) ? ((right) ? GTK_ALIGN_END : GTK_ALIGN_CENTER) :
                                  ((right) ? GTK_ALIGN_FILL : GTK_ALIGN_START);
              else if (node & MARGIN_RIGHT)
                halign = (right) ? ((left) ? GTK_ALIGN_START : GTK_ALIGN_CENTER) :
                                   ((left) ? GTK_ALIGN_FILL : GTK_ALIGN_END);

              if ((gwidget = glade_widget_get_from_gobject (selection)))
                {
                  GladeProperty *property;

                  glade_command_push_group (_("Editing alignments of %s"),
                                            glade_widget_get_name (gwidget));

                  if (gtk_widget_get_valign (selection) != valign)
                    {
                      if ((property = glade_widget_get_property (gwidget, "valign")))
                        glade_command_set_property (property, valign);
                    }
                  if (gtk_widget_get_halign (selection) != halign)
                    {
                      if ((property = glade_widget_get_property (gwidget, "halign")))
                        glade_command_set_property (property, halign);
                    }
                  glade_command_pop_group ();
                }
            }
655
          break;
656
657
658
659
660
661
662
663
664
665
          case ACTIVITY_MARGINS:
            priv->m_dx = x + ((priv->margin & MARGIN_LEFT) ? 
                              gtk_widget_get_margin_left (selection) :
                                gtk_widget_get_margin_right (selection) * -1);
            priv->m_dy = y + ((priv->margin & MARGIN_TOP) ?
                              gtk_widget_get_margin_top (selection) :
                                gtk_widget_get_margin_bottom (selection) * -1);

            gdl_set_cursor (priv, priv->cursors[gdl_margin_get_activity (priv->margin)]);
            return FALSE;
666
667
          break;
          default:
668
            gdl_set_cursor (priv, priv->cursors[priv->activity]);
669
670
          break;
        }
671
    }
672

673
  gtk_widget_get_allocation (child, &child_allocation);
674

675
676
  priv->dx = x - (child_allocation.x + child_allocation.width + priv->child_offset);
  priv->dy = y - (child_allocation.y + child_allocation.height + priv->child_offset);
677

678
679
680
681
682
683
  if (activity != ACTIVITY_NONE &&
      !glade_project_is_toplevel_active (priv->project, child))
    {
      _glade_design_view_freeze (priv->view);
      glade_project_selection_set (priv->project, G_OBJECT (child), TRUE);
      _glade_design_view_thaw (priv->view);
684
    }
685
686

  return FALSE;
687
688
689
}

static gboolean
690
691
glade_design_layout_button_release_event (GtkWidget *widget,
                                          GdkEventButton *ev)
692
{
693
694
  GladeDesignLayoutPrivate *priv;
  GtkWidget *child;
695

696
697
  if ((child = gtk_bin_get_child (GTK_BIN (widget))) == NULL)
    return FALSE;
698

699
  priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (widget);
700

701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
  /* Check if margins where edited and execute corresponding glade command */
  if (priv->selection && priv->activity == ACTIVITY_MARGINS)
    {
      GladeWidget *gwidget = glade_widget_get_from_gobject (priv->selection);
      gint top, bottom, left, right;
      GladeProperty *property;

      top = gtk_widget_get_margin_top (priv->selection);
      bottom = gtk_widget_get_margin_bottom (priv->selection);
      left = gtk_widget_get_margin_left (priv->selection);
      right = gtk_widget_get_margin_right (priv->selection);

      glade_command_push_group (_("Editing margins of %s"),
                                glade_widget_get_name (gwidget));
      if (priv->top != top)
        {
          if ((property = glade_widget_get_property (gwidget, "margin-top")))
            glade_command_set_property (property, top);
        }
      if (priv->bottom != bottom)
        {
          if ((property = glade_widget_get_property (gwidget, "margin-bottom")))
            glade_command_set_property (property, bottom);
        }
      if (priv->left != left)
        {
          if ((property = glade_widget_get_property (gwidget, "margin-left")))
            glade_command_set_property (property, left);
        }
      if (priv->right != right)
        {
          if ((property = glade_widget_get_property (gwidget, "margin-right")))
            glade_command_set_property (property, right);
        }
735
736
737

      glade_command_pop_group ();
    }
738
739
740
741
742
  else if (priv->activity == ACTIVITY_ALIGNMENTS)
    {
      priv->node_over = 0;
      gdk_window_invalidate_rect (priv->window, NULL, FALSE);
    }
743
  
744
  priv->activity = ACTIVITY_NONE;
745
  gdl_set_cursor (priv, NULL);
746

747
  return FALSE;
748
749
750
}

static void
751
752
glade_design_layout_get_preferred_height (GtkWidget *widget,
                                          gint *minimum, gint *natural)
753
{
754
755
756
757
758
  GladeDesignLayoutPrivate *priv;
  GtkWidget *child;
  GladeWidget *gchild;
  gint child_height = 0;
  guint border_width = 0;
759

760
  priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (widget);
761

762
  *minimum = 0;
763

764
  child = gtk_bin_get_child (GTK_BIN (widget));
765

766
767
  if (child && gtk_widget_get_visible (child))
    {
768
      GtkRequisition req;
769
770
      gint height;

771
772
      gchild = glade_widget_get_from_gobject (child);
      g_assert (gchild);
773

774
      gtk_widget_get_preferred_size (child, &req, NULL);
775

776
      g_object_get (gchild, "toplevel-height", &child_height, NULL);
777

778
      child_height = MAX (child_height, req.height);
779

780
781
782
783
      if (priv->widget_name)
        pango_layout_get_pixel_size (priv->widget_name, NULL, &height);
      else
        height = PADDING;
784
785

      *minimum = MAX (*minimum, PADDING + 2.5*OUTLINE_WIDTH + height + child_height);
786
787
    }

788
  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
789
  *minimum += border_width * 2;
790
  *natural = *minimum;
791
}
792

793
static void
794
795
glade_design_layout_get_preferred_width (GtkWidget *widget,
                                         gint *minimum, gint *natural)
796
{
797
798
799
800
801
802
803
804
805
806
807
  GtkWidget *child;
  GladeWidget *gchild;
  gint child_width = 0;
  guint border_width = 0;

  *minimum = 0;

  child = gtk_bin_get_child (GTK_BIN (widget));

  if (child && gtk_widget_get_visible (child))
    {
808
809
      GtkRequisition req;
      
810
811
812
      gchild = glade_widget_get_from_gobject (child);
      g_assert (gchild);

813
      gtk_widget_get_preferred_size (child, &req, NULL);
814
815
816

      g_object_get (gchild, "toplevel-width", &child_width, NULL);

817
      child_width = MAX (child_width, req.width);
818

819
      *minimum = MAX (*minimum, 2*PADDING + 2*OUTLINE_WIDTH + child_width);
820
821
822
823
    }

  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
  *minimum += border_width * 2;
824
  *natural = *minimum;
825
826
}

827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
static void
glade_design_layout_get_preferred_width_for_height (GtkWidget       *widget,
                                                    gint             height,
                                                    gint            *minimum_width,
                                                    gint            *natural_width)
{
  glade_design_layout_get_preferred_width (widget, minimum_width, natural_width);
}

static void
glade_design_layout_get_preferred_height_for_width (GtkWidget       *widget,
                                                    gint             width,
                                                    gint            *minimum_height,
                                                    gint            *natural_height)
{
  glade_design_layout_get_preferred_height (widget, minimum_height, natural_height);
}

845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
static void
update_rectangles (GladeDesignLayoutPrivate *priv, GtkAllocation *alloc)
{
  GdkRectangle *rect = &priv->south_east;
  gint width, height;

  /* Update rectangles used to resize the children */
  priv->east.x = alloc->width + priv->child_offset;
  priv->east.y = priv->child_offset;
  priv->east.height = alloc->height;

  priv->south.x = priv->child_offset;
  priv->south.y = alloc->height + priv->child_offset;
  priv->south.width = alloc->width;
  
  /* Update south east rectangle width */
  pango_layout_get_pixel_size (priv->widget_name, &width, &height);
  priv->layout_width = width + (OUTLINE_WIDTH*2);
  width = MIN (alloc->width, width);

  rect->x = alloc->x + priv->child_offset + alloc->width - width - OUTLINE_WIDTH/2;
  rect->y = alloc->y + priv->child_offset + alloc->height + OUTLINE_WIDTH/2;
  rect->width = width + (OUTLINE_WIDTH*2);
  rect->height = height + OUTLINE_WIDTH;

  /* Update south rectangle width */
  priv->south.width = rect->x - priv->south.x;
}

874
static void
875
876
glade_design_layout_size_allocate (GtkWidget *widget,
                                   GtkAllocation *allocation)
877
{
878
  GtkWidget *child;
879

880
  gtk_widget_set_allocation (widget, allocation);
881
    
882
  if (gtk_widget_get_realized (widget))
883
  {
884
    gdk_window_move_resize (gtk_widget_get_window (widget),
885
886
887
888
                            allocation->x, allocation->y,
                            allocation->width, allocation->height);
  }

889
  child = gtk_bin_get_child (GTK_BIN (widget));
890

891
892
  if (child && gtk_widget_get_visible (child))
    {
893
894
895
      GladeDesignLayoutPrivate *priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (widget);
      GtkAllocation alloc;
      gint height, offset;
896

897
898
      offset = gtk_container_get_border_width (GTK_CONTAINER (widget)) + PADDING + OUTLINE_WIDTH;
      priv->child_offset = offset;
899

900
901
902
903
904
905
      if (priv->widget_name)
        pango_layout_get_pixel_size (priv->widget_name, NULL, &height);
      else
        height = PADDING;
      
      alloc.x = alloc.y = 0;
906
907
      priv->current_width = alloc.width = allocation->width - (offset * 2);
      priv->current_height = alloc.height = allocation->height - (offset + OUTLINE_WIDTH * 1.5 + height);
908
      
909
910
      if (gtk_widget_get_realized (widget))
        gdk_window_move_resize (priv->offscreen_window,
911
                                0, 0, alloc.width, alloc.height);
912

913
      gtk_widget_size_allocate (child, &alloc);
914
      update_rectangles (priv, &alloc);
915
    }
916
917
}

918
919
920
921
static void
on_glade_widget_name_notify (GObject *gobject, GParamSpec *pspec, GladeDesignLayout *layout) 
{
  GladeDesignLayoutPrivate *priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (layout);
922
  
923
924
925
926
  pango_layout_set_text (priv->widget_name, glade_widget_get_name (GLADE_WIDGET (gobject)), -1);
  gtk_widget_queue_resize (GTK_WIDGET (layout));
}

927
static void
928
glade_design_layout_add (GtkContainer *container, GtkWidget *widget)
929
{
930
931
  GladeDesignLayoutPrivate *priv = GLADE_DESIGN_LAYOUT_GET_PRIVATE (container);
  GladeDesignLayout *layout = GLADE_DESIGN_LAYOUT (container);
932
  GladeWidget *gchild;
933

934
935
  layout->priv->current_width = 0;
  layout->priv->current_height = 0;
936

937
  gtk_widget_set_parent_window (widget, priv->offscreen_window);
938

939
940
  GTK_CONTAINER_CLASS (glade_design_layout_parent_class)->add (container,
                                                               widget);
941

942
943
944
945
946
947
948
  if ((gchild = glade_widget_get_from_gobject (G_OBJECT (widget))))
    {
      on_glade_widget_name_notify (G_OBJECT (gchild), NULL, layout);
      g_signal_connect (gchild, "notify::name", G_CALLBACK (on_glade_widget_name_notify), layout);
    }
    
  gtk_widget_queue_draw (GTK_WIDGET (container)); 
949
950
951
}

static void
952
glade_design_layout_remove (GtkContainer *container, GtkWidget *widget)
953
{
954
955
956
957
958
959
  GladeWidget *gchild;

  if ((gchild = glade_widget_get_from_gobject (G_OBJECT (widget))))
    g_signal_handlers_disconnect_by_func (gchild, on_glade_widget_name_notify,
                                          GLADE_DESIGN_LAYOUT (container));

960
  GTK_CONTAINER_CLASS (glade_design_layout_parent_class)->remove (container, widget);
961
  gtk_widget_queue_draw (GTK_WIDGET (container));
962
963
}

964
965
966
967
968
969
970
static gboolean
glade_design_layout_damage (GtkWidget *widget, GdkEventExpose *event)
{
  gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, TRUE);
  return TRUE;
}

971
static inline void
972
973
draw_frame (cairo_t *cr, GladeDesignLayoutPrivate *priv, gboolean selected,
            int x, int y, int w, int h)
974
{
975
  cairo_save (cr);
976

977
  cairo_set_line_width (cr, OUTLINE_WIDTH);
978

979
980
981
  cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
  cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);

982
983
  gdk_cairo_set_source_rgba (cr, (selected) ? &priv->frame_color_active[0] :
                             &priv->frame_color[0]);
984
985

  /* rectangle */
986
  cairo_rectangle (cr, x, y, w, h);
987
  cairo_stroke (cr);
988

989
  if (priv->widget_name)
990
    {
991
      GdkRGBA *color = (selected) ? &priv->frame_color_active[1] : &priv->frame_color[1];
992
      GdkRectangle *rect = &priv->south_east;
993
      cairo_pattern_t *pattern;
994
      gdouble xx, yy;
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
      
      xx = rect->x + rect->width;
      yy = rect->y + rect->height;

      /* Draw tab */
      cairo_move_to (cr, rect->x, rect->y);
      cairo_line_to (cr, xx, rect->y);
      cairo_line_to (cr, xx, yy-8);
      cairo_curve_to