3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
14 #include "frontends/Toolbars.h"
16 #include "frontends/LyXView.h"
19 #include "BufferParams.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
27 #include "TextClass.h"
36 Toolbars::Toolbars(LyXView & owner)
39 last_textclass_(TextClassPtr())
42 #define TurnOnFlag(x) flags |= ToolbarInfo::x
43 #define TurnOffFlag(x) flags &= ~ToolbarInfo::x
45 void Toolbars::initFlags(ToolbarInfo & tbinfo)
47 ToolbarSection::ToolbarInfo & info = LyX::ref().session().toolbars().load(tbinfo.name);
49 unsigned int flags = static_cast<unsigned int>(tbinfo.flags);
51 // Remove default.ui positions. Only when a valid postion is stored
52 // in the session file the default.ui value will be overwritten
53 unsigned int save = flags;
59 bool valid_location = true;
60 // init tbinfo.flags with saved location
61 if (info.location == ToolbarSection::ToolbarInfo::TOP)
63 else if (info.location == ToolbarSection::ToolbarInfo::BOTTOM)
65 else if (info.location == ToolbarSection::ToolbarInfo::RIGHT)
67 else if (info.location == ToolbarSection::ToolbarInfo::LEFT)
70 // use setting from default.ui
72 valid_location = false;
75 // invalid location is for a new toolbar that has no saved information,
76 // so info.visible is not used for this case.
78 // init tbinfo.flags with saved visibility,
82 if (info.state == ToolbarSection::ToolbarInfo::ON)
84 else if (info.state == ToolbarSection::ToolbarInfo::OFF)
90 std::cout << "State " << info.state << " FLAGS: " << flags
91 << " ON:" << (flags & ToolbarBackend::ON)
92 << " OFF:" << (flags & ToolbarBackend::OFF)
93 << " L:" << (flags & ToolbarBackend::LEFT)
94 << " R:" << (flags & ToolbarBackend::RIGHT)
95 << " T:" << (flags & ToolbarBackend::TOP)
96 << " B:" << (flags & ToolbarBackend::BOTTOM)
97 << " MA:" << (flags & ToolbarBackend::MATH)
98 << " RE:" << (flags & ToolbarBackend::REVIEW)
99 << " TB:" << (flags & ToolbarBackend::TABLE)
100 << " AU:" << (flags & ToolbarBackend::AUTO)
104 tbinfo.flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
108 void Toolbars::init()
110 // extracts the toolbars from the backend
111 ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
112 ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
114 // init flags will also add these toolbars to session if they
115 // are not already there (e.g. first run of lyx).
116 for (; cit != end; ++cit)
119 // add toolbars according the order in session
120 ToolbarSection::ToolbarList::const_iterator tb = LyX::ref().session().toolbars().begin();
121 ToolbarSection::ToolbarList::const_iterator te = LyX::ref().session().toolbars().end();
122 ToolbarSection::ToolbarInfo::Location last_loc = ToolbarSection::ToolbarInfo::NOTSET;
125 for (; tb != te; ++tb) {
126 LYXERR(Debug::INIT) << "Adding " << tb->get<0>() << " at position " << tb->get<1>().posx << " " << tb->get<1>().posy << endl;
127 // add toolbar break if posx or posy changes
128 bool newline = tb->get<1>().location == last_loc && (
129 // if two toolbars at the same location, assume uninitialized and add toolbar break
130 (tb->get<1>().posx == last_posx && tb->get<1>().posy == last_posy) ||
131 (last_loc == ToolbarSection::ToolbarInfo::TOP && tb->get<1>().posy != last_posy) ||
132 (last_loc == ToolbarSection::ToolbarInfo::BOTTOM && tb->get<1>().posy != last_posy) ||
133 (last_loc == ToolbarSection::ToolbarInfo::LEFT && tb->get<1>().posx != last_posx) ||
134 (last_loc == ToolbarSection::ToolbarInfo::RIGHT && tb->get<1>().posx != last_posx) );
135 // find the backend item and add
136 for (cit = toolbarbackend.begin(); cit != end; ++cit)
137 if (cit->name == tb->get<0>()) {
139 last_loc = tb->get<1>().location;
140 last_posx = tb->get<1>().posx;
141 last_posy = tb->get<1>().posy;
148 Toolbar * Toolbars::display(string const & name, bool show)
150 ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
151 ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
153 for (; cit != end; ++cit) {
154 if (cit->name == name) {
155 unsigned int flags = cit->flags;
163 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
164 return displayToolbar(*cit, show);
168 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
174 ToolbarInfo * Toolbars::getToolbarInfo(string const & name)
176 return toolbarbackend.getUsedToolbarInfo(name);
180 void Toolbars::toggleToolbarState(string const & name, bool allowauto)
182 ToolbarInfo * tbi = toolbarbackend.getUsedToolbarInfo(name);
185 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
190 int flags = tbi->flags;
192 if (flags & ToolbarInfo::OFF) {
196 } else if (flags & ToolbarInfo::AUTO) {
200 && ((flags & ToolbarInfo::MATH)
201 || (flags & ToolbarInfo::TABLE)
202 || (flags & ToolbarInfo::REVIEW))) {
203 // for math etc, toggle from on -> auto
207 // for others, toggle from on -> off
211 tbi->flags = static_cast<ToolbarInfo::Flags>(flags);
217 void Toolbars::update(bool in_math, bool in_table, bool review)
221 // extracts the toolbars from the backend
222 ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
223 ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
225 for (; cit != end; ++cit) {
226 if (cit->flags & ToolbarInfo::ON)
227 displayToolbar(*cit, true);
228 else if (cit->flags & ToolbarInfo::OFF)
229 displayToolbar(*cit, false);
230 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::MATH))
231 displayToolbar(*cit, in_math);
232 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::TABLE))
233 displayToolbar(*cit, in_table);
234 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::REVIEW))
235 displayToolbar(*cit, review);
240 bool Toolbars::visible(string const & name) const
242 std::map<string, ToolbarPtr>::const_iterator it =
243 toolbars_.find(name);
244 if (it == toolbars_.end())
246 return it->second.get()->isVisible();
250 void Toolbars::saveToolbarInfo()
252 ToolbarSection & tb = LyX::ref().session().toolbars();
254 for (ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
255 cit != toolbarbackend.end(); ++cit) {
256 ToolbarsMap::iterator it = toolbars_.find(cit->name);
257 BOOST_ASSERT(it != toolbars_.end());
258 // get toolbar info from session.
259 ToolbarSection::ToolbarInfo & info = tb.load(cit->name);
260 if (cit->flags & ToolbarInfo::ON)
261 info.state = ToolbarSection::ToolbarInfo::ON;
262 else if (cit->flags & ToolbarInfo::OFF)
263 info.state = ToolbarSection::ToolbarInfo::OFF;
264 else if (cit->flags & ToolbarInfo::AUTO)
265 info.state = ToolbarSection::ToolbarInfo::AUTO;
266 // save other information
267 // if auto, frontend should *not* set on/off
268 it->second->saveInfo(info);
269 // maybe it is useful to update flags with real status. I do not know
271 if (!(cit->flags & ToolbarInfo::AUTO)) {
272 unsigned int flags = static_cast<unsigned int>(cit->flags);
273 flags &= ~(info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::OFF : ToolbarInfo::ON);
274 flags |= (info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::ON : ToolbarInfo::OFF);
275 if (info.state == ToolbarSection::ToolbarInfo::ON)
276 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
283 void Toolbars::setLayout(docstring const & layout)
286 layout_->set(layout);
290 bool Toolbars::updateLayoutList(TextClassPtr textclass)
292 // update the layout display
293 if (last_textclass_ != textclass) {
296 last_textclass_ = textclass;
303 void Toolbars::openLayoutList()
310 void Toolbars::clearLayoutList()
312 last_textclass_ = TextClassPtr();
318 void Toolbars::add(ToolbarInfo const & tbinfo, bool newline)
320 ToolbarPtr tb_ptr(owner_.makeToolbar(tbinfo, newline));
321 toolbars_[tbinfo.name] = tb_ptr;
323 if (tbinfo.flags & ToolbarInfo::ON)
328 if (tb_ptr->layout())
329 layout_ = tb_ptr->layout();
333 Toolbar * Toolbars::displayToolbar(ToolbarInfo const & tbinfo,
336 ToolbarsMap::iterator it = toolbars_.find(tbinfo.name);
337 BOOST_ASSERT(it != toolbars_.end());
340 it->second->show(true);
342 it->second->hide(true);
344 return it->second.get();
348 void Toolbars::update()
350 ToolbarsMap::const_iterator it = toolbars_.begin();
351 ToolbarsMap::const_iterator const end = toolbars_.end();
352 for (; it != end; ++it)
353 it->second->update();
356 lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled();
359 layout_->setEnabled(enable);
363 void layoutSelected(LyXView & lv, docstring const & name)
365 TextClass const & tc = lv.buffer()->params().getTextClass();
367 TextClass::const_iterator it = tc.begin();
368 TextClass::const_iterator const end = tc.end();
369 for (; it != end; ++it) {
370 docstring const & itname = (*it)->name();
371 if (translateIfPossible(itname) == name) {
372 FuncRequest const func(LFUN_LAYOUT, itname,
373 FuncRequest::TOOLBAR);
378 lyxerr << "ERROR (layoutSelected): layout not found!"
382 } // namespace frontend