ui/winui/table.cpp

branch
newapi
changeset 227
04b317bc6f13
parent 224
88bc21b19213
child 228
b4d7686b30dc
equal deleted inserted replaced
226:4eef1d49f794 227:04b317bc6f13
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 switch (i) {
232 case 0: OutputDebugString(L"pointer moved 0\n"); break;
233 case 1: OutputDebugString(L"pointer moved 1\n"); break;
234 case 2: OutputDebugString(L"pointer moved 2\n"); break;
235 case 3: OutputDebugString(L"pointer moved 3\n"); break;
236 }
237 auto point = args.GetCurrentPoint(this->resizedCol);
238 auto position = point.Position();
239 if (position.X > 1) {
240 this->resizedCol.Width(position.X);
241 }
242 }
243 })
244 );
245 headerBorder.PointerEntered(
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 headerBorder.PointerExited(
252 winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
253 [=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
254 // TODO: background
255 })
256 );
257
258
259
260 // add controls
261 headerBorder.Child(hLabel);
262
263 grid.SetColumn(headerBorder, i);
264 grid.SetRow(headerBorder, 0);
265 grid.Children().Append(headerBorder);
190 266
191 UiTableColumn h; 267 UiTableColumn h;
192 h.header = button; 268 h.header = headerBorder;
193 header.push_back(h); 269 header.push_back(h);
194 } 270 }
195 271
196 maxrows = 1; 272 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 } 273 }
212 274
213 static ULONG64 getsystime() { 275 static ULONG64 getsystime() {
214 SYSTEMTIME st; 276 SYSTEMTIME st;
215 GetSystemTime(&st); 277 GetSystemTime(&st);

mercurial