Displaying large text file in WPF without reading it into memory - wpf

I would like to be able to display a large text file in WPF - preferably as a FlowDocument (in order to style its appearance), but without reading the entire file into memory.
My idea would be to implement some sort of virtualized document = list of lines which however holds a limited set of lines in memory at a time. For example, if the file has 1,000,000 lines, the collection would initially only load lines 1-1000 into memory and when asked for lines beyond that would load another 100 lines while dropping the first 100, etc. This would need to be able to go in both directions, so when navigating back, the collection would need to re-read the first 100 lines, while dropping the last 100 lines from memory.
Furthermore, the collection would need to correctly reflect the total amount of lines - ideally up front, alternatively as it proceeds through the file.
(note: collection size 1000 and "page" size 100 are just examples).
How could this be displayed in a WPF control without the control itself storing all lines? I believe an ItemsControl with a virtualizing StackPanel could perhaps somehow do this, but it may be somewhat limited. Could FlowDocumentScrollViewer be customized to support this?

Related

Scrolling data in ncurses

I'm working on a small text editor in ncurses with the purpose of learning more about the library. One of the first challenges was implementing a proper scrollable text buffer, retaining the editing abilities. I've created a cursor struct, containing the screen coordinates and the buffer coordinates. When you move the cursor, the x and y are constrained to the LINES and COLS max values. The buffer coordinates, however, are constrainted to the limits of the text file (size and linesize).
This works well, but i was wondering if there's a better way of doing this. Right now, every cursor movement operation results in modifications to both coordinate systems. Maybe there's a way of converting between coordinates and keep just one (the buffer one, preferably)?
Have you tried using a pad? As a window can be no larger than the terminal itself, else data is lost when if passes over the edge boundary. A pad is used to allow for larger data display by the use of newpad. The pad can be any length the system memory has available; viewed by way of a window subpad which displays the contents of the pad at a specified location.

Maintain text position in RichTextBox while adding and removing text to the end and beginning respectively

I have a very simple form with a RichTextBox field. It is acting as a crude log file viewer. As log data is added to the file I append the new data to the text box. This works great but I wanted to prevent the text box from scrolling when the new data was appended so the currently visible data could be read. I experimented with this for a while and I ultimately had to call the native SendEvent function to get and set the scroll position before and after appending the text. This has also been working great, however, I now have a new problem: over time the log file can grow quite large and use huge amounts of memory to keep the entire contents of the log file in the text box. So now what I've done is limited the amount of text in the text box by removing text from the beginning if the text size reaches a certain threshold.
So now my problem is that I can maintain the scroll position, but the text will change since I am removing text from the beginning. Is there a simple way to change the scroll position to maintain readability (obviously if the user is reading the first line of the text box and that is removed then there is nothing that can be done, but I think that is acceptable)?
Here's an example: say my threshold is 1MB of text in the text box. If there is currently 1MB of text, and I need to append 1000 characters then I also need to remove 1000 characters from the beginning. If I am scrolled to the middle of the text then I can maintain that scroll position after the removal and appending of text but now the text that I was reading is further up.
Maybe I need to rethink my approach?
I realized that modifying the text while the user is viewing it doesn't really make sense. So I decided to add an option to this form to 'freeze' updates to the text box. When frozen no text will be appended or removed and the user can freely scroll around. When unfrozen all of the appending/removing works as normal.

WPF: find how much space a control needs

In my application I have an area in the main window that at any time can contain one of several different controls.
This controls are generated at runtime and their contents can vary depending on underlying data, so I do not know beforehand how much space they'll take up.
What I want to know is: is there a way to determine at runtime how much space a control needs in order not to be "cut off" or need a scroll? ie: how much space does it need to be COMPLETELY visible?
I tried the "DesiredSize" property and it kinda works, but not always: if the control has been used already (it has already a size) it returns it's last used size rather than the correct one, even if I call "InvalidateMeasure()".
Any ideas??
Call Measure on the control. Give it infinite space as the available size for the calculation. Then check the DesiredSize to get the needed width (and/or height).

Is it possible to get rendered text from textBlock

I have textBlock defined such that it fills the entire screen of the phone.
The textBlock is initialized with some data which cannot be displayed in the boundary and hence gets clipped.
I want to read the data which actually got rendered on the screen (i.e. whole data - clipped data).
Putting a breakpoint shows me that myNewTextBlock.Text contains the entire data that it was initialized with.
Thanks
You could look at using Measure and MeasureOverride to determine how much of the Text would fit in the available space.
You'll likely need to test various trimmed versions of the Text but it shouldn't be too tricky.

TextBox control limits lines to 1024 characters regardless of WordWrap setting. How to increase?

The TextBox control (in a .NET winforms application) seems to hard limit line lengths to 1024 characters, even with the WordWrap property set to false. Is there a way override this?
I need to view lengthy data in a visual sense. Currently a line of 2000 characters wraps onto the second line and causes the right edge of several lines to be jagged rather than aligning.
The problem is that the Windows Edit control that the TextBox uses for its implementation has a limit of 1024 characters per line. This lmit has been around forever. Rather than fix it (which could cause problems with programs that somehow rely on this limit), they simply introduced the RichEdit control that doesn't have that limit and has many more features.

Resources