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