2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team
9 * This file is Copyright 2000
12 * ====================================================== */
22 #include "support/LOstream.h"
27 // This is only the very first implemetation and use of the TextCache,
28 // operations on it needs to be put into a class or a namespace, that part
29 // is _NOT_ finished so don't bother to come with too many comments on it
30 // (unless you have some nice ideas on where/how to do it)
32 // I think we need a global TextCache that is common for all BufferViews,
33 // also the BufferList needs access to the TextCache. Please tell if you
36 // Q. What are we caching?
37 // A. We are caching the screen representations (LyXText) of the
38 // documents (Buffer,LyXParagraph) for specific BufferView widths.
40 // A. It is not really needed, but it speeds things up a lot
41 // when you have more than one document loaded at once since a total
42 // rebreak (reformatting) need not be done when switching between
43 // documents. When the cache is in function a document only needs to be
44 // formatted upon loading and when the with of the BufferView changes.
45 // Later it will also be unneccessary to reformat when having two
46 // BufferViews of equal width with the same document, a simple copy
47 // of the LyXText structure will do.
48 // Invariant for the TextCache:
49 // - The buffer of the text in the TextCache _must_ exists
51 // - For a text in the TextCache there _must not_ be an equivalent
52 // text in any BufferView. (same buffer and width).
53 // Among others this mean:
54 // - When a document is closed all trace of it must be removed from
57 // I believe there are only three possible scenarios where the two first
58 // are also covered by the third.
59 // - The simplest scenario is what we have now, a single
62 // Nothing to do with the TextCache is done when opening a file.
64 // We switch from buffer A to buffer B.
65 // * A's text is cached in TextCache.
66 // * We make a search for a text in TextCache that fits B
67 // (same buffer and same width).
68 // o Horizontal resize
69 // If the BufferView's width (LyXView) is horizontally changed all
70 // the entries in the TextCache are deleted. (This causes
71 // reformat of all loaded documents when next viewed)
73 // When a buffer is closed we don't have to do anything, because
74 // to close a single buffer it is required to only exist in the
75 // BufferView and not in the TextCache. Upon LFUN_QUIT we
76 // don't really care since everything is deleted anyway.
77 // - The next scenario is when we have several BufferViews (in one or
78 // more LyXViews) of equal width.
80 // Nothing to do with the TextCache is done when opening a file.
82 // We switch from buffer A to buffer B.
83 // * If A is in another Bufferview we do not put it into TextCache.
84 // else we put A into TextCache.
85 // * If B is viewed in another BufferView we make a copy of its
86 // text and use that, else we search in TextCache for a match.
87 // (same buffer same width)
88 // o Horizontal resize
89 // If the BufferView's width (LyXView) is horisontaly changed all
90 // the entries in the TextCache is deleted. (This causes
91 // reformat of all loaded documents when next viewed)
93 // - The last scenario should cover both the previous ones, this time
94 // we have several BufferViews (in one or more LyXViews) with no
95 // limitations on width. (And if you wonder why the two other
96 // senarios are needed... I used them to get to this one.)
98 // Nothing to do with the TextCache is done when opening a file.
100 // We switch from buffer A to buffer B.
101 // o Horisontal rezize
104 /** This class is used to cache generated LyXText's.
105 The LyXText's is used by the BufferView to visualize the contents
106 of a buffer and its paragraphs. Instead of deleting the LyXText when
107 we switch from one document to another we cache it here so that when
108 we switch back we do not have to reformat. This makes switching very
109 fast at the expense of a bit higher memory usage.
114 typedef vector<LyXText*> Cache;
116 typedef LyXText * value_type;
118 /** Returns a pointer to a LyXText that fits the provided buffer
119 and width. Of there is no match 0 is returned. */
120 LyXText * findFit(Buffer * b, unsigned short p);
121 /** Lists all the LyXText's currently in the cache.
122 Uses msg as header for the list. */
123 void show(ostream & o, string const & msg);
124 /// Gives info on a single LyXText (buffer and width)
125 static void show(ostream & o, LyXText const *);
126 /** Adds a LyXText to the cache iff its buffer is
127 present in bufferlist. */
129 /** Clears the cache. Deletes all LyXText's and releases
130 the allocated memory. */
132 /// Removes all LyXText's that has buffer b from the TextCache
133 void removeAllWithBuffer(Buffer * b);
140 extern TextCache textcache;