moved encoding handling into StyledEditView and began reading the be:encoding attribute

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3866 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
shatty 2003-07-06 04:05:22 +00:00
parent 62ab1aa585
commit 670567f9d4
7 changed files with 100 additions and 30 deletions

View File

@ -59,6 +59,8 @@ const uint32 DISABLE_ITEMS ='DIit';
const uint32 CHANGE_WINDOW ='CHwi'; const uint32 CHANGE_WINDOW ='CHwi';
const uint32 TEXT_CHANGED ='TEch'; const uint32 TEXT_CHANGED ='TEch';
const uint32 SAVE_AS_ENCODING ='SAen'; // file panel constants
const uint32 OPEN_AS_ENCODING ='FPoe';
const uint32 SAVE_AS_ENCODING ='FPse';
#endif // CONSTANTS_H #endif // CONSTANTS_H

View File

@ -1,6 +1,8 @@
#include <Autolock.h> #include <Autolock.h>
#include <Path.h> #include <Path.h>
#include <MenuItem.h>
#include "CharacterSet.h"
#include "Constants.h" #include "Constants.h"
#include "StyledEditApp.h" #include "StyledEditApp.h"
#include "StyledEditWindow.h" #include "StyledEditWindow.h"
@ -16,10 +18,31 @@ StyledEditApp::StyledEditApp()
BMenuBar * menuBar = BMenuBar * menuBar =
dynamic_cast<BMenuBar*>(fOpenPanel->Window()->FindView("MenuBar")); dynamic_cast<BMenuBar*>(fOpenPanel->Window()->FindView("MenuBar"));
fOpenAsEncoding = 0;
fOpenPanelEncodingMenu= new BMenu("Encoding"); fOpenPanelEncodingMenu= new BMenu("Encoding");
menuBar->AddItem(fOpenPanelEncodingMenu); menuBar->AddItem(fOpenPanelEncodingMenu);
fOpenPanelEncodingMenu->SetRadioMode(true);
// TODO: add encodings status_t status = B_OK;
CharacterSetRoster * roster = CharacterSetRoster::Roster(&status);
if (status == B_OK) {
for (int index = 0; (index < roster->GetCharacterSetCount()) ; index++) {
const CharacterSet * cs = roster->GetCharacterSet(index);
BString name(cs->GetPrintName());
const char * mime = cs->GetMIMEName();
if (mime) {
name.Append(" (");
name.Append(mime);
name.Append(")");
}
BMenuItem * item = new BMenuItem(name.String(),new BMessage(OPEN_AS_ENCODING));
item->SetTarget(this);
fOpenPanelEncodingMenu->AddItem(item);
if (index == fOpenAsEncoding) {
item->SetMarked(true);
}
}
}
fWindowCount= 0; fWindowCount= 0;
fNext_Untitled_Window= 1; fNext_Untitled_Window= 1;
@ -63,6 +86,14 @@ StyledEditApp::MessageReceived(BMessage *message)
case B_SILENT_RELAUNCH: case B_SILENT_RELAUNCH:
OpenDocument(); OpenDocument();
break; break;
case OPEN_AS_ENCODING:
void * ptr;
if (message->FindPointer("source",&ptr) == B_OK) {
if (fOpenPanelEncodingMenu != 0) {
fOpenAsEncoding = (uint32)fOpenPanelEncodingMenu->IndexOf((BMenuItem*)ptr);
}
}
break;
default: default:
BApplication::MessageReceived(message); BApplication::MessageReceived(message);
break; break;
@ -72,7 +103,7 @@ StyledEditApp::MessageReceived(BMessage *message)
void void
StyledEditApp::OpenDocument() StyledEditApp::OpenDocument()
{ {
new StyledEditWindow(windowRect,fNext_Untitled_Window++); new StyledEditWindow(windowRect,fNext_Untitled_Window++,fOpenAsEncoding);
windowRect.OffsetBy(15,15); // TODO: wrap around screen windowRect.OffsetBy(15,15); // TODO: wrap around screen
fWindowCount++; fWindowCount++;
} }
@ -80,7 +111,7 @@ StyledEditApp::OpenDocument()
void void
StyledEditApp::OpenDocument(entry_ref * ref) StyledEditApp::OpenDocument(entry_ref * ref)
{ {
new StyledEditWindow(windowRect,ref); new StyledEditWindow(windowRect,ref,fOpenAsEncoding);
windowRect.OffsetBy(15,15); // TODO: wrap around screen windowRect.OffsetBy(15,15); // TODO: wrap around screen
fWindowCount++; fWindowCount++;
} }

View File

@ -28,6 +28,7 @@ public:
private: private:
BFilePanel *fOpenPanel; BFilePanel *fOpenPanel;
BMenu *fOpenPanelEncodingMenu; BMenu *fOpenPanelEncodingMenu;
uint32 fOpenAsEncoding;
int32 fWindowCount; int32 fWindowCount;
int32 fNext_Untitled_Window; int32 fNext_Untitled_Window;

View File

@ -18,6 +18,7 @@ StyledEditView::StyledEditView(BRect viewFrame, BRect textBounds, BHandler *hand
fHandler= handler; fHandler= handler;
fMessenger= new BMessenger(handler); fMessenger= new BMessenger(handler);
fSuppressChanges = false; fSuppressChanges = false;
fEncoding = 0;
}/***StyledEditView()***/ }/***StyledEditView()***/
StyledEditView::~StyledEditView(){ StyledEditView::~StyledEditView(){
@ -60,10 +61,28 @@ StyledEditView::GetStyledText(BPositionIO * stream)
status_t result = B_OK; status_t result = B_OK;
fSuppressChanges = true; fSuppressChanges = true;
result = BTranslationUtils::GetStyledText(stream, this, NULL); result = BTranslationUtils::GetStyledText(stream, this, NULL);
BNode * node = dynamic_cast<BNode*>(stream); BNode * node = dynamic_cast<BNode*>(stream);
if (node != 0) { if (node != 0) {
ssize_t bytesRead; ssize_t bytesRead;
// decode encoding
int32 encoding;
bytesRead = node->ReadAttr("be:encoding",0,0,&encoding,sizeof(encoding));
if (bytesRead > 0) {
CharacterSetRoster * roster = CharacterSetRoster::Roster(&result);
if (result != B_OK) {
return result;
}
if (encoding == 65535) {
fEncoding = 0;
} else {
const CharacterSet * cs = roster->FindCharacterSetByConversionID(encoding);
if (cs != 0) {
fEncoding = cs->GetFontID();
}
}
}
// restore alignment // restore alignment
alignment align; alignment align;
bytesRead = node->ReadAttr("alignment",0,0,&align,sizeof(align)); bytesRead = node->ReadAttr("alignment",0,0,&align,sizeof(align));
@ -91,14 +110,14 @@ StyledEditView::GetStyledText(BPositionIO * stream)
} }
status_t status_t
StyledEditView::WriteStyledEditFile(BFile * file, uint32 charSet) StyledEditView::WriteStyledEditFile(BFile * file)
{ {
status_t result = B_OK; status_t result = B_OK;
result = BTranslationUtils::WriteStyledEditFile(this,file); result = BTranslationUtils::WriteStyledEditFile(this,file);
if (result != B_OK) { if (result != B_OK) {
return result; return result;
} }
if (charSet == 0) { if (fEncoding == 0) {
int32 encoding = 65535; int32 encoding = 65535;
file->WriteAttr("be:encoding",B_INT32_TYPE,0,&encoding,sizeof(encoding)); file->WriteAttr("be:encoding",B_INT32_TYPE,0,&encoding,sizeof(encoding));
} else { } else {
@ -114,7 +133,7 @@ StyledEditView::WriteStyledEditFile(BFile * file, uint32 charSet)
if (result != B_OK) { if (result != B_OK) {
return result; return result;
} }
uint32 id = roster->GetCharacterSet(charSet)->GetConversionID(); uint32 id = roster->FindCharacterSetByFontID(fEncoding)->GetConversionID();
const char * outText = Text(); const char * outText = Text();
int32 sourceLength = TextLength(); int32 sourceLength = TextLength();
int32 state = 0; int32 state = 0;
@ -161,7 +180,20 @@ StyledEditView::Select(int32 start, int32 finish)
BTextView::Select(start, finish); BTextView::Select(start, finish);
} }
void StyledEditView::InsertText(const char *text, int32 length, int32 offset, const text_run_array *runs) void
StyledEditView::SetEncoding(uint32 encoding)
{
fEncoding = encoding;
}
uint32
StyledEditView::GetEncoding() const
{
return fEncoding;
}
void
StyledEditView::InsertText(const char *text, int32 length, int32 offset, const text_run_array *runs)
{ {
if (!fSuppressChanges) if (!fSuppressChanges)
fMessenger-> SendMessage(new BMessage(TEXT_CHANGED)); fMessenger-> SendMessage(new BMessage(TEXT_CHANGED));
@ -170,7 +202,8 @@ void StyledEditView::InsertText(const char *text, int32 length, int32 offset, co
}/****StyledEditView::InsertText()***/ }/****StyledEditView::InsertText()***/
void StyledEditView::DeleteText(int32 start, int32 finish) void
StyledEditView::DeleteText(int32 start, int32 finish)
{ {
if (!fSuppressChanges) if (!fSuppressChanges)
fMessenger-> SendMessage(new BMessage(TEXT_CHANGED)); fMessenger-> SendMessage(new BMessage(TEXT_CHANGED));

View File

@ -15,7 +15,10 @@ public:
virtual void Reset(); virtual void Reset();
virtual status_t GetStyledText(BPositionIO * stream); virtual status_t GetStyledText(BPositionIO * stream);
virtual status_t WriteStyledEditFile(BFile * file, uint32 charSet = 0); virtual status_t WriteStyledEditFile(BFile * file);
virtual void SetEncoding(uint32 encoding);
virtual uint32 GetEncoding() const;
protected: protected:
virtual void InsertText(const char *text, int32 length, int32 offset, const text_run_array *runs); virtual void InsertText(const char *text, int32 length, int32 offset, const text_run_array *runs);
virtual void DeleteText(int32 start, int32 finish); virtual void DeleteText(int32 start, int32 finish);
@ -25,6 +28,7 @@ private:
BMessage *fChangeMessage; BMessage *fChangeMessage;
BMessenger *fMessenger; BMessenger *fMessenger;
bool fSuppressChanges; bool fSuppressChanges;
uint32 fEncoding;
}; };
#endif // STYLED_EDIT_VIEW_H #endif // STYLED_EDIT_VIEW_H

View File

@ -26,10 +26,10 @@
#include "StyledEditView.h" #include "StyledEditView.h"
#include "StyledEditWindow.h" #include "StyledEditWindow.h"
StyledEditWindow::StyledEditWindow(BRect frame, int32 id) StyledEditWindow::StyledEditWindow(BRect frame, int32 id, uint32 encoding)
: BWindow(frame,"untitled",B_DOCUMENT_WINDOW,0) : BWindow(frame,"untitled",B_DOCUMENT_WINDOW,0)
{ {
InitWindow(); InitWindow(encoding);
BString unTitled; BString unTitled;
unTitled.SetTo("Untitled "); unTitled.SetTo("Untitled ");
unTitled << id; unTitled << id;
@ -38,10 +38,10 @@ StyledEditWindow::StyledEditWindow(BRect frame, int32 id)
Show(); Show();
} /***StyledEditWindow()***/ } /***StyledEditWindow()***/
StyledEditWindow::StyledEditWindow(BRect frame, entry_ref *ref) StyledEditWindow::StyledEditWindow(BRect frame, entry_ref *ref, uint32 encoding)
: BWindow(frame,"untitled",B_DOCUMENT_WINDOW,0) : BWindow(frame,"untitled",B_DOCUMENT_WINDOW,0)
{ {
InitWindow(); InitWindow(encoding);
OpenFile(ref); OpenFile(ref);
Show(); Show();
} /***StyledEditWindow()***/ } /***StyledEditWindow()***/
@ -57,7 +57,7 @@ StyledEditWindow::~StyledEditWindow()
} /***~StyledEditWindow()***/ } /***~StyledEditWindow()***/
void void
StyledEditWindow::InitWindow() StyledEditWindow::InitWindow(uint32 encoding)
{ {
fPrintSettings= NULL; fPrintSettings= NULL;
fSaveMessage= NULL; fSaveMessage= NULL;
@ -105,7 +105,7 @@ StyledEditWindow::InitWindow()
fTextView= new StyledEditView(viewFrame, textBounds, this); fTextView= new StyledEditView(viewFrame, textBounds, this);
fTextView->SetDoesUndo(true); fTextView->SetDoesUndo(true);
fTextView->SetStylable(true); fTextView->SetStylable(true);
fTextView->SetEncoding(encoding);
fScrollView= new BScrollView("scrollview", fTextView, B_FOLLOW_ALL, 0, true, true, B_PLAIN_BORDER); fScrollView= new BScrollView("scrollview", fTextView, B_FOLLOW_ALL, 0, true, true, B_PLAIN_BORDER);
AddChild(fScrollView); AddChild(fScrollView);
@ -279,7 +279,6 @@ StyledEditWindow::InitWindow()
fSavePanel = 0; // build lazily fSavePanel = 0; // build lazily
fSavePanelEncodingMenu = 0; // build lazily fSavePanelEncodingMenu = 0; // build lazily
fSaveAsEncoding = 0; // default UTF-8
} /***StyledEditWindow::Initwindow()***/ } /***StyledEditWindow::Initwindow()***/
void void
@ -612,7 +611,7 @@ StyledEditWindow::MessageReceived(BMessage *message)
void * ptr; void * ptr;
if (message->FindPointer("source",&ptr) == B_OK) { if (message->FindPointer("source",&ptr) == B_OK) {
if (fSavePanelEncodingMenu != 0) { if (fSavePanelEncodingMenu != 0) {
fSaveAsEncoding = (uint32)fSavePanelEncodingMenu->IndexOf((BMenuItem*)ptr); fTextView->SetEncoding((uint32)fSavePanelEncodingMenu->IndexOf((BMenuItem*)ptr));
} }
} }
break; break;
@ -819,7 +818,7 @@ StyledEditWindow::Save(BMessage *message)
if(err!= B_OK) if(err!= B_OK)
return err; return err;
err= fTextView->WriteStyledEditFile(&file,fSaveAsEncoding); err= fTextView->WriteStyledEditFile(&file);
if(err != B_OK) if(err != B_OK)
return err; return err;
@ -872,13 +871,16 @@ StyledEditWindow::SaveAs()
for (int index = 0; (index < roster->GetCharacterSetCount()) ; index++) { for (int index = 0; (index < roster->GetCharacterSetCount()) ; index++) {
const CharacterSet * cs = roster->GetCharacterSet(index); const CharacterSet * cs = roster->GetCharacterSet(index);
BString name(cs->GetPrintName()); BString name(cs->GetPrintName());
name.Append(" ("); const char * mime = cs->GetMIMEName();
name.Append(cs->GetMIMEName()); if (mime) {
name.Append(")"); name.Append(" (");
name.Append(mime);
name.Append(")");
}
BMenuItem * item = new BMenuItem(name.String(),new BMessage(SAVE_AS_ENCODING)); BMenuItem * item = new BMenuItem(name.String(),new BMessage(SAVE_AS_ENCODING));
item->SetTarget(this); item->SetTarget(this);
fSavePanelEncodingMenu->AddItem(item); fSavePanelEncodingMenu->AddItem(item);
if (index == fSaveAsEncoding) { if (index == fTextView->GetEncoding()) {
item->SetMarked(true); item->SetMarked(true);
} }
} }
@ -887,7 +889,6 @@ StyledEditWindow::SaveAs()
// its own scope allows the lock to be released before Show() // its own scope allows the lock to be released before Show()
{ {
// TODO: add encodings
BAutolock lock(fSavePanel->Window()); BAutolock lock(fSavePanel->Window());
if (lock.IsLocked()) { if (lock.IsLocked()) {
fSavePanelTextView->SetText(Title()); fSavePanelTextView->SetText(Title());

View File

@ -15,8 +15,8 @@ class StyledEditWindow
: public BWindow : public BWindow
{ {
public: public:
StyledEditWindow(BRect frame, int32 id); StyledEditWindow(BRect frame, int32 id, uint32 encoding = 0);
StyledEditWindow(BRect frame, entry_ref *ref); StyledEditWindow(BRect frame, entry_ref *ref, uint32 encoding = 0);
~StyledEditWindow(); ~StyledEditWindow();
virtual void Quit(); virtual void Quit();
@ -32,7 +32,7 @@ public:
void SearchAllWindows(BString find, BString replace, bool casesens); void SearchAllWindows(BString find, BString replace, bool casesens);
private: private:
void InitWindow(); void InitWindow(uint32 encoding = 0);
bool Search(BString searchfor, bool casesens, bool wrap, bool backsearch); bool Search(BString searchfor, bool casesens, bool wrap, bool backsearch);
void FindSelection(); void FindSelection();
bool Replace(BString findthis, BString replacewith, bool casesens, bool wrap, bool backsearch); bool Replace(BString findthis, BString replacewith, bool casesens, bool wrap, bool backsearch);
@ -97,8 +97,6 @@ private:
BFilePanel *fSavePanel; BFilePanel *fSavePanel;
BTextControl *fSavePanelTextView; BTextControl *fSavePanelTextView;
BMenu *fSavePanelEncodingMenu; BMenu *fSavePanelEncodingMenu;
uint32 fSaveAsEncoding;
}; };