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