gridbox.cpp
algorithm
cstddef
memory
utility
vector
ftxui/dom/box_helper.hpp
ftxui/dom/elements.hpp
ftxui/dom/node.hpp
ftxui/dom/requirement.hpp
ftxui/screen/box.hpp
ftxui/dom/canvas.hpp
ftxui/dom/deprecated.hpp
ftxui/dom/direction.hpp
ftxui/dom/elements.hpp
ftxui/dom/flexbox_config.hpp
ftxui/dom/linear_gradient.hpp
ftxui/dom/node.hpp
ftxui/dom/requirement.hpp
ftxui/dom/selection.hpp
ftxui/screen/box.hpp
ftxui/screen/color.hpp
ftxui/screen/image.hpp
ftxui/screen/pixel.hpp
ftxui/screen/screen.hpp
ftxui/screen/string.hpp
ftxui/screen/terminal.hpp
ftxui/util/autoreset.hpp
ftxui/util/ref.hpp
ftxui/dom/box_helper.hpp
src/ftxui/dom/gridbox.cpp
algorithm
cstddef
cstdint
ftxui/dom/take_any_args.hpp
functional
memory
optional
sstream
string
unordered_map
utility
variant
vector
ftxui

    //Copyright2020ArthurSonzogni.Allrightsreserved.
    //UseofthissourcecodeisgovernedbytheMITlicensethatcanbefoundin
    //theLICENSEfile.
    #include<algorithm>//formax,min
    #include<cstddef>//forsize_t
    #include<memory>//for__shared_ptr_access,shared_ptr,make_shared,allocator_traits<>::value_type
    #include<utility>//formove
    #include<vector>//forvector,__alloc_traits<>::value_type

    #include"ftxui/dom/box_helper.hpp"//forElement,Compute
    #include"ftxui/dom/elements.hpp"//forElements,filler,Element,gridbox
    #include"ftxui/dom/node.hpp"//forNode
    #include"ftxui/dom/requirement.hpp"//forRequirement
    #include"ftxui/screen/box.hpp"//forBox

    namespaceftxui{
    classScreen;

    namespace{

    //AccumulatethevaluesofalistU[n]intov[n].Sothat:
    //V[0]=0;
    //V[n+1]=v[n]+U[n]
    //returnthesumofU[n].
    intIntegrate(std::vector<int>&elements){
    intaccu=0;
    for(auto&i:elements){
    constintold_accu=accu;
    accu+=i;
    i=old_accu;
    }
    returnaccu;
    }

    classGridBox:publicNode{
    public:
    explicitGridBox(std::vector<Elements>lines):lines_(std::move(lines)){
    y_size=static_cast<int>(lines_.size());
    for(constauto&line:lines_){
    x_size=std::max(x_size,int(line.size()));
    }

    //Fillinemptycells,incasetheuserdidnotusedtheAPIcorrectly:
    for(auto&line:lines_){
    while(line.size()<size_t(x_size)){
    line.push_back(filler());
    }
    }
    }

    voidComputeRequirement()override{
    requirement_=Requirement{};
    for(auto&line:lines_){
    for(auto&cell:line){
    cell->ComputeRequirement();
    }
    }

    //Computethesizeofeachcolumns/row.
    std::vector<int>size_x(x_size,0);
    std::vector<int>size_y(y_size,0);
    for(intx=0;x<x_size;++x){
    for(inty=0;y<y_size;++y){
    size_x[x]=std::max(size_x[x],lines_[y][x]->requirement().min_x);
    size_y[y]=std::max(size_y[y],lines_[y][x]->requirement().min_y);
    }
    }

    requirement_.min_x=Integrate(size_x);
    requirement_.min_y=Integrate(size_y);

    //Forwardthefocused/focusedchildstate:
    for(intx=0;x<x_size;++x){
    for(inty=0;y<y_size;++y){
    if(requirement_.focused.enabled||
    !lines_[y][x]->requirement().focused.enabled){
    continue;
    }
    requirement_.focused=lines_[y][x]->requirement().focused;
    requirement_.focused.box.Shift(size_x[x],size_y[y]);
    }
    }
    }

    voidSetBox(Boxbox)override{
    Node::SetBox(box);

    box_helper::Elementinit;
    init.min_size=0;
    init.flex_grow=1024;//NOLINT
    init.flex_shrink=1024;//NOLINT
    std::vector<box_helper::Element>elements_x(x_size,init);
    std::vector<box_helper::Element>elements_y(y_size,init);

    for(inty=0;y<y_size;++y){
    for(intx=0;x<x_size;++x){
    constauto&cell=lines_[y][x];
    constauto&requirement=cell->requirement();
    auto&e_x=elements_x[x];
    auto&e_y=elements_y[y];
    e_x.min_size=std::max(e_x.min_size,requirement.min_x);
    e_y.min_size=std::max(e_y.min_size,requirement.min_y);
    e_x.flex_grow=std::min(e_x.flex_grow,requirement.flex_grow_x);
    e_y.flex_grow=std::min(e_y.flex_grow,requirement.flex_grow_y);
    e_x.flex_shrink=std::min(e_x.flex_shrink,requirement.flex_shrink_x);
    e_y.flex_shrink=std::min(e_y.flex_shrink,requirement.flex_shrink_y);
    }
    }

    constinttarget_size_x=box.x_max-box.x_min+1;
    constinttarget_size_y=box.y_max-box.y_min+1;
    box_helper::Compute(&elements_x,target_size_x);
    box_helper::Compute(&elements_y,target_size_y);

    Boxbox_y=box;
    inty=box_y.y_min;
    for(intiy=0;iy<y_size;++iy){
    box_y.y_min=y;
    y+=elements_y[iy].size;
    box_y.y_max=y-1;

    Boxbox_x=box_y;
    intx=box_x.x_min;
    for(intix=0;ix<x_size;++ix){
    box_x.x_min=x;
    x+=elements_x[ix].size;
    box_x.x_max=x-1;
    lines_[iy][ix]->SetBox(box_x);
    }
    }
    }

    voidRender(Screen&screen)override{
    for(auto&line:lines_){
    for(auto&cell:line){
    cell->Render(screen);
    }
    }
    }

    intx_size=0;
    inty_size=0;
    std::vector<Elements>lines_;
    };
    }//namespace
    //
    ///@briefAcontainerdisplayingagridofelements.
    ///@paramlinesAlistoflines,eachlinebeingalistofelements.
    ///@returnThecontainer.
    ///
    ///####Example
    ///
    ///```cpp
    ///autocell=[](constchar*t){returntext(t)|border;};
    ///autodocument=gridbox({
    ///{cell("north-west"),cell("north"),cell("north-east")},
    ///{cell("west"),cell("center"),cell("east")},
    ///{cell("south-west"),cell("south"),cell("south-east")},
    ///});
    ///```
    ///Output:
    ///```
    ///╭──────────╮╭──────╮╭──────────╮
    ///│north-west││north││north-east│
    ///╰──────────╯╰──────╯╰──────────╯
    ///╭──────────╮╭──────╮╭──────────╮
    ///│west││center││east│
    ///╰──────────╯╰──────╯╰──────────╯
    ///╭──────────╮╭──────╮╭──────────╮
    ///│south-west││south││south-east│
    ///╰──────────╯╰──────╯╰──────────╯
    ///```
    Elementgridbox(std::vector<Elements>lines){
    returnstd::make_shared<GridBox>(std::move(lines));
    }

    }//namespaceftxui