1 //this file is part of notepad++
2 //Copyright (C)2003 Don HO <donho@altern.org>
3 //
4 //This program is free software; you can redistribute it and/or
5 //modify it under the terms of the GNU General Public License
6 //as published by the Free Software Foundation; either
7 //version 2 of the License, or (at your option) any later version.
8 //
9 //This program is distributed in the hope that it will be useful,
10 //but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //GNU General Public License for more details.
13 //
14 //You should have received a copy of the GNU General Public License
15 //along with this program; if not, write to the Free Software
16 //Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 /**
18  * general plugin functions
19  *
20  * Author: dokutoku, https://twitter.com/dokutoku3
21  * License: GPL-2.0 or later
22  */
23 module npp_api.pluginfunc.basic;
24 
25 
26 version (Windows):
27 
28 pragma(lib, "kernel32.lib");
29 pragma(lib, "user32.lib");
30 
31 private static import core.sys.windows.winbase;
32 private static import core.sys.windows.windef;
33 private static import core.sys.windows.winnt;
34 private static import core.sys.windows.winuser;
35 private static import std.format;
36 private static import std.utf;
37 private static import npp_api.pluginfunc.string;
38 
39 //plugin func
40 mixin template plugin_messageBox(wstring plugin_name)
41 {
42 	private static import core.sys.windows.winbase;
43 	private static import core.sys.windows.windef;
44 	private static import core.sys.windows.winnt;
45 	private static import core.sys.windows.winuser;
46 	private static import std.format;
47 	private static import std.utf;
48 	private static import npp_api.pluginfunc.string;
49 
50 	static assert(plugin_name.length != 0);
51 	static assert(plugin_name[$ - 1] == '\0');
52 
53 	/**
54 	 * show message
55 	 */
56 	nothrow
57 	void msgbox(C)(const (C)[] message)
58 
59 		in
60 		{
61 			static import std.utf;
62 			static import std.exception;
63 
64 			std.exception.assertNotThrown(std.utf.validate(message));
65 		}
66 
67 		do
68 		{
69 			static import core.sys.windows.winuser;
70 			static import std.utf;
71 
72 			try {
73 				core.sys.windows.winuser.MessageBoxW(.nppData._nppHandle, std.utf.toUTF16z(message), &(plugin_name[0]), core.sys.windows.winuser.MB_OK);
74 			} catch (Exception e) {
75 			}
76 		}
77 
78 	nothrow @nogc
79 	trusted_msgbox(const wchar[] c_string)
80 
81 		in
82 		{
83 			assert(c_string.length != 0);
84 			assert(c_string[$ - 1] == '\0');
85 		}
86 
87 		do
88 		{
89 			static import core.sys.windows.winuser;
90 
91 			core.sys.windows.winuser.MessageBoxW(.nppData._nppHandle, &(c_string[0]), &(plugin_name[0]), core.sys.windows.winuser.MB_OK);
92 		}
93 
94 	/**
95 	 * Windowsのシステムエラーメッセージを表示する関数
96 	 */
97 	nothrow @nogc
98 	void show_windows_error(core.sys.windows.windef.DWORD lang_id = 0)
99 
100 		do
101 		{
102 			static import core.sys.windows.winbase;
103 			static import core.sys.windows.windef;
104 			static import core.sys.windows.winnt;
105 			static import core.sys.windows.winuser;
106 			static import npp_api.pluginfunc.string;
107 
108 			core.sys.windows.windef.DWORD error_code = core.sys.windows.winbase.GetLastError();
109 
110 			if (error_code != 0) {
111 				core.sys.windows.winnt.WCHAR[512] message = '\0';
112 
113 				if ((core.sys.windows.winbase.FormatMessageW(core.sys.windows.winbase.FORMAT_MESSAGE_FROM_SYSTEM, core.sys.windows.windef.NULL, error_code, lang_id, &(message[0]), message.length, core.sys.windows.windef.NULL)) == 0) {
114 					//show error code
115 					core.sys.windows.winuser.wsprintfW(&(message[0]), &(npp_api.pluginfunc..string.c_wstring!(`Windows Error Code: 0x%X`w)[0]), error_code);
116 					core.sys.windows.winuser.MessageBoxW(.nppData._nppHandle, &(message[0]), &(plugin_name[0]), core.sys.windows.winuser.MB_OK);
117 				} else {
118 					//show error message
119 					core.sys.windows.winuser.MessageBoxW(.nppData._nppHandle, &(message[0]), &(plugin_name[0]), core.sys.windows.winuser.MB_OK);
120 				}
121 			}
122 		}
123 
124 	pure nothrow @safe @nogc
125 	void debug_format_msgbox(wstring format, A...)(A value)
126 
127 		do
128 		{
129 			static import core.sys.windows.winuser;
130 			static import std.format;
131 			static import std.utf;
132 			static import npp_api.pluginfunc.string;
133 
134 			debug {
135 				try {
136 					core.sys.windows.winuser.MessageBoxW(.nppData._nppHandle, std.utf.toUTF16z(std.format.format!(format)(value[0 .. $])), &(plugin_name[0]), core.sys.windows.winuser.MB_OK);
137 				} catch (Exception e) {
138 					core.sys.windows.winuser.MessageBoxW(.nppData._nppHandle, &(npp_api.pluginfunc..string.c_wstring!(`debug message Exception!`w)[0]), &(plugin_name[0]), core.sys.windows.winuser.MB_OK);
139 				}
140 			}
141 		}
142 }
143 
144 
145 /**
146  * show message
147  */
148 nothrow
149 void msgbox(C1, C2)(const (C1)[] message, const (C2)[] title)
150 
151 	in
152 	{
153 		static import std.utf;
154 		static import std.exception;
155 
156 		std.exception.assertNotThrown(std.utf.validate(message));
157 	}
158 
159 	do
160 	{
161 		static import core.sys.windows.winuser;
162 		static import std.utf;
163 
164 		try {
165 			core.sys.windows.winuser.MessageBoxW(.nppData._nppHandle, std.utf.toUTF16z(message), std.utf.toUTF16z(title), core.sys.windows.winuser.MB_OK);
166 		} catch (Exception e) {
167 		}
168 	}
169 
170 /**
171  * Windowsのシステムエラーメッセージを表示する関数
172  */
173 nothrow @nogc
174 void show_windows_error(core.sys.windows.windef.HWND _nppHandle, const wchar[] title, core.sys.windows.windef.DWORD lang_id = 0)
175 
176 	in
177 	{
178 		assert(title != null);
179 		assert(title[$ - 1] == '\0');
180 	}
181 
182 	do
183 	{
184 		static import core.sys.windows.winbase;
185 		static import core.sys.windows.windef;
186 		static import core.sys.windows.winnt;
187 		static import core.sys.windows.winuser;
188 		static import npp_api.pluginfunc.string;
189 
190 		core.sys.windows.windef.DWORD error_code = core.sys.windows.winbase.GetLastError();
191 
192 		if (error_code != 0) {
193 			core.sys.windows.winnt.WCHAR[512] message = '\0';
194 
195 			if ((core.sys.windows.winbase.FormatMessageW(core.sys.windows.winbase.FORMAT_MESSAGE_FROM_SYSTEM, core.sys.windows.windef.NULL, error_code, lang_id, &(message[0]), message.length, core.sys.windows.windef.NULL)) == 0) {
196 				//show error code
197 				core.sys.windows.winuser.wsprintfW(&(message[0]), &(npp_api.pluginfunc..string.c_wstring!(`Windows Error Code: 0x%X`w)[0]), error_code);
198 				core.sys.windows.winuser.MessageBoxW(_nppHandle, &(message[0]), &(title[0]), core.sys.windows.winuser.MB_OK);
199 			} else {
200 				//show error message
201 				core.sys.windows.winuser.MessageBoxW(_nppHandle, &(message[0]), &(title[0]), core.sys.windows.winuser.MB_OK);
202 			}
203 		}
204 	}
205 
206 pure nothrow @safe @nogc
207 void debug_format_msgbox(wstring format, A...)(core.sys.windows.windef.HWND _nppHandle, const wchar[] title, A value)
208 
209 	in
210 	{
211 		assert(title != null);
212 		assert(title[$ - 1] == '\0');
213 	}
214 
215 	do
216 	{
217 		static import core.sys.windows.winuser;
218 		static import std.format;
219 		static import std.utf;
220 		static import npp_api.pluginfunc.string;
221 
222 		debug {
223 			try {
224 				core.sys.windows.winuser.MessageBoxW(_nppHandle, std.utf.toUTF16z(std.format.format!(format)(value[0 .. $])), &(title[0]), core.sys.windows.winuser.MB_OK);
225 			} catch (Exception e) {
226 				core.sys.windows.winuser.MessageBoxW(_nppHandle, &(npp_api.pluginfunc..string.c_wstring!(`debug message Exception!`w)[0]), &(title[0]), core.sys.windows.winuser.MB_OK);
227 			}
228 		}
229 	}