File: GtkLayout.cpp

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (311 lines) | stat: -rw-r--r-- 8,988 bytes parent folder | download | duplicates (2)
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
#include "stdafx.h"
#include "GtkLayout.h"

#ifdef GUI_GTK


G_DEFINE_TYPE(Basic, basic, GTK_TYPE_CONTAINER)

enum {
	CHILD_PROP_0,
	CHILD_PROP_X,
	CHILD_PROP_Y,
	CHILD_PROP_W,
	CHILD_PROP_H,
};

static GType basic_child_type(GtkContainer *container) {
	return GTK_TYPE_WIDGET;
}

static BasicChild *get_child(Basic *me, GtkWidget *widget) {
	for (GList *at = me->children; at; at = at->next) {
		BasicChild *child = (BasicChild *)at->data;
		if (child->widget == widget)
			return child;
	}
	return null;
}

static void move(Basic *me, BasicChild *child, gint x, gint y, gint w, gint h) {
	gtk_widget_freeze_child_notify(child->widget);

	if (child->x != x)
		gtk_widget_child_notify(child->widget, "x");
	if (child->y != y)
		gtk_widget_child_notify(child->widget, "y");
	if (child->w != w)
		gtk_widget_child_notify(child->widget, "w");
	if (child->h != h)
		gtk_widget_child_notify(child->widget, "h");

	child->x = x;
	child->y = y;
	child->w = w;
	child->h = h;

	gtk_widget_thaw_child_notify(child->widget);
	// This breaks scrollbars in GtkScrolledWindow...
	// if (gtk_widget_get_visible(child->widget) && gtk_widget_get_visible(GTK_WIDGET(me)))
	// 	gtk_widget_queue_resize(GTK_WIDGET(me));
}

void basic_put(Basic *layout, GtkWidget *widget, gint x, gint y, gint w, gint h) {
	if (w < 0)
		w = 0;
	if (h < 0)
		h = 0;

	BasicChild *child = g_new(BasicChild, 1);
	child->widget = widget;
	child->x = x;
	child->y = y;
	child->w = w;
	child->h = h;

	gtk_widget_set_parent(widget, GTK_WIDGET(layout));
	layout->children = g_list_append(layout->children, child);
}

void basic_move(Basic *layout, GtkWidget *widget, gint x, gint y, gint w, gint h) {
	if (w < 0)
		w = 0;
	if (h < 0)
		h = 0;

	// Child coordinates seem to be relative to the same origin as the allocation of this widget.
	GtkAllocation allocation;
	gtk_widget_get_allocation(GTK_WIDGET(layout), &allocation);

	BasicChild *child = get_child(layout, widget);
	assert(child, L"No child info!");
	move(layout, child, x, y, w, h);

	// Update the allocation of the widget as well.
	GtkAllocation alloc;
	alloc.x = allocation.x + x;
	alloc.y = allocation.y + y;
	alloc.width = w;
	alloc.height = h;
	gtk_widget_size_allocate(widget, &alloc);
}

void basic_set_min_size(Basic *layout, gint width, gint height) {
	layout->min_width = width;
	layout->min_height = height;
}

static void basic_set_property(GtkContainer *here, GtkWidget *child, guint id, const GValue *value, GParamSpec *spec) {
	Basic *me = BASIC(here);
	BasicChild *info = get_child(me, child);
	assert(info, L"No child info!");

	switch (id) {
	case CHILD_PROP_X:
		move(me, info, g_value_get_int(value), info->y, info->w, info->h);
		break;
	case CHILD_PROP_Y:
		move(me, info, info->x, g_value_get_int(value), info->w, info->h);
		break;
	case CHILD_PROP_W:
		move(me, info, info->x, info->y, g_value_get_int(value), info->h);
		break;
	case CHILD_PROP_H:
		move(me, info, info->x, info->y, info->w, g_value_get_int(value));
		break;
	default:
		GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID(here, id, spec);
		break;
	}
}

static void basic_get_property(GtkContainer *here, GtkWidget *child, guint id, GValue *value, GParamSpec *spec) {
	BasicChild *info = get_child(BASIC(here), child);
	assert(info, L"No child info!");

	switch (id) {
	case CHILD_PROP_X:
		g_value_set_int(value, info->x);
		break;
	case CHILD_PROP_Y:
		g_value_set_int(value, info->y);
		break;
	case CHILD_PROP_W:
		g_value_set_int(value, info->w);
		break;
	case CHILD_PROP_H:
		g_value_set_int(value, info->h);
		break;
	default:
		GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID(here, id, spec);
		break;
	}
}

static void basic_preferred_width(GtkWidget *widget, int *minimum, int *natural) {
	Basic *me = BASIC(widget);

	*minimum = me->min_width;
	*natural = me->min_width;

	// for (GList *children = me->children; children; children = children->next) {
	// 	BasicChild *child = (BasicChild *)children->data;

	// 	if (!gtk_widget_get_visible(child->widget))
	// 		continue;

	// 	gint child_min, child_nat;
	// 	gtk_widget_get_preferred_width(child->widget, &child_min, &child_nat);

	// 	*minimum = max(*minimum, child->x + child_min);
	// 	*natural = max(*natural, child->x + child_nat);
	// }

	// // We report a minimum size of zero to allow the window to resize to any size. Any constraints
	// // are handled in Storm.
	// *minimum = 0;
}

static void basic_preferred_height(GtkWidget *widget, int *minimum, int *natural) {
	Basic *me = BASIC(widget);

	*minimum = me->min_height;
	*natural = me->min_height;

	// for (GList *children = me->children; children; children = children->next) {
	// 	BasicChild *child = (BasicChild *)children->data;

	// 	if (!gtk_widget_get_visible(child->widget))
	// 		continue;

	// 	gint child_min, child_nat;
	// 	gtk_widget_get_preferred_height(child->widget, &child_min, &child_nat);

	// 	*minimum = max(*minimum, child->y + child_min);
	// 	*natural = max(*natural, child->y + child_nat);
	// }

	// // We report a minimum size of zero to allow the window to resize to any size. Any constraints
	// // are handled in Storm.
	// *minimum = 0;
}

static void basic_size_allocate(GtkWidget *widget, GtkAllocation *allocation) {
	Basic *me = BASIC(widget);

	gtk_widget_set_allocation(widget, allocation);
	if (gtk_widget_get_has_window(widget) && gtk_widget_get_realized(widget)) {
		// Seems to never be called, but good to have just in case...
		gdk_window_move_resize(gtk_widget_get_window(widget),
							allocation->x, allocation->y,
							allocation->width, allocation->height);
	}

	for (GList *children = me->children; children; children = children->next) {
		BasicChild *child = (BasicChild *)children->data;

		if (!gtk_widget_get_visible(child->widget))
			continue;

		GtkAllocation alloc;
		alloc.x = allocation->x + child->x;
		alloc.y = allocation->y + child->y;
		alloc.width = child->w;
		alloc.height = child->h;

		gtk_widget_size_allocate(child->widget, &alloc);
	}
}

static void basic_add(GtkContainer *container, GtkWidget *widget) {
	basic_put(BASIC(container), widget, 0, 0, 100, 100);
}

static void basic_remove(GtkContainer *container, GtkWidget *widget) {
	Basic *me = BASIC(container);

	for (GList *children = me->children; children; children = children->next) {
		BasicChild *child = (BasicChild *)children->data;

		if (child->widget == widget) {
			gboolean was_visible = gtk_widget_get_visible(widget);
			gtk_widget_unparent(widget);

			me->children = g_list_remove_link(me->children, children);
			g_list_free(children);
			g_free(child);

			if (was_visible && gtk_widget_get_visible((GtkWidget *)me))
				gtk_widget_queue_resize((GtkWidget *)me);

			break;
		}
	}
}

static void basic_forall(GtkContainer *container, gboolean internals, GtkCallback callback, gpointer data) {
	Basic *me = BASIC(container);

	GList *children = me->children;
	while (children) {
		BasicChild *child = (BasicChild *)children->data;
		children = children->next;

		(*callback)(child->widget, data);
	}
}

static void basic_class_init(BasicClass *klass) {
	GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
	GtkContainerClass *container_class = (GtkContainerClass *)klass;

	widget_class->get_preferred_width = &basic_preferred_width;
	widget_class->get_preferred_height = &basic_preferred_height;
	widget_class->size_allocate = &basic_size_allocate;

	container_class->add = &basic_add;
	container_class->remove = &basic_remove;
	container_class->forall = &basic_forall;
	container_class->child_type = &basic_child_type;
	container_class->set_child_property = &basic_set_property;
	container_class->get_child_property = &basic_get_property;
	gtk_container_class_handle_border_width(container_class);

	gtk_container_class_install_child_property(container_class,
											CHILD_PROP_X,
											g_param_spec_int("x",
															"X position",
															"X position of child widget",
															G_MININT, G_MAXINT, 0, (GParamFlags)G_PARAM_READWRITE));
	gtk_container_class_install_child_property(container_class,
											CHILD_PROP_Y,
											g_param_spec_int("y",
															"Y position",
															"Y position of child widget",
															G_MININT, G_MAXINT, 0, (GParamFlags)G_PARAM_READWRITE));
	gtk_container_class_install_child_property(container_class,
											CHILD_PROP_W,
											g_param_spec_int("w",
															"Width",
															"Width of child widget",
															0, G_MAXINT, 0, (GParamFlags)G_PARAM_READWRITE));
	gtk_container_class_install_child_property(container_class,
											CHILD_PROP_H,
											g_param_spec_int("h",
															"Height",
															"Height of child widget",
															0, G_MAXINT, 0, (GParamFlags)G_PARAM_READWRITE));
}

static void basic_init(Basic *instance) {
	instance->children = NULL;
	instance->min_width = 0;
	instance->min_height = 0;
	gtk_widget_set_has_window(GTK_WIDGET(instance), FALSE);
}

GtkWidget *basic_new() {
	return (GtkWidget *)g_object_new(TYPE_BASIC, NULL);
}

#endif