| 1 | // Aseprite UI Library |
| 2 | // Copyright (C) 2001-2017 David Capello |
| 3 | // |
| 4 | // This file is released under the terms of the MIT license. |
| 5 | // Read LICENSE.txt for more information. |
| 6 | |
| 7 | #ifndef UI_GRID_H_INCLUDED |
| 8 | #define UI_GRID_H_INCLUDED |
| 9 | #pragma once |
| 10 | |
| 11 | #include "ui/widget.h" |
| 12 | |
| 13 | #include <vector> |
| 14 | |
| 15 | namespace ui { |
| 16 | |
| 17 | class Grid : public Widget { |
| 18 | public: |
| 19 | struct Info { |
| 20 | int col, row; |
| 21 | int hspan, vspan; |
| 22 | int grid_cols, grid_rows; |
| 23 | Info() : col(0), row(0), |
| 24 | hspan(0), vspan(0), |
| 25 | grid_cols(0), grid_rows(0) { |
| 26 | } |
| 27 | }; |
| 28 | |
| 29 | Grid(int columns, bool same_width_columns); |
| 30 | ~Grid(); |
| 31 | |
| 32 | void addChildInCell(Widget* child, int hspan, int vspan, int align); |
| 33 | Info getChildInfo(Widget* child); |
| 34 | |
| 35 | protected: |
| 36 | // Events |
| 37 | void onResize(ResizeEvent& ev) override; |
| 38 | void onSizeHint(SizeHintEvent& ev) override; |
| 39 | |
| 40 | private: |
| 41 | struct Cell { |
| 42 | Cell* parent; |
| 43 | Widget* child; |
| 44 | int hspan; |
| 45 | int vspan; |
| 46 | int align; |
| 47 | int w, h; |
| 48 | |
| 49 | Cell(); |
| 50 | }; |
| 51 | |
| 52 | struct Strip { |
| 53 | int size; |
| 54 | int expand_count; |
| 55 | }; |
| 56 | |
| 57 | void sumStripSize(const std::vector<Strip>& strip, int& size); |
| 58 | void calculateCellSize(int start, int span, const std::vector<Strip>& strip, int& size); |
| 59 | void calculateSize(); |
| 60 | void calculateStripSize(std::vector<Strip>& colstrip, |
| 61 | std::vector<Strip>& rowstrip, int align); |
| 62 | void expandStrip(std::vector<Strip>& colstrip, |
| 63 | std::vector<Strip>& rowstrip, |
| 64 | void (Grid::*incCol)(int, int)); |
| 65 | void distributeSize(const gfx::Rect& rect); |
| 66 | void distributeStripSize(std::vector<Strip>& colstrip, |
| 67 | int rect_size, int border_size, bool same_width); |
| 68 | bool putWidgetInCell(Widget* child, int hspan, int vspan, int align); |
| 69 | void expandRows(int rows); |
| 70 | void incColSize(int col, int size); |
| 71 | void incRowSize(int row, int size); |
| 72 | |
| 73 | bool m_same_width_columns; |
| 74 | std::vector<Strip> m_colstrip; |
| 75 | std::vector<Strip> m_rowstrip; |
| 76 | std::vector<std::vector<Cell*> > m_cells; |
| 77 | }; |
| 78 | |
| 79 | } // namespace ui |
| 80 | |
| 81 | #endif |
| 82 | |