From c7087c91837c663ba6794265914f5c408c4f3375 Mon Sep 17 00:00:00 2001 From: Siarzhuk Zharski Date: Wed, 9 Jan 2013 20:23:36 +0100 Subject: [PATCH] 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! --- src/apps/stylededit/Constants.h | 2 +- src/apps/stylededit/StyledEditApp.cpp | 12 +++---- src/apps/stylededit/StyledEditWindow.cpp | 42 +++++------------------- 3 files changed, 16 insertions(+), 40 deletions(-) diff --git a/src/apps/stylededit/Constants.h b/src/apps/stylededit/Constants.h index 03f7387f6e..4c955b6bac 100644 --- a/src/apps/stylededit/Constants.h +++ b/src/apps/stylededit/Constants.h @@ -81,7 +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'; +const uint32 UPDATE_LINE_SELECTION = 'UPls'; #endif // CONSTANTS_H diff --git a/src/apps/stylededit/StyledEditApp.cpp b/src/apps/stylededit/StyledEditApp.cpp index 664ecbff4c..0460af8d18 100644 --- a/src/apps/stylededit/StyledEditApp.cpp +++ b/src/apps/stylededit/StyledEditApp.cpp @@ -266,18 +266,18 @@ StyledEditApp::RefsReceived(BMessage* message) length = -1; } - BMessage* selMessage = NULL; + BMessage* selection = NULL; if (line >= 0 || (start >= 0 && length >= 0)) { - selMessage = new BMessage(UPDATE_LINE_SEL); + selection = new BMessage(UPDATE_LINE_SELECTION); if (line >= 0) - selMessage->AddInt32("be:line", line); + selection->AddInt32("be:line", line); if (start >= 0) { - selMessage->AddInt32("be:selection_offset", start); - selMessage->AddInt32("be:selection_length", max_c(0, length)); + selection->AddInt32("be:selection_offset", start); + selection->AddInt32("be:selection_length", max_c(0, length)); } } - OpenDocument(&ref, selMessage); + OpenDocument(&ref, selection); index++; } } diff --git a/src/apps/stylededit/StyledEditWindow.cpp b/src/apps/stylededit/StyledEditWindow.cpp index 3193bcfc85..9e08d4f5ad 100644 --- a/src/apps/stylededit/StyledEditWindow.cpp +++ b/src/apps/stylededit/StyledEditWindow.cpp @@ -535,7 +535,7 @@ StyledEditWindow::MessageReceived(BMessage* message) break; } - case UPDATE_LINE_SEL: + case UPDATE_LINE_SELECTION: { int32 line; 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 - 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 position = 0; + if (documentNode.ReadAttr("be:caret_position", B_INT32_TYPE, 0, + &position, sizeof(position)) != sizeof(position)) + position = 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); + fTextView->Select(position, position); + fTextView->ScrollToOffset(position); } @@ -1390,17 +1372,11 @@ 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)); - + // preserve caret line and position int32 start, end; fTextView->GetSelection(&start, &end); - int32 length = end - start; - documentNode.WriteAttr("be:selection_offset", + documentNode.WriteAttr("be:caret_position", B_INT32_TYPE, 0, &start, sizeof(start)); - documentNode.WriteAttr("be:selection_length", - B_INT32_TYPE, 0, &length, sizeof(length)); }