1 // -*- C++ -*-
2 /**
3  * \file TocBackend.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Jean-Marc Lasgouttes
8  * \author Angus Leeming
9  * \author Abdelrazak Younes
10  * \author Guillaume Munch
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14 
15 #ifndef TOC_BACKEND_H
16 #define TOC_BACKEND_H
17 
18 #include "DocIterator.h"
19 #include "FuncRequest.h"
20 #include "OutputEnums.h"
21 #include "Toc.h"
22 #include "TocBuilder.h"
23 
24 #include "support/strfwd.h"
25 #include "support/unique_ptr.h"
26 
27 
28 namespace lyx {
29 
30 class Buffer;
31 
32 /* Toc types are described by strings. They cannot be converted into an enum
33  * because of the user-configurable categories for index and the user-definable
34  * toc types provided in layout files.
35  *
36  * Here is a summary of built-in toc types
37  *
38  * Non-customizable (used without TocBuilder): "tableofcontents", "change",
39  * "citation", "label", "senseless".
40  *
41  * Built-in but customizable (used with TocBuilder): "child", "graphics",
42  * "equation", "index", "index:<user-str>", "nomencl", "listings", "math-macro",
43  * "external", any float type (as defined in the layouts).
44  *
45  * The following are used for XHTML output: "tableofcontents" (InsetText),
46  * "citation" (InsetCitation), any float type.
47  *
48  * Other types are defined in the layouts.
49  */
50 
51 ///
52 /**
53 */
54 class TocItem
55 {
56 public:
57 	/// Default constructor for STL containers.
TocItem()58 	TocItem() : dit_(0), depth_(0), output_(false) {}
59 	///
60 	TocItem(DocIterator const & dit,
61 		int depth,
62 		docstring const & s,
63 		bool output_active,
64 		FuncRequest action = FuncRequest(LFUN_UNKNOWN_ACTION)
65 		);
66 	///
dit()67 	DocIterator const & dit() const { return dit_; }
68 	///
depth()69 	int depth() const { return depth_; }
70 	///
str()71 	docstring const & str() const { return str_; }
72 	///
str(docstring const & s)73 	void str(docstring const & s) { str_ = s; }
74 	///
isOutput()75 	bool isOutput() const { return output_; }
76 	///
setAction(FuncRequest a)77 	void setAction(FuncRequest a) { action_ = a; }
78 
79 	/// custom action, or the default one (paragraph-goto) if not customised
80 	FuncRequest action() const;
81 	///
82 	int id() const;
83 	/// String for display, e.g. it has a mark if output is inactive
84 	docstring const asString() const;
85 
86 private:
87 	/// Current position of item.
88 	DocIterator dit_;
89 	/// nesting depth
90 	int depth_;
91 	/// Full item string
92 	docstring str_;
93 	/// Is this item in a note, inactive branch, etc?
94 	bool output_;
95 	/// Custom action
96 	FuncRequest action_;
97 };
98 
99 
100 /// Class to build and access the Tocs of a particular buffer.
101 class TocBackend
102 {
103 public:
104 	static Toc::const_iterator findItem(Toc const & toc,
105 	                                    DocIterator const & dit);
106 	/// Look for a TocItem given its depth and string.
107 	/// \return The first matching item.
108 	/// \retval end() if no item was found.
109 	static Toc::iterator findItem(Toc & toc, int depth, docstring const & str);
110 	///
TocBackend(Buffer const * buffer)111 	TocBackend(Buffer const * buffer) : buffer_(buffer) {}
112 	///
setBuffer(Buffer const * buffer)113 	void setBuffer(Buffer const * buffer) { buffer_ = buffer; }
114 	///
115 	void update(bool output_active, UpdateType utype);
116 	/// \return true if the item was updated.
117 	bool updateItem(DocIterator const & pit);
118 	///
tocs()119 	TocList const & tocs() const { return tocs_; }
120 	/// never null
121 	std::shared_ptr<Toc const> toc(std::string const & type) const;
122 	/// never null
123 	std::shared_ptr<Toc> toc(std::string const & type);
124 	/// \return the current TocBuilder for the Toc of type \param type, or
125 	/// creates one if it does not already exist.
126 	TocBuilder & builder(std::string const & type);
127 	/// \return the first Toc Item before the cursor.
128 	/// \param type: Type of Toc.
129 	/// \param dit: The cursor location in the document.
130 	Toc::const_iterator
131 	item(std::string const & type, DocIterator const & dit) const;
132 
133 	///
134 	void writePlaintextTocList(std::string const & type,
135 	        odocstringstream & os, size_t max_length) const;
136 	/// Localised name for type
137 	docstring outlinerName(std::string const & type) const;
138 	/// Add a new (localised) name if yet unknown
139 	void addName(std::string const & type, docstring const & name);
140 	/// Whether a toc type is less important and appears in the "Other lists"
141 	/// submenu
142 	static bool isOther(std::string const & type);
143 
144 private:
145 	///
146 	void resetOutlinerNames();
147 	///
148 	TocList tocs_;
149 	///
150 	std::map<std::string, unique_ptr<TocBuilder>> builders_;
151 	/// Stores localised outliner names from this buffer and its children
152 	std::map<std::string, docstring> outliner_names_;
153 	///
154 	Buffer const * buffer_;
155 }; // TocBackend
156 
157 
158 } // namespace lyx
159 
160 #endif // TOC_BACKEND_H
161