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.
17 #include "BufferParams.h"
19 #include "FuncRequest.h"
20 #include "FuncStatus.h"
23 #include "TextClass.h"
34 Toolbars::Toolbars(LyXView & owner)
40 #define TurnOnFlag(x) flags |= ToolbarInfo::x
41 #define TurnOffFlag(x) flags &= ~ToolbarInfo::x
43 void Toolbars::initFlags(ToolbarInfo & tbinfo)
45 ToolbarSection::ToolbarInfo & info = LyX::ref().session().toolbars().load(tbinfo.name);
47 unsigned int flags = static_cast<unsigned int>(tbinfo.flags);
49 // Remove default.ui positions. Only when a valid postion is stored
50 // in the session file the default.ui value will be overwritten
51 unsigned int save = flags;
57 bool valid_location = true;
58 // init tbinfo.flags with saved location
59 if (info.location == ToolbarSection::ToolbarInfo::TOP)
61 else if (info.location == ToolbarSection::ToolbarInfo::BOTTOM)
63 else if (info.location == ToolbarSection::ToolbarInfo::RIGHT)
65 else if (info.location == ToolbarSection::ToolbarInfo::LEFT)
68 // use setting from default.ui
70 valid_location = false;
73 // invalid location is for a new toolbar that has no saved information,
74 // so info.visible is not used for this case.
76 // init tbinfo.flags with saved visibility,
80 if (info.state == ToolbarSection::ToolbarInfo::ON)
82 else if (info.state == ToolbarSection::ToolbarInfo::OFF)
88 std::cout << "State " << info.state << " FLAGS: " << flags
89 << " ON:" << (flags & ToolbarBackend::ON)
90 << " OFF:" << (flags & ToolbarBackend::OFF)
91 << " L:" << (flags & ToolbarBackend::LEFT)
92 << " R:" << (flags & ToolbarBackend::RIGHT)
93 << " T:" << (flags & ToolbarBackend::TOP)
94 << " B:" << (flags & ToolbarBackend::BOTTOM)
95 << " MA:" << (flags & ToolbarBackend::MATH)
96 << " RE:" << (flags & ToolbarBackend::REVIEW)
97 << " TB:" << (flags & ToolbarBackend::TABLE)
98 << " AU:" << (flags & ToolbarBackend::AUTO)
102 tbinfo.flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
106 void Toolbars::init()
108 // extracts the toolbars from the backend
109 ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
110 ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
112 // init flags will also add these toolbars to session if they
113 // are not already there (e.g. first run of lyx).
114 for (; cit != end; ++cit)
117 // add toolbars according the order in session
118 ToolbarSection::ToolbarList::const_iterator tb = LyX::ref().session().toolbars().begin();
119 ToolbarSection::ToolbarList::const_iterator te = LyX::ref().session().toolbars().end();
120 ToolbarSection::ToolbarInfo::Location last_loc = ToolbarSection::ToolbarInfo::NOTSET;
123 for (; tb != te; ++tb) {
124 LYXERR(Debug::INIT) << "Adding " << tb->get<0>() << " at position " << tb->get<1>().posx << " " << tb->get<1>().posy << endl;
125 // add toolbar break if posx or posy changes
126 bool newline = tb->get<1>().location == last_loc && (
127 // if two toolbars at the same location, assume uninitialized and add toolbar break
128 (tb->get<1>().posx == last_posx && tb->get<1>().posy == last_posy) ||
129 (last_loc == ToolbarSection::ToolbarInfo::TOP && tb->get<1>().posy != last_posy) ||
130 (last_loc == ToolbarSection::ToolbarInfo::BOTTOM && tb->get<1>().posy != last_posy) ||
131 (last_loc == ToolbarSection::ToolbarInfo::LEFT && tb->get<1>().posx != last_posx) ||
132 (last_loc == ToolbarSection::ToolbarInfo::RIGHT && tb->get<1>().posx != last_posx) );
133 // find the backend item and add
134 for (cit = toolbarbackend.begin(); cit != end; ++cit)
135 if (cit->name == tb->get<0>()) {
137 last_loc = tb->get<1>().location;
138 last_posx = tb->get<1>().posx;
139 last_posy = tb->get<1>().posy;
146 void Toolbars::display(string const & name, bool show)
148 ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
149 ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
151 for (; cit != end; ++cit) {
152 if (cit->name == name) {
153 unsigned int flags = cit->flags;
161 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
162 displayToolbar(*cit, show);
167 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
172 ToolbarInfo::Flags Toolbars::getToolbarState(string const & name)
174 ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
175 ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
177 for (; cit != end; ++cit) {
178 if (cit->name == name)
182 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
185 // return dummy for msvc
186 return ToolbarInfo::OFF;
190 void Toolbars::toggleToolbarState(string const & name, bool allowauto)
192 ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
193 ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
195 for (; cit != end; ++cit) {
196 if (cit->name == name) {
197 int flags = cit->flags;
199 if (flags & ToolbarInfo::OFF) {
203 } else if (flags & ToolbarInfo::AUTO) {
207 && ((flags & ToolbarInfo::MATH)
208 || (flags & ToolbarInfo::TABLE)
209 || (flags & ToolbarInfo::REVIEW))) {
210 // for math etc, toggle from on -> auto
214 // for others, toggle from on -> off
218 cit->flags = static_cast<ToolbarInfo::Flags>(flags);
222 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
229 void Toolbars::update(bool in_math, bool in_table, bool review)
233 // extracts the toolbars from the backend
234 ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
235 ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
237 for (; cit != end; ++cit) {
238 if (cit->flags & ToolbarInfo::ON)
239 displayToolbar(*cit, true);
240 else if (cit->flags & ToolbarInfo::OFF)
241 displayToolbar(*cit, false);
242 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::MATH))
243 displayToolbar(*cit, in_math);
244 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::TABLE))
245 displayToolbar(*cit, in_table);
246 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::REVIEW))
247 displayToolbar(*cit, review);
252 bool Toolbars::visible(string const & name) const
254 std::map<string, ToolbarPtr>::const_iterator it =
255 toolbars_.find(name);
256 if (it == toolbars_.end())
258 return it->second.get()->isVisible();
262 void Toolbars::saveToolbarInfo()
264 ToolbarSection & tb = LyX::ref().session().toolbars();
266 for (ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
267 cit != toolbarbackend.end(); ++cit) {
268 ToolbarsMap::iterator it = toolbars_.find(cit->name);
269 BOOST_ASSERT(it != toolbars_.end());
270 // get toolbar info from session.
271 ToolbarSection::ToolbarInfo & info = tb.load(cit->name);
272 if (cit->flags & ToolbarInfo::ON)
273 info.state = ToolbarSection::ToolbarInfo::ON;
274 else if (cit->flags & ToolbarInfo::OFF)
275 info.state = ToolbarSection::ToolbarInfo::OFF;
276 else if (cit->flags & ToolbarInfo::AUTO)
277 info.state = ToolbarSection::ToolbarInfo::AUTO;
278 // save other information
279 // if auto, frontend should *not* set on/off
280 it->second->saveInfo(info);
281 // maybe it is useful to update flags with real status. I do not know
283 if (!(cit->flags & ToolbarInfo::AUTO)) {
284 unsigned int flags = static_cast<unsigned int>(cit->flags);
285 flags &= ~(info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::OFF : ToolbarInfo::ON);
286 flags |= (info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::ON : ToolbarInfo::OFF);
287 if (info.state == ToolbarSection::ToolbarInfo::ON)
288 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
295 void Toolbars::setLayout(string const & layout)
298 layout_->set(layout);
302 bool Toolbars::updateLayoutList(int textclass)
304 // update the layout display
305 if (last_textclass_ != textclass) {
308 last_textclass_ = textclass;
315 void Toolbars::openLayoutList()
322 void Toolbars::clearLayoutList()
324 last_textclass_ = -1;
330 void Toolbars::add(ToolbarInfo const & tbinfo, bool newline)
332 ToolbarPtr tb_ptr = owner_.makeToolbar(tbinfo, newline);
333 toolbars_[tbinfo.name] = tb_ptr;
335 if (tbinfo.flags & ToolbarInfo::ON)
340 if (tb_ptr->layout())
341 layout_ = tb_ptr->layout();
345 void Toolbars::displayToolbar(ToolbarInfo const & tbinfo,
348 ToolbarsMap::iterator it = toolbars_.find(tbinfo.name);
349 BOOST_ASSERT(it != toolbars_.end());
352 it->second->show(true);
354 it->second->hide(true);
358 void Toolbars::update()
360 ToolbarsMap::const_iterator it = toolbars_.begin();
361 ToolbarsMap::const_iterator const end = toolbars_.end();
362 for (; it != end; ++it)
363 it->second->update();
366 lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled();
369 layout_->setEnabled(enable);
373 void layoutSelected(LyXView & lv, string const & name)
375 TextClass const & tc = lv.buffer()->params().getTextClass();
377 TextClass::const_iterator it = tc.begin();
378 TextClass::const_iterator const end = tc.end();
379 for (; it != end; ++it) {
380 string const & itname = (*it)->name();
381 // Yes, the lyx::to_utf8(_()) is correct
382 if (lyx::to_utf8(_(itname)) == name) {
383 FuncRequest const func(LFUN_LAYOUT, itname,
384 FuncRequest::TOOLBAR);
389 lyxerr << "ERROR (layoutSelected): layout not found!"