clutter-fixed-layout.c 5.08 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 31 32 33 34 35 36
/*
 * Clutter.
 *
 * An OpenGL based 'interactive canvas' library.
 *
 * Copyright (C) 2009  Intel Corporation.
 *
 * 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 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 library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Author:
 *   Emmanuele Bassi <ebassi@linux.intel.com>
 *
 * Based on the fixed layout code inside clutter-group.c
 */

/**
 * SECTION:clutter-fixed-layout
 * @short_description: A fixed layout manager
 *
 * #ClutterFixedLayout is a layout manager implementing the same
 * layout policies as #ClutterGroup.
 *
 * #ClutterFixedLayout is available since Clutter 1.2
 */

37
#ifdef HAVE_CONFIG_H
38
#include "clutter-build-config.h"
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#endif

#include "clutter-debug.h"
#include "clutter-fixed-layout.h"
#include "clutter-private.h"

G_DEFINE_TYPE (ClutterFixedLayout,
               clutter_fixed_layout,
               CLUTTER_TYPE_LAYOUT_MANAGER);

static void
clutter_fixed_layout_get_preferred_width (ClutterLayoutManager *manager,
                                          ClutterContainer     *container,
                                          gfloat                for_height,
                                          gfloat               *min_width_p,
                                          gfloat               *nat_width_p)
{
56
  ClutterActor *actor, *child;
57 58
  gdouble min_right;
  gdouble natural_right;
59 60 61 62

  min_right = 0;
  natural_right = 0;

63
  actor = CLUTTER_ACTOR (container);
64

65 66 67
  for (child = clutter_actor_get_first_child (actor);
       child != NULL;
       child = clutter_actor_get_next_sibling (child))
68 69 70 71 72 73 74 75 76
    {
      gfloat child_x, child_min, child_natural;

      child_x = clutter_actor_get_x (child);

      clutter_actor_get_preferred_size (child,
                                        &child_min, NULL,
                                        &child_natural, NULL);

77 78 79 80 81
      if (child_x + child_min > min_right)
        min_right = child_x + child_min;

      if (child_x + child_natural > natural_right)
        natural_right = child_x + child_natural;
82 83 84
    }

  if (min_width_p)
85
    *min_width_p = min_right;
86 87

  if (nat_width_p)
88
    *nat_width_p = natural_right;
89 90 91 92 93 94 95 96 97
}

static void
clutter_fixed_layout_get_preferred_height (ClutterLayoutManager *manager,
                                           ClutterContainer     *container,
                                           gfloat                for_width,
                                           gfloat               *min_height_p,
                                           gfloat               *nat_height_p)
{
98
  ClutterActor *actor, *child;
99 100
  gdouble min_bottom;
  gdouble natural_bottom;
101 102 103 104

  min_bottom = 0;
  natural_bottom = 0;

105
  actor = CLUTTER_ACTOR (container);
106

107 108 109
  for (child = clutter_actor_get_first_child (actor);
       child != NULL;
       child = clutter_actor_get_next_sibling (child))
110 111 112 113 114 115 116 117 118
    {
      gfloat child_y, child_min, child_natural;

      child_y = clutter_actor_get_y (child);

      clutter_actor_get_preferred_size (child,
                                        NULL, &child_min,
                                        NULL, &child_natural);

119 120 121 122 123
      if (child_y + child_min > min_bottom)
        min_bottom = child_y + child_min;

      if (child_y + child_natural > natural_bottom)
        natural_bottom = child_y + child_natural;
124 125 126
    }

  if (min_height_p)
127
    *min_height_p = min_bottom;
128 129

  if (nat_height_p)
130
    *nat_height_p = natural_bottom;
131 132 133 134 135 136 137 138
}

static void
clutter_fixed_layout_allocate (ClutterLayoutManager   *manager,
                               ClutterContainer       *container,
                               const ClutterActorBox  *allocation,
                               ClutterAllocationFlags  flags)
{
139
  ClutterActor *child;
140

141 142 143
  for (child = clutter_actor_get_first_child (CLUTTER_ACTOR (container));
       child != NULL;
       child = clutter_actor_get_next_sibling (child))
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    {
      clutter_actor_allocate_preferred_size (child, flags);
    }
}

static void
clutter_fixed_layout_class_init (ClutterFixedLayoutClass *klass)
{
  ClutterLayoutManagerClass *manager_class =
    CLUTTER_LAYOUT_MANAGER_CLASS (klass);

  manager_class->get_preferred_width =
    clutter_fixed_layout_get_preferred_width;
  manager_class->get_preferred_height =
    clutter_fixed_layout_get_preferred_height;
  manager_class->allocate = clutter_fixed_layout_allocate;
}

static void
clutter_fixed_layout_init (ClutterFixedLayout *self)
{
}

167 168 169 170 171 172 173
/**
 * clutter_fixed_layout_new:
 *
 * Creates a new #ClutterFixedLayout
 *
 * Return value: the newly created #ClutterFixedLayout
 *
174
 * Since: 1.2
175
 */
176 177 178 179 180
ClutterLayoutManager *
clutter_fixed_layout_new (void)
{
  return g_object_new (CLUTTER_TYPE_FIXED_LAYOUT, NULL);
}