glade-property-shell.c 8.98 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-property-editor.h"
32 33 34 35 36 37 38 39 40 41 42 43 44
#include "glade-property-shell.h"

/* GObjectClass */
static void      glade_property_shell_finalize          (GObject         *object);
static void      glade_property_shell_set_real_property (GObject         *object,
							 guint            prop_id,
							 const GValue    *value,
							 GParamSpec      *pspec);
static void      glade_property_shell_get_real_property (GObject         *object,
							 guint            prop_id,
							 GValue          *value,
							 GParamSpec      *pspec);

45 46 47
/* GladePropertyEditorInterface */
static void      glade_property_shell_property_editor_init (GladePropertyEditorInterface *iface);

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
struct _GladePropertyShellPrivate
{
  /* Current State */
  GladeWidgetAdaptor  *adaptor;
  GladeEditorProperty *property_editor;

  /* Properties, used to load the internal editor */
  gchar               *property_name;
  guint                packing : 1;
  guint                use_command : 1;
};

enum {
  PROP_0,
  PROP_PROPERTY_NAME,
  PROP_PACKING,
  PROP_USE_COMMAND,
};

67 68 69
G_DEFINE_TYPE_WITH_CODE (GladePropertyShell, glade_property_shell, GTK_TYPE_BOX,
			 G_IMPLEMENT_INTERFACE (GLADE_TYPE_PROPERTY_EDITOR,
                                                glade_property_shell_property_editor_init));
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

static void
glade_property_shell_init (GladePropertyShell *shell)
{
  shell->priv = 
    G_TYPE_INSTANCE_GET_PRIVATE (shell,
				 GLADE_TYPE_PROPERTY_SHELL,
				 GladePropertyShellPrivate);

  shell->priv->packing = FALSE;
  shell->priv->use_command = TRUE;
}

static void
glade_property_shell_class_init (GladePropertyShellClass *class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
  
  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"),
			    _("The property name to use when loading by widget"),
			    NULL, G_PARAM_READWRITE));

  g_object_class_install_property
      (gobject_class, PROP_PACKING,
       g_param_spec_boolean ("packing", _("Packing"),
			     _("Whether the property to load is a packing property or not"),
			     FALSE, G_PARAM_READWRITE));

  g_object_class_install_property
      (gobject_class, PROP_USE_COMMAND,
       g_param_spec_boolean ("use-command", _("Use Command"),
			     _("Whether to use the GladeCommand API when modifying properties"),
108
			     TRUE, G_PARAM_READWRITE));
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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

  g_type_class_add_private (gobject_class, sizeof (GladePropertyShellPrivate));
}


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

  g_free (shell->priv->property_name);

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

static void
glade_property_shell_set_real_property (GObject         *object,
					guint            prop_id,
					const GValue    *value,
					GParamSpec      *pspec)
{
  GladePropertyShell *shell = GLADE_PROPERTY_SHELL (object);

  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));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
glade_property_shell_get_real_property (GObject         *object,
					guint            prop_id,
					GValue          *value,
					GParamSpec      *pspec)
{
  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;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

177 178 179 180 181 182
/*******************************************************************************
 *                         GladePropertyEditorInterface                        *                               
 *******************************************************************************/
static void
glade_property_shell_property_editor_load (GladePropertyEditor  *editor,
					   GladeWidget          *widget)
183
{
184
  GladePropertyShell *shell = GLADE_PROPERTY_SHELL (editor);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  GladePropertyShellPrivate *priv;

  g_return_if_fail (shell->priv->property_name != NULL);

  priv = shell->priv;

  if (widget)
    {
      GladeWidgetAdaptor *adaptor = glade_widget_get_adaptor (widget);

      /* Need to rebuild the internal editor */
      if (priv->adaptor != adaptor)
	{
	  if (priv->property_editor)
	    gtk_widget_destroy (GTK_WIDGET (priv->property_editor));

	  priv->adaptor = adaptor;

	  priv->property_editor = 
	    glade_widget_adaptor_create_eprop_by_name (priv->adaptor,
						       priv->property_name,
						       priv->packing,
						       priv->use_command);

	  if (priv->property_editor)
	    gtk_container_add (GTK_CONTAINER (shell), GTK_WIDGET (priv->property_editor));
	}

      /* If we have an editor for the right adaptor, load it */
      if (priv->property_editor)
215
	glade_property_editor_load (GLADE_PROPERTY_EDITOR (priv->property_editor), widget);
216 217
    }
  else if (priv->property_editor)
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    glade_property_editor_load (GLADE_PROPERTY_EDITOR (priv->property_editor), NULL);
}

static void
glade_property_shell_property_editor_init (GladePropertyEditorInterface *iface)
{
  iface->load = glade_property_shell_property_editor_load;
}

/***********************************************************
 *                            API                          *
 ***********************************************************/
GtkWidget *
glade_property_shell_new (void)
{
  return g_object_new (GLADE_TYPE_PROPERTY_SHELL, NULL);
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 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 313
}

void
glade_property_shell_set_property_name (GladePropertyShell *shell,
					const gchar        *property_name)
{
  GladePropertyShellPrivate *priv;

  g_return_if_fail (GLADE_IS_PROPERTY_SHELL (shell));

  priv = shell->priv;

  if (g_strcmp0 (priv->property_name, property_name))
    {
      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;
}

void
glade_property_shell_set_packing (GladePropertyShell *shell,
				  gboolean            packing)
{
  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,
				      gboolean            use_command)
{
  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;
}