summaryrefslogtreecommitdiff
path: root/src/libs/log/msgbox_win.c
blob: c4e0021ad69a8d7040eb74df2a8816ada014d4b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "msgbox.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdlib.h>
#include <malloc.h>


// Converts a UTF-8 string to Windows WideChar.
// Caller is responsible for free()ing the returned string.
static LPWSTR
toWideChar (const /*UTF-8*/char *str)
{
	int cch;
	LPWSTR wstr;

	cch = MultiByteToWideChar (CP_UTF8, 0, str, -1, NULL, 0);
	if (cch == 0)
		return NULL; // failed, probably no UTF8 converter

	wstr = malloc (cch * sizeof (WCHAR));
	if (!wstr)
		return NULL; // out of memory

	cch = MultiByteToWideChar (CP_UTF8, 0, str, -1, wstr, cch);
	if (cch == 0)
	{	// Failed. It should not fail here if it succeeded just above,
		// but it did. Not much can be done about it.
		free (wstr);
		return NULL;
	}

	return wstr;
}

void
log_displayBox (const /*UTF-8*/char *title, int isError,
		const /*UTF-8*/char *msg)
{
	LPWSTR swTitle = toWideChar (title);
	LPWSTR swMsg = toWideChar (msg);
	UINT uType = isError ? MB_ICONWARNING : MB_ICONINFORMATION;

	if (swTitle && swMsg)
		MessageBoxW (NULL, swMsg, swTitle, uType);
	else // Could not convert; let's try ASCII, though it may look ugly
		MessageBoxA (NULL, msg, title, uType);

	free (swTitle);
	free (swMsg);
}