summaryrefslogtreecommitdiffstats
path: root/Examples/WhisperDesktop/Utils/LanguageDropdown.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/WhisperDesktop/Utils/LanguageDropdown.cpp')
-rw-r--r--Examples/WhisperDesktop/Utils/LanguageDropdown.cpp87
1 files changed, 87 insertions, 0 deletions
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<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() );
+} \ No newline at end of file