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