glade-design-view.c 25.7 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-widget.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
static void
340 341 342
on_chooser_adaptor_widget_selected (_GladeAdaptorChooserWidget *chooser,
                                    GladeWidgetAdaptor         *adaptor,
                                    GladeProject               *project)
343 344 345

{
  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
}

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);
  
365 366 367 368 369
  chooser = _glade_adaptor_chooser_widget_new (GLADE_ADAPTOR_CHOOSER_WIDGET_TOPLEVEL |
                                               GLADE_ADAPTOR_CHOOSER_WIDGET_WIDGET |
                                               GLADE_ADAPTOR_CHOOSER_WIDGET_SKIP_DEPRECATED,
                                               priv->project);
  _glade_adaptor_chooser_widget_populate (GLADE_ADAPTOR_CHOOSER_WIDGET (chooser));
370
  g_signal_connect (chooser, "adaptor-selected",
371
                    G_CALLBACK (on_chooser_adaptor_widget_selected),
372 373 374 375
                    priv->project);

  gtk_container_add (GTK_CONTAINER (pop), chooser);
  gtk_widget_show (chooser);
376
  gtk_popover_popup (GTK_POPOVER (pop));
377 378 379 380

  return TRUE;
}

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

388 389 390 391
  gtk_style_context_get_color (context, gtk_style_context_get_state (context),
                               &fg_color);

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

  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;
}


416
static void
417
glade_design_view_init (GladeDesignView *view)
418
{
419
  GtkWidget *viewport;
420

421
  view->priv = glade_design_view_get_instance_private (view);
422

423
  gtk_widget_set_no_show_all (GTK_WIDGET (view), TRUE);
424
  gtk_orientable_set_orientation (GTK_ORIENTABLE (view),
425
                                  GTK_ORIENTATION_VERTICAL);
426

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

  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);
438 439 440 441
  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);
442 443 444
  g_signal_connect (viewport, "draw",
                    G_CALLBACK (glade_design_view_viewport_draw),
                    view);
445
  gtk_viewport_set_shadow_type (GTK_VIEWPORT (viewport), GTK_SHADOW_NONE);
446
  gtk_container_add (GTK_CONTAINER (viewport), view->priv->layout_box);
447 448 449 450
  gtk_container_add (GTK_CONTAINER (view->priv->scrolled_window), viewport);

  gtk_widget_show (view->priv->scrolled_window);
  gtk_widget_show (viewport);
451
  gtk_widget_show_all (view->priv->layout_box);
452 453 454
  
  gtk_box_pack_start (GTK_BOX (view), view->priv->scrolled_window, TRUE, TRUE, 0);
  
455
  gtk_container_set_border_width (GTK_CONTAINER (view), 0);
456

457 458
  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
459 460

  _glade_dnd_dest_set (GTK_WIDGET (view));
461 462 463
}

static void
464
glade_design_view_finalize (GObject *object)
465
{
466
  GladeDesignView *view = GLADE_DESIGN_VIEW (object);
467

468
  glade_design_view_set_project (view, NULL);
469

470
  G_OBJECT_CLASS (parent_class)->finalize (object);
471
}
472

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
473
typedef struct
474
{
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
475 476 477
  GtkWidget *child;
  gint x, y;
} FindInContainerData;
478

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

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
485 486
  if (data->child || !gtk_widget_get_mapped (widget))
    return;
487

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
488 489 490
  x = data->x;
  y = data->y;
  gtk_widget_get_allocation (widget, &alloc);
491

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

499
static void
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
500 501 502
glade_design_view_drag_highlight (_GladeDrag *dest,
                                  gint       x,
                                  gint       y)
503
{
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
504 505 506 507 508 509 510 511 512 513 514 515 516
  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));
        }
    }
517

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
518
  _glade_drag_highlight (dest, x, y);
519 520
}

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

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

537 538 539 540
      if (target)
        gtk_drag_get_data (widget, context, target, time);
    }

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
541 542 543 544 545 546
  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);
547

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
548
  if (data.child)
549
    {
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
550 551 552 553 554 555 556 557
      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);
558

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
559
      if (drag_target)
560
        {
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
561 562 563 564 565 566 567 568 569 570 571 572 573
          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);
            }
574
        }
575
    }
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
576 577 578 579 580 581 582 583 584 585 586 587
  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);
    }
588
  
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
589 590 591 592 593 594 595 596 597 598 599 600
  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;
601 602 603 604
}

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

  if (priv->drag_target)
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
611
    glade_design_view_drag_highlight (priv->drag_target, -1, -1);
612
}
613

614
static void
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
615 616 617
on_source_drag_end (GtkWidget       *widget,
                    GdkDragContext  *context, 
                    GladeDesignView *view)
618 619 620
{
  GladeDesignViewPrivate *priv = view->priv;
  
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
621 622 623 624 625 626 627
  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);
628 629 630
}

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

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
643
  g_signal_handlers_disconnect_by_func (source, on_source_drag_end, view);
644

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
645 646
  g_clear_object (&priv->drag_data);
  priv->drag_data = _glade_dnd_get_data (context, selection, info);
647

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
648 649 650 651
  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);
652 653
}

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

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

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
668 669 670 671 672 673 674 675 676 677 678
      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);
679

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
680 681
  return TRUE;
}
682

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
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 708 709 710
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;
711
    }
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
712
  else if ((gsource = glade_widget_get_from_gobject (data)))
713
    {
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
714 715 716 717
      GList widgets = {gsource, NULL, NULL};
      glade_command_dnd (&widgets, NULL, NULL);
      return TRUE;
    }
718

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
719 720
  return FALSE;
}
721

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

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
728 729 730 731
  if (priv->drag_highlight == highlight)
    return;
      
  priv->drag_highlight = highlight;
732

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
733 734 735 736 737 738 739 740 741 742
  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;
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
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 */

776 777 778 779 780 781
/**
 * glade_design_view_get_project:
 * @view: A #GladeDesignView
 *
 * Returns: (transfer none): a #GladeProject
 */
782 783 784 785 786 787 788 789 790
GladeProject *
glade_design_view_get_project (GladeDesignView *view)
{
  g_return_val_if_fail (GLADE_IS_DESIGN_VIEW (view), NULL);

  return view->priv->project;

}

791 792 793 794 795 796
/**
 * glade_design_view_new:
 * @project: A #GladeProject
 *
 * Returns: (transfer full): a new #GladeDesignView
 */
797 798 799 800 801 802 803 804 805 806 807 808
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);
}

809 810 811 812 813 814
/**
 * glade_design_view_get_from_project:
 * @project: A #GladeProject
 *
 * Returns: (transfer none) (nullable): a #GladeDesignView
 */
815 816 817 818 819 820 821 822 823 824
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;
825
}