add input option to ui_dialog newapi

Sun, 11 Feb 2024 15:44:33 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 11 Feb 2024 15:44:33 +0100
branch
newapi
changeset 252
7d176764756d
parent 251
22dc0b739dd8
child 253
087cc9216f28

add input option to ui_dialog

make/vs/testapp/main.c file | annotate | diff | comparison | revisions
ui/ui/window.h file | annotate | diff | comparison | revisions
ui/winui/window.cpp file | annotate | diff | comparison | revisions
--- a/make/vs/testapp/main.c	Sun Feb 11 13:59:40 2024 +0100
+++ b/make/vs/testapp/main.c	Sun Feb 11 15:44:33 2024 +0100
@@ -185,11 +185,12 @@
 }
 
 void dialog_result(UiEvent *evt, void *data) {
+    char *str = evt->eventdata;
     printf("dialog: %d\n", (int)evt->intval);
 }
 
 void btn_dialog(UiEvent *evt, void *data) {
-    ui_dialog(evt->obj, .title = "Title", .content = "Hello World", .button1_label = "Yes", .button2_label = "No", .closebutton_label = "Close", .result = dialog_result);
+    ui_dialog(evt->obj, .title = "Title", .input = TRUE, .content = "Hello World", .button1_label = "Yes", .button2_label = "No", .closebutton_label = "Close", .result = dialog_result);
 }
 
 
--- a/ui/ui/window.h	Sun Feb 11 13:59:40 2024 +0100
+++ b/ui/ui/window.h	Sun Feb 11 15:44:33 2024 +0100
@@ -45,6 +45,7 @@
     const char *button1_label;
     const char *button2_label;
     const char *closebutton_label;
+    UiBool input;
     ui_callback result;
     void *resultdata;
 } UiDialogArgs;
--- a/ui/winui/window.cpp	Sun Feb 11 13:59:40 2024 +0100
+++ b/ui/winui/window.cpp	Sun Feb 11 15:44:33 2024 +0100
@@ -220,16 +220,41 @@
 		dialog.Title(winrt::box_value(str));
 		free(str);
 	}
-	if (args.content) {
-		wchar_t *str = str2wstr(args.content, nullptr);
-		dialog.Content(winrt::box_value(str));
-		free(str);
+
+	TextBox textfield{ nullptr };
+	if (args.input) {
+		StackPanel panel = StackPanel();
+		panel.Orientation(Orientation::Vertical);
+		if (args.content) {
+			wchar_t *str = str2wstr(args.content, nullptr);
+			TextBlock label = TextBlock();
+			label.Text(str);
+			panel.Children().Append(label);
+			free(str);
+		}
+
+		textfield = TextBox();
+		Thickness margin = { 0, 5, 0, 0 };
+		textfield.Margin(margin);
+		panel.Margin(margin);
+
+		panel.Children().Append(textfield);
+
+		dialog.Content(panel);
+
+	} else {
+		if (args.content) {
+			wchar_t *str = str2wstr(args.content, nullptr);
+			dialog.Content(winrt::box_value(str));
+			free(str);
+		}
 	}
 
 	if (args.button1_label) {
 		wchar_t *str = str2wstr(args.button1_label, nullptr);
 		dialog.PrimaryButtonText(winrt::hstring(str));
 		free(str);
+		dialog.DefaultButton(ContentDialogButton::Primary);
 	}
 	if (args.button2_label) {
 		wchar_t *str = str2wstr(args.button2_label, nullptr);
@@ -257,7 +282,17 @@
 			evt.intval = 2;
 		}
 
+		if (args.input) {
+			std::wstring wstr(textfield.Text());
+			char *text = wchar2utf8(wstr.c_str(), wstr.length());
+			evt.eventdata = text;
+		}
+
 		args.result(&evt, args.resultdata);
+
+		if (evt.eventdata) {
+			free(evt.eventdata);
+		}
 	}
 }
 

mercurial