glade-widget.c 125 KB
Newer Older
Jose Maria Celorio's avatar
Jose Maria Celorio committed
1
/*
2
 * Copyright (C) 2008 Tristan Van Berkom
3
4
 * Copyright (C) 2004 Joaquin Cuenca Abela
 * Copyright (C) 2001, 2002, 2003 Ximian, Inc.
Jose Maria Celorio's avatar
Jose Maria Celorio committed
5
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Jose Maria Celorio's avatar
Jose Maria Celorio committed
19
20
 *
 * Authors:
21
 *   Joaquin Cuenca Abela <e98cuenc@yahoo.com>
Jose Maria Celorio's avatar
Jose Maria Celorio committed
22
 *   Chema Celorio <chema@celorio.com>
23
 *   Tristan Van Berkom <tvb@gnome.org>
Jose Maria Celorio's avatar
Jose Maria Celorio committed
24
25
 */

26
27
28
29
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

30
31
32
33
34
35
36
37
38
/**
 * SECTION:glade-widget
 * @Short_Description: An object wrapper for the Glade runtime environment.
 *
 * #GladeWidget is the proxy between the instantiated runtime object and
 * the Glade core metadata. This api will be mostly usefull for its
 * convenience api for getting and setting properties (mostly from the plugin).
 */

Jose Maria Celorio's avatar
Jose Maria Celorio committed
39
#include <string.h>
40
#include <glib-object.h>
41
42
#include <gdk/gdkkeysyms.h>
#include <glib/gi18n-lib.h>
Jose Maria Celorio's avatar
Jose Maria Celorio committed
43
#include "glade.h"
44
#include "glade-accumulators.h"
Jose Maria Celorio's avatar
Jose Maria Celorio committed
45
#include "glade-project.h"
46
#include "glade-widget-adaptor.h"
47
48
#include "glade-widget.h"
#include "glade-marshallers.h"
Jose Maria Celorio's avatar
Jose Maria Celorio committed
49
50
#include "glade-property.h"
#include "glade-property-class.h"
51
#include "glade-placeholder.h"
52
#include "glade-signal.h"
53
#include "glade-popup.h"
54
#include "glade-editor.h"
55
#include "glade-app.h"
56
#include "glade-design-view.h"
57
#include "glade-widget-action.h"
58

Jose Maria Celorio's avatar
Jose Maria Celorio committed
59

60

61
62
63
64
65
66
static void glade_widget_set_adaptor (GladeWidget * widget,
                                      GladeWidgetAdaptor * adaptor);
static void glade_widget_set_properties (GladeWidget * widget,
                                         GList * properties);
static void glade_widget_set_object (GladeWidget * gwidget,
                                     GObject * new_object, gboolean destroy);
67

68
enum
Jose Maria Celorio's avatar
Jose Maria Celorio committed
69
{
70
71
72
73
74
75
76
77
  ADD_SIGNAL_HANDLER,
  REMOVE_SIGNAL_HANDLER,
  CHANGE_SIGNAL_HANDLER,
  BUTTON_PRESS_EVENT,
  BUTTON_RELEASE_EVENT,
  MOTION_NOTIFY_EVENT,
  SUPPORT_CHANGED,
  LAST_SIGNAL
78
};
Jose Maria Celorio's avatar
Jose Maria Celorio committed
79

80
enum
81
{
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  PROP_0,
  PROP_NAME,
  PROP_INTERNAL,
  PROP_ANARCHIST,
  PROP_OBJECT,
  PROP_ADAPTOR,
  PROP_PROJECT,
  PROP_PROPERTIES,
  PROP_PARENT,
  PROP_INTERNAL_NAME,
  PROP_TEMPLATE,
  PROP_TEMPLATE_EXACT,
  PROP_REASON,
  PROP_TOPLEVEL_WIDTH,
  PROP_TOPLEVEL_HEIGHT,
  PROP_SUPPORT_WARNING
98
99
};

100
101
102
static guint glade_widget_signals[LAST_SIGNAL] = { 0 };

static GQuark glade_widget_name_quark = 0;
Tristan Van Berkom's avatar
updated    
Tristan Van Berkom committed
103

104

105
106
107
108
109
#define IS_GLADE_WIDGET_EVENT(event)		 \
	((event) == GDK_BUTTON_PRESS ||		 \
	 (event) == GDK_BUTTON_RELEASE ||	 \
	 (event) == GDK_MOTION_NOTIFY)

110
G_DEFINE_TYPE (GladeWidget, glade_widget, G_TYPE_INITIALLY_UNOWNED)
111
112
113
/*******************************************************************************
                           GladeWidget class methods
 *******************************************************************************/
114
115
116
     static void
         glade_widget_set_packing_actions (GladeWidget * widget,
                                           GladeWidget * parent)
117
{
118
119
120
121
122
123
124
125
126
127
  if (widget->packing_actions)
    {
      g_list_foreach (widget->packing_actions, (GFunc) g_object_unref, NULL);
      g_list_free (widget->packing_actions);
      widget->packing_actions = NULL;
    }

  if (parent->adaptor->packing_actions)
    widget->packing_actions =
        glade_widget_adaptor_pack_actions_new (parent->adaptor);
128
129
}

130
static void
131
132
glade_widget_add_child_impl (GladeWidget * widget,
                             GladeWidget * child, gboolean at_mouse)
133
{
134
  g_object_ref (child);
135

136
137
138
139
140
  /* Safe to set the parent first... setting it afterwards
   * creates packing properties, and that is not always
   * desirable.
   */
  glade_widget_set_parent (child, widget);
141

142
143
144
  /* Set packing actions first so we have access from the plugin
   */
  glade_widget_set_packing_actions (child, widget);
145

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  glade_widget_adaptor_add (widget->adaptor, widget->object, child->object);

  /* XXX FIXME:
   * We have a fundamental flaw here, we set packing props
   * after parenting the widget so that we can introspect the
   * values setup by the runtime widget, in which case the plugin
   * cannot access its packing properties and set them sensitive
   * or connect to thier signals etc. maybe its not so important
   * but its a flaw worthy of note, some kind of double pass api
   * would be needed to accomadate this.
   */


  /* Setup packing properties here so we can introspect the new
   * values from the backend.
   */
  glade_widget_set_packing_properties (child, widget);
163
}
164

165
static void
166
glade_widget_remove_child_impl (GladeWidget * widget, GladeWidget * child)
167
{
168
  glade_widget_adaptor_remove (widget->adaptor, widget->object, child->object);
169

170
  child->parent = NULL;
171

172
  g_object_unref (child);
173
}
174

Tristan Van Berkom's avatar
updated    
Tristan Van Berkom committed
175
static void
176
177
glade_widget_replace_child_impl (GladeWidget * widget,
                                 GObject * old_object, GObject * new_object)
Tristan Van Berkom's avatar
updated    
Tristan Van Berkom committed
178
{
179
180
  GladeWidget *gnew_widget = glade_widget_get_from_gobject (new_object);
  GladeWidget *gold_widget = glade_widget_get_from_gobject (old_object);
Tristan Van Berkom's avatar
updated    
Tristan Van Berkom committed
181

182
183
184
  if (gnew_widget)
    {
      g_object_ref (gnew_widget);
185

186
      gnew_widget->parent = widget;
187

188
189
190
191
      /* Set packing actions first so we have access from the plugin
       */
      glade_widget_set_packing_actions (gnew_widget, widget);
    }
192

193
194
195
  if (gold_widget)
    {
      g_object_unref (gold_widget);
196

197
198
199
      if (gold_widget != gnew_widget)
        gold_widget->parent = NULL;
    }
Tristan Van Berkom's avatar
updated    
Tristan Van Berkom committed
200

201
202
  glade_widget_adaptor_replace_child
      (widget->adaptor, widget->object, old_object, new_object);
Tristan Van Berkom's avatar
updated    
Tristan Van Berkom committed
203

204
205
206
207
208
  /* Setup packing properties here so we can introspect the new
   * values from the backend.
   */
  if (gnew_widget)
    glade_widget_set_packing_properties (gnew_widget, widget);
Tristan Van Berkom's avatar
updated    
Tristan Van Berkom committed
209
210
}

211
static void
212
213
glade_widget_add_signal_handler_impl (GladeWidget * widget,
                                      GladeSignal * signal_handler)
214
{
215
216
  GPtrArray *signals;
  GladeSignal *new_signal_handler;
217

218
219
  g_return_if_fail (GLADE_IS_WIDGET (widget));
  g_return_if_fail (GLADE_IS_SIGNAL (signal_handler));
220

221
222
223
224
225
226
227
  signals = glade_widget_list_signal_handlers (widget, signal_handler->name);
  if (!signals)
    {
      signals = g_ptr_array_new ();
      g_hash_table_insert (widget->signals, g_strdup (signal_handler->name),
                           signals);
    }
228

229
230
  new_signal_handler = glade_signal_clone (signal_handler);
  g_ptr_array_add (signals, new_signal_handler);
231

232
  glade_project_update_signal_support_warning (widget, new_signal_handler);
233
234
}

235
static void
236
237
glade_widget_remove_signal_handler_impl (GladeWidget * widget,
                                         GladeSignal * signal_handler)
238
{
239
240
241
  GPtrArray *signals;
  GladeSignal *tmp_signal_handler;
  guint i;
242

243
244
  g_return_if_fail (GLADE_IS_WIDGET (widget));
  g_return_if_fail (GLADE_IS_SIGNAL (signal_handler));
245

246
  signals = glade_widget_list_signal_handlers (widget, signal_handler->name);
247

248
249
  /* trying to remove an inexistent signal? */
  g_assert (signals);
250

251
252
253
254
255
256
257
258
259
260
  for (i = 0; i < signals->len; i++)
    {
      tmp_signal_handler = g_ptr_array_index (signals, i);
      if (glade_signal_equal (tmp_signal_handler, signal_handler))
        {
          glade_signal_free (tmp_signal_handler);
          g_ptr_array_remove_index (signals, i);
          break;
        }
    }
261
}
262

263
static void
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
glade_widget_change_signal_handler_impl (GladeWidget * widget,
                                         GladeSignal * old_signal_handler,
                                         GladeSignal * new_signal_handler)
{
  GPtrArray *signals;
  GladeSignal *signal_handler_iter;
  guint i;

  g_return_if_fail (GLADE_IS_WIDGET (widget));
  g_return_if_fail (GLADE_IS_SIGNAL (old_signal_handler));
  g_return_if_fail (GLADE_IS_SIGNAL (new_signal_handler));
  g_return_if_fail (strcmp (old_signal_handler->name, new_signal_handler->name)
                    == 0);

  signals =
      glade_widget_list_signal_handlers (widget, old_signal_handler->name);

  /* trying to remove an inexistent signal? */
  g_assert (signals);

  for (i = 0; i < signals->len; i++)
    {
      signal_handler_iter = g_ptr_array_index (signals, i);
      if (glade_signal_equal (signal_handler_iter, old_signal_handler))
        {
          if (strcmp (old_signal_handler->handler,
                      new_signal_handler->handler) != 0)
            {
              g_free (signal_handler_iter->handler);
              signal_handler_iter->handler =
                  g_strdup (new_signal_handler->handler);
            }

          /* Handler */
          if (signal_handler_iter->handler)
            g_free (signal_handler_iter->handler);
          signal_handler_iter->handler = g_strdup (new_signal_handler->handler);

          /* Object */
          if (signal_handler_iter->userdata)
            g_free (signal_handler_iter->userdata);
          signal_handler_iter->userdata =
              g_strdup (new_signal_handler->userdata);

          signal_handler_iter->after = new_signal_handler->after;
          signal_handler_iter->swapped = new_signal_handler->swapped;
          break;
        }
    }
313
}
Tristan Van Berkom's avatar
Tristan Van Berkom committed
314
315


316
static gboolean
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
glade_widget_button_press_event_impl (GladeWidget * gwidget,
                                      GdkEvent * base_event)
{
  GtkWidget *widget;
  GdkEventButton *event = (GdkEventButton *) base_event;
  gboolean handled = FALSE;

  /* make sure to grab focus, since we may stop default handlers */
  widget = GTK_WIDGET (glade_widget_get_object (gwidget));
  if (gtk_widget_get_can_focus (widget) && !gtk_widget_has_focus (widget))
    gtk_widget_grab_focus (widget);

  /* if it's already selected don't stop default handlers, e.g. toggle button */
  if (event->button == 1)
    {
      if (event->state & GDK_CONTROL_MASK)
        {
          if (glade_project_is_selected (gwidget->project, gwidget->object))
            glade_app_selection_remove (gwidget->object, TRUE);
          else
            glade_app_selection_add (gwidget->object, TRUE);
          handled = TRUE;
        }
      else if (glade_project_is_selected (gwidget->project,
                                          gwidget->object) == FALSE)
        {
          glade_util_clear_selection ();
          glade_app_selection_set (gwidget->object, TRUE);

          /* Add selection without interrupting event flow 
           * when shift is down, this allows better behaviour
           * for GladeFixed children 
           */
          handled = !(event->state & GDK_SHIFT_MASK);
        }
    }

  /* Give some kind of access in case of missing right button */
  if (!handled && glade_popup_is_popup_event (event))
    {
      glade_popup_widget_pop (gwidget, event, TRUE);
      handled = TRUE;
    }

  return handled;
362
}
363

364
static gboolean
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
glade_widget_event_impl (GladeWidget * gwidget, GdkEvent * event)
{
  gboolean handled = FALSE;

  g_return_val_if_fail (GLADE_IS_WIDGET (gwidget), FALSE);

  switch (event->type)
    {
      case GDK_BUTTON_PRESS:
        g_signal_emit (gwidget,
                       glade_widget_signals[BUTTON_PRESS_EVENT], 0,
                       event, &handled);
        break;
      case GDK_BUTTON_RELEASE:
        g_signal_emit (gwidget,
                       glade_widget_signals[BUTTON_RELEASE_EVENT], 0,
                       event, &handled);
        break;
      case GDK_MOTION_NOTIFY:
        g_signal_emit (gwidget,
                       glade_widget_signals[MOTION_NOTIFY_EVENT], 0,
                       event, &handled);
        break;
      default:
        break;
    }

  return handled;
393
394
}

395
396
397
398
399
400
401
402

/**
 * glade_widget_event:
 * @event: A #GdkEvent
 *
 * Feed an event to be handled on the project GladeWidget
 * hierarchy.
 *
403
 * Returns: whether the event was handled or not.
404
405
 */
gboolean
406
glade_widget_event (GladeWidget * gwidget, GdkEvent * event)
Tristan Van Berkom's avatar
updated    
Tristan Van Berkom committed
407
{
408
  gboolean handled = FALSE;
Tristan Van Berkom's avatar
updated    
Tristan Van Berkom committed
409

410
411
412
  /* Lets just avoid some synthetic events (like focus-change) */
  if (((GdkEventAny *) event)->window == NULL)
    return FALSE;
413

414
  handled = GLADE_WIDGET_GET_CLASS (gwidget)->event (gwidget, event);
Tristan Van Berkom's avatar
updated    
Tristan Van Berkom committed
415

416
#if 0
417
418
419
  if (event->type != GDK_EXPOSE)
    g_print ("event widget '%s' handled '%d' event '%d'\n",
             gwidget->name, handled, event->type);
Tristan Van Berkom's avatar
updated    
Tristan Van Berkom committed
420
#endif
421

422
  return handled;
Tristan Van Berkom's avatar
updated    
Tristan Van Berkom committed
423
424
}

425
426
427
/*******************************************************************************
                      GObjectClass & Object Construction
 *******************************************************************************/
428
429
430

/*
 * This function creates new GObject parameters based on the GType of the 
431
 * GladeWidgetAdaptor and its default values.
432
433
434
435
436
 *
 * If a GladeWidget is specified, it will be used to apply the
 * values currently in use.
 */
static GParameter *
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
glade_widget_template_params (GladeWidget * widget,
                              gboolean construct, guint * n_params)
{
  GladeWidgetAdaptor *klass;
  GArray *params;
  GObjectClass *oclass;
  GParamSpec **pspec;
  GladeProperty *glade_property;
  GladePropertyClass *pclass;
  guint n_props, i;

  g_return_val_if_fail (GLADE_IS_WIDGET (widget), NULL);
  g_return_val_if_fail (n_params != NULL, NULL);

  klass = widget->adaptor;

  /* As a slight optimization, we never unref the class
   */
  oclass = g_type_class_ref (klass->type);
  pspec = g_object_class_list_properties (oclass, &n_props);
  params = g_array_new (FALSE, FALSE, sizeof (GParameter));

  for (i = 0; i < n_props; i++)
    {
      GParameter parameter = { 0, };

      if ((glade_property =
           glade_widget_get_property (widget, pspec[i]->name)) == NULL)
        continue;

      pclass = glade_property->klass;

      /* Ignore properties based on some criteria
       */
      if (!glade_property_get_enabled (glade_property) || pclass == NULL ||     /* Unaccounted for in the builder */
          pclass->virt ||       /* should not be set before 
                                   GladeWidget wrapper exists */
          pclass->ignore)       /* Catalog explicitly ignores the object */
        continue;

      if (construct &&
          (pspec[i]->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0)
        continue;
      else if (!construct &&
               (pspec[i]->flags &
                (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) != 0)
        continue;

      if (g_value_type_compatible (G_VALUE_TYPE (pclass->def),
                                   pspec[i]->value_type) == FALSE)
        {
          g_critical ("Type mismatch on %s property of %s",
                      parameter.name, klass->name);
          continue;
        }

      if (g_param_values_cmp (pspec[i],
                              glade_property->value, pclass->orig_def) == 0)
        continue;


      parameter.name = pspec[i]->name;  /* These are not copied/freed */
      g_value_init (&parameter.value, pspec[i]->value_type);
      g_value_copy (glade_property->value, &parameter.value);

      g_array_append_val (params, parameter);
    }
  g_free (pspec);

  *n_params = params->len;
  return (GParameter *) g_array_free (params, FALSE);
508
509
510
}

static void
511
free_params (GParameter * params, guint n_params)
512
{
513
514
515
516
  gint i;
  for (i = 0; i < n_params; i++)
    g_value_unset (&(params[i].value));
  g_free (params);
517
518
519
520

}

static GObject *
521
522
523
524
525
526
glade_widget_build_object (GladeWidget * widget,
                           GladeWidget * template, GladeCreateReason reason)
{
  GParameter *params;
  GObject *object;
  guint n_params, i;
527

528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
  if (reason == GLADE_CREATE_LOAD)
    {
      object = glade_widget_adaptor_construct_object (widget->adaptor, 0, NULL);
      glade_widget_set_object (widget, object, TRUE);
      return object;
    }

  if (template)
    params = glade_widget_template_params (widget, TRUE, &n_params);
  else
    params =
        glade_widget_adaptor_default_params (widget->adaptor, TRUE, &n_params);

  /* Create the new object with the correct parameters.
   */
  object =
      glade_widget_adaptor_construct_object (widget->adaptor, n_params, params);
545

546
  free_params (params, n_params);
547

548
549
  /* Dont destroy toplevels when rebuilding, handle that separately */
  glade_widget_set_object (widget, object, reason != GLADE_CREATE_REBUILD);
550

551
552
553
554
555
  if (template)
    params = glade_widget_template_params (widget, FALSE, &n_params);
  else
    params =
        glade_widget_adaptor_default_params (widget->adaptor, FALSE, &n_params);
556

557
558
559
  for (i = 0; i < n_params; i++)
    glade_widget_adaptor_set_property (widget->adaptor, object, params[i].name,
                                       &(params[i].value));
560

561
562
563
  free_params (params, n_params);

  return object;
564
565
}

566
567
/**
 * glade_widget_dup_properties:
568
 * @dest_widget: the widget we are copying properties for
569
570
 * @template_props: the #GladeProperty list to copy
 * @as_load: whether to behave as if loading the project
571
572
 * @copy_parentless: whether to copy reffed widgets at all
 * @exact: whether to copy reffed widgets exactly
573
574
575
576
577
578
 *
 * Copies a list of properties, if @as_load is specified, then
 * properties that are not saved to the glade file are ignored.
 *
 * Returns: A newly allocated #GList of new #GladeProperty objects.
 */
579
GList *
580
581
582
glade_widget_dup_properties (GladeWidget * dest_widget, GList * template_props,
                             gboolean as_load, gboolean copy_parentless,
                             gboolean exact)
583
{
584
585
586
587
588
  GList *list, *properties = NULL;

  for (list = template_props; list && list->data; list = list->next)
    {
      GladeProperty *prop = list->data;
589

590
591
      if (prop->klass->save == FALSE && as_load)
        continue;
592

593

594
595
596
597
      if (prop->klass->parentless_widget && copy_parentless)
        {
          GObject *object = NULL;
          GladeWidget *parentless;
598

599
          glade_property_get (prop, &object);
600

601
          prop = glade_property_dup (prop, NULL);
602

603
604
605
          if (object)
            {
              parentless = glade_widget_get_from_gobject (object);
606

607
              parentless = glade_widget_dup (parentless, exact);
608

609
              glade_widget_set_project (parentless, dest_widget->project);
610

611
612
613
614
615
              glade_property_set (prop, parentless->object);
            }
        }
      else
        prop = glade_property_dup (prop, NULL);
616
617


618
619
620
      properties = g_list_prepend (properties, prop);
    }
  return g_list_reverse (properties);
621
622
}

623
624
625
626
627
628
629
630
631
632
633
634
635
/**
 * glade_widget_remove_property:
 * @widget: A #GladeWidget
 * @id_property: the name of the property
 *
 * Removes the #GladeProperty indicated by @id_property
 * from @widget (this is intended for use in the plugin, to
 * remove properties from composite children that dont make
 * sence to allow the user to specify, notably - properties
 * that are proxied through the composite widget's properties or
 * style properties).
 */
void
636
glade_widget_remove_property (GladeWidget * widget, const gchar * id_property)
637
{
638
  GladeProperty *prop;
639

640
641
  g_return_if_fail (GLADE_IS_WIDGET (widget));
  g_return_if_fail (id_property);
642

643
644
645
646
647
648
649
650
651
652
653
654
655
  /* XXX FIXME: currently we arent calling this on packing properties,
   * but doing so could cause crashes because the hash table is not
   * managed properly
   */
  if ((prop = glade_widget_get_property (widget, id_property)) != NULL)
    {
      widget->properties = g_list_remove (widget->properties, prop);
      g_hash_table_remove (widget->props_hash, prop->klass->id);
      g_object_unref (prop);
    }
  else
    g_critical ("Couldnt find property %s on widget %s\n",
                id_property, widget->name);
656
657
}

658
static void
659
glade_widget_set_catalog_defaults (GList * list)
660
{
661
662
663
664
665
666
667
668
669
670
  GList *l;
  for (l = list; l && l->data; l = l->next)
    {
      GladeProperty *prop = l->data;
      GladePropertyClass *klass = prop->klass;

      if (glade_property_equals_value (prop, klass->orig_def) &&
          g_param_values_cmp (klass->pspec, klass->orig_def, klass->def))
        glade_property_reset (prop);
    }
671
}
672

673
static void
674
glade_widget_sync_custom_props (GladeWidget * widget)
675
{
676
677
678
679
  GList *l;
  for (l = widget->properties; l && l->data; l = l->next)
    {
      GladeProperty *prop = GLADE_PROPERTY (l->data);
680

681
682
      if (prop->klass->virt || prop->klass->needs_sync)
        glade_property_sync (prop);
683

684
    }
685
686
687
}

static void
688
glade_widget_sync_packing_props (GladeWidget * widget)
689
{
690
691
692
693
694
695
  GList *l;
  for (l = widget->packing_properties; l && l->data; l = l->next)
    {
      GladeProperty *prop = GLADE_PROPERTY (l->data);
      glade_property_sync (prop);
    }
696
697
}

698

699
static GObject *
700
701
702
703
704
705
706
glade_widget_constructor (GType type,
                          guint n_construct_properties,
                          GObjectConstructParam * construct_properties)
{
  GladeWidget *gwidget;
  GObject *ret_obj, *object;
  GList *properties = NULL, *list;
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
735
736
737
738
739
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
804
805
806
  ret_obj = G_OBJECT_CLASS (glade_widget_parent_class)->constructor
      (type, n_construct_properties, construct_properties);

  gwidget = GLADE_WIDGET (ret_obj);

  if (gwidget->name == NULL)
    {
      if (gwidget->internal)
        {
          gchar *name_base = g_strdup_printf ("%s-%s",
                                              gwidget->construct_internal,
                                              gwidget->internal);

          if (gwidget->project)
            {
              gwidget->name =
                  glade_project_new_widget_name (gwidget->project,
                                                 gwidget, name_base);
              g_free (name_base);
            }
          else
            gwidget->name = name_base;

        }
      else if (gwidget->project)
        gwidget->name = glade_project_new_widget_name
            (gwidget->project, gwidget, gwidget->adaptor->generic_name);
      else
        gwidget->name = g_strdup (gwidget->adaptor->generic_name);
    }

  if (gwidget->construct_template)
    {
      properties = glade_widget_dup_properties
          (gwidget, gwidget->construct_template->properties, FALSE, TRUE,
           gwidget->construct_exact);

      glade_widget_set_properties (gwidget, properties);
    }

  if (gwidget->object == NULL)
    {
      object = glade_widget_build_object (gwidget,
                                          gwidget->construct_template,
                                          gwidget->construct_reason);
    }

  /* Copy sync parentless widget props here after a dup
   */
  if (gwidget->construct_reason == GLADE_CREATE_COPY)
    {
      for (list = gwidget->properties; list; list = list->next)
        {
          GladeProperty *property = list->data;
          if (property->klass->parentless_widget)
            glade_property_sync (property);
        }
    }

  /* Setup width/height */
  gwidget->width = GWA_DEFAULT_WIDTH (gwidget->adaptor);
  gwidget->height = GWA_DEFAULT_HEIGHT (gwidget->adaptor);

  /* Introspect object properties before passing it to post_create,
   * but only when its freshly created (depend on glade file at
   * load time and copying properties at dup time).
   */
  if (gwidget->construct_reason == GLADE_CREATE_USER)
    for (list = gwidget->properties; list; list = list->next)
      glade_property_load (GLADE_PROPERTY (list->data));

  /* We only use catalog defaults when the widget was created by the user! */
  if (gwidget->construct_reason == GLADE_CREATE_USER)
    glade_widget_set_catalog_defaults (gwidget->properties);

  /* Only call this once the GladeWidget is completely built
   * (but before calling custom handlers...)
   */
  glade_widget_adaptor_post_create (gwidget->adaptor,
                                    gwidget->object, gwidget->construct_reason);

  /* Virtual properties need to be explicitly synchronized.
   */
  if (gwidget->construct_reason == GLADE_CREATE_USER)
    glade_widget_sync_custom_props (gwidget);

  if (gwidget->parent && gwidget->packing_properties == NULL)
    glade_widget_set_packing_properties (gwidget, gwidget->parent);

  if (GTK_IS_WIDGET (gwidget->object) &&
      !gtk_widget_is_toplevel (GTK_WIDGET (gwidget->object)))
    {
      gwidget->visible = TRUE;
      gtk_widget_show_all (GTK_WIDGET (gwidget->object));
    }
  else if (GTK_IS_WIDGET (gwidget->object) == FALSE)
    gwidget->visible = TRUE;

  return ret_obj;
807
808
}

809
static void
810
glade_widget_finalize (GObject * object)
811
{
812
  GladeWidget *widget = GLADE_WIDGET (object);
813

814
  g_return_if_fail (GLADE_IS_WIDGET (object));
815

816
#if 0
817
818
  /* A good way to check if refcounts are balancing at project close time */
  g_print ("Finalizing widget %s\n", widget->name);
819
820
#endif

821
822
823
824
  g_free (widget->name);
  g_free (widget->internal);
  g_free (widget->support_warning);
  g_hash_table_destroy (widget->signals);
825

826
827
828
829
  if (widget->props_hash)
    g_hash_table_destroy (widget->props_hash);
  if (widget->pack_props_hash)
    g_hash_table_destroy (widget->pack_props_hash);
830

831
  G_OBJECT_CLASS (glade_widget_parent_class)->finalize (object);
832
833
834
}

static void
835
reset_object_property (GladeProperty * property, GladeProject * project)
836
{
837
838
  if (glade_property_class_is_object (property->klass))
    glade_property_reset (property);
839
840
}

841
static void
842
843
844
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
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
glade_widget_dispose (GObject * object)
{
  GladeWidget *widget = GLADE_WIDGET (object);

  glade_widget_push_superuser ();

  /* Release references by way of object properties... */
  while (widget->prop_refs)
    {
      GladeProperty *property = GLADE_PROPERTY (widget->prop_refs->data);
      glade_property_set (property, NULL);
    }

  if (widget->properties)
    g_list_foreach (widget->properties, (GFunc) reset_object_property,
                    widget->project);

  /* We have to make sure properties release thier references on other widgets first 
   * hence the reset (for object properties) */
  if (widget->properties)
    {
      g_list_foreach (widget->properties, (GFunc) g_object_unref, NULL);
      g_list_free (widget->properties);
      widget->properties = NULL;
    }

  glade_widget_set_object (widget, NULL, TRUE);

  if (widget->packing_properties)
    {
      g_list_foreach (widget->packing_properties, (GFunc) g_object_unref, NULL);
      g_list_free (widget->packing_properties);
      widget->packing_properties = NULL;
    }

  if (widget->actions)
    {
      g_list_foreach (widget->actions, (GFunc) g_object_unref, NULL);
      g_list_free (widget->actions);
      widget->actions = NULL;
    }

  if (widget->packing_actions)
    {
      g_list_foreach (widget->packing_actions, (GFunc) g_object_unref, NULL);
      g_list_free (widget->packing_actions);
      widget->packing_actions = NULL;
    }

  glade_widget_pop_superuser ();

  G_OBJECT_CLASS (glade_widget_parent_class)->dispose (object);
894
895
}

896
static void
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
glade_widget_set_real_property (GObject * object,
                                guint prop_id,
                                const GValue * value, GParamSpec * pspec)
{
  GladeWidget *widget;

  widget = GLADE_WIDGET (object);

  switch (prop_id)
    {
      case PROP_NAME:
        glade_widget_set_name (widget, g_value_get_string (value));
        break;
      case PROP_INTERNAL:
        glade_widget_set_internal (widget, g_value_get_string (value));
        break;
      case PROP_ANARCHIST:
        widget->anarchist = g_value_get_boolean (value);
        break;
      case PROP_OBJECT:
        if (g_value_get_object (value))
          glade_widget_set_object (widget, g_value_get_object (value), TRUE);
        break;
      case PROP_PROJECT:
        glade_widget_set_project (widget,
                                  GLADE_PROJECT (g_value_get_object (value)));
        break;
      case PROP_ADAPTOR:
        glade_widget_set_adaptor (widget, GLADE_WIDGET_ADAPTOR
                                  (g_value_get_object (value)));
        break;
      case PROP_PROPERTIES:
        glade_widget_set_properties (widget,
                                     (GList *) g_value_get_pointer (value));
        break;
      case PROP_PARENT:
        glade_widget_set_parent (widget,
                                 GLADE_WIDGET (g_value_get_object (value)));
        break;
      case PROP_INTERNAL_NAME:
        if (g_value_get_string (value))
          widget->construct_internal = g_value_dup_string (value);
        break;
      case PROP_TEMPLATE:
        widget->construct_template = g_value_get_object (value);
        break;
      case PROP_TEMPLATE_EXACT:
        widget->construct_exact = g_value_get_boolean (value);
        break;
      case PROP_REASON:
        widget->construct_reason = g_value_get_int (value);
        break;
      case PROP_TOPLEVEL_WIDTH:
        widget->width = g_value_get_int (value);
        break;
      case PROP_TOPLEVEL_HEIGHT:
        widget->height = g_value_get_int (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
959
960
961
}

static void
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
glade_widget_get_real_property (GObject * object,
                                guint prop_id,
                                GValue * value, GParamSpec * pspec)
{
  GladeWidget *widget;

  widget = GLADE_WIDGET (object);

  switch (prop_id)
    {
      case PROP_NAME:
        g_value_set_string (value, widget->name);
        break;
      case PROP_INTERNAL:
        g_value_set_string (value, widget->internal);
        break;
      case PROP_ANARCHIST:
        g_value_set_boolean (value, widget->anarchist);
        break;
      case PROP_ADAPTOR:
        g_value_set_object (value, widget->adaptor);
        break;
      case PROP_PROJECT:
        g_value_set_object (value, G_OBJECT (widget->project));
        break;
      case PROP_OBJECT:
        g_value_set_object (value, widget->object);
        break;
      case PROP_PROPERTIES:
        g_value_set_pointer (value, widget->properties);
        break;
      case PROP_PARENT:
        g_value_set_object (value, widget->parent);
        break;
      case PROP_TOPLEVEL_WIDTH:
        g_value_set_int (value, widget->width);
        break;
      case PROP_TOPLEVEL_HEIGHT:
        g_value_set_int (value, widget->height);
        break;
      case PROP_SUPPORT_WARNING:
        g_value_set_string (value, widget->support_warning);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
1009
1010
1011
}

static void
1012
free_signals (gpointer value)
1013
{
1014
1015
1016
  GPtrArray *signals = (GPtrArray *) value;
  guint i;
  guint nb_signals;
1017

1018
1019
  if (signals == NULL)
    return;
1020

1021
1022
1023
1024
1025
  /* g_ptr_array_foreach (signals, (GFunc) glade_signal_free, NULL);
   * only available in modern versions of Gtk+ */
  nb_signals = signals->len;
  for (i = 0; i < nb_signals; i++)
    glade_signal_free (g_ptr_array_index (signals, i));
1026

1027
  g_ptr_array_free (signals, TRUE);
1028
1029
}

1030
static void
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
glade_widget_init (GladeWidget * widget)
{
  widget->adaptor = NULL;
  widget->project = NULL;
  widget->name = NULL;
  widget->internal = NULL;
  widget->object = NULL;
  widget->properties = NULL;
  widget->packing_properties = NULL;
  widget->prop_refs = NULL;
  widget->signals = g_hash_table_new_full
      (g_str_hash, g_str_equal,
       (GDestroyNotify) g_free, (GDestroyNotify) free_signals);

  /* Initial invalid values */
  widget->width = -1;
  widget->height = -1;
1048
1049
}

1050
static void
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
glade_widget_class_init (GladeWidgetClass * klass)
{
  GObjectClass *object_class;

  if (glade_widget_name_quark == 0)
    glade_widget_name_quark = g_quark_from_static_string ("GladeWidgetDataTag");

  object_class = G_OBJECT_CLASS (klass);

  object_class->constructor = glade_widget_constructor;
  object_class->finalize = glade_widget_finalize;
  object_class->dispose = glade_widget_dispose;
  object_class->set_property = glade_widget_set_real_property;
  object_class->get_property = glade_widget_get_real_property;

  klass->add_child = glade_widget_add_child_impl;
  klass->remove_child = glade_widget_remove_child_impl;
  klass->replace_child = glade_widget_replace_child_impl;
  klass->event = glade_widget_event_impl;

  klass->add_signal_handler = glade_widget_add_signal_handler_impl;
  klass->remove_signal_handler = glade_widget_remove_signal_handler_impl;
  klass->change_signal_handler = glade_widget_change_signal_handler_impl;

  klass->button_press_event = glade_widget_button_press_event_impl;
  klass->button_release_event = NULL;
  klass->motion_notify_event = NULL;

  g_object_class_install_property
      (object_class, PROP_NAME,
       g_param_spec_string ("name", _("Name"),
                            _("The name of the widget"),
                            NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property
      (object_class, PROP_INTERNAL,
       g_param_spec_string ("internal", _("Internal name"),
                            _("The internal name of the widget"),
                            NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property
      (object_class, PROP_ANARCHIST,
       g_param_spec_boolean ("anarchist", _("Anarchist"),
                             _("Whether this composite child is "
                               "an ancestral child or an anarchist child"),
                             FALSE, G_PARAM_READWRITE |
                             G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property
      (object_class, PROP_OBJECT,
       g_param_spec_object ("object", _("Object"),
                            _("The object associated"),
                            G_TYPE_OBJECT,
                            G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property
      (object_class, PROP_ADAPTOR,
       g_param_spec_object ("adaptor", _("Adaptor"),
                            _("The class adaptor for the associated widget"),
                            GLADE_TYPE_WIDGET_ADAPTOR,
                            G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property
      (object_class, PROP_PROJECT,
       g_param_spec_object ("project", _("Project"),
                            _("The glade project that "
                              "this widget belongs to"),
                            GLADE_TYPE_PROJECT,
                            G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property
      (object_class, PROP_PROPERTIES,
       g_param_spec_pointer ("properties", _("Properties"),
                             _("A list of GladeProperties"),
                             G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property
      (object_class, PROP_PARENT,
       g_param_spec_object ("parent", _("Parent"),
                            _("A pointer to the parenting GladeWidget"),
                            GLADE_TYPE_WIDGET,
                            G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  g_object_class_install_property
      (object_class, PROP_INTERNAL_NAME,
       g_param_spec_string ("internal-name", _("Internal Name"),
                            _("A generic name prefix for internal widgets"),
                            NULL, G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));

  g_object_class_install_property
      (object_class, PROP_TEMPLATE,
       g_param_spec_object ("template", _("Template"),
                            _("A GladeWidget template to base a new widget on"),
                            GLADE_TYPE_WIDGET,
                            G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));

  g_object_class_install_property
      (object_class, PROP_TEMPLATE_EXACT,
       g_param_spec_boolean ("template-exact", _("Exact Template"),
                             _
                             ("Whether we are creating an exact duplicate when using a template"),
                             FALSE, G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property
      (object_class, PROP_REASON,
       g_param_spec_int ("reason", _("Reason"),
                         _("A GladeCreateReason for this creation"),
                         GLADE_CREATE_USER,
                         GLADE_CREATE_REASONS - 1,
                         GLADE_CREATE_USER,
                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));

  g_object_class_install_property
      (object_class, PROP_TOPLEVEL_WIDTH,
       g_param_spec_int ("toplevel-width", _("Toplevel Width"),
                         _("The width of the widget when toplevel in "
                           "the GladeDesignLayout"),
                         -1, G_MAXINT, -1, G_PARAM_READWRITE));

  g_object_class_install_property
      (object_class, PROP_TOPLEVEL_HEIGHT,
       g_param_spec_int ("toplevel-height", _("Toplevel Height"),
                         _("The height of the widget when toplevel in "
                           "the GladeDesignLayout"),
                         -1, G_MAXINT, -1, G_PARAM_READWRITE));

  g_object_class_install_property
      (object_class, PROP_SUPPORT_WARNING,
       g_param_spec_string ("support warning", _("Support Warning"),
                            _("A warning string about version mismatches"),
                            NULL, G_PARAM_READABLE));

        /**
1184
1185
1186
1187
	 * GladeWidget::add-signal-handler:
	 * @gladewidget: the #GladeWidget which received the signal.
	 * @arg1: the #GladeSignal that was added to @gladewidget.
	 */
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
  glade_widget_signals[ADD_SIGNAL_HANDLER] =
      g_signal_new ("add-signal-handler",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeWidgetClass, add_signal_handler),
                    NULL, NULL,
                    g_cclosure_marshal_VOID__POINTER,
                    G_TYPE_NONE, 1, G_TYPE_POINTER);

        /**
1198
1199
1200
1201
	 * GladeWidget::remove-signal-handler:
	 * @gladewidget: the #GladeWidget which received the signal.
	 * @arg1: the #GladeSignal that was removed from @gladewidget.
	 */
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
  glade_widget_signals[REMOVE_SIGNAL_HANDLER] =
      g_signal_new ("remove-signal-handler",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeWidgetClass, remove_signal_handler),
                    NULL, NULL,
                    g_cclosure_marshal_VOID__POINTER,
                    G_TYPE_NONE, 1, G_TYPE_POINTER);


        /**
1213
1214
1215
1216
1217
	 * GladeWidget::change-signal-handler:
	 * @gladewidget: the #GladeWidget which received the signal.
	 * @arg1: the old #GladeSignal
	 * @arg2: the new #GladeSignal
	 */
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
  glade_widget_signals[CHANGE_SIGNAL_HANDLER] =
      g_signal_new ("change-signal-handler",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeWidgetClass, change_signal_handler),
                    NULL, NULL,
                    glade_marshal_VOID__POINTER_POINTER,
                    G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_POINTER);


        /**
1229
1230
1231
1232
	 * GladeWidget::button-press-event:
	 * @gladewidget: the #GladeWidget which received the signal.
	 * @arg1: the #GdkEvent
	 */
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
  glade_widget_signals[BUTTON_PRESS_EVENT] =
      g_signal_new ("button-press-event",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeWidgetClass, button_press_event),
                    glade_boolean_handled_accumulator, NULL,
                    glade_marshal_BOOLEAN__BOXED,
                    G_TYPE_BOOLEAN, 1,
                    GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);

        /**
1244
1245
1246
1247
	 * GladeWidget::button-relese-event:
	 * @gladewidget: the #GladeWidget which received the signal.
	 * @arg1: the #GdkEvent
	 */
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
  glade_widget_signals[BUTTON_RELEASE_EVENT] =
      g_signal_new ("button-release-event",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeWidgetClass, button_release_event),
                    glade_boolean_handled_accumulator, NULL,
                    glade_marshal_BOOLEAN__BOXED,
                    G_TYPE_BOOLEAN, 1,
                    GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);


        /**
1260
1261
1262
1263
	 * GladeWidget::motion-notify-event:
	 * @gladewidget: the #GladeWidget which received the signal.
	 * @arg1: the #GdkEvent
	 */
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
  glade_widget_signals[MOTION_NOTIFY_EVENT] =
      g_signal_new ("motion-notify-event",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeWidgetClass, motion_notify_event),
                    glade_boolean_handled_accumulator, NULL,
                    glade_marshal_BOOLEAN__BOXED,
                    G_TYPE_BOOLEAN, 1,
                    GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);


        /**
1276
1277
1278
1279
1280
1281
	 * GladeWidget::support-changed:
	 * @gladewidget: the #GladeWidget which received the signal.
	 *
	 * Emitted when property and signal support metadatas and messages
	 * have been updated.
	 */
1282
1283
1284
1285
1286
1287
  glade_widget_signals[SUPPORT_CHANGED] =
      g_signal_new ("support-changed",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    0, NULL, NULL,
                    g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1288
1289


1290
1291
}

1292
1293
1294
/*******************************************************************************
                                Static stuff....
 *******************************************************************************/
1295
static void
1296
1297
1298
glade_widget_copy_packing_props (GladeWidget * parent,
                                 GladeWidget * child,
                                 GladeWidget * template_widget)
1299
{
1300
1301
  GladeProperty *dup_prop, *orig_prop;
  GList *l;
1302

1303
  g_return_if_fail (child->parent == parent);
1304

1305
  glade_widget_set_packing_properties (child, parent);
1306

1307
1308
1309
1310
1311
1312
1313
  for (l = child->packing_properties; l && l->data; l = l->next)
    {
      dup_prop = GLADE_PROPERTY (l->data);
      orig_prop =
          glade_widget_get_pack_property (template_widget, dup_prop->klass->id);
      glade_property_set_value (dup_prop, orig_prop->value);
    }
1314
}
1315

1316
static void
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
glade_widget_set_default_packing_properties (GladeWidget * container,
                                             GladeWidget * child)
{
  GladePropertyClass *property_class;
  GList *l;

  for (l = container->adaptor->packing_props; l; l = l->next)
    {
      const gchar *def;
      GValue *value;

      property_class = l->data;

      if ((def =
           glade_widget_adaptor_get_packing_default
           (child->adaptor, container->adaptor, property_class->id)) == NULL)
        continue;

      value = glade_property_class_make_gvalue_from_string (property_class,
                                                            def,
                                                            child->project,
                                                            child);

      glade_widget_child_set_property (container, child,
                                       property_class->id, value);
      g_value_unset (value);
      g_free (value);
    }
1345
1346
}

1347
/**
1348
 * When looking for an internal child we have to walk up the hierarchy...
1349
 */
1350
static GObject *
1351
glade_widget_get_internal_child (GladeWidget * parent, const gchar * internal)
1352
{
1353
1354
1355
1356
  while (parent)
    {
      GladeWidgetAdaptorClass *adaptor_class =
          GLADE_WIDGET_ADAPTOR_GET_CLASS (parent->adaptor);
1357

1358
1359
1360
      if (adaptor_class->get_internal_child)
        return glade_widget_adaptor_get_internal_child
            (parent->adaptor, parent->object, internal);
1361

1362
1363
1364
      parent = glade_widget_get_parent (parent);
    }
  return NULL;
1365
1366
}

1367
static GladeGetInternalFunc
1368
1369
glade_widget_get_internal_func (GladeWidget * main_target,
                                GladeWidget * parent, GladeWidget ** parent_ret)
1370
{
1371
  GladeWidget *gwidget;
1372

1373
  g_return_val_if_fail (GLADE_IS_WIDGET (parent), NULL);
1374

1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
  for (gwidget = parent; gwidget; gwidget = gwidget->parent)
    {
      GladeWidgetAdaptorClass *adaptor_class =
          GLADE_WIDGET_ADAPTOR_GET_CLASS (gwidget->adaptor);

      if (adaptor_class->get_internal_child)
        {
          if (parent_ret)
            *parent_ret = gwidget;
          return adaptor_class->get_internal_child;
        }

      /* Limit the itterations into where the copy routine stared */
      if (gwidget == main_target)
        break;
    }
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1391

1392
  return NULL;
1393
1394
1395
}


1396
static GladeWidget *
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
glade_widget_dup_internal (GladeWidget * main_target,
                           GladeWidget * parent,
                           GladeWidget * template_widget, gboolean exact)
{
  GladeGetInternalFunc get_internal;
  GladeWidget *gwidget = NULL, *internal_parent;
  GList *children;
  GtkWidget *placeholder;
  gchar *child_type;
  GList *l;

  g_return_val_if_fail (GLADE_IS_WIDGET (template_widget), NULL);
  g_return_val_if_fail (parent == NULL || GLADE_IS_WIDGET (parent), NULL);

  /* Dont actually duplicate internal widgets, but recurse through them anyway. */
  if (parent && template_widget->internal)
    {
      GObject *internal_object = NULL;

      if ((get_internal =
           glade_widget_get_internal_func (main_target, parent,
                                           &internal_parent)) != NULL)
        {
          /* We cant use "parent" here, we have to recurse up the hierarchy to find
           * the "parent" that has `get_internal_child' support (i.e. internal children
           * may have depth).
           */
          if ((internal_object = get_internal (internal_parent->adaptor,
                                               internal_parent->object,
                                               template_widget->internal)) !=
              NULL)
            {
              gwidget = glade_widget_get_from_gobject (internal_object);
              g_assert (gwidget);
            }
        }
    }

  /* If either it was not internal, or we failed to lookup the internal child
   * in the copied hierarchy (this can happen when copying an internal vbox from
   * a composite dialog for instance). */
  if (gwidget == NULL)
    {
      gchar *name = g_strdup (template_widget->name);
      gwidget = glade_widget_adaptor_create_widget
          (template_widget->adaptor, FALSE,
           "name", name,
           "parent", parent,
           "project", template_widget->project,
           "template", template_widget,
           "template-exact", exact, "reason", GLADE_CREATE_COPY, NULL);
      g_free (name);
    }

  /* Copy signals over here regardless of internal or not... */
  if (exact)
    glade_widget_copy_signals (gwidget, template_widget);

  if ((children =
       glade_widget_adaptor_get_children (template_widget->adaptor,
                                          template_widget->object)) != NULL)
    {
      GList *list;

      for (list = children; list && list->data; list = list->next)
        {
          GObject *child = G_OBJECT (list->data);
          GladeWidget *child_gwidget, *child_dup;

          child_type = g_object_get_data (child, "special-child-type");

          if ((child_gwidget = glade_widget_get_from_gobject (child)) == NULL)
            {
              /* Bring the placeholders along ...
               * but not unmarked internal children */
              if (GLADE_IS_PLACEHOLDER (child))
                {
                  placeholder = glade_placeholder_new ();

                  g_object_set_data_full (G_OBJECT (placeholder),
                                          "special-child-type",
                                          g_strdup (child_type), g_free);

                  glade_widget_adaptor_add (gwidget->adaptor,
                                            gwidget->object,
                                            G_OBJECT (placeholder));
                }
            }
          else
            {
              /* Recurse through every GladeWidget (internal or not) */
              child_dup =
                  glade_widget_dup_internal (main_target, gwidget,
                                             child_gwidget, exact);

              if (child_dup->internal == NULL)
                {
                  g_object_set_data_full (child_dup->object,
                                          "special-child-type",
                                          g_strdup (child_type), g_free);

                  glade_widget_add_child (gwidget, child_dup, FALSE);
                }

              /* Internal children that are not heirarchic children
               * need to avoid copying these packing props (like popup windows
               * created on behalf of composite widgets).
               */
              if (glade_widget_adaptor_has_child (gwidget->adaptor,
                                                  gwidget->object,
                                                  child_dup->object))
                glade_widget_copy_packing_props (gwidget,
                                                 child_dup, child_gwidget);

            }
        }
      g_list_free (children