]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiToolbars.cpp
mainly reduce include dependencies
[lyx.git] / src / frontends / qt4 / GuiToolbars.cpp
1 /**
2  * \file GuiToolbars.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Angus Leeming
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiToolbars.h"
16
17 #include "GuiCommandBuffer.h"
18 #include "GuiToolbar.h"
19 #include "GuiView.h"
20
21 #include "Buffer.h"
22 #include "BufferParams.h"
23 #include "debug.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "gettext.h"
27 #include "Layout.h"
28 #include "LyX.h"
29 #include "LyXFunc.h"
30 #include "TextClass.h"
31 #include "ToolbarBackend.h"
32
33
34 using std::endl;
35 using std::string;
36
37 namespace lyx {
38 namespace frontend {
39
40 #define TurnOnFlag(x)   flags |= ToolbarInfo::x
41 #define TurnOffFlag(x)  flags &= ~ToolbarInfo::x
42
43 GuiToolbars::GuiToolbars(GuiViewBase & owner)
44         : owner_(owner),
45           layout_(0),
46           last_textclass_(TextClassPtr())
47 {}
48
49
50 void GuiToolbars::initFlags(ToolbarInfo & tbinfo)
51 {
52         ToolbarSection::ToolbarInfo & info = LyX::ref().session().toolbars().load(tbinfo.name);
53
54         unsigned int flags = static_cast<unsigned int>(tbinfo.flags);
55
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;
59         TurnOffFlag(TOP);
60         TurnOffFlag(BOTTOM);
61         TurnOffFlag(RIGHT);
62         TurnOffFlag(LEFT);
63
64         bool valid_location = true;
65         // init tbinfo.flags with saved location
66         if (info.location == ToolbarSection::ToolbarInfo::TOP)
67                 TurnOnFlag(TOP);
68         else if (info.location == ToolbarSection::ToolbarInfo::BOTTOM)
69                 TurnOnFlag(BOTTOM);
70         else if (info.location == ToolbarSection::ToolbarInfo::RIGHT)
71                 TurnOnFlag(RIGHT);
72         else if (info.location == ToolbarSection::ToolbarInfo::LEFT)
73                 TurnOnFlag(LEFT);
74         else {
75                 // use setting from default.ui
76                 flags = save;
77                 valid_location = false;
78         }
79
80         // invalid location is for a new toolbar that has no saved information,
81         // so info.visible is not used for this case.
82         if (valid_location) {
83                 // init tbinfo.flags with saved visibility,
84                 TurnOffFlag(ON);
85                 TurnOffFlag(OFF);
86                 TurnOffFlag(AUTO);
87                 if (info.state == ToolbarSection::ToolbarInfo::ON)
88                         TurnOnFlag(ON);
89                 else if (info.state == ToolbarSection::ToolbarInfo::OFF)
90                         TurnOnFlag(OFF);
91                 else
92                         TurnOnFlag(AUTO);
93         }
94         /*
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)
106                 << std::endl;
107         */
108         // now set the flags
109         tbinfo.flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
110 }
111
112
113 void GuiToolbars::init()
114 {
115         // extracts the toolbars from the backend
116         ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
117         ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
118
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)
122                 initFlags(*cit);
123
124         // add toolbars according the order in session
125         ToolbarSection::ToolbarList::const_iterator tb = LyX::ref().session().toolbars().begin();
126         ToolbarSection::ToolbarList::const_iterator te = LyX::ref().session().toolbars().end();
127         ToolbarSection::ToolbarInfo::Location last_loc = ToolbarSection::ToolbarInfo::NOTSET;
128         int last_posx = 0;
129         int last_posy = 0;
130         for (; tb != te; ++tb) {
131                 LYXERR(Debug::INIT) << "Adding " << tb->get<0>() << " at position " << tb->get<1>().posx << " " << tb->get<1>().posy << endl;
132                 // add toolbar break if posx or posy changes
133                 bool newline = tb->get<1>().location == last_loc && (
134                         // if two toolbars at the same location, assume uninitialized and add toolbar break
135                         (tb->get<1>().posx == last_posx && tb->get<1>().posy == last_posy) ||
136                         (last_loc == ToolbarSection::ToolbarInfo::TOP && tb->get<1>().posy != last_posy) ||
137                         (last_loc == ToolbarSection::ToolbarInfo::BOTTOM && tb->get<1>().posy != last_posy) ||
138                         (last_loc == ToolbarSection::ToolbarInfo::LEFT && tb->get<1>().posx != last_posx) ||
139                         (last_loc == ToolbarSection::ToolbarInfo::RIGHT && tb->get<1>().posx != last_posx) );
140                 // find the backend item and add
141                 for (cit = toolbarbackend.begin(); cit != end; ++cit)
142                         if (cit->name == tb->get<0>()) {
143                                 add(*cit, newline);
144                                 last_loc = tb->get<1>().location;
145                                 last_posx = tb->get<1>().posx;
146                                 last_posy = tb->get<1>().posy;
147                                 break;
148                         }
149         }
150 }
151
152
153 void GuiToolbars::display(string const & name, bool show)
154 {
155         ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
156         ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
157
158         for (; cit != end; ++cit) {
159                 if (cit->name == name) {
160                         unsigned int flags = cit->flags;
161                         TurnOffFlag(ON);
162                         TurnOffFlag(OFF);
163                         TurnOffFlag(AUTO);
164                         if (show)
165                                 TurnOnFlag(ON);
166                         else
167                                 TurnOnFlag(OFF);
168                         cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
169                         displayToolbar(*cit, show);
170                 }
171         }
172
173         LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
174                 << name << endl;
175 }
176
177
178 ToolbarInfo * GuiToolbars::getToolbarInfo(string const & name)
179 {
180         return toolbarbackend.getUsedToolbarInfo(name);
181 }
182
183
184 void GuiToolbars::toggleToolbarState(string const & name, bool allowauto)
185 {
186         ToolbarInfo * tbi = toolbarbackend.getUsedToolbarInfo(name);
187
188         if (!tbi) {
189                 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
190                         << name << endl;
191                 return;
192         }
193
194         int flags = tbi->flags;
195         // off -> on
196         if (flags & ToolbarInfo::OFF) {
197                 TurnOffFlag(OFF);
198                 TurnOnFlag(ON);
199         // auto -> off
200         } else if (flags & ToolbarInfo::AUTO) {
201                 TurnOffFlag(AUTO);
202                 TurnOnFlag(OFF);
203         } else if (allowauto 
204                    && ((flags & ToolbarInfo::MATH) 
205                        || (flags & ToolbarInfo::TABLE)
206                        || (flags & ToolbarInfo::REVIEW))) {
207                 // for math etc, toggle from on -> auto
208                 TurnOffFlag(ON);
209                 TurnOnFlag(AUTO);
210         } else {
211                 // for others, toggle from on -> off
212                 TurnOffFlag(ON);
213                 TurnOnFlag(OFF);
214         }
215         tbi->flags = static_cast<ToolbarInfo::Flags>(flags);
216 }
217 #undef TurnOnFlag
218 #undef TurnOffFlag
219
220
221 void GuiToolbars::update(bool in_math, bool in_table, bool review)
222 {
223         updateIcons();
224
225         // extracts the toolbars from the backend
226         ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
227         ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
228
229         for (; cit != end; ++cit) {
230                 if (cit->flags & ToolbarInfo::ON)
231                         displayToolbar(*cit, true);
232                 else if (cit->flags & ToolbarInfo::OFF)
233                         displayToolbar(*cit, false);
234                 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::MATH))
235                         displayToolbar(*cit, in_math);
236                 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::TABLE))
237                         displayToolbar(*cit, in_table);
238                 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::REVIEW))
239                         displayToolbar(*cit, review);
240         }
241 }
242
243
244 bool GuiToolbars::visible(string const & name) const
245 {
246         std::map<string, GuiToolbar *>::const_iterator it =
247                 toolbars_.find(name);
248         if (it == toolbars_.end())
249                 return false;
250         return it->second->isVisible();
251 }
252
253
254 void GuiToolbars::saveToolbarInfo()
255 {
256         ToolbarSection & tb = LyX::ref().session().toolbars();
257
258         for (ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
259                 cit != toolbarbackend.end(); ++cit) {
260                 ToolbarsMap::iterator it = toolbars_.find(cit->name);
261                 BOOST_ASSERT(it != toolbars_.end());
262                 // get toolbar info from session.
263                 ToolbarSection::ToolbarInfo & info = tb.load(cit->name);
264                 if (cit->flags & ToolbarInfo::ON)
265                         info.state = ToolbarSection::ToolbarInfo::ON;
266                 else if (cit->flags & ToolbarInfo::OFF)
267                         info.state = ToolbarSection::ToolbarInfo::OFF;
268                 else if (cit->flags & ToolbarInfo::AUTO)
269                         info.state = ToolbarSection::ToolbarInfo::AUTO;
270                 // save other information
271                 // if auto, frontend should *not* set on/off
272                 it->second->saveInfo(info);
273                 // maybe it is useful to update flags with real status. I do not know
274                 /*
275                 if (!(cit->flags & ToolbarInfo::AUTO)) {
276                         unsigned int flags = static_cast<unsigned int>(cit->flags);
277                         flags &= ~(info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::OFF : ToolbarInfo::ON);
278                         flags |= (info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::ON : ToolbarInfo::OFF);
279                         if (info.state == ToolbarSection::ToolbarInfo::ON)
280                         cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
281                 }
282                 */
283         }
284 }
285
286
287 void GuiToolbars::setLayout(docstring const & layout)
288 {
289         if (layout_)
290                 layout_->set(layout);
291 }
292
293
294 bool GuiToolbars::updateLayoutList(TextClassPtr textclass)
295 {
296         // update the layout display
297         if (last_textclass_ != textclass) {
298                 if (layout_)
299                         layout_->updateContents();
300                 last_textclass_ = textclass;
301                 return true;
302         } else
303                 return false;
304 }
305
306
307 void GuiToolbars::openLayoutList()
308 {
309         if (layout_)
310                 layout_->showPopup();
311 }
312
313
314 void GuiToolbars::clearLayoutList()
315 {
316         last_textclass_ = TextClassPtr();
317         if (layout_)
318                 layout_->clear();
319 }
320
321
322 void GuiToolbars::add(ToolbarInfo const & tbinfo, bool newline)
323 {
324         GuiToolbar * tb_ptr = owner_.makeToolbar(tbinfo, newline);
325         toolbars_[tbinfo.name] = tb_ptr;
326
327         if (tbinfo.flags & ToolbarInfo::ON)
328                 tb_ptr->show();
329         else
330                 tb_ptr->hide();
331
332         if (tb_ptr->layout())
333                 layout_ = tb_ptr->layout();
334 }
335
336
337 void GuiToolbars::displayToolbar(ToolbarInfo const & tbinfo,
338                               bool show_it)
339 {
340         ToolbarsMap::iterator it = toolbars_.find(tbinfo.name);
341         BOOST_ASSERT(it != toolbars_.end());
342
343         if (show_it) {
344                 if (it->second->isVisible())
345                         return;
346                 it->second->show();
347         }
348         else if (it->second->isVisible())
349                 it->second->hide();
350 }
351
352
353 void GuiToolbars::updateIcons()
354 {
355         ToolbarsMap::const_iterator it = toolbars_.begin();
356         ToolbarsMap::const_iterator const end = toolbars_.end();
357         for (; it != end; ++it)
358                 it->second->updateContents();
359
360         bool const enable =
361                 lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled();
362
363         if (layout_)
364                 layout_->setEnabled(enable);
365 }
366
367
368 void GuiToolbars::showCommandBuffer(bool show_it)
369 {
370         ToolbarsMap::const_iterator it = toolbars_.begin();
371         ToolbarsMap::const_iterator const end = toolbars_.end();
372         for (; it != end; ++it) {
373                 GuiCommandBuffer * cb = it->second->commandBuffer();
374                 if (!cb)
375                         continue;
376                 if (!show_it) {
377                         it->second->hide();
378                         return;
379                 }
380                 if (!it->second->isVisible())
381                         it->second->show();
382                 cb->setFocus();
383                 return;
384         }
385 }
386
387 } // namespace frontend
388 } // namespace lyx