ui/winui/window.cpp

changeset 79
483d7342b439
parent 76
641dcc79e0ef
--- a/ui/winui/window.cpp	Mon Nov 11 22:42:09 2024 +0100
+++ b/ui/winui/window.cpp	Tue Nov 12 15:05:57 2024 +0100
@@ -35,6 +35,7 @@
 #include "commandbar.h"
 #include "container.h"
 #include "util.h"
+#include "button.h"
 
 #include "../common/context.h"
 #include "../common/object.h"
@@ -201,8 +202,168 @@
 	return obj;
 }
 
+static void dialog_button_add_callback(ContentDialog dialog, Button button, int num, UiObject *obj, ui_callback onclick, void *onclickdata) {
+	button.Click([dialog, num, obj, onclick, onclickdata](IInspectable const& sender, RoutedEventArgs) {
+		if (onclick) {
+			UiEvent evt;
+			evt.obj = obj;
+			evt.window = obj->window;
+			evt.document = obj->ctx->document;
+			evt.eventdata = nullptr;
+			evt.intval = num;
+			onclick(&evt, onclickdata);
+		}
+		dialog.Hide();
+	});
+}
+
 UIEXPORT UiObject* ui_dialog_window_create(UiObject *parent, UiDialogWindowArgs args) {
-	return NULL;
+	UiWindow *window = parent->wobj;
+	if (!window) {
+		return NULL;
+	}
+	
+	CxMempool* mp = cxBasicMempoolCreate(256);
+	UiObject* obj = (UiObject*)cxCalloc(mp->allocator, 1, sizeof(UiObject));
+	
+	obj->ctx = uic_context(obj, mp);
+	
+	ContentDialog dialog = ContentDialog();
+	UIElement elm = dialog;
+	UiWidget* widget = new UiWidget(elm);
+	ui_context_add_widget_destructor(obj->ctx, widget);
+	obj->widget = widget;
+
+	if (args.title) {
+		wchar_t* wtitle = str2wstr(args.title, nullptr);
+		dialog.Title(box_value(wtitle));
+		free(wtitle);
+	}
+
+	
+	Grid dialogContent = Grid();
+	GridLength gl;
+
+	// content row
+	gl.Value = 1;
+	gl.GridUnitType = GridUnitType::Star;
+	RowDefinition rowdef0 = RowDefinition();
+	rowdef0.Height(gl);
+	dialogContent.RowDefinitions().Append(rowdef0);
+
+	// button row
+	gl.Value = 0;
+	gl.GridUnitType = GridUnitType::Auto;
+	RowDefinition rowdef1 = RowDefinition();
+	rowdef1.Height(gl);
+	dialogContent.RowDefinitions().Append(rowdef1);
+
+	// coldef
+	gl.Value = 1;
+	gl.GridUnitType = GridUnitType::Star;
+	ColumnDefinition coldef = ColumnDefinition();
+	coldef.Width(gl);
+	dialogContent.ColumnDefinitions().Append(coldef);
+
+	// content
+	Grid grid = Grid();
+	grid.SetRow(grid, 0);
+	grid.SetColumn(grid, 0);
+	dialogContent.Children().Append(grid);
+	obj->container = new UiBoxContainer(grid, UI_BOX_CONTAINER_VBOX, 0, 0);
+
+	// buttons
+	Grid buttons = Grid();
+	Thickness btnsMargin = { (double)0, (double)10, (double)0, (double)0 };
+	buttons.Margin(btnsMargin);
+
+	RowDefinition btnrowdef = RowDefinition();
+	gl.Value = 0;
+	gl.GridUnitType = GridUnitType::Auto;
+	btnrowdef.Height(gl);
+	buttons.RowDefinitions().Append(btnrowdef);
+
+	gl.Value = 1;
+	gl.GridUnitType = GridUnitType::Auto;
+	int c = 0;
+	if (args.lbutton1) {
+		ColumnDefinition bcoldef = ColumnDefinition();
+		bcoldef.Width(gl);
+		buttons.ColumnDefinitions().Append(bcoldef);
+
+		Button btn = Button();
+		ui_set_button_label(btn, args.lbutton1, NULL, NULL, UI_LABEL_TEXT);
+		Thickness margin = { (double)5, (double)5, (double)5, (double)5 };
+		btn.Margin(margin);
+		btn.HorizontalAlignment(HorizontalAlignment::Stretch);
+		dialog_button_add_callback(dialog, btn, 1, obj, args.onclick, args.onclickdata);
+
+		buttons.SetRow(btn, 0);
+		buttons.SetColumn(btn, c++);
+		buttons.Children().Append(btn);
+	}
+	if (args.lbutton2) {
+		ColumnDefinition bcoldef = ColumnDefinition();
+		bcoldef.Width(gl);
+		buttons.ColumnDefinitions().Append(bcoldef);
+
+		Button btn = Button();
+		ui_set_button_label(btn, args.lbutton2, NULL, NULL, UI_LABEL_TEXT);
+		Thickness margin = { (double)5, (double)5, (double)5, (double)5 };
+		btn.Margin(margin);
+		btn.HorizontalAlignment(HorizontalAlignment::Stretch);
+		dialog_button_add_callback(dialog, btn, 2, obj, args.onclick, args.onclickdata);
+
+		buttons.SetRow(btn, 0);
+		buttons.SetColumn(btn, c++);
+		buttons.Children().Append(btn);
+	}
+	if (args.rbutton3) {
+		ColumnDefinition bcoldef = ColumnDefinition();
+		bcoldef.Width(gl);
+		buttons.ColumnDefinitions().Append(bcoldef);
+
+		Button btn = Button();
+		ui_set_button_label(btn, args.rbutton3, NULL, NULL, UI_LABEL_TEXT);
+		Thickness margin = { (double)5, (double)5, (double)5, (double)5 };
+		btn.Margin(margin);
+		btn.HorizontalAlignment(HorizontalAlignment::Stretch);
+		dialog_button_add_callback(dialog, btn, 3, obj, args.onclick, args.onclickdata);
+
+		buttons.SetRow(btn, 0);
+		buttons.SetColumn(btn, c++);
+		buttons.Children().Append(btn);
+	}
+	if (args.rbutton4) {
+		ColumnDefinition bcoldef = ColumnDefinition();
+		bcoldef.Width(gl);
+		buttons.ColumnDefinitions().Append(bcoldef);
+
+		Button btn = Button();
+		ui_set_button_label(btn, args.rbutton4, NULL, NULL, UI_LABEL_TEXT);
+		Thickness margin = { (double)5, (double)5, (double)5, (double)5 };
+		btn.Margin(margin);
+		btn.HorizontalAlignment(HorizontalAlignment::Stretch);
+		dialog_button_add_callback(dialog, btn, 4, obj, args.onclick, args.onclickdata);
+
+		buttons.SetRow(btn, 0);
+		buttons.SetColumn(btn, c++);
+		buttons.Children().Append(btn);
+	}
+
+	dialogContent.SetRow(buttons, 1);
+	dialogContent.SetColumn(buttons, 0);
+	dialogContent.Children().Append(buttons);
+
+
+	dialog.Content(dialogContent);
+	dialog.XamlRoot(window->window.Content().XamlRoot());
+
+	obj->widget->Show = [dialog]() {
+		dialog.ShowAsync();
+	};
+
+	return obj;
 }
 
 void ui_window_size(UiObject *obj, int width, int height) {

mercurial