summaryrefslogtreecommitdiffstats
path: root/Examples/WhisperDesktop/Utils/LanguageDropdown.cpp
blob: 36cd1a84302986f24d4adc694b081cd4346cede3 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "stdafx.h"
#include "LanguageDropdown.h"
#include "miscUtils.h"

namespace
{
	inline wchar_t toUpper( wchar_t c )
	{
		size_t st = (uint16_t)c;
		st = reinterpret_cast<size_t>( CharUpperW( reinterpret_cast<LPWSTR>( st ) ) );
		return (wchar_t)(uint16_t)st;
	}

	void makeTitleCase( CString& s )
	{
		bool cap = true;
		for( int i = 0; i < s.GetLength(); i++ )
		{
			wchar_t c = s[ i ];
			if( cap )
			{
				c = toUpper( c );
				s.SetAt( i, c );
			}
			cap = false;
			if( c == ' ' )
				cap = true;
		}
	}
}

int LanguageDropdown::getInitialSelection( AppState& state ) const
{
	constexpr uint32_t english = 0x6E65;

	// Load preference from the registry
	uint32_t id = state.languageRead();
	if( id == UINT_MAX )
		id = english;

	auto it = std::find( keys.begin(), keys.end(), id );
	if( it == keys.end() )
	{
		id = english;
		it = std::find( keys.begin(), keys.end(), id );
		assert( it != keys.end() );
	}

	ptrdiff_t idx = it - keys.begin();
	return (int)idx;
}

void LanguageDropdown::initialize( HWND owner, int idc, AppState& state )
{
	m_hWnd = GetDlgItem( owner, idc );
	assert( nullptr != m_hWnd );

	Whisper::sLanguageList list;
	Whisper::getSupportedLanguages( list );

	const size_t length = list.length;
	keys.resize( length );
	CString utf16;
	for( size_t i = 0; i < length; i++ )
	{
		keys[ i ] = list.pointer[ i ].key;
		makeUtf16( utf16, list.pointer[ i ].name );
		makeTitleCase( utf16 );
		SendMessage( m_hWnd, CB_ADDSTRING, 0, (LPARAM)cstr( utf16 ) );
	}

	const int curSel = getInitialSelection( state );
	SendMessage( m_hWnd, CB_SETCURSEL, curSel, 0 );
}

uint32_t LanguageDropdown::selectedLanguage()
{
	const int cs = (int)SendMessage( m_hWnd, CB_GETCURSEL, 0, 0 );
	if( cs < 0 || cs >= keys.size() )
		return UINT_MAX;
	return keys[ cs ];
}

void LanguageDropdown::saveSelection( AppState& state )
{
	state.languageWrite( selectedLanguage() );
}