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