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 "GuiCommandBuffer.h"
18 #include "GuiToolbar.h"
22 #include "BufferParams.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
30 #include "TextClass.h"
31 #include "ToolbarBackend.h"
40 #define TurnOnFlag(x) flags |= ToolbarInfo::x
41 #define TurnOffFlag(x) flags &= ~ToolbarInfo::x
43 GuiToolbars::GuiToolbars(GuiViewBase & owner)
46 last_textclass_(TextClassPtr())
50 void GuiToolbars::initFlags(ToolbarInfo & tbinfo)
52 ToolbarSection::ToolbarInfo & info = LyX::ref().session().toolbars().load(tbinfo.name);
54 unsigned int flags = static_cast<unsigned int>(tbinfo.flags);
56 // Remove default.ui positions. Only when a valid postion is stored
57 // in the session file the default.ui value will be overwritten
58 unsigned int save = flags;
64 bool valid_location = true;
65 // init tbinfo.flags with saved location
66 if (info.location == ToolbarSection::ToolbarInfo::TOP)
68 else if (info.location == ToolbarSection::ToolbarInfo::BOTTOM)
70 else if (info.location == ToolbarSection::ToolbarInfo::RIGHT)
72 else if (info.location == ToolbarSection::ToolbarInfo::LEFT)
75 // use setting from default.ui
77 valid_location = false;
80 // invalid location is for a new toolbar that has no saved information,
81 // so info.visible is not used for this case.
83 // init tbinfo.flags with saved visibility,
87 if (info.state == ToolbarSection::ToolbarInfo::ON)
89 else if (info.state == ToolbarSection::ToolbarInfo::OFF)
95 std::cout << "State " << info.state << " FLAGS: " << flags
96 << " ON:" << (flags & ToolbarBackend::ON)
97 << " OFF:" << (flags & ToolbarBackend::OFF)
98 << " L:" << (flags & ToolbarBackend::LEFT)
99 << " R:" << (flags & ToolbarBackend::RIGHT)
100 << " T:" << (flags & ToolbarBackend::TOP)
101 << " B:" << (flags & ToolbarBackend::BOTTOM)
102 << " MA:" << (flags & ToolbarBackend::MATH)
103 << " RE:" << (flags & ToolbarBackend::REVIEW)
104 << " TB:" << (flags & ToolbarBackend::TABLE)
105 << " AU:" << (flags & ToolbarBackend::AUTO)
109 tbinfo.flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
113 void GuiToolbars::init()
115 // extracts the toolbars from the backend
116 ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
117 ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
119 // init flags will also add these toolbars to session if they
120 // are not already there (e.g. first run of lyx).
121 for (; cit != end; ++cit)
124 // add toolbars according the order in session
125 ToolbarSection::ToolbarList::const_iterator tb =
126 LyX::ref().session().toolbars().begin();
127 ToolbarSection::ToolbarList::const_iterator te =
128 LyX::ref().session().toolbars().end();
129 ToolbarSection::ToolbarInfo::Location last_loc =
130 ToolbarSection::ToolbarInfo::NOTSET;
133 for (; tb != te; ++tb) {
134 LYXERR(Debug::INIT) << "Adding " << tb->key << " at position "
135 << tb->info.posx << " " << tb->info.posy << endl;
136 // add toolbar break if posx or posy changes
137 bool newline = tb->info.location == last_loc && (
138 // if two toolbars at the same location, assume uninitialized and add toolbar break
139 (tb->info.posx == last_posx && tb->info.posy == last_posy) ||
140 (last_loc == ToolbarSection::ToolbarInfo::TOP && tb->info.posy != last_posy) ||
141 (last_loc == ToolbarSection::ToolbarInfo::BOTTOM && tb->info.posy != last_posy) ||
142 (last_loc == ToolbarSection::ToolbarInfo::LEFT && tb->info.posx != last_posx) ||
143 (last_loc == ToolbarSection::ToolbarInfo::RIGHT && tb->info.posx != last_posx) );
144 // find the backend item and add
145 for (cit = toolbarbackend.begin(); cit != end; ++cit)
146 if (cit->name == tb->key) {
148 last_loc = tb->info.location;
149 last_posx = tb->info.posx;
150 last_posy = tb->info.posy;
157 void GuiToolbars::display(string const & name, bool show)
159 ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
160 ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
162 for (; cit != end; ++cit) {
163 if (cit->name == name) {
164 unsigned int flags = cit->flags;
172 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
173 displayToolbar(*cit, show);
177 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
182 ToolbarInfo * GuiToolbars::getToolbarInfo(string const & name)
184 return toolbarbackend.getUsedToolbarInfo(name);
188 void GuiToolbars::toggleToolbarState(string const & name, bool allowauto)
190 ToolbarInfo * tbi = toolbarbackend.getUsedToolbarInfo(name);
193 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
198 int flags = tbi->flags;
200 if (flags & ToolbarInfo::OFF) {
204 } else if (flags & ToolbarInfo::AUTO) {
208 && ((flags & ToolbarInfo::MATH)
209 || (flags & ToolbarInfo::TABLE)
210 || (flags & ToolbarInfo::REVIEW))) {
211 // for math etc, toggle from on -> auto
215 // for others, toggle from on -> off
219 tbi->flags = static_cast<ToolbarInfo::Flags>(flags);
225 void GuiToolbars::update(bool in_math, bool in_table, bool review)
229 // extracts the toolbars from the backend
230 ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
231 ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
233 for (; cit != end; ++cit) {
234 if (cit->flags & ToolbarInfo::ON)
235 displayToolbar(*cit, true);
236 else if (cit->flags & ToolbarInfo::OFF)
237 displayToolbar(*cit, false);
238 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::MATH))
239 displayToolbar(*cit, in_math);
240 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::TABLE))
241 displayToolbar(*cit, in_table);
242 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::REVIEW))
243 displayToolbar(*cit, review);
248 bool GuiToolbars::visible(string const & name) const
250 std::map<string, GuiToolbar *>::const_iterator it =
251 toolbars_.find(name);
252 if (it == toolbars_.end())
254 return it->second->isVisible();
258 void GuiToolbars::saveToolbarInfo()
260 ToolbarSection & tb = LyX::ref().session().toolbars();
262 for (ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
263 cit != toolbarbackend.end(); ++cit) {
264 ToolbarsMap::iterator it = toolbars_.find(cit->name);
265 BOOST_ASSERT(it != toolbars_.end());
266 // get toolbar info from session.
267 ToolbarSection::ToolbarInfo & info = tb.load(cit->name);
268 if (cit->flags & ToolbarInfo::ON)
269 info.state = ToolbarSection::ToolbarInfo::ON;
270 else if (cit->flags & ToolbarInfo::OFF)
271 info.state = ToolbarSection::ToolbarInfo::OFF;
272 else if (cit->flags & ToolbarInfo::AUTO)
273 info.state = ToolbarSection::ToolbarInfo::AUTO;
274 // save other information
275 // if auto, frontend should *not* set on/off
276 it->second->saveInfo(info);
277 // maybe it is useful to update flags with real status. I do not know
279 if (!(cit->flags & ToolbarInfo::AUTO)) {
280 unsigned int flags = static_cast<unsigned int>(cit->flags);
281 flags &= ~(info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::OFF : ToolbarInfo::ON);
282 flags |= (info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::ON : ToolbarInfo::OFF);
283 if (info.state == ToolbarSection::ToolbarInfo::ON)
284 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
291 void GuiToolbars::setLayout(docstring const & layout)
294 layout_->set(layout);
298 bool GuiToolbars::updateLayoutList(TextClassPtr textclass)
300 // update the layout display
301 if (last_textclass_ != textclass) {
303 layout_->updateContents();
304 last_textclass_ = textclass;
311 void GuiToolbars::openLayoutList()
314 layout_->showPopup();
318 void GuiToolbars::clearLayoutList()
320 last_textclass_ = TextClassPtr();
326 void GuiToolbars::add(ToolbarInfo const & tbinfo, bool newline)
328 GuiToolbar * tb_ptr = owner_.makeToolbar(tbinfo, newline);
329 toolbars_[tbinfo.name] = tb_ptr;
331 if (tbinfo.flags & ToolbarInfo::ON)
336 if (tb_ptr->layout())
337 layout_ = tb_ptr->layout();
341 void GuiToolbars::displayToolbar(ToolbarInfo const & tbinfo,
344 ToolbarsMap::iterator it = toolbars_.find(tbinfo.name);
345 BOOST_ASSERT(it != toolbars_.end());
348 if (it->second->isVisible())
352 else if (it->second->isVisible())
357 void GuiToolbars::updateIcons()
359 ToolbarsMap::const_iterator it = toolbars_.begin();
360 ToolbarsMap::const_iterator const end = toolbars_.end();
361 for (; it != end; ++it)
362 it->second->updateContents();
365 lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled();
368 layout_->setEnabled(enable);
372 void GuiToolbars::showCommandBuffer(bool show_it)
374 ToolbarsMap::const_iterator it = toolbars_.begin();
375 ToolbarsMap::const_iterator const end = toolbars_.end();
376 for (; it != end; ++it) {
377 GuiCommandBuffer * cb = it->second->commandBuffer();
384 if (!it->second->isVisible())
391 } // namespace frontend