2 * \file GuiToolbars.cpp
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
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "GuiToolbars.h"
17 #include "GuiToolbar.h"
21 #include "BufferParams.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
29 #include "TextClass.h"
38 #define TurnOnFlag(x) flags |= ToolbarInfo::x
39 #define TurnOffFlag(x) flags &= ~ToolbarInfo::x
41 GuiToolbars::GuiToolbars(GuiViewBase & owner)
44 last_textclass_(TextClassPtr())
48 void GuiToolbars::initFlags(ToolbarInfo & tbinfo)
50 ToolbarSection::ToolbarInfo & info = LyX::ref().session().toolbars().load(tbinfo.name);
52 unsigned int flags = static_cast<unsigned int>(tbinfo.flags);
54 // Remove default.ui positions. Only when a valid postion is stored
55 // in the session file the default.ui value will be overwritten
56 unsigned int save = flags;
62 bool valid_location = true;
63 // init tbinfo.flags with saved location
64 if (info.location == ToolbarSection::ToolbarInfo::TOP)
66 else if (info.location == ToolbarSection::ToolbarInfo::BOTTOM)
68 else if (info.location == ToolbarSection::ToolbarInfo::RIGHT)
70 else if (info.location == ToolbarSection::ToolbarInfo::LEFT)
73 // use setting from default.ui
75 valid_location = false;
78 // invalid location is for a new toolbar that has no saved information,
79 // so info.visible is not used for this case.
81 // init tbinfo.flags with saved visibility,
85 if (info.state == ToolbarSection::ToolbarInfo::ON)
87 else if (info.state == ToolbarSection::ToolbarInfo::OFF)
93 std::cout << "State " << info.state << " FLAGS: " << flags
94 << " ON:" << (flags & ToolbarBackend::ON)
95 << " OFF:" << (flags & ToolbarBackend::OFF)
96 << " L:" << (flags & ToolbarBackend::LEFT)
97 << " R:" << (flags & ToolbarBackend::RIGHT)
98 << " T:" << (flags & ToolbarBackend::TOP)
99 << " B:" << (flags & ToolbarBackend::BOTTOM)
100 << " MA:" << (flags & ToolbarBackend::MATH)
101 << " RE:" << (flags & ToolbarBackend::REVIEW)
102 << " TB:" << (flags & ToolbarBackend::TABLE)
103 << " AU:" << (flags & ToolbarBackend::AUTO)
107 tbinfo.flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
111 void GuiToolbars::init()
113 // extracts the toolbars from the backend
114 ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
115 ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
117 // init flags will also add these toolbars to session if they
118 // are not already there (e.g. first run of lyx).
119 for (; cit != end; ++cit)
122 // add toolbars according the order in session
123 ToolbarSection::ToolbarList::const_iterator tb = LyX::ref().session().toolbars().begin();
124 ToolbarSection::ToolbarList::const_iterator te = LyX::ref().session().toolbars().end();
125 ToolbarSection::ToolbarInfo::Location last_loc = ToolbarSection::ToolbarInfo::NOTSET;
128 for (; tb != te; ++tb) {
129 LYXERR(Debug::INIT) << "Adding " << tb->get<0>() << " at position " << tb->get<1>().posx << " " << tb->get<1>().posy << endl;
130 // add toolbar break if posx or posy changes
131 bool newline = tb->get<1>().location == last_loc && (
132 // if two toolbars at the same location, assume uninitialized and add toolbar break
133 (tb->get<1>().posx == last_posx && tb->get<1>().posy == last_posy) ||
134 (last_loc == ToolbarSection::ToolbarInfo::TOP && tb->get<1>().posy != last_posy) ||
135 (last_loc == ToolbarSection::ToolbarInfo::BOTTOM && tb->get<1>().posy != last_posy) ||
136 (last_loc == ToolbarSection::ToolbarInfo::LEFT && tb->get<1>().posx != last_posx) ||
137 (last_loc == ToolbarSection::ToolbarInfo::RIGHT && tb->get<1>().posx != last_posx) );
138 // find the backend item and add
139 for (cit = toolbarbackend.begin(); cit != end; ++cit)
140 if (cit->name == tb->get<0>()) {
142 last_loc = tb->get<1>().location;
143 last_posx = tb->get<1>().posx;
144 last_posy = tb->get<1>().posy;
151 void GuiToolbars::display(string const & name, bool show)
153 ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
154 ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
156 for (; cit != end; ++cit) {
157 if (cit->name == name) {
158 unsigned int flags = cit->flags;
166 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
167 displayToolbar(*cit, show);
171 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
176 ToolbarInfo * GuiToolbars::getToolbarInfo(string const & name)
178 return toolbarbackend.getUsedToolbarInfo(name);
182 void GuiToolbars::toggleToolbarState(string const & name, bool allowauto)
184 ToolbarInfo * tbi = toolbarbackend.getUsedToolbarInfo(name);
187 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
192 int flags = tbi->flags;
194 if (flags & ToolbarInfo::OFF) {
198 } else if (flags & ToolbarInfo::AUTO) {
202 && ((flags & ToolbarInfo::MATH)
203 || (flags & ToolbarInfo::TABLE)
204 || (flags & ToolbarInfo::REVIEW))) {
205 // for math etc, toggle from on -> auto
209 // for others, toggle from on -> off
213 tbi->flags = static_cast<ToolbarInfo::Flags>(flags);
219 void GuiToolbars::update(bool in_math, bool in_table, bool review)
223 // extracts the toolbars from the backend
224 ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
225 ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
227 for (; cit != end; ++cit) {
228 if (cit->flags & ToolbarInfo::ON)
229 displayToolbar(*cit, true);
230 else if (cit->flags & ToolbarInfo::OFF)
231 displayToolbar(*cit, false);
232 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::MATH))
233 displayToolbar(*cit, in_math);
234 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::TABLE))
235 displayToolbar(*cit, in_table);
236 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::REVIEW))
237 displayToolbar(*cit, review);
242 bool GuiToolbars::visible(string const & name) const
244 std::map<string, GuiToolbar *>::const_iterator it =
245 toolbars_.find(name);
246 if (it == toolbars_.end())
248 return it->second->isVisible();
252 void GuiToolbars::saveToolbarInfo()
254 ToolbarSection & tb = LyX::ref().session().toolbars();
256 for (ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
257 cit != toolbarbackend.end(); ++cit) {
258 ToolbarsMap::iterator it = toolbars_.find(cit->name);
259 BOOST_ASSERT(it != toolbars_.end());
260 // get toolbar info from session.
261 ToolbarSection::ToolbarInfo & info = tb.load(cit->name);
262 if (cit->flags & ToolbarInfo::ON)
263 info.state = ToolbarSection::ToolbarInfo::ON;
264 else if (cit->flags & ToolbarInfo::OFF)
265 info.state = ToolbarSection::ToolbarInfo::OFF;
266 else if (cit->flags & ToolbarInfo::AUTO)
267 info.state = ToolbarSection::ToolbarInfo::AUTO;
268 // save other information
269 // if auto, frontend should *not* set on/off
270 it->second->saveInfo(info);
271 // maybe it is useful to update flags with real status. I do not know
273 if (!(cit->flags & ToolbarInfo::AUTO)) {
274 unsigned int flags = static_cast<unsigned int>(cit->flags);
275 flags &= ~(info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::OFF : ToolbarInfo::ON);
276 flags |= (info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::ON : ToolbarInfo::OFF);
277 if (info.state == ToolbarSection::ToolbarInfo::ON)
278 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
285 void GuiToolbars::setLayout(docstring const & layout)
288 layout_->set(layout);
292 bool GuiToolbars::updateLayoutList(TextClassPtr textclass)
294 // update the layout display
295 if (last_textclass_ != textclass) {
297 layout_->updateContents();
298 last_textclass_ = textclass;
305 void GuiToolbars::openLayoutList()
308 layout_->showPopup();
312 void GuiToolbars::clearLayoutList()
314 last_textclass_ = TextClassPtr();
320 void GuiToolbars::add(ToolbarInfo const & tbinfo, bool newline)
322 GuiToolbar * tb_ptr = owner_.makeToolbar(tbinfo, newline);
323 toolbars_[tbinfo.name] = tb_ptr;
325 if (tbinfo.flags & ToolbarInfo::ON)
330 if (tb_ptr->layout())
331 layout_ = tb_ptr->layout();
335 void GuiToolbars::displayToolbar(ToolbarInfo const & tbinfo,
338 ToolbarsMap::iterator it = toolbars_.find(tbinfo.name);
339 BOOST_ASSERT(it != toolbars_.end());
342 if (it->second->isVisible())
346 else if (it->second->isVisible())
351 void GuiToolbars::updateIcons()
353 ToolbarsMap::const_iterator it = toolbars_.begin();
354 ToolbarsMap::const_iterator const end = toolbars_.end();
355 for (; it != end; ++it)
356 it->second->updateContents();
359 lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled();
362 layout_->setEnabled(enable);
366 void GuiToolbars::showCommandBuffer(bool show_it)
368 ToolbarsMap::const_iterator it = toolbars_.begin();
369 ToolbarsMap::const_iterator const end = toolbars_.end();
370 for (; it != end; ++it) {
371 GuiCommandBuffer * cb = it->second->commandBuffer();
378 if (!it->second->isVisible())
385 } // namespace frontend