#include "saffron_layout.h" #include "saffron_api.h" #include "saffron_widget.h" #include #include #include #include #include #include SaffronBox* saffron_box_new(SaffronOrientation orientation, SaffronHorizontalAlignment halign, SaffronVerticalAlignment valign, int spacing, int padding, int margin, int width, int height) { SaffronBox* box = malloc(sizeof(SaffronBox)); if (!box) return NULL; saffron_widget_init((SaffronWidget*)box); ((SaffronWidget*)box)->type = SAFFRON_WIDGET_BOX; box->orientation = orientation; box->halign = halign; box->valign = valign; box->spacing = spacing; box->padding = padding; box->margin = margin; return box; } void saffron_box_layout(SaffronBox* box) { if (!box || ((SaffronWidget*)box)->child_count == 0) return; int content_x = ((SaffronWidget*)box)->x + box->margin; int content_y = ((SaffronWidget*)box)->y + box->margin; int content_w = ((SaffronWidget*)box)->w - (box->margin * 2); int content_h = ((SaffronWidget*)box)->h - (box->margin * 2); int inner_x = content_x + box->padding; int inner_y = content_y + box->padding; int inner_w = content_w - (box->padding * 2); int inner_h = content_h - (box->padding * 2); int x_offset = inner_x; int y_offset = inner_y; /* TODO make it account for stretch and stuff */ for (int i = 0; i < ((SaffronWidget*)box)->child_count; i++) { SaffronWidget* child = ((SaffronWidget*)box)->children[i]; if (child->pixel_perfect) continue; if (box->orientation == SAFFRON_ORIENTATION_HORIZONTAL) { child->x = x_offset; child->y = y_offset; x_offset += child->w + box->spacing; } else { child->x = x_offset; child->y = y_offset; y_offset += child->h + box->spacing; } if (child->type == SAFFRON_WIDGET_BOX) { saffron_box_layout((SaffronBox*)child); } } return; }