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"
26 #include "TextClass.h"
35 Toolbars::Toolbars(LyXView & owner)
38 last_textclass_(TextClassPtr())
41 #define TurnOnFlag(x) flags |= ToolbarInfo::x
42 #define TurnOffFlag(x) flags &= ~ToolbarInfo::x
44 void Toolbars::initFlags(ToolbarInfo & tbinfo)
46 ToolbarSection::ToolbarInfo & info = LyX::ref().session().toolbars().load(tbinfo.name);
48 unsigned int flags = static_cast<unsigned int>(tbinfo.flags);
50 // Remove default.ui positions. Only when a valid postion is stored
51 // in the session file the default.ui value will be overwritten
52 unsigned int save = flags;
58 bool valid_location = true;
59 // init tbinfo.flags with saved location
60 if (info.location == ToolbarSection::ToolbarInfo::TOP)
62 else if (info.location == ToolbarSection::ToolbarInfo::BOTTOM)
64 else if (info.location == ToolbarSection::ToolbarInfo::RIGHT)
66 else if (info.location == ToolbarSection::ToolbarInfo::LEFT)
69 // use setting from default.ui
71 valid_location = false;
74 // invalid location is for a new toolbar that has no saved information,
75 // so info.visible is not used for this case.
77 // init tbinfo.flags with saved visibility,
81 if (info.state == ToolbarSection::ToolbarInfo::ON)
83 else if (info.state == ToolbarSection::ToolbarInfo::OFF)
89 std::cout << "State " << info.state << " FLAGS: " << flags
90 << " ON:" << (flags & ToolbarBackend::ON)
91 << " OFF:" << (flags & ToolbarBackend::OFF)
92 << " L:" << (flags & ToolbarBackend::LEFT)
93 << " R:" << (flags & ToolbarBackend::RIGHT)
94 << " T:" << (flags & ToolbarBackend::TOP)
95 << " B:" << (flags & ToolbarBackend::BOTTOM)
96 << " MA:" << (flags & ToolbarBackend::MATH)
97 << " RE:" << (flags & ToolbarBackend::REVIEW)
98 << " TB:" << (flags & ToolbarBackend::TABLE)
99 << " AU:" << (flags & ToolbarBackend::AUTO)
103 tbinfo.flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
107 void Toolbars::init()
109 // extracts the toolbars from the backend
110 ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
111 ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
113 // init flags will also add these toolbars to session if they
114 // are not already there (e.g. first run of lyx).
115 for (; cit != end; ++cit)
118 // add toolbars according the order in session
119 ToolbarSection::ToolbarList::const_iterator tb = LyX::ref().session().toolbars().begin();
120 ToolbarSection::ToolbarList::const_iterator te = LyX::ref().session().toolbars().end();
121 ToolbarSection::ToolbarInfo::Location last_loc = ToolbarSection::ToolbarInfo::NOTSET;
124 for (; tb != te; ++tb) {
125 LYXERR(Debug::INIT) << "Adding " << tb->get<0>() << " at position " << tb->get<1>().posx << " " << tb->get<1>().posy << endl;
126 // add toolbar break if posx or posy changes
127 bool newline = tb->get<1>().location == last_loc && (
128 // if two toolbars at the same location, assume uninitialized and add toolbar break
129 (tb->get<1>().posx == last_posx && tb->get<1>().posy == last_posy) ||
130 (last_loc == ToolbarSection::ToolbarInfo::TOP && tb->get<1>().posy != last_posy) ||
131 (last_loc == ToolbarSection::ToolbarInfo::BOTTOM && tb->get<1>().posy != last_posy) ||
132 (last_loc == ToolbarSection::ToolbarInfo::LEFT && tb->get<1>().posx != last_posx) ||
133 (last_loc == ToolbarSection::ToolbarInfo::RIGHT && tb->get<1>().posx != last_posx) );
134 // find the backend item and add
135 for (cit = toolbarbackend.begin(); cit != end; ++cit)
136 if (cit->name == tb->get<0>()) {
138 last_loc = tb->get<1>().location;
139 last_posx = tb->get<1>().posx;
140 last_posy = tb->get<1>().posy;
147 Toolbar * Toolbars::display(string const & name, bool show)
149 ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
150 ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
152 for (; cit != end; ++cit) {
153 if (cit->name == name) {
154 unsigned int flags = cit->flags;
162 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
163 return displayToolbar(*cit, show);
167 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
173 ToolbarInfo * Toolbars::getToolbarInfo(string const & name)
175 return toolbarbackend.getUsedToolbarInfo(name);
179 void Toolbars::toggleToolbarState(string const & name, bool allowauto)
181 ToolbarInfo * tbi = toolbarbackend.getUsedToolbarInfo(name);
184 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
189 int flags = tbi->flags;
191 if (flags & ToolbarInfo::OFF) {
195 } else if (flags & ToolbarInfo::AUTO) {
199 && ((flags & ToolbarInfo::MATH)
200 || (flags & ToolbarInfo::TABLE)
201 || (flags & ToolbarInfo::REVIEW))) {
202 // for math etc, toggle from on -> auto
206 // for others, toggle from on -> off
210 tbi->flags = static_cast<ToolbarInfo::Flags>(flags);
216 void Toolbars::update(bool in_math, bool in_table, bool review)
220 // extracts the toolbars from the backend
221 ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
222 ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
224 for (; cit != end; ++cit) {
225 if (cit->flags & ToolbarInfo::ON)
226 displayToolbar(*cit, true);
227 else if (cit->flags & ToolbarInfo::OFF)
228 displayToolbar(*cit, false);
229 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::MATH))
230 displayToolbar(*cit, in_math);
231 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::TABLE))
232 displayToolbar(*cit, in_table);
233 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::REVIEW))
234 displayToolbar(*cit, review);
239 bool Toolbars::visible(string const & name) const
241 std::map<string, ToolbarPtr>::const_iterator it =
242 toolbars_.find(name);
243 if (it == toolbars_.end())
245 return it->second.get()->isVisible();
249 void Toolbars::saveToolbarInfo()
251 ToolbarSection & tb = LyX::ref().session().toolbars();
253 for (ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
254 cit != toolbarbackend.end(); ++cit) {
255 ToolbarsMap::iterator it = toolbars_.find(cit->name);
256 BOOST_ASSERT(it != toolbars_.end());
257 // get toolbar info from session.
258 ToolbarSection::ToolbarInfo & info = tb.load(cit->name);
259 if (cit->flags & ToolbarInfo::ON)
260 info.state = ToolbarSection::ToolbarInfo::ON;
261 else if (cit->flags & ToolbarInfo::OFF)
262 info.state = ToolbarSection::ToolbarInfo::OFF;
263 else if (cit->flags & ToolbarInfo::AUTO)
264 info.state = ToolbarSection::ToolbarInfo::AUTO;
265 // save other information
266 // if auto, frontend should *not* set on/off
267 it->second->saveInfo(info);
268 // maybe it is useful to update flags with real status. I do not know
270 if (!(cit->flags & ToolbarInfo::AUTO)) {
271 unsigned int flags = static_cast<unsigned int>(cit->flags);
272 flags &= ~(info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::OFF : ToolbarInfo::ON);
273 flags |= (info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::ON : ToolbarInfo::OFF);
274 if (info.state == ToolbarSection::ToolbarInfo::ON)
275 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
282 void Toolbars::setLayout(docstring const & layout)
285 layout_->set(layout);
289 bool Toolbars::updateLayoutList(TextClassPtr textclass)
291 // update the layout display
292 if (last_textclass_ != textclass) {
295 last_textclass_ = textclass;
302 void Toolbars::openLayoutList()
309 void Toolbars::clearLayoutList()
311 last_textclass_ = TextClassPtr();
317 void Toolbars::add(ToolbarInfo const & tbinfo, bool newline)
319 ToolbarPtr tb_ptr = owner_.makeToolbar(tbinfo, newline);
320 toolbars_[tbinfo.name] = tb_ptr;
322 if (tbinfo.flags & ToolbarInfo::ON)
327 if (tb_ptr->layout())
328 layout_ = tb_ptr->layout();
332 Toolbar * Toolbars::displayToolbar(ToolbarInfo const & tbinfo,
335 ToolbarsMap::iterator it = toolbars_.find(tbinfo.name);
336 BOOST_ASSERT(it != toolbars_.end());
339 it->second->show(true);
341 it->second->hide(true);
343 return it->second.get();
347 void Toolbars::update()
349 ToolbarsMap::const_iterator it = toolbars_.begin();
350 ToolbarsMap::const_iterator const end = toolbars_.end();
351 for (; it != end; ++it)
352 it->second->update();
355 lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled();
358 layout_->setEnabled(enable);
362 void layoutSelected(LyXView & lv, docstring const & name)
364 TextClass const & tc = lv.buffer()->params().getTextClass();
366 TextClass::const_iterator it = tc.begin();
367 TextClass::const_iterator const end = tc.end();
368 for (; it != end; ++it) {
369 docstring const & itname = (*it)->name();
370 if (translateIfPossible(itname) == name) {
371 FuncRequest const func(LFUN_LAYOUT, itname,
372 FuncRequest::TOOLBAR);
377 lyxerr << "ERROR (layoutSelected): layout not found!"
381 } // namespace frontend