glade-placeholder.c 20.3 KB
Newer Older
Jose Maria Celorio's avatar
Jose Maria Celorio committed
1
/*
2
 * Copyright (C) 2003, 2004 Joaquin Cuenca Abela
Jose Maria Celorio's avatar
Jose Maria Celorio committed
3
 *
4 5 6
 * Authors:
 *   Joaquin Cuenca Abela <e98cuenc@yahoo.com> 
 * 
Jose Maria Celorio's avatar
Jose Maria Celorio committed
7 8 9 10 11 12 13 14 15 16 17 18
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Jose Maria Celorio's avatar
Jose Maria Celorio committed
20
 *
21
*/
Jose Maria Celorio's avatar
Jose Maria Celorio committed
22

Christian Persch's avatar
Christian Persch committed
23 24
#include "config.h"

25 26 27 28 29 30 31 32 33 34 35
/**
 * SECTION:glade-placeholder
 * @Short_Description: A #GtkWidget to fill empty places.
 *
 * Generally in Glade, container widgets are implemented with #GladePlaceholder 
 * children to allow users to 'click' and add thier widgets to a container. 
 * It is the responsability of the plugin writer to create placeholders for 
 * container widgets where appropriate; usually in #GladePostCreateFunc 
 * when the #GladeCreateReason is %GLADE_CREATE_USER.
 */

36
#include <gtk/gtk.h>
37
#include "glade-marshallers.h"
38
#include "glade.h"
Jose Maria Celorio's avatar
Jose Maria Celorio committed
39
#include "glade-placeholder.h"
40 41
#include "glade-xml-utils.h"
#include "glade-project.h"
42
#include "glade-command.h"
43
#include "glade-palette.h"
44
#include "glade-popup.h"
Paolo Borelli's avatar
Paolo Borelli committed
45
#include "glade-cursor.h"
46
#include "glade-widget.h"
47
#include "glade-app.h"
48
#include "glade-adaptor-chooser-widget.h"
49
#include <math.h>
50

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
51 52 53
#include "glade-dnd.h"
#include "glade-drag.h"

54 55 56
#define WIDTH_REQUISITION    20
#define HEIGHT_REQUISITION   20

57 58
static cairo_pattern_t *placeholder_pattern = NULL;

59 60 61 62 63
struct _GladePlaceholderPrivate
{
  GList *packing_actions;

  GdkWindow *event_window;
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
64 65

  gboolean drag_highlight;
66 67
};

68 69 70 71 72 73 74
enum
{
  PROP_0,
  PROP_HADJUSTMENT,
  PROP_VADJUSTMENT,
  PROP_HSCROLL_POLICY,
  PROP_VSCROLL_POLICY
75 76
};

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
77
#define GLADE_PLACEHOLDER_PRIVATE(object) (((GladePlaceholder*)object)->priv)
78

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
79
static void glade_placeholder_drag_init (_GladeDragInterface *iface);
80

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
81
G_DEFINE_TYPE_WITH_CODE (GladePlaceholder, glade_placeholder, GTK_TYPE_WIDGET,
82
                         G_ADD_PRIVATE (GladePlaceholder)
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
83 84 85
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL)
                         G_IMPLEMENT_INTERFACE (GLADE_TYPE_DRAG, 
                                                glade_placeholder_drag_init))
Jose Maria Celorio's avatar
Jose Maria Celorio committed
86

87
static void
88
glade_placeholder_notify_parent (GObject    *gobject,
89
                                 GParamSpec *arg1,
90
                                 gpointer    user_data)
91
{
92
  GladePlaceholder *placeholder = GLADE_PLACEHOLDER (gobject);
93
  GladeWidgetAdaptor *parent_adaptor = NULL;
94 95
  GladeWidget *parent = glade_placeholder_get_parent (placeholder);

96
  if (placeholder->priv->packing_actions)
97
    {
98
      g_list_foreach (placeholder->priv->packing_actions, (GFunc) g_object_unref,
99
                      NULL);
100 101
      g_list_free (placeholder->priv->packing_actions);
      placeholder->priv->packing_actions = NULL;
102 103
    }

104 105 106
  if (parent)
    parent_adaptor = glade_widget_get_adaptor (parent);

107
  if (parent_adaptor)
108
    placeholder->priv->packing_actions =
109
      glade_widget_adaptor_pack_actions_new (parent_adaptor);
110 111
}

112
static void
113
glade_placeholder_init (GladePlaceholder *placeholder)
Jose Maria Celorio's avatar
Jose Maria Celorio committed
114
{
115
  placeholder->priv =  glade_placeholder_get_instance_private (placeholder);
116 117

  placeholder->priv->packing_actions = NULL;
118 119 120

  gtk_widget_set_can_focus (GTK_WIDGET (placeholder), TRUE);
  gtk_widget_set_has_window (GTK_WIDGET (placeholder), FALSE);
121

122 123
  gtk_widget_set_size_request (GTK_WIDGET (placeholder),
                               WIDTH_REQUISITION, HEIGHT_REQUISITION);
124

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
125 126
  _glade_dnd_dest_set (GTK_WIDGET (placeholder));

127 128
  g_signal_connect (placeholder, "notify::parent",
                    G_CALLBACK (glade_placeholder_notify_parent), NULL);
129

130 131
  gtk_widget_set_hexpand (GTK_WIDGET (placeholder), TRUE);
  gtk_widget_set_vexpand (GTK_WIDGET (placeholder), TRUE);
132
  gtk_widget_show (GTK_WIDGET (placeholder));
Jose Maria Celorio's avatar
Jose Maria Celorio committed
133 134
}

135
static void
136
glade_placeholder_finalize (GObject *object)
137
{
138 139 140 141 142
  GladePlaceholder *placeholder;

  g_return_if_fail (GLADE_IS_PLACEHOLDER (object));
  placeholder = GLADE_PLACEHOLDER (object);

143
  if (placeholder->priv->packing_actions)
144
    {
145 146
      g_list_foreach (placeholder->priv->packing_actions, (GFunc) g_object_unref, NULL);
      g_list_free (placeholder->priv->packing_actions);
147 148 149
    }

  G_OBJECT_CLASS (glade_placeholder_parent_class)->finalize (object);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
150 151
}

152
static void
153 154
glade_placeholder_set_property (GObject      *object,
                                guint         prop_id,
155
                                const GValue *value,
156
                                GParamSpec   *pspec)
157 158 159 160
{

  switch (prop_id)
    {
161 162 163 164 165 166 167 168
      case PROP_HADJUSTMENT:
      case PROP_VADJUSTMENT:
      case PROP_HSCROLL_POLICY:
      case PROP_VSCROLL_POLICY:
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
169 170 171 172
    }
}

static void
173 174 175
glade_placeholder_get_property (GObject    *object,
                                guint       prop_id,
                                GValue     *value,
176
                                GParamSpec *pspec)
177 178 179
{
  switch (prop_id)
    {
180 181 182 183 184 185 186 187 188 189 190
      case PROP_HADJUSTMENT:
      case PROP_VADJUSTMENT:
        g_value_set_object (value, NULL);
        break;
      case PROP_HSCROLL_POLICY:
      case PROP_VSCROLL_POLICY:
        g_value_set_enum (value, GTK_SCROLL_MINIMUM);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
191 192 193
    }
}

194
static void
195
glade_placeholder_realize (GtkWidget *widget)
Jose Maria Celorio's avatar
Jose Maria Celorio committed
196
{
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  GladePlaceholder *placeholder;
  GtkAllocation allocation;
  GdkWindow *window;
  GdkWindowAttr attributes;
  gint attributes_mask;

  placeholder = GLADE_PLACEHOLDER (widget);

  gtk_widget_set_realized (widget, TRUE);

  gtk_widget_get_allocation (widget, &allocation);
  attributes.x = allocation.x;
  attributes.y = allocation.y;
  attributes.width = allocation.width;
  attributes.height = allocation.height;

  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.wclass = GDK_INPUT_ONLY;
  attributes.event_mask =
      gtk_widget_get_events (widget) |
      GDK_POINTER_MOTION_MASK |
      GDK_POINTER_MOTION_HINT_MASK |
      GDK_BUTTON_PRESS_MASK |
      GDK_BUTTON_RELEASE_MASK | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK;
  attributes_mask = GDK_WA_X | GDK_WA_Y;

  window = gtk_widget_get_parent_window (widget);
  gtk_widget_set_window (widget, g_object_ref (window));

226
  placeholder->priv->event_window =
227 228
      gdk_window_new (gtk_widget_get_parent_window (widget), &attributes,
                      attributes_mask);
229
  gdk_window_set_user_data (placeholder->priv->event_window, widget);
230
}
231

232
static void
233
glade_placeholder_unrealize (GtkWidget *widget)
234
{
235 236 237 238
  GladePlaceholder *placeholder;

  placeholder = GLADE_PLACEHOLDER (widget);

239
  if (placeholder->priv->event_window)
240
    {
241 242 243
      gdk_window_set_user_data (placeholder->priv->event_window, NULL);
      gdk_window_destroy (placeholder->priv->event_window);
      placeholder->priv->event_window = NULL;
244 245 246
    }

  GTK_WIDGET_CLASS (glade_placeholder_parent_class)->unrealize (widget);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
247 248
}

249
static void
250
glade_placeholder_map (GtkWidget *widget)
Jose Maria Celorio's avatar
Jose Maria Celorio committed
251
{
252
  GladePlaceholder *placeholder;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
253

254
  placeholder = GLADE_PLACEHOLDER (widget);
255

256
  if (placeholder->priv->event_window)
257
    {
258
      gdk_window_show (placeholder->priv->event_window);
259
    }
260

261
  GTK_WIDGET_CLASS (glade_placeholder_parent_class)->map (widget);
262 263 264
}

static void
265
glade_placeholder_unmap (GtkWidget *widget)
266
{
267
  GladePlaceholder *placeholder;
268

269
  placeholder = GLADE_PLACEHOLDER (widget);
270

271
  if (placeholder->priv->event_window)
272
    {
273
      gdk_window_hide (placeholder->priv->event_window);
274
    }
275

276
  GTK_WIDGET_CLASS (glade_placeholder_parent_class)->unmap (widget);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
277
}
278

279
static void
280
glade_placeholder_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
Jose Maria Celorio's avatar
Jose Maria Celorio committed
281
{
282
  GladePlaceholder *placeholder;
283

284
  placeholder = GLADE_PLACEHOLDER (widget);
285

286
  gtk_widget_set_allocation (widget, allocation);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
287

288 289
  if (gtk_widget_get_realized (widget))
    {
290
      gdk_window_move_resize (placeholder->priv->event_window,
291 292 293
                              allocation->x, allocation->y,
                              allocation->width, allocation->height);
    }
Jose Maria Celorio's avatar
Jose Maria Celorio committed
294
}
295

296
static gboolean
297
glade_placeholder_draw (GtkWidget *widget, cairo_t *cr)
298
{
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
299
  GladePlaceholder *placeholder = GLADE_PLACEHOLDER (widget);
300 301
  gint h = gtk_widget_get_allocated_height (widget) - 1;
  gint w = gtk_widget_get_allocated_width (widget) - 1;
302

303 304 305 306 307 308 309 310
  if (placeholder_pattern)
    {
      cairo_save (cr);
      cairo_rectangle (cr, 0, 0, w, h);
      cairo_set_source (cr, placeholder_pattern);
      cairo_fill (cr);
      cairo_restore (cr);
    }
311

312
  cairo_translate (cr, .5, .5);
313
  cairo_set_line_width (cr, 1.0);
314

315 316 317 318 319 320 321 322 323 324 325 326
  /* We hardcode colors here since we are already using an image as bg pattern */
  cairo_set_source_rgb (cr, .9, .9, .9);
  cairo_move_to (cr, w, 0);
  cairo_line_to (cr, 0, 0);
  cairo_line_to (cr, 0, h);
  cairo_stroke (cr);

  cairo_set_source_rgb (cr, .64, .64, .64);
  cairo_move_to (cr, w, 0);
  cairo_line_to (cr, w, h);
  cairo_line_to (cr, 0, h);
  cairo_stroke (cr);
327

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
328 329 330 331 332 333 334 335
  if (placeholder->priv->drag_highlight)
    {
      cairo_pattern_t *gradient;
      GtkStyleContext *context;
      gdouble ww, hh;
      GdkRGBA c;

      context = gtk_widget_get_style_context (widget);
336 337 338 339
      gtk_style_context_save (context);
      gtk_style_context_get_background_color (context,
                                              gtk_style_context_get_state (context) |
                                              GTK_STATE_FLAG_SELECTED |
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
340
                                              GTK_STATE_FLAG_FOCUSED, &c);
341
      gtk_style_context_restore (context);
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

      ww = w/2.0;
      hh = h/2.0;
      gradient = cairo_pattern_create_radial (ww, hh, MIN (w, h)/6,
                                              ww, hh, MAX (ww, hh));
      cairo_pattern_add_color_stop_rgba (gradient, 0, c.red, c.green, c.blue, .08);
      cairo_pattern_add_color_stop_rgba (gradient, 1, c.red, c.green, c.blue, .28);

      cairo_set_source (cr, gradient);

      cairo_rectangle (cr, 0, 0, w, h);
      cairo_fill (cr);

      cairo_pattern_destroy (gradient);
    }

358
  return FALSE;
359 360
}

361 362
static void
glade_placeholder_update_cursor (GladePlaceholder *placeholder, GdkWindow *win)
Paolo Borelli's avatar
Paolo Borelli committed
363
{
364 365
  GladeProject *project = glade_placeholder_get_project (placeholder);
  GladePointerMode pointer_mode = glade_project_get_pointer_mode (project);
366

367
  if (pointer_mode == GLADE_POINTER_SELECT)
368
    glade_cursor_set (project, win, GLADE_CURSOR_SELECTOR);
369
  else if (pointer_mode == GLADE_POINTER_ADD_WIDGET)
370 371
    glade_cursor_set (project, win, GLADE_CURSOR_ADD_WIDGET);
}
372

373 374 375 376 377 378 379 380 381 382 383 384
static gboolean
glade_placeholder_enter_notify_event (GtkWidget        *widget, 
                                      GdkEventCrossing *event)
{
  glade_placeholder_update_cursor (GLADE_PLACEHOLDER (widget), event->window);
  return FALSE;
}

static gboolean
glade_placeholder_motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
{
  glade_placeholder_update_cursor (GLADE_PLACEHOLDER (widget), event->window);
385
  return FALSE;
Paolo Borelli's avatar
Paolo Borelli committed
386 387
}

388
static void
389 390 391
on_chooser_adaptor_widget_selected (_GladeAdaptorChooserWidget *chooser,
                                    GladeWidgetAdaptor         *adaptor,
                                    GladePlaceholder           *placeholder)
392 393 394 395

{
  glade_command_create (adaptor, glade_placeholder_get_parent (placeholder),
                        placeholder, glade_placeholder_get_project (placeholder));
396
  gtk_widget_destroy (gtk_widget_get_ancestor (GTK_WIDGET (chooser), GTK_TYPE_POPOVER));
397 398 399 400 401 402 403 404
}

static GtkWidget *
glade_placeholder_popover_new (GladePlaceholder *placeholder, GtkWidget *relative_to)
{
  GtkWidget *pop = gtk_popover_new (relative_to);
  GtkWidget *chooser;

405 406 407 408 409
  chooser = _glade_adaptor_chooser_widget_new (GLADE_ADAPTOR_CHOOSER_WIDGET_WIDGET |
                                               GLADE_ADAPTOR_CHOOSER_WIDGET_SKIP_TOPLEVEL |
                                               GLADE_ADAPTOR_CHOOSER_WIDGET_SKIP_DEPRECATED,
                                               glade_placeholder_get_project (placeholder));
  _glade_adaptor_chooser_widget_populate (GLADE_ADAPTOR_CHOOSER_WIDGET (chooser));
410
  g_signal_connect (chooser, "adaptor-selected",
411
                    G_CALLBACK (on_chooser_adaptor_widget_selected),
412 413 414 415 416 417 418 419
                    placeholder);
  gtk_popover_set_position (GTK_POPOVER (pop), GTK_POS_BOTTOM);
  gtk_container_add (GTK_CONTAINER (pop), chooser);
  gtk_widget_show (chooser);

  return pop;
}

420
static gboolean
421
glade_placeholder_button_press (GtkWidget *widget, GdkEventButton *event)
422
{
423 424
  GladePlaceholder   *placeholder;
  GladeProject       *project;
425
  GladeWidgetAdaptor *adaptor;
426
  gboolean            handled = FALSE;
427 428 429 430

  g_return_val_if_fail (GLADE_IS_PLACEHOLDER (widget), FALSE);

  placeholder = GLADE_PLACEHOLDER (widget);
431 432
  project     = glade_placeholder_get_project (placeholder);
  adaptor     = glade_project_get_add_item (project);
433 434 435 436

  if (!gtk_widget_has_focus (widget))
    gtk_widget_grab_focus (widget);

437 438
  if ((event->button == 1 || event->button == 2) &&
       event->type == GDK_BUTTON_PRESS && adaptor != NULL)
439
    {
440
      GladeWidget *parent = glade_placeholder_get_parent (placeholder);
441

442 443 444 445
      /* A widget type is selected in the palette.
       * Add a new widget of that type.
       */
      glade_command_create (adaptor, parent, placeholder, project);
446

447 448 449
      /* Let the user use the middle button to create more than one widget */
      if (event->button != 2)
        {
450 451
          glade_project_set_add_item (project, NULL);
          /* reset the cursor */
452
          glade_project_set_pointer_mode (project, GLADE_POINTER_SELECT);
453
        }
454 455

      handled = TRUE;
456
    }
457 458 459 460 461 462 463 464 465 466 467 468 469 470
  else if (event->button == 1 &&
           event->type == GDK_2BUTTON_PRESS &&
           adaptor == NULL)
    {
      GtkWidget *event_widget = gtk_get_event_widget ((GdkEvent*) event);
      GladeWidget *toplevel = glade_widget_get_toplevel (glade_placeholder_get_parent (placeholder));
      GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (glade_widget_get_object (toplevel)));
      GtkWidget *pop = glade_placeholder_popover_new (placeholder, parent);
      GdkRectangle rect = {0, 0, 8, 8};

      gtk_widget_translate_coordinates (event_widget, parent,
                                        event->x, event->y,
                                        &rect.x, &rect.y);
      gtk_popover_set_pointing_to (GTK_POPOVER (pop), &rect);
471
      gtk_popover_popup (GTK_POPOVER (pop));
472 473
      handled = TRUE;
   }
474 475 476 477 478 479 480 481

  if (!handled && glade_popup_is_popup_event (event))
    {
      glade_popup_placeholder_pop (placeholder, event);
      handled = TRUE;
    }

  return handled;
482 483 484
}

static gboolean
485
glade_placeholder_popup_menu (GtkWidget *widget)
486
{
487
  g_return_val_if_fail (GLADE_IS_PLACEHOLDER (widget), FALSE);
488

489
  glade_popup_placeholder_pop (GLADE_PLACEHOLDER (widget), NULL);
490

491
  return TRUE;
492
}
493

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
static gboolean
glade_placeholder_drag_can_drag (_GladeDrag *source)
{
  GladeWidget *parent = glade_placeholder_get_parent (GLADE_PLACEHOLDER (source));
  return (parent) ? _glade_drag_can_drag (GLADE_DRAG (parent)) : FALSE;
}

static gboolean
glade_placeholder_drag_can_drop (_GladeDrag *dest, gint x, gint y, GObject *data)
{
  if (GLADE_IS_WIDGET_ADAPTOR (data))
    {
      GType otype = glade_widget_adaptor_get_object_type (GLADE_WIDGET_ADAPTOR (data));

      if (g_type_is_a (otype, GTK_TYPE_WIDGET) && !GWA_IS_TOPLEVEL (data))
        return TRUE;
    }
  else if (GTK_IS_WIDGET (data))
    {
      GladeWidget *parent, *new_child;

      /* Avoid recursion */
      if (gtk_widget_is_ancestor (GTK_WIDGET (dest), GTK_WIDGET (data)))
        return FALSE;

      parent = glade_placeholder_get_parent (GLADE_PLACEHOLDER (dest));

      if ((new_child = glade_widget_get_from_gobject (data)) &&
          !glade_widget_add_verify (parent, new_child, FALSE))
        return FALSE;

      return TRUE;
    }

  return FALSE;
}

static gboolean
glade_placeholder_drag_drop (_GladeDrag *dest, gint x, gint y, GObject *data)
{
  GladePlaceholder *placeholder = GLADE_PLACEHOLDER (dest);
  GladeWidget *gsource;

  if (!data)
    return FALSE;
  
  if (GLADE_IS_WIDGET_ADAPTOR (data))
    {
      GladeWidget *parent = glade_placeholder_get_parent (placeholder);
      
      glade_command_create (GLADE_WIDGET_ADAPTOR (data), parent, placeholder, 
                            glade_widget_get_project (parent));
      return TRUE;
    }
  else if ((gsource = glade_widget_get_from_gobject (data)))
    {
      GladeWidget *parent = glade_placeholder_get_parent (placeholder);
      GList widgets = {gsource, NULL, NULL};

      /* Check for recursive paste */
      if (parent != gsource)
        {
          glade_command_dnd (&widgets, parent, placeholder);
          return TRUE;
        }
    }

  return FALSE;
}

static void
glade_placeholder_drag_highlight (_GladeDrag *dest, gint x, gint y)
{
  GladePlaceholderPrivate *priv = GLADE_PLACEHOLDER (dest)->priv;
  gboolean highlight = !(x < 0 || y < 0);

  if (priv->drag_highlight == highlight)
    return;

  priv->drag_highlight = highlight;
  gtk_widget_queue_draw (GTK_WIDGET (dest));
}

static void
glade_placeholder_drag_init (_GladeDragInterface *iface)
{
  iface->can_drag = glade_placeholder_drag_can_drag;
  iface->can_drop = glade_placeholder_drag_can_drop;
  iface->drop = glade_placeholder_drag_drop;
  iface->highlight = glade_placeholder_drag_highlight;
}

static void
glade_placeholder_class_init (GladePlaceholderClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  gchar *path;
  cairo_surface_t *surface;

  object_class->finalize = glade_placeholder_finalize;
  object_class->set_property = glade_placeholder_set_property;
  object_class->get_property = glade_placeholder_get_property;

  widget_class->realize = glade_placeholder_realize;
  widget_class->unrealize = glade_placeholder_unrealize;
  widget_class->map = glade_placeholder_map;
  widget_class->unmap = glade_placeholder_unmap;
  widget_class->size_allocate = glade_placeholder_size_allocate;
  widget_class->draw = glade_placeholder_draw;
604
  widget_class->enter_notify_event = glade_placeholder_enter_notify_event;
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
  widget_class->motion_notify_event = glade_placeholder_motion_notify_event;
  widget_class->button_press_event = glade_placeholder_button_press;
  widget_class->popup_menu = glade_placeholder_popup_menu;
  
  /* GtkScrollable implementation */
  g_object_class_override_property (object_class, PROP_HADJUSTMENT,
                                    "hadjustment");
  g_object_class_override_property (object_class, PROP_VADJUSTMENT,
                                    "vadjustment");
  g_object_class_override_property (object_class, PROP_HSCROLL_POLICY,
                                    "hscroll-policy");
  g_object_class_override_property (object_class, PROP_VSCROLL_POLICY,
                                    "vscroll-policy");

  /* Create our tiled background pattern */
  path = g_build_filename (glade_app_get_pixmaps_dir (), "placeholder.png", NULL);
  surface = cairo_image_surface_create_from_png (path);

  if (!surface)
    g_warning ("Failed to create surface for %s\n", path);
  else
    {
      placeholder_pattern = cairo_pattern_create_for_surface (surface);
      cairo_pattern_set_extend (placeholder_pattern, CAIRO_EXTEND_REPEAT);
    }
  g_free (path);
}
632 633 634 635 636 637 638 639 640 641 642 643 644

/**
 * glade_placeholder_new:
 * 
 * Returns: a new #GladePlaceholder cast as a #GtkWidget
 */
GtkWidget *
glade_placeholder_new (void)
{
  return g_object_new (GLADE_TYPE_PLACEHOLDER, NULL);
}

GladeProject *
645
glade_placeholder_get_project (GladePlaceholder *placeholder)
646 647 648 649 650 651
{
  GladeWidget *parent;
  parent = glade_placeholder_get_parent (placeholder);
  return parent ? glade_widget_get_project (parent) : NULL;
}

652
GladeWidget *
653
glade_placeholder_get_parent (GladePlaceholder *placeholder)
654
{
655 656 657 658 659 660 661 662 663 664 665 666
  GtkWidget *widget;
  GladeWidget *parent = NULL;

  g_return_val_if_fail (GLADE_IS_PLACEHOLDER (placeholder), NULL);

  for (widget = gtk_widget_get_parent (GTK_WIDGET (placeholder));
       widget != NULL; widget = gtk_widget_get_parent (widget))
    {
      if ((parent = glade_widget_get_from_gobject (widget)) != NULL)
        break;
    }
  return parent;
667
}
668 669 670 671 672 673 674 675 676

GList *
glade_placeholder_packing_actions (GladePlaceholder *placeholder)
{
  g_return_val_if_fail (GLADE_IS_PLACEHOLDER (placeholder), NULL);

  return placeholder->priv->packing_actions;
}