StyledEdit now makes use of the new BTranslationUtils functions which
simplify the code quite a lot. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@19845 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2006, Haiku, Inc. All Rights Reserved.
|
* Copyright 2002-2007, Haiku, Inc. All Rights Reserved.
|
||||||
* Distributed under the terms of the MIT License.
|
* Distributed under the terms of the MIT License.
|
||||||
*
|
*
|
||||||
* Authors:
|
* Authors:
|
||||||
@@ -84,7 +84,12 @@ StyledEditApp::StyledEditApp()
|
|||||||
BCharacterSetRoster roster;
|
BCharacterSetRoster roster;
|
||||||
BCharacterSet charset;
|
BCharacterSet charset;
|
||||||
while (roster.GetNextCharacterSet(&charset) == B_NO_ERROR) {
|
while (roster.GetNextCharacterSet(&charset) == B_NO_ERROR) {
|
||||||
BString name(charset.GetPrintName());
|
BString name;
|
||||||
|
if (charset.GetFontID() == B_UNICODE_UTF8)
|
||||||
|
name = "Default";
|
||||||
|
else
|
||||||
|
name = charset.GetPrintName();
|
||||||
|
|
||||||
const char* mime = charset.GetMIMEName();
|
const char* mime = charset.GetMIMEName();
|
||||||
if (mime) {
|
if (mime) {
|
||||||
name.Append(" (");
|
name.Append(" (");
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2006, Haiku, Inc. All Rights Reserved.
|
* Copyright 2002-2007, Haiku, Inc. All Rights Reserved.
|
||||||
* Distributed under the terms of the MIT License.
|
* Distributed under the terms of the MIT License.
|
||||||
*
|
*
|
||||||
* Authors:
|
* Authors:
|
||||||
* Mattias Sundblad
|
* Mattias Sundblad
|
||||||
* Andrew Bachmann
|
* Andrew Bachmann
|
||||||
|
* Axel Dörfler, [email protected]
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@@ -23,17 +24,17 @@
|
|||||||
#include <CharacterSetRoster.h>
|
#include <CharacterSetRoster.h>
|
||||||
#include <UTF8.h>
|
#include <UTF8.h>
|
||||||
|
|
||||||
|
|
||||||
using namespace BPrivate;
|
using namespace BPrivate;
|
||||||
|
|
||||||
|
|
||||||
StyledEditView::StyledEditView(BRect viewFrame, BRect textBounds, BHandler *handler)
|
StyledEditView::StyledEditView(BRect viewFrame, BRect textBounds, BHandler *handler)
|
||||||
: BTextView(viewFrame, "textview", textBounds,
|
: BTextView(viewFrame, "textview", textBounds,
|
||||||
B_FOLLOW_ALL, B_FRAME_EVENTS|B_WILL_DRAW)
|
B_FOLLOW_ALL, B_FRAME_EVENTS | B_WILL_DRAW)
|
||||||
{
|
{
|
||||||
fHandler = handler;
|
fHandler = handler;
|
||||||
fMessenger = new BMessenger(handler);
|
fMessenger = new BMessenger(handler);
|
||||||
fSuppressChanges = false;
|
fSuppressChanges = false;
|
||||||
fEncoding = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -79,7 +80,8 @@ status_t
|
|||||||
StyledEditView::GetStyledText(BPositionIO* stream)
|
StyledEditView::GetStyledText(BPositionIO* stream)
|
||||||
{
|
{
|
||||||
fSuppressChanges = true;
|
fSuppressChanges = true;
|
||||||
status_t result = BTranslationUtils::GetStyledText(stream, this, NULL);
|
status_t result = BTranslationUtils::GetStyledText(stream, this,
|
||||||
|
fEncoding.String());
|
||||||
fSuppressChanges = false;
|
fSuppressChanges = false;
|
||||||
|
|
||||||
if (result != B_OK)
|
if (result != B_OK)
|
||||||
@@ -87,25 +89,29 @@ StyledEditView::GetStyledText(BPositionIO* stream)
|
|||||||
|
|
||||||
BNode* node = dynamic_cast<BNode*>(stream);
|
BNode* node = dynamic_cast<BNode*>(stream);
|
||||||
if (node != NULL) {
|
if (node != NULL) {
|
||||||
ssize_t bytesRead;
|
// get encoding
|
||||||
// decode encoding
|
if (node->ReadAttrString("be:encoding", &fEncoding) != B_OK) {
|
||||||
int32 encoding;
|
// try to read as "int32"
|
||||||
bytesRead = node->ReadAttr("be:encoding", 0, 0, &encoding, sizeof(encoding));
|
int32 encoding;
|
||||||
if (bytesRead == (ssize_t)sizeof(encoding)) {
|
ssize_t bytesRead = node->ReadAttr("be:encoding", B_INT32_TYPE, 0,
|
||||||
if (encoding == 65535) {
|
&encoding, sizeof(encoding));
|
||||||
// UTF-8
|
if (bytesRead == (ssize_t)sizeof(encoding)) {
|
||||||
fEncoding = 0;
|
if (encoding == 65535) {
|
||||||
} else {
|
fEncoding = "UTF-8";
|
||||||
const BCharacterSet* characterSet
|
} else {
|
||||||
= BCharacterSetRoster::GetCharacterSetByConversionID(encoding);
|
const BCharacterSet* characterSet
|
||||||
if (characterSet != 0)
|
= BCharacterSetRoster::GetCharacterSetByConversionID(encoding);
|
||||||
fEncoding = characterSet->GetFontID();
|
if (characterSet != NULL)
|
||||||
|
fEncoding = characterSet->GetName();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: move those into BTranslationUtils::GetStyledText() as well?
|
||||||
|
|
||||||
// restore alignment
|
// restore alignment
|
||||||
int32 align;
|
int32 align;
|
||||||
bytesRead = node->ReadAttr("alignment", 0, 0, &align, sizeof(align));
|
ssize_t bytesRead = node->ReadAttr("alignment", 0, 0, &align, sizeof(align));
|
||||||
if (bytesRead == (ssize_t)sizeof(align))
|
if (bytesRead == (ssize_t)sizeof(align))
|
||||||
SetAlignment((alignment)align);
|
SetAlignment((alignment)align);
|
||||||
|
|
||||||
@@ -126,63 +132,6 @@ StyledEditView::GetStyledText(BPositionIO* stream)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fEncoding != 0) {
|
|
||||||
int32 length = stream->Seek(0, SEEK_END);
|
|
||||||
|
|
||||||
// Here we save the run_array before it gets overwritten...
|
|
||||||
text_run_array* runArray = RunArray(0, length);
|
|
||||||
uint32 id = BCharacterSetRoster::GetCharacterSetByFontID(fEncoding)->GetConversionID();
|
|
||||||
|
|
||||||
fSuppressChanges = true;
|
|
||||||
SetText("");
|
|
||||||
fSuppressChanges = false;
|
|
||||||
|
|
||||||
char inBuffer[32768];
|
|
||||||
off_t location = 0;
|
|
||||||
int32 textOffset = 0;
|
|
||||||
int32 state = 0;
|
|
||||||
int32 bytesRead;
|
|
||||||
while ((bytesRead = stream->ReadAt(location, inBuffer, sizeof(inBuffer))) > 0) {
|
|
||||||
char* inPtr = inBuffer;
|
|
||||||
char textBuffer[32768];
|
|
||||||
int32 textLength = sizeof(textBuffer);
|
|
||||||
int32 bytes = bytesRead;
|
|
||||||
while (textLength > 0 && bytes > 0) {
|
|
||||||
result = convert_to_utf8(id, inPtr, &bytes, textBuffer, &textLength, &state);
|
|
||||||
if (result != B_OK)
|
|
||||||
return result;
|
|
||||||
|
|
||||||
fSuppressChanges = true;
|
|
||||||
InsertText(textBuffer, textLength, textOffset);
|
|
||||||
fSuppressChanges = false;
|
|
||||||
textOffset += textLength;
|
|
||||||
inPtr += bytes;
|
|
||||||
location += bytes;
|
|
||||||
bytesRead -= bytes;
|
|
||||||
bytes = bytesRead;
|
|
||||||
if (textLength > 0)
|
|
||||||
textLength = sizeof(textBuffer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ... and here we restore it
|
|
||||||
SetRunArray(0, length, runArray);
|
|
||||||
|
|
||||||
#ifdef HAIKU_TARGET_PLATFORM_BEOS
|
|
||||||
// FreeRunArray does not exist on R5
|
|
||||||
|
|
||||||
// Call destructors explicitly
|
|
||||||
for (int32 i = 0; i < runArray->count; i++)
|
|
||||||
runArray->runs[i].font.~BFont();
|
|
||||||
|
|
||||||
free(runArray);
|
|
||||||
|
|
||||||
#else
|
|
||||||
FreeRunArray(runArray);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,63 +139,8 @@ StyledEditView::GetStyledText(BPositionIO* stream)
|
|||||||
status_t
|
status_t
|
||||||
StyledEditView::WriteStyledEditFile(BFile* file)
|
StyledEditView::WriteStyledEditFile(BFile* file)
|
||||||
{
|
{
|
||||||
status_t result = B_OK;
|
return BTranslationUtils::WriteStyledEditFile(this, file,
|
||||||
ssize_t bytes = 0;
|
fEncoding.String());
|
||||||
result = BTranslationUtils::WriteStyledEditFile(this, file);
|
|
||||||
if (result != B_OK)
|
|
||||||
return result;
|
|
||||||
|
|
||||||
if (fEncoding == 0) {
|
|
||||||
int32 encoding = 65535;
|
|
||||||
bytes = file->WriteAttr("be:encoding", B_INT32_TYPE, 0, &encoding, sizeof(encoding));
|
|
||||||
if (bytes < 0)
|
|
||||||
return bytes;
|
|
||||||
} else {
|
|
||||||
result = file->SetSize(0);
|
|
||||||
if (result != B_OK)
|
|
||||||
return result;
|
|
||||||
|
|
||||||
bytes = file->Seek(0, SEEK_SET);
|
|
||||||
if (bytes != 0)
|
|
||||||
return bytes;
|
|
||||||
|
|
||||||
const BCharacterSet* cs = BCharacterSetRoster::GetCharacterSetByFontID(fEncoding);
|
|
||||||
if (cs != 0) {
|
|
||||||
uint32 id = cs->GetConversionID();
|
|
||||||
const char * outText = Text();
|
|
||||||
int32 sourceLength = TextLength();
|
|
||||||
int32 state = 0;
|
|
||||||
char buffer[32768];
|
|
||||||
while (sourceLength > 0) {
|
|
||||||
int32 length = sourceLength;
|
|
||||||
int32 written = 32768;
|
|
||||||
result = convert_from_utf8(id,outText,&length,buffer,&written,&state);
|
|
||||||
if (result != B_OK) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
bytes = file->Write(buffer,written);
|
|
||||||
if (bytes < 0)
|
|
||||||
return bytes;
|
|
||||||
sourceLength -= length;
|
|
||||||
outText += length;
|
|
||||||
}
|
|
||||||
bytes = file->WriteAttr("be:encoding", B_INT32_TYPE, 0, &id, sizeof(id));
|
|
||||||
if (bytes < 0)
|
|
||||||
return bytes;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int32 align = Alignment();
|
|
||||||
bytes = file->WriteAttr("alignment", B_INT32_TYPE, 0, &align, sizeof(align));
|
|
||||||
if (bytes < 0)
|
|
||||||
return bytes;
|
|
||||||
|
|
||||||
bool wrap = DoesWordWrap();
|
|
||||||
bytes = file->WriteAttr("wrap", B_BOOL_TYPE, 0, &wrap, sizeof(wrap));
|
|
||||||
if (bytes < 0)
|
|
||||||
return bytes;
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -255,6 +149,7 @@ StyledEditView::Reset()
|
|||||||
{
|
{
|
||||||
fSuppressChanges = true;
|
fSuppressChanges = true;
|
||||||
SetText("");
|
SetText("");
|
||||||
|
fEncoding = "";
|
||||||
fSuppressChanges = false;
|
fSuppressChanges = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -272,14 +167,30 @@ StyledEditView::Select(int32 start, int32 finish)
|
|||||||
void
|
void
|
||||||
StyledEditView::SetEncoding(uint32 encoding)
|
StyledEditView::SetEncoding(uint32 encoding)
|
||||||
{
|
{
|
||||||
fEncoding = encoding;
|
if (encoding == 0) {
|
||||||
|
fEncoding = "";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const BCharacterSet* set = BCharacterSetRoster::GetCharacterSetByFontID(encoding);
|
||||||
|
if (set != NULL)
|
||||||
|
fEncoding = set->GetName();
|
||||||
|
else
|
||||||
|
fEncoding = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
uint32
|
uint32
|
||||||
StyledEditView::GetEncoding() const
|
StyledEditView::GetEncoding() const
|
||||||
{
|
{
|
||||||
return fEncoding;
|
if (fEncoding == "")
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
const BCharacterSet* set = BCharacterSetRoster::FindCharacterSetByName(fEncoding.String());
|
||||||
|
if (set != NULL)
|
||||||
|
return set->GetFontID();
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,20 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2006, Haiku, Inc. All Rights Reserved.
|
* Copyright 2002-2007, Haiku, Inc. All Rights Reserved.
|
||||||
* Distributed under the terms of the MIT License.
|
* Distributed under the terms of the MIT License.
|
||||||
*
|
*
|
||||||
* Authors:
|
* Authors:
|
||||||
* Mattias Sundblad
|
* Mattias Sundblad
|
||||||
* Andrew Bachmann
|
* Andrew Bachmann
|
||||||
|
* Axel Dörfler, [email protected]
|
||||||
*/
|
*/
|
||||||
#ifndef STYLED_EDIT_VIEW_H
|
#ifndef STYLED_EDIT_VIEW_H
|
||||||
#define STYLED_EDIT_VIEW_H
|
#define STYLED_EDIT_VIEW_H
|
||||||
|
|
||||||
|
|
||||||
#include <File.h>
|
#include <File.h>
|
||||||
#include <TextView.h>
|
|
||||||
#include <DataIO.h>
|
#include <DataIO.h>
|
||||||
|
#include <String.h>
|
||||||
|
#include <TextView.h>
|
||||||
|
|
||||||
|
|
||||||
class StyledEditView : public BTextView {
|
class StyledEditView : public BTextView {
|
||||||
@@ -26,7 +28,7 @@ class StyledEditView : public BTextView {
|
|||||||
virtual void Reset();
|
virtual void Reset();
|
||||||
virtual status_t GetStyledText(BPositionIO * stream);
|
virtual status_t GetStyledText(BPositionIO * stream);
|
||||||
virtual status_t WriteStyledEditFile(BFile * file);
|
virtual status_t WriteStyledEditFile(BFile * file);
|
||||||
|
|
||||||
virtual void SetEncoding(uint32 encoding);
|
virtual void SetEncoding(uint32 encoding);
|
||||||
virtual uint32 GetEncoding() const;
|
virtual uint32 GetEncoding() const;
|
||||||
|
|
||||||
@@ -39,7 +41,7 @@ class StyledEditView : public BTextView {
|
|||||||
BMessage *fChangeMessage;
|
BMessage *fChangeMessage;
|
||||||
BMessenger *fMessenger;
|
BMessenger *fMessenger;
|
||||||
bool fSuppressChanges;
|
bool fSuppressChanges;
|
||||||
uint32 fEncoding;
|
BString fEncoding;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // STYLED_EDIT_VIEW_H
|
#endif // STYLED_EDIT_VIEW_H
|
||||||
|
|||||||
Reference in New Issue
Block a user