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
10 * Full author contact details are available in file CREDITS.
16 #include "support/FileName.h"
17 #include "support/types.h"
24 // used by at least frontends/qt4/GuiPref.cpp
25 const long maxlastfiles = 20;
27 /** This session file maintains
28 1. the latest documents loaded (lastfiles)
29 2. cursor positions of files closed (lastfilepos)
30 3. opened files when a lyx session is closed (lastopened)
32 5. general purpose session info in the form of key/value pairs
36 /* base class for all sections in the session file
44 virtual ~SessionSection() {}
46 /// read section from std::istream
47 virtual void read(std::istream & is) = 0;
49 /// write to std::ostream
50 virtual void write(std::ostream & os) const = 0;
54 SessionSection(SessionSection const &);
55 void operator=(SessionSection const &);
59 class LastFilesSection : SessionSection
63 typedef std::deque<support::FileName> LastFiles;
67 explicit LastFilesSection(unsigned int num = 4);
70 void read(std::istream & is);
73 void write(std::ostream & os) const;
75 /// Return lastfiles container (deque)
76 LastFiles const lastFiles() const { return lastfiles; }
78 /** Insert #file# into the lastfile dequeue.
79 This funtion inserts #file# into the last files list. If the file
80 already exists it is moved to the top of the list, else exist it
81 is placed on the top of the list. If the list is full the last
82 file in the list is popped from the end.
83 @param file the file to insert in the lastfile list.
85 void add(support::FileName const & file);
88 /// Default number of lastfiles.
89 unsigned int const default_num_last_files;
91 /// Max number of lastfiles.
92 unsigned int const absolute_max_last_files;
94 /// a list of lastfiles
97 /// number of files in the lastfiles list.
98 unsigned int num_lastfiles;
100 /** Used by the constructor to set the number of stored last files.
101 @param num the number of lastfiles to set.
103 void setNumberOfLastFiles(unsigned int num);
107 class LastOpenedSection : SessionSection
111 typedef std::vector<support::FileName> LastOpened;
115 void read(std::istream & is);
118 void write(std::ostream & os) const;
120 /// Return lastopened container (vector)
121 LastOpened const getfiles() const { return lastopened; }
123 /** add file to lastopened file list
124 @param file filename to add
126 void add(support::FileName const & file);
128 /** clear lastopened file list
133 /// a list of lastopened files
134 LastOpened lastopened;
138 class LastFilePosSection : SessionSection
143 FilePos() : pit(0), pos(0) {}
149 typedef std::map<support::FileName, FilePos> FilePosMap;
153 LastFilePosSection() : num_lastfilepos(100) {}
156 void read(std::istream & is);
159 void write(std::ostream & os) const;
161 /** add cursor position to the fname entry in the filepos map
162 @param fname file entry for which to save position information
163 @param pos position of the cursor when the file is closed.
165 void save(support::FileName const & fname, FilePos const & pos);
167 /** load saved cursor position from the fname entry in the filepos map
168 @param fname file entry for which to load position information
170 FilePos load(support::FileName const & fname) const;
173 /// default number of lastfilepos to save */
174 unsigned int const num_lastfilepos;
177 /// a map of file positions
178 FilePosMap lastfilepos;
182 class BookmarksSection : SessionSection
185 /// A bookmark is composed of three parts
187 /// 2. bottom (whole document) level pit and pos, used to (inaccurately) save/restore a bookmark
188 /// 3. top level id and pos, used to accurately locate bookmark when lyx is running
189 /// top and bottom level information sometimes needs to be sync'ed. In particular,
190 /// top_id is determined when a bookmark is restored from session; and
191 /// bottom_pit and bottom_pos are determined from top_id when a bookmark
192 /// is save to session. (What a mess! :-)
194 /// TODO: bottom level pit and pos will be replaced by StableDocIterator
198 support::FileName filename;
199 /// Bottom level cursor pit, will be saved/restored by .lyx/session
201 /// Bottom level cursor position, will be saved/restore by .lyx/session
203 /// Top level cursor id, used to lcoate bookmarks for opened files
205 /// Top level cursor position within a paragraph
208 Bookmark() : bottom_pit(0), bottom_pos(0), top_id(0), top_pos(0) {}
210 Bookmark(support::FileName const & f, pit_type pit, pos_type pos, int id, pos_type tpos)
211 : filename(f), bottom_pit(pit), bottom_pos(pos), top_id(id), top_pos(tpos) {}
212 /// set bookmark top_id, this is because newly loaded bookmark
213 /// may have zero par_id and par_pit can change during editing, see bug 3092
214 void updatePos(pit_type pit, pos_type pos, int id) {
222 typedef std::vector<Bookmark> BookmarkList;
225 /// constructor, set max_bookmarks
226 /// allow 9 regular bookmarks, bookmark 0 is temporary
227 BookmarksSection() : bookmarks(10), max_bookmarks(9) {}
229 /// Save the current position as bookmark
230 void save(support::FileName const & fname, pit_type bottom_pit, pos_type bottom_pos,
231 int top_id, pos_type top_pos, unsigned int idx);
233 /// return bookmark 0-9, bookmark 0 is the temporary bookmark
234 Bookmark const & bookmark(unsigned int i) const;
236 /// does the given bookmark have a saved position ?
237 bool isValid(unsigned int i) const;
240 unsigned int size() const { return max_bookmarks; }
242 /// clear all bookmarks
246 void read(std::istream & is);
249 void write(std::ostream & os) const;
251 /** return bookmark list. Non-const container is used since
252 bookmarks will be cleaned after use.
254 BookmarkList & load() { return bookmarks; }
258 /// a list of bookmarks
259 BookmarkList bookmarks;
262 unsigned int const max_bookmarks;
266 class ToolbarSection : SessionSection
269 /// information about a toolbar, not all information can be
270 /// saved/restored by all frontends, but this class provides
271 /// a superset of things that can be managed by session.
277 : state(ON), location(NOTSET), posx(0), posy(0)
280 //ToolbarInfo(int s, int loc, int x = 0, int y = 0)
281 // : state(State(s)), location(Location(loc)), posx(x), posy(y)
294 /// location: this can be intepreted differently.
305 /// x-position of the toolbar
308 /// y-position of the toolbar
311 /// potentially, icons
319 /// info for each toolbar
320 typedef std::vector<ToolbarItem> ToolbarList;
325 void read(std::istream & is);
328 void write(std::ostream & os) const;
330 /// return reference to toolbar info, create a new one if needed
331 ToolbarInfo & load(std::string const & name);
334 ToolbarList::const_iterator begin() { return toolbars.begin(); }
337 ToolbarList::const_iterator end() { return toolbars.end(); }
340 /// toolbar information
341 ToolbarList toolbars;
344 /// comparison operator to sort toolbars, the rules are:
346 /// TOP < BOTTOM < LEFT < RIGHT
347 /// Line at each side
348 /// order in each line
349 bool operator<(ToolbarSection::ToolbarItem const & a,
350 ToolbarSection::ToolbarItem const & b);
353 class SessionInfoSection : SessionSection
357 typedef std::map<std::string, std::string> MiscInfo;
361 void read(std::istream & is);
364 void write(std::ostream & os) const;
367 @param key key of the value to store
368 @param value value, a string without newline ('\n')
370 void save(std::string const & key, std::string const & value);
372 /** load session info
373 @param key a key to extract value from the session file
374 @param release whether or not clear the value. Default to true
375 since most of such values are supposed to be used only once.
377 std::string const load(std::string const & key, bool release = true);
380 /// a map to save session info
381 MiscInfo sessioninfo;
388 /// Read the session file. @param num length of lastfiles
389 explicit Session(unsigned int num = 4);
390 /// Write the session file.
391 void writeFile() const;
393 LastFilesSection & lastFiles() { return last_files; }
395 LastFilesSection const & lastFiles() const { return last_files; }
397 LastOpenedSection & lastOpened() { return last_opened; }
399 LastOpenedSection const & lastOpened() const { return last_opened; }
401 LastFilePosSection & lastFilePos() { return last_file_pos; }
403 LastFilePosSection const & lastFilePos() const { return last_file_pos; }
405 BookmarksSection & bookmarks() { return bookmarks_; }
407 BookmarksSection const & bookmarks() const { return bookmarks_; }
409 ToolbarSection & toolbars() { return toolbars_; }
411 ToolbarSection const & toolbars() const { return toolbars_; }
413 SessionInfoSection & sessionInfo() { return session_info; }
415 SessionInfoSection const & sessionInfo() const { return session_info; }
419 Session(Session const &);
420 void operator=(Session const &);
422 /// file to save session, determined in the constructor.
423 support::FileName session_file;
425 /** Read the session file.
426 Reads the #.lyx/session# at the beginning of the LyX session.
427 This will read the session file (usually #.lyx/session#).
428 @param file the file containing the session.
433 LastFilesSection last_files;
435 LastOpenedSection last_opened;
437 LastFilePosSection last_file_pos;
439 BookmarksSection bookmarks_;
441 ToolbarSection toolbars_;
443 SessionInfoSection session_info;