From 8c4603c73675958efc960fbd4bb599a2909d106a Mon Sep 17 00:00:00 2001 From: Konstantin Date: Mon, 16 Jan 2023 14:52:43 +0100 Subject: Source codes --- Examples/WhisperDesktop/Utils/LanguageDropdown.cpp | 87 ++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 Examples/WhisperDesktop/Utils/LanguageDropdown.cpp (limited to 'Examples/WhisperDesktop/Utils/LanguageDropdown.cpp') diff --git a/Examples/WhisperDesktop/Utils/LanguageDropdown.cpp b/Examples/WhisperDesktop/Utils/LanguageDropdown.cpp new file mode 100644 index 0000000..36cd1a8 --- /dev/null +++ b/Examples/WhisperDesktop/Utils/LanguageDropdown.cpp @@ -0,0 +1,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( CharUpperW( reinterpret_cast( 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() ); +} \ No newline at end of file -- cgit v1.2.3