improved soft wrapping, now saves alignment and wrapping states

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@2061 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
shatty
2002-11-22 05:53:58 +00:00
parent fea5713c6d
commit 0b46ec9848
4 changed files with 171 additions and 79 deletions
+35 -1
View File
@@ -2,6 +2,7 @@
#include <Messenger.h> #include <Messenger.h>
#include <Rect.h> #include <Rect.h>
#include <TranslationUtils.h> #include <TranslationUtils.h>
#include <Node.h>
#include "StyledEditView.h" #include "StyledEditView.h"
#include "Constants.h" #include "Constants.h"
@@ -24,11 +25,11 @@ StyledEditView::FrameResized(float width, float height)
{ {
BTextView::FrameResized(width, height); BTextView::FrameResized(width, height);
if(DoesWordWrap()){
BRect textRect; BRect textRect;
textRect = Bounds(); textRect = Bounds();
textRect.OffsetTo(B_ORIGIN); textRect.OffsetTo(B_ORIGIN);
textRect.InsetBy(TEXT_INSET,TEXT_INSET); textRect.InsetBy(TEXT_INSET,TEXT_INSET);
if (DoesWordWrap() || (textRect.Width() > TextRect().Width())) {
SetTextRect(textRect); SetTextRect(textRect);
} }
} }
@@ -39,10 +40,43 @@ StyledEditView::GetStyledText(BPositionIO * stream)
status_t result = B_OK; status_t result = B_OK;
fSuppressChanges = true; fSuppressChanges = true;
result = BTranslationUtils::GetStyledText(stream, this, NULL); result = BTranslationUtils::GetStyledText(stream, this, NULL);
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);
}
}
fSuppressChanges = false; fSuppressChanges = false;
return result; return result;
} }
status_t
StyledEditView::WriteStyledEditFile(BFile * file)
{
status_t result = B_OK;
result = BTranslationUtils::WriteStyledEditFile(this,file);
if (result != B_OK) {
return result;
}
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;
}
void void
StyledEditView::Reset() StyledEditView::Reset()
{ {
+2
View File
@@ -1,6 +1,7 @@
#ifndef STYLED_EDIT_VIEW_H #ifndef STYLED_EDIT_VIEW_H
#define STYLED_EDIT_VIEW_H #define STYLED_EDIT_VIEW_H
#include <File.h>
#include <TextView.h> #include <TextView.h>
#include <DataIO.h> #include <DataIO.h>
@@ -14,6 +15,7 @@ public:
virtual void Reset(); virtual void Reset();
virtual status_t GetStyledText(BPositionIO * stream); virtual status_t GetStyledText(BPositionIO * stream);
virtual status_t WriteStyledEditFile(BFile * file);
protected: protected:
virtual void InsertText(const char *text, int32 length, int32 offset, const text_run_array *runs); virtual void InsertText(const char *text, int32 length, int32 offset, const text_run_array *runs);
virtual void DeleteText(int32 start, int32 finish); virtual void DeleteText(int32 start, int32 finish);
+66 -13
View File
@@ -264,11 +264,12 @@ StyledEditWindow::InitWindow()
//"Align"-subMenu: //"Align"-subMenu:
subMenu= new BMenu("Align"); subMenu= new BMenu("Align");
subMenu->SetRadioMode(true); subMenu->SetRadioMode(true);
subMenu->AddItem(menuItem= new BMenuItem("Left", new BMessage(ALIGN_LEFT)));
subMenu->AddItem(fAlignLeft = new BMenuItem("Left", new BMessage(ALIGN_LEFT)));
menuItem->SetMarked(true); menuItem->SetMarked(true);
subMenu->AddItem(menuItem= new BMenuItem("Center", new BMessage(ALIGN_CENTER))); subMenu->AddItem(fAlignCenter = new BMenuItem("Center", new BMessage(ALIGN_CENTER)));
subMenu->AddItem(menuItem= new BMenuItem("Right", new BMessage(ALIGN_RIGHT))); subMenu->AddItem(fAlignRight = new BMenuItem("Right", new BMessage(ALIGN_RIGHT)));
menu->AddItem(subMenu); menu->AddItem(subMenu);
menu->AddItem(fWrapItem = new BMenuItem("Wrap Lines", new BMessage(WRAP_LINES))); menu->AddItem(fWrapItem = new BMenuItem("Wrap Lines", new BMessage(WRAP_LINES)));
fWrapItem->SetMarked(true); fWrapItem->SetMarked(true);
@@ -444,39 +445,52 @@ StyledEditWindow::MessageReceived(BMessage *message)
/*********"Document"-menu*************/ /*********"Document"-menu*************/
case ALIGN_LEFT: case ALIGN_LEFT:
fTextView->SetAlignment(B_ALIGN_LEFT); fTextView->SetAlignment(B_ALIGN_LEFT);
fClean = false;
fRevertItem->SetEnabled(fSaveMessage != NULL);
fSaveItem->SetEnabled(true);
break; break;
case ALIGN_CENTER: case ALIGN_CENTER:
fTextView->SetAlignment(B_ALIGN_CENTER); fTextView->SetAlignment(B_ALIGN_CENTER);
fClean = false;
fRevertItem->SetEnabled(fSaveMessage != NULL);
fSaveItem->SetEnabled(true);
break; break;
case ALIGN_RIGHT: case ALIGN_RIGHT:
fTextView->SetAlignment(B_ALIGN_RIGHT); fTextView->SetAlignment(B_ALIGN_RIGHT);
fClean = false;
fRevertItem->SetEnabled(fSaveMessage != NULL);
fSaveItem->SetEnabled(true);
break; break;
case WRAP_LINES: case WRAP_LINES:
if (fTextView->DoesWordWrap()) { if (fTextView->DoesWordWrap()) {
fTextView->SetWordWrap(false); fTextView->SetWordWrap(false);
fWrapItem->SetMarked(false); fWrapItem->SetMarked(false);
} } else {
else{
fTextView->SetWordWrap(true); fTextView->SetWordWrap(true);
fWrapItem->SetMarked(true); fWrapItem->SetMarked(true);
BRect textRect;
textRect = fTextView->Bounds();
textRect.OffsetTo(B_ORIGIN);
textRect.InsetBy(TEXT_INSET,TEXT_INSET);
fTextView->SetTextRect(textRect);
} }
fClean = false;
fRevertItem->SetEnabled(fSaveMessage != NULL);
fSaveItem->SetEnabled(true);
break; break;
case ENABLE_ITEMS: case ENABLE_ITEMS: {
{
fCutItem->SetEnabled(true); fCutItem->SetEnabled(true);
fCopyItem->SetEnabled(true); fCopyItem->SetEnabled(true);
fClearItem->SetEnabled(true); fClearItem->SetEnabled(true);
} }
break; break;
case DISABLE_ITEMS: case DISABLE_ITEMS: {
{
fCutItem->SetEnabled(false); fCutItem->SetEnabled(false);
fCopyItem->SetEnabled(false); fCopyItem->SetEnabled(false);
fClearItem->SetEnabled(false); fClearItem->SetEnabled(false);
} }
break; break;
case TEXT_CHANGED: case TEXT_CHANGED: {
{
if (fUndoFlag) { if (fUndoFlag) {
if (fUndoCleans) { if (fUndoCleans) {
// we cleaned! // we cleaned!
@@ -631,7 +645,7 @@ StyledEditWindow::Save(BMessage *message)
if(err!= B_OK) if(err!= B_OK)
return err; return err;
err= BTranslationUtils::WriteStyledEditFile(fTextView, &file); err= fTextView->WriteStyledEditFile(&file);
if(err != B_OK) if(err != B_OK)
return err; return err;
@@ -700,9 +714,29 @@ StyledEditWindow::OpenFile(entry_ref *ref)
if (fileinit == B_OK){ if (fileinit == B_OK){
status_t result; status_t result;
result = fTextView->GetStyledText(&file); //he he he :) result = fTextView->GetStyledText(&file); //he he he :)
if (result == B_OK) {
if (result != B_OK) {
// TODO: notify user? // TODO: notify user?
} }
// update alignment
switch (fTextView->Alignment()) {
case B_ALIGN_LEFT:
fAlignLeft->SetMarked(true);
break;
case B_ALIGN_CENTER:
fAlignCenter->SetMarked(true);
break;
case B_ALIGN_RIGHT:
fAlignRight->SetMarked(true);
break;
default:
// weird
break;
}
// update word wrapping
fWrapItem->SetMarked(fTextView->DoesWordWrap());
} else { } else {
fSaveItem->SetEnabled(true); // allow saving new files fSaveItem->SetEnabled(true); // allow saving new files
} }
@@ -754,6 +788,25 @@ StyledEditWindow::RevertToSaved()
fTextView->Reset(); //clear the textview... fTextView->Reset(); //clear the textview...
fTextView->GetStyledText(&file); //and fill it from the file fTextView->GetStyledText(&file); //and fill it from the file
// update alignment
switch (fTextView->Alignment()) {
case B_ALIGN_LEFT:
fAlignLeft->SetMarked(true);
break;
case B_ALIGN_CENTER:
fAlignCenter->SetMarked(true);
break;
case B_ALIGN_RIGHT:
fAlignRight->SetMarked(true);
break;
default:
// weird
break;
}
// update word wrapping
fWrapItem->SetMarked(fTextView->DoesWordWrap());
// clear undo modes // clear undo modes
fUndoItem->SetLabel("Can't Undo"); fUndoItem->SetLabel("Can't Undo");
fUndoItem->SetEnabled(false); fUndoItem->SetEnabled(false);
+3
View File
@@ -53,6 +53,9 @@ private:
BMenuItem *fCopyItem; BMenuItem *fCopyItem;
BMenuItem *fClearItem; BMenuItem *fClearItem;
BMenuItem *fWrapItem; BMenuItem *fWrapItem;
BMenuItem *fAlignLeft;
BMenuItem *fAlignCenter;
BMenuItem *fAlignRight;
BString fStringToFind; BString fStringToFind;
BString fReplaceString; BString fReplaceString;