Minsky
grid.cc
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2021
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #undef CLASSDESC_ARITIES
21 #define CLASSDESC_ARITIES 0x3f
22 #include "cairoItems.h"
23 #include "grid.h"
24 #include "variablePane.h"
25 #include "lasso.h"
26 #include <pango.h>
27 #include "grid.rcd"
28 #include "grid.xcd"
29 #include "pango.rcd"
30 #include "pango.xcd"
31 #include "minsky_epilogue.h"
32 #include <algorithm>
33 
34 namespace minsky
35 {
36  namespace
37  {
38  constexpr double padx=2;
39  }
40 
41  template <class Cell>
43  {
44  bottomRowMargin.clear();
45  if (evenHeight())
46  {
47  const double rowHeight=cell(0,0).height();
48  for (unsigned i=0; i<numRows(); ++i)
49  bottomRowMargin.push_back((i+1)*rowHeight);
50  }
51  else
52  {
53  double y=0;
54  for (unsigned i=0; i<numRows(); ++i)
55  {
56  double rowHeight=0;
57  for (unsigned j=0; j<numCols(); ++j)
58  rowHeight=std::max(rowHeight, cell(i,j).height());
59  y+=rowHeight;
60  bottomRowMargin.push_back(y);
61  }
62  }
63  assert(bottomRowMargin.size()==numRows());
64 
65  rightColMargin.clear();
66  double x=0;
67  for (unsigned col=0; col<numCols(); ++col)
68  {
69  double colWidth=0, y=0;
70  switch (justification(col))
71  {
72  case right: case centre:
73  // work out column width
74  for (unsigned row=0; row<numRows(); ++row)
75  colWidth=std::max(colWidth,cell(row,col).width()+padx);
76  break;
77  default:
78  break;
79  }
80 
81 
82  for (unsigned row=0; row<numRows(); ++row)
83  {
84  auto& currentCell=cell(row,col);
85  double offset=0;
86  switch (justification(col))
87  {
88  case left: break;
89  case right: offset+=colWidth-currentCell.width(); break;
90  case centre: offset+=0.5*(colWidth-currentCell.width()); break;
91  }
92  moveCursorTo(x+offset+0.5*padx,y);
93  currentCell.show();
94  colWidth=std::max(colWidth, currentCell.width()+padx);
95  y=bottomRowMargin[row];
96  }
97  x+=colWidth;
98  rightColMargin.push_back(x);
99  }
100  }
101 
102  template <class Cell>
103  int Grid<Cell>::colX(double x) const
104  {
105  if (rightColMargin.empty() || x<0 || x>=rightColMargin.back())
106  return -1;
107  auto p=std::upper_bound(rightColMargin.begin(), rightColMargin.end(), x);
108  return p-rightColMargin.begin();
109  }
110 
111  template <class Cell>
112  int Grid<Cell>::rowY(double y) const
113  {
114  if (bottomRowMargin.empty() || y<0 || y>=bottomRowMargin.back())
115  return -1;
116  auto p=std::upper_bound(bottomRowMargin.begin(), bottomRowMargin.end(), y);
117  return p-bottomRowMargin.begin();
118  }
119 
120  template class Grid<ICell>;
121  template class Grid<ecolab::Pango>;
122  template class Grid<VariablePaneCell>;
123 
124 }
constexpr double padx
padding between cells
Definition: grid.cc:38
int colX(double x) const
column at x in unzoomed coordinates
Definition: grid.cc:103
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
int rowY(double y) const
row at y in unzoomed coordinates
Definition: grid.cc:112
CLASSDESC_ACCESS_EXPLICIT_INSTANTIATION(minsky::Grid< minsky::ICell >)
void draw()
draw the grid
Definition: grid.cc:42