Re: Memory limitation of NSScrollView
Re: Memory limitation of NSScrollView
- Subject: Re: Memory limitation of NSScrollView
- From: Greg Titus <email@hidden>
- Date: Thu, 26 Jul 2001 12:11:37 -0700
On Thursday, July 26, 2001, at 06:16 AM, Christian Mike wrote:
>
I am implementing a scrolling command window for my application as an
>
NSScrollView. I would like the user to be able to move the cursor
>
anywhere
>
in the window, modify the text that is there, and press the enter key to
>
"execute" whatever is on that line. In order to implement that, I need
>
some
>
help locating the cursor.
The first thing that will probably help is to realize that you are going
to be putting the text into an NSTextView. The NSScrollView surrounds
the text view and handles the scrollbars. In Interface Builder, you want
to connect your outlet to the text view and not the scroll view around
it.
>
1.) Is there any way to find out where the cursor is within the text
>
stream
>
of the view?
[textView selectedRange] will return an NSRange which has a location and
a length. The length will be zero if you have the insertion point, and
non-zero if the user has selected some text.
>
2.) Is there any way to programmatically move the cursor back to the
>
bottom
>
of the text stream so that all new output is appended to the end of
>
everything?
[textView setSelectedRange:aRange];
You can get the range of the text in the stream by looking at the
NSTextView's NSTextStorage, which is the object which actually holds the
text itself.
endRange = NSMakeRange([[textView textStorage] length], 0);
...will get you a zero-length range at the very end of the text.
>
I am porting an application from Mac OS 9 (and Windows NT) to Mac OS X.
>
This
>
application has a user command window for entering text commands and
>
viewing
>
text output. In Max OS 9 and Windows NT, I must keep track of the size
>
of
>
the buffer that contains the "contents" of the command window. As that
>
buffer fills up, I discard the oldest stuff at the top and keep only the
>
newest text that is getting appended to the end. The user can scroll the
>
window back quite a ways and view anything that was not discarded.
>
>
Now I want to do the same thing in Cocoa. I have a few questions.
>
>
1.) If I create an NSScrollView in IB and start throwing text into it,
>
how
>
big is the default buffer before it fills up and won't accept any more?
As above, it's the text view not the scroll view. And there is no
limit - it will grow to whatever size you want.
>
2.) If it will grow automatically to an unlimited size, should I put
>
some
>
artificial limit (say 2MB) on the size and start throwing out the oldest
>
stuff so that it doesn't hog memory?
Probably a good idea, yes, at least as a preference. Terminal.app, for
instance, lets you set a preference for how much of a scrollback buffer
to keep - either unlimited length, or a user configurable # of lines.
>
3.) In Windows NT, I programmatically select the oldest text in the
>
window
>
(usually scrolled off the top) and delete (cut) it. Will that same
>
approach
>
work here, or should I do something different?
You can do it the way you describe, by programmatically changing the
selection in the NSTextView, sending a -delete: action, then resetting
the selection.
But better: you can simply adjust the text content of the NSTextStorage
and the view will automatically reflect the change. The text storage is
a subclass of NSMutableAttributedString, so you can use the
-deleteCharactersInRange: method:
someRange = NSMakeRange(0, 1000); // the first 1000 characters...
[[textView textStorage] deleteCharactersInRange:someRange];
Hope this helps,
--Greg