StyledEdit::StatusView Encoding context menu implemented

This commit is contained in:
Siarzhuk Zharski 2013-01-16 14:51:41 +01:00
parent c45e8003d3
commit 8a85cd4ce8
4 changed files with 37 additions and 25 deletions

View File

@ -26,6 +26,7 @@
#include <Window.h> #include <Window.h>
#include "Constants.h" #include "Constants.h"
#include "StyledEditWindow.h"
const float kHorzSpacing = 5.f; const float kHorzSpacing = 5.f;
@ -158,17 +159,19 @@ StatusView::MouseDown(BPoint where)
if (!fReadOnly) if (!fReadOnly)
return; return;
float left = fCellWidth[kPositionCell] + fCellWidth[kEncodingCell]; if (where.x < fCellWidth[kPositionCell])
if (where.x < left)
return; 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"),
new BMessage(UNLOCK_FILE)));
where.x = left; where.x = left;
where.y = Bounds().bottom; where.y = Bounds().bottom;
BPopUpMenu *menu = new BPopUpMenu(B_EMPTY_STRING, false, false);
menu->AddItem(new BMenuItem(B_TRANSLATE("Unlock file"),
new BMessage(UNLOCK_FILE)));
ConvertToScreen(&where); ConvertToScreen(&where);
menu->SetTargetForItems(this); menu->SetTargetForItems(this);
menu->Go(where, true, true, true); menu->Go(where, true, true, true);
@ -188,20 +191,21 @@ StatusView::SetStatus(BMessage* message)
fCellText[kPositionCell].SetTo(info); fCellText[kPositionCell].SetTo(info);
} }
BString encoding; if (B_OK == message->FindString("encoding", &fEncoding)) {
if (B_OK == message->FindString("encoding", &encoding)) {
// sometime corresponding Int-32 "encoding" attrib is read as string :( // sometime corresponding Int-32 "encoding" attrib is read as string :(
if (encoding.Length() == 0 if (fEncoding.Length() == 0
|| encoding.Compare("\xff\xff") == 0 || fEncoding.Compare("\xff\xff") == 0
|| encoding.Compare("UTF-8") == 0) || fEncoding.Compare("UTF-8") == 0)
{ {
fCellText[kEncodingCell] = "UTF-8"; fCellText[kEncodingCell] = "UTF-8";
fEncoding.Truncate(0);
} else { } else {
const BCharacterSet* charset const BCharacterSet* charset
= BCharacterSetRoster::FindCharacterSetByName(encoding); = BCharacterSetRoster::FindCharacterSetByName(fEncoding);
fCellText[kEncodingCell] fCellText[kEncodingCell]
= charset != NULL ? charset->GetPrintName() : ""; = charset != NULL ? charset->GetPrintName() : "";
} }
fCellText[kEncodingCell] << " " UTF8_EXPAND_ARROW;
} }
bool modified = false; bool modified = false;

View File

@ -45,6 +45,7 @@ private:
BString fCellText[kStatusCellCount]; BString fCellText[kStatusCellCount];
float fCellWidth[kStatusCellCount]; float fCellWidth[kStatusCellCount];
bool fReadOnly; bool fReadOnly;
BString fEncoding;
}; };
#endif // STATUS_VIEW_H #endif // STATUS_VIEW_H

View File

@ -287,8 +287,8 @@ StyledEditWindow::MessageReceived(BMessage* message)
case MSG_REPLACE_ALL: case MSG_REPLACE_ALL:
{ {
message->FindBool("casesens", &fCaseSensitive); message->FindBool("casesens", &fCaseSensitive);
message->FindString("FindText",&fStringToFind); message->FindString("FindText", &fStringToFind);
message->FindString("ReplaceText",&fReplaceString); message->FindString("ReplaceText", &fReplaceString);
bool allWindows; bool allWindows;
message->FindBool("allwindows", &allWindows); message->FindBool("allwindows", &allWindows);
@ -1291,7 +1291,9 @@ StyledEditWindow::_InitWindow(uint32 encoding)
fWrapItem->SetMarked(true); fWrapItem->SetMarked(true);
fWrapItem->SetShortcut('W', B_OPTION_KEY); fWrapItem->SetShortcut('W', B_OPTION_KEY);
menu->AddItem(fEncodingItem = _MakeEncodingMenuItem()); menu->AddItem(fEncodingItem = new BMenuItem(PopulateEncodingMenu(
new BMenu(B_TRANSLATE("Text encoding")), "UTF-8"),
new BMessage(MENU_RELOAD)));
fEncodingItem->SetEnabled(false); fEncodingItem->SetEnabled(false);
menu->AddSeparatorItem(); menu->AddSeparatorItem();
@ -1564,7 +1566,7 @@ StyledEditWindow::_UnlockFile()
status_t status = dir.InitCheck(); status_t status = dir.InitCheck();
if (status != B_OK) if (status != B_OK)
return status; return status;
status = entry.InitCheck(); status = entry.InitCheck();
if (status != B_OK) if (status != B_OK)
return status; return status;
@ -1578,7 +1580,7 @@ StyledEditWindow::_UnlockFile()
status = file.GetStat(&st); status = file.GetStat(&st);
if (status != B_OK) if (status != B_OK)
return status; return status;
st.st_mode |= S_IWUSR; st.st_mode |= S_IWUSR;
status = file.SetPermissions(st.st_mode); status = file.SetPermissions(st.st_mode);
if (status == B_OK) if (status == B_OK)
@ -1882,10 +1884,13 @@ StyledEditWindow::_ShowAlert(const BString& text, const BString& label,
} }
BMenuItem* BMenu*
StyledEditWindow::_MakeEncodingMenuItem() StyledEditWindow::PopulateEncodingMenu(BMenu* menu, const char* currentEncoding)
{ {
BMenu* menu = new BMenu(B_TRANSLATE("Text encoding")); menu->SetRadioMode(true);
BString encoding(currentEncoding);
if (encoding.Length() == 0)
encoding.SetTo("UTF-8");
BCharacterSetRoster roster; BCharacterSetRoster roster;
BCharacterSet charset; BCharacterSet charset;
@ -1899,7 +1904,10 @@ StyledEditWindow::_MakeEncodingMenuItem()
BMessage *message = new BMessage(MENU_RELOAD); BMessage *message = new BMessage(MENU_RELOAD);
if (message != NULL) { if (message != NULL) {
message->AddString("encoding", charset.GetName()); message->AddString("encoding", charset.GetName());
menu->AddItem(new BMenuItem(name, message)); BMenuItem* item = new BMenuItem(name, message);
if (encoding.Compare(charset.GetName()) == 0)
item->SetMarked(true);
menu->AddItem(item);
} }
} }
@ -1908,9 +1916,7 @@ StyledEditWindow::_MakeEncodingMenuItem()
message->AddString("encoding", "auto"); message->AddString("encoding", "auto");
menu->AddItem(new BMenuItem(B_TRANSLATE("Autodetect"), message)); menu->AddItem(new BMenuItem(B_TRANSLATE("Autodetect"), message));
menu->SetRadioMode(true); return menu;
return new BMenuItem(menu, new BMessage(MENU_RELOAD));
} }

View File

@ -49,9 +49,10 @@ public:
bool caseSensitive); bool caseSensitive);
bool IsDocumentEntryRef(const entry_ref* ref); bool IsDocumentEntryRef(const entry_ref* ref);
static BMenu* PopulateEncodingMenu(BMenu* menu,
const char* encoding);
private: private:
void _InitWindow(uint32 encoding = 0); void _InitWindow(uint32 encoding = 0);
BMenuItem* _MakeEncodingMenuItem();
void _LoadAttrs(); void _LoadAttrs();
void _SaveAttrs(); void _SaveAttrs();
status_t _LoadFile(entry_ref* ref, status_t _LoadFile(entry_ref* ref,