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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gtkfixedlayout
|
|
|
|
* @Short_description: A layout manager that allows positioning at fixed
|
|
|
|
* coordinates
|
|
|
|
* @Title: GtkFixedLayout
|
|
|
|
*
|
|
|
|
* #GtkFixedLayout is a layout manager which can place child widgets
|
|
|
|
* at fixed positions, and with fixed sizes.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* In addition, #GtkFixedLayout does not pay attention to text direction and
|
|
|
|
* 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;
|
|
|
|
* #GtkFixedLayout won't be able to do that for you.
|
|
|
|
*
|
|
|
|
* Finally, fixed positioning makes it kind of annoying to add/remove GUI
|
|
|
|
* elements, since you have to reposition all the other elements. This is a
|
|
|
|
* long-term maintenance problem for your application.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkfixedlayout.h"
|
|
|
|
|
|
|
|
#include "gtkintl.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
|
|
|
|
2019-08-25 12:56:13 +00:00
|
|
|
child_props[PROP_CHILD_TRANSFORM] =
|
|
|
|
g_param_spec_boxed ("transform",
|
|
|
|
P_("transform"),
|
|
|
|
P_("The transform of a child of a fixed layout"),
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-25 12:56:13 +00:00
|
|
|
* gtk_fixed_layout_child_set_transform:
|
2019-03-26 16:50:13 +00:00
|
|
|
* @child: a #GtkFixedLayoutChild
|
2019-08-25 12:56:13 +00:00
|
|
|
* @transform: a #GskTransform
|
2019-03-26 16:50:13 +00:00
|
|
|
*
|
2019-03-26 19:17:26 +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));
|
|
|
|
|
2019-08-25 12:56:13 +00:00
|
|
|
child->transform = gsk_transform_transform (child->transform, 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:
|
2019-03-26 16:50:13 +00:00
|
|
|
* @child: a #GtkFixedLayoutChild
|
|
|
|
*
|
2019-03-26 19:17:26 +00:00
|
|
|
* Retrieves the transformation of the child of a #GtkFixedLayout.
|
|
|
|
*
|
|
|
|
* 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);
|
|
|
|
|
2019-08-25 12:56:13 +00:00
|
|
|
gsk_transform_transform_bounds (child_info->transform,
|
2019-03-26 19:17:26 +00:00
|
|
|
&GRAPHENE_RECT_INIT (0.f, 0.f, child_min, child_min_opp),
|
|
|
|
&min_rect);
|
2019-08-25 12:56:13 +00:00
|
|
|
gsk_transform_transform_bounds (child_info->transform,
|
2019-03-26 19:17:26 +00:00
|
|
|
&GRAPHENE_RECT_INIT (0.f, 0.f, child_nat, child_nat_opp),
|
|
|
|
&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:
|
|
|
|
*
|
|
|
|
* Creates a new #GtkFixedLayout.
|
|
|
|
*
|
|
|
|
* Returns: the newly created #GtkFixedLayout
|
|
|
|
*/
|
2019-03-26 16:50:13 +00:00
|
|
|
GtkLayoutManager *
|
|
|
|
gtk_fixed_layout_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_FIXED_LAYOUT, NULL);
|
|
|
|
}
|