2002-09-23 23:01:55 +00:00
|
|
|
#include <Message.h>
|
|
|
|
|
#include <Messenger.h>
|
|
|
|
|
#include <Rect.h>
|
2002-11-22 07:52:00 +00:00
|
|
|
#include <Region.h>
|
2002-11-05 09:24:25 +00:00
|
|
|
#include <TranslationUtils.h>
|
2002-11-22 05:53:58 +00:00
|
|
|
#include <Node.h>
|
2003-07-05 21:29:23 +00:00
|
|
|
#include <stdio.h>
|
2002-09-23 23:01:55 +00:00
|
|
|
|
2002-11-05 09:24:25 +00:00
|
|
|
#include "StyledEditView.h"
|
|
|
|
|
#include "Constants.h"
|
2003-07-05 21:29:23 +00:00
|
|
|
#include "CharacterSet.h"
|
|
|
|
|
#include "UTF8.h"
|
2002-09-23 23:01:55 +00:00
|
|
|
|
2002-11-05 09:24:25 +00:00
|
|
|
StyledEditView::StyledEditView(BRect viewFrame, BRect textBounds, BHandler *handler)
|
|
|
|
|
: BTextView(viewFrame, "textview", textBounds,
|
|
|
|
|
B_FOLLOW_ALL, B_FRAME_EVENTS|B_WILL_DRAW)
|
|
|
|
|
{
|
|
|
|
|
fHandler= handler;
|
|
|
|
|
fMessenger= new BMessenger(handler);
|
|
|
|
|
fSuppressChanges = false;
|
|
|
|
|
}/***StyledEditView()***/
|
2002-09-23 23:01:55 +00:00
|
|
|
|
2002-11-05 09:24:25 +00:00
|
|
|
StyledEditView::~StyledEditView(){
|
2002-09-23 23:01:55 +00:00
|
|
|
|
2002-11-05 09:24:25 +00:00
|
|
|
}/***~StyledEditView***/
|
2002-09-23 23:01:55 +00:00
|
|
|
|
2002-11-05 09:24:25 +00:00
|
|
|
void
|
|
|
|
|
StyledEditView::FrameResized(float width, float height)
|
|
|
|
|
{
|
2002-09-23 23:01:55 +00:00
|
|
|
BTextView::FrameResized(width, height);
|
|
|
|
|
|
2002-11-22 07:52:00 +00:00
|
|
|
if (DoesWordWrap()) {
|
|
|
|
|
BRect textRect;
|
|
|
|
|
textRect = Bounds();
|
|
|
|
|
textRect.OffsetTo(B_ORIGIN);
|
|
|
|
|
textRect.InsetBy(TEXT_INSET,TEXT_INSET);
|
2002-11-22 05:53:58 +00:00
|
|
|
SetTextRect(textRect);
|
2002-09-23 23:01:55 +00:00
|
|
|
}
|
2002-11-22 07:52:00 +00:00
|
|
|
/* // I tried to do some sort of intelligent resize thing but it just doesn't work
|
|
|
|
|
// so we revert to the R5 stylededit yucky practice of setting the text rect to
|
|
|
|
|
// some crazy large number when word wrap is turned off :-(
|
|
|
|
|
else if (textRect.Width() > TextRect().Width()) {
|
|
|
|
|
SetTextRect(textRect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BRegion region;
|
|
|
|
|
GetTextRegion(0,TextLength(),®ion);
|
|
|
|
|
float textWidth = region.Frame().Width();
|
|
|
|
|
if (textWidth < textRect.Width()) {
|
|
|
|
|
BRect textRect(B_ORIGIN,BPoint(textWidth+TEXT_INSET*2,Bounds().Height()));
|
|
|
|
|
textRect.InsetBy(TEXT_INSET,TEXT_INSET);
|
|
|
|
|
SetTextRect(textRect);
|
|
|
|
|
}
|
|
|
|
|
*/
|
2002-09-23 23:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
2002-11-05 09:24:25 +00:00
|
|
|
status_t
|
|
|
|
|
StyledEditView::GetStyledText(BPositionIO * stream)
|
|
|
|
|
{
|
|
|
|
|
status_t result = B_OK;
|
|
|
|
|
fSuppressChanges = true;
|
|
|
|
|
result = BTranslationUtils::GetStyledText(stream, this, NULL);
|
2002-11-22 05:53:58 +00:00
|
|
|
|
|
|
|
|
BNode * node = dynamic_cast<BNode*>(stream);
|
|
|
|
|
if (node != 0) {
|
|
|
|
|
ssize_t bytesRead;
|
|
|
|
|
// restore alignment
|
|
|
|
|
alignment align;
|
|
|
|
|
bytesRead = node->ReadAttr("alignment",0,0,&align,sizeof(align));
|
|
|
|
|
if (bytesRead > 0) {
|
|
|
|
|
SetAlignment(align);
|
|
|
|
|
}
|
|
|
|
|
// restore wrapping
|
|
|
|
|
bool wrap;
|
|
|
|
|
bytesRead = node->ReadAttr("wrap",0,0,&wrap,sizeof(wrap));
|
|
|
|
|
if (bytesRead > 0) {
|
|
|
|
|
SetWordWrap(wrap);
|
|
|
|
|
}
|
2002-11-22 07:52:00 +00:00
|
|
|
if (wrap == false) {
|
|
|
|
|
BRect textRect;
|
|
|
|
|
textRect = Bounds();
|
|
|
|
|
textRect.OffsetTo(B_ORIGIN);
|
|
|
|
|
textRect.InsetBy(TEXT_INSET,TEXT_INSET);
|
|
|
|
|
// the width comes from stylededit R5. TODO: find a better way
|
|
|
|
|
textRect.SetRightBottom(BPoint(1500.0,textRect.RightBottom().y));
|
|
|
|
|
SetTextRect(textRect);
|
|
|
|
|
}
|
2002-11-22 05:53:58 +00:00
|
|
|
}
|
2002-11-05 09:24:25 +00:00
|
|
|
fSuppressChanges = false;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2002-11-22 05:53:58 +00:00
|
|
|
status_t
|
2003-07-05 21:29:23 +00:00
|
|
|
StyledEditView::WriteStyledEditFile(BFile * file, uint32 charSet)
|
2002-11-22 05:53:58 +00:00
|
|
|
{
|
|
|
|
|
status_t result = B_OK;
|
|
|
|
|
result = BTranslationUtils::WriteStyledEditFile(this,file);
|
|
|
|
|
if (result != B_OK) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2003-07-05 21:29:23 +00:00
|
|
|
if (charSet == 0) {
|
|
|
|
|
int32 encoding = 65535;
|
|
|
|
|
file->WriteAttr("be:encoding",B_INT32_TYPE,0,&encoding,sizeof(encoding));
|
|
|
|
|
} else {
|
|
|
|
|
result = file->SetSize(0);
|
|
|
|
|
if (result != B_OK) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
result = file->Seek(0,SEEK_SET);
|
|
|
|
|
if (result != B_OK) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
CharacterSetRoster * roster = CharacterSetRoster::Roster(&result);
|
|
|
|
|
if (result != B_OK) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
uint32 id = roster->GetCharacterSet(charSet)->GetConversionID();
|
|
|
|
|
const char * outText = Text();
|
|
|
|
|
int32 sourceLength = TextLength();
|
|
|
|
|
int32 state = 0;
|
|
|
|
|
char buffer[256];
|
|
|
|
|
while (sourceLength > 0) {
|
|
|
|
|
int32 length = sourceLength;
|
|
|
|
|
int32 written = 256;
|
|
|
|
|
result = convert_from_utf8(id,outText,&length,buffer,&written,&state);
|
|
|
|
|
if (result != B_OK) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
file->Write(buffer,written);
|
|
|
|
|
sourceLength -= length;
|
|
|
|
|
}
|
|
|
|
|
file->WriteAttr("be:encoding",B_INT32_TYPE,0,&id,sizeof(id));
|
|
|
|
|
}
|
|
|
|
|
|
2002-11-22 05:53:58 +00:00
|
|
|
alignment align = Alignment();
|
|
|
|
|
file->WriteAttr("alignment",B_INT32_TYPE,0,&align,sizeof(align));
|
|
|
|
|
bool wrap = DoesWordWrap();
|
|
|
|
|
file->WriteAttr("wrap",B_BOOL_TYPE,0,&wrap,sizeof(wrap));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2002-11-05 09:24:25 +00:00
|
|
|
void
|
|
|
|
|
StyledEditView::Reset()
|
|
|
|
|
{
|
|
|
|
|
fSuppressChanges = true;
|
|
|
|
|
SetText("");
|
|
|
|
|
fSuppressChanges = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
StyledEditView::Select(int32 start, int32 finish)
|
|
|
|
|
{
|
2002-09-23 23:01:55 +00:00
|
|
|
if(start==finish)
|
|
|
|
|
fChangeMessage= new BMessage(DISABLE_ITEMS);
|
|
|
|
|
else
|
|
|
|
|
fChangeMessage= new BMessage(ENABLE_ITEMS);
|
|
|
|
|
|
|
|
|
|
fMessenger->SendMessage(fChangeMessage);
|
|
|
|
|
|
|
|
|
|
BTextView::Select(start, finish);
|
|
|
|
|
}
|
|
|
|
|
|
2002-11-05 09:24:25 +00:00
|
|
|
void StyledEditView::InsertText(const char *text, int32 length, int32 offset, const text_run_array *runs)
|
|
|
|
|
{
|
|
|
|
|
if (!fSuppressChanges)
|
|
|
|
|
fMessenger-> SendMessage(new BMessage(TEXT_CHANGED));
|
2002-09-23 23:01:55 +00:00
|
|
|
|
|
|
|
|
BTextView::InsertText(text, length, offset, runs);
|
|
|
|
|
|
|
|
|
|
}/****StyledEditView::InsertText()***/
|
|
|
|
|
|
2002-11-05 09:24:25 +00:00
|
|
|
void StyledEditView::DeleteText(int32 start, int32 finish)
|
|
|
|
|
{
|
|
|
|
|
if (!fSuppressChanges)
|
|
|
|
|
fMessenger-> SendMessage(new BMessage(TEXT_CHANGED));
|
2002-09-23 23:01:55 +00:00
|
|
|
|
|
|
|
|
BTextView::DeleteText(start, finish);
|
|
|
|
|
|
|
|
|
|
}/***StyledEditView::DeleteText***/
|