Blame examples/box-layout.c

Packit 31ecd5
/*
Packit 31ecd5
 * Copyright 2009 Intel Corporation.
Packit 31ecd5
 *
Packit 31ecd5
 * This program is free software; you can redistribute it and/or modify it
Packit 31ecd5
 * under the terms and conditions of the GNU Lesser General Public License,
Packit 31ecd5
 * version 2.1, as published by the Free Software Foundation.
Packit 31ecd5
 *
Packit 31ecd5
 * This program is distributed in the hope it will be useful, but WITHOUT ANY
Packit 31ecd5
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
Packit 31ecd5
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
Packit 31ecd5
 * more details.
Packit 31ecd5
 *
Packit 31ecd5
 * You should have received a copy of the GNU Lesser General Public License
Packit 31ecd5
 * along with this program; if not, write to the Free Software Foundation,
Packit 31ecd5
 * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
Packit 31ecd5
 * Boston, MA 02111-1307, USA.
Packit 31ecd5
 *
Packit 31ecd5
 */
Packit 31ecd5
#include <stdlib.h>
Packit 31ecd5
#include <clutter/clutter.h>
Packit 31ecd5
Packit 31ecd5
#define INSTRUCTIONS \
Packit 31ecd5
        "Press v\t\342\236\236\tSwitch horizontal/vertical\n"           \
Packit 31ecd5
        "Press h\t\342\236\236\tToggle homogeneous\n"			\
Packit 31ecd5
        "Press p\t\342\236\236\tToggle pack start/end\n"                \
Packit 31ecd5
        "Press s\t\342\236\236\tIncrement spacing (up to 12px)\n"       \
Packit 31ecd5
        "Press +\t\342\236\236\tAdd a new actor\n"                      \
Packit 31ecd5
        "Press a\t\342\236\236\tToggle animations\n"                    \
Packit 31ecd5
        "Press q\t\342\236\236\tQuit"
Packit 31ecd5
Packit 31ecd5
Packit 31ecd5
static const gchar *
Packit 31ecd5
get_align_name (ClutterActorAlign align)
Packit 31ecd5
{
Packit 31ecd5
  switch (align)
Packit 31ecd5
    {
Packit 31ecd5
    case CLUTTER_ACTOR_ALIGN_FILL:
Packit 31ecd5
      return "fill";
Packit 31ecd5
Packit 31ecd5
    case CLUTTER_ACTOR_ALIGN_START:
Packit 31ecd5
      return "start";
Packit 31ecd5
Packit 31ecd5
    case CLUTTER_ACTOR_ALIGN_CENTER:
Packit 31ecd5
      return "center";
Packit 31ecd5
Packit 31ecd5
    case CLUTTER_ACTOR_ALIGN_END:
Packit 31ecd5
      return "end";
Packit 31ecd5
Packit 31ecd5
    default:
Packit 31ecd5
      g_assert_not_reached ();
Packit 31ecd5
    }
Packit 31ecd5
}
Packit 31ecd5
Packit 31ecd5
static gboolean
Packit 31ecd5
button_release_cb (ClutterActor *rect,
Packit 31ecd5
                   ClutterEvent *event,
Packit 31ecd5
                   gpointer      user_data)
Packit 31ecd5
{
Packit 31ecd5
  ClutterActorAlign x_align, y_align;
Packit 31ecd5
  gboolean x_expand, y_expand;
Packit 31ecd5
Packit 31ecd5
  g_object_get (rect,
Packit 31ecd5
                "x-align", &x_align,
Packit 31ecd5
                "y-align", &y_align,
Packit 31ecd5
                "x-expand", &x_expand,
Packit 31ecd5
                "y-expand", &y_expand,
Packit 31ecd5
                NULL);
Packit 31ecd5
Packit 31ecd5
  switch (clutter_event_get_button (event))
Packit 31ecd5
    {
Packit 31ecd5
    case CLUTTER_BUTTON_PRIMARY:
Packit 31ecd5
      if (clutter_event_has_shift_modifier (event))
Packit 31ecd5
        {
Packit 31ecd5
          if (y_align < 3)
Packit 31ecd5
            y_align += 1;
Packit 31ecd5
          else
Packit 31ecd5
            y_align = 0;
Packit 31ecd5
          break;
Packit 31ecd5
        }
Packit 31ecd5
      else
Packit 31ecd5
        {
Packit 31ecd5
          if (x_align < 3)
Packit 31ecd5
            x_align += 1;
Packit 31ecd5
          else
Packit 31ecd5
            x_align = 0;
Packit 31ecd5
        }
Packit 31ecd5
      break;
Packit 31ecd5
Packit 31ecd5
    case CLUTTER_BUTTON_SECONDARY:
Packit 31ecd5
      if (clutter_event_has_shift_modifier (event))
Packit 31ecd5
        y_expand = !y_expand;
Packit 31ecd5
      else
Packit 31ecd5
        x_expand = !x_expand;
Packit 31ecd5
      break;
Packit 31ecd5
Packit 31ecd5
    default:
Packit 31ecd5
      break;
Packit 31ecd5
    }
Packit 31ecd5
Packit 31ecd5
  g_object_set (rect,
Packit 31ecd5
                "x-align", x_align,
Packit 31ecd5
                "y-align", y_align,
Packit 31ecd5
                "x-expand", x_expand,
Packit 31ecd5
                "y-expand", y_expand,
Packit 31ecd5
                NULL);
Packit 31ecd5
  return TRUE;
Packit 31ecd5
}
Packit 31ecd5
Packit 31ecd5
static void
Packit 31ecd5
changed_cb (ClutterActor *actor,
Packit 31ecd5
            GParamSpec   *pspec,
Packit 31ecd5
            ClutterActor *text)
Packit 31ecd5
{
Packit 31ecd5
  ClutterActorAlign x_align, y_align;
Packit 31ecd5
  gboolean x_expand, y_expand;
Packit 31ecd5
  gchar *label;
Packit 31ecd5
Packit 31ecd5
  g_object_get (actor,
Packit 31ecd5
                "x-align", &x_align,
Packit 31ecd5
                "y-align", &y_align,
Packit 31ecd5
                "x-expand", &x_expand,
Packit 31ecd5
                "y-expand", &y_expand,
Packit 31ecd5
                NULL);
Packit 31ecd5
Packit 31ecd5
  label = g_strdup_printf ("%d,%d\n"
Packit 31ecd5
                           "%s\n%s",
Packit 31ecd5
                           x_expand, y_expand,
Packit 31ecd5
                           get_align_name (x_align),
Packit 31ecd5
                           get_align_name (y_align));
Packit 31ecd5
  clutter_text_set_text (CLUTTER_TEXT (text), label);
Packit 31ecd5
  g_free (label);
Packit 31ecd5
}
Packit 31ecd5
Packit 31ecd5
static void
Packit 31ecd5
add_actor (ClutterActor *box,
Packit 31ecd5
           gint          position)
Packit 31ecd5
{
Packit 31ecd5
  ClutterActor *rect, *text;
Packit 31ecd5
  ClutterColor color;
Packit 31ecd5
  ClutterLayoutManager *layout;
Packit 31ecd5
Packit 31ecd5
  clutter_color_from_hls (&color,
Packit 31ecd5
                          g_random_double_range (0.0, 360.0),
Packit 31ecd5
                          0.5,
Packit 31ecd5
                          0.5);
Packit 31ecd5
  color.alpha = 255;
Packit 31ecd5
Packit 31ecd5
  layout = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
Packit 31ecd5
                                   CLUTTER_BIN_ALIGNMENT_CENTER);
Packit 31ecd5
  rect = clutter_actor_new ();
Packit 31ecd5
  clutter_actor_set_layout_manager (rect, layout);
Packit 31ecd5
  clutter_actor_set_background_color (rect, &color;;
Packit 31ecd5
  clutter_actor_set_reactive (rect, TRUE);
Packit 31ecd5
  clutter_actor_set_size (rect, 32, 64);
Packit 31ecd5
  clutter_actor_set_x_expand (rect, TRUE);
Packit 31ecd5
  clutter_actor_set_y_expand (rect, TRUE);
Packit 31ecd5
  clutter_actor_set_x_align (rect, CLUTTER_ACTOR_ALIGN_CENTER);
Packit 31ecd5
  clutter_actor_set_y_align (rect, CLUTTER_ACTOR_ALIGN_CENTER);
Packit 31ecd5
Packit 31ecd5
  text = clutter_text_new_with_text ("Sans 8px", NULL);
Packit 31ecd5
  clutter_text_set_line_alignment (CLUTTER_TEXT (text),
Packit 31ecd5
                                   PANGO_ALIGN_CENTER);
Packit 31ecd5
  clutter_actor_add_child (rect, text);
Packit 31ecd5
Packit 31ecd5
  g_signal_connect (rect, "button-release-event",
Packit 31ecd5
                    G_CALLBACK (button_release_cb), NULL);
Packit 31ecd5
  g_signal_connect (rect, "notify::x-expand",
Packit 31ecd5
                    G_CALLBACK (changed_cb), text);
Packit 31ecd5
  g_signal_connect (rect, "notify::y-expand",
Packit 31ecd5
                    G_CALLBACK (changed_cb), text);
Packit 31ecd5
  g_signal_connect (rect, "notify::x-align",
Packit 31ecd5
                    G_CALLBACK (changed_cb), text);
Packit 31ecd5
  g_signal_connect (rect, "notify::y-align",
Packit 31ecd5
                    G_CALLBACK (changed_cb), text);
Packit 31ecd5
  changed_cb (rect, NULL, text);
Packit 31ecd5
Packit 31ecd5
  clutter_actor_insert_child_at_index (box, rect, position);
Packit 31ecd5
}
Packit 31ecd5
Packit 31ecd5
static gboolean
Packit 31ecd5
key_release_cb (ClutterActor *stage,
Packit 31ecd5
                ClutterEvent *event,
Packit 31ecd5
                ClutterActor *box)
Packit 31ecd5
{
Packit 31ecd5
  ClutterBoxLayout *layout;
Packit 31ecd5
  gboolean toggle;
Packit 31ecd5
  guint spacing;
Packit 31ecd5
Packit 31ecd5
  layout = CLUTTER_BOX_LAYOUT (clutter_actor_get_layout_manager (box));
Packit 31ecd5
Packit 31ecd5
  switch (clutter_event_get_key_symbol (event))
Packit 31ecd5
    {
Packit 31ecd5
    case CLUTTER_KEY_a:
Packit 31ecd5
      {
Packit 31ecd5
        ClutterActorIter iter;
Packit 31ecd5
        ClutterActor *child;
Packit 31ecd5
Packit 31ecd5
        clutter_actor_iter_init (&iter, box);
Packit 31ecd5
        while (clutter_actor_iter_next (&iter, &child))
Packit 31ecd5
          {
Packit 31ecd5
            guint duration;
Packit 31ecd5
Packit 31ecd5
            duration = clutter_actor_get_easing_duration (child);
Packit 31ecd5
            if (duration != 0)
Packit 31ecd5
              duration = 0;
Packit 31ecd5
            else
Packit 31ecd5
              duration = 250;
Packit 31ecd5
Packit 31ecd5
            clutter_actor_set_easing_duration (child, duration);
Packit 31ecd5
          }
Packit 31ecd5
      }
Packit 31ecd5
      break;
Packit 31ecd5
Packit 31ecd5
    case CLUTTER_KEY_v:
Packit 31ecd5
      {
Packit 31ecd5
        ClutterOrientation orientation;
Packit 31ecd5
Packit 31ecd5
        orientation = clutter_box_layout_get_orientation (layout);
Packit 31ecd5
Packit 31ecd5
        if (orientation == CLUTTER_ORIENTATION_HORIZONTAL)
Packit 31ecd5
          orientation = CLUTTER_ORIENTATION_VERTICAL;
Packit 31ecd5
        else
Packit 31ecd5
          orientation = CLUTTER_ORIENTATION_HORIZONTAL;
Packit 31ecd5
Packit 31ecd5
        clutter_box_layout_set_orientation (layout, orientation);
Packit 31ecd5
      }
Packit 31ecd5
      break;
Packit 31ecd5
Packit 31ecd5
    case CLUTTER_KEY_h:
Packit 31ecd5
      toggle = clutter_box_layout_get_homogeneous (layout);
Packit 31ecd5
      clutter_box_layout_set_homogeneous (layout, !toggle);
Packit 31ecd5
      break;
Packit 31ecd5
Packit 31ecd5
    case CLUTTER_KEY_p:
Packit 31ecd5
      toggle = clutter_box_layout_get_pack_start (layout);
Packit 31ecd5
      clutter_box_layout_set_pack_start (layout, !toggle);
Packit 31ecd5
      break;
Packit 31ecd5
Packit 31ecd5
    case CLUTTER_KEY_s:
Packit 31ecd5
      spacing = clutter_box_layout_get_spacing (layout);
Packit 31ecd5
Packit 31ecd5
      if (spacing > 12)
Packit 31ecd5
        spacing = 0;
Packit 31ecd5
      else
Packit 31ecd5
        spacing++;
Packit 31ecd5
Packit 31ecd5
      clutter_box_layout_set_spacing (layout, spacing);
Packit 31ecd5
      break;
Packit 31ecd5
Packit 31ecd5
    case CLUTTER_KEY_plus:
Packit 31ecd5
      add_actor (box, g_random_int_range (0, clutter_actor_get_n_children (box)));
Packit 31ecd5
      break;
Packit 31ecd5
Packit 31ecd5
    case CLUTTER_KEY_q:
Packit 31ecd5
      clutter_main_quit ();
Packit 31ecd5
      break;
Packit 31ecd5
Packit 31ecd5
    default:
Packit 31ecd5
      return FALSE;
Packit 31ecd5
    }
Packit 31ecd5
Packit 31ecd5
  return TRUE;
Packit 31ecd5
}
Packit 31ecd5
Packit 31ecd5
int
Packit 31ecd5
main (int argc, char *argv[])
Packit 31ecd5
{
Packit 31ecd5
  ClutterActor *stage, *box, *instructions;
Packit 31ecd5
  ClutterLayoutManager *layout;
Packit 31ecd5
  gint i;
Packit 31ecd5
Packit 31ecd5
  if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
Packit 31ecd5
    return EXIT_FAILURE;
Packit 31ecd5
Packit 31ecd5
  stage = clutter_stage_new ();
Packit 31ecd5
  clutter_stage_set_title (CLUTTER_STAGE (stage), "Box Layout");
Packit 31ecd5
  clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE);
Packit 31ecd5
Packit 31ecd5
  /* make the stage a vbox */
Packit 31ecd5
  layout = clutter_box_layout_new ();
Packit 31ecd5
  clutter_box_layout_set_orientation (CLUTTER_BOX_LAYOUT (layout),
Packit 31ecd5
                                      CLUTTER_ORIENTATION_VERTICAL);
Packit 31ecd5
  clutter_actor_set_layout_manager (stage, layout);
Packit 31ecd5
Packit 31ecd5
  box = clutter_actor_new ();
Packit 31ecd5
  clutter_actor_set_background_color (box, CLUTTER_COLOR_LightGray);
Packit 31ecd5
  clutter_actor_set_x_expand (box, TRUE);
Packit 31ecd5
  clutter_actor_set_y_expand (box, TRUE);
Packit 31ecd5
  layout = clutter_box_layout_new ();
Packit 31ecd5
  clutter_actor_set_layout_manager (box, layout);
Packit 31ecd5
  clutter_actor_add_child (stage, box);
Packit 31ecd5
Packit 31ecd5
  instructions = clutter_text_new_with_text ("Sans 12px", INSTRUCTIONS);
Packit 31ecd5
  clutter_actor_set_x_expand (instructions, TRUE);
Packit 31ecd5
  clutter_actor_set_y_expand (instructions, FALSE);
Packit 31ecd5
  clutter_actor_set_x_align (instructions, CLUTTER_ACTOR_ALIGN_START);
Packit 31ecd5
  clutter_actor_set_margin_top (instructions, 4);
Packit 31ecd5
  clutter_actor_set_margin_left (instructions, 4);
Packit 31ecd5
  clutter_actor_set_margin_bottom (instructions, 4);
Packit 31ecd5
  clutter_actor_add_child (stage, instructions);
Packit 31ecd5
Packit 31ecd5
  for (i = 0; i < 5; i++)
Packit 31ecd5
    add_actor (box, i);
Packit 31ecd5
Packit 31ecd5
  g_signal_connect (stage, "destroy",
Packit 31ecd5
                    G_CALLBACK (clutter_main_quit), NULL);
Packit 31ecd5
  g_signal_connect (stage, "key-release-event",
Packit 31ecd5
                    G_CALLBACK (key_release_cb), box);
Packit 31ecd5
Packit 31ecd5
  clutter_actor_show (stage);
Packit 31ecd5
  clutter_main ();
Packit 31ecd5
Packit 31ecd5
  return EXIT_SUCCESS;
Packit 31ecd5
}