ui/gtk/container.c

changeset 44
473954dc6b74
parent 32
e5f4d8af567e
child 45
ab71409644b0
equal deleted inserted replaced
43:ef01d2c90128 44:473954dc6b74
50 } 50 }
51 return 1; 51 return 1;
52 } 52 }
53 53
54 GtkWidget* ui_gtk_vbox_new(int spacing) { 54 GtkWidget* ui_gtk_vbox_new(int spacing) {
55 #ifdef UI_GTK3 55 #if GTK_MAJOR_VERSION >= 3
56 return gtk_box_new(GTK_ORIENTATION_VERTICAL, spacing); 56 return gtk_box_new(GTK_ORIENTATION_VERTICAL, spacing);
57 #else 57 #else
58 return gtk_vbox_new(FALSE, spacing); 58 return gtk_vbox_new(FALSE, spacing);
59 #endif 59 #endif
60 } 60 }
61 61
62 GtkWidget* ui_gtk_hbox_new(int spacing) { 62 GtkWidget* ui_gtk_hbox_new(int spacing) {
63 #ifdef UI_GTK3 63 #if GTK_MAJOR_VERSION >= 3
64 return gtk_box_new(GTK_ORIENTATION_HORIZONTAL, spacing); 64 return gtk_box_new(GTK_ORIENTATION_HORIZONTAL, spacing);
65 #else 65 #else
66 return gtk_hbox_new(FALSE, spacing); 66 return gtk_hbox_new(FALSE, spacing);
67 #endif 67 #endif
68 } 68 }
69 69
70 70
71 71
72 /* -------------------- Box Container -------------------- */ 72 /* -------------------- Box Container -------------------- */
73 UiContainer* ui_box_container(UiObject *obj, GtkWidget *box) { 73 UiContainer* ui_box_container(UiObject *obj, GtkWidget *box, UiSubContainerType type) {
74 UiBoxContainer *ct = cxCalloc( 74 UiBoxContainer *ct = cxCalloc(
75 obj->ctx->allocator, 75 obj->ctx->allocator,
76 1, 76 1,
77 sizeof(UiBoxContainer)); 77 sizeof(UiBoxContainer));
78 ct->container.widget = box; 78 ct->container.widget = box;
79 ct->container.add = ui_box_container_add; 79 ct->container.add = ui_box_container_add;
80 ct->type = type;
80 return (UiContainer*)ct; 81 return (UiContainer*)ct;
81 } 82 }
82 83
83 void ui_box_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) { 84 void ui_box_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) {
84 UiBoxContainer *bc = (UiBoxContainer*)ct; 85 UiBoxContainer *bc = (UiBoxContainer*)ct;
93 if(fill) { 94 if(fill) {
94 bc->has_fill = TRUE; 95 bc->has_fill = TRUE;
95 } 96 }
96 97
97 UiBool expand = fill; 98 UiBool expand = fill;
99 #if GTK_MAJOR_VERSION >= 4
100 gtk_box_append(GTK_BOX(ct->widget), widget);
101 GtkAlign align = expand ? GTK_ALIGN_FILL : GTK_ALIGN_START;
102 if(bc->type == UI_CONTAINER_VBOX) {
103 gtk_widget_set_valign(widget, align);
104 gtk_widget_set_vexpand(widget, expand);
105 gtk_widget_set_hexpand(widget, TRUE);
106 } else if(bc->type == UI_CONTAINER_HBOX) {
107 gtk_widget_set_halign(widget, align);
108 gtk_widget_set_hexpand(widget, expand);
109 gtk_widget_set_vexpand(widget, TRUE);
110 }
111
112 #else
98 gtk_box_pack_start(GTK_BOX(ct->widget), widget, expand, fill, 0); 113 gtk_box_pack_start(GTK_BOX(ct->widget), widget, expand, fill, 0);
114 #endif
99 115
100 ui_reset_layout(ct->layout); 116 ui_reset_layout(ct->layout);
101 ct->current = widget; 117 ct->current = widget;
102 } 118 }
103 119
106 obj->ctx->allocator, 122 obj->ctx->allocator,
107 1, 123 1,
108 sizeof(UiGridContainer)); 124 sizeof(UiGridContainer));
109 ct->container.widget = grid; 125 ct->container.widget = grid;
110 ct->container.add = ui_grid_container_add; 126 ct->container.add = ui_grid_container_add;
111 #ifdef UI_GTK2 127 UI_GTK_V2(ct->width = 0);
112 ct->width = 0; 128 UI_GTK_V2(ct->height = 1);
113 ct->height = 1;
114 #endif
115 return (UiContainer*)ct; 129 return (UiContainer*)ct;
116 } 130 }
117 131
118 #ifdef UI_GTK3 132 #if GTK_MAJOR_VERSION >= 3
119 void ui_grid_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) { 133 void ui_grid_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) {
120 UiGridContainer *grid = (UiGridContainer*)ct; 134 UiGridContainer *grid = (UiGridContainer*)ct;
121 135
122 if(ct->layout.newline) { 136 if(ct->layout.newline) {
123 grid->x = 0; 137 grid->x = 0;
132 } 146 }
133 if(ct->layout.vexpand != UI_LAYOUT_UNDEFINED) { 147 if(ct->layout.vexpand != UI_LAYOUT_UNDEFINED) {
134 vexpand = ct->layout.vexpand; 148 vexpand = ct->layout.vexpand;
135 } 149 }
136 150
137 if(hexpand) { 151 gtk_widget_set_hexpand(widget, hexpand);
138 gtk_widget_set_hexpand(widget, TRUE); 152 gtk_widget_set_vexpand(widget, vexpand);
139 }
140 if(vexpand) {
141 gtk_widget_set_vexpand(widget, TRUE);
142 }
143 153
144 int colspan = ct->layout.colspan > 0 ? ct->layout.colspan : 1; 154 int colspan = ct->layout.colspan > 0 ? ct->layout.colspan : 1;
145 int rowspan = ct->layout.rowspan > 0 ? ct->layout.rowspan : 1; 155 int rowspan = ct->layout.rowspan > 0 ? ct->layout.rowspan : 1;
146 156
147 gtk_grid_attach(GTK_GRID(ct->widget), widget, grid->x, grid->y, colspan, rowspan); 157 gtk_grid_attach(GTK_GRID(ct->widget), widget, grid->x, grid->y, colspan, rowspan);
200 return ct; 210 return ct;
201 } 211 }
202 212
203 void ui_scrolledwindow_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) { 213 void ui_scrolledwindow_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) {
204 // TODO: check if the widget implements GtkScrollable 214 // TODO: check if the widget implements GtkScrollable
205 #ifdef UI_GTK3 215 SCROLLEDWINDOW_SET_CHILD(ct->widget, widget);
206 gtk_container_add(GTK_CONTAINER(ct->widget), widget);
207 #else
208 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(ct->widget), widget);
209 #endif
210 ui_reset_layout(ct->layout); 216 ui_reset_layout(ct->layout);
211 ct->current = widget; 217 ct->current = widget;
212 } 218 }
213 219
214 UiContainer* ui_tabview_container(UiObject *obj, GtkWidget *tabview) { 220 UiContainer* ui_tabview_container(UiObject *obj, GtkWidget *tabview) {
233 239
234 240
235 241
236 static GtkWidget* box_set_margin(GtkWidget *box, int margin) { 242 static GtkWidget* box_set_margin(GtkWidget *box, int margin) {
237 GtkWidget *ret = box; 243 GtkWidget *ret = box;
238 #ifdef UI_GTK3 244 #if GTK_MAJOR_VERSION >= 3
239 #if GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION >= 12 245 #if GTK_MAJOR_VERSION * 1000 + GTK_MINOR_VERSION >= 3012
240 gtk_widget_set_margin_start(box, margin); 246 gtk_widget_set_margin_start(box, margin);
241 gtk_widget_set_margin_end(box, margin); 247 gtk_widget_set_margin_end(box, margin);
242 #else 248 #else
243 gtk_widget_set_margin_left(box, margin); 249 gtk_widget_set_margin_left(box, margin);
244 gtk_widget_set_margin_right(box, margin); 250 gtk_widget_set_margin_right(box, margin);
262 GtkWidget *box = type == UI_CONTAINER_VBOX ? ui_gtk_vbox_new(args.spacing) : ui_gtk_hbox_new(args.spacing); 268 GtkWidget *box = type == UI_CONTAINER_VBOX ? ui_gtk_vbox_new(args.spacing) : ui_gtk_hbox_new(args.spacing);
263 GtkWidget *widget = args.margin > 0 ? box_set_margin(box, args.margin) : box; 269 GtkWidget *widget = args.margin > 0 ? box_set_margin(box, args.margin) : box;
264 ct->add(ct, widget, TRUE); 270 ct->add(ct, widget, TRUE);
265 271
266 UiObject *newobj = uic_object_new(obj, box); 272 UiObject *newobj = uic_object_new(obj, box);
267 newobj->container = ui_box_container(obj, box); 273 newobj->container = ui_box_container(obj, box, type);
268 uic_obj_add(obj, newobj); 274 uic_obj_add(obj, newobj);
269 275
270 return widget; 276 return widget;
271 } 277 }
272 278
276 282
277 UIEXPORT UIWIDGET ui_hbox_create(UiObject *obj, UiContainerArgs args) { 283 UIEXPORT UIWIDGET ui_hbox_create(UiObject *obj, UiContainerArgs args) {
278 return ui_box_create(obj, args, UI_CONTAINER_HBOX); 284 return ui_box_create(obj, args, UI_CONTAINER_HBOX);
279 } 285 }
280 286
281 287 static GtkWidget* create_grid(int colspacing, int rowspacing) {
288 #if GTK_MAJOR_VERSION >= 3
289 GtkWidget *grid = gtk_grid_new();
290 gtk_grid_set_column_spacing(GTK_GRID(grid), colspacing);
291 gtk_grid_set_row_spacing(GTK_GRID(grid), rowspacing);
292 #else
293 GtkWidget *grid = gtk_table_new(1, 1, FALSE);
294 gtk_table_set_col_spacings(GTK_TABLE(grid), colspacing);
295 gtk_table_set_row_spacings(GTK_TABLE(grid), rowspacing);
296 #endif
297 return grid;
298 }
282 299
283 UIWIDGET ui_grid_create(UiObject *obj, UiContainerArgs args) { 300 UIWIDGET ui_grid_create(UiObject *obj, UiContainerArgs args) {
284 UiObject* current = uic_current_obj(obj); 301 UiObject* current = uic_current_obj(obj);
285 UI_APPLY_LAYOUT1(current, args); 302 UI_APPLY_LAYOUT1(current, args);
286 GtkWidget *widget; 303 GtkWidget *widget;
287 304
288 #ifdef UI_GTK3 305 GtkWidget *grid = create_grid(args.columnspacing, args.rowspacing);
289 GtkWidget *grid = gtk_grid_new(); 306 widget = box_set_margin(grid, args.margin);
290 gtk_grid_set_column_spacing(GTK_GRID(grid), args.columnspacing);
291 gtk_grid_set_row_spacing(GTK_GRID(grid), args.rowspacing);
292 #if GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION >= 12
293 gtk_widget_set_margin_start(grid, args.margin);
294 gtk_widget_set_margin_end(grid, args.margin);
295 #else
296 gtk_widget_set_margin_left(grid, args.margin);
297 gtk_widget_set_margin_right(grid, args.margin);
298 #endif
299 gtk_widget_set_margin_top(grid, args.margin);
300 gtk_widget_set_margin_bottom(grid, args.margin);
301
302 widget = grid;
303 #elif defined(UI_GTK2)
304 GtkWidget *grid = gtk_table_new(1, 1, FALSE);
305
306 gtk_table_set_col_spacings(GTK_TABLE(grid), columnspacing);
307 gtk_table_set_row_spacings(GTK_TABLE(grid), rowspacing);
308
309 if(margin > 0) {
310 GtkWidget *a = gtk_alignment_new(0.5, 0.5, 1, 1);
311 gtk_alignment_set_padding(GTK_ALIGNMENT(a), margin, margin, margin, margin);
312 gtk_container_add(GTK_CONTAINER(a), grid);
313 widget = a;
314 } else {
315 widget = grid;
316 }
317 #endif
318 current->container->add(current->container, widget, TRUE); 307 current->container->add(current->container, widget, TRUE);
319 308
320 UiObject *newobj = uic_object_new(obj, grid); 309 UiObject *newobj = uic_object_new(obj, grid);
321 newobj->container = ui_grid_container(obj, grid); 310 newobj->container = ui_grid_container(obj, grid);
322 uic_obj_add(obj, newobj); 311 uic_obj_add(obj, newobj);
327 316
328 UIWIDGET ui_scrolledwindow_create(UiObject* obj, UiFrameArgs args) { 317 UIWIDGET ui_scrolledwindow_create(UiObject* obj, UiFrameArgs args) {
329 UiObject* current = uic_current_obj(obj); 318 UiObject* current = uic_current_obj(obj);
330 UI_APPLY_LAYOUT1(current, args); 319 UI_APPLY_LAYOUT1(current, args);
331 320
332 GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL); 321 GtkWidget *sw = SCROLLEDWINDOW_NEW();
333 UiObject *newobj = uic_object_new(obj, sw); 322 UiObject *newobj = uic_object_new(obj, sw);
334 newobj->container = ui_scrolledwindow_container(obj, sw); 323 newobj->container = ui_scrolledwindow_container(obj, sw);
335 uic_obj_add(obj, newobj); 324 uic_obj_add(obj, newobj);
336 325
337 return sw; 326 return sw;
343 } 332 }
344 333
345 /* -------------------- Splitpane -------------------- */ 334 /* -------------------- Splitpane -------------------- */
346 335
347 static GtkWidget* create_paned(UiOrientation orientation) { 336 static GtkWidget* create_paned(UiOrientation orientation) {
348 #ifdef UI_GTK3 337 #if GTK_MAJOR_VERSION >= 3
349 switch(orientation) { 338 switch(orientation) {
350 case UI_HORIZONTAL: return gtk_paned_new(GTK_ORIENTATION_HORIZONTAL); 339 case UI_HORIZONTAL: return gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
351 case UI_VERTICAL: return gtk_paned_new(GTK_ORIENTATION_VERTICAL); 340 case UI_VERTICAL: return gtk_paned_new(GTK_ORIENTATION_VERTICAL);
352 } 341 }
353 #else 342 #else

mercurial