ui/winui/table.cpp

changeset 3
f154867f54dc
parent 0
2483f517c562
child 13
5a8762fcfecc
equal deleted inserted replaced
2:fbdfaacc4182 3:f154867f54dc
57 57
58 extern "C" void reg_table_destructor(UiContext * ctx, UiTable * table) { 58 extern "C" void reg_table_destructor(UiContext * ctx, UiTable * table) {
59 // TODO: 59 // TODO:
60 } 60 }
61 61
62 static void textblock_set_str(TextBlock& t, const char* str) {
63 if (str) {
64 wchar_t* wstr = str2wstr(str, nullptr);
65 t.Text(winrt::hstring(wstr));
66 free(wstr);
67 }
68 }
69
70 static void textblock_set_int(TextBlock& t, int i) {
71 wchar_t buf[16];
72 swprintf(buf, 16, L"%d", i);
73 t.Text(winrt::hstring(buf));
74 }
75
62 UIEXPORT UIWIDGET ui_table_create(UiObject* obj, UiListArgs args) { 76 UIEXPORT UIWIDGET ui_table_create(UiObject* obj, UiListArgs args) {
63 if (!args.model) { 77 if (!args.model) {
64 return nullptr; 78 return nullptr;
65 } 79 }
66 80
160 // add header row definition 174 // add header row definition
161 auto headerRowDef = RowDefinition(); 175 auto headerRowDef = RowDefinition();
162 headerRowDef.Height(gl); 176 headerRowDef.Height(gl);
163 grid.RowDefinitions().Append(headerRowDef); 177 grid.RowDefinitions().Append(headerRowDef);
164 178
179 winrt::Windows::UI::Color borderColor = { 63, 0, 0, 0 };
180 SolidColorBrush borderBrush = SolidColorBrush(borderColor);
181
165 182
166 for (int i = 0; i < model->columns;i++) { 183 for (int i = 0; i < model->columns;i++) {
167 char* title = model->titles[i]; 184 char* title = model->titles[i];
168 UiModelType type = model->types[i]; 185 UiModelType type = model->types[i];
169 186
170 // add grid column definition 187 // add grid column definition
171 auto colDef = ColumnDefinition(); 188 auto colDef = ColumnDefinition();
172 colDef.Width(gl); 189 colDef.Width(gl);
173 grid.ColumnDefinitions().Append(colDef); 190 grid.ColumnDefinitions().Append(colDef);
174 191
175 // add button 192 // header column border
176 auto button = Button(); 193 Border headerBorder = Border();
177 wchar_t* wlabel = str2wstr(title, nullptr);
178 button.Content(box_value(wlabel));
179 free(wlabel);
180
181 // some styling for the button
182 Thickness border = { 0,0,1,0 }; 194 Thickness border = { 0,0,1,0 };
183 CornerRadius corner = { 0,0,0,0 }; 195 headerBorder.BorderThickness(border);
184 button.BorderThickness(border); 196 headerBorder.BorderBrush(borderBrush);
185 button.CornerRadius(corner); 197
186 198 // add text
187 grid.SetColumn(button, i); 199 auto hLabel = TextBlock();
188 grid.SetRow(button, 0); 200 textblock_set_str(hLabel, title);
189 grid.Children().Append(button); 201 Thickness cellpadding = { 10,4,4,4 };
202 hLabel.Padding(cellpadding);
203 hLabel.VerticalAlignment(VerticalAlignment::Stretch);
204
205 // event handler for highlighting and column resizing
206 headerBorder.PointerPressed(
207 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
208 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
209 // the last column doesn't need resize capabilities
210 if (i + 1 < model->columns) {
211 double width = headerBorder.ActualWidth();
212 auto point = args.GetCurrentPoint(headerBorder);
213 auto position = point.Position();
214 if (position.X + 4 >= width) {
215 this->resize = true;
216 this->resizedCol = headerBorder;
217 }
218 }
219 })
220 );
221 headerBorder.PointerReleased(
222 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
223 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
224 this->resize = false;
225 })
226 );
227 headerBorder.PointerMoved(
228 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
229 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
230 if (this->resize) {
231 auto point = args.GetCurrentPoint(this->resizedCol);
232 auto position = point.Position();
233 if (position.X > 1) {
234 this->resizedCol.Width(position.X);
235 }
236 }
237 })
238 );
239 headerBorder.PointerEntered(
240 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
241 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
242 // TODO: background
243 })
244 );
245 headerBorder.PointerExited(
246 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
247 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
248 // TODO: background
249 })
250 );
251
252
253
254 // add controls
255 headerBorder.Child(hLabel);
256
257 grid.SetColumn(headerBorder, i);
258 grid.SetRow(headerBorder, 0);
259 grid.Children().Append(headerBorder);
190 260
191 UiTableColumn h; 261 UiTableColumn h;
192 h.header = button; 262 h.header = headerBorder;
193 header.push_back(h); 263 header.push_back(h);
194 } 264 }
195 265
196 maxrows = 1; 266 maxrows = 1;
197 }
198
199 static void textblock_set_str(TextBlock &t, const char *str) {
200 if (str) {
201 wchar_t* wstr = str2wstr(str, nullptr);
202 t.Text(winrt::hstring(wstr));
203 free(wstr);
204 }
205 }
206
207 static void textblock_set_int(TextBlock& t, int i) {
208 wchar_t buf[16];
209 swprintf(buf, 16, L"%d", i);
210 t.Text(winrt::hstring(buf));
211 } 267 }
212 268
213 static ULONG64 getsystime() { 269 static ULONG64 getsystime() {
214 SYSTEMTIME st; 270 SYSTEMTIME st;
215 GetSystemTime(&st); 271 GetSystemTime(&st);

mercurial