4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
15 #include "support/std_string.h"
23 // This is only the very first implemetation and use of the TextCache,
24 // operations on it needs to be put into a class or a namespace, that part
25 // is _NOT_ finished so don't bother to come with too many comments on it
26 // (unless you have some nice ideas on where/how to do it)
28 // I think we need a global TextCache that is common for all BufferViews,
29 // also the BufferList needs access to the TextCache. Please tell if you
32 // Q. What are we caching?
33 // A. We are caching the screen representations (LyXText) of the
34 // documents (Buffer,Paragraph) for specific BufferView widths.
36 // A. It is not really needed, but it speeds things up a lot
37 // when you have more than one document loaded at once since a total
38 // rebreak (reformatting) need not be done when switching between
39 // documents. When the cache is in function a document only needs to be
40 // formatted upon loading and when the with of the BufferView changes.
41 // Later it will also be unneccessary to reformat when having two
42 // BufferViews of equal width with the same document, a simple copy
43 // of the LyXText structure will do.
44 // Invariant for the TextCache:
45 // - The buffer of the text in the TextCache _must_ exists
47 // - For a text in the TextCache there _must not_ be an equivalent
48 // text in any BufferView. (same buffer and width).
49 // Among others this mean:
50 // - When a document is closed all trace of it must be removed from
53 // I believe there are only three possible scenarios where the two first
54 // are also covered by the third.
55 // - The simplest scenario is what we have now, a single
58 // Nothing to do with the TextCache is done when opening a file.
60 // We switch from buffer A to buffer B.
61 // * A's text is cached in TextCache.
62 // * We make a search for a text in TextCache that fits B
63 // (same buffer and same width).
64 // o Horizontal resize
65 // If the BufferView's width (LyXView) is horizontally changed all
66 // the entries in the TextCache are deleted. (This causes
67 // reformat of all loaded documents when next viewed)
69 // When a buffer is closed we don't have to do anything, because
70 // to close a single buffer it is required to only exist in the
71 // BufferView and not in the TextCache. Upon LFUN_QUIT we
72 // don't really care since everything is deleted anyway.
73 // - The next scenario is when we have several BufferViews (in one or
74 // more LyXViews) of equal width.
76 // Nothing to do with the TextCache is done when opening a file.
78 // We switch from buffer A to buffer B.
79 // * If A is in another Bufferview we do not put it into TextCache.
80 // else we put A into TextCache.
81 // * If B is viewed in another BufferView we make a copy of its
82 // text and use that, else we search in TextCache for a match.
83 // (same buffer same width)
84 // o Horizontal resize
85 // If the BufferView's width (LyXView) is horisontaly changed all
86 // the entries in the TextCache is deleted. (This causes
87 // reformat of all loaded documents when next viewed)
89 // - The last scenario should cover both the previous ones, this time
90 // we have several BufferViews (in one or more LyXViews) with no
91 // limitations on width. (And if you wonder why the two other
92 // senarios are needed... I used them to get to this one.)
94 // Nothing to do with the TextCache is done when opening a file.
96 // We switch from buffer A to buffer B.
97 // o Horisontal rezize
100 /** This class is used to cache generated LyXText's.
101 The LyXText's is used by the BufferView to visualize the contents
102 of a buffer and its paragraphs. Instead of deleting the LyXText when
103 we switch from one document to another we cache it here so that when
104 we switch back we do not have to reformat. This makes switching very
105 fast at the expense of a bit higher memory usage.
110 typedef std::map<Buffer *, std::pair<int,LyXText *> > Cache;
113 typedef Cache::value_type value_type;
115 /** Returns a pointer to a LyXText that fits the provided buffer
116 and width. Of there is no match 0 is returned. */
117 LyXText * findFit(Buffer * b, int p);
118 /** Lists all the LyXText's currently in the cache.
119 Uses msg as header for the list. */
120 void show(std::ostream & o, string const & msg);
121 /// Gives info on a single LyXText (buffer and width)
122 static void show(std::ostream & o, value_type const &);
123 /** Adds a LyXText to the cache iff its buffer is
124 present in bufferlist. */
125 void add(Buffer *, int witdth, LyXText *);
126 /** Clears the cache. Deletes all LyXText's and releases
127 the allocated memory. */
129 /// Removes all LyXText's that has buffer b from the TextCache
130 void removeAllWithBuffer(Buffer * b);
137 extern TextCache textcache;