Handle be:line and °K in RefsReceived, store it in file attributes

This work was done during GCI2012

Fixes #4794

Signed-off-by: Siarzhuk Zharski <zharik@gmx.li>
This commit is contained in:
Vlad Slepukhin 2012-12-23 21:41:11 +01:00 committed by Siarzhuk Zharski
parent ec8f666514
commit 46f1daff68
4 changed files with 94 additions and 7 deletions

View File

@ -81,6 +81,7 @@ const uint32 SAVE_THEN_QUIT = 'FPsq';
// Update StatusView
const uint32 UPDATE_STATUS = 'UPSt';
const uint32 UNLOCK_FILE = 'UNLk';
const uint32 UPDATE_LINE_SEL = 'UPls';
#endif // CONSTANTS_H

View File

@ -178,7 +178,7 @@ StyledEditApp::OpenDocument()
status_t
StyledEditApp::OpenDocument(entry_ref* ref)
StyledEditApp::OpenDocument(entry_ref* ref, BMessage* message)
{
// traverse eventual symlink
BEntry entry(ref, true);
@ -217,13 +217,19 @@ StyledEditApp::OpenDocument(entry_ref* ref)
if (document->Lock()) {
document->Activate();
document->Unlock();
if (message != NULL)
document->PostMessage(message);
return B_OK;
}
}
}
cascade();
new StyledEditWindow(gWindowRect, ref, fOpenAsEncoding);
document = new StyledEditWindow(gWindowRect, ref, fOpenAsEncoding);
if (message != NULL)
document->PostMessage(message);
fWindowCount++;
return B_OK;
@ -248,8 +254,31 @@ StyledEditApp::RefsReceived(BMessage* message)
int32 index = 0;
entry_ref ref;
while (message->FindRef("refs", index++, &ref) == B_OK) {
OpenDocument(&ref);
while (message->FindRef("refs", index, &ref) == B_OK) {
int32 line;
if (message->FindInt32("be:line", index, &line) != B_OK)
line = -1;
int32 start, length;
if (message->FindInt32("be:selection_length", index, &length) != B_OK
|| message->FindInt32("be:selection_offset", index, &start) != B_OK)
{
start = -1;
length = -1;
}
BMessage* selMessage = NULL;
if (line >= 0 || (start >= 0 && length >= 0)) {
selMessage = new BMessage(UPDATE_LINE_SEL);
if (line >= 0)
selMessage->AddInt32("be:line", line);
if (start >= 0) {
selMessage->AddInt32("be:selection_offset", start);
selMessage->AddInt32("be:selection_length", max_c(0, length));
}
}
OpenDocument(&ref, selMessage);
index++;
}
}
@ -309,7 +338,7 @@ StyledEditApp::ReadyToRun()
int32
StyledEditApp::NumberOfWindows()
{
return fWindowCount;
return fWindowCount;
}

View File

@ -36,7 +36,8 @@ public:
int32 NumberOfWindows();
void OpenDocument();
status_t OpenDocument(entry_ref* ref);
status_t OpenDocument(entry_ref* ref,
BMessage* message = NULL);
void CloseDocument();
private:

View File

@ -535,6 +535,24 @@ StyledEditWindow::MessageReceived(BMessage* message)
break;
}
case UPDATE_LINE_SEL:
{
int32 line;
if (message->FindInt32("be:line", &line) == B_OK) {
fTextView->GoToLine(line);
fTextView->ScrollToSelection();
}
int32 start, length;
if (message->FindInt32("be:selection_offset", &start) == B_OK) {
if (message->FindInt32("be:selection_length", &length) != B_OK)
length = 0;
fTextView->Select(start, start + length);
fTextView->ScrollToOffset(start);
}
break;
}
default:
BWindow::MessageReceived(message);
break;
@ -867,7 +885,6 @@ StyledEditWindow::OpenFile(entry_ref* ref)
fReloadItem->SetEnabled(fSaveMessage != NULL);
fEncodingItem->SetEnabled(fSaveMessage != NULL);
fTextView->Select(0, 0);
}
@ -1318,6 +1335,33 @@ StyledEditWindow::_LoadAttrs()
MoveTo(newFrame.left, newFrame.top);
ResizeTo(newFrame.Width(), newFrame.Height());
}
// info about position of caret may live in the file attributes
int32 line = 0;
int32 lineMax = fTextView->CountLines();
if (documentNode.ReadAttr("be:line",
B_INT32_TYPE, 0, &line, sizeof(line)) == sizeof(line))
line = min_c(max_c(0, line), lineMax);
else
line = 0;
int32 start = 0, length = 0, finish = 0;
int32 offsetMax = fTextView->OffsetAt(lineMax);
if (documentNode.ReadAttr("be:selection_offset",
B_INT32_TYPE, 0, &start, sizeof(start)) == sizeof(start)
&& documentNode.ReadAttr("be:selection_length",
B_INT32_TYPE, 0, &length, sizeof(length)) == sizeof(length))
{
finish = start + length;
start = min_c(max_c(0, start), offsetMax);
finish = min_c(max_c(0, finish), offsetMax);
} else {
start = fTextView->OffsetAt(line);
finish = start;
}
fTextView->Select(start, finish);
fTextView->ScrollToOffset(start);
}
@ -1345,6 +1389,18 @@ StyledEditWindow::_SaveAttrs()
documentNode.WriteAttr(kInfoAttributeName, B_RECT_TYPE, 0, &frame,
sizeof(BRect));
// preserve current line and selection too
int32 line = fTextView->CurrentLine();
documentNode.WriteAttr("be:line", B_INT32_TYPE, 0, &line, sizeof(line));
int32 start, end;
fTextView->GetSelection(&start, &end);
int32 length = end - start;
documentNode.WriteAttr("be:selection_offset",
B_INT32_TYPE, 0, &start, sizeof(start));
documentNode.WriteAttr("be:selection_length",
B_INT32_TYPE, 0, &length, sizeof(length));
}