1 // Copyright (c) 2017 Matthew Brennan Jones <matthew.brennan.jones@gmail.com>
2 // Boost Software License - Version 1.0
3 // A simple message box for the D programming language
4 // https://github.com/workhorsy/d-message-box
5 
6 
7 module message_box_dlangui;
8 
9 import message_box : MessageBoxBase, IconType, use_log, is_sdl2_loadable;
10 
11 
12 class MessageBoxDlangUI : MessageBoxBase {
13 	this(string exe_dir, string title, string message, IconType icon_type) {
14 		super(title, message, icon_type);
15 		_exe_dir = exe_dir;
16 	}
17 
18 	override void show() {
19 		import std.process : pipeProcess, wait, Redirect;
20 		import std.string : format;
21 		import std.file : exists;
22 		import std.path : buildPath;
23 		import message_box_helpers : programPaths, logProgramOutput;
24 
25 		// Find the message box program
26 		string path = buildPath(_exe_dir, "message_box_dlangui.exe");
27 		if (! exists(path)) {
28 			this.fireOnError(new Exception("Failed to find message_box_dlangui.exe."));
29 			return;
30 		}
31 
32 		// Get the icon type arg
33 		string icon_type = "";
34 		final switch (_icon_type) {
35 			case IconType.None: icon_type = "None"; break;
36 			case IconType.Information: icon_type = "Information"; break;
37 			case IconType.Error: icon_type = "Error"; break;
38 			case IconType.Warning: icon_type = "Warning"; break;
39 		}
40 
41 		// Create all the command line args
42 		string[] args = [
43 			path,
44 			`--title=%s`.format(_title),
45 			`--message=%s`.format(_message),
46 			`--icon_type=%s`.format(icon_type)
47 		];
48 
49 		// Run the message box program
50 		auto pipes = pipeProcess(args, Redirect.stdin | Redirect.stdout | Redirect.stderr);
51 		int status = wait(pipes.pid);
52 		if (use_log) {
53 			logProgramOutput(pipes);
54 		}
55 		if (status != 0) {
56 			this.fireOnError(new Exception("Failed to show the dlangui message box."));
57 		}
58 	}
59 
60 	static bool isSupported() {
61 		version (Windows) {
62 			return true;
63 		} else {
64 			return is_sdl2_loadable;
65 		}
66 	}
67 
68 	string _exe_dir;
69 }