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"
19 #include <boost/utility.hpp>
20 #include <boost/tuple/tuple.hpp>
27 // used by at least frontends/qt2/QPref.C
28 const long maxlastfiles = 20;
30 /** This session file maintains
31 1. the latest documents loaded (lastfiles)
32 2. cursor positions of files closed (lastfilepos)
33 3. opened files when a lyx session is closed (lastopened)
35 5. general purpose session info in the form of key/value pairs
39 /** base class for all sections in the session file
41 class SessionSection : boost::noncopyable {
45 virtual ~SessionSection() {}
47 /// read section from std::istream
48 virtual void read(std::istream & is) = 0;
50 /// write to std::ostream
51 virtual void write(std::ostream & os) const = 0;
55 class LastFilesSection : SessionSection
59 typedef std::deque<support::FileName> LastFiles;
63 explicit LastFilesSection(unsigned int num = 4);
66 void read(std::istream & is);
69 void write(std::ostream & os) const;
71 /// Return lastfiles container (deque)
72 LastFiles const lastFiles() const { return lastfiles; }
74 /** Insert #file# into the lastfile dequeue.
75 This funtion inserts #file# into the last files list. If the file
76 already exists it is moved to the top of the list, else exist it
77 is placed on the top of the list. If the list is full the last
78 file in the list is popped from the end.
79 @param file the file to insert in the lastfile list.
81 void add(support::FileName const & file);
84 /// Default number of lastfiles.
85 unsigned int const default_num_last_files;
87 /// Max number of lastfiles.
88 unsigned int const absolute_max_last_files;
90 /// a list of lastfiles
93 /// number of files in the lastfiles list.
94 unsigned int num_lastfiles;
96 /** Used by the constructor to set the number of stored last files.
97 @param num the number of lastfiles to set.
99 void setNumberOfLastFiles(unsigned int num);
103 class LastOpenedSection : SessionSection
107 typedef std::vector<support::FileName> LastOpened;
111 void read(std::istream & is);
114 void write(std::ostream & os) const;
116 /// Return lastopened container (vector)
117 LastOpened const getfiles() const { return lastopened; }
119 /** add file to lastopened file list
120 @param file filename to add
122 void add(support::FileName const & file);
124 /** clear lastopened file list
129 /// a list of lastopened files
130 LastOpened lastopened;
134 class LastFilePosSection : SessionSection
138 typedef boost::tuple<pit_type, pos_type> FilePos;
141 typedef std::map<support::FileName, FilePos> FilePosMap;
145 LastFilePosSection() : num_lastfilepos(100) {}
148 void read(std::istream & is);
151 void write(std::ostream & os) const;
153 /** add cursor position to the fname entry in the filepos map
154 @param fname file entry for which to save position information
155 @param pos position of the cursor when the file is closed.
157 void save(support::FileName const & fname, FilePos pos);
159 /** load saved cursor position from the fname entry in the filepos map
160 @param fname file entry for which to load position information
162 FilePos load(support::FileName const & fname) const;
165 /// default number of lastfilepos to save */
166 unsigned int const num_lastfilepos;
169 /// a map of file positions
170 FilePosMap lastfilepos;
174 class BookmarksSection : SessionSection
181 support::FileName filename;
182 /// Cursor pit, will be saved/restored by .lyx/session
184 /// Cursor paragraph Id, used to lcoate bookmarks for opened files
186 /// Cursor position within a paragraph
189 Bookmark() : par_id(0), par_pos(0) {}
191 Bookmark(support::FileName const & f, pit_type pit, int id, pos_type pos)
192 : filename(f), par_pit(pit), par_id(id), par_pos(pos) {}
193 /// set bookmark par_id, this is because newly loaded bookmark
194 /// may have zero par_id and par_pit can change during editing, see bug 3092
195 void setPos(pit_type pit, int id) {
202 typedef std::vector<Bookmark> BookmarkList;
205 /// constructor, set max_bookmarks
206 /// allow 9 regular bookmarks, bookmark 0 is temporary
207 BookmarksSection() : bookmarks(10), max_bookmarks(9) {}
209 /// Save the current position as bookmark
210 void save(support::FileName const & fname, pit_type pit, int par_id, pos_type par_pos, unsigned int idx);
212 /// return bookmark 0-9, bookmark 0 is the temporary bookmark
213 Bookmark const & bookmark(unsigned int i) const;
215 /// does the given bookmark have a saved position ?
216 bool isValid(unsigned int i) const;
219 unsigned int size() const { return max_bookmarks; }
221 /// clear all bookmarks
225 void read(std::istream & is);
228 void write(std::ostream & os) const;
230 /** return bookmark list. Non-const container is used since
231 bookmarks will be cleaned after use.
233 BookmarkList & load() { return bookmarks; }
237 /// a list of bookmarks
238 BookmarkList bookmarks;
241 unsigned int const max_bookmarks;
245 class ToolbarSection : SessionSection
248 /// information about a toolbar, not all information can be
249 /// saved/restored by all frontends, but this class provides
250 /// a superset of things that can be managed by session.
256 state(ON), location(NOTSET), posx(0), posy(0) { }
258 ToolbarInfo(int s, int loc, int x=0, int y=0) :
259 state(static_cast<State>(s)),
260 location(static_cast<Location>(loc)),
275 /// location: this can be intepreted differently.
286 /// x-position of the toolbar
289 /// y-position of the toolbar
292 /// potentially, icons
295 typedef boost::tuple<std::string, ToolbarInfo> ToolbarItem;
297 /// info for each toolbar
298 typedef std::vector<ToolbarItem> ToolbarList;
303 void read(std::istream & is);
306 void write(std::ostream & os) const;
308 /// return reference to toolbar info, create a new one if needed
309 ToolbarInfo & load(std::string const & name);
312 ToolbarList::const_iterator begin() { return toolbars.begin(); }
315 ToolbarList::const_iterator end() { return toolbars.end(); }
318 /// toolbar information
319 ToolbarList toolbars;
322 /// comparison operator to sort toolbars, the rules are:
324 /// TOP < BOTTOM < LEFT < RIGHT
325 /// Line at each side
326 /// order in each line
327 bool operator< (ToolbarSection::ToolbarItem const & a, ToolbarSection::ToolbarItem const & b);
330 class SessionInfoSection : SessionSection
334 typedef std::map<std::string, std::string> MiscInfo;
338 void read(std::istream & is);
341 void write(std::ostream & os) const;
344 @param key key of the value to store
345 @param value value, a string without newline ('\n')
347 void save(std::string const & key, std::string const & value);
349 /** load session info
350 @param key a key to extract value from the session file
351 @param release whether or not clear the value. Default to true
352 since most of such values are supposed to be used only once.
354 std::string const load(std::string const & key, bool release = true);
357 /// a map to save session info
358 MiscInfo sessioninfo;
362 class Session : boost::noncopyable {
365 /** Read the session file.
366 @param num length of lastfiles
368 explicit Session(unsigned int num = 4);
370 /** Write the session file.
372 void writeFile() const;
375 LastFilesSection & lastFiles() { return last_files; }
378 LastFilesSection const & lastFiles() const { return last_files; }
381 LastOpenedSection & lastOpened() { return last_opened; }
384 LastOpenedSection const & lastOpened() const { return last_opened; }
387 LastFilePosSection & lastFilePos() { return last_file_pos; }
390 LastFilePosSection const & lastFilePos() const { return last_file_pos; }
393 BookmarksSection & bookmarks() { return bookmarks_; }
396 BookmarksSection const & bookmarks() const { return bookmarks_; }
399 ToolbarSection & toolbars() { return toolbars_; }
402 ToolbarSection const & toolbars() const { return toolbars_; }
405 SessionInfoSection & sessionInfo() { return session_info; }
408 SessionInfoSection const & sessionInfo() const { return session_info; }
411 /// file to save session, determined in the constructor.
412 support::FileName session_file;
414 /** Read the session file.
415 Reads the #.lyx/session# at the beginning of the LyX session.
416 This will read the session file (usually #.lyx/session#).
417 @param file the file containing the session.
422 LastFilesSection last_files;
425 LastOpenedSection last_opened;
428 LastFilePosSection last_file_pos;
431 BookmarksSection bookmarks_;
434 ToolbarSection toolbars_;
437 SessionInfoSection session_info;