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)
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) {
206 } else if ((flags & ToolbarInfo::MATH) || (flags & ToolbarInfo::TABLE)
207 || (flags & ToolbarInfo::REVIEW)) {
208 // for math etc, toggle from on -> auto
212 // for others, toggle from on -> off
216 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
220 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
227 void Toolbars::update(bool in_math, bool in_table, bool review)
231 // extracts the toolbars from the backend
232 ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
233 ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
235 for (; cit != end; ++cit) {
236 if (cit->flags & ToolbarInfo::ON)
237 displayToolbar(*cit, true);
238 else if (cit->flags & ToolbarInfo::OFF)
239 displayToolbar(*cit, false);
240 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::MATH))
241 displayToolbar(*cit, in_math);
242 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::TABLE))
243 displayToolbar(*cit, in_table);
244 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::REVIEW))
245 displayToolbar(*cit, review);
250 bool Toolbars::visible(string const & name) const
252 std::map<string, ToolbarPtr>::const_iterator it =
253 toolbars_.find(name);
254 if (it == toolbars_.end())
256 return it->second.get()->isVisible();
260 void Toolbars::saveToolbarInfo()
262 ToolbarSection & tb = LyX::ref().session().toolbars();
264 for (ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
265 cit != toolbarbackend.end(); ++cit) {
266 ToolbarsMap::iterator it = toolbars_.find(cit->name);
267 BOOST_ASSERT(it != toolbars_.end());
268 // get toolbar info from session.
269 ToolbarSection::ToolbarInfo & info = tb.load(cit->name);
270 if (cit->flags & ToolbarInfo::ON)
271 info.state = ToolbarSection::ToolbarInfo::ON;
272 else if (cit->flags & ToolbarInfo::OFF)
273 info.state = ToolbarSection::ToolbarInfo::OFF;
274 else if (cit->flags & ToolbarInfo::AUTO)
275 info.state = ToolbarSection::ToolbarInfo::AUTO;
276 // save other information
277 // if auto, frontend should *not* set on/off
278 it->second->saveInfo(info);
279 // maybe it is useful to update flags with real status. I do not know
281 if (!(cit->flags & ToolbarInfo::AUTO)) {
282 unsigned int flags = static_cast<unsigned int>(cit->flags);
283 flags &= ~(info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::OFF : ToolbarInfo::ON);
284 flags |= (info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::ON : ToolbarInfo::OFF);
285 if (info.state == ToolbarSection::ToolbarInfo::ON)
286 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
293 void Toolbars::setLayout(string const & layout)
296 layout_->set(layout);
300 bool Toolbars::updateLayoutList(int textclass)
302 // update the layout display
303 if (last_textclass_ != textclass) {
306 last_textclass_ = textclass;
313 void Toolbars::openLayoutList()
320 void Toolbars::clearLayoutList()
322 last_textclass_ = -1;
328 void Toolbars::add(ToolbarInfo const & tbinfo, bool newline)
330 ToolbarPtr tb_ptr = owner_.makeToolbar(tbinfo, newline);
331 toolbars_[tbinfo.name] = tb_ptr;
333 if (tbinfo.flags & ToolbarInfo::ON)
338 if (tb_ptr->layout())
339 layout_ = tb_ptr->layout();
343 void Toolbars::displayToolbar(ToolbarInfo const & tbinfo,
346 ToolbarsMap::iterator it = toolbars_.find(tbinfo.name);
347 BOOST_ASSERT(it != toolbars_.end());
350 it->second->show(true);
352 it->second->hide(true);
356 void Toolbars::update()
358 ToolbarsMap::const_iterator it = toolbars_.begin();
359 ToolbarsMap::const_iterator const end = toolbars_.end();
360 for (; it != end; ++it)
361 it->second->update();
364 lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled();
367 layout_->setEnabled(enable);
371 void layoutSelected(LyXView & lv, string const & name)
373 TextClass const & tc = lv.buffer()->params().getTextClass();
375 TextClass::const_iterator it = tc.begin();
376 TextClass::const_iterator const end = tc.end();
377 for (; it != end; ++it) {
378 string const & itname = (*it)->name();
379 // Yes, the lyx::to_utf8(_()) is correct
380 if (lyx::to_utf8(_(itname)) == name) {
381 FuncRequest const func(LFUN_LAYOUT, itname,
382 FuncRequest::TOOLBAR);
387 lyxerr << "ERROR (layoutSelected): layout not found!"