ui/winui/window.cpp

branch
newapi
changeset 379
958bae372271
parent 378
d41b1ffc5f77
equal deleted inserted replaced
378:d41b1ffc5f77 379:958bae372271
33 33
34 #include "appmenu.h" 34 #include "appmenu.h"
35 #include "commandbar.h" 35 #include "commandbar.h"
36 #include "container.h" 36 #include "container.h"
37 #include "util.h" 37 #include "util.h"
38 #include "button.h"
38 39
39 #include "../common/context.h" 40 #include "../common/context.h"
40 #include "../common/object.h" 41 #include "../common/object.h"
41 42
42 #include <stdlib.h> 43 #include <stdlib.h>
199 obj->window = window_data; 200 obj->window = window_data;
200 201
201 return obj; 202 return obj;
202 } 203 }
203 204
205 static void dialog_button_add_callback(ContentDialog dialog, Button button, int num, UiObject *obj, ui_callback onclick, void *onclickdata) {
206 button.Click([dialog, num, obj, onclick, onclickdata](IInspectable const& sender, RoutedEventArgs) {
207 if (onclick) {
208 UiEvent evt;
209 evt.obj = obj;
210 evt.window = obj->window;
211 evt.document = obj->ctx->document;
212 evt.eventdata = nullptr;
213 evt.intval = num;
214 onclick(&evt, onclickdata);
215 }
216 dialog.Hide();
217 });
218 }
219
204 UIEXPORT UiObject* ui_dialog_window_create(UiObject *parent, UiDialogWindowArgs args) { 220 UIEXPORT UiObject* ui_dialog_window_create(UiObject *parent, UiDialogWindowArgs args) {
205 return NULL; 221 UiWindow *window = parent->wobj;
222 if (!window) {
223 return NULL;
224 }
225
226 CxMempool* mp = cxBasicMempoolCreate(256);
227 UiObject* obj = (UiObject*)cxCalloc(mp->allocator, 1, sizeof(UiObject));
228
229 obj->ctx = uic_context(obj, mp);
230
231 ContentDialog dialog = ContentDialog();
232 UIElement elm = dialog;
233 UiWidget* widget = new UiWidget(elm);
234 ui_context_add_widget_destructor(obj->ctx, widget);
235 obj->widget = widget;
236
237 if (args.title) {
238 wchar_t* wtitle = str2wstr(args.title, nullptr);
239 dialog.Title(box_value(wtitle));
240 free(wtitle);
241 }
242
243
244 Grid dialogContent = Grid();
245 GridLength gl;
246
247 // content row
248 gl.Value = 1;
249 gl.GridUnitType = GridUnitType::Star;
250 RowDefinition rowdef0 = RowDefinition();
251 rowdef0.Height(gl);
252 dialogContent.RowDefinitions().Append(rowdef0);
253
254 // button row
255 gl.Value = 0;
256 gl.GridUnitType = GridUnitType::Auto;
257 RowDefinition rowdef1 = RowDefinition();
258 rowdef1.Height(gl);
259 dialogContent.RowDefinitions().Append(rowdef1);
260
261 // coldef
262 gl.Value = 1;
263 gl.GridUnitType = GridUnitType::Star;
264 ColumnDefinition coldef = ColumnDefinition();
265 coldef.Width(gl);
266 dialogContent.ColumnDefinitions().Append(coldef);
267
268 // content
269 Grid grid = Grid();
270 grid.SetRow(grid, 0);
271 grid.SetColumn(grid, 0);
272 dialogContent.Children().Append(grid);
273 obj->container = new UiBoxContainer(grid, UI_BOX_CONTAINER_VBOX, 0, 0);
274
275 // buttons
276 Grid buttons = Grid();
277 Thickness btnsMargin = { (double)0, (double)10, (double)0, (double)0 };
278 buttons.Margin(btnsMargin);
279
280 RowDefinition btnrowdef = RowDefinition();
281 gl.Value = 0;
282 gl.GridUnitType = GridUnitType::Auto;
283 btnrowdef.Height(gl);
284 buttons.RowDefinitions().Append(btnrowdef);
285
286 gl.Value = 1;
287 gl.GridUnitType = GridUnitType::Auto;
288 int c = 0;
289 if (args.lbutton1) {
290 ColumnDefinition bcoldef = ColumnDefinition();
291 bcoldef.Width(gl);
292 buttons.ColumnDefinitions().Append(bcoldef);
293
294 Button btn = Button();
295 ui_set_button_label(btn, args.lbutton1, NULL, NULL, UI_LABEL_TEXT);
296 Thickness margin = { (double)5, (double)5, (double)5, (double)5 };
297 btn.Margin(margin);
298 btn.HorizontalAlignment(HorizontalAlignment::Stretch);
299 dialog_button_add_callback(dialog, btn, 1, obj, args.onclick, args.onclickdata);
300
301 buttons.SetRow(btn, 0);
302 buttons.SetColumn(btn, c++);
303 buttons.Children().Append(btn);
304 }
305 if (args.lbutton2) {
306 ColumnDefinition bcoldef = ColumnDefinition();
307 bcoldef.Width(gl);
308 buttons.ColumnDefinitions().Append(bcoldef);
309
310 Button btn = Button();
311 ui_set_button_label(btn, args.lbutton2, NULL, NULL, UI_LABEL_TEXT);
312 Thickness margin = { (double)5, (double)5, (double)5, (double)5 };
313 btn.Margin(margin);
314 btn.HorizontalAlignment(HorizontalAlignment::Stretch);
315 dialog_button_add_callback(dialog, btn, 2, obj, args.onclick, args.onclickdata);
316
317 buttons.SetRow(btn, 0);
318 buttons.SetColumn(btn, c++);
319 buttons.Children().Append(btn);
320 }
321 if (args.rbutton3) {
322 ColumnDefinition bcoldef = ColumnDefinition();
323 bcoldef.Width(gl);
324 buttons.ColumnDefinitions().Append(bcoldef);
325
326 Button btn = Button();
327 ui_set_button_label(btn, args.rbutton3, NULL, NULL, UI_LABEL_TEXT);
328 Thickness margin = { (double)5, (double)5, (double)5, (double)5 };
329 btn.Margin(margin);
330 btn.HorizontalAlignment(HorizontalAlignment::Stretch);
331 dialog_button_add_callback(dialog, btn, 3, obj, args.onclick, args.onclickdata);
332
333 buttons.SetRow(btn, 0);
334 buttons.SetColumn(btn, c++);
335 buttons.Children().Append(btn);
336 }
337 if (args.rbutton4) {
338 ColumnDefinition bcoldef = ColumnDefinition();
339 bcoldef.Width(gl);
340 buttons.ColumnDefinitions().Append(bcoldef);
341
342 Button btn = Button();
343 ui_set_button_label(btn, args.rbutton4, NULL, NULL, UI_LABEL_TEXT);
344 Thickness margin = { (double)5, (double)5, (double)5, (double)5 };
345 btn.Margin(margin);
346 btn.HorizontalAlignment(HorizontalAlignment::Stretch);
347 dialog_button_add_callback(dialog, btn, 4, obj, args.onclick, args.onclickdata);
348
349 buttons.SetRow(btn, 0);
350 buttons.SetColumn(btn, c++);
351 buttons.Children().Append(btn);
352 }
353
354 dialogContent.SetRow(buttons, 1);
355 dialogContent.SetColumn(buttons, 0);
356 dialogContent.Children().Append(buttons);
357
358
359 dialog.Content(dialogContent);
360 dialog.XamlRoot(window->window.Content().XamlRoot());
361
362 obj->widget->Show = [dialog]() {
363 dialog.ShowAsync();
364 };
365
366 return obj;
206 } 367 }
207 368
208 void ui_window_size(UiObject *obj, int width, int height) { 369 void ui_window_size(UiObject *obj, int width, int height) {
209 UIWINDOW win = obj->wobj; 370 UIWINDOW win = obj->wobj;
210 if (win) { 371 if (win) {

mercurial