]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlToc.cpp
fix scrolling bug: 3320 and 3652, maybe not perfect
[lyx.git] / src / frontends / controllers / ControlToc.cpp
1 /**
2  * \file ControlToc.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "ControlToc.h"
15
16 #include "Buffer.h"
17 #include "BufferView.h"
18 #include "BufferParams.h"
19 #include "debug.h"
20 #include "FloatList.h"
21 #include "FuncRequest.h"
22 #include "gettext.h"
23
24 #include "frontends/LyXView.h"
25
26 #include "support/convert.h"
27
28 using std::string;
29
30
31 class Buffer;
32
33 namespace lyx {
34 namespace frontend {
35
36
37 ControlToc::ControlToc(Dialog & d)
38         : ControlCommand(d, "tableofcontents", "toc")
39 {
40 }
41
42
43 TocList const & ControlToc::tocs() const
44 {
45         return kernel().buffer().getMasterBuffer()->tocBackend().tocs();
46 }
47
48
49 bool ControlToc::initialiseParams(string const & data)
50 {
51         if (!ControlCommand::initialiseParams(data))
52                 return false;
53
54         types_.clear();
55         type_names_.clear();
56         TocList const & tocs = kernel().buffer().getMasterBuffer()->
57                 tocBackend().tocs();
58         TocList::const_iterator it = tocs.begin();
59         TocList::const_iterator end = tocs.end();
60         for (; it != end; ++it) {
61                 types_.push_back(it->first);
62                 type_names_.push_back(getGuiName(it->first));
63         }
64
65         string selected_type ;
66         if(params()["type"].empty()) //Then plain toc...
67                 selected_type = params().getCmdName();
68         else
69                 selected_type = to_ascii(params()["type"]);
70         selected_type_ = -1;
71         for (size_t i = 0;  i != types_.size(); ++i) {
72                 if (selected_type == types_[i]) {
73                         selected_type_ = i;
74                         break;
75                 }
76         }
77
78         return true;
79 }
80
81 void ControlToc::goTo(TocItem const & item)
82 {
83         string const tmp = convert<string>(item.id());
84         kernel().lyxview().dispatch(FuncRequest(LFUN_PARAGRAPH_GOTO, tmp));
85 }
86
87
88 bool ControlToc::canOutline(size_t type) const
89 {
90         return types_[type] == "tableofcontents";
91 }
92
93
94 void ControlToc::outlineUp()
95 {
96         kernel().dispatch(FuncRequest(LFUN_OUTLINE_UP));
97 }
98
99
100 void ControlToc::outlineDown()
101 {
102         kernel().dispatch(FuncRequest(LFUN_OUTLINE_DOWN));
103 }
104
105
106 void ControlToc::outlineIn()
107 {
108         kernel().dispatch(FuncRequest(LFUN_OUTLINE_IN));
109 }
110
111
112 void ControlToc::outlineOut()
113 {
114         kernel().dispatch(FuncRequest(LFUN_OUTLINE_OUT));
115 }
116
117
118 void ControlToc::updateBackend()
119 {
120         kernel().buffer().getMasterBuffer()->tocBackend().update();
121         kernel().buffer().structureChanged();
122 }
123
124
125 TocIterator const ControlToc::getCurrentTocItem(size_t type) const
126 {
127         BOOST_ASSERT(kernel().bufferview());
128         ParConstIterator it(kernel().bufferview()->cursor());
129         Buffer const * master = kernel().buffer().getMasterBuffer();
130         return master->tocBackend().item(types_[type], it);
131 }
132
133
134 docstring const ControlToc::getGuiName(string const & type) const
135 {
136         if (type == "tableofcontents")
137                 return _("Table of Contents");
138
139         FloatList const & floats =
140                 kernel().buffer().params().getTextClass().floats();
141         if (floats.typeExist(type))
142                 return from_utf8(floats.getType(type).name());
143         else
144                 return _(type);
145 }
146
147 } // namespace frontend
148 } // namespace lyx