/*
 *  $Id: shrink-wrap.c 29990 2026-05-25 12:47:33Z yeti-dn $
 *  Copyright (C) 2026 David Necas (Yeti).
 *  E-mail: yeti@gwyddion.net.
 *
 *  This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
 *  License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any
 *  later version.
 *
 *  This program 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 General Public License for more
 *  details.
 *
 *  You should have received a copy of the GNU General Public License along with this program; if not, write to the
 *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include <glib-object.h>
#include <gtk/gtk.h>

#include "libgwyddion/macros.h"

#include "libgwyui/shrink-wrap.h"

// FIXME: Mode should probably be a porperty.

struct _GwyShrinkWrapPrivate {
    GtkSizeGroupMode mode;
};

static void  get_preferred_width                        (GtkWidget *widget,
                                                         gint *minimum,
                                                         gint *natural);
static void  get_preferred_height                       (GtkWidget *widget,
                                                         gint *minimum,
                                                         gint *natural);
static void  get_preferred_width_for_height             (GtkWidget *widget,
                                                         gint for_size,
                                                         gint *minimum_size,
                                                         gint *natural_size);
static void  get_preferred_height_for_width             (GtkWidget *widget,
                                                         gint for_size,
                                                         gint *minimum_size,
                                                         gint *natural_size);
static void  get_preferred_height_and_baseline_for_width(GtkWidget *widget,
                                                         gint width,
                                                         gint *minimum_size,
                                                         gint *natural_size,
                                                         gint *minimum_baseline,
                                                         gint *natural_baseline);
static GType container_child_type                       (GtkContainer *container);

static GtkBinClass *parent_class = NULL;

G_DEFINE_TYPE_WITH_CODE(GwyShrinkWrap, gwy_shrink_wrap, GTK_TYPE_BIN,
                        G_ADD_PRIVATE(GwyShrinkWrap))

static void
gwy_shrink_wrap_class_init(GwyShrinkWrapClass *klass)
{
    //GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
    GtkContainerClass *container_class = GTK_CONTAINER_CLASS(klass);

    parent_class = gwy_shrink_wrap_parent_class;

    widget_class->get_preferred_width = get_preferred_width;
    widget_class->get_preferred_height = get_preferred_height;
    widget_class->get_preferred_width_for_height = get_preferred_width_for_height;
    widget_class->get_preferred_height_for_width = get_preferred_height_for_width;
    widget_class->get_preferred_height_and_baseline_for_width = get_preferred_height_and_baseline_for_width;

    container_class->child_type = container_child_type;
    gtk_container_class_handle_border_width(container_class);
}

static void
gwy_shrink_wrap_init(GwyShrinkWrap *shrinkwrap)
{
    GwyShrinkWrapPrivate *priv;

    priv = shrinkwrap->priv = gwy_shrink_wrap_get_instance_private(shrinkwrap);
    priv->mode = GTK_SIZE_GROUP_NONE;
}

/**
 * gwy_shrink_wrap_new:
 * @mode: In which directions the shrinking should occur.
 *
 * Creates a new shrink-wrap container.
 *
 * The mode is given as #GtkSizeGroupMode because it has the same logic.
 *
 * Returns: The new shrink-wrap widget.
 **/
GtkWidget*
gwy_shrink_wrap_new(GtkSizeGroupMode mode)
{
    GtkWidget *widget = gtk_widget_new(GWY_TYPE_SHRINK_WRAP, NULL);
    GWY_SHRINK_WRAP(widget)->priv->mode = mode;
    return widget;
}

/**
 * gwy_shrink_wrap_set_mode:
 * @shrinkwrap: A shrink-wrap container.
 * @mode: In which directions the shrinking should occur.
 *
 * Sets the direction in which a shrink-wrap container shrinks its child.
 **/
void
gwy_shrink_wrap_set_mode(GwyShrinkWrap *shrinkwrap,
                         GtkSizeGroupMode mode)
{
    g_return_if_fail(GWY_IS_SHRINK_WRAP(shrinkwrap));
    g_return_if_fail(mode == GTK_SIZE_GROUP_NONE
                     || mode == GTK_SIZE_GROUP_HORIZONTAL
                     || mode == GTK_SIZE_GROUP_VERTICAL
                     || mode == GTK_SIZE_GROUP_BOTH);

    GwyShrinkWrapPrivate *priv = shrinkwrap->priv;
    if (mode == priv->mode || !gtk_bin_get_child(GTK_BIN(shrinkwrap)))
        return;
    priv->mode = mode;
    gtk_widget_queue_resize(GTK_WIDGET(shrinkwrap));
}

/**
 * gwy_shrink_wrap_get_mode:
 * @shrinkwrap: A shrink-wrap container.
 *
 * Reports the direction in which a shrink-wrap container shrinks its child.
 *
 * Returns: The shrinking mode.
 **/
GtkSizeGroupMode
gwy_shrink_wrap_get_mode(GwyShrinkWrap *shrinkwrap)
{
    g_return_val_if_fail(GWY_IS_SHRINK_WRAP(shrinkwrap), GTK_SIZE_GROUP_NONE);
    return shrinkwrap->priv->mode;
}


static void
get_preferred_width(GtkWidget *widget,
                    gint *minimum, gint *natural)
{
    GwyShrinkWrapPrivate *priv = GWY_SHRINK_WRAP(widget)->priv;
    *minimum = *natural = 0;

    GtkWidget *child = gtk_bin_get_child(GTK_BIN(widget));
    if (!child)
        return;

    gtk_widget_get_preferred_width(child, minimum, natural);
    if (priv->mode == GTK_SIZE_GROUP_HORIZONTAL || priv->mode == GTK_SIZE_GROUP_BOTH)
        *natural = *minimum = MIN(*natural, *minimum);
}

static void
get_preferred_height(GtkWidget *widget,
                     gint *minimum, gint *natural)
{
    GwyShrinkWrapPrivate *priv = GWY_SHRINK_WRAP(widget)->priv;
    *minimum = *natural = 0;

    GtkWidget *child = gtk_bin_get_child(GTK_BIN(widget));
    if (!child)
        return;

    gtk_widget_get_preferred_height(child, minimum, natural);
    if (priv->mode == GTK_SIZE_GROUP_VERTICAL || priv->mode == GTK_SIZE_GROUP_BOTH)
        *natural = *minimum = MIN(*natural, *minimum);
}

static void
get_preferred_width_for_height(GtkWidget *widget,
                               gint for_size,
                               gint *minimum, gint *natural)
{
    GwyShrinkWrapPrivate *priv = GWY_SHRINK_WRAP(widget)->priv;
    *minimum = *natural = 0;

    GtkWidget *child = gtk_bin_get_child(GTK_BIN(widget));
    if (!child)
        return;

    gtk_widget_get_preferred_width_for_height(child, for_size, minimum, natural);
    if (priv->mode == GTK_SIZE_GROUP_HORIZONTAL || priv->mode == GTK_SIZE_GROUP_BOTH)
        *natural = *minimum = MIN(*natural, *minimum);
}

static void
get_preferred_height_for_width(GtkWidget *widget,
                               gint for_size,
                               gint *minimum, gint *natural)
{
    GwyShrinkWrapPrivate *priv = GWY_SHRINK_WRAP(widget)->priv;
    *minimum = *natural = 0;

    GtkWidget *child = gtk_bin_get_child(GTK_BIN(widget));
    if (!child)
        return;

    gtk_widget_get_preferred_height_for_width(child, for_size, minimum, natural);
    if (priv->mode == GTK_SIZE_GROUP_VERTICAL || priv->mode == GTK_SIZE_GROUP_BOTH)
        *natural = *minimum = MIN(*natural, *minimum);
}

static void
get_preferred_height_and_baseline_for_width(GtkWidget *widget,
                                            gint width,
                                            gint *minimum_size, gint *natural_size,
                                            gint *minimum_baseline, gint *natural_baseline)
{
    GwyShrinkWrapPrivate *priv = GWY_SHRINK_WRAP(widget)->priv;
    *minimum_size = *natural_size = *minimum_baseline = *natural_baseline = 0;

    GtkWidget *child = gtk_bin_get_child(GTK_BIN(widget));
    if (!child)
        return;

    // This is probably whatever. If the child tries any real width for height trading, it cannot be so easily
    // shrinked, but then it also should not be put into a ShrinkWrap in the first place.
    gtk_widget_get_preferred_height_and_baseline_for_width(child, width,
                                                           minimum_size, natural_size,
                                                           minimum_baseline, natural_baseline);
    if (priv->mode == GTK_SIZE_GROUP_VERTICAL || priv->mode == GTK_SIZE_GROUP_BOTH) {
        *natural_size = *minimum_size = MIN(*natural_size, *minimum_size);
        *natural_baseline = *minimum_baseline;
    }
}

static GType
container_child_type(GtkContainer *container)
{
    if (gtk_bin_get_child(GTK_BIN(container)))
        return G_TYPE_NONE;
    return GTK_TYPE_WIDGET;
}

/**
 * SECTION: shrink-wrap
 * @title: GwyShrinkWrap
 * @short_description: Container shrinking natural sizes to minimum
 *
 * #GwyShrinkWrap is a simple wrapper container widget which requests its child's minimum size as the natural size,
 * essentially making it never request more than the minimum size. It is mainly useful for corner cases such as
 * ellipsised labels, which have small minimum size but the natural size corresponds to the full text. This causes
 * containers to give them a lot of space they do not need and can also make default window sizes unnecessarily large.
 *
 * Child widgets that trade width for height may not behave reasonably inside because the shrinking breaks the
 * trading.
 **/

/* vim: set cin columns=120 tw=118 et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */
