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
|
// 7 april 2015
#include "uipriv_unix.h"
struct boxChild {
uiControl *c;
int stretchy;
gboolean oldhexpand;
GtkAlign oldhalign;
gboolean oldvexpand;
GtkAlign oldvalign;
};
struct uiBox {
uiUnixControl c;
GtkWidget *widget;
GtkContainer *container;
GtkBox *box;
GArray *controls;
int vertical;
int padded;
GtkSizeGroup *stretchygroup; // ensures all stretchy controls have the same size
};
uiUnixControlAllDefaultsExceptDestroy(uiBox)
#define ctrl(b, i) &g_array_index(b->controls, struct boxChild, i)
static void uiBoxDestroy(uiControl *c)
{
uiBox *b = uiBox(c);
struct boxChild *bc;
guint i;
// kill the size group
g_object_unref(b->stretchygroup);
// free all controls
for (i = 0; i < b->controls->len; i++) {
bc = ctrl(b, i);
uiControlSetParent(bc->c, NULL);
// and make sure the widget itself stays alive
uiUnixControlSetContainer(uiUnixControl(bc->c), b->container, TRUE);
uiControlDestroy(bc->c);
}
g_array_free(b->controls, TRUE);
// and then ourselves
g_object_unref(b->widget);
uiFreeControl(uiControl(b));
}
void uiBoxAppend(uiBox *b, uiControl *c, int stretchy)
{
struct boxChild bc;
GtkWidget *widget;
bc.c = c;
bc.stretchy = stretchy;
widget = GTK_WIDGET(uiControlHandle(bc.c));
bc.oldhexpand = gtk_widget_get_hexpand(widget);
bc.oldhalign = gtk_widget_get_halign(widget);
bc.oldvexpand = gtk_widget_get_vexpand(widget);
bc.oldvalign = gtk_widget_get_valign(widget);
if (bc.stretchy) {
if (b->vertical) {
gtk_widget_set_vexpand(widget, TRUE);
gtk_widget_set_valign(widget, GTK_ALIGN_FILL);
} else {
gtk_widget_set_hexpand(widget, TRUE);
gtk_widget_set_halign(widget, GTK_ALIGN_FILL);
}
gtk_size_group_add_widget(b->stretchygroup, widget);
} else
if (b->vertical)
gtk_widget_set_vexpand(widget, FALSE);
else
gtk_widget_set_hexpand(widget, FALSE);
// and make them fill the opposite direction
if (b->vertical) {
gtk_widget_set_hexpand(widget, TRUE);
gtk_widget_set_halign(widget, GTK_ALIGN_FILL);
} else {
gtk_widget_set_vexpand(widget, TRUE);
gtk_widget_set_valign(widget, GTK_ALIGN_FILL);
}
uiControlSetParent(bc.c, uiControl(b));
uiUnixControlSetContainer(uiUnixControl(bc.c), b->container, FALSE);
g_array_append_val(b->controls, bc);
}
void uiBoxDelete(uiBox *b, int index)
{
struct boxChild *bc;
GtkWidget *widget;
bc = ctrl(b, index);
widget = GTK_WIDGET(uiControlHandle(bc->c));
uiControlSetParent(bc->c, NULL);
uiUnixControlSetContainer(uiUnixControl(bc->c), b->container, TRUE);
if (bc->stretchy)
gtk_size_group_remove_widget(b->stretchygroup, widget);
gtk_widget_set_hexpand(widget, bc->oldhexpand);
gtk_widget_set_halign(widget, bc->oldhalign);
gtk_widget_set_vexpand(widget, bc->oldvexpand);
gtk_widget_set_valign(widget, bc->oldvalign);
g_array_remove_index(b->controls, index);
}
int uiBoxPadded(uiBox *b)
{
return b->padded;
}
void uiBoxSetPadded(uiBox *b, int padded)
{
b->padded = padded;
if (b->padded)
if (b->vertical)
gtk_box_set_spacing(b->box, gtkYPadding);
else
gtk_box_set_spacing(b->box, gtkXPadding);
else
gtk_box_set_spacing(b->box, 0);
}
static uiBox *finishNewBox(GtkOrientation orientation)
{
uiBox *b;
uiUnixNewControl(uiBox, b);
b->widget = gtk_box_new(orientation, 0);
b->container = GTK_CONTAINER(b->widget);
b->box = GTK_BOX(b->widget);
b->vertical = orientation == GTK_ORIENTATION_VERTICAL;
if (b->vertical)
b->stretchygroup = gtk_size_group_new(GTK_SIZE_GROUP_VERTICAL);
else
b->stretchygroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
b->controls = g_array_new(FALSE, TRUE, sizeof (struct boxChild));
return b;
}
uiBox *uiNewHorizontalBox(void)
{
return finishNewBox(GTK_ORIENTATION_HORIZONTAL);
}
uiBox *uiNewVerticalBox(void)
{
return finishNewBox(GTK_ORIENTATION_VERTICAL);
}
|