mirror of
https://github.com/SpartanJ/eepp.git
synced 2026-05-31 18:46:29 +03:00
File encoding picker.
This commit is contained in:
@@ -25,12 +25,16 @@ class EE_API TextFormat {
|
||||
LineEnding newLine = LineEnding::LF;
|
||||
bool bom = false;
|
||||
|
||||
static std::vector<std::pair<Encoding, std::string>> encodings();
|
||||
|
||||
static std::string lineEndingToString( const TextFormat::LineEnding& le );
|
||||
|
||||
static TextFormat::LineEnding stringToLineEnding( const std::string& str );
|
||||
|
||||
static std::string encodingToString( TextFormat::Encoding enc );
|
||||
|
||||
static Encoding encodingFromString( const std::string_view& str );
|
||||
|
||||
static TextFormat autodetect( IOStream& ins );
|
||||
};
|
||||
|
||||
|
||||
@@ -520,6 +520,20 @@ TextFormat::LineEnding TextFormat::stringToLineEnding( const std::string& str )
|
||||
return TextFormat::LineEnding::LF;
|
||||
}
|
||||
|
||||
TextFormat::Encoding TextFormat::encodingFromString( const std::string_view& str ) {
|
||||
switch ( String::hash( str ) ) {
|
||||
case static_cast<String::HashType>( TextFormat::Encoding::UTF16LE ):
|
||||
return TextFormat::Encoding::UTF16LE;
|
||||
case static_cast<String::HashType>( TextFormat::Encoding::UTF16BE ):
|
||||
return TextFormat::Encoding::UTF16BE;
|
||||
case static_cast<String::HashType>( TextFormat::Encoding::Latin1 ):
|
||||
return TextFormat::Encoding::Latin1;
|
||||
case static_cast<String::HashType>( TextFormat::Encoding::UTF8 ):
|
||||
default:
|
||||
return TextFormat::Encoding::UTF8;
|
||||
}
|
||||
}
|
||||
|
||||
std::string TextFormat::encodingToString( TextFormat::Encoding enc ) {
|
||||
switch ( enc ) {
|
||||
case TextFormat::Encoding::UTF16LE:
|
||||
@@ -535,4 +549,13 @@ std::string TextFormat::encodingToString( TextFormat::Encoding enc ) {
|
||||
return "UTF-8";
|
||||
}
|
||||
|
||||
std::vector<std::pair<TextFormat::Encoding, std::string>> TextFormat::encodings() {
|
||||
std::vector<std::pair<TextFormat::Encoding, std::string>> encs;
|
||||
encs.emplace_back( Encoding::UTF8, encodingToString( Encoding::UTF8 ) );
|
||||
encs.emplace_back( Encoding::UTF16BE, encodingToString( Encoding::UTF16BE ) );
|
||||
encs.emplace_back( Encoding::UTF16LE, encodingToString( Encoding::UTF16LE ) );
|
||||
encs.emplace_back( Encoding::Latin1, encodingToString( Encoding::Latin1 ) );
|
||||
return encs;
|
||||
}
|
||||
|
||||
}}} // namespace EE::UI::Doc
|
||||
|
||||
@@ -245,6 +245,34 @@ UIMenu* SettingsMenu::createDocumentMenu() {
|
||||
mApp->getConfig().doc.autoDetectIndentType )
|
||||
->setId( "auto_indent_cur" );
|
||||
|
||||
UIPopUpMenu* fileEncoding = UIPopUpMenu::New();
|
||||
auto encodings = TextFormat::encodings();
|
||||
for ( const auto& enc : encodings )
|
||||
fileEncoding->addRadioButton( enc.second )->setId( enc.second );
|
||||
mDocMenu->addSubMenu( i18n( "file_encoding", "File Encoding" ), nullptr, fileEncoding )
|
||||
->setId( "file_encoding" );
|
||||
fileEncoding->on( Event::OnItemClicked, [this]( const Event* event ) {
|
||||
const String& text = event->getNode()->asType<UIMenuRadioButton>()->getId();
|
||||
if ( mSplitter->curEditorExistsAndFocused() ) {
|
||||
auto enc = TextFormat::encodingFromString( text.toUtf8() );
|
||||
if ( enc == mSplitter->getCurEditor()->getDocument().getEncoding() )
|
||||
return;
|
||||
mSplitter->getCurEditor()->getDocument().setEncoding( enc );
|
||||
mApp->updateDocInfo( mSplitter->getCurEditor()->getDocument() );
|
||||
if ( !mSplitter->getCurEditor()->getDocument().hasFilepath() )
|
||||
return;
|
||||
auto msgBox = UIMessageBox::New(
|
||||
UIMessageBox::YES_NO, i18n( "confirm_new_file_encoding",
|
||||
"To confirm the new file encoding it's required to "
|
||||
"save the file. Do you want to save it now?" ) );
|
||||
msgBox->on( Event::OnConfirm, [this]( auto ) {
|
||||
if ( mSplitter->curEditorExistsAndFocused() )
|
||||
mSplitter->getCurEditor()->getDocument().save();
|
||||
} );
|
||||
msgBox->showWhenReady();
|
||||
}
|
||||
} );
|
||||
|
||||
UIPopUpMenu* tabTypeMenu = UIPopUpMenu::New();
|
||||
tabTypeMenu->addRadioButton( i18n( "tabs", "Tabs" ) )->setId( "tabs" );
|
||||
tabTypeMenu->addRadioButton( i18n( "spaces", "Spaces" ) )->setId( "spaces" );
|
||||
@@ -1561,6 +1589,13 @@ void SettingsMenu::updateDocumentMenu() {
|
||||
->asType<UIMenuCheckBox>()
|
||||
->setActive( doc.getAutoDetectIndentType() );
|
||||
|
||||
auto* curEncoding = mDocMenu->find( "file_encoding" )
|
||||
->asType<UIMenuSubMenu>()
|
||||
->getSubMenu()
|
||||
->find( TextFormat::encodingToString( doc.getEncoding() ) );
|
||||
if ( curEncoding )
|
||||
curEncoding->asType<UIMenuRadioButton>()->setActive( true );
|
||||
|
||||
auto* curIndent = mDocMenu->find( "indent_width_cur" )
|
||||
->asType<UIMenuSubMenu>()
|
||||
->getSubMenu()
|
||||
|
||||
Reference in New Issue
Block a user