Implement "be:caret_position" document file attribute.

* Use "be:caret_position"document file attribute instead of
  "be:line" and "be:selection_{length|offset}" file attributes;
* Fixed code style violations. Thanks to Axel for pointing it out!
This commit is contained in:
Siarzhuk Zharski
2013-01-09 21:51:33 +01:00
parent 2b67e9a3a7
commit c7087c9183
3 changed files with 16 additions and 40 deletions
+1 -1
View File
@@ -81,7 +81,7 @@ const uint32 SAVE_THEN_QUIT = 'FPsq';
// Update StatusView // Update StatusView
const uint32 UPDATE_STATUS = 'UPSt'; const uint32 UPDATE_STATUS = 'UPSt';
const uint32 UNLOCK_FILE = 'UNLk'; const uint32 UNLOCK_FILE = 'UNLk';
const uint32 UPDATE_LINE_SEL = 'UPls'; const uint32 UPDATE_LINE_SELECTION = 'UPls';
#endif // CONSTANTS_H #endif // CONSTANTS_H
+6 -6
View File
@@ -266,18 +266,18 @@ StyledEditApp::RefsReceived(BMessage* message)
length = -1; length = -1;
} }
BMessage* selMessage = NULL; BMessage* selection = NULL;
if (line >= 0 || (start >= 0 && length >= 0)) { if (line >= 0 || (start >= 0 && length >= 0)) {
selMessage = new BMessage(UPDATE_LINE_SEL); selection = new BMessage(UPDATE_LINE_SELECTION);
if (line >= 0) if (line >= 0)
selMessage->AddInt32("be:line", line); selection->AddInt32("be:line", line);
if (start >= 0) { if (start >= 0) {
selMessage->AddInt32("be:selection_offset", start); selection->AddInt32("be:selection_offset", start);
selMessage->AddInt32("be:selection_length", max_c(0, length)); selection->AddInt32("be:selection_length", max_c(0, length));
} }
} }
OpenDocument(&ref, selMessage); OpenDocument(&ref, selection);
index++; index++;
} }
} }
+9 -33
View File
@@ -535,7 +535,7 @@ StyledEditWindow::MessageReceived(BMessage* message)
break; break;
} }
case UPDATE_LINE_SEL: case UPDATE_LINE_SELECTION:
{ {
int32 line; int32 line;
if (message->FindInt32("be:line", &line) == B_OK) { if (message->FindInt32("be:line", &line) == B_OK) {
@@ -1337,31 +1337,13 @@ StyledEditWindow::_LoadAttrs()
} }
// info about position of caret may live in the file attributes // info about position of caret may live in the file attributes
int32 line = 0; int32 position = 0;
int32 lineMax = fTextView->CountLines(); if (documentNode.ReadAttr("be:caret_position", B_INT32_TYPE, 0,
if (documentNode.ReadAttr("be:line", &position, sizeof(position)) != sizeof(position))
B_INT32_TYPE, 0, &line, sizeof(line)) == sizeof(line)) position = 0;
line = min_c(max_c(0, line), lineMax);
else
line = 0;
int32 start = 0, length = 0, finish = 0; fTextView->Select(position, position);
int32 offsetMax = fTextView->OffsetAt(lineMax); fTextView->ScrollToOffset(position);
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);
} }
@@ -1390,17 +1372,11 @@ StyledEditWindow::_SaveAttrs()
documentNode.WriteAttr(kInfoAttributeName, B_RECT_TYPE, 0, &frame, documentNode.WriteAttr(kInfoAttributeName, B_RECT_TYPE, 0, &frame,
sizeof(BRect)); sizeof(BRect));
// preserve current line and selection too // preserve caret line and position
int32 line = fTextView->CurrentLine();
documentNode.WriteAttr("be:line", B_INT32_TYPE, 0, &line, sizeof(line));
int32 start, end; int32 start, end;
fTextView->GetSelection(&start, &end); fTextView->GetSelection(&start, &end);
int32 length = end - start; documentNode.WriteAttr("be:caret_position",
documentNode.WriteAttr("be:selection_offset",
B_INT32_TYPE, 0, &start, sizeof(start)); B_INT32_TYPE, 0, &start, sizeof(start));
documentNode.WriteAttr("be:selection_length",
B_INT32_TYPE, 0, &length, sizeof(length));
} }