nautilus-container-max-width.c 9.63 KB
Newer Older
1 2
#include "nautilus-container-max-width.h"

3 4
#define DEFAULT_MAX_SIZE 120

5 6 7 8
struct _NautilusContainerMaxWidth
{
    GtkBin parent_instance;
    guint max_width;
9 10 11

    gboolean width_maximized;
    guint change_width_maximized_idle_id;
12 13 14 15 16 17 18 19
};

G_DEFINE_TYPE (NautilusContainerMaxWidth, nautilus_container_max_width, GTK_TYPE_BIN)

enum
{
    PROP_0,
    PROP_MAX_WIDTH,
20
    PROP_WIDTH_MAXIMIZED,
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    N_PROPS
};

void
nautilus_container_max_width_set_max_width (NautilusContainerMaxWidth *self,
                                            guint                      max_width)
{
    self->max_width = max_width;
    gtk_widget_queue_allocate (GTK_WIDGET (self));
}

guint
nautilus_container_max_width_get_max_width (NautilusContainerMaxWidth *self)
{
    return self->max_width;
}

38 39 40 41 42 43
gboolean
nautilus_container_max_width_get_width_maximized (NautilusContainerMaxWidth *self)
{
    return self->width_maximized;
}

44 45 46 47 48 49 50 51 52
NautilusContainerMaxWidth *
nautilus_container_max_width_new (void)
{
    return g_object_new (NAUTILUS_TYPE_CONTAINER_MAX_WIDTH, NULL);
}

static void
nautilus_container_max_width_finalize (GObject *object)
{
53 54 55 56 57 58 59
    NautilusContainerMaxWidth *self = NAUTILUS_CONTAINER_MAX_WIDTH (object);

    if (self->change_width_maximized_idle_id != 0)
    {
        g_source_remove (self->change_width_maximized_idle_id);
    }

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    G_OBJECT_CLASS (nautilus_container_max_width_parent_class)->finalize (object);
}

static void
nautilus_container_max_width_get_property (GObject    *object,
                                           guint       prop_id,
                                           GValue     *value,
                                           GParamSpec *pspec)
{
    NautilusContainerMaxWidth *self = NAUTILUS_CONTAINER_MAX_WIDTH (object);

    switch (prop_id)
    {
        case PROP_MAX_WIDTH:
        {
            g_value_set_int (value, self->max_width);
        }
77
        break;
78

79 80 81 82 83 84
        case PROP_WIDTH_MAXIMIZED:
        {
            g_value_set_boolean (value, self->width_maximized);
        }
        break;

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        default:
        {
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
        break;
    }
}

static void
nautilus_container_max_width_set_property (GObject      *object,
                                           guint         prop_id,
                                           const GValue *value,
                                           GParamSpec   *pspec)
{
    NautilusContainerMaxWidth *self = NAUTILUS_CONTAINER_MAX_WIDTH (object);

    switch (prop_id)
    {
        case PROP_MAX_WIDTH:
        {
            nautilus_container_max_width_set_max_width (self, g_value_get_int (value));
        }
        break;

        default:
        {
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
        break;
    }
}

static void
get_preferred_width (GtkWidget *widget,
                     gint      *minimum_size,
                     gint      *natural_size)
{
    GtkWidget *child;
    NautilusContainerMaxWidth *self;
    GtkStyleContext *style_context;
    GtkBorder padding;

    self = NAUTILUS_CONTAINER_MAX_WIDTH (widget);
    child = gtk_bin_get_child (GTK_BIN (self));

    *natural_size = 0;
    *minimum_size = 0;
    gtk_widget_get_preferred_width (child, minimum_size, natural_size);

134 135 136 137 138
    if (self->max_width != -1 && *minimum_size > self->max_width)
    {
        g_critical ("NautilusContainerMaxWidth's child requested %d while set maximum width is %d",
                    *minimum_size, self->max_width);
    }
139 140 141
    *natural_size = self->max_width == -1 ? *natural_size :
                    MAX (*minimum_size, MIN (self->max_width, *natural_size));

142
    style_context = gtk_widget_get_style_context (widget);
143
    gtk_style_context_get_padding (style_context,
144
                                   gtk_widget_get_state_flags (widget),
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
                                   &padding);
    *minimum_size += padding.left + padding.right;
    *natural_size += padding.left + padding.right;
}

static void
get_preferred_height (GtkWidget *widget,
                      gint      *minimum_size,
                      gint      *natural_size)
{
    GtkWidget *child;
    NautilusContainerMaxWidth *self;
    gint minimum_width = 0;
    gint natural_width = 0;
    GtkStyleContext *style_context;
    GtkBorder padding;

    self = NAUTILUS_CONTAINER_MAX_WIDTH (widget);
    child = gtk_bin_get_child (GTK_BIN (self));

    get_preferred_width (widget, &minimum_width, &natural_width);
    natural_width = self->max_width == -1 ? natural_width : MIN (self->max_width, natural_width);

    gtk_widget_get_preferred_height_for_width (child, natural_width, minimum_size, natural_size);

170
    style_context = gtk_widget_get_style_context (widget);
171
    gtk_style_context_get_padding (style_context,
172
                                   gtk_widget_get_state_flags (widget),
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
                                   &padding);
    *minimum_size += padding.top + padding.bottom;
    *natural_size += padding.top + padding.bottom;
}

static void
get_preferred_height_for_width (GtkWidget *widget,
                                gint       width,
                                gint      *minimum_size,
                                gint      *natural_size)
{
    get_preferred_height (widget, minimum_size, natural_size);
}

static void
size_allocate (GtkWidget     *widget,
               GtkAllocation *allocation)
{
    GTK_WIDGET_CLASS (nautilus_container_max_width_parent_class)->size_allocate (widget, allocation);
}

static void
get_preferred_width_for_height (GtkWidget *widget,
                                gint       height,
                                gint      *minimum_size,
                                gint      *natural_size)
{
    get_preferred_width (widget, minimum_size, natural_size);
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
static gboolean
change_width_maximized_idle_callback (gpointer userdata)
{
    NautilusContainerMaxWidth *self = userdata;

    self->change_width_maximized_idle_id = 0;

    self->width_maximized = !self->width_maximized;
    g_object_notify (G_OBJECT (self), "width-maximized");

    return G_SOURCE_REMOVE;
}

static void
on_size_allocate (GtkWidget    *widget,
                  GdkRectangle *allocation,
                  gpointer      userdata)
{
    NautilusContainerMaxWidth *self = NAUTILUS_CONTAINER_MAX_WIDTH (widget);
    gboolean is_width_maximized;

    is_width_maximized = self->max_width == -1 ? FALSE : allocation->width >= self->max_width;

    if (self->width_maximized != is_width_maximized)
    {
        /* The handlers of the "notify::width-maximized" signal may trigger
         * a reallocation, which shouldn't happen at this point because we are
         * still in size_allocate phase. So, change the property on idle*/
        if (self->change_width_maximized_idle_id == 0)
        {
            self->change_width_maximized_idle_id = g_idle_add (change_width_maximized_idle_callback, self);
        }
    }
    else if (self->change_width_maximized_idle_id != 0)
    {
        /* This was going to change self->width_maximized, let's cancel it. */
        g_source_remove (self->change_width_maximized_idle_id);
        self->change_width_maximized_idle_id = 0;
    }
}

244 245 246 247 248 249 250 251 252 253
static void
constructed (GObject *obj)
{
    NautilusContainerMaxWidth *self = NAUTILUS_CONTAINER_MAX_WIDTH (obj);

    G_OBJECT_CLASS (nautilus_container_max_width_parent_class)->constructed (obj);

    /* We want our parent to gives our preferred width */
    gtk_widget_set_halign (GTK_WIDGET (self), GTK_ALIGN_CENTER);
    self->max_width = -1;
254 255 256 257 258 259 260

    /* We want to know when the container has grown to its max width */
    self->width_maximized = FALSE;
    g_signal_connect (GTK_WIDGET (self),
                      "size-allocate",
                      G_CALLBACK (on_size_allocate),
                      NULL);
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
}

static void
nautilus_container_max_width_class_init (NautilusContainerMaxWidthClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

    object_class->finalize = nautilus_container_max_width_finalize;
    object_class->get_property = nautilus_container_max_width_get_property;
    object_class->set_property = nautilus_container_max_width_set_property;
    object_class->constructed = constructed;

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

    g_object_class_install_property (object_class,
                                     PROP_MAX_WIDTH,
                                     g_param_spec_int ("max-width",
                                                       "Max width",
                                                       "The max width of the container",
                                                       G_MININT,
                                                       G_MAXINT,
                                                       0,
                                                       G_PARAM_READWRITE));

290 291 292 293 294 295 296 297
    g_object_class_install_property (object_class,
                                     PROP_WIDTH_MAXIMIZED,
                                     g_param_spec_boolean ("width-maximized",
                                                           "Width maximized",
                                                           "Whether the container is at the max width",
                                                           FALSE,
                                                           G_PARAM_READABLE));
}
298 299 300 301
static void
nautilus_container_max_width_init (NautilusContainerMaxWidth *self)
{
}