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:
parent
62ab1aa585
commit
670567f9d4
@ -59,6 +59,8 @@ const uint32 DISABLE_ITEMS ='DIit';
|
||||
const uint32 CHANGE_WINDOW ='CHwi';
|
||||
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
|
||||
|
@ -1,6 +1,8 @@
|
||||
|
||||
#include <Autolock.h>
|
||||
#include <Path.h>
|
||||
#include <MenuItem.h>
|
||||
#include "CharacterSet.h"
|
||||
#include "Constants.h"
|
||||
#include "StyledEditApp.h"
|
||||
#include "StyledEditWindow.h"
|
||||
@ -16,10 +18,31 @@ StyledEditApp::StyledEditApp()
|
||||
BMenuBar * menuBar =
|
||||
dynamic_cast<BMenuBar*>(fOpenPanel->Window()->FindView("MenuBar"));
|
||||
|
||||
fOpenAsEncoding = 0;
|
||||
fOpenPanelEncodingMenu= new BMenu("Encoding");
|
||||
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;
|
||||
fNext_Untitled_Window= 1;
|
||||
@ -63,6 +86,14 @@ StyledEditApp::MessageReceived(BMessage *message)
|
||||
case B_SILENT_RELAUNCH:
|
||||
OpenDocument();
|
||||
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:
|
||||
BApplication::MessageReceived(message);
|
||||
break;
|
||||
@ -72,7 +103,7 @@ StyledEditApp::MessageReceived(BMessage *message)
|
||||
void
|
||||
StyledEditApp::OpenDocument()
|
||||
{
|
||||
new StyledEditWindow(windowRect,fNext_Untitled_Window++);
|
||||
new StyledEditWindow(windowRect,fNext_Untitled_Window++,fOpenAsEncoding);
|
||||
windowRect.OffsetBy(15,15); // TODO: wrap around screen
|
||||
fWindowCount++;
|
||||
}
|
||||
@ -80,7 +111,7 @@ StyledEditApp::OpenDocument()
|
||||
void
|
||||
StyledEditApp::OpenDocument(entry_ref * ref)
|
||||
{
|
||||
new StyledEditWindow(windowRect,ref);
|
||||
new StyledEditWindow(windowRect,ref,fOpenAsEncoding);
|
||||
windowRect.OffsetBy(15,15); // TODO: wrap around screen
|
||||
fWindowCount++;
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ public:
|
||||
private:
|
||||
BFilePanel *fOpenPanel;
|
||||
BMenu *fOpenPanelEncodingMenu;
|
||||
uint32 fOpenAsEncoding;
|
||||
int32 fWindowCount;
|
||||
int32 fNext_Untitled_Window;
|
||||
|
||||
|
@ -18,6 +18,7 @@ StyledEditView::StyledEditView(BRect viewFrame, BRect textBounds, BHandler *hand
|
||||
fHandler= handler;
|
||||
fMessenger= new BMessenger(handler);
|
||||
fSuppressChanges = false;
|
||||
fEncoding = 0;
|
||||
}/***StyledEditView()***/
|
||||
|
||||
StyledEditView::~StyledEditView(){
|
||||
@ -60,10 +61,28 @@ StyledEditView::GetStyledText(BPositionIO * stream)
|
||||
status_t result = B_OK;
|
||||
fSuppressChanges = true;
|
||||
result = BTranslationUtils::GetStyledText(stream, this, NULL);
|
||||
|
||||
|
||||
BNode * node = dynamic_cast<BNode*>(stream);
|
||||
if (node != 0) {
|
||||
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
|
||||
alignment align;
|
||||
bytesRead = node->ReadAttr("alignment",0,0,&align,sizeof(align));
|
||||
@ -91,14 +110,14 @@ StyledEditView::GetStyledText(BPositionIO * stream)
|
||||
}
|
||||
|
||||
status_t
|
||||
StyledEditView::WriteStyledEditFile(BFile * file, uint32 charSet)
|
||||
StyledEditView::WriteStyledEditFile(BFile * file)
|
||||
{
|
||||
status_t result = B_OK;
|
||||
result = BTranslationUtils::WriteStyledEditFile(this,file);
|
||||
if (result != B_OK) {
|
||||
return result;
|
||||
}
|
||||
if (charSet == 0) {
|
||||
if (fEncoding == 0) {
|
||||
int32 encoding = 65535;
|
||||
file->WriteAttr("be:encoding",B_INT32_TYPE,0,&encoding,sizeof(encoding));
|
||||
} else {
|
||||
@ -114,7 +133,7 @@ StyledEditView::WriteStyledEditFile(BFile * file, uint32 charSet)
|
||||
if (result != B_OK) {
|
||||
return result;
|
||||
}
|
||||
uint32 id = roster->GetCharacterSet(charSet)->GetConversionID();
|
||||
uint32 id = roster->FindCharacterSetByFontID(fEncoding)->GetConversionID();
|
||||
const char * outText = Text();
|
||||
int32 sourceLength = TextLength();
|
||||
int32 state = 0;
|
||||
@ -161,7 +180,20 @@ StyledEditView::Select(int32 start, int32 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)
|
||||
fMessenger-> SendMessage(new BMessage(TEXT_CHANGED));
|
||||
@ -170,7 +202,8 @@ void StyledEditView::InsertText(const char *text, int32 length, int32 offset, co
|
||||
|
||||
}/****StyledEditView::InsertText()***/
|
||||
|
||||
void StyledEditView::DeleteText(int32 start, int32 finish)
|
||||
void
|
||||
StyledEditView::DeleteText(int32 start, int32 finish)
|
||||
{
|
||||
if (!fSuppressChanges)
|
||||
fMessenger-> SendMessage(new BMessage(TEXT_CHANGED));
|
||||
|
@ -15,7 +15,10 @@ public:
|
||||
|
||||
virtual void Reset();
|
||||
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:
|
||||
virtual void InsertText(const char *text, int32 length, int32 offset, const text_run_array *runs);
|
||||
virtual void DeleteText(int32 start, int32 finish);
|
||||
@ -25,6 +28,7 @@ private:
|
||||
BMessage *fChangeMessage;
|
||||
BMessenger *fMessenger;
|
||||
bool fSuppressChanges;
|
||||
uint32 fEncoding;
|
||||
};
|
||||
|
||||
#endif // STYLED_EDIT_VIEW_H
|
||||
|
@ -26,10 +26,10 @@
|
||||
#include "StyledEditView.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)
|
||||
{
|
||||
InitWindow();
|
||||
InitWindow(encoding);
|
||||
BString unTitled;
|
||||
unTitled.SetTo("Untitled ");
|
||||
unTitled << id;
|
||||
@ -38,10 +38,10 @@ StyledEditWindow::StyledEditWindow(BRect frame, int32 id)
|
||||
Show();
|
||||
} /***StyledEditWindow()***/
|
||||
|
||||
StyledEditWindow::StyledEditWindow(BRect frame, entry_ref *ref)
|
||||
StyledEditWindow::StyledEditWindow(BRect frame, entry_ref *ref, uint32 encoding)
|
||||
: BWindow(frame,"untitled",B_DOCUMENT_WINDOW,0)
|
||||
{
|
||||
InitWindow();
|
||||
InitWindow(encoding);
|
||||
OpenFile(ref);
|
||||
Show();
|
||||
} /***StyledEditWindow()***/
|
||||
@ -57,7 +57,7 @@ StyledEditWindow::~StyledEditWindow()
|
||||
} /***~StyledEditWindow()***/
|
||||
|
||||
void
|
||||
StyledEditWindow::InitWindow()
|
||||
StyledEditWindow::InitWindow(uint32 encoding)
|
||||
{
|
||||
fPrintSettings= NULL;
|
||||
fSaveMessage= NULL;
|
||||
@ -105,7 +105,7 @@ StyledEditWindow::InitWindow()
|
||||
fTextView= new StyledEditView(viewFrame, textBounds, this);
|
||||
fTextView->SetDoesUndo(true);
|
||||
fTextView->SetStylable(true);
|
||||
|
||||
fTextView->SetEncoding(encoding);
|
||||
|
||||
fScrollView= new BScrollView("scrollview", fTextView, B_FOLLOW_ALL, 0, true, true, B_PLAIN_BORDER);
|
||||
AddChild(fScrollView);
|
||||
@ -279,7 +279,6 @@ StyledEditWindow::InitWindow()
|
||||
|
||||
fSavePanel = 0; // build lazily
|
||||
fSavePanelEncodingMenu = 0; // build lazily
|
||||
fSaveAsEncoding = 0; // default UTF-8
|
||||
} /***StyledEditWindow::Initwindow()***/
|
||||
|
||||
void
|
||||
@ -612,7 +611,7 @@ StyledEditWindow::MessageReceived(BMessage *message)
|
||||
void * ptr;
|
||||
if (message->FindPointer("source",&ptr) == B_OK) {
|
||||
if (fSavePanelEncodingMenu != 0) {
|
||||
fSaveAsEncoding = (uint32)fSavePanelEncodingMenu->IndexOf((BMenuItem*)ptr);
|
||||
fTextView->SetEncoding((uint32)fSavePanelEncodingMenu->IndexOf((BMenuItem*)ptr));
|
||||
}
|
||||
}
|
||||
break;
|
||||
@ -819,7 +818,7 @@ StyledEditWindow::Save(BMessage *message)
|
||||
if(err!= B_OK)
|
||||
return err;
|
||||
|
||||
err= fTextView->WriteStyledEditFile(&file,fSaveAsEncoding);
|
||||
err= fTextView->WriteStyledEditFile(&file);
|
||||
if(err != B_OK)
|
||||
return err;
|
||||
|
||||
@ -872,13 +871,16 @@ StyledEditWindow::SaveAs()
|
||||
for (int index = 0; (index < roster->GetCharacterSetCount()) ; index++) {
|
||||
const CharacterSet * cs = roster->GetCharacterSet(index);
|
||||
BString name(cs->GetPrintName());
|
||||
name.Append(" (");
|
||||
name.Append(cs->GetMIMEName());
|
||||
name.Append(")");
|
||||
const char * mime = cs->GetMIMEName();
|
||||
if (mime) {
|
||||
name.Append(" (");
|
||||
name.Append(mime);
|
||||
name.Append(")");
|
||||
}
|
||||
BMenuItem * item = new BMenuItem(name.String(),new BMessage(SAVE_AS_ENCODING));
|
||||
item->SetTarget(this);
|
||||
fSavePanelEncodingMenu->AddItem(item);
|
||||
if (index == fSaveAsEncoding) {
|
||||
if (index == fTextView->GetEncoding()) {
|
||||
item->SetMarked(true);
|
||||
}
|
||||
}
|
||||
@ -887,7 +889,6 @@ StyledEditWindow::SaveAs()
|
||||
|
||||
// its own scope allows the lock to be released before Show()
|
||||
{
|
||||
// TODO: add encodings
|
||||
BAutolock lock(fSavePanel->Window());
|
||||
if (lock.IsLocked()) {
|
||||
fSavePanelTextView->SetText(Title());
|
||||
|
@ -15,8 +15,8 @@ class StyledEditWindow
|
||||
: public BWindow
|
||||
{
|
||||
public:
|
||||
StyledEditWindow(BRect frame, int32 id);
|
||||
StyledEditWindow(BRect frame, entry_ref *ref);
|
||||
StyledEditWindow(BRect frame, int32 id, uint32 encoding = 0);
|
||||
StyledEditWindow(BRect frame, entry_ref *ref, uint32 encoding = 0);
|
||||
~StyledEditWindow();
|
||||
|
||||
virtual void Quit();
|
||||
@ -32,7 +32,7 @@ public:
|
||||
void SearchAllWindows(BString find, BString replace, bool casesens);
|
||||
|
||||
private:
|
||||
void InitWindow();
|
||||
void InitWindow(uint32 encoding = 0);
|
||||
bool Search(BString searchfor, bool casesens, bool wrap, bool backsearch);
|
||||
void FindSelection();
|
||||
bool Replace(BString findthis, BString replacewith, bool casesens, bool wrap, bool backsearch);
|
||||
@ -97,8 +97,6 @@ private:
|
||||
BFilePanel *fSavePanel;
|
||||
BTextControl *fSavePanelTextView;
|
||||
BMenu *fSavePanelEncodingMenu;
|
||||
|
||||
uint32 fSaveAsEncoding;
|
||||
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user