2 * Copyright 2002-2007, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
8 * Axel Dörfler, axeld@pinc-software.de
12 #include "Constants.h"
13 #include "StyledEditView.h"
15 #include <CharacterSet.h>
16 #include <CharacterSetRoster.h>
20 #include <Messenger.h>
23 #include <TranslationUtils.h>
30 using namespace BPrivate
;
33 StyledEditView::StyledEditView(BRect viewFrame
, BRect textBounds
,
36 BTextView(viewFrame
, "textview", textBounds
, NULL
,
37 &(fInitialColor
= ui_color(B_DOCUMENT_TEXT_COLOR
)),
38 B_FOLLOW_ALL
, B_FRAME_EVENTS
| B_WILL_DRAW
)
40 SetViewUIColor(B_DOCUMENT_BACKGROUND_COLOR
);
41 SetLowUIColor(ViewUIColor());
43 fMessenger
= new BMessenger(handler
);
44 fSuppressChanges
= false;
48 StyledEditView::~StyledEditView()
56 StyledEditView::FrameResized(float width
, float height
)
58 BTextView::FrameResized(width
, height
);
63 textRect
.OffsetTo(B_ORIGIN
);
64 textRect
.InsetBy(TEXT_INSET
, TEXT_INSET
);
65 SetTextRect(textRect
);
71 StyledEditView::DeleteText(int32 start
, int32 finish
)
73 if (!fSuppressChanges
)
74 fMessenger
-> SendMessage(TEXT_CHANGED
);
76 BTextView::DeleteText(start
, finish
);
82 StyledEditView::InsertText(const char* text
, int32 length
, int32 offset
,
83 const text_run_array
* runs
)
85 if (!fSuppressChanges
)
86 fMessenger
->SendMessage(TEXT_CHANGED
);
88 BTextView::InsertText(text
, length
, offset
, runs
);
94 StyledEditView::Select(int32 start
, int32 finish
)
96 fMessenger
->SendMessage(start
== finish
? DISABLE_ITEMS
: ENABLE_ITEMS
);
97 BTextView::Select(start
, finish
);
103 StyledEditView::Reset()
105 fSuppressChanges
= true;
108 fSuppressChanges
= false;
113 StyledEditView::SetSuppressChanges(bool suppressChanges
)
115 fSuppressChanges
= suppressChanges
;
120 StyledEditView::GetStyledText(BPositionIO
* stream
, const char* forceEncoding
)
122 if (forceEncoding
!= NULL
)
123 fEncoding
= strcmp(forceEncoding
, "auto") != 0 ? forceEncoding
: "";
125 fSuppressChanges
= true;
126 status_t result
= BTranslationUtils::GetStyledText(stream
, this,
128 fSuppressChanges
= false;
133 BNode
* node
= dynamic_cast<BNode
*>(stream
);
135 if (forceEncoding
== NULL
) {
137 if (node
->ReadAttrString("be:encoding", &fEncoding
) != B_OK
) {
138 // try to read as "int32"
140 ssize_t bytesRead
= node
->ReadAttr("be:encoding", B_INT32_TYPE
, 0,
141 &encoding
, sizeof(encoding
));
142 if (bytesRead
== (ssize_t
)sizeof(encoding
)) {
143 if (encoding
== 65535) {
146 const BCharacterSet
* characterSet
147 = BCharacterSetRoster::GetCharacterSetByConversionID(encoding
);
148 if (characterSet
!= NULL
)
149 fEncoding
= characterSet
->GetName();
154 // TODO: move those into BTranslationUtils::GetStyledText() as well?
158 ssize_t bytesRead
= node
->ReadAttr("alignment", 0, 0, &align
, sizeof(align
));
159 if (bytesRead
== (ssize_t
)sizeof(align
))
160 SetAlignment((alignment
)align
);
164 bytesRead
= node
->ReadAttr("wrap", 0, 0, &wrap
, sizeof(wrap
));
165 if (bytesRead
== (ssize_t
)sizeof(wrap
)) {
170 textRect
.OffsetTo(B_ORIGIN
);
171 textRect
.InsetBy(TEXT_INSET
, TEXT_INSET
);
172 // the width comes from stylededit R5. TODO: find a better way
173 textRect
.SetRightBottom(BPoint(1500.0, textRect
.RightBottom().y
));
174 SetTextRect(textRect
);
184 StyledEditView::WriteStyledEditFile(BFile
* file
)
186 return BTranslationUtils::WriteStyledEditFile(this, file
,
192 StyledEditView::SetEncoding(uint32 encoding
)
198 const BCharacterSet
* set
199 = BCharacterSetRoster::GetCharacterSetByFontID(encoding
);
202 fEncoding
= set
->GetName();
207 StyledEditView::GetEncoding() const
212 const BCharacterSet
* set
=
213 BCharacterSetRoster::FindCharacterSetByName(fEncoding
.String());
215 return set
->GetFontID();
222 StyledEditView::_UpdateStatus()
224 int32 selStart
, selFinish
;
225 GetSelection(&selStart
, &selFinish
);
227 int32 line
= CurrentLine();
228 int32 lineStart
= OffsetAt(line
);
231 int32 tabSize
= (int32
)ceilf(TabWidth() / StringWidth("s"));
232 for (int i
= lineStart
; i
< selStart
; i
++) {
233 unsigned char ch
= ByteAt(i
);
234 if ((ch
& 0xC0) != 0x80) {
236 while (column
% tabSize
)
242 BMessage
* message
= new BMessage(UPDATE_STATUS
);
243 message
->AddInt32("line", line
+ 1);
244 message
->AddInt32("column", column
);
245 message
->AddString("encoding", fEncoding
.String());
246 fMessenger
->SendMessage(message
);