glade-design-view.c 25 KB
Newer Older
1 2 3
/*
 * glade-design-view.c
 *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
4
 * Copyright (C)      2006 Vincent Geddes
5
 *               2011-2016 Juan Pablo Ugarte
6 7 8
 *
 * Authors:
 *   Vincent Geddes <vincent.geddes@gmail.com>
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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
26 27 28 29 30 31

/**
 * SECTION:glade-design-view
 * @Title: GladeDesignView
 * @Short_Description: A widget to embed the workspace.
 *
32
 * Use this widget to embed toplevel widgets in a given #GladeProject.
33
 */
34

Christian Persch's avatar
Christian Persch committed
35 36
#include "config.h"

37
#include "glade.h"
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
38
#include "glade-dnd.h"
39 40 41
#include "glade-utils.h"
#include "glade-design-view.h"
#include "glade-design-layout.h"
42
#include "glade-design-private.h"
43
#include "glade-path.h"
44
#include "glade-adaptor-chooser.h"
45 46 47 48 49 50 51 52

#include <glib.h>
#include <glib/gi18n.h>

#define GLADE_DESIGN_VIEW_KEY "GLADE_DESIGN_VIEW_KEY"

enum
{
53
  PROP_0,
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
54
  PROP_PROJECT
55 56 57 58
};

struct _GladeDesignViewPrivate
{
59
  GladeProject *project;
60 61
  GtkWidget *scrolled_window;  /* Main scrolled window */
  GtkWidget *layout_box;       /* Box to pack a GladeDesignLayout for each toplevel in project */
62

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
63 64 65
  _GladeDrag *drag_target;
  GObject *drag_data;
  gboolean drag_highlight;
66 67 68 69
};

static GtkVBoxClass *parent_class = NULL;

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
70 71 72
static void glade_design_view_drag_init (_GladeDragInterface *iface);

G_DEFINE_TYPE_WITH_CODE (GladeDesignView, glade_design_view, GTK_TYPE_BOX,
73
                         G_ADD_PRIVATE (GladeDesignView)
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
74 75
                         G_IMPLEMENT_INTERFACE (GLADE_TYPE_DRAG,
                                                glade_design_view_drag_init))
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
static void
glade_design_layout_scroll (GladeDesignView *view, gint x, gint y, gint w, gint h)
{
  gdouble vadj_val, hadj_val, vpage_end, hpage_end;
  GtkAdjustment *vadj, *hadj;

  vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (view->priv->scrolled_window));
  hadj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (view->priv->scrolled_window));

  vadj_val = gtk_adjustment_get_value (vadj);
  hadj_val = gtk_adjustment_get_value (hadj);
  vpage_end = gtk_adjustment_get_page_size (vadj) + vadj_val;
  hpage_end = gtk_adjustment_get_page_size (hadj) + hadj_val;

  /* TODO: we could set this value in increments in a timeout callback 
   * to make it look like its scrolling instead of jumping.
   */
  if (y < vadj_val || y > vpage_end || (y + h) > vpage_end)
    gtk_adjustment_set_value (vadj, y);

  if (x < hadj_val || x > hpage_end || (x + w) > hpage_end)
    gtk_adjustment_set_value (hadj, x);
}

static void 
on_layout_size_allocate (GtkWidget *widget, GtkAllocation *alloc, GladeDesignView *view)
{
  glade_design_layout_scroll (view, alloc->x, alloc->y, alloc->width, alloc->height);
  g_signal_handlers_disconnect_by_func (widget, on_layout_size_allocate, view);
}
107

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
static void
glade_design_view_update_state (GList *objects, GtkStateFlags state)
{
  GList *l;

  for (l = objects; l && l->data; l = g_list_next (l))
    {
      GtkWidget *view, *widget = l->data;

      if (GTK_IS_WIDGET (widget) &&
          gtk_widget_get_visible (widget) &&
          (view = gtk_widget_get_ancestor (widget, GLADE_TYPE_DESIGN_LAYOUT)))
        {
          gtk_widget_set_state_flags (view, state, TRUE);
        }
    }
}

126
static void
127
glade_design_view_selection_changed (GladeProject *project, GladeDesignView *view)
128
{
129
  GtkWidget *layout;
130
  GList *selection;
131

132 133 134 135 136 137 138 139
  glade_design_view_update_state (glade_project_toplevels (project),
                                  GTK_STATE_FLAG_NORMAL);

  if (!(selection = glade_project_selection_get (project)))
    return;

  glade_design_view_update_state (selection, GTK_STATE_FLAG_SELECTED);

140
  /* Check if its only one widget selected and scroll viewport to show toplevel */
141 142 143
  if (g_list_next (selection) == NULL &&
      GTK_IS_WIDGET (selection->data) &&
      (layout = gtk_widget_get_ancestor (selection->data, GLADE_TYPE_DESIGN_LAYOUT)))
144
    {
145 146
      GtkAllocation alloc;
      gtk_widget_get_allocation (layout, &alloc);
147

148 149 150 151
      if (alloc.x < 0)
        g_signal_connect (layout, "size-allocate", G_CALLBACK (on_layout_size_allocate), view);
      else
        glade_design_layout_scroll (view, alloc.x, alloc.y, alloc.width, alloc.height);
152 153
    }
}
154

155 156 157 158
static void
glade_design_view_add_toplevel (GladeDesignView *view, GladeWidget *widget)
{
  GtkWidget *layout;
159
  GList *toplevels;
160
  GObject *object;
161

162 163 164 165 166
  if (glade_widget_get_parent (widget) ||
      (object = glade_widget_get_object (widget)) == NULL ||
      !GTK_IS_WIDGET (object) ||
      gtk_widget_get_parent (GTK_WIDGET (object)))
    return;
167

168 169
  /* Create a GladeDesignLayout and add the toplevel widget to the view */
  layout = _glade_design_layout_new (view);
170 171
  gtk_widget_set_halign (layout, GTK_ALIGN_START);
  gtk_box_pack_start (GTK_BOX (view->priv->layout_box), layout, FALSE, FALSE, 0);
172

173 174 175
  if ((toplevels = glade_project_toplevels (view->priv->project)))
    gtk_box_reorder_child (GTK_BOX (view->priv->layout_box), layout, 
                           g_list_index (toplevels, GTK_WIDGET (object)));
176

177
  gtk_container_add (GTK_CONTAINER (layout), GTK_WIDGET (object));
178

179 180
  gtk_widget_show (GTK_WIDGET (object));
  gtk_widget_show (layout);
181 182
}

183 184 185 186 187 188 189 190 191 192 193
static void
glade_design_view_remove_toplevel (GladeDesignView *view, GladeWidget *widget)
{
  GtkWidget *layout;
  GObject *object;

  if (glade_widget_get_parent (widget) ||
      (object = glade_widget_get_object (widget)) == NULL ||
      !GTK_IS_WIDGET (object)) return;
  
  /* Remove toplevel widget from the view */
194 195
  if ((layout = gtk_widget_get_parent (GTK_WIDGET (object))) &&
      gtk_widget_is_ancestor (layout, GTK_WIDGET (view)))
196 197 198 199 200 201
    {
      gtk_container_remove (GTK_CONTAINER (layout), GTK_WIDGET (object));
      gtk_container_remove (GTK_CONTAINER (view->priv->layout_box), layout);
    }
}

202
static void
203 204 205 206
glade_design_view_widget_visibility_changed (GladeProject    *project,
                                             GladeWidget     *widget,
                                             gboolean         visible,
                                             GladeDesignView *view)
207
{
208 209 210 211
  if (visible)
    glade_design_view_add_toplevel (view, widget);
  else
    glade_design_view_remove_toplevel (view, widget);
212 213 214 215
}

static void
on_project_add_widget (GladeProject *project, GladeWidget *widget, GladeDesignView *view)
216
{
217
  glade_design_view_add_toplevel (view, widget);
218 219 220 221 222 223
}

static void
on_project_remove_widget (GladeProject *project, GladeWidget *widget, GladeDesignView *view)
{
  glade_design_view_remove_toplevel (view, widget);
224 225
}

226
static void
227
glade_design_view_set_project (GladeDesignView *view, GladeProject *project)
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
  GladeDesignViewPrivate *priv;

  priv = view->priv;

  if (priv->project)
    {
      g_signal_handlers_disconnect_by_func (priv->project,
                                            on_project_add_widget,
                                            view);
      g_signal_handlers_disconnect_by_func (priv->project,
                                            on_project_remove_widget,
                                            view);
      g_signal_handlers_disconnect_by_func (priv->project,
                                            gtk_widget_hide,
                                            priv->scrolled_window);
      g_signal_handlers_disconnect_by_func (priv->project,
                                            gtk_widget_show,
                                            priv->scrolled_window);
      g_signal_handlers_disconnect_by_func (priv->project,
                                            glade_design_view_selection_changed,
                                            view);
      g_signal_handlers_disconnect_by_func (priv->project,
                                            glade_design_view_widget_visibility_changed,
                                            view);

      g_object_set_data (G_OBJECT (priv->project), GLADE_DESIGN_VIEW_KEY, NULL);
255
      g_clear_object (&priv->project);
256 257
    }

258 259 260 261
  if (!project)
    return;

  priv->project = g_object_ref (project);
262

263 264 265 266
  g_signal_connect (project, "add-widget",
                    G_CALLBACK (on_project_add_widget), view);
  g_signal_connect (project, "remove-widget",
                    G_CALLBACK (on_project_remove_widget), view);
267 268
  g_signal_connect_swapped (project, "parse-began",
                            G_CALLBACK (gtk_widget_hide),
269
                            priv->scrolled_window);
270 271
  g_signal_connect_swapped (project, "parse-finished",
                            G_CALLBACK (gtk_widget_show),
272
                            priv->scrolled_window);
273 274
  g_signal_connect (project, "selection-changed",
                    G_CALLBACK (glade_design_view_selection_changed), view);
275 276
  g_signal_connect (project, "widget-visibility-changed",
                    G_CALLBACK (glade_design_view_widget_visibility_changed), view);
277

278
  g_object_set_data (G_OBJECT (project), GLADE_DESIGN_VIEW_KEY, view);
279 280
}

281
static void
282
glade_design_view_set_property (GObject *object,
283
                                guint prop_id,
284
                                const GValue *value, GParamSpec *pspec)
285
{
286 287 288 289 290 291 292 293 294
  switch (prop_id)
    {
      case PROP_PROJECT:
        glade_design_view_set_project (GLADE_DESIGN_VIEW (object),
                                       g_value_get_object (value));
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
295 296 297 298
    }
}

static void
299
glade_design_view_get_property (GObject *object,
300
                                guint prop_id,
301 302
                                GValue *value,
                                GParamSpec *pspec)
303
{
304 305 306 307 308 309 310 311 312
  switch (prop_id)
    {
      case PROP_PROJECT:
        g_value_set_object (value, GLADE_DESIGN_VIEW (object)->priv->project);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
313 314
}

315
static void
316
logo_draw (GtkWidget *widget, cairo_t *cr, GdkRGBA *c)
317 318 319 320 321 322 323 324
{
  GtkAllocation alloc;
  gdouble scale;

  gtk_widget_get_allocation (widget, &alloc);

  cairo_save (cr);
  
325
  cairo_set_source_rgba (cr, c->red, c->green, c->blue, .06);
326 327 328 329 330 331 332 333 334 335 336 337 338

  scale = MIN ((alloc.width/1.5)/(glade_path_WIDTH), (alloc.height/1.5)/(glade_path_HEIGHT));

  cairo_scale (cr, scale, scale);
  
  cairo_translate (cr, (alloc.width / scale) - glade_path_WIDTH,
                   (alloc.height / scale) - glade_path_HEIGHT);
  cairo_append_path (cr, &glade_path);
  cairo_fill (cr);

  cairo_restore (cr);
}

339 340 341 342 343 344 345
static void
on_chooser_adaptor_selected (_GladeAdaptorChooser *chooser,
                             GladeWidgetAdaptor   *adaptor,
                             GladeProject         *project)

{
  glade_command_create (adaptor, NULL, NULL, project);
346
  gtk_widget_destroy (gtk_widget_get_ancestor (GTK_WIDGET (chooser), GTK_TYPE_POPOVER));
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
}

static gboolean
glade_design_view_viewport_button_press (GtkWidget       *widget,
                                         GdkEventButton  *event,
                                         GladeDesignView *view)
{
  GladeDesignViewPrivate *priv = view->priv;
  GdkRectangle rect = {event->x, event->y, 8, 8};
  GtkWidget *pop, *chooser;
  
  if (event->type != GDK_2BUTTON_PRESS)
    return FALSE;

  pop = gtk_popover_new (widget);
  gtk_popover_set_pointing_to (GTK_POPOVER (pop), &rect);
  gtk_popover_set_position (GTK_POPOVER (pop), GTK_POS_BOTTOM);
  
  chooser = _glade_adaptor_chooser_new (GLADE_ADAPTOR_CHOOSER_TOPLEVEL,
                                        priv->project);
  g_signal_connect (chooser, "adaptor-selected",
                    G_CALLBACK (on_chooser_adaptor_selected),
                    priv->project);

  gtk_container_add (GTK_CONTAINER (pop), chooser);
  gtk_widget_show (chooser);
  gtk_widget_show (pop);

  return TRUE;
}

378 379 380 381
static gboolean
glade_design_view_viewport_draw (GtkWidget *widget, cairo_t *cr, GladeDesignView *view)
{
  GladeDesignViewPrivate *priv = GLADE_DESIGN_VIEW (view)->priv;
382 383
  GtkStyleContext *context = gtk_widget_get_style_context (widget);
  GdkRGBA fg_color;
384

385 386 387 388
  gtk_style_context_get_color (context, gtk_style_context_get_state (context),
                               &fg_color);

  logo_draw (widget, cr, &fg_color);
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412

  if (priv->drag_highlight)
    {
      GdkRGBA c;

      gtk_style_context_save (context);
      gtk_style_context_get_background_color (context,
                                              gtk_style_context_get_state (context) |
                                              GTK_STATE_FLAG_SELECTED |
                                              GTK_STATE_FLAG_FOCUSED, &c);
      gtk_style_context_restore (context);

      cairo_set_line_width (cr, 2);
      gdk_cairo_set_source_rgba (cr, &c);
      cairo_rectangle (cr, 0, 0,
                       gtk_widget_get_allocated_width (widget),
                       gtk_widget_get_allocated_height (widget));
      cairo_stroke (cr);
    }

  return FALSE;
}


413
static void
414
glade_design_view_init (GladeDesignView *view)
415
{
416
  GtkWidget *viewport;
417

418
  view->priv = glade_design_view_get_instance_private (view);
419

420
  gtk_widget_set_no_show_all (GTK_WIDGET (view), TRUE);
421 422
  gtk_orientable_set_orientation (GTK_ORIENTABLE (view),
				  GTK_ORIENTATION_VERTICAL);
423

424
  view->priv->project = NULL;
425
  view->priv->layout_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
426
  gtk_widget_set_valign (view->priv->layout_box, GTK_ALIGN_START);
427
  gtk_container_set_border_width (GTK_CONTAINER (view->priv->layout_box), 0);
428 429 430 431 432 433 434

  view->priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW
                                  (view->priv->scrolled_window),
                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  viewport = gtk_viewport_new (NULL, NULL);
435 436 437 438
  gtk_widget_add_events (viewport, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
  g_signal_connect (viewport, "button-press-event",
                    G_CALLBACK (glade_design_view_viewport_button_press),
                    view);
439 440 441
  g_signal_connect (viewport, "draw",
                    G_CALLBACK (glade_design_view_viewport_draw),
                    view);
442
  gtk_viewport_set_shadow_type (GTK_VIEWPORT (viewport), GTK_SHADOW_NONE);
443
  gtk_container_add (GTK_CONTAINER (viewport), view->priv->layout_box);
444 445 446 447
  gtk_container_add (GTK_CONTAINER (view->priv->scrolled_window), viewport);

  gtk_widget_show (view->priv->scrolled_window);
  gtk_widget_show (viewport);
448
  gtk_widget_show_all (view->priv->layout_box);
449 450 451
  
  gtk_box_pack_start (GTK_BOX (view), view->priv->scrolled_window, TRUE, TRUE, 0);
  
452
  gtk_container_set_border_width (GTK_CONTAINER (view), 0);
453

454 455
  gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (view)),
                               GTK_STYLE_CLASS_VIEW);
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
456 457

  _glade_dnd_dest_set (GTK_WIDGET (view));
458 459 460
}

static void
461
glade_design_view_finalize (GObject *object)
462
{
463
  GladeDesignView *view = GLADE_DESIGN_VIEW (object);
464

465
  glade_design_view_set_project (view, NULL);
466

467
  G_OBJECT_CLASS (parent_class)->finalize (object);
468
}
469

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
470
typedef struct
471
{
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
472 473 474
  GtkWidget *child;
  gint x, y;
} FindInContainerData;
475

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
476 477
static void
find_inside_container (GtkWidget *widget, FindInContainerData *data)
478
{
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
479 480
  GtkAllocation alloc;
  gint x, y;
481

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
482 483
  if (data->child || !gtk_widget_get_mapped (widget))
    return;
484

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
485 486 487
  x = data->x;
  y = data->y;
  gtk_widget_get_allocation (widget, &alloc);
488

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
489 490
  if (x >= alloc.x && x <= (alloc.x + alloc.width) &&
      y >= alloc.y && y <= (alloc.y + alloc.height))
491
    {
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
492
      data->child = widget;
493 494 495
    }
}

496
static void
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
497 498 499
glade_design_view_drag_highlight (_GladeDrag *dest,
                                  gint       x,
                                  gint       y)
500
{
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
501 502 503 504 505 506 507 508 509 510 511 512 513
  if (GLADE_IS_WIDGET (dest))
    {
      GObject *obj = glade_widget_get_object (GLADE_WIDGET (dest));
      
      if (GTK_IS_WIDGET (obj))
        {
          GtkWidget *layout = gtk_widget_get_ancestor (GTK_WIDGET (obj),
                                                       GLADE_TYPE_DESIGN_LAYOUT);
          if (layout)
            _glade_design_layout_set_highlight (GLADE_DESIGN_LAYOUT (layout),
                                                (x<0 || y<0) ? NULL : GLADE_WIDGET (dest));
        }
    }
514

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
515
  _glade_drag_highlight (dest, x, y);
516 517
}

518
static gboolean
519 520 521 522
glade_design_view_drag_motion (GtkWidget *widget,
                               GdkDragContext *context,
                               gint x, gint y,
                               guint time)
523
{
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
524 525 526 527 528
  GladeDesignView *view = GLADE_DESIGN_VIEW (widget);
  GladeDesignViewPrivate *priv = view->priv;
  FindInContainerData data;
  _GladeDrag *drag = NULL;
  gint xx, yy;
529

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
530
  if (!priv->drag_data)
531 532 533
    {
      GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);

534 535 536 537
      if (target)
        gtk_drag_get_data (widget, context, target, time);
    }

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
538 539 540 541 542 543
  data.child = NULL;
  gtk_widget_translate_coordinates (widget, GTK_WIDGET (priv->layout_box),
                                    x, y, &data.x, &data.y);
  gtk_container_forall (GTK_CONTAINER (priv->layout_box),
                        (GtkCallback) find_inside_container,
                        &data);
544

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
545
  if (data.child)
546
    {
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
547 548 549 550 551 552 553 554
      GladeDesignLayout *layout = GLADE_DESIGN_LAYOUT (data.child);
      GladeWidget *gchild = _glade_design_layout_get_child (layout);
      GtkWidget *child = GTK_WIDGET (glade_widget_get_object (gchild));
      GtkWidget *drag_target;

      gtk_widget_translate_coordinates (widget, child, x, y, &xx, &yy);
      
      drag_target = _glade_design_layout_get_child_at_position (child, xx, yy);
555

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
556
      if (drag_target)
557
        {
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
558 559 560 561 562 563 564 565 566 567 568 569 570
          GladeWidget *gwidget;

          if (GLADE_IS_PLACEHOLDER (drag_target))
            drag = GLADE_DRAG (drag_target);
          else if ((gwidget = glade_widget_get_from_gobject (drag_target)))
            {
              while (gwidget && !_glade_drag_can_drop (GLADE_DRAG (gwidget),
                                                       xx, yy, priv->drag_data))
                gwidget = glade_widget_get_parent (gwidget);

              if (gwidget)
                drag = GLADE_DRAG (gwidget);
            }
571
        }
572
    }
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
573 574 575 576 577 578 579 580 581 582 583 584
  else if (_glade_drag_can_drop (GLADE_DRAG (widget), x, y, priv->drag_data))
    {
      drag = GLADE_DRAG (widget);
      xx = x;
      yy = y;
    }

  if (priv->drag_target && priv->drag_target != drag)
    {
      glade_design_view_drag_highlight (priv->drag_target, -1, -1);
      g_clear_object (&priv->drag_target);
    }
585
  
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
586 587 588 589 590 591 592 593 594 595 596 597
  if (drag)
    {
      priv->drag_target = g_object_ref (drag);

      glade_design_view_drag_highlight (drag, xx, yy);
      
      gdk_drag_status (context, GDK_ACTION_COPY, time);
      return TRUE;
    }

  gdk_drag_status (context, 0, time);
  return FALSE;
598 599 600 601
}

static void
glade_design_view_drag_leave (GtkWidget      *widget,
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
602
                              GdkDragContext *context,
603 604 605 606 607
                              guint           time)
{
  GladeDesignViewPrivate *priv = GLADE_DESIGN_VIEW (widget)->priv;

  if (priv->drag_target)
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
608
    glade_design_view_drag_highlight (priv->drag_target, -1, -1);
609
}
610

611
static void
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
612 613 614
on_source_drag_end (GtkWidget       *widget,
                    GdkDragContext  *context, 
                    GladeDesignView *view)
615 616 617
{
  GladeDesignViewPrivate *priv = view->priv;
  
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
618 619 620 621 622 623 624
  if (priv->drag_target)
    {
      glade_design_view_drag_highlight (priv->drag_target, -1, -1);
      g_clear_object (&priv->drag_target);
    }

  g_clear_object (&priv->drag_data);
625 626 627
}

static void
628 629 630 631 632 633 634
glade_design_view_drag_data_received (GtkWidget        *widget,
                                      GdkDragContext   *context,
                                      gint              x,
                                      gint              y,
                                      GtkSelectionData *selection,
                                      guint             info,
                                      guint             time)
635
{
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
636 637 638
  GtkWidget *source = gtk_drag_get_source_widget (context);
  GladeDesignView *view = GLADE_DESIGN_VIEW (widget);
  GladeDesignViewPrivate *priv = view->priv;
639

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
640
  g_signal_handlers_disconnect_by_func (source, on_source_drag_end, view);
641

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
642 643
  g_clear_object (&priv->drag_data);
  priv->drag_data = _glade_dnd_get_data (context, selection, info);
644

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
645 646 647 648
  if (priv->drag_data)
    g_object_ref (priv->drag_data);

  g_signal_connect_object (source, "drag-end", G_CALLBACK (on_source_drag_end), view, 0);
649 650
}

651
static gboolean
652 653 654 655 656
glade_design_view_drag_drop (GtkWidget       *widget,
                             GdkDragContext  *context,
                             gint             x,
                             gint             y,
                             guint            time)  
657 658 659
{
  GladeDesignViewPrivate *priv = GLADE_DESIGN_VIEW (widget)->priv;

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
660
  if (priv->drag_data && priv->drag_target)
661
    {
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
662 663
      GtkWidget *target;
      gint xx, yy;
664

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
665 666 667 668 669 670 671 672 673 674 675
      if (GLADE_IS_WIDGET (priv->drag_target))
        target = GTK_WIDGET (glade_widget_get_object (GLADE_WIDGET (priv->drag_target)));
      else
        target = GTK_WIDGET (priv->drag_target);
      
      gtk_widget_translate_coordinates (widget, target, x, y, &xx, &yy);
      _glade_drag_drop (GLADE_DRAG (priv->drag_target), xx, yy, priv->drag_data);
      gtk_drag_finish (context, TRUE, FALSE, time);
    }
  else
    gtk_drag_finish (context, FALSE, FALSE, time);
676

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
677 678
  return TRUE;
}
679

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
static gboolean
glade_design_view_drag_iface_can_drop (_GladeDrag *drag,
                                       gint x, gint y,
                                       GObject *data)
{
  GladeWidget *gwidget;

  if (GLADE_IS_WIDGET_ADAPTOR (data) ||
      ((gwidget = glade_widget_get_from_gobject (data)) &&
       glade_widget_get_parent (gwidget)))
    return TRUE;
  else
    return FALSE;
}

static gboolean
glade_design_view_drag_iface_drop (_GladeDrag *drag,
                                   gint x, gint y,
                                   GObject *data)
{
  GladeDesignViewPrivate *priv = GLADE_DESIGN_VIEW (drag)->priv;
  GladeWidget *gsource;

  if (GLADE_IS_WIDGET_ADAPTOR (data))
    {
      glade_command_create (GLADE_WIDGET_ADAPTOR (data),
                            NULL, NULL, priv->project);
      return TRUE;
708
    }
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
709
  else if ((gsource = glade_widget_get_from_gobject (data)))
710
    {
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
711 712 713 714
      GList widgets = {gsource, NULL, NULL};
      glade_command_dnd (&widgets, NULL, NULL);
      return TRUE;
    }
715

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
716 717
  return FALSE;
}
718

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
719 720 721 722
static void
glade_design_view_drag_iface_highlight (_GladeDrag *drag, gint x, gint y)
{
  GladeDesignViewPrivate *priv = GLADE_DESIGN_VIEW (drag)->priv;
723
  gboolean highlight = !(x < 0 || y < 0);
724

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
725 726 727 728
  if (priv->drag_highlight == highlight)
    return;
      
  priv->drag_highlight = highlight;
729

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
730 731 732 733 734 735 736 737 738 739
  gtk_widget_queue_draw (priv->scrolled_window);
}

static void
glade_design_view_drag_init (_GladeDragInterface *iface)
{
  iface->can_drag = NULL;
  iface->can_drop = glade_design_view_drag_iface_can_drop;
  iface->drop = glade_design_view_drag_iface_drop;
  iface->highlight = glade_design_view_drag_iface_highlight;
740 741
}

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
static void
glade_design_view_class_init (GladeDesignViewClass *klass)
{
  GObjectClass *object_class;
  GtkWidgetClass *widget_class;

  parent_class = g_type_class_peek_parent (klass);
  object_class = G_OBJECT_CLASS (klass);
  widget_class = GTK_WIDGET_CLASS (klass);

  object_class->finalize = glade_design_view_finalize;
  object_class->get_property = glade_design_view_get_property;
  object_class->set_property = glade_design_view_set_property;

  widget_class->drag_motion = glade_design_view_drag_motion;
  widget_class->drag_leave = glade_design_view_drag_leave;
  widget_class->drag_data_received = glade_design_view_drag_data_received;
  widget_class->drag_drop = glade_design_view_drag_drop;
  
  g_object_class_install_property (object_class,
                                   PROP_PROJECT,
                                   g_param_spec_object ("project",
                                                        "Project",
                                                        "The project for this view",
                                                        GLADE_TYPE_PROJECT,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));
}

/* Public API */

GladeProject *
glade_design_view_get_project (GladeDesignView *view)
{
  g_return_val_if_fail (GLADE_IS_DESIGN_VIEW (view), NULL);

  return view->priv->project;

}

GtkWidget *
glade_design_view_new (GladeProject *project)
{
  GladeDesignView *view;

  g_return_val_if_fail (GLADE_IS_PROJECT (project), NULL);

  view = g_object_new (GLADE_TYPE_DESIGN_VIEW, "project", project, NULL);

  return GTK_WIDGET (view);
}

GladeDesignView *
glade_design_view_get_from_project (GladeProject *project)
{
  gpointer p;

  g_return_val_if_fail (GLADE_IS_PROJECT (project), NULL);

  p = g_object_get_data (G_OBJECT (project), GLADE_DESIGN_VIEW_KEY);

  return (p != NULL) ? GLADE_DESIGN_VIEW (p) : NULL;
804
}