2019-03-26 16:50:13 +00:00
|
|
|
/* gtkfixedlayout.c: Fixed positioning layout manager
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
*
|
|
|
|
* Copyright 2019 GNOME Foundation
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-02-28 18:09:08 +00:00
|
|
|
* GtkFixedLayout:
|
2019-03-26 16:50:13 +00:00
|
|
|
*
|
2021-02-28 18:09:08 +00:00
|
|
|
* `GtkFixedLayout` is a layout manager which can place child widgets
|
|
|
|
* at fixed positions.
|
2019-03-26 16:50:13 +00:00
|
|
|
*
|
|
|
|
* Most applications should never use this layout manager; fixed positioning
|
|
|
|
* and sizing requires constant recalculations on where children need to be
|
|
|
|
* positioned and sized. Other layout managers perform this kind of work
|
|
|
|
* internally so that application developers don't need to do it. Specifically,
|
|
|
|
* widgets positioned in a fixed layout manager will need to take into account:
|
|
|
|
*
|
|
|
|
* - Themes, which may change widget sizes.
|
|
|
|
*
|
|
|
|
* - Fonts other than the one you used to write the app will of course
|
|
|
|
* change the size of widgets containing text; keep in mind that
|
|
|
|
* users may use a larger font because of difficulty reading the
|
|
|
|
* default, or they may be using a different OS that provides different
|
|
|
|
* fonts.
|
|
|
|
*
|
|
|
|
* - Translation of text into other languages changes its size. Also,
|
|
|
|
* display of non-English text will use a different font in many
|
|
|
|
* cases.
|
|
|
|
*
|
2021-02-28 18:09:08 +00:00
|
|
|
* In addition, `GtkFixedLayout` does not pay attention to text direction and
|
2019-03-26 16:50:13 +00:00
|
|
|
* thus may produce unwanted results if your app is run under right-to-left
|
|
|
|
* languages such as Hebrew or Arabic. That is: normally GTK will order
|
|
|
|
* containers appropriately depending on the text direction, e.g. to put labels
|
|
|
|
* to the right of the thing they label when using an RTL language;
|
2021-02-28 18:09:08 +00:00
|
|
|
* `GtkFixedLayout` won't be able to do that for you.
|
2019-03-26 16:50:13 +00:00
|
|
|
*
|
2021-02-28 18:09:08 +00:00
|
|
|
* Finally, fixed positioning makes it kind of annoying to add/remove UI
|
2019-03-26 16:50:13 +00:00
|
|
|
* elements, since you have to reposition all the other elements. This is a
|
|
|
|
* long-term maintenance problem for your application.
|
|
|
|
*/
|
|
|
|
|
2021-02-28 18:09:08 +00:00
|
|
|
/**
|
|
|
|
* GtkFixedLayoutChild:
|
|
|
|
*
|
|
|
|
* `GtkLayoutChild` subclass for children in a `GtkFixedLayout`.
|
|
|
|
*/
|
|
|
|
|
2019-03-26 16:50:13 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkfixedlayout.h"
|
|
|
|
|
|
|
|
#include "gtklayoutchild.h"
|
|
|
|
#include "gtkprivate.h"
|
|
|
|
#include "gtkwidgetprivate.h"
|
|
|
|
|
|
|
|
#include <graphene-gobject.h>
|
|
|
|
|
|
|
|
struct _GtkFixedLayout
|
|
|
|
{
|
|
|
|
GtkLayoutManager parent_instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkFixedLayoutChild
|
|
|
|
{
|
|
|
|
GtkLayoutChild parent_instance;
|
|
|
|
|
2019-08-25 12:56:13 +00:00
|
|
|
GskTransform *transform;
|
2019-03-26 16:50:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2019-08-25 12:56:13 +00:00
|
|
|
PROP_CHILD_TRANSFORM = 1,
|
2019-03-26 16:50:13 +00:00
|
|
|
|
|
|
|
N_CHILD_PROPERTIES
|
|
|
|
};
|
|
|
|
|
|
|
|
static GParamSpec *child_props[N_CHILD_PROPERTIES];
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GtkFixedLayoutChild, gtk_fixed_layout_child, GTK_TYPE_LAYOUT_CHILD)
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_fixed_layout_child_set_property (GObject *gobject,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkFixedLayoutChild *self = GTK_FIXED_LAYOUT_CHILD (gobject);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
2019-08-25 12:56:13 +00:00
|
|
|
case PROP_CHILD_TRANSFORM:
|
|
|
|
gtk_fixed_layout_child_set_transform (self, g_value_get_boxed (value));
|
2019-03-26 16:50:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_fixed_layout_child_get_property (GObject *gobject,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkFixedLayoutChild *self = GTK_FIXED_LAYOUT_CHILD (gobject);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
2019-08-25 12:56:13 +00:00
|
|
|
case PROP_CHILD_TRANSFORM:
|
|
|
|
g_value_set_boxed (value, &self->transform);
|
2019-03-26 16:50:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 19:17:26 +00:00
|
|
|
static void
|
|
|
|
gtk_fixed_layout_child_finalize (GObject *gobject)
|
|
|
|
{
|
|
|
|
GtkFixedLayoutChild *self = GTK_FIXED_LAYOUT_CHILD (gobject);
|
|
|
|
|
2019-08-25 12:56:13 +00:00
|
|
|
gsk_transform_unref (self->transform);
|
2019-03-26 19:17:26 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_fixed_layout_child_parent_class)->finalize (gobject);
|
|
|
|
}
|
|
|
|
|
2019-03-26 16:50:13 +00:00
|
|
|
static void
|
|
|
|
gtk_fixed_layout_child_class_init (GtkFixedLayoutChildClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->set_property = gtk_fixed_layout_child_set_property;
|
|
|
|
gobject_class->get_property = gtk_fixed_layout_child_get_property;
|
2019-03-26 19:17:26 +00:00
|
|
|
gobject_class->finalize = gtk_fixed_layout_child_finalize;
|
2019-03-26 16:50:13 +00:00
|
|
|
|
2021-02-28 18:09:08 +00:00
|
|
|
/**
|
|
|
|
* GtkFixedLayoutChild:transform: (attributes org.gtk.Property.get=gtk_fixed_layout_child_get_transform org.gtk.Property.set=gtk_fixed_layout_child_set_transform)
|
|
|
|
*
|
|
|
|
* The transform of the child.
|
|
|
|
*/
|
2019-08-25 12:56:13 +00:00
|
|
|
child_props[PROP_CHILD_TRANSFORM] =
|
2022-05-11 12:19:39 +00:00
|
|
|
g_param_spec_boxed ("transform", NULL, NULL,
|
2019-03-26 19:17:26 +00:00
|
|
|
GSK_TYPE_TRANSFORM,
|
2019-03-26 16:50:13 +00:00
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_STATIC_STRINGS |
|
|
|
|
G_PARAM_EXPLICIT_NOTIFY);
|
|
|
|
|
|
|
|
g_object_class_install_properties (gobject_class, N_CHILD_PROPERTIES, child_props);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_fixed_layout_child_init (GtkFixedLayoutChild *self)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-28 18:09:08 +00:00
|
|
|
* gtk_fixed_layout_child_set_transform: (attributes org.gtk.Method.set_property=transform)
|
|
|
|
* @child: a `GtkFixedLayoutChild`
|
|
|
|
* @transform: a `GskTransform`
|
2019-03-26 16:50:13 +00:00
|
|
|
*
|
2021-02-28 18:09:08 +00:00
|
|
|
* Sets the transformation of the child of a `GtkFixedLayout`.
|
2019-03-26 16:50:13 +00:00
|
|
|
*/
|
|
|
|
void
|
2019-08-25 12:56:13 +00:00
|
|
|
gtk_fixed_layout_child_set_transform (GtkFixedLayoutChild *child,
|
|
|
|
GskTransform *transform)
|
2019-03-26 16:50:13 +00:00
|
|
|
{
|
|
|
|
GtkLayoutManager *layout;
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_FIXED_LAYOUT_CHILD (child));
|
|
|
|
|
2020-01-08 15:34:04 +00:00
|
|
|
gsk_transform_unref (child->transform);
|
|
|
|
child->transform = gsk_transform_ref (transform);
|
2019-03-26 16:50:13 +00:00
|
|
|
|
|
|
|
layout = gtk_layout_child_get_layout_manager (GTK_LAYOUT_CHILD (child));
|
|
|
|
gtk_layout_manager_layout_changed (layout);
|
|
|
|
|
2019-08-25 12:56:13 +00:00
|
|
|
g_object_notify_by_pspec (G_OBJECT (child), child_props[PROP_CHILD_TRANSFORM]);
|
2019-03-26 16:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-25 12:56:13 +00:00
|
|
|
* gtk_fixed_layout_child_get_transform:
|
2021-02-28 18:09:08 +00:00
|
|
|
* @child: a `GtkFixedLayoutChild`
|
2019-03-26 16:50:13 +00:00
|
|
|
*
|
2021-02-28 18:09:08 +00:00
|
|
|
* Retrieves the transformation of the child.
|
2019-03-26 19:17:26 +00:00
|
|
|
*
|
2021-02-28 18:09:08 +00:00
|
|
|
* Returns: (transfer none) (nullable): a `GskTransform`
|
2019-03-26 16:50:13 +00:00
|
|
|
*/
|
2019-03-26 19:17:26 +00:00
|
|
|
GskTransform *
|
2019-08-25 12:56:13 +00:00
|
|
|
gtk_fixed_layout_child_get_transform (GtkFixedLayoutChild *child)
|
2019-03-26 16:50:13 +00:00
|
|
|
{
|
2019-03-26 19:17:26 +00:00
|
|
|
g_return_val_if_fail (GTK_IS_FIXED_LAYOUT_CHILD (child), NULL);
|
2019-03-26 16:50:13 +00:00
|
|
|
|
2019-08-25 12:56:13 +00:00
|
|
|
return child->transform;
|
2019-03-26 16:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GtkFixedLayout, gtk_fixed_layout, GTK_TYPE_LAYOUT_MANAGER)
|
|
|
|
|
2019-04-12 16:08:21 +00:00
|
|
|
static GtkSizeRequestMode
|
|
|
|
gtk_fixed_layout_get_request_mode (GtkLayoutManager *layout_manager,
|
|
|
|
GtkWidget *widget)
|
|
|
|
{
|
|
|
|
return GTK_SIZE_REQUEST_CONSTANT_SIZE;
|
|
|
|
}
|
|
|
|
|
2019-03-26 16:50:13 +00:00
|
|
|
static void
|
|
|
|
gtk_fixed_layout_measure (GtkLayoutManager *layout_manager,
|
|
|
|
GtkWidget *widget,
|
|
|
|
GtkOrientation orientation,
|
|
|
|
int for_size,
|
|
|
|
int *minimum,
|
|
|
|
int *natural,
|
|
|
|
int *minimum_baseline,
|
|
|
|
int *natural_baseline)
|
|
|
|
{
|
|
|
|
GtkFixedLayoutChild *child_info;
|
|
|
|
GtkWidget *child;
|
|
|
|
int minimum_size = 0;
|
|
|
|
int natural_size = 0;
|
|
|
|
|
|
|
|
for (child = _gtk_widget_get_first_child (widget);
|
|
|
|
child != NULL;
|
|
|
|
child = _gtk_widget_get_next_sibling (child))
|
|
|
|
{
|
2019-03-26 19:17:26 +00:00
|
|
|
int child_min = 0, child_nat = 0;
|
|
|
|
int child_min_opp = 0, child_nat_opp = 0;
|
|
|
|
graphene_rect_t min_rect, nat_rect;
|
2019-03-26 16:50:13 +00:00
|
|
|
|
2019-06-10 18:01:59 +00:00
|
|
|
if (!gtk_widget_should_layout (child))
|
2019-03-26 16:50:13 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
child_info = GTK_FIXED_LAYOUT_CHILD (gtk_layout_manager_get_layout_child (layout_manager, child));
|
|
|
|
|
2019-03-26 19:17:26 +00:00
|
|
|
gtk_widget_measure (child, orientation, -1,
|
|
|
|
&child_min, &child_nat,
|
|
|
|
NULL, NULL);
|
|
|
|
gtk_widget_measure (child, OPPOSITE_ORIENTATION (orientation), -1,
|
|
|
|
&child_min_opp, &child_nat_opp,
|
|
|
|
NULL, NULL);
|
|
|
|
|
2020-04-25 17:18:34 +00:00
|
|
|
min_rect.origin.x = min_rect.origin.y = 0;
|
|
|
|
nat_rect.origin.x = nat_rect.origin.y = 0;
|
|
|
|
if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
|
|
|
{
|
|
|
|
min_rect.size.width = child_min;
|
|
|
|
min_rect.size.height = child_min_opp;
|
|
|
|
nat_rect.size.width = child_nat;
|
|
|
|
nat_rect.size.height = child_nat_opp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
min_rect.size.width = child_min_opp;
|
|
|
|
min_rect.size.height = child_min;
|
|
|
|
nat_rect.size.width = child_nat_opp;
|
|
|
|
nat_rect.size.height = child_nat;
|
|
|
|
}
|
|
|
|
|
|
|
|
gsk_transform_transform_bounds (child_info->transform, &min_rect, &min_rect);
|
|
|
|
gsk_transform_transform_bounds (child_info->transform, &nat_rect, &nat_rect);
|
2019-03-26 16:50:13 +00:00
|
|
|
|
|
|
|
if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
|
|
|
{
|
2019-03-26 19:17:26 +00:00
|
|
|
minimum_size = MAX (minimum_size, min_rect.origin.x + min_rect.size.width);
|
|
|
|
natural_size = MAX (natural_size, nat_rect.origin.x + nat_rect.size.width);
|
2019-03-26 16:50:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-26 19:17:26 +00:00
|
|
|
minimum_size = MAX (minimum_size, min_rect.origin.y + min_rect.size.height);
|
|
|
|
natural_size = MAX (natural_size, nat_rect.origin.y + nat_rect.size.height);
|
2019-03-26 16:50:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minimum != NULL)
|
|
|
|
*minimum = minimum_size;
|
|
|
|
if (natural != NULL)
|
|
|
|
*natural = natural_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_fixed_layout_allocate (GtkLayoutManager *layout_manager,
|
|
|
|
GtkWidget *widget,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int baseline)
|
|
|
|
{
|
|
|
|
GtkFixedLayoutChild *child_info;
|
|
|
|
GtkWidget *child;
|
|
|
|
|
|
|
|
for (child = _gtk_widget_get_first_child (widget);
|
|
|
|
child != NULL;
|
|
|
|
child = _gtk_widget_get_next_sibling (child))
|
|
|
|
{
|
|
|
|
GtkRequisition child_req;
|
|
|
|
|
2019-06-10 18:01:59 +00:00
|
|
|
if (!gtk_widget_should_layout (child))
|
2019-03-26 16:50:13 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
child_info = GTK_FIXED_LAYOUT_CHILD (gtk_layout_manager_get_layout_child (layout_manager, child));
|
|
|
|
gtk_widget_get_preferred_size (child, &child_req, NULL);
|
|
|
|
|
2019-03-26 19:17:26 +00:00
|
|
|
gtk_widget_allocate (child,
|
|
|
|
child_req.width,
|
|
|
|
child_req.height,
|
|
|
|
-1,
|
2019-08-25 12:56:13 +00:00
|
|
|
gsk_transform_ref (child_info->transform));
|
2019-03-26 16:50:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkLayoutChild *
|
|
|
|
gtk_fixed_layout_create_layout_child (GtkLayoutManager *manager,
|
|
|
|
GtkWidget *widget,
|
|
|
|
GtkWidget *for_child)
|
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_FIXED_LAYOUT_CHILD,
|
|
|
|
"layout-manager", manager,
|
|
|
|
"child-widget", for_child,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_fixed_layout_class_init (GtkFixedLayoutClass *klass)
|
|
|
|
{
|
|
|
|
GtkLayoutManagerClass *layout_class = GTK_LAYOUT_MANAGER_CLASS (klass);
|
|
|
|
|
2019-03-28 16:58:00 +00:00
|
|
|
layout_class->layout_child_type = GTK_TYPE_FIXED_LAYOUT_CHILD;
|
|
|
|
|
2019-04-12 16:08:21 +00:00
|
|
|
layout_class->get_request_mode = gtk_fixed_layout_get_request_mode;
|
2019-03-26 16:50:13 +00:00
|
|
|
layout_class->measure = gtk_fixed_layout_measure;
|
|
|
|
layout_class->allocate = gtk_fixed_layout_allocate;
|
|
|
|
layout_class->create_layout_child = gtk_fixed_layout_create_layout_child;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_fixed_layout_init (GtkFixedLayout *self)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-02 02:57:49 +00:00
|
|
|
/**
|
|
|
|
* gtk_fixed_layout_new:
|
|
|
|
*
|
2021-02-28 18:09:08 +00:00
|
|
|
* Creates a new `GtkFixedLayout`.
|
2019-05-02 02:57:49 +00:00
|
|
|
*
|
2021-02-28 18:09:08 +00:00
|
|
|
* Returns: the newly created `GtkFixedLayout`
|
2019-05-02 02:57:49 +00:00
|
|
|
*/
|
2019-03-26 16:50:13 +00:00
|
|
|
GtkLayoutManager *
|
|
|
|
gtk_fixed_layout_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_FIXED_LAYOUT, NULL);
|
|
|
|
}
|