
#include "my-demo.h"

/* Include Glade generated interface, this file was quoted with a bash script like this:
 *
 *     IFS=""; while read line; do echo -n \"; echo -n $line | sed -e 's|\"|\\"|g'; echo \"; done < my-demo.glade
 *
 * The header file contains the definition of the 'demo_builder_ui' string constant.
 *
 */
#include "my-demo-ui.h"

/* GObjectClass prototypes... */
static void my_demo_constructed   (GObject              *object);
static void my_demo_get_property  (GObject              *object,
				   guint                 prop_id, 
				   GValue               *value,
				   GParamSpec           *pspec);
static void my_demo_set_property  (GObject              *object,
				   guint                 prop_id, 
				   const GValue         *value,
				   GParamSpec           *pspec);

enum {
  PROP_0,
  PROP_MESSAGE,
};


struct _MyDemoPrivate
{
  GtkWidget *label;
  GtkWidget *message;
};

G_DEFINE_TYPE (MyDemo, my_demo, GTK_TYPE_EVENT_BOX)

static void
my_demo_class_init (MyDemoClass * klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed  = my_demo_constructed;
  object_class->get_property = my_demo_get_property;
  object_class->set_property = my_demo_set_property;


  g_object_class_install_property (object_class, PROP_MESSAGE,
				   g_param_spec_string ("message", "Message",
							"The Message to show in the demo widget",
							NULL, G_PARAM_READABLE | G_PARAM_WRITABLE));

  g_type_class_add_private (object_class, sizeof (MyDemoPrivate));
}

static void
my_demo_init (MyDemo * demo)
{
  demo->priv = G_TYPE_INSTANCE_GET_PRIVATE ((demo), MY_TYPE_DEMO, MyDemoPrivate);
}


/* Here is where the magic of encapsulating a glade file
 * into an object takes place.
 *
 * Here in the ->constructed() method, we simply build our Glade file
 * static string with GtkBuilder and add the created widgetry to this
 * widget automatically.
 */
static void
my_demo_constructed (GObject *object)
{
  MyDemo        *demo = MY_DEMO (object);
  MyDemoPrivate *priv = demo->priv;
  GtkBuilder    *builder;
  GtkWidget     *contents;

  /* Chain up... */
  G_OBJECT_CLASS (my_demo_parent_class)->constructed (object);

  /* Build UI */
  builder = gtk_builder_new ();
  gtk_builder_add_from_string (builder, demo_builder_ui, -1, NULL);

  /* Fetch pointers */
  priv->label   = (GtkWidget *)gtk_builder_get_object (builder, "indicator-label");
  priv->message = (GtkWidget *)gtk_builder_get_object (builder, "message-label");

  /* Add contents to self */
  contents = (GtkWidget *)gtk_builder_get_object (builder, "contents");
  gtk_container_add (GTK_CONTAINER (demo), contents);

  /* Connect signals, pass the 'self' instance to all callbacks */
  gtk_builder_connect_signals (builder, demo);

  /* Finished */
  g_object_unref (builder);
}

static void
my_demo_get_property  (GObject              *object,
		       guint                 prop_id, 
		       GValue               *value,
		       GParamSpec           *pspec)
{
  MyDemo        *demo = MY_DEMO (object);
  MyDemoPrivate *priv = demo->priv;

  switch (prop_id)
    {
    case PROP_MESSAGE:
      g_value_set_string (value, gtk_label_get_text (GTK_LABEL (priv->message)));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
my_demo_set_property  (GObject              *object,
		       guint                 prop_id, 
		       const GValue         *value,
		       GParamSpec           *pspec)
{
  MyDemo        *demo = MY_DEMO (object);
  MyDemoPrivate *priv = demo->priv;

  switch (prop_id)
    {
    case PROP_MESSAGE:
      gtk_label_set_text (GTK_LABEL (priv->message), g_value_get_string (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

/****************************************************
 *                     Callbacks                    *
 ****************************************************/
void
my_demo_button_clicked (GtkToggleButton *button,
			MyDemo          *demo)
{
  MyDemoPrivate *priv = demo->priv;

  if (gtk_toggle_button_get_active (button))
    gtk_label_set_text (GTK_LABEL (priv->label), "pressed");
  else
    gtk_label_set_text (GTK_LABEL (priv->label), "not pressed");
}

