glade-property-shell.c 18.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright (C) 2013 Tristan Van Berkom.
 *
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Authors:
 *   Tristan Van Berkom <tvb@gnome.org>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib/gi18n-lib.h>

#include "glade.h"
#include "glade-widget.h"
#include "glade-popup.h"
31
#include "glade-editable.h"
32
#include "glade-property-shell.h"
33
#include "glade-marshallers.h"
34 35

/* GObjectClass */
36 37
static void      glade_property_shell_finalize          (GObject       *object);
static void      glade_property_shell_set_real_property (GObject       *object,
38 39 40
                                                         guint          prop_id,
                                                         const GValue  *value,
                                                         GParamSpec    *pspec);
41
static void      glade_property_shell_get_real_property (GObject       *object,
42 43 44
                                                         guint          prop_id,
                                                         GValue        *value,
                                                         GParamSpec    *pspec);
45

46 47
/* GladeEditableIface */
static void      glade_property_shell_editable_init     (GladeEditableIface *iface);
48

49 50 51 52 53
struct _GladePropertyShellPrivate
{
  /* Current State */
  GladeWidgetAdaptor  *adaptor;
  GladeEditorProperty *property_editor;
54 55
  gulong               pre_commit_id;
  gulong               post_commit_id;
56 57

  /* Properties, used to load the internal editor */
58
  GType                editor_type;
59
  gchar               *property_name;
60
  gchar               *custom_text;
61 62
  guint                packing : 1;
  guint                use_command : 1;
63
  guint                disable_check : 1;
64 65 66 67 68 69 70
};

enum {
  PROP_0,
  PROP_PROPERTY_NAME,
  PROP_PACKING,
  PROP_USE_COMMAND,
71
  PROP_EDITOR_TYPE,
72 73
  PROP_CUSTOM_TEXT,
  PROP_DISABLE_CHECK
74 75
};

76 77 78 79 80 81 82 83 84
enum
{
  PRE_COMMIT,
  POST_COMMIT,
  LAST_SIGNAL
};

static guint glade_property_shell_signals[LAST_SIGNAL] = { 0, };

85 86
static GladeEditableIface *parent_editable_iface;

87
G_DEFINE_TYPE_WITH_CODE (GladePropertyShell, glade_property_shell, GTK_TYPE_BOX,
88
                         G_ADD_PRIVATE (GladePropertyShell)
89
                         G_IMPLEMENT_INTERFACE (GLADE_TYPE_EDITABLE,
90
                                                glade_property_shell_editable_init));
91 92 93 94

static void
glade_property_shell_init (GladePropertyShell *shell)
{
95
  shell->priv = glade_property_shell_get_instance_private (shell);
96 97 98

  shell->priv->packing = FALSE;
  shell->priv->use_command = TRUE;
99
  shell->priv->disable_check = FALSE;
100 101 102
}

static void
103
glade_property_shell_class_init (GladePropertyShellClass *klass)
104
{
105
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
106 107 108 109 110 111 112 113
  
  gobject_class->finalize = glade_property_shell_finalize;
  gobject_class->set_property = glade_property_shell_set_real_property;
  gobject_class->get_property = glade_property_shell_get_real_property;

  g_object_class_install_property
      (gobject_class, PROP_PROPERTY_NAME,
       g_param_spec_string ("property-name", _("Property Name"),
114 115
                            _("The property name to use when loading by widget"),
                            NULL, G_PARAM_READWRITE));
116 117 118 119

  g_object_class_install_property
      (gobject_class, PROP_PACKING,
       g_param_spec_boolean ("packing", _("Packing"),
120 121
                             _("Whether the property to load is a packing property or not"),
                             FALSE, G_PARAM_READWRITE));
122 123 124 125

  g_object_class_install_property
      (gobject_class, PROP_USE_COMMAND,
       g_param_spec_boolean ("use-command", _("Use Command"),
126 127
                             _("Whether to use the GladeCommand API when modifying properties"),
                             TRUE, G_PARAM_READWRITE));
128

129 130 131
  g_object_class_install_property
      (gobject_class, PROP_EDITOR_TYPE,
       g_param_spec_string ("editor-type", _("Editor Property Type Name"),
132 133
                            _("Specify the actual editor property type name to use for this shell"),
                            NULL, G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
134

135 136 137
  g_object_class_install_property
      (gobject_class, PROP_CUSTOM_TEXT,
       g_param_spec_string ("custom-text", _("Custom Text"),
138 139
                            _("Custom Text to display in the property label"),
                            NULL, G_PARAM_READWRITE));
140 141 142 143
  
  g_object_class_install_property
      (gobject_class, PROP_DISABLE_CHECK,
       g_param_spec_boolean ("disable-check", _("Disable Check"),
144 145
                             _("Whether to explicitly disable the check button"),
                             FALSE, G_PARAM_READWRITE));
146
  
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
  /**
   * GladePropertyShell::pre-commit:
   * @gladeeditorproperty: the #GladeEditorProperty which changed value
   * @arg1: the new #GValue to commit.
   *
   * Emitted before a property's value is committed, can be useful to serialize
   * commands before a property's commit command from custom editors.
   */
  glade_property_shell_signals[PRE_COMMIT] =
      g_signal_new ("pre-commit",
                    G_TYPE_FROM_CLASS (gobject_class),
                    G_SIGNAL_RUN_LAST,
                    0, NULL, NULL,
                    _glade_marshal_VOID__POINTER,
                    G_TYPE_NONE, 1, G_TYPE_POINTER);

  /**
   * GladePropertyShell::post-commit:
   * @gladeeditorproperty: the #GladeEditorProperty which changed value
   * @arg1: the new #GValue to commit.
   *
   * Emitted after a property's value is committed, can be useful to serialize
   * commands after a property's commit command from custom editors.
   */
  glade_property_shell_signals[POST_COMMIT] =
      g_signal_new ("post-commit",
                    G_TYPE_FROM_CLASS (gobject_class),
                    G_SIGNAL_RUN_LAST,
                    0, NULL, NULL,
                    _glade_marshal_VOID__POINTER,
                    G_TYPE_NONE, 1, G_TYPE_POINTER);
178 179 180 181 182 183 184 185 186 187 188 189
}


/***********************************************************
 *                     GObjectClass                        *
 ***********************************************************/
static void
glade_property_shell_finalize (GObject *object)
{
  GladePropertyShell *shell = GLADE_PROPERTY_SHELL (object);

  g_free (shell->priv->property_name);
190
  g_free (shell->priv->custom_text);
191 192 193 194 195

  G_OBJECT_CLASS (glade_property_shell_parent_class)->finalize (object);
}

static void
196
glade_property_shell_set_real_property (GObject      *object,
197 198 199
                                        guint         prop_id,
                                        const GValue *value,
                                        GParamSpec   *pspec)
200 201
{
  GladePropertyShell *shell = GLADE_PROPERTY_SHELL (object);
202 203 204
  GladePropertyShellPrivate *priv = shell->priv;
  const gchar *type_name = NULL;
  GType type = 0;
205 206 207 208 209 210 211 212 213 214 215

  switch (prop_id)
    {
    case PROP_PROPERTY_NAME:
      glade_property_shell_set_property_name (shell, g_value_get_string (value));
      break;
    case PROP_PACKING:
      glade_property_shell_set_packing (shell, g_value_get_boolean (value));
      break;
    case PROP_USE_COMMAND:
      glade_property_shell_set_use_command (shell, g_value_get_boolean (value));
216 217 218 219 220
      break;
    case PROP_EDITOR_TYPE:
      type_name = g_value_get_string (value);

      if (type_name)
221
        type = glade_util_get_type_from_name (type_name, FALSE);
222 223

      if (type > 0 && !g_type_is_a (type, GLADE_TYPE_EDITOR_PROPERTY))
224
        g_warning ("Editor type '%s' is not a GladeEditorProperty", type_name);
225
      else
226
        priv->editor_type = type;
227

228 229 230
      break;
    case PROP_CUSTOM_TEXT:
      glade_property_shell_set_custom_text (shell, g_value_get_string (value));
231
      break;
232 233 234
    case PROP_DISABLE_CHECK:
      glade_property_shell_set_disable_check (shell, g_value_get_boolean (value));
      break;
235 236 237 238 239 240 241
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
242
glade_property_shell_get_real_property (GObject    *object,
243 244 245
                                        guint       prop_id,
                                        GValue     *value,
                                        GParamSpec *pspec)
246 247 248 249 250 251 252 253 254 255 256 257 258 259
{
  GladePropertyShell *shell = GLADE_PROPERTY_SHELL (object);

  switch (prop_id)
    {
    case PROP_PROPERTY_NAME:
      g_value_set_string (value, glade_property_shell_get_property_name (shell));
      break;
    case PROP_PACKING:
      g_value_set_boolean (value, glade_property_shell_get_packing (shell));
      break;
    case PROP_USE_COMMAND:
      g_value_set_boolean (value, glade_property_shell_get_use_command (shell));
      break;
260 261 262
    case PROP_CUSTOM_TEXT:
      g_value_set_string (value, glade_property_shell_get_custom_text (shell));
      break;
263 264 265
    case PROP_DISABLE_CHECK:
      g_value_set_boolean (value, glade_property_shell_get_disable_check (shell));
      break;
266 267 268 269 270 271
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

272
/*******************************************************************************
273
 *                            GladeEditableIface                               *                               
274
 *******************************************************************************/
275 276
static void
propagate_pre_commit (GladeEditorProperty *property,
277 278
                      GValue              *value,
                      GladePropertyShell  *shell)
279 280 281 282 283 284
{
  g_signal_emit (G_OBJECT (shell), glade_property_shell_signals[PRE_COMMIT], 0, value);
}

static void
propagate_post_commit (GladeEditorProperty *property,
285 286
                       GValue              *value,
                       GladePropertyShell  *shell)
287 288 289 290 291 292
{
  g_signal_emit (G_OBJECT (shell), glade_property_shell_signals[POST_COMMIT], 0, value);
}

static void
glade_property_shell_set_eprop (GladePropertyShell  *shell,
293
                                GladeEditorProperty *eprop)
294 295 296 297 298 299
{
  GladePropertyShellPrivate *priv = shell->priv;

  if (priv->property_editor != eprop)
    {
      if (priv->property_editor)
300 301 302 303 304
        {
          g_signal_handler_disconnect (priv->property_editor, priv->pre_commit_id);
          g_signal_handler_disconnect (priv->property_editor, priv->post_commit_id);
          priv->pre_commit_id = 0;
          priv->post_commit_id = 0;
305

306 307
          gtk_widget_destroy (GTK_WIDGET (priv->property_editor));
        }
308 309 310 311

      priv->property_editor = eprop;

      if (priv->property_editor)
312 313
        {
          glade_editor_property_set_custom_text (priv->property_editor, priv->custom_text);
314 315
          glade_editor_property_set_disable_check (priv->property_editor, priv->disable_check);
            
316 317 318 319
          priv->pre_commit_id = g_signal_connect (priv->property_editor, "commit",
                                                  G_CALLBACK (propagate_pre_commit), shell);
          priv->post_commit_id = g_signal_connect_after (priv->property_editor, "commit",
                                                         G_CALLBACK (propagate_post_commit), shell);
320

321 322
          gtk_container_add (GTK_CONTAINER (shell), GTK_WIDGET (priv->property_editor));
        }
323 324 325
    }
}

326
static void
327
glade_property_shell_load (GladeEditable *editable,
328
                           GladeWidget   *widget)
329
{
330
  GladePropertyShell *shell = GLADE_PROPERTY_SHELL (editable);
331 332
  GladePropertyShellPrivate *priv;

333 334 335
  /* Chain up to default implementation */
  parent_editable_iface->load (editable, widget);

336 337 338 339 340 341
  g_return_if_fail (shell->priv->property_name != NULL);

  priv = shell->priv;

  if (widget)
    {
342 343 344 345
      GladeWidgetAdaptor *adaptor = NULL;

      /* Use the parent adaptor if we're a packing property */
      if (priv->packing)
346 347
        {
          GladeWidget *parent = glade_widget_get_parent (widget);
348

349 350 351
          if (parent)
            adaptor = glade_widget_get_adaptor (parent);
        }
352
      else
353
        adaptor = glade_widget_get_adaptor (widget);
354 355 356

      /* Need to rebuild the internal editor */
      if (priv->adaptor != adaptor)
357 358 359 360 361 362 363 364 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 393 394 395 396 397 398 399
        {
          GladePropertyClass *pclass = NULL;
          GladeEditorProperty *eprop = NULL;

          priv->adaptor = adaptor;

          if (adaptor)
            {
              if (priv->packing)
                pclass = glade_widget_adaptor_get_pack_property_class (priv->adaptor,
                                                                       priv->property_name);
              else
                pclass = glade_widget_adaptor_get_property_class (priv->adaptor,
                                                                  priv->property_name);
            }

          /* Be forgiving, allow usage of properties that wont work, so that
           * some editors can include properties for subclasses, and hide
           * those properties if they're not applicable
           */
          if (pclass == NULL)
            {
              priv->property_editor = NULL;
            }
          /* Construct custom editor property if specified */
          else if (g_type_is_a (priv->editor_type, GLADE_TYPE_EDITOR_PROPERTY))
            {
              eprop = g_object_new (priv->editor_type,
                                    "property-class", pclass,
                                    "use-command", priv->use_command,
                                    NULL);
            }
          else
            {
              /* Let the adaptor create one */
              eprop = glade_widget_adaptor_create_eprop_by_name (priv->adaptor,
                                                                 priv->property_name,
                                                                 priv->packing,
                                                                 priv->use_command);
            }

          glade_property_shell_set_eprop (shell, eprop);
        }
400 401 402

      /* If we have an editor for the right adaptor, load it */
      if (priv->property_editor)
403
        glade_editable_load (GLADE_EDITABLE (priv->property_editor), widget);
404 405
    }
  else if (priv->property_editor)
406
    glade_editable_load (GLADE_EDITABLE (priv->property_editor), NULL);
407 408 409
}

static void
410
glade_property_shell_set_show_name (GladeEditable *editable, gboolean show_name)
411
{
412 413 414
}

static void
415
glade_property_shell_editable_init (GladeEditableIface *iface)
416 417 418 419 420
{
  parent_editable_iface = g_type_default_interface_peek (GLADE_TYPE_EDITABLE);

  iface->load = glade_property_shell_load;
  iface->set_show_name = glade_property_shell_set_show_name;
421 422 423 424 425 426 427 428 429
}

/***********************************************************
 *                            API                          *
 ***********************************************************/
GtkWidget *
glade_property_shell_new (void)
{
  return g_object_new (GLADE_TYPE_PROPERTY_SHELL, NULL);
430 431 432 433
}

void
glade_property_shell_set_property_name (GladePropertyShell *shell,
434
                                        const gchar        *property_name)
435 436 437 438 439 440 441
{
  GladePropertyShellPrivate *priv;

  g_return_if_fail (GLADE_IS_PROPERTY_SHELL (shell));

  priv = shell->priv;

442
  if (g_strcmp0 (priv->property_name, property_name) != 0)
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    {
      g_free (priv->property_name);
      priv->property_name = g_strdup (property_name);

      g_object_notify (G_OBJECT (shell), "property-name");
    }
}

const gchar *
glade_property_shell_get_property_name (GladePropertyShell *shell)
{
  g_return_val_if_fail (GLADE_IS_PROPERTY_SHELL (shell), NULL);

  return shell->priv->property_name;
}

459 460
void
glade_property_shell_set_custom_text (GladePropertyShell *shell,
461
                                      const gchar        *custom_text)
462 463 464 465 466 467 468 469 470 471 472 473 474
{
  GladePropertyShellPrivate *priv;

  g_return_if_fail (GLADE_IS_PROPERTY_SHELL (shell));

  priv = shell->priv;

  if (g_strcmp0 (priv->custom_text, custom_text) != 0)
    {
      g_free (priv->custom_text);
      priv->custom_text = g_strdup (custom_text);

      if (priv->property_editor)
475
        glade_editor_property_set_custom_text (priv->property_editor, custom_text);
476 477 478 479 480 481 482 483 484 485 486 487 488

      g_object_notify (G_OBJECT (shell), "custom-text");
    }
}

const gchar *
glade_property_shell_get_custom_text (GladePropertyShell *shell)
{
  g_return_val_if_fail (GLADE_IS_PROPERTY_SHELL (shell), NULL);

  return shell->priv->custom_text;
}

489 490
void
glade_property_shell_set_packing (GladePropertyShell *shell,
491
                                  gboolean            packing)
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
{
  GladePropertyShellPrivate *priv;

  g_return_if_fail (GLADE_IS_PROPERTY_SHELL (shell));

  priv = shell->priv;

  if (priv->packing != packing)
    {
      priv->packing = packing;

      g_object_notify (G_OBJECT (shell), "packing");
    }
}

gboolean
glade_property_shell_get_packing (GladePropertyShell *shell)
{
  g_return_val_if_fail (GLADE_IS_PROPERTY_SHELL (shell), FALSE);

  return shell->priv->packing;
}

void
glade_property_shell_set_use_command (GladePropertyShell *shell,
517
                                      gboolean            use_command)
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
{
  GladePropertyShellPrivate *priv;

  g_return_if_fail (GLADE_IS_PROPERTY_SHELL (shell));

  priv = shell->priv;

  if (priv->use_command != use_command)
    {
      priv->use_command = use_command;

      g_object_notify (G_OBJECT (shell), "use-command");
    }
}

gboolean
glade_property_shell_get_use_command (GladePropertyShell *shell)
{
  g_return_val_if_fail (GLADE_IS_PROPERTY_SHELL (shell), FALSE);

  return shell->priv->use_command;
}
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

void
glade_property_shell_set_disable_check (GladePropertyShell *shell,
                                        gboolean            disable_check)
{
  GladePropertyShellPrivate *priv;

  g_return_if_fail (GLADE_IS_PROPERTY_SHELL (shell));

  priv = shell->priv;

  if (priv->disable_check != disable_check)
    {
      priv->disable_check = disable_check;

      if (priv->property_editor)
556
        g_object_set (priv->property_editor, "disable-check", disable_check, NULL);
557 558 559 560 561 562 563 564 565 566 567 568

      g_object_notify (G_OBJECT (shell), "disable-check");
    }
}

gboolean
glade_property_shell_get_disable_check (GladePropertyShell *shell)
{
  g_return_val_if_fail (GLADE_IS_PROPERTY_SHELL (shell), FALSE);

  return shell->priv->disable_check;
}