ui/winui/table.cpp

branch
newapi
changeset 215
1bd5534c395d
parent 214
279c0c81d3b1
child 216
391c2c723029
equal deleted inserted replaced
214:279c0c81d3b1 215:1bd5534c395d
29 #include "pch.h" 29 #include "pch.h"
30 30
31 #include "table.h" 31 #include "table.h"
32 #include "container.h" 32 #include "container.h"
33 #include "util.h" 33 #include "util.h"
34 #include "icons.h"
34 35
35 #include "../common/context.h" 36 #include "../common/context.h"
36 #include "../common/object.h" 37 #include "../common/object.h"
38 #include "../common/types.h"
37 39
38 #include <winrt/Microsoft.UI.Xaml.Data.h> 40 #include <winrt/Microsoft.UI.Xaml.Data.h>
39 #include <winrt/Microsoft.UI.Xaml.Media.h> 41 #include <winrt/Microsoft.UI.Xaml.Media.h>
40 #include <winrt/Microsoft.UI.Xaml.Input.h> 42 #include <winrt/Microsoft.UI.Xaml.Input.h>
41 #include <winrt/Windows.UI.Core.h> 43 #include <winrt/Windows.UI.Core.h>
128 // key event for hanling the table cursor or enter 130 // key event for hanling the table cursor or enter
129 }) 131 })
130 ); 132 );
131 } 133 }
132 134
135 UiTable::~UiTable() {
136 ui_model_free(NULL, model);
137 }
138
133 void UiTable::add_header(UiModel* model) { 139 void UiTable::add_header(UiModel* model) {
140 this->model = ui_model_copy(NULL, model);
141
134 GridLength gl; 142 GridLength gl;
135 gl.Value = 0; 143 gl.Value = 0;
136 gl.GridUnitType = GridUnitType::Auto; 144 gl.GridUnitType = GridUnitType::Auto;
137 145
138 // add header row definition 146 // add header row definition
170 h.header = button; 178 h.header = button;
171 header.push_back(h); 179 header.push_back(h);
172 } 180 }
173 181
174 maxrows = 1; 182 maxrows = 1;
183 }
184
185 static void textblock_set_str(TextBlock &t, const char *str) {
186 if (str) {
187 wchar_t* wstr = str2wstr(str, nullptr);
188 t.Text(winrt::hstring(wstr));
189 free(wstr);
190 }
191 }
192
193 static void textblock_set_int(TextBlock& t, int i) {
194 wchar_t buf[16];
195 swprintf(buf, 16, L"%d", i);
196 t.Text(winrt::hstring(buf));
175 } 197 }
176 198
177 void UiTable::update(UiList* list, int i) { 199 void UiTable::update(UiList* list, int i) {
178 if (getvalue == nullptr) { 200 if (getvalue == nullptr) {
179 return; 201 return;
195 rowdef.Height(gl); 217 rowdef.Height(gl);
196 grid.RowDefinitions().Append(rowdef); 218 grid.RowDefinitions().Append(rowdef);
197 maxrows = row; 219 maxrows = row;
198 } 220 }
199 221
200 for (int col = 0; col < header.size(); col++) { 222 Thickness cellpadding = { 10,0,4,0 };
223
224 int model_col = 0;
225 for (int col = 0; col < header.size(); col++, model_col++) {
201 // create ui elements with the correct cell border 226 // create ui elements with the correct cell border
202 // dependeing on the column 227 // dependeing on the column
203 Border cellBorder = Border(); 228 Border cellBorder = Border();
204 cellBorder.Background(defaultBrush); 229 cellBorder.Background(defaultBrush);
205 TextBlock cell = TextBlock(); 230
206 cellBorder.Child(cell); 231 // set the cell value
232 UiModelType type = model->types[col];
233 switch (type) {
234 case UI_STRING: {
235 TextBlock cell = TextBlock();
236 cell.Padding(cellpadding);
237 cell.VerticalAlignment(VerticalAlignment::Stretch);
238 textblock_set_str(cell, (char*)getvalue(elm, model_col));
239 cellBorder.Child(cell);
240 break;
241 }
242 case UI_INTEGER: {
243 TextBlock cell = TextBlock();
244 cell.Padding(cellpadding);
245 cell.VerticalAlignment(VerticalAlignment::Stretch);
246 int *value = (int*)getvalue(elm, model_col);
247 if (value) {
248 textblock_set_int(cell, *value);
249 }
250 cellBorder.Child(cell);
251 break;
252 }
253 case UI_ICON: {
254 UiIcon* iconConstr = (UiIcon*)getvalue(elm, model_col);
255 if (iconConstr) {
256 IconElement icon = iconConstr->getIcon();
257 cellBorder.Child(icon);
258 }
259 break;
260 }
261 case UI_ICON_TEXT: {
262 StackPanel cellPanel = StackPanel();
263 cellPanel.Spacing(2);
264 cellPanel.Padding(cellpadding);
265 cellPanel.VerticalAlignment(VerticalAlignment::Stretch);
266
267 cellPanel.Orientation(Orientation::Horizontal);
268 UiIcon* iconConstr = (UiIcon*)getvalue(elm, model_col++);
269 char* str = (char*)getvalue(elm, model_col);
270 if (iconConstr) {
271 IconElement icon = iconConstr->getIcon();
272 cellPanel.Children().Append(icon);
273 }
274 TextBlock cell = TextBlock();
275 textblock_set_str(cell, str);
276 cellPanel.Children().Append(cell);
277 cellBorder.Child(cellPanel);
278 break;
279 }
280 }
281
207 cellBorder.BorderBrush(defaultBrush); 282 cellBorder.BorderBrush(defaultBrush);
208 if (col == 0) { 283 if (col == 0) {
209 cellBorder.BorderThickness(b1); 284 cellBorder.BorderThickness(b1);
210 } 285 }
211 else if (col + 1 == header.size()) { 286 else if (col + 1 == header.size()) {
212 cellBorder.BorderThickness(b3); 287 cellBorder.BorderThickness(b3);
213 } 288 }
214 else { 289 else {
215 cellBorder.BorderThickness(b2); 290 cellBorder.BorderThickness(b2);
216 }
217 Thickness padding = { 10,0,4,0 };
218 cell.Padding(padding);
219 cell.VerticalAlignment(VerticalAlignment::Stretch);
220
221 // set cell value
222 char* value = (char*)getvalue(elm, col);
223 if (value) {
224 wchar_t* wstr = str2wstr(value, nullptr);
225 cell.Text(winrt::hstring(wstr));
226 free(wstr);
227 } 291 }
228 292
229 // event handler 293 // event handler
230 cellBorder.PointerPressed( 294 cellBorder.PointerPressed(
231 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler( 295 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(

mercurial