Update translations for LyX 1.6.0
[lyx.git] / src / Session.h
blobd673da22e950121849a8be5707c14b6f83fde558
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.
7 * \author Lars Gullik Bjønnes
8 * \author Bo Peng
10 * Full author contact details are available in file CREDITS.
13 #ifndef SESSION_H
14 #define SESSION_H
16 #include "support/FileName.h"
17 #include "support/types.h"
19 #include <string>
20 #include <deque>
21 #include <vector>
22 #include <map>
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
31 namespace lyx {
33 /* base class for all sections in the session file
35 class SessionSection
37 public:
38 ///
39 SessionSection() {}
40 ///
41 virtual ~SessionSection() {}
43 /// read section from std::istream
44 virtual void read(std::istream & is) = 0;
46 /// write to std::ostream
47 virtual void write(std::ostream & os) const = 0;
49 private:
50 /// uncopiable
51 SessionSection(SessionSection const &);
52 void operator=(SessionSection const &);
56 class LastFilesSection : SessionSection
58 public:
59 ///
60 typedef std::deque<support::FileName> LastFiles;
62 public:
63 ///
64 explicit LastFilesSection(unsigned int num = 4);
66 ///
67 void read(std::istream & is);
69 ///
70 void write(std::ostream & os) const;
72 /// Return lastfiles container (deque)
73 LastFiles const lastFiles() const { return lastfiles; }
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.
82 void add(support::FileName const & file);
84 private:
85 /// Default number of lastfiles.
86 unsigned int const default_num_last_files;
88 /// Max number of lastfiles.
89 unsigned int const absolute_max_last_files;
91 /// a list of lastfiles
92 LastFiles lastfiles;
94 /// number of files in the lastfiles list.
95 unsigned int num_lastfiles;
97 /** Used by the constructor to set the number of stored last files.
98 @param num the number of lastfiles to set.
100 void setNumberOfLastFiles(unsigned int num);
104 class LastOpenedSection : SessionSection
106 public:
108 typedef std::vector<support::FileName> LastOpened;
110 public:
112 void read(std::istream & is);
115 void write(std::ostream & os) const;
117 /// Return lastopened container (vector)
118 LastOpened const getfiles() const { return lastopened; }
120 /** add file to lastopened file list
121 @param file filename to add
123 void add(support::FileName const & file);
125 /** clear lastopened file list
127 void clear();
129 private:
130 /// a list of lastopened files
131 LastOpened lastopened;
135 class LastFilePosSection : SessionSection
137 public:
139 struct FilePos {
140 FilePos() : pit(0), pos(0) {}
141 pit_type pit;
142 pos_type pos;
146 typedef std::map<support::FileName, FilePos> FilePosMap;
148 public:
150 LastFilePosSection() : num_lastfilepos(100) {}
153 void read(std::istream & is);
156 void write(std::ostream & os) const;
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.
162 void save(support::FileName const & fname, FilePos const & pos);
164 /** load saved cursor position from the fname entry in the filepos map
165 @param fname file entry for which to load position information
167 FilePos load(support::FileName const & fname) const;
169 private:
170 /// default number of lastfilepos to save */
171 unsigned int const num_lastfilepos;
174 /// a map of file positions
175 FilePosMap lastfilepos;
179 class BookmarksSection : SessionSection
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! :-)
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;
205 Bookmark() : bottom_pit(0), bottom_pos(0), top_id(0), top_pos(0) {}
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;
219 typedef std::vector<Bookmark> BookmarkList;
221 public:
222 /// constructor, set max_bookmarks
223 /// allow 9 regular bookmarks, bookmark 0 is temporary
224 BookmarksSection() : bookmarks(10), max_bookmarks(9) {}
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);
230 /// return bookmark 0-9, bookmark 0 is the temporary bookmark
231 Bookmark const & bookmark(unsigned int i) const;
233 /// does the given bookmark have a saved position ?
234 bool isValid(unsigned int i) const;
237 unsigned int size() const { return max_bookmarks; }
239 /// clear all bookmarks
240 void clear();
243 void read(std::istream & is);
246 void write(std::ostream & os) const;
248 /** return bookmark list. Non-const container is used since
249 bookmarks will be cleaned after use.
251 BookmarkList & load() { return bookmarks; }
253 private:
255 /// a list of bookmarks
256 BookmarkList bookmarks;
259 unsigned int const max_bookmarks;
263 class Session
265 public:
266 /// Read the session file. @param num length of lastfiles
267 explicit Session(unsigned int num = 4);
268 /// Write the session file.
269 void writeFile() const;
271 LastFilesSection & lastFiles() { return last_files; }
273 LastFilesSection const & lastFiles() const { return last_files; }
275 LastOpenedSection & lastOpened() { return last_opened; }
277 LastOpenedSection const & lastOpened() const { return last_opened; }
279 LastFilePosSection & lastFilePos() { return last_file_pos; }
281 LastFilePosSection const & lastFilePos() const { return last_file_pos; }
283 BookmarksSection & bookmarks() { return bookmarks_; }
285 BookmarksSection const & bookmarks() const { return bookmarks_; }
287 private:
288 friend class LyX;
289 /// uncopiable
290 Session(Session const &);
291 void operator=(Session const &);
293 /// file to save session, determined in the constructor.
294 support::FileName session_file;
296 /** Read the session file.
297 Reads the #.lyx/session# at the beginning of the LyX session.
298 This will read the session file (usually #.lyx/session#).
299 @param file the file containing the session.
301 void readFile();
304 LastFilesSection last_files;
306 LastOpenedSection last_opened;
308 LastFilePosSection last_file_pos;
310 BookmarksSection bookmarks_;
313 /// This is a singleton class. Get the instance.
314 /// Implemented in LyX.cpp.
315 Session & theSession();
317 } // lyx
319 #endif