summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/windows/winutf8.cpp
blob: bfc1d86ad383fddc5a1f3912719f400f60951615 (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
//============================================================
//
//  winutf8.cpp - Win32 OSD core utility functions
//
//============================================================

// MAMEOS headers
#include "winutf8.h"
#include "strconv.h"

#include <cstdlib>

// standard windows headers
#include <shellapi.h>


//============================================================
//  win_output_debug_string_utf8
//============================================================

void win_output_debug_string_utf8(const char *string)
{
	auto t_string = osd::text::to_tstring(string);
	OutputDebugString(t_string.c_str());
}


#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)

//============================================================
//  win_message_box_utf8
//============================================================

int win_message_box_utf8(HWND window, const char *text, const char *caption, UINT type)
{
	LPCTSTR t_text = nullptr;
	LPCTSTR t_caption = nullptr;
	std::basic_string<TCHAR> ts_text;
	std::basic_string<TCHAR> ts_caption;

	if (text)
	{
		ts_text = osd::text::to_tstring(text);
		t_text = ts_text.c_str();
	}

	if (caption)
	{
		ts_caption = osd::text::to_tstring(caption);
		t_caption = ts_caption.c_str();
	}

	return MessageBox(window, t_text, t_caption, type);
}

#endif



//============================================================
//  win_set_window_text_utf8
//============================================================

BOOL win_set_window_text_utf8(HWND window, const char *text)
{
	BOOL result = FALSE;
	LPCTSTR t_text = nullptr;
	std::basic_string<TCHAR> ts_text;

	if (text)
	{
		ts_text = osd::text::to_tstring(text);
		t_text = ts_text.c_str();
	}

#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
	result = SetWindowText(window, t_text);
#else
	Windows::UI::ViewManagement::ApplicationView::GetForCurrentView()->Title = ref new Platform::String(t_text);
	result = TRUE;
#endif

	return result;
}



//============================================================
//  win_get_window_text_utf8
//============================================================

std::string win_get_window_text_utf8(HWND window)
{
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
	{
		// invoke the core Win32 API
		int length = GetWindowTextLength(window);
		if (length <= 0)
			return std::string();

		TCHAR *buffer = (TCHAR *) alloca((length + 1) * sizeof(TCHAR));
		GetWindowText(window, buffer, length + 1);
		return osd::text::from_tstring(buffer);
	}
#else
	{
		TCHAR t_buffer[256];
		auto title = Windows::UI::ViewManagement::ApplicationView::GetForCurrentView()->Title;
		wcsncpy(t_buffer, title->Data(), ARRAY_LENGTH(t_buffer));
		return osd::text::from_tstring(t_buffer);
	}
#endif
}


#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)

//============================================================
//  win_create_window_ex_utf8
//============================================================

HWND win_create_window_ex_utf8(DWORD exstyle, const char* classname, const char* windowname, DWORD style,
								int x, int y, int width, int height, HWND parent, HMENU menu,
								HINSTANCE instance, void* param)
{
	std::basic_string<TCHAR> ts_classname = osd::text::to_tstring(classname);

	LPCTSTR t_windowname = nullptr;
	std::basic_string<TCHAR> ts_windowname;
	if (windowname != nullptr)
	{
		ts_windowname = osd::text::to_tstring(windowname);
		t_windowname = ts_windowname.c_str();
	}

	return CreateWindowEx(exstyle, ts_classname.c_str(), t_windowname, style, x, y, width, height, parent,
		menu, instance, param);
}

#endif