StyledEditView.cpp revision 8f63353b
1#include <Message.h>
2#include <Messenger.h>
3#include <Rect.h>
4#include <Region.h>
5#include <TranslationUtils.h>
6#include <Node.h>
7
8#include "StyledEditView.h"
9#include "Constants.h"
10
11StyledEditView::StyledEditView(BRect viewFrame, BRect textBounds, BHandler *handler)
12	: BTextView(viewFrame, "textview", textBounds,
13			    B_FOLLOW_ALL, B_FRAME_EVENTS|B_WILL_DRAW)
14{
15	fHandler= handler;
16	fMessenger= new BMessenger(handler);
17	fSuppressChanges = false;
18}/***StyledEditView()***/
19
20StyledEditView::~StyledEditView(){
21
22}/***~StyledEditView***/
23
24void
25StyledEditView::FrameResized(float width, float height)
26{
27	BTextView::FrameResized(width, height);
28
29	if (DoesWordWrap()) {
30		BRect textRect;
31		textRect = Bounds();
32		textRect.OffsetTo(B_ORIGIN);
33		textRect.InsetBy(TEXT_INSET,TEXT_INSET);
34		SetTextRect(textRect);
35	}
36/*	// I tried to do some sort of intelligent resize thing but it just doesn't work
37	// so we revert to the R5 stylededit yucky practice of setting the text rect to
38	// some crazy large number when word wrap is turned off :-(
39	 else if (textRect.Width() > TextRect().Width()) {
40		SetTextRect(textRect);
41	}
42
43	BRegion region;
44	GetTextRegion(0,TextLength(),&region);
45	float textWidth = region.Frame().Width();
46	if (textWidth < textRect.Width()) {
47		BRect textRect(B_ORIGIN,BPoint(textWidth+TEXT_INSET*2,Bounds().Height()));
48		textRect.InsetBy(TEXT_INSET,TEXT_INSET);
49		SetTextRect(textRect);
50	}
51	*/
52}
53
54status_t
55StyledEditView::GetStyledText(BPositionIO * stream)
56{
57	status_t result = B_OK;
58	fSuppressChanges = true;
59	result = BTranslationUtils::GetStyledText(stream, this, NULL);
60
61	BNode * node = dynamic_cast<BNode*>(stream);
62	if (node != 0) {
63		ssize_t bytesRead;
64		// restore alignment
65		alignment align;
66		bytesRead = node->ReadAttr("alignment",0,0,&align,sizeof(align));
67		if (bytesRead > 0) {
68			SetAlignment(align);
69		}
70		// restore wrapping
71		bool wrap;
72		bytesRead = node->ReadAttr("wrap",0,0,&wrap,sizeof(wrap));
73		if (bytesRead > 0) {
74			SetWordWrap(wrap);
75		}
76		if (wrap == false) {
77			BRect textRect;
78			textRect = Bounds();
79			textRect.OffsetTo(B_ORIGIN);
80			textRect.InsetBy(TEXT_INSET,TEXT_INSET);
81				// the width comes from stylededit R5. TODO: find a better way
82			textRect.SetRightBottom(BPoint(1500.0,textRect.RightBottom().y));
83			SetTextRect(textRect);
84		}
85	}
86	fSuppressChanges = false;
87	return result;
88}
89
90status_t
91StyledEditView::WriteStyledEditFile(BFile * file)
92{
93	status_t result = B_OK;
94	result = BTranslationUtils::WriteStyledEditFile(this,file);
95	if (result != B_OK) {
96		return result;
97	}
98	alignment align = Alignment();
99	file->WriteAttr("alignment",B_INT32_TYPE,0,&align,sizeof(align));
100	bool wrap = DoesWordWrap();
101	file->WriteAttr("wrap",B_BOOL_TYPE,0,&wrap,sizeof(wrap));
102	return result;
103}
104
105void
106StyledEditView::Reset()
107{
108	fSuppressChanges = true;
109	SetText("");
110	fSuppressChanges = false;
111}
112
113void
114StyledEditView::Select(int32 start, int32 finish)
115{
116	if(start==finish)
117		fChangeMessage= new BMessage(DISABLE_ITEMS);
118	else
119		fChangeMessage= new BMessage(ENABLE_ITEMS);
120
121	fMessenger->SendMessage(fChangeMessage);
122
123	BTextView::Select(start, finish);
124}
125
126void StyledEditView::InsertText(const char *text, int32 length, int32 offset, const text_run_array *runs)
127{
128	if (!fSuppressChanges)
129		fMessenger-> SendMessage(new BMessage(TEXT_CHANGED));
130
131	BTextView::InsertText(text, length, offset, runs);
132
133}/****StyledEditView::InsertText()***/
134
135void StyledEditView::DeleteText(int32 start, int32 finish)
136{
137	if (!fSuppressChanges)
138		fMessenger-> SendMessage(new BMessage(TEXT_CHANGED));
139
140	BTextView::DeleteText(start, finish);
141
142}/***StyledEditView::DeleteText***/
143