]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiToolbars.cpp
reduce line noise
[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(GuiView & 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 =
126                 LyX::ref().session().toolbars().begin();
127         ToolbarSection::ToolbarList::const_iterator te =
128                 LyX::ref().session().toolbars().end();
129         ToolbarSection::ToolbarInfo::Location last_loc =
130                 ToolbarSection::ToolbarInfo::NOTSET;
131         int last_posx = 0;
132         int last_posy = 0;
133         for (; tb != te; ++tb) {
134                 LYXERR(Debug::INIT, "Adding " << tb->key << " at position "
135                         << tb->info.posx << " " << tb->info.posy);
136                 // add toolbar break if posx or posy changes
137                 bool newline = tb->info.location == last_loc && (
138                         // if two toolbars at the same location, assume uninitialized and add toolbar break
139                         (tb->info.posx == last_posx && tb->info.posy == last_posy) ||
140                         (last_loc == ToolbarSection::ToolbarInfo::TOP && tb->info.posy != last_posy) ||
141                         (last_loc == ToolbarSection::ToolbarInfo::BOTTOM && tb->info.posy != last_posy) ||
142                         (last_loc == ToolbarSection::ToolbarInfo::LEFT && tb->info.posx != last_posx) ||
143                         (last_loc == ToolbarSection::ToolbarInfo::RIGHT && tb->info.posx != last_posx) );
144                 // find the backend item and add
145                 for (cit = toolbarbackend.begin(); cit != end; ++cit)
146                         if (cit->name == tb->key) {
147                                 add(*cit, newline);
148                                 last_loc = tb->info.location;
149                                 last_posx = tb->info.posx;
150                                 last_posy = tb->info.posy;
151                                 break;
152                         }
153         }
154 }
155
156
157 void GuiToolbars::display(string const & name, bool show)
158 {
159         ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
160         ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
161
162         for (; cit != end; ++cit) {
163                 if (cit->name == name) {
164                         unsigned int flags = cit->flags;
165                         TurnOffFlag(ON);
166                         TurnOffFlag(OFF);
167                         TurnOffFlag(AUTO);
168                         if (show)
169                                 TurnOnFlag(ON);
170                         else
171                                 TurnOnFlag(OFF);
172                         cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
173                         displayToolbar(*cit, show);
174                 }
175         }
176
177         LYXERR(Debug::GUI, "Toolbar::display: no toolbar named " << name);
178 }
179
180
181 ToolbarInfo * GuiToolbars::getToolbarInfo(string const & name)
182 {
183         return toolbarbackend.getUsedToolbarInfo(name);
184 }
185
186
187 void GuiToolbars::toggleToolbarState(string const & name, bool allowauto)
188 {
189         ToolbarInfo * tbi = toolbarbackend.getUsedToolbarInfo(name);
190
191         if (!tbi) {
192                 LYXERR(Debug::GUI, "Toolbar::display: no toolbar named " << name);
193                 return;
194         }
195
196         int flags = tbi->flags;
197         // off -> on
198         if (flags & ToolbarInfo::OFF) {
199                 TurnOffFlag(OFF);
200                 TurnOnFlag(ON);
201         // auto -> off
202         } else if (flags & ToolbarInfo::AUTO) {
203                 TurnOffFlag(AUTO);
204                 TurnOnFlag(OFF);
205         } else if (allowauto 
206                    && ((flags & ToolbarInfo::MATH) 
207                        || (flags & ToolbarInfo::TABLE)
208                        || (flags & ToolbarInfo::REVIEW))) {
209                 // for math etc, toggle from on -> auto
210                 TurnOffFlag(ON);
211                 TurnOnFlag(AUTO);
212         } else {
213                 // for others, toggle from on -> off
214                 TurnOffFlag(ON);
215                 TurnOnFlag(OFF);
216         }
217         tbi->flags = static_cast<ToolbarInfo::Flags>(flags);
218 }
219 #undef TurnOnFlag
220 #undef TurnOffFlag
221
222
223 void GuiToolbars::update(bool in_math, bool in_table, bool review)
224 {
225         updateIcons();
226
227         // extracts the toolbars from the backend
228         ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
229         ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
230
231         for (; cit != end; ++cit) {
232                 if (cit->flags & ToolbarInfo::ON)
233                         displayToolbar(*cit, true);
234                 else if (cit->flags & ToolbarInfo::OFF)
235                         displayToolbar(*cit, false);
236                 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::MATH))
237                         displayToolbar(*cit, in_math);
238                 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::TABLE))
239                         displayToolbar(*cit, in_table);
240                 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::REVIEW))
241                         displayToolbar(*cit, review);
242         }
243 }
244
245
246 bool GuiToolbars::visible(string const & name) const
247 {
248         std::map<string, GuiToolbar *>::const_iterator it =
249                 toolbars_.find(name);
250         if (it == toolbars_.end())
251                 return false;
252         return it->second->isVisible();
253 }
254
255
256 void GuiToolbars::saveToolbarInfo()
257 {
258         ToolbarSection & tb = LyX::ref().session().toolbars();
259
260         for (ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
261                 cit != toolbarbackend.end(); ++cit) {
262                 ToolbarsMap::iterator it = toolbars_.find(cit->name);
263                 BOOST_ASSERT(it != toolbars_.end());
264                 // get toolbar info from session.
265                 ToolbarSection::ToolbarInfo & info = tb.load(cit->name);
266                 if (cit->flags & ToolbarInfo::ON)
267                         info.state = ToolbarSection::ToolbarInfo::ON;
268                 else if (cit->flags & ToolbarInfo::OFF)
269                         info.state = ToolbarSection::ToolbarInfo::OFF;
270                 else if (cit->flags & ToolbarInfo::AUTO)
271                         info.state = ToolbarSection::ToolbarInfo::AUTO;
272                 // save other information
273                 // if auto, frontend should *not* set on/off
274                 it->second->saveInfo(info);
275                 // maybe it is useful to update flags with real status. I do not know
276                 /*
277                 if (!(cit->flags & ToolbarInfo::AUTO)) {
278                         unsigned int flags = static_cast<unsigned int>(cit->flags);
279                         flags &= ~(info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::OFF : ToolbarInfo::ON);
280                         flags |= (info.state == ToolbarSection::ToolbarInfo::ON ? ToolbarInfo::ON : ToolbarInfo::OFF);
281                         if (info.state == ToolbarSection::ToolbarInfo::ON)
282                         cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
283                 }
284                 */
285         }
286 }
287
288
289 void GuiToolbars::setLayout(docstring const & layout)
290 {
291         if (layout_)
292                 layout_->set(layout);
293 }
294
295
296 bool GuiToolbars::updateLayoutList(TextClassPtr textclass, bool force)
297 {
298         // update the layout display
299         if (last_textclass_ != textclass || force) {
300                 if (layout_)
301                         layout_->updateContents();
302                 last_textclass_ = textclass;
303                 return true;
304         } else
305                 return false;
306 }
307
308
309 void GuiToolbars::openLayoutList()
310 {
311         if (layout_)
312                 layout_->showPopup();
313 }
314
315
316 void GuiToolbars::clearLayoutList()
317 {
318         last_textclass_ = TextClassPtr();
319         if (layout_)
320                 layout_->clear();
321 }
322
323
324 void GuiToolbars::add(ToolbarInfo const & tbinfo, bool newline)
325 {
326         GuiToolbar * tb_ptr = owner_.makeToolbar(tbinfo, newline);
327         toolbars_[tbinfo.name] = tb_ptr;
328
329         if (tbinfo.flags & ToolbarInfo::ON)
330                 tb_ptr->show();
331         else
332                 tb_ptr->hide();
333
334         if (tb_ptr->layout())
335                 layout_ = tb_ptr->layout();
336 }
337
338
339 void GuiToolbars::displayToolbar(ToolbarInfo const & tbinfo,
340                               bool show_it)
341 {
342         ToolbarsMap::iterator it = toolbars_.find(tbinfo.name);
343         BOOST_ASSERT(it != toolbars_.end());
344
345         if (show_it) {
346                 if (it->second->isVisible())
347                         return;
348                 it->second->show();
349         }
350         else if (it->second->isVisible())
351                 it->second->hide();
352 }
353
354
355 void GuiToolbars::updateIcons()
356 {
357         ToolbarsMap::const_iterator it = toolbars_.begin();
358         ToolbarsMap::const_iterator const end = toolbars_.end();
359         for (; it != end; ++it)
360                 it->second->updateContents();
361
362         bool const enable =
363                 lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled();
364
365         if (layout_)
366                 layout_->setEnabled(enable);
367 }
368
369
370 void GuiToolbars::showCommandBuffer(bool show_it)
371 {
372         ToolbarsMap::const_iterator it = toolbars_.begin();
373         ToolbarsMap::const_iterator const end = toolbars_.end();
374         for (; it != end; ++it) {
375                 GuiCommandBuffer * cb = it->second->commandBuffer();
376                 if (!cb)
377                         continue;
378                 if (!show_it) {
379                         // FIXME: this is a hack, "minibuffer" should not be
380                         // hardcoded.
381                         display("minibuffer", false);
382                         return;
383                 }
384                 if (!it->second->isVisible())
385                         display("minibuffer", true);
386                 cb->setFocus();
387                 return;
388         }
389 }
390
391 } // namespace frontend
392 } // namespace lyx