void apply_format(WidgetGrid & grid, uint16_t * row_height, uint16_t * column_width) { uint16_t height = 0; for (uint16_t row_index = 0; row_index < grid.get_nb_rows(); row_index++) { grid.set_row_height(row_index, row_height[row_index]); height += row_height[row_index] + grid.border * 2; } grid.rect.cy = height; for (uint16_t column_index = 0; column_index < grid.nb_columns; column_index++) { grid.set_column_width(column_index, column_width[column_index]); } }
inline void compute_format(WidgetGrid & grid, ColumnWidthStrategy * column_width_strategies, uint16_t * row_height, uint16_t * column_width) { uint16_t column_width_optimal[GRID_NB_COLUMNS_MAX] = { 0 }; for (uint16_t row_index = 0; row_index < grid.get_nb_rows(); row_index++) { for (uint16_t column_index = 0; column_index < grid.nb_columns; column_index++) { Widget2 * w = grid.get_widget(row_index, column_index); if (!w) { continue; } Dimension dim = w->get_optimal_dim(); if (column_width_optimal[column_index] < dim.w) { column_width_optimal[column_index] = dim.w + 2; } if (row_height[row_index] < dim.h) { row_height[row_index] = dim.h; } } } // TODO Optiomize this uint16_t unsatisfied_column_count = 0; // min uint16_t unused_width = static_cast<int16_t>(grid.rect.cx - grid.border * 2 * grid.nb_columns); for (uint16_t column_index = 0; column_index < grid.nb_columns; column_index++) { column_width[column_index] = column_width_strategies[column_index].min; unused_width -= static_cast<int16_t>(column_width_strategies[column_index].min); if (column_width[column_index] < std::min(column_width_optimal[column_index], column_width_strategies[column_index].max)) { unsatisfied_column_count++; } } // optimal while ((unused_width > 0) && (unsatisfied_column_count > 0)) { const uint16_t part = unused_width / unsatisfied_column_count; if (!part) { break; } unsatisfied_column_count = 0; for (uint16_t column_index = 0; column_index < grid.nb_columns; column_index++) { uint16_t optimal_max = std::min(column_width_optimal[column_index], column_width_strategies[column_index].max); if (column_width[column_index] < optimal_max) { uint16_t ajusted_part = std::min<uint16_t>(part, optimal_max - column_width[column_index]); column_width[column_index] += ajusted_part; unused_width -= ajusted_part; if (column_width[column_index] < optimal_max) { unsatisfied_column_count++; } } } } // max unsatisfied_column_count = 0; for (uint16_t column_index = 0; column_index < grid.nb_columns; column_index++) { if (column_width[column_index] < column_width_strategies[column_index].max) { unsatisfied_column_count++; } } while ((unused_width > 0) && (unsatisfied_column_count > 0)) { const uint16_t part = unused_width / unsatisfied_column_count; if (!part) { break; } unsatisfied_column_count = 0; for (uint16_t column_index = 0; column_index < grid.nb_columns; column_index++) { if (column_width[column_index] < column_width_strategies[column_index].max) { uint16_t ajusted_part = std::min<uint16_t>(part, column_width_strategies[column_index].max - column_width[column_index]); column_width[column_index] += ajusted_part; unused_width -= ajusted_part; if (column_width[column_index] < column_width_strategies[column_index].max) { unsatisfied_column_count++; } } } } }