ui/winui/table.cpp

branch
newapi
changeset 214
279c0c81d3b1
parent 210
83556205edad
child 215
1bd5534c395d
equal deleted inserted replaced
213:7e39db525fd9 214:279c0c81d3b1
36 #include "../common/object.h" 36 #include "../common/object.h"
37 37
38 #include <winrt/Microsoft.UI.Xaml.Data.h> 38 #include <winrt/Microsoft.UI.Xaml.Data.h>
39 #include <winrt/Microsoft.UI.Xaml.Media.h> 39 #include <winrt/Microsoft.UI.Xaml.Media.h>
40 #include <winrt/Microsoft.UI.Xaml.Input.h> 40 #include <winrt/Microsoft.UI.Xaml.Input.h>
41 #include <winrt/Windows.UI.Core.h>
41 42
42 using namespace winrt; 43 using namespace winrt;
43 using namespace Microsoft::UI::Xaml; 44 using namespace Microsoft::UI::Xaml;
44 using namespace Microsoft::UI::Xaml::Controls; 45 using namespace Microsoft::UI::Xaml::Controls;
45 using namespace Windows::UI::Xaml::Interop; 46 using namespace Windows::UI::Xaml::Interop;
118 winrt::Windows::UI::Color selectedBg = { 255, 204, 232, 255 }; // test color 119 winrt::Windows::UI::Color selectedBg = { 255, 204, 232, 255 }; // test color
119 selectedBrush = SolidColorBrush(selectedBg); 120 selectedBrush = SolidColorBrush(selectedBg);
120 121
121 winrt::Windows::UI::Color selectedFg = { 255, 0, 90, 158 }; // test color 122 winrt::Windows::UI::Color selectedFg = { 255, 0, 90, 158 }; // test color
122 selectedBorderBrush = SolidColorBrush(selectedFg); 123 selectedBorderBrush = SolidColorBrush(selectedFg);
124
125 grid.KeyDown(
126 winrt::Microsoft::UI::Xaml::Input::KeyEventHandler(
127 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::KeyRoutedEventArgs const& args) {
128 // key event for hanling the table cursor or enter
129 })
130 );
123 } 131 }
124 132
125 void UiTable::add_header(UiModel* model) { 133 void UiTable::add_header(UiModel* model) {
126 GridLength gl; 134 GridLength gl;
127 gl.Value = 0; 135 gl.Value = 0;
188 grid.RowDefinitions().Append(rowdef); 196 grid.RowDefinitions().Append(rowdef);
189 maxrows = row; 197 maxrows = row;
190 } 198 }
191 199
192 for (int col = 0; col < header.size(); col++) { 200 for (int col = 0; col < header.size(); col++) {
201 // create ui elements with the correct cell border
202 // dependeing on the column
193 Border cellBorder = Border(); 203 Border cellBorder = Border();
194 cellBorder.Background(defaultBrush); 204 cellBorder.Background(defaultBrush);
195 TextBlock cell = TextBlock(); 205 TextBlock cell = TextBlock();
196 cellBorder.Child(cell); 206 cellBorder.Child(cell);
197 cellBorder.BorderBrush(defaultBrush); 207 cellBorder.BorderBrush(defaultBrush);
202 cellBorder.BorderThickness(b3); 212 cellBorder.BorderThickness(b3);
203 } 213 }
204 else { 214 else {
205 cellBorder.BorderThickness(b2); 215 cellBorder.BorderThickness(b2);
206 } 216 }
207 217 Thickness padding = { 10,0,4,0 };
218 cell.Padding(padding);
219 cell.VerticalAlignment(VerticalAlignment::Stretch);
220
221 // set cell value
208 char* value = (char*)getvalue(elm, col); 222 char* value = (char*)getvalue(elm, col);
209 if (value) { 223 if (value) {
210 wchar_t* wstr = str2wstr(value, nullptr); 224 wchar_t* wstr = str2wstr(value, nullptr);
211 cell.Text(winrt::hstring(wstr)); 225 cell.Text(winrt::hstring(wstr));
212 free(wstr); 226 free(wstr);
213 } 227 }
214 Thickness padding = { 10,0,4,0 };
215 cell.Padding(padding);
216 cell.VerticalAlignment(VerticalAlignment::Stretch);
217 228
218 // event handler 229 // event handler
219 cellBorder.PointerPressed( 230 cellBorder.PointerPressed(
220 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler( 231 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
221 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) { 232 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
222 if (selection > 0) { 233 winrt::Windows::System::VirtualKeyModifiers modifiers = args.KeyModifiers();
223 row_background(selection, defaultBrush, defaultBrush); 234
224 } 235 if (modifiers == winrt::Windows::System::VirtualKeyModifiers::Control) {
225 row_background(row, selectedBrush, selectedBorderBrush); 236 // add/remove current row
226 selection = row; 237 if (!is_row_selected(row)) {
238 row_background(row, selectedBrush, selectedBorderBrush);
239 selection.push_back(row);
240 }
241 else {
242 row_background(row, highlightBrush, highlightBrush);
243 remove_from_selection(row);
244 }
245 }
246 else if (modifiers == winrt::Windows::System::VirtualKeyModifiers::None || selection.size() == 0) {
247 // no modifier or shift is pressed but there is no selection
248 if (selection.size() > 0) {
249 change_rows_bg(selection, defaultBrush, defaultBrush);
250 }
251
252 row_background(row, selectedBrush, selectedBorderBrush);
253 selection = { row };
254 }
255 else if (modifiers == winrt::Windows::System::VirtualKeyModifiers::Shift) {
256 // select everything between the first selection and the current row
257 std::sort(selection.begin(), selection.end());
258 int first = selection.front();
259
260 // clear previous selection
261 change_rows_bg(selection, defaultBrush, defaultBrush);
262
263 // create new selection
264 std::vector<int> newselection;
265 for (int s = first; s <= row; s++) {
266 newselection.push_back(s);
267 }
268 selection = newselection;
269 change_rows_bg(selection, selectedBrush, selectedBorderBrush);
270 }
227 }) 271 })
228 ); 272 );
229 cellBorder.PointerReleased( 273 cellBorder.PointerReleased(
230 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler( 274 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
231 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) { 275 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
233 }) 277 })
234 ); 278 );
235 cellBorder.PointerEntered( 279 cellBorder.PointerEntered(
236 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler( 280 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
237 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) { 281 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
238 if (selection != row) { 282 if (!is_row_selected(row)) {
239 row_background(row, highlightBrush, highlightBrush); 283 row_background(row, highlightBrush, highlightBrush);
240 } 284 }
241 }) 285 })
242 ); 286 );
243 cellBorder.PointerExited( 287 cellBorder.PointerExited(
244 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler( 288 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
245 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) { 289 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
246 if (selection != row) { 290 if (!is_row_selected(row)) {
247 row_background(row, defaultBrush, defaultBrush); 291 row_background(row, defaultBrush, defaultBrush);
248 } 292 }
249 }) 293 })
250 ); 294 );
251 295
282 b.Background(brush); 326 b.Background(brush);
283 b.BorderBrush(borderBrush); 327 b.BorderBrush(borderBrush);
284 } 328 }
285 } 329 }
286 } 330 }
331
332 void UiTable::change_rows_bg(std::vector<int> rows, winrt::Microsoft::UI::Xaml::Media::Brush brush, winrt::Microsoft::UI::Xaml::Media::Brush borderBrush) {
333 std::for_each(rows.cbegin(), rows.cend(), [&](const int& row) {row_background(row, brush, borderBrush); });
334 }
335
336 bool UiTable::is_row_selected(int row) {
337 return std::find(selection.begin(), selection.end(), row) != selection.end() ? true : false;
338 }
339
340 void UiTable::remove_from_selection(int row) {
341 selection.erase(std::remove(selection.begin(), selection.end(), row), selection.end());
342 selection.shrink_to_fit();
343 }

mercurial