Hi,

The following patches changes a way of selected text highlighting (NSTextField,
NSTexView, etc.). Now selected text highlights with 
[NSColor selectedTextBackground] instead of NSHighlightRect().

However it's not finished. Here the things that works:
  - double click selects all text;
  - selecting text by mouse and keyboard(Shift-RightArrow) only from the 
    beginning of the text line (text field);
  - clicking on the text object(e.g NSTextView) unhighlights text;
  - pressing motion keys unhighlits text;
 
Here the things have to do:
  - selecting text inside line;
  - selecting text from the end of line (NSSelectionAffinityUpstream?);

Does anybody has suggestions about TODOs? I don't understand why
selection inside text(not from beginning) doesn't highlights text?

-- 
Serg Stoyan
--- GSSimpleLayoutManager.m.orig        Tue Jun 25 09:15:37 2002
+++ GSSimpleLayoutManager.m     Tue Jul  9 14:59:44 2002
@@ -511,8 +511,6 @@
   unsigned end = [self lineLayoutIndexForGlyphIndex: NSMaxRange(glyphRange)];
   NSRange lineRange = NSMakeRange(start, end + 1 - start);
 
-  [self drawLinesInLineRange: lineRange];
-
   // We have to redraw the part of the selection that is inside
   // the redrawn lines
   newRange = NSIntersectionRange(selectedRange, glyphRange);
@@ -522,6 +521,8 @@
     {
       [self drawSelectionAsRangeNoCaret: newRange];
     }
+
+  [self drawLinesInLineRange: lineRange];
 }
 
 - (void) setLineFragmentRect: (NSRect)fragmentRect
@@ -726,20 +727,11 @@
 
 - (void) drawSelectionAsRangeNoCaret: (NSRange)aRange
 {
-  unsigned i, count;
-  NSTextContainer *aTextContainer;
-  NSRect *rects;
-
-  aTextContainer = [self textContainerForGlyphAtIndex: aRange.location
-                        effectiveRange: NULL];
-  rects = [self rectArrayForCharacterRange: aRange 
-               withinSelectedCharacterRange: aRange
-               inTextContainer: aTextContainer
-               rectCount: &count];
-
-  for (i = 0; i < count; i++)
+  if (aRange.length)
     {
-      NSHighlightRect (rects[i]);
+         [_textStorage addAttribute: NSBackgroundColorAttributeName
+                                       value: [NSColor selectedTextBackgroundColor]
+                                       range: aRange];
     }
 }
 
--- NSTextView.m.orig   Tue Jul  2 18:32:44 2002
+++ NSTextView.m        Tue Jul  9 14:34:40 2002
@@ -1334,6 +1334,7 @@
      displayed selection could have been a temporary selection,
      different from the last official one: */
   NSRange oldDisplayedRange = _selected_range;
+  int     textStorageLength = 0;
 
   if (flag == YES)
     {
@@ -1375,6 +1376,30 @@
   /* FIXME: when and if to restart timer <and where to stop it before> */
   [self updateInsertionPointStateAndRestartTimer: !flag];
 
+  // Unhighlighting old selected range
+  textStorageLength = [_textStorage length];
+  if ( oldRange.length && (NSMaxRange(oldRange) <= textStorageLength) )
+       {
+         if (oldDisplayedRange.length)
+               {
+                 [_textStorage removeAttribute: NSBackgroundColorAttributeName
+                                               range: oldDisplayedRange];
+               }
+       }
+  else if ( oldRange.length && (NSMaxRange(oldRange) >= textStorageLength) )
+       {
+         // leaving text smaller then old range(deleted range)
+         [_textStorage removeAttribute: NSBackgroundColorAttributeName
+                                       range: NSMakeRange(0, textStorageLength)];
+       }
+  else if ( textStorageLength == 1 )
+       {
+         // Should not enter here: deleteForward calls us berfore deleting oldRange
+         // selected text
+         [_textStorage removeAttribute: NSBackgroundColorAttributeName
+                                       range: NSMakeRange(0, 1)];
+       }
+
   if (flag == NO)
     {
       [self updateFontPanel];
@@ -2403,14 +2428,14 @@
       return;
     }
 
+  /* The new selected range is just the insertion point at the beginning 
+     of deleted range */
+  [self setSelectedRange: NSMakeRange (range.location, 0)];
+
   [_textStorage beginEditing];
   [_textStorage deleteCharactersInRange: range];
   [_textStorage endEditing];
   [self didChangeText];
-
-  /* The new selected range is just the insertion point at the beginning 
-     of deleted range */
-  [self setSelectedRange: NSMakeRange (range.location, 0)];
 }
 
 - (void) deleteBackward: (id)sender

Reply via email to