Skip to content

Commit

Permalink
Handle be:line and °K in RefsReceived, store it in file attributes
Browse files Browse the repository at this point in the history
This work was done during GCI2012

Fixes #4794

Signed-off-by: Siarzhuk Zharski <zharik@gmx.li>
  • Loading branch information
vladfau authored and siarzhuk committed Dec 23, 2012
1 parent ec8f666 commit 46f1daf
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/apps/stylededit/Constants.h
Expand Up @@ -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

39 changes: 34 additions & 5 deletions src/apps/stylededit/StyledEditApp.cpp
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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++;
}
}

Expand Down Expand Up @@ -309,7 +338,7 @@ StyledEditApp::ReadyToRun()
int32
StyledEditApp::NumberOfWindows()
{
return fWindowCount;
return fWindowCount;
}


Expand Down
3 changes: 2 additions & 1 deletion src/apps/stylededit/StyledEditApp.h
Expand Up @@ -36,7 +36,8 @@ class StyledEditApp : public BApplication {

int32 NumberOfWindows();
void OpenDocument();
status_t OpenDocument(entry_ref* ref);
status_t OpenDocument(entry_ref* ref,
BMessage* message = NULL);
void CloseDocument();

private:
Expand Down
58 changes: 57 additions & 1 deletion src/apps/stylededit/StyledEditWindow.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -867,7 +885,6 @@ StyledEditWindow::OpenFile(entry_ref* ref)

fReloadItem->SetEnabled(fSaveMessage != NULL);
fEncodingItem->SetEnabled(fSaveMessage != NULL);
fTextView->Select(0, 0);
}


Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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));
}


Expand Down

0 comments on commit 46f1daf

Please sign in to comment.