]> git.lyx.org Git - lyx.git/blob - src/Session.h
Fix bug 4741: Pasting with middle mouse button into read only document works
[lyx.git] / src / Session.h
1 // -*- C++ -*-
2 /**
3  * \file Session.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  * \author Bo Peng
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef SESSION_H
14 #define SESSION_H
15
16 #include "support/FileName.h"
17 #include "support/types.h"
18
19 #include <string>
20 #include <deque>
21 #include <vector>
22 #include <map>
23
24 /** This session file maintains
25   1. the latest documents loaded (lastfiles)
26   2. cursor positions of files closed (lastfilepos)
27   3. opened files when a lyx session is closed (lastopened)
28   4. bookmarks
29   5. general purpose session info in the form of key/value pairs
30  */
31 namespace lyx {
32
33 /* base class for all sections in the session file
34 */
35 class SessionSection
36 {
37 public:
38         ///
39         SessionSection() {}
40         ///
41         virtual ~SessionSection() {}
42
43         /// read section from std::istream
44         virtual void read(std::istream & is) = 0;
45
46         /// write to std::ostream
47         virtual void write(std::ostream & os) const = 0;
48
49 private:
50         /// uncopiable
51         SessionSection(SessionSection const &);
52         void operator=(SessionSection const &);
53 };
54
55
56 class LastFilesSection : SessionSection
57 {
58 public:
59         ///
60         typedef std::deque<support::FileName> LastFiles;
61
62 public:
63         ///
64         explicit LastFilesSection(unsigned int num = 4);
65
66         ///
67         void read(std::istream & is);
68
69         ///
70         void write(std::ostream & os) const;
71
72         /// Return lastfiles container (deque)
73         LastFiles const lastFiles() const { return lastfiles; }
74
75         /** Insert #file# into the lastfile dequeue.
76             This funtion inserts #file# into the last files list. If the file
77             already exists it is moved to the top of the list, else exist it
78             is placed on the top of the list. If the list is full the last
79             file in the list is popped from the end.
80             @param file the file to insert in the lastfile list.
81         */
82         void add(support::FileName const & file);
83
84 private:
85         /// Default number of lastfiles.
86         unsigned int const default_num_last_files;
87
88         /// Max number of lastfiles.
89         unsigned int const absolute_max_last_files;
90
91         /// a list of lastfiles
92         LastFiles lastfiles;
93
94         /// number of files in the lastfiles list.
95         unsigned int num_lastfiles;
96
97         /** Used by the constructor to set the number of stored last files.
98             @param num the number of lastfiles to set.
99         */
100         void setNumberOfLastFiles(unsigned int num);
101 };
102
103
104 class LastOpenedSection : SessionSection
105 {
106 public:
107         ///
108         typedef std::vector<support::FileName> LastOpened;
109
110 public:
111         ///
112         void read(std::istream & is);
113
114         ///
115         void write(std::ostream & os) const;
116
117         /// Return lastopened container (vector)
118         LastOpened const getfiles() const { return lastopened; }
119
120         /** add file to lastopened file list
121             @param file filename to add
122         */
123         void add(support::FileName const & file);
124
125         /** clear lastopened file list
126          */
127         void clear();
128
129 private:
130         /// a list of lastopened files
131         LastOpened lastopened;
132 };
133
134
135 class LastFilePosSection : SessionSection
136 {
137 public:
138         ///
139         struct FilePos {
140                 FilePos() : pit(0), pos(0) {}
141                 pit_type pit;
142                 pos_type pos;
143         };
144
145         ///
146         typedef std::map<support::FileName, FilePos> FilePosMap;
147
148 public:
149         ///
150         LastFilePosSection() : num_lastfilepos(100) {}
151
152         ///
153         void read(std::istream & is);
154
155         ///
156         void write(std::ostream & os) const;
157
158         /** add cursor position to the fname entry in the filepos map
159             @param fname file entry for which to save position information
160             @param pos position of the cursor when the file is closed.
161         */
162         void save(support::FileName const & fname, FilePos const & pos);
163
164         /** load saved cursor position from the fname entry in the filepos map
165             @param fname file entry for which to load position information
166         */
167         FilePos load(support::FileName const & fname) const;
168
169 private:
170         /// default number of lastfilepos to save */
171         unsigned int const num_lastfilepos;
172
173
174         /// a map of file positions
175         FilePosMap lastfilepos;
176 };
177
178
179 class BookmarksSection : SessionSection
180 {
181 public:
182         /// A bookmark is composed of three parts
183         /// 1. filename
184         /// 2. bottom (whole document) level pit and pos, used to (inaccurately) save/restore a bookmark
185         /// 3. top level id and pos, used to accurately locate bookmark when lyx is running
186         /// top and bottom level information sometimes needs to be sync'ed. In particular,
187         /// top_id is determined when a bookmark is restored from session; and
188         /// bottom_pit and bottom_pos are determined from top_id when a bookmark
189         /// is save to session. (What a mess! :-)
190         ///
191         /// TODO: bottom level pit and pos will be replaced by StableDocIterator
192         class Bookmark {
193         public:
194                 /// Filename
195                 support::FileName filename;
196                 /// Bottom level cursor pit, will be saved/restored by .lyx/session
197                 pit_type bottom_pit;
198                 /// Bottom level cursor position, will be saved/restore by .lyx/session
199                 pos_type bottom_pos;
200                 /// Top level cursor id, used to lcoate bookmarks for opened files
201                 int top_id;
202                 /// Top level cursor position within a paragraph
203                 pos_type top_pos;
204                 ///
205                 Bookmark() : bottom_pit(0), bottom_pos(0), top_id(0), top_pos(0) {}
206                 ///
207                 Bookmark(support::FileName const & f, pit_type pit, pos_type pos, int id, pos_type tpos)
208                         : filename(f), bottom_pit(pit), bottom_pos(pos), top_id(id), top_pos(tpos) {}
209                 /// set bookmark top_id, this is because newly loaded bookmark
210                 /// may have zero par_id and par_pit can change during editing, see bug 3092
211                 void updatePos(pit_type pit, pos_type pos, int id) {
212                         bottom_pit = pit;
213                         bottom_pos = pos;
214                         top_id = id;
215                 }
216         };
217
218         ///
219         typedef std::vector<Bookmark> BookmarkList;
220
221 public:
222         /// constructor, set max_bookmarks
223         /// allow 9 regular bookmarks, bookmark 0 is temporary
224         BookmarksSection() : bookmarks(10), max_bookmarks(9) {}
225
226         /// Save the current position as bookmark
227         void save(support::FileName const & fname, pit_type bottom_pit, pos_type bottom_pos,
228                 int top_id, pos_type top_pos, unsigned int idx);
229
230         /// return bookmark 0-9, bookmark 0 is the temporary bookmark
231         Bookmark const & bookmark(unsigned int i) const;
232
233         /// does the given bookmark have a saved position ?
234         bool isValid(unsigned int i) const;
235
236         /// is there at least one bookmark that has a saved position ?
237         bool hasValid() const;
238
239         ///
240         unsigned int size() const { return max_bookmarks; }
241
242         /// clear all bookmarks
243         void clear();
244
245         ///
246         void read(std::istream & is);
247
248         ///
249         void write(std::ostream & os) const;
250
251         /** return bookmark list. Non-const container is used since
252                 bookmarks will be cleaned after use.
253         */
254         BookmarkList & load() { return bookmarks; }
255
256 private:
257
258         /// a list of bookmarks
259         BookmarkList bookmarks;
260
261         ///
262         unsigned int const max_bookmarks;
263 };
264
265
266 class Session
267 {
268 public:
269         /// Read the session file.  @param num length of lastfiles
270         explicit Session(unsigned int num = 4);
271         /// Write the session file.
272         void writeFile() const;
273         ///
274         LastFilesSection & lastFiles() { return last_files; }
275         ///
276         LastFilesSection const & lastFiles() const { return last_files; }
277         ///
278         LastOpenedSection & lastOpened() { return last_opened; }
279         ///
280         LastOpenedSection const & lastOpened() const { return last_opened; }
281         ///
282         LastFilePosSection & lastFilePos() { return last_file_pos; }
283         ///
284         LastFilePosSection const & lastFilePos() const { return last_file_pos; }
285         ///
286         BookmarksSection & bookmarks() { return bookmarks_; }
287         ///
288         BookmarksSection const & bookmarks() const { return bookmarks_; }
289
290 private:
291         friend class LyX;
292         /// uncopiable
293         Session(Session const &);
294         void operator=(Session const &);
295
296         /// file to save session, determined in the constructor.
297         support::FileName session_file;
298
299         /** Read the session file.
300             Reads the #.lyx/session# at the beginning of the LyX session.
301             This will read the session file (usually #.lyx/session#).
302             @param file the file containing the session.
303         */
304         void readFile();
305
306         ///
307         LastFilesSection last_files;
308         ///
309         LastOpenedSection last_opened;
310         ///
311         LastFilePosSection last_file_pos;
312         ///
313         BookmarksSection bookmarks_;
314 };
315
316 /// This is a singleton class. Get the instance.
317 /// Implemented in LyX.cpp.
318 Session & theSession();
319
320 } // lyx
321
322 #endif