From f8668ab42f2b09cdd3f7fe4964fe5485b2960dc2 Mon Sep 17 00:00:00 2001 From: Siarzhuk Zharski Date: Sun, 14 Apr 2013 12:55:46 +0200 Subject: [PATCH] Improve File Encoding StatusView cell. (Fixes #9653) * Encoding cell of the StyledEdit StatusView is visible now only in case the currently opened file encoding is not equal to default UTF-8 one; * The Encodings menu that was opened by click on this cell is removed; * Cmd-Opt-PgDn/PgUp shortcuts are added for quick iteration through the list of encodings. --- src/apps/stylededit/StatusView.cpp | 14 ++++------- src/apps/stylededit/StyledEditWindow.cpp | 32 ++++++++++++++++++++---- src/apps/stylededit/StyledEditWindow.h | 4 +-- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/apps/stylededit/StatusView.cpp b/src/apps/stylededit/StatusView.cpp index a6bdfccf76..ea29c26da8 100644 --- a/src/apps/stylededit/StatusView.cpp +++ b/src/apps/stylededit/StatusView.cpp @@ -26,7 +26,6 @@ #include #include "Constants.h" -#include "StyledEditWindow.h" const float kHorzSpacing = 5.f; @@ -159,7 +158,8 @@ StatusView::MouseDown(BPoint where) if (!fReadOnly) return; - if (where.x < fCellWidth[kPositionCell]) + float left = fCellWidth[kPositionCell] + fCellWidth[kEncodingCell]; + if (where.x < left) return; int32 clicks = 0; @@ -169,11 +169,7 @@ StatusView::MouseDown(BPoint where) return; BPopUpMenu *menu = new BPopUpMenu(B_EMPTY_STRING, false, false); - float left = fCellWidth[kPositionCell] + fCellWidth[kEncodingCell]; - if (where.x < left) - StyledEditWindow::PopulateEncodingMenu(menu, fEncoding); - else - menu->AddItem(new BMenuItem(B_TRANSLATE("Unlock file"), + menu->AddItem(new BMenuItem(B_TRANSLATE("Unlock file"), new BMessage(UNLOCK_FILE))); where.x = left; where.y = Bounds().bottom; @@ -203,7 +199,8 @@ StatusView::SetStatus(BMessage* message) || fEncoding.Compare("\xff\xff") == 0 || fEncoding.Compare("UTF-8") == 0) { - fCellText[kEncodingCell] = "UTF-8"; + // do not display default UTF-8 encoding + fCellText[kEncodingCell].Truncate(0); fEncoding.Truncate(0); } else { const BCharacterSet* charset @@ -211,7 +208,6 @@ StatusView::SetStatus(BMessage* message) fCellText[kEncodingCell] = charset != NULL ? charset->GetPrintName() : ""; } - fCellText[kEncodingCell] << " " UTF8_EXPAND_ARROW; } bool modified = false; diff --git a/src/apps/stylededit/StyledEditWindow.cpp b/src/apps/stylededit/StyledEditWindow.cpp index fac265180b..68986e8ac8 100644 --- a/src/apps/stylededit/StyledEditWindow.cpp +++ b/src/apps/stylededit/StyledEditWindow.cpp @@ -47,6 +47,7 @@ #include #include #include +#include using namespace BPrivate; @@ -1295,7 +1296,7 @@ StyledEditWindow::_InitWindow(uint32 encoding) BMessage *message = new BMessage(MENU_RELOAD); message->AddString("encoding", "auto"); - menu->AddItem(fEncodingItem = new BMenuItem(PopulateEncodingMenu( + menu->AddItem(fEncodingItem = new BMenuItem(_PopulateEncodingMenu( new BMenu(B_TRANSLATE("Text encoding")), "UTF-8"), message)); fEncodingItem->SetEnabled(false); @@ -1501,13 +1502,27 @@ StyledEditWindow::_ReloadDocument(BMessage* message) return; } + const BCharacterSet* charset + = BCharacterSetRoster::GetCharacterSetByFontID( + fTextView->GetEncoding()); const char* forceEncoding = NULL; if (message->FindString("encoding", &forceEncoding) != B_OK) { - const BCharacterSet* charset - = BCharacterSetRoster::GetCharacterSetByFontID( - fTextView->GetEncoding()); if (charset != NULL) forceEncoding = charset->GetName(); + } else { + if (charset != NULL) { + // UTF8 id assumed equal to -1 + const uint32 idUTF8 = -1; + uint32 id = charset->GetConversionID(); + if (strcmp(forceEncoding, "next") == 0) + id = id == B_MS_WINDOWS_1250_CONVERSION ? idUTF8 : id + 1; + else if (strcmp(forceEncoding, "previous") == 0) + id = id == idUTF8 ? B_MS_WINDOWS_1250_CONVERSION : id - 1; + const BCharacterSet* newCharset + = BCharacterSetRoster::GetCharacterSetByConversionID(id); + if (newCharset != NULL) + forceEncoding = newCharset->GetName(); + } } BScrollBar* vertBar = fScrollView->ScrollBar(B_VERTICAL); @@ -1888,7 +1903,7 @@ StyledEditWindow::_ShowAlert(const BString& text, const BString& label, BMenu* -StyledEditWindow::PopulateEncodingMenu(BMenu* menu, const char* currentEncoding) +StyledEditWindow::_PopulateEncodingMenu(BMenu* menu, const char* currentEncoding) { menu->SetRadioMode(true); BString encoding(currentEncoding); @@ -1919,6 +1934,13 @@ StyledEditWindow::PopulateEncodingMenu(BMenu* menu, const char* currentEncoding) message->AddString("encoding", "auto"); menu->AddItem(new BMenuItem(B_TRANSLATE("Autodetect"), message)); + message = new BMessage(MENU_RELOAD); + message->AddString("encoding", "next"); + AddShortcut(B_PAGE_DOWN, B_OPTION_KEY, message); + message = new BMessage(MENU_RELOAD); + message->AddString("encoding", "previous"); + AddShortcut(B_PAGE_UP, B_OPTION_KEY, message); + return menu; } diff --git a/src/apps/stylededit/StyledEditWindow.h b/src/apps/stylededit/StyledEditWindow.h index af8dc04f4b..bf239a6e84 100644 --- a/src/apps/stylededit/StyledEditWindow.h +++ b/src/apps/stylededit/StyledEditWindow.h @@ -49,8 +49,6 @@ public: bool caseSensitive); bool IsDocumentEntryRef(const entry_ref* ref); - static BMenu* PopulateEncodingMenu(BMenu* menu, - const char* encoding); private: void _InitWindow(uint32 encoding = 0); void _LoadAttrs(); @@ -79,6 +77,8 @@ private: const BString& label, const BString& label2, const BString& label3, alert_type type) const; + BMenu* _PopulateEncodingMenu(BMenu* menu, + const char* encoding); // node monitoring helper class _NodeMonitorSuspender {