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 "lyxtextclass.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;
162 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
163 displayToolbar(*cit, show);
168 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
173 ToolbarInfo::Flags Toolbars::getToolbarState(string const & name)
175 ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
176 ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
178 for (; cit != end; ++cit) {
179 if (cit->name == name)
183 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
186 // return dummy for msvc
187 return ToolbarInfo::OFF;
191 void Toolbars::toggleToolbarState(string const & name)
193 ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
194 ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
196 for (; cit != end; ++cit) {
197 if (cit->name == name) {
198 int flags = cit->flags;
200 if (flags & ToolbarInfo::OFF) {
204 } else if (flags & ToolbarInfo::AUTO) {
207 } else if ((flags & ToolbarInfo::MATH) || (flags & ToolbarInfo::TABLE)
208 || (flags & ToolbarInfo::REVIEW)) {
209 // for math etc, toggle from on -> auto
213 // for others, toggle from on -> off
217 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
221 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
228 void Toolbars::update(bool in_math, bool in_table, bool review)
232 // extracts the toolbars from the backend
233 ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
234 ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
236 for (; cit != end; ++cit) {
237 if (cit->flags & ToolbarInfo::ON)
238 displayToolbar(*cit, true);
239 else if (cit->flags & ToolbarInfo::OFF)
240 displayToolbar(*cit, false);
241 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::MATH))
242 displayToolbar(*cit, in_math);
243 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::TABLE))
244 displayToolbar(*cit, in_table);
245 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::REVIEW))
246 displayToolbar(*cit, review);
251 void Toolbars::saveToolbarInfo()
253 ToolbarSection & tb = LyX::ref().session().toolbars();
255 for (ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
256 cit != toolbarbackend.end(); ++cit) {
257 ToolbarsMap::iterator it = toolbars_.find(cit->name);
258 BOOST_ASSERT(it != toolbars_.end());
259 // get toolbar info from session.
260 ToolbarSection::ToolbarInfo & info = tb.load(cit->name);
261 if (cit->flags & ToolbarInfo::ON)
262 info.state = ToolbarSection::ToolbarInfo::ON;
263 else if (cit->flags & ToolbarInfo::OFF)
264 info.state = ToolbarSection::ToolbarInfo::OFF;
265 else if (cit->flags & ToolbarInfo::AUTO)
266 info.state = ToolbarSection::ToolbarInfo::AUTO;
267 // save other information
268 // if auto, frontend should *not* set on/off
269 it->second->saveInfo(info);
270 // maybe it is useful to update flags with real status. I do not know
272 if (!(cit->flags & ToolbarInfo::AUTO)) {
273 unsigned int flags = static_cast<unsigned int>(cit->flags);
274 flags &= ~(info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::OFF : ToolbarInfo::ON);
275 flags |= (info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::ON : ToolbarInfo::OFF);
276 if (info.state == ToolbarSection::ToolbarInfo::ON)
277 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
284 void Toolbars::setLayout(string const & layout)
287 layout_->set(layout);
291 bool Toolbars::updateLayoutList(int textclass)
293 // update the layout display
294 if (last_textclass_ != textclass) {
297 last_textclass_ = textclass;
304 void Toolbars::openLayoutList()
311 void Toolbars::clearLayoutList()
313 last_textclass_ = -1;
319 void Toolbars::add(ToolbarInfo const & tbinfo, bool newline)
321 ToolbarPtr tb_ptr = owner_.makeToolbar(tbinfo, newline);
322 toolbars_[tbinfo.name] = tb_ptr;
324 if (tbinfo.flags & ToolbarInfo::ON)
329 if (tb_ptr->layout())
330 layout_ = tb_ptr->layout();
334 void Toolbars::displayToolbar(ToolbarInfo const & tbinfo,
337 ToolbarsMap::iterator it = toolbars_.find(tbinfo.name);
338 BOOST_ASSERT(it != toolbars_.end());
341 it->second->show(true);
343 it->second->hide(true);
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, string const & name)
364 LyXTextClass const & tc = lv.buffer()->params().getLyXTextClass();
366 LyXTextClass::const_iterator it = tc.begin();
367 LyXTextClass::const_iterator const end = tc.end();
368 for (; it != end; ++it) {
369 string const & itname = (*it)->name();
370 // Yes, the lyx::to_utf8(_()) is correct
371 if (lyx::to_utf8(_(itname)) == name) {
372 FuncRequest const func(LFUN_LAYOUT, itname,
373 FuncRequest::TOOLBAR);
378 lyxerr << "ERROR (layoutSelected): layout not found!"