예제 #1
0
static void
st_bin_get_preferred_height (ClutterActor *self,
                             gfloat        for_width,
                             gfloat       *min_height_p,
                             gfloat       *natural_height_p)
{
  StBinPrivate *priv = ST_BIN (self)->priv;
  StThemeNode *theme_node = st_widget_get_theme_node (ST_WIDGET (self));

  st_theme_node_adjust_for_width (theme_node, &for_width);

  if (priv->child == NULL)
    {
      if (min_height_p)
        *min_height_p = 0;

      if (natural_height_p)
        *natural_height_p = 0;
    }
  else
    {
      _st_actor_get_preferred_height (priv->child, for_width, priv->x_fill,
                                      min_height_p,
                                      natural_height_p);
    }

  st_theme_node_adjust_preferred_height (theme_node, min_height_p, natural_height_p);
}
static void
shell_slicer_get_preferred_height (ClutterActor *self,
                                   gfloat        for_width,
                                   gfloat       *min_height_p,
                                   gfloat       *natural_height_p)
{
  ClutterActor *child = st_bin_get_child (ST_BIN (self));
  StThemeNode *theme_node = st_widget_get_theme_node (ST_WIDGET (self));

  st_theme_node_adjust_for_width (theme_node, &for_width);

  if (min_height_p)
    *min_height_p = 0;

  if (child == NULL)
    {
      if (natural_height_p)
        *natural_height_p = 0;
    }
  else
    {
      _st_actor_get_preferred_height (child, for_width, FALSE,
                                      NULL,
                                      natural_height_p);
    }

  st_theme_node_adjust_preferred_height (theme_node, min_height_p, natural_height_p);
}
예제 #3
0
static void
st_scroll_bar_get_preferred_height (ClutterActor *self,
                                    gfloat        for_width,
                                    gfloat       *min_height_p,
                                    gfloat       *natural_height_p)
{
  StScrollBar *bar = ST_SCROLL_BAR (self);
  StScrollBarPrivate *priv = bar->priv;
  StThemeNode *theme_node = st_widget_get_theme_node (ST_WIDGET (self));
  gfloat trough_min_height, trough_natural_height;
  gfloat handle_min_height, handle_natural_height;

  st_theme_node_adjust_for_width (theme_node, &for_width);

  _st_actor_get_preferred_height (priv->trough, for_width, TRUE,
                                  &trough_min_height, &trough_natural_height);

  _st_actor_get_preferred_height (priv->handle, for_width, TRUE,
                                  &handle_min_height, &handle_natural_height);

  if (priv->vertical)
    {
      if (min_height_p)
        *min_height_p = trough_min_height + handle_min_height;

      if (natural_height_p)
        *natural_height_p = trough_natural_height + handle_natural_height;
    }
  else
    {
      if (min_height_p)
        *min_height_p = MAX (trough_min_height, handle_min_height);

      if (natural_height_p)
        *natural_height_p = MAX (trough_natural_height, handle_natural_height);
    }

  st_theme_node_adjust_preferred_height (theme_node, min_height_p, natural_height_p);
}
예제 #4
0
static void
st_table_get_preferred_height (ClutterActor *self,
                               gfloat        for_width,
                               gfloat       *min_height_p,
                               gfloat       *natural_height_p)
{
  gint *min_heights, *pref_heights;
  gfloat total_min_height, total_pref_height;
  StTablePrivate *priv = ST_TABLE (self)->priv;
  StThemeNode *theme_node = st_widget_get_theme_node (ST_WIDGET (self));
  gint i;
  gint *min_widths;
  ClutterActor *child;

  /* We only support height-for-width allocation. So if we are called
   * width-for-height, calculate heights based on our natural width
   */
  if (for_width < 0)
    {
      float natural_width;

      clutter_actor_get_preferred_width (self, -1, NULL, &natural_width);
      for_width = natural_width;
    }

  if (priv->n_rows < 1)
    {
      *min_height_p = 0;
      *natural_height_p = 0;
      return;
    }

  st_theme_node_adjust_for_width (theme_node, &for_width);

  /* Setting size to zero and then what we want it to be causes a clear if
   * clear flag is set (which it should be.)
   */
  g_array_set_size (priv->min_heights, 0);
  g_array_set_size (priv->pref_heights, 0);
  g_array_set_size (priv->min_heights, priv->n_rows);
  g_array_set_size (priv->pref_heights, priv->n_rows);

  /* use min_widths to help allocation of height-for-width widgets */
  min_widths = st_table_calculate_col_widths (ST_TABLE (self), for_width);

  min_heights = (gint *) priv->min_heights->data;
  pref_heights = (gint *) priv->pref_heights->data;

  /* calculate minimum row heights */
  for (child = clutter_actor_get_first_child (self);
       child != NULL;
       child = clutter_actor_get_next_sibling (child))
    {
      gint row, col, col_span, cell_width, row_span;
      gfloat min, pref;
      StTableChild *meta;

      meta = (StTableChild *) clutter_container_get_child_meta (CLUTTER_CONTAINER (self), child);

      if (!meta->allocate_hidden && !CLUTTER_ACTOR_IS_VISIBLE (child))
        continue;

      /* get child properties */
      row = meta->row;
      col = meta->col;
      col_span = meta->col_span;
      row_span = meta->row_span;

      cell_width = 0;
      for (i = 0; i < col_span && col + i < priv->n_cols; i++)
        cell_width += min_widths[col + i];

      _st_actor_get_preferred_height (child, (float) cell_width, meta->x_fill,
                                      &min, &pref);

      if (row_span == 1 && min > min_heights[row])
        min_heights[row] = min;
      if (row_span == 1 && pref > pref_heights[row])
        pref_heights[row] = pref;
    }

  /* start off with row spacing */
  total_min_height = (priv->n_rows - 1) * (float) (priv->row_spacing);
  total_pref_height = total_min_height;

  for (i = 0; i < priv->n_rows; i++)
    {
      total_min_height += min_heights[i];
      total_pref_height += pref_heights[i];
    }

  if (min_height_p)
    *min_height_p = total_min_height;
  if (natural_height_p)
    *natural_height_p = total_pref_height;

  st_theme_node_adjust_preferred_height (theme_node, min_height_p, natural_height_p);
}
예제 #5
0
static gint *
st_table_calculate_row_heights (StTable *table,
                                gint     for_height,
                                gint   * col_widths)
{
  StTablePrivate *priv = ST_TABLE (table)->priv;
  gint *is_expand_row, *min_heights, *pref_heights, *row_heights, extra_row_height;
  gint i, total_min_height;
  gint expanded_rows = 0;
  gint n_expanded_rows = 0;
  ClutterActor *child;

  g_array_set_size (priv->row_heights, 0);
  g_array_set_size (priv->row_heights, priv->n_rows);
  row_heights = (gboolean *) priv->row_heights->data;

  g_array_set_size (priv->is_expand_row, 0);
  g_array_set_size (priv->is_expand_row, priv->n_rows);
  is_expand_row = (gboolean *) priv->is_expand_row->data;

  g_array_set_size (priv->min_heights, 0);
  g_array_set_size (priv->min_heights, priv->n_rows);
  min_heights = (gboolean *) priv->min_heights->data;

  g_array_set_size (priv->pref_heights, 0);
  g_array_set_size (priv->pref_heights, priv->n_rows);
  pref_heights = (gboolean *) priv->pref_heights->data;

  for (child = clutter_actor_get_first_child (CLUTTER_ACTOR (table));
       child != NULL;
       child = clutter_actor_get_next_sibling (child))
    {
      gint row, col, cell_width;
      gfloat h_min, h_pref;
      gboolean y_expand;
      StTableChild *meta;
      gint col_span, row_span;

      meta = (StTableChild *) clutter_container_get_child_meta (CLUTTER_CONTAINER (table), child);

      if (!meta->allocate_hidden && !CLUTTER_ACTOR_IS_VISIBLE (child))
        continue;

      /* get child properties */
      col = meta->col;
      row = meta->row;
      y_expand = meta->y_expand;
      col_span = meta->col_span;
      row_span = meta->row_span;

      if (y_expand)
        is_expand_row[row] = TRUE;

      /* calculate the cell width by including any spanned columns */
      cell_width = 0;
      for (i = 0; i < col_span && col + i < priv->n_cols; i++)
        cell_width += (float)(col_widths[col + i]);

      if (!meta->x_fill)
        {
          gfloat width;
          _st_actor_get_preferred_width (child, -1, meta->y_fill, NULL, &width);
          cell_width = MIN (cell_width, width);
        }

      _st_actor_get_preferred_height (child, cell_width, meta->x_fill,
                                      &h_min, &h_pref);

      if (row_span == 1 && h_pref > pref_heights[row])
        {
          pref_heights[row] = (int)(h_pref);
        }
      if (row_span == 1 && h_min > min_heights[row])
        {
          min_heights[row] = (int)(h_min);
        }
    }

  total_min_height = 0; // priv->row_spacing * (priv->n_rows - 1);
  for (i = 0; i < priv->n_rows; i++)
    total_min_height += pref_heights[i];

  /* calculate the remaining space and distribute it evenly onto all rows/cols
   * with the x/y expand property set. */
  for (i = 0; i < priv->n_rows; i++)
    if (is_expand_row[i])
      {
        expanded_rows += pref_heights[i];
        n_expanded_rows++;
      }

  /* extra row height = for height - row spacings - total_min_height */
  for_height -= (priv->row_spacing * (priv->n_rows - 1));
  extra_row_height = for_height - total_min_height;


  if (extra_row_height < 0)
    {
      gint *skip = g_slice_alloc0 (sizeof (gint) * priv->n_rows);
      gint total_shrink_height;

      /* If we need to shrink rows, we need to do multiple passes.
       *
       * We start by assuming all rows can shrink. All rows are sized
       * proportional to their height in the total table size. If a row would be
       * sized smaller than its minimum size, we mark it as non-shrinkable, and
       * reduce extra_row_height by the amount it has been shrunk. The amount
       * it has been shrunk by is the difference between the preferred and
       * minimum height, since all rows start at their preferred height. We
       * also then reduce the total table size (stored in total_shrink_height) by the height
       * of the row we are going to be skipping.
       *
       */

      /* We start by assuming all rows can shrink */
      total_shrink_height = total_min_height;
      for (i = 0; i < priv->n_rows; i++)
        {
          if (!skip[i])
            {
              gint tmp;

              /* Calculate the height of the row by starting with the preferred
               * height and taking away the extra row height proportional to
               * the preferred row height over the rows that are being shrunk
               */
              tmp = pref_heights[i]
                    + (extra_row_height * (pref_heights[i] / (float) total_shrink_height));

              if (tmp < min_heights[i])
                {
                  /* This was a row we *were* set to shrink, but we now find it would have
                   * been shrunk too much. We remove it from the list of rows to shrink and
                   * adjust extra_row_height and total_shrink_height appropriately */
                  skip[i] = TRUE;
                  row_heights[i] = min_heights[i];

                  /* Reduce extra_row_height by the amount we have reduced this
                   * actor by */
                  extra_row_height += (pref_heights[i] - min_heights[i]);
                  /* now take off the row from the total shrink height */
                  total_shrink_height -= pref_heights[i];

                  /* restart the loop */
                  i = -1;
                }
              else
                {
                  skip[i] = FALSE;
                  row_heights[i] = tmp;
                }
            }

        }

      g_slice_free1 (sizeof (gint) * priv->n_rows, skip);
    }
  else
    {
      for (i = 0; i < priv->n_rows; i++)
        {
          if (is_expand_row[i])
            row_heights[i] = pref_heights[i] + (extra_row_height / n_expanded_rows);
          else
            row_heights[i] = pref_heights[i];
        }
    }


  return row_heights;
}
예제 #6
0
static void
st_box_layout_allocate (ClutterActor          *actor,
                        const ClutterActorBox *box,
                        ClutterAllocationFlags flags)
{
  StBoxLayoutPrivate *priv = ST_BOX_LAYOUT (actor)->priv;
  StThemeNode *theme_node = st_widget_get_theme_node (ST_WIDGET (actor));
  ClutterActorBox content_box;
  gfloat avail_width, avail_height, min_width, natural_width, min_height, natural_height;
  gfloat position, next_position;
  GList *l, *children;
  gint n_expand_children = 0, i;
  gfloat expand_amount, shrink_amount;
  BoxChildShrink *shrinks = NULL;
  gboolean flip = (st_widget_get_direction (ST_WIDGET (actor)) == ST_TEXT_DIRECTION_RTL)
                   && (!priv->is_vertical);

  CLUTTER_ACTOR_CLASS (st_box_layout_parent_class)->allocate (actor, box,
                                                              flags);

  children = st_container_get_children_list (ST_CONTAINER (actor));
  if (children == NULL)
    return;

  st_theme_node_get_content_box (theme_node, box, &content_box);

  avail_width  = content_box.x2 - content_box.x1;
  avail_height = content_box.y2 - content_box.y1;

  get_content_preferred_width (ST_BOX_LAYOUT (actor), avail_height,
                               &min_width, &natural_width);
  get_content_preferred_height (ST_BOX_LAYOUT (actor), MAX (avail_width, min_width),
                                &min_height, &natural_height);


  /* update adjustments for scrolling */
  if (priv->vadjustment)
    {
      gdouble prev_value;

      g_object_set (G_OBJECT (priv->vadjustment),
                    "lower", 0.0,
                    "upper", MAX (min_height, avail_height),
                    "page-size", avail_height,
                    "step-increment", avail_height / 6,
                    "page-increment", avail_height - avail_height / 6,
                    NULL);

      prev_value = st_adjustment_get_value (priv->vadjustment);
      st_adjustment_set_value (priv->vadjustment, prev_value);
    }

  if (priv->hadjustment)
    {
      gdouble prev_value;

      g_object_set (G_OBJECT (priv->hadjustment),
                    "lower", 0.0,
                    "upper", MAX (min_width, avail_width),
                    "page-size", avail_width,
                    "step-increment", avail_width / 6,
                    "page-increment", avail_width - avail_width / 6,
                    NULL);

      prev_value = st_adjustment_get_value (priv->hadjustment);
      st_adjustment_set_value (priv->hadjustment, prev_value);
    }

  if (avail_height < min_height)
    {
      avail_height = min_height;
      content_box.y2 = content_box.y1 + avail_height;
    }

  if (avail_width < min_width)
    {
      avail_width = min_width;
      content_box.x2 = content_box.x1 + avail_width;
    }

  if (priv->is_vertical)
    {
      expand_amount = MAX (0, avail_height - natural_height);
      shrink_amount = MAX (0, natural_height - avail_height);
    }
  else
    {
      expand_amount = MAX (0, avail_width - natural_width);
      shrink_amount = MAX (0, natural_width - avail_width);
    }


  if (expand_amount > 0)
    {
      /* count the number of children with expand set to TRUE */
      n_expand_children = 0;
      for (l = children; l; l = l->next)
        {
          ClutterActor *child = l->data;
          gboolean expand;

          if (!CLUTTER_ACTOR_IS_VISIBLE (child) ||
              clutter_actor_get_fixed_position_set (child))
            continue;

          clutter_container_child_get ((ClutterContainer *) actor,
                                       child,
                                       "expand", &expand,
                                       NULL);
          if (expand)
            n_expand_children++;
        }

      if (n_expand_children == 0)
        expand_amount = 0;
    }
  else if (shrink_amount > 0)
    {
      shrinks = compute_shrinks (ST_BOX_LAYOUT (actor),
                                 priv->is_vertical ? avail_width : avail_height,
                                 shrink_amount);
     }

  if (priv->is_vertical)
    position = content_box.y1;
  else if (flip)
    position = content_box.x2;
  else
    position = content_box.x1;

  if (priv->is_pack_start)
    {
      l = g_list_last (children);
      i = g_list_length (children);
    }
  else
    {
      l = children;
      i = 0;
    }
    
  gboolean firstchild = TRUE;
  gfloat init_padding = (avail_width/2) - (natural_width/2);
  while (l)
    {
      ClutterActor *child = (ClutterActor*) l->data;
      ClutterActorBox child_box;
      gfloat child_min, child_nat, child_allocated;
      gboolean xfill, yfill, expand, fixed;
      StAlign xalign, yalign;

      if (!CLUTTER_ACTOR_IS_VISIBLE (child))
        goto next_child;

      fixed = clutter_actor_get_fixed_position_set (child);
      if (fixed)
        {
          clutter_actor_allocate_preferred_size (child, flags);
          goto next_child;
        }

      clutter_container_child_get ((ClutterContainer*) actor, child,
                                   "x-fill", &xfill,
                                   "y-fill", &yfill,
                                   "x-align", &xalign,
                                   "y-align", &yalign,
                                   "expand", &expand,
                                   NULL);

      if (priv->is_vertical)
        {
          _st_actor_get_preferred_height (child, avail_width, xfill,
                                          &child_min, &child_nat);
        }
      else
        {
          _st_actor_get_preferred_width (child, avail_height, yfill,
                                         &child_min, &child_nat);
        }

      child_allocated = child_nat;
      if (expand_amount > 0 && expand)
        child_allocated +=  expand_amount / n_expand_children;
      else if (shrink_amount > 0)
        child_allocated -= shrinks[i].shrink_amount;

      if (flip) {
        next_position = position - child_allocated;
        if (xalign == ST_ALIGN_CENTER_SPECIAL && next_position < content_box.x1)
          next_position = content_box.x1;
      }
      else {
        next_position = position + child_allocated;
        if (xalign == ST_ALIGN_CENTER_SPECIAL && next_position > content_box.x2)
          next_position = content_box.x2;
      }

      if (priv->is_vertical)
        {
          child_box.y1 = (int)(0.5 + position);
          child_box.y2 = (int)(0.5 + next_position);
          child_box.x1 = content_box.x1;
          child_box.x2 = content_box.x2;

          _st_allocate_fill (ST_WIDGET (actor), child, &child_box,
                             xalign, yalign, xfill, yfill);
          clutter_actor_allocate (child, &child_box, flags);

        }
      else
        {
          if (flip)
            {
              if (firstchild && xalign == ST_ALIGN_CENTER_SPECIAL)
                {
                  position -= init_padding;
                  next_position = position - child_allocated;
                  firstchild = FALSE;
                }
                if (xalign == ST_ALIGN_CENTER_SPECIAL && position > content_box.x2) {
                  position = content_box.x2;
                }
              child_box.x1 = (int)(0.5 + next_position);
              child_box.x2 = (int)(0.5 + position);
            }
          else
            {
              if (firstchild && xalign == ST_ALIGN_CENTER_SPECIAL)
                {
                  position += init_padding;
                  if (position < content_box.x1) {
                    position = content_box.x1;
                  }
                  next_position = position + child_allocated;
                  firstchild = FALSE;
                }
              if (xalign == ST_ALIGN_CENTER_SPECIAL && position < content_box.x1) {
                    position = content_box.x1;
                  }
              child_box.x1 = (int)(0.5 + position);
              child_box.x2 = (int)(0.5 + next_position);
            }

          child_box.y1 = content_box.y1;
          child_box.y2 = content_box.y2;

          _st_allocate_fill (ST_WIDGET (actor), child, &child_box,
                             xalign, yalign, xfill, yfill);
          clutter_actor_allocate (child, &child_box, flags);
        }

      if (flip)
        position = next_position - priv->spacing;
      else
        position = next_position + priv->spacing;

    next_child:
      if (priv->is_pack_start)
        {
          l = l->prev;
          i--;
        }
      else
        {
          l = l->next;
          i++;
        }
    }

  if (shrinks)
    g_free (shrinks);
}
예제 #7
0
static BoxChildShrink *
compute_shrinks (StBoxLayout *self,
                 gfloat       for_length,
                 gfloat       total_shrink)
{
  StBoxLayoutPrivate *priv = self->priv;
  GList *children = st_container_get_children_list (ST_CONTAINER (self));
  int n_children = g_list_length (children);
  BoxChildShrink *shrinks = g_new0 (BoxChildShrink, n_children);
  gfloat shrink_so_far;
  gfloat base_shrink = 0; /* the "= 0" is just to make gcc happy */
  int n_shrink_children;
  GList *l;
  int i;

  /* The effect that we want is that all the children get an equal chance
   * to expand from their minimum size up to the natural size. Or to put
   * it a different way, we want to start by shrinking only the child that
   * can shrink most, then shrink that and the next most shrinkable child,
   * to the point where we are shrinking everything.
   */

  /* Find the amount of possible shrink for each child */
  int n_possible_shrink_children = 0;
  for (l = children, i = 0; l; l = l->next, i++)
    {
      ClutterActor *child;
      gfloat child_min, child_nat;
      gboolean child_fill;
      gboolean fixed;

      child = (ClutterActor*) l->data;
      fixed = clutter_actor_get_fixed_position_set (child);

      shrinks[i].child_index = i;
      if (CLUTTER_ACTOR_IS_VISIBLE (child) && !fixed)
        {
          if (priv->is_vertical)
            {
              clutter_container_child_get ((ClutterContainer*) self, child,
                                           "x-fill", &child_fill,
                                           NULL);
              _st_actor_get_preferred_height (child,
                                              for_length, child_fill,
                                              &child_min, &child_nat);
            }
          else
            {
              clutter_container_child_get ((ClutterContainer*) self, child,
                                           "y-fill", &child_fill,
                                           NULL);
              _st_actor_get_preferred_width (child,
                                             for_length, child_fill,
                                             &child_min, &child_nat);
            }

          shrinks[i].shrink_amount = MAX (0., child_nat - child_min);
          n_possible_shrink_children++;
        }
      else
        {
          shrinks[i].shrink_amount = -1.;
        }
    }

  /* We want to process children starting from the child with the maximum available
   * shrink, so sort in this order; !visible children end up at the end */
  qsort (shrinks, n_children, sizeof (BoxChildShrink), compare_by_shrink_amount);

  /*   +--+
   *   |  |
   *   |  | +--
   *   |  | | |
   *   |  | | | +-+
   * --+--+-+-+-+-+----------
   *   |  | | | | | +-+ +-+
   *   |  | | | | | | | | |
   * --+--+-+-+-+-+-+-+------
   *
   * We are trying to find the correct position for the upper line the "water mark"
   * so that total of the portion of the bars above the line is equal to the total
   * amount we want to shrink.
   */

  /* Start by moving the line downward, top-of-bar by top-of-bar */
  shrink_so_far = 0;
  for (n_shrink_children = 1; n_shrink_children <= n_possible_shrink_children; n_shrink_children++)
    {
      if (n_shrink_children < n_possible_shrink_children)
        base_shrink = shrinks[n_shrink_children].shrink_amount;
      else
        base_shrink = 0;
      shrink_so_far += n_shrink_children * (shrinks[n_shrink_children - 1].shrink_amount - base_shrink);

      if (shrink_so_far >= total_shrink || n_shrink_children == n_possible_shrink_children)
        break;
    }

  /* OK, we found enough shrinkage, move it back upwards to the right position */
  base_shrink += (shrink_so_far - total_shrink) / n_shrink_children;
  if (base_shrink < 0) /* can't shrink that much, probably round-off error */
    base_shrink = 0;

  /* Assign the portion above the base shrink line to the shrink_amount */
  for (i = 0; i < n_shrink_children; i++)
    shrinks[i].shrink_amount -= base_shrink;
  for (; i < n_children; i++)
    shrinks[i].shrink_amount = 0;

  /* And sort back to their original order */
  qsort (shrinks, n_children, sizeof (BoxChildShrink), compare_by_child_index);

  return shrinks;
}
예제 #8
0
static void
get_content_preferred_height (StBoxLayout *self,
                              gfloat       for_width,
                              gfloat      *min_height_p,
                              gfloat      *natural_height_p)
{
  StBoxLayoutPrivate *priv = self->priv;
  gint n_children = 0;
  gint n_fixed = 0;
  gfloat min_height, natural_height;
  GList *l, *children;

  min_height = 0;
  natural_height = 0;

  children = st_container_get_children_list (ST_CONTAINER (self));

  for (l = children; l; l = g_list_next (l))
    {
      ClutterActor *child = l->data;
      gfloat child_min = 0, child_nat = 0;
      gboolean child_fill = FALSE;

      if (!CLUTTER_ACTOR_IS_VISIBLE (child))
        continue;

      n_children++;

      if (clutter_actor_get_fixed_position_set (child))
        {
          n_fixed++;
          continue;
        }

      if (priv->is_vertical)
        {
          clutter_container_child_get ((ClutterContainer*) self, child,
                                       "x-fill", &child_fill,
                                       NULL);
        }
      _st_actor_get_preferred_height (child,
                                      (priv->is_vertical) ? for_width : -1,
                                      child_fill,
                                      &child_min,
                                      &child_nat);

      if (!priv->is_vertical)
        {
          min_height = MAX (child_min, min_height);
          natural_height = MAX (child_nat, natural_height);
        }
      else
        {
          min_height += child_min;
          natural_height += child_nat;
        }
    }

  if (priv->is_vertical && (n_children - n_fixed) > 1)
    {
      min_height += priv->spacing * (n_children - n_fixed - 1);
      natural_height += priv->spacing * (n_children - n_fixed - 1);
    }

  if (min_height_p)
    *min_height_p = min_height;

  if (natural_height_p)
    *natural_height_p = natural_height;
}
예제 #9
0
static void
st_table_calculate_row_heights (StTable *table,
                                gint     for_height)
{
  StTablePrivate *priv = ST_TABLE (table)->priv;
  GList *list, *children;
  gint i;
  DimensionData *rows, *columns;

  g_array_set_size (priv->rows, 0);
  g_array_set_size (priv->rows, priv->n_rows);
  rows = &g_array_index (priv->rows, DimensionData, 0);

  columns = &g_array_index (priv->columns, DimensionData, 0);

  /* Reset the visible rows */
  priv->visible_rows = 0;
  for (i = 0; i < priv->n_rows; i++)
    rows[i].is_visible = FALSE;

  /* STAGE ONE: calculate row heights for non-spanned children */
  children = st_container_get_children_list (ST_CONTAINER (table));
  for (list = children; list; list = list->next)
    {
      StTableChild *meta;
      ClutterActor *child;
      DimensionData *row;
      gfloat c_min, c_pref;

      child = CLUTTER_ACTOR (list->data);

      meta = (StTableChild *)
        clutter_container_get_child_meta (CLUTTER_CONTAINER (table), child);

      if (!meta->allocate_hidden && !CLUTTER_ACTOR_IS_VISIBLE (child))
        continue;

      if (meta->row_span > 1)
        continue;

      row = &rows[meta->row];

      /* If this child is visible, then its row is visible */
      if (!row->is_visible)
        {
          row->is_visible = TRUE;
          priv->visible_rows++;
        }

      _st_actor_get_preferred_height (child, columns[meta->col].final_size,
                                      meta->x_fill, &c_min, &c_pref);

      row->min_size = MAX (row->min_size, c_min);
      row->final_size = row->pref_size = MAX (row->pref_size, c_pref);
      row->expand = MAX (row->expand, meta->y_expand);
    }



  /* STAGE TWO: take spanning children into account */
  for (list = children; list; list = list->next)
    {
      StTableChild *meta;
      ClutterActor *child;
      gfloat c_min, c_pref;
      gfloat min_height, pref_height;
      gint start_row, end_row;
      gint n_expand;

      child = CLUTTER_ACTOR (list->data);

      meta = (StTableChild *)
        clutter_container_get_child_meta (CLUTTER_CONTAINER (table), child);

      if (!meta->allocate_hidden && !CLUTTER_ACTOR_IS_VISIBLE (child))
        continue;

      if (meta->row_span < 2)
        continue;

      start_row = meta->row;
      end_row = meta->row + meta->row_span - 1;

      _st_actor_get_preferred_height (child, columns[meta->col].final_size,
                                      meta->x_fill, &c_min, &c_pref);


      /* check there is enough room for this actor */
      min_height = 0;
      pref_height = 0;
      n_expand = 0;
      for (i = start_row; i <= end_row; i++)
        {
          min_height += rows[i].min_size;
          pref_height += rows[i].pref_size;

          if (rows[i].expand)
            {
              n_expand++;
            }

          /* If this actor is visible, then all the rows is spans are visible */
          if (!rows[i].is_visible)
            {
              rows[i].is_visible = TRUE;
              priv->visible_rows++;
            }
          rows[i].expand = MAX (rows[i].expand, meta->y_expand);
        }
      min_height += priv->row_spacing * (meta->row_span - 1);
      pref_height += priv->row_spacing * (meta->row_span - 1);

      /* 1) If the minimum height of the rows spanned is less than the minimum
       * height of the child that is spanning them, then we must increase the
       * minimum height of the rows spanned.
       *
       * 2) If the preferred height of the spanned rows is more that the minimum
       * height of the spanning child, then we can start at this size and
       * decrease each row evenly.
       *
       * 3) If the preferred height of the rows is more than the minimum height
       * of the spanned child, then we can start at the preferred height and
       * expand.
       */
      /* (1) */
      if (c_min > min_height)
        {

          /* (2) */
          /* we can start from preferred height and decrease */
          if (pref_height > c_min)
            {
              for (i = start_row; i <= end_row; i++)
                {
                  rows[i].final_size = rows[i].pref_size;
                }

              while (pref_height > c_min)
                {
                  for (i = start_row; i <= end_row; i++)
                    {
                      if (rows[i].final_size > rows[i].min_size)
                        {
                          rows[i].final_size--;
                          pref_height--;
                        }
                    }
                }
              for (i = start_row; i <= end_row; i++)
                {
                  rows[i].min_size = rows[i].final_size;
                }

            }
          else
            {
              /* (3) */
              /* we can expand from preferred size */
              gfloat expand_by;

              expand_by = c_pref - pref_height;

              for (i = start_row; i <= end_row; i++)
                {
                  if (n_expand)
                    {
                      if (rows[i].expand)
                        rows[i].min_size =
                          rows[i].pref_size + expand_by / n_expand;
                    }
                  else
                    {
                      rows[i].min_size =
                        rows[i].pref_size + expand_by / meta->row_span;
                    }

                }
            }
        }

    }


  /* calculate final heights */
  if (for_height >= 0)
    {
      gfloat min_height, pref_height;
      gint n_expand;

      min_height = 0;
      pref_height = 0;
      n_expand = 0;
      for (i = 0; i < priv->n_rows; i++)
        {
          pref_height += rows[i].pref_size;
          min_height += rows[i].min_size;
          if (rows[i].expand)
            n_expand++;
        }
      pref_height += priv->row_spacing * (priv->n_rows - 1);
      min_height += priv->row_spacing * (priv->n_rows - 1);

      if (for_height <= min_height)
        {
          /* erk, we can't shrink this! */
          for (i = 0; i < priv->n_rows; i++)
            {
              rows[i].final_size = rows[i].min_size;
            }
          return;
        }

      if (for_height == pref_height)
        {
          /* perfect! */
          for (i = 0; i < priv->n_rows; i++)
            {
              rows[i].final_size = rows[i].pref_size;
            }
          return;
        }

      /* for_height is between min_height and pref_height */
      if (for_height < pref_height && for_height > min_height)
        {
          gfloat height;

          /* shrink rows until they reach min_height */

          /* start with all rows at preferred size */
          for (i = 0; i < priv->n_rows; i++)
            {
              rows[i].final_size = rows[i].pref_size;
            }
          height = pref_height;

          while (height > for_height)
            {
              for (i = 0; i < priv->n_rows; i++)
                {
                  if (rows[i].final_size > rows[i].min_size)
                    {
                      rows[i].final_size--;
                      height--;
                    }
                }
            }

          return;
        }

      /* expand rows */
      if (for_height > pref_height)
        {
          gfloat extra_height = for_height - pref_height;
          gint remaining;

          if (n_expand)
            remaining = (gint) extra_height % n_expand;
          else
            remaining = (gint) extra_height % priv->n_rows;

          for (i = 0; i < priv->n_rows; i++)
            {
              if (rows[i].expand)
                rows[i].final_size =
                  rows[i].pref_size + (extra_height / n_expand);
              else if (!n_expand)
                rows[i].final_size =
                  rows[i].pref_size + (extra_height / priv->n_rows);
              else
                rows[i].final_size = rows[i].pref_size;
            }

          /* distribute the remainder among children */
          i = 0;
          while (remaining)
            {
              if (rows[i].expand || !n_expand)
                {
                  rows[i].final_size++;
                  remaining--;
                }
              i++;
            }
        }
    }

}