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");
29 //pragma(lib, "user32");
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 npp_api.pluginfunc.string;
36 private static import std.exception;
37 private static import std.format;
38 private static import std.utf;
39 
40 //plugin func
41 mixin template plugin_messageBox(wstring plugin_name)
42 {
43 	private static import core.sys.windows.winbase;
44 	private static import core.sys.windows.windef;
45 	private static import core.sys.windows.winnt;
46 	private static import core.sys.windows.winuser;
47 	private static import npp_api.pluginfunc.string;
48 	private static import std.exception;
49 	private static import std.format;
50 	private static import std.utf;
51 
52 	static assert(plugin_name.length != 0);
53 	static assert(plugin_name[$ - 1] == '\0');
54 
55 	/**
56 	 * show message
57 	 */
58 	nothrow
59 	void msgbox(C)(const (C)[] message)
60 
61 		in
62 		{
63 			std.exception.assertNotThrown(std.utf.validate(message));
64 		}
65 
66 		do
67 		{
68 			try {
69 				core.sys.windows.winuser.MessageBoxW(.nppData._nppHandle, std.utf.toUTF16z(message), &(plugin_name[0]), core.sys.windows.winuser.MB_OK);
70 			} catch (Exception e) {
71 			}
72 		}
73 
74 	nothrow @nogc
75 	trusted_msgbox(const wchar[] c_string)
76 
77 		in
78 		{
79 			assert(c_string.length != 0);
80 			assert(c_string[$ - 1] == '\0');
81 		}
82 
83 		do
84 		{
85 			core.sys.windows.winuser.MessageBoxW(.nppData._nppHandle, &(c_string[0]), &(plugin_name[0]), core.sys.windows.winuser.MB_OK);
86 		}
87 
88 	/**
89 	 * Windowsのシステムエラーメッセージを表示する関数
90 	 */
91 	nothrow @nogc
92 	void show_windows_error(core.sys.windows.windef.DWORD lang_id = 0)
93 
94 		do
95 		{
96 			core.sys.windows.windef.DWORD error_code = core.sys.windows.winbase.GetLastError();
97 
98 			if (error_code != 0) {
99 				core.sys.windows.winnt.WCHAR[512] message = '\0';
100 
101 				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) {
102 					//show error code
103 					core.sys.windows.winuser.wsprintfW(&(message[0]), &(npp_api.pluginfunc..string.c_wstring!(`Windows Error Code: 0x%X`w)[0]), error_code);
104 					core.sys.windows.winuser.MessageBoxW(.nppData._nppHandle, &(message[0]), &(plugin_name[0]), core.sys.windows.winuser.MB_OK);
105 				} else {
106 					//show error message
107 					core.sys.windows.winuser.MessageBoxW(.nppData._nppHandle, &(message[0]), &(plugin_name[0]), core.sys.windows.winuser.MB_OK);
108 				}
109 			}
110 		}
111 
112 	pure nothrow @safe @nogc
113 	void debug_format_msgbox(wstring format, A...)(A value)
114 
115 		do
116 		{
117 			debug {
118 				try {
119 					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);
120 				} catch (Exception e) {
121 					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);
122 				}
123 			}
124 		}
125 }
126 
127 
128 /**
129  * show message
130  */
131 nothrow
132 void msgbox(C1, C2)(const (C1)[] message, const (C2)[] title)
133 
134 	in
135 	{
136 		std.exception.assertNotThrown(std.utf.validate(message));
137 	}
138 
139 	do
140 	{
141 		try {
142 			core.sys.windows.winuser.MessageBoxW(.nppData._nppHandle, std.utf.toUTF16z(message), std.utf.toUTF16z(title), core.sys.windows.winuser.MB_OK);
143 		} catch (Exception e) {
144 		}
145 	}
146 
147 /**
148  * Windowsのシステムエラーメッセージを表示する関数
149  */
150 nothrow @nogc
151 void show_windows_error(core.sys.windows.windef.HWND _nppHandle, const wchar[] title, core.sys.windows.windef.DWORD lang_id = 0)
152 
153 	in
154 	{
155 		assert(title != null);
156 		assert(title[$ - 1] == '\0');
157 	}
158 
159 	do
160 	{
161 		core.sys.windows.windef.DWORD error_code = core.sys.windows.winbase.GetLastError();
162 
163 		if (error_code != 0) {
164 			core.sys.windows.winnt.WCHAR[512] message = '\0';
165 
166 			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) {
167 				//show error code
168 				core.sys.windows.winuser.wsprintfW(&(message[0]), &(npp_api.pluginfunc..string.c_wstring!(`Windows Error Code: 0x%X`w)[0]), error_code);
169 				core.sys.windows.winuser.MessageBoxW(_nppHandle, &(message[0]), &(title[0]), core.sys.windows.winuser.MB_OK);
170 			} else {
171 				//show error message
172 				core.sys.windows.winuser.MessageBoxW(_nppHandle, &(message[0]), &(title[0]), core.sys.windows.winuser.MB_OK);
173 			}
174 		}
175 	}
176 
177 pure nothrow @safe @nogc
178 void debug_format_msgbox(wstring format, A...)(core.sys.windows.windef.HWND _nppHandle, const wchar[] title, A value)
179 
180 	in
181 	{
182 		assert(title != null);
183 		assert(title[$ - 1] == '\0');
184 	}
185 
186 	do
187 	{
188 		debug {
189 			try {
190 				core.sys.windows.winuser.MessageBoxW(_nppHandle, std.utf.toUTF16z(std.format.format!(format)(value[0 .. $])), &(title[0]), core.sys.windows.winuser.MB_OK);
191 			} catch (Exception e) {
192 				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);
193 			}
194 		}
195 	}