/* Testing clutter_text_get_chars */

#include <clutter/clutter.h>
#include <atk-bridge.h>

#define WIDTH 640
#define HEIGHT 480

ClutterActor *entry = NULL;

static void
on_role_change (GObject    *gobject,
                GParamSpec *pspec,
                gpointer    data)
{
  g_print ("Role is %s\n", atk_role_get_name (atk_object_get_role (ATK_OBJECT (gobject))));
}

static gboolean
button_press_cb (ClutterActor *actor,
                 ClutterButtonEvent *event,
                 gpointer data)
{
  static gboolean active = TRUE;

  if (active)
    clutter_text_set_password_char (CLUTTER_TEXT (entry), '*');
  else
    clutter_text_set_password_char (CLUTTER_TEXT (entry), 0);

  active = !active;

  return TRUE;
}

static ClutterActor*
_create_button (const gchar *text)
{
  ClutterActor *button     = NULL;
  ClutterActor *rectangle  = NULL;
  ClutterActor *label      = NULL;

  button = clutter_actor_new ();
  rectangle = clutter_actor_new ();

  clutter_actor_set_background_color (rectangle, CLUTTER_COLOR_Magenta);
  clutter_actor_set_size (rectangle, 375, 35);

  label = clutter_text_new_full ("Sans Bold 32px",
                                 text,
                                 CLUTTER_COLOR_Black);
  clutter_actor_add_child (button, rectangle);
  clutter_actor_add_child (button, label);
  clutter_actor_set_reactive (button, TRUE);

  return button;
}

int
main (int argc, char **argv)
{
  ClutterActor *stage = NULL;
  ClutterColor  color = { 0x00, 0x00, 0x00, 0xff };
  ClutterColor  color_entry = { 0xff, 0x00, 0x00, 0xff };
  ClutterActor *button = NULL;
  AtkObject    *accessible;

  if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
    return 1;
  atk_bridge_adaptor_init (&argc, &argv);

  stage = clutter_stage_new ();
  g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);

  clutter_actor_set_background_color (stage, &color);
  clutter_actor_set_size (stage, WIDTH, HEIGHT);
  entry = clutter_text_new_full ("Sans Bold 32px", "Default Stage", &color_entry);
  clutter_text_set_editable (CLUTTER_TEXT (entry), TRUE);
  clutter_text_set_selectable (CLUTTER_TEXT (entry), TRUE);
  clutter_text_set_activatable (CLUTTER_TEXT (entry), TRUE);
  clutter_actor_set_reactive (entry, TRUE);

  button = _create_button ("Toggle privacy");
  clutter_actor_set_position (button, 100, 200);
  clutter_actor_add_child (stage, button);
  g_signal_connect_after (button, "button-press-event",
                          G_CALLBACK (button_press_cb), NULL);


  accessible = clutter_actor_get_accessible (entry);
  g_signal_connect (accessible, "notify::accessible-role",
                    G_CALLBACK (on_role_change),
                    NULL);

  clutter_actor_add_child (stage, entry);
  clutter_actor_show (stage);

  clutter_main ();

  return 0;
}
