WINDEX
table.h
1 namespace wex
2 {
3 
26  class table : public gui
27  {
28  public:
29  table(gui *parent)
30  : gui(parent),
31  myRowDisplayCount(25),
32  myRowStart(0),
33  myRowHiLit(-1)
34 
35  {
36  registerEventHandlers();
37  text("");
38  }
39 
45  void set(const std::vector<std::vector<std::string>> &val)
46  {
47  myContents = val;
48  }
55  void set(const std::vector<std::string> &val, int colcount)
56  {
57  std::vector<std::vector<std::string>> vvs;
58  for (
59  int row = 0;
60  row < val.size() / colcount;
61  row++)
62  {
63  std::vector<std::string> vrow;
64  for (int col = 0; col < colcount; col++)
65  {
66  // if (col == 0)
67  // vrow.push_back(
68  // val[row * colcount + col] + " " + val[row * colcount + col + 1]);
69  // else if (col == 1)
70  // continue;
71  // else
72  vrow.push_back(val[row * colcount + col]);
73  }
74  vvs.push_back(vrow);
75  }
76  set(vvs);
77  }
78 
81  void rowInc(int i)
82  {
83  myRowStart += i;
84  if (myRowStart < 0)
85  myRowStart = 0;
86  }
87 
88  void rowLastDisplay()
89  {
90  myRowStart = myContents.size() - 1;
91  }
92 
93  void rowHiLit( int k )
94  {
95  myRowHiLit = k;
96  }
97 
98  private:
99  std::vector<std::vector<std::string>> myContents;
100  std::vector<int> myRowID;
101  int myRowDisplayCount;
102  int myRowStart;
103  int myRowHiLit;
104 
105  void draw(shapes &S)
106  {
107  myRowID.clear();
108 
109  if (!myContents.size())
110  return;
111 
112  int colCount = myContents[0].size();
113  int colWidth;
114  if (colCount <= 1)
115  colWidth = size()[0];
116  else
117  colWidth = size()[0] / (colCount - 1);
118  int rowStop;
119  if (myContents.size() < 20)
120  {
121  myRowStart = 0;
122  rowStop = myContents.size();
123  }
124  else
125  {
126  if (myRowStart > myContents.size() - 20)
127  myRowStart = myContents.size() - 20;
128  rowStop = myRowStart + 30;
129  if (rowStop > myContents.size())
130  rowStop = myContents.size();
131  }
132  // std::cout << "row " << myRowStart << " to " << myRowStart + rowStop - 1
133  // << " from " << myContents.size() << "\n";
134  for (int kr = myRowStart; kr < rowStop; kr++)
135  {
136  if (myContents[kr].size() > colCount)
137  throw std::runtime_error(
138  "bad col count");
139 
140  if( kr == myRowHiLit )
141  S.color(0x0000FF);
142  else
143  S.color(0);
144 
145  for (int kc = 0; kc < colCount; kc++)
146  {
147  auto &val = myContents[kr][kc];
148  int x, w;
149  if (kc == 0)
150  {
151  x = 0;
152  w = 50;
153 
154  // save the database ID of the row
155  // so it can be sent if the row is clicked
156  // assumes first column contains the db id
157  myRowID.push_back(atoi(val.c_str()));
158  }
159  else
160  {
161  x = 50 + (kc - 1) * colWidth;
162  w = colWidth;
163  }
164  S.text(
165  val,
166  {x, (kr - myRowStart) * 20,
167  w - 5, 20});
168  int xline = x + w - 10;
169  S.line({xline, (kr - myRowStart) * 20,
170  xline, (kr - myRowStart + 1) * 20});
171  }
172  }
173  }
174 
175  void registerEventHandlers()
176  {
177  events().draw(
178  [&](PAINTSTRUCT &ps)
179  {
180  wex::shapes S(ps);
181  draw(S);
182  });
183 
184  events().click(
185  [this]
186  {
187  PostMessageA(
188  myParent->handle(),
189  wex::eventMsgID::asyncReadComplete,
190  myRowID[getMouseStatus().y / 20],
191  0);
192  });
193  }
194  };
195 }
void click(std::function< void(void)> f, bool propogate=false)
register click event handler
Definition: wex.h:280
The base class for all windex gui elements.
Definition: wex.h:900
std::vector< int > size()
Size of window client area.
Definition: wex.h:1719
eventhandler & events()
Get event handler.
Definition: wex.h:1742
HWND handle()
get window handle
Definition: wex.h:1748
sMouse getMouseStatus()
Get mouse status.
Definition: wex.h:1247
gui()
Construct top level window with no parent.
Definition: wex.h:907
A class that offers application code methods to draw on a window.
Definition: wex.h:529
A read only table of values.
Definition: table.h:27
void rowInc(int i)
Move display window.
Definition: table.h:81
void set(const std::vector< std::string > &val, int colcount)
Set table values.
Definition: table.h:55
void set(const std::vector< std::vector< std::string >> &val)
Set table values.
Definition: table.h:45