summaryrefslogtreecommitdiff
path: root/csrc/widgets/limit-container.c
blob: 123acefff95085631af8dc834de71dfa4a1bb4dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
134
135
136
137
138
139
140
141
142
143
144
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
170
171
172
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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "widgets/limit-container.h"

//
// FoobarLimitContainer:
//
// A container which uses the natural size requested by the child as its minimum size, but only up to a certain point.
//
// This is useful because gtk_scrolled_window_set_max_content_height doesn't seem to be doing what I understand it to
// do. This way, we can set propagate_natural_height to TRUE and then limit the height through this container.
//

struct _FoobarLimitContainer
{
	GtkWidget  parent_instance;
	GtkWidget* child;
	gint       max_width;
	gint       max_height;
};

enum
{
	PROP_CHILD = 1,
	PROP_MAX_WIDTH,
	PROP_MAX_HEIGHT,
	N_PROPS,
};

static GParamSpec* props[N_PROPS] = { 0 };

static void               foobar_limit_container_class_init      ( FoobarLimitContainerClass* klass );
static void               foobar_limit_container_init            ( FoobarLimitContainer*      self );
static void               foobar_limit_container_get_property    ( GObject*                   object,
                                                                   guint                      prop_id,
                                                                   GValue*                    value,
                                                                   GParamSpec*                pspec );
static void               foobar_limit_container_set_property    ( GObject*                   object,
                                                                   guint                      prop_id,
                                                                   GValue const*              value,
                                                                   GParamSpec*                pspec );
static void               foobar_limit_container_dispose         ( GObject*                   object );
static GtkSizeRequestMode foobar_limit_container_get_request_mode( GtkWidget*                 widget );
static void               foobar_limit_container_measure         ( GtkWidget*                 widget,
                                                                   GtkOrientation             orientation,
                                                                   int                        for_size,
                                                                   int*                       minimum,
                                                                   int*                       natural,
                                                                   int*                       minimum_baseline,
                                                                   int*                       natural_baseline );
static void               foobar_limit_container_size_allocate   ( GtkWidget*                 widget,
                                                                   int                        width,
                                                                   int                        height,
                                                                   int                        baseline );

G_DEFINE_FINAL_TYPE( FoobarLimitContainer, foobar_limit_container, GTK_TYPE_WIDGET )

// ---------------------------------------------------------------------------------------------------------------------
// Widget Implementation
// ---------------------------------------------------------------------------------------------------------------------

//
// Static initialization for limit containers.
//
void foobar_limit_container_class_init( FoobarLimitContainerClass* klass )
{
	GtkWidgetClass* widget_klass = GTK_WIDGET_CLASS( klass );
	widget_klass->get_request_mode = foobar_limit_container_get_request_mode;
	widget_klass->measure = foobar_limit_container_measure;
	widget_klass->size_allocate = foobar_limit_container_size_allocate;
	gtk_widget_class_set_css_name( widget_klass, "limit-container" );

	GObjectClass* object_klass = G_OBJECT_CLASS( klass );
	object_klass->get_property = foobar_limit_container_get_property;
	object_klass->set_property = foobar_limit_container_set_property;
	object_klass->dispose = foobar_limit_container_dispose;

	props[PROP_CHILD] = g_param_spec_object(
		"child",
		"Child",
		"Child widget for the panel item.",
		GTK_TYPE_WIDGET,
		G_PARAM_READWRITE );
	props[PROP_MAX_WIDTH] = g_param_spec_int(
		"max-width",
		"Max Width",
		"The maximum natural width supported as a minimum size.",
		0,
		INT_MAX,
		0,
		G_PARAM_READWRITE );
	props[PROP_MAX_HEIGHT] = g_param_spec_int(
		"max-height",
		"Max Height",
		"The maximum natural height supported as a minimum size.",
		0,
		INT_MAX,
		0,
		G_PARAM_READWRITE );
	g_object_class_install_properties( object_klass, N_PROPS, props );
}

//
// Instance initialization for limit containers.
//
void foobar_limit_container_init( FoobarLimitContainer* self )
{
	(void)self;
}

//
// Property getter implementation, mapping a property id to a method.
//
void foobar_limit_container_get_property(
	GObject*    object,
	guint       prop_id,
	GValue*     value,
	GParamSpec* pspec )
{
	FoobarLimitContainer* self = (FoobarLimitContainer*)object;

	switch ( prop_id )
	{
		case PROP_CHILD:
			g_value_set_object( value, foobar_limit_container_get_child( self ) );
			break;
		case PROP_MAX_WIDTH:
			g_value_set_int( value, foobar_limit_container_get_max_width( self ) );
			break;
		case PROP_MAX_HEIGHT:
			g_value_set_int( value, foobar_limit_container_get_max_height( self ) );
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID( object, prop_id, pspec );
			break;
	}
}

//
// Property setter implementation, mapping a property id to a method.
//
void foobar_limit_container_set_property(
	GObject*      object,
	guint         prop_id,
	GValue const* value,
	GParamSpec*   pspec )
{
	FoobarLimitContainer* self = (FoobarLimitContainer*)object;

	switch ( prop_id )
	{
		case PROP_CHILD:
			foobar_limit_container_set_child( self, g_value_get_object( value ) );
			break;
		case PROP_MAX_WIDTH:
			foobar_limit_container_set_max_width( self, g_value_get_int( value ) );
			break;
		case PROP_MAX_HEIGHT:
			foobar_limit_container_set_max_height( self, g_value_get_int( value ) );
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID( object, prop_id, pspec );
			break;
	}
}

//
// Instance de-initialization for limit containers.
//
void foobar_limit_container_dispose( GObject* object )
{
	FoobarLimitContainer* self = (FoobarLimitContainer*)object;

	g_clear_pointer( &self->child, gtk_widget_unparent );

	G_OBJECT_CLASS( foobar_limit_container_parent_class )->dispose( object );
}

//
// Determine the size request mode used to measure the widget.
//
// This implementation just forwards the request mode from the child widget.
//
GtkSizeRequestMode foobar_limit_container_get_request_mode( GtkWidget* widget )
{
	FoobarLimitContainer* self = (FoobarLimitContainer*)widget;
	if ( !self->child ) { return GTK_SIZE_REQUEST_CONSTANT_SIZE; }

	return gtk_widget_get_request_mode( self->child );
}

//
// Compute the requested size of the widget.
//
void foobar_limit_container_measure(
	GtkWidget*     widget,
	GtkOrientation orientation,
	int            for_size,
	int*           minimum,
	int*           natural,
	int*           minimum_baseline,
	int*           natural_baseline )
{
	FoobarLimitContainer* self = (FoobarLimitContainer*)widget;
	if ( !self->child )
	{
		*minimum = 0;
		*natural = 0;
		*minimum_baseline = 0;
		*natural_baseline = 0;
		return;
	}

	gtk_widget_measure( self->child, orientation, for_size, minimum, natural, minimum_baseline, natural_baseline );
	gint natural_minimum = *natural;
	switch ( orientation )
	{
		case GTK_ORIENTATION_HORIZONTAL:
			natural_minimum = MIN( natural_minimum, self->max_width );
			break;
		case GTK_ORIENTATION_VERTICAL:
			natural_minimum = MIN( natural_minimum, self->max_height );
			break;
		default:
			g_warn_if_reached( );
			break;
	}
	*minimum = MAX( *minimum, natural_minimum );
}

//
// Arrange the child widget.
//
void foobar_limit_container_size_allocate(
	GtkWidget* widget,
	int        width,
	int        height,
	int        baseline )
{
	FoobarLimitContainer* self = (FoobarLimitContainer*)widget;
	if ( !self->child ) { return; }

	gtk_widget_allocate( self->child, width, height, baseline, NULL );
}

// ---------------------------------------------------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------------------------------------------------

//
// Create a new limit container instance.
//
GtkWidget* foobar_limit_container_new( void )
{
	return g_object_new( FOOBAR_TYPE_LIMIT_CONTAINER, NULL );
}

//
// Get the container's child widget.
//
GtkWidget* foobar_limit_container_get_child( FoobarLimitContainer* self )
{
	g_return_val_if_fail( FOOBAR_IS_LIMIT_CONTAINER( self ), NULL );
	return self->child;
}

//
// Get the maximum natural width of the child to use as the minimum width for this container.
//
gint foobar_limit_container_get_max_width( FoobarLimitContainer* self )
{
	g_return_val_if_fail( FOOBAR_IS_LIMIT_CONTAINER( self ), 0 );
	return self->max_width;
}

//
// Get the maximum natural height of the child to use as the minimum height for this container.
//
gint foobar_limit_container_get_max_height( FoobarLimitContainer* self )
{
	g_return_val_if_fail( FOOBAR_IS_LIMIT_CONTAINER( self ), 0 );
	return self->max_height;
}

//
// Update the container's child widget.
//
void foobar_limit_container_set_child(
	FoobarLimitContainer* self,
	GtkWidget*            child )
{
	g_return_if_fail( FOOBAR_IS_LIMIT_CONTAINER( self ) );
	g_return_if_fail( child == NULL || self->child == child || gtk_widget_get_parent( child ) == NULL );

	if ( self->child != child )
	{
		g_clear_pointer( &self->child, gtk_widget_unparent );

		if ( child )
		{
			self->child = child;
			gtk_widget_set_parent( child, GTK_WIDGET( self ) );
		}

		g_object_notify_by_pspec( G_OBJECT( self ), props[PROP_CHILD] );
	}
}

//
// Update the maximum natural width of the child to use as the minimum width for this container.
//
void foobar_limit_container_set_max_width(
	FoobarLimitContainer* self,
	gint                  value )
{
	g_return_if_fail( FOOBAR_IS_LIMIT_CONTAINER( self ) );
	g_return_if_fail( value >= 0 );

	if ( self->max_width != value )
	{
		self->max_width = value;
		gtk_widget_queue_resize( GTK_WIDGET( self ) );
		g_object_notify_by_pspec( G_OBJECT( self ), props[PROP_MAX_WIDTH] );
	}
}

//
// Update the maximum natural height of the child to use as the minimum height for this container.
//
void foobar_limit_container_set_max_height(
	FoobarLimitContainer* self,
	gint                  value )
{
	g_return_if_fail( FOOBAR_IS_LIMIT_CONTAINER( self ) );
	g_return_if_fail( value >= 0 );

	if ( self->max_height != value )
	{
		self->max_height = value;
		gtk_widget_queue_resize( GTK_WIDGET( self ) );
		g_object_notify_by_pspec( G_OBJECT( self ), props[PROP_MAX_HEIGHT] );
	}
}