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"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
28 #include "ToolbarBackend.h"
30 #include "support/debug.h"
31 #include "support/gettext.h"
38 #define TurnOnFlag(x) flags |= ToolbarInfo::x
39 #define TurnOffFlag(x) flags &= ~ToolbarInfo::x
41 GuiToolbars::GuiToolbars(GuiView & owner)
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 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 ToolbarsMap::const_iterator it = toolbars_.begin();
114 for (; it != toolbars_.end(); ++it)
118 // extracts the toolbars from the backend
119 ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
120 ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
122 // init flags will also add these toolbars to session if they
123 // are not already there (e.g. first run of lyx).
124 for (; cit != end; ++cit)
127 // add toolbars according the order in session
128 ToolbarSection::ToolbarList::const_iterator tb =
129 LyX::ref().session().toolbars().begin();
130 ToolbarSection::ToolbarList::const_iterator te =
131 LyX::ref().session().toolbars().end();
132 ToolbarSection::ToolbarInfo::Location last_loc =
133 ToolbarSection::ToolbarInfo::NOTSET;
136 for (; tb != te; ++tb) {
137 LYXERR(Debug::INIT, "Adding " << tb->key << " at position "
138 << tb->info.posx << " " << tb->info.posy);
139 // add toolbar break if posx or posy changes
140 bool newline = tb->info.location == last_loc && (
141 // if two toolbars at the same location, assume uninitialized and add toolbar break
142 (tb->info.posx == last_posx && tb->info.posy == last_posy) ||
143 (last_loc == ToolbarSection::ToolbarInfo::TOP && tb->info.posy != last_posy) ||
144 (last_loc == ToolbarSection::ToolbarInfo::BOTTOM && tb->info.posy != last_posy) ||
145 (last_loc == ToolbarSection::ToolbarInfo::LEFT && tb->info.posx != last_posx) ||
146 (last_loc == ToolbarSection::ToolbarInfo::RIGHT && tb->info.posx != last_posx) );
147 // find the backend item and add
148 for (cit = toolbarbackend.begin(); cit != end; ++cit)
149 if (cit->name == tb->key) {
151 last_loc = tb->info.location;
152 last_posx = tb->info.posx;
153 last_posy = tb->info.posy;
160 void GuiToolbars::display(string const & name, bool show)
162 ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
163 ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
165 for (; cit != end; ++cit) {
166 if (cit->name == name) {
167 unsigned int flags = cit->flags;
175 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
176 displayToolbar(*cit, show);
180 LYXERR(Debug::GUI, "Toolbar::display: no toolbar named " << name);
184 ToolbarInfo * GuiToolbars::getToolbarInfo(string const & name)
186 return toolbarbackend.getUsedToolbarInfo(name);
190 void GuiToolbars::toggleToolbarState(string const & name, bool allowauto)
192 ToolbarInfo * tbi = toolbarbackend.getUsedToolbarInfo(name);
195 LYXERR(Debug::GUI, "Toolbar::display: no toolbar named " << name);
199 int flags = tbi->flags;
201 if (flags & ToolbarInfo::OFF) {
205 } else if (flags & ToolbarInfo::AUTO) {
209 && ((flags & ToolbarInfo::MATH)
210 || (flags & ToolbarInfo::TABLE)
211 || (flags & ToolbarInfo::REVIEW)
212 || (flags & ToolbarInfo::MATHMACROTEMPLATE))) {
213 // for math etc, toggle from on -> auto
217 // for others, toggle from on -> off
221 tbi->flags = static_cast<ToolbarInfo::Flags>(flags);
225 void GuiToolbars::toggleFullScreen(bool start_full_screen)
227 // extracts the toolbars from the backend
228 ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
229 ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
232 for (; cit != end; ++cit) {
234 if (start_full_screen) {
235 flags = cit->before_fullscreen = cit->flags;
240 flags = cit->before_fullscreen;
242 cit->flags = static_cast<ToolbarInfo::Flags>(flags);
249 void GuiToolbars::update(bool in_math, bool in_table, bool review,
250 bool in_mathmacrotemplate)
254 // extracts the toolbars from the backend
255 ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
256 ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
258 for (; cit != end; ++cit) {
259 if (cit->flags & ToolbarInfo::ON)
260 displayToolbar(*cit, true);
261 else if (cit->flags & ToolbarInfo::OFF)
262 displayToolbar(*cit, false);
263 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::MATH))
264 displayToolbar(*cit, in_math);
265 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::TABLE))
266 displayToolbar(*cit, in_table);
267 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::REVIEW))
268 displayToolbar(*cit, review);
269 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::MATHMACROTEMPLATE))
270 displayToolbar(*cit, in_mathmacrotemplate);
275 bool GuiToolbars::visible(string const & name) const
277 map<string, GuiToolbar *>::const_iterator it =
278 toolbars_.find(name);
279 if (it == toolbars_.end())
281 return it->second->isVisible();
285 void GuiToolbars::saveToolbarInfo()
287 ToolbarSection & tb = LyX::ref().session().toolbars();
289 for (ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
290 cit != toolbarbackend.end(); ++cit) {
291 ToolbarsMap::iterator it = toolbars_.find(cit->name);
292 BOOST_ASSERT(it != toolbars_.end());
293 // get toolbar info from session.
294 ToolbarSection::ToolbarInfo & info = tb.load(cit->name);
295 if (cit->flags & ToolbarInfo::ON)
296 info.state = ToolbarSection::ToolbarInfo::ON;
297 else if (cit->flags & ToolbarInfo::OFF)
298 info.state = ToolbarSection::ToolbarInfo::OFF;
299 else if (cit->flags & ToolbarInfo::AUTO)
300 info.state = ToolbarSection::ToolbarInfo::AUTO;
301 // save other information
302 // if auto, frontend should *not* set on/off
303 it->second->saveInfo(info);
304 // maybe it is useful to update flags with real status. I do not know
306 if (!(cit->flags & ToolbarInfo::AUTO)) {
307 unsigned int flags = static_cast<unsigned int>(cit->flags);
308 flags &= ~(info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::OFF : ToolbarInfo::ON);
309 flags |= (info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::ON : ToolbarInfo::OFF);
310 if (info.state == ToolbarSection::ToolbarInfo::ON)
311 cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
318 void GuiToolbars::add(ToolbarInfo const & tbinfo, bool newline)
320 GuiToolbar * tb_ptr = owner_.makeToolbar(tbinfo, newline);
321 toolbars_[tbinfo.name] = tb_ptr;
323 if (tbinfo.flags & ToolbarInfo::ON)
330 void GuiToolbars::displayToolbar(ToolbarInfo const & tbinfo,
333 ToolbarsMap::iterator it = toolbars_.find(tbinfo.name);
334 BOOST_ASSERT(it != toolbars_.end());
337 if (it->second->isVisible())
341 else if (it->second->isVisible())
346 void GuiToolbars::updateIcons()
348 ToolbarsMap::const_iterator it = toolbars_.begin();
349 ToolbarsMap::const_iterator const end = toolbars_.end();
350 for (; it != end; ++it)
351 it->second->updateContents();
355 void GuiToolbars::showCommandBuffer(bool show_it)
357 ToolbarsMap::const_iterator it = toolbars_.begin();
358 ToolbarsMap::const_iterator const end = toolbars_.end();
359 for (; it != end; ++it) {
360 GuiCommandBuffer * cb = it->second->commandBuffer();
364 // FIXME: this is a hack, "minibuffer" should not be
366 display("minibuffer", false);
369 if (!it->second->isVisible())
370 display("minibuffer", true);
376 } // namespace frontend