]> git.lyx.org Git - features.git/blob - src/frontends/Toolbars.cpp
329a8425226facad66cd2741e49a8d67308bc0f4
[features.git] / src / frontends / Toolbars.cpp
1 /**
2  * \file Toolbars.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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "frontends/Toolbars.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "debug.h"
19 #include "FuncRequest.h"
20 #include "FuncStatus.h"
21 #include "gettext.h"
22 #include "Layout.h"
23 #include "LyX.h"
24 #include "LyXFunc.h"
25 #include "TextClass.h"
26
27
28 using std::endl;
29 using std::string;
30
31 namespace lyx {
32 namespace frontend {
33
34 Toolbars::Toolbars()
35 {}
36
37 #define TurnOnFlag(x)   flags |= ToolbarInfo::x
38 #define TurnOffFlag(x)  flags &= ~ToolbarInfo::x
39
40 void Toolbars::initFlags(ToolbarInfo & tbinfo)
41 {
42         ToolbarSection::ToolbarInfo & info = LyX::ref().session().toolbars().load(tbinfo.name);
43
44         unsigned int flags = static_cast<unsigned int>(tbinfo.flags);
45
46         // Remove default.ui positions. Only when a valid postion is stored
47         // in the session file the default.ui value will be overwritten
48         unsigned int save = flags;
49         TurnOffFlag(TOP);
50         TurnOffFlag(BOTTOM);
51         TurnOffFlag(RIGHT);
52         TurnOffFlag(LEFT);
53
54         bool valid_location = true;
55         // init tbinfo.flags with saved location
56         if (info.location == ToolbarSection::ToolbarInfo::TOP)
57                 TurnOnFlag(TOP);
58         else if (info.location == ToolbarSection::ToolbarInfo::BOTTOM)
59                 TurnOnFlag(BOTTOM);
60         else if (info.location == ToolbarSection::ToolbarInfo::RIGHT)
61                 TurnOnFlag(RIGHT);
62         else if (info.location == ToolbarSection::ToolbarInfo::LEFT)
63                 TurnOnFlag(LEFT);
64         else {
65                 // use setting from default.ui
66                 flags = save;
67                 valid_location = false;
68         }
69
70         // invalid location is for a new toolbar that has no saved information,
71         // so info.visible is not used for this case.
72         if (valid_location) {
73                 // init tbinfo.flags with saved visibility,
74                 TurnOffFlag(ON);
75                 TurnOffFlag(OFF);
76                 TurnOffFlag(AUTO);
77                 if (info.state == ToolbarSection::ToolbarInfo::ON)
78                         TurnOnFlag(ON);
79                 else if (info.state == ToolbarSection::ToolbarInfo::OFF)
80                         TurnOnFlag(OFF);
81                 else
82                         TurnOnFlag(AUTO);
83         }
84         /*
85         std::cout << "State " << info.state << " FLAGS: " << flags
86                 << " ON:" << (flags & ToolbarBackend::ON)
87                 << " OFF:" << (flags & ToolbarBackend::OFF)
88                 << " L:" << (flags & ToolbarBackend::LEFT)
89                 << " R:" << (flags & ToolbarBackend::RIGHT)
90                 << " T:" << (flags & ToolbarBackend::TOP)
91                 << " B:" << (flags & ToolbarBackend::BOTTOM)
92                 << " MA:" << (flags & ToolbarBackend::MATH)
93                 << " RE:" << (flags & ToolbarBackend::REVIEW)
94                 << " TB:" << (flags & ToolbarBackend::TABLE)
95                 << " AU:" << (flags & ToolbarBackend::AUTO)
96                 << std::endl;
97         */
98         // now set the flags
99         tbinfo.flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
100 }
101
102
103 void Toolbars::init()
104 {
105         // extracts the toolbars from the backend
106         ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
107         ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
108
109         // init flags will also add these toolbars to session if they
110         // are not already there (e.g. first run of lyx).
111         for (; cit != end; ++cit)
112                 initFlags(*cit);
113
114         // add toolbars according the order in session
115         ToolbarSection::ToolbarList::const_iterator tb = LyX::ref().session().toolbars().begin();
116         ToolbarSection::ToolbarList::const_iterator te = LyX::ref().session().toolbars().end();
117         ToolbarSection::ToolbarInfo::Location last_loc = ToolbarSection::ToolbarInfo::NOTSET;
118         int last_posx = 0;
119         int last_posy = 0;
120         for (; tb != te; ++tb) {
121                 LYXERR(Debug::INIT) << "Adding " << tb->get<0>() << " at position " << tb->get<1>().posx << " " << tb->get<1>().posy << endl;
122                 // add toolbar break if posx or posy changes
123                 bool newline = tb->get<1>().location == last_loc && (
124                         // if two toolbars at the same location, assume uninitialized and add toolbar break
125                         (tb->get<1>().posx == last_posx && tb->get<1>().posy == last_posy) ||
126                         (last_loc == ToolbarSection::ToolbarInfo::TOP && tb->get<1>().posy != last_posy) ||
127                         (last_loc == ToolbarSection::ToolbarInfo::BOTTOM && tb->get<1>().posy != last_posy) ||
128                         (last_loc == ToolbarSection::ToolbarInfo::LEFT && tb->get<1>().posx != last_posx) ||
129                         (last_loc == ToolbarSection::ToolbarInfo::RIGHT && tb->get<1>().posx != last_posx) );
130                 // find the backend item and add
131                 for (cit = toolbarbackend.begin(); cit != end; ++cit)
132                         if (cit->name == tb->get<0>()) {
133                                 add(*cit, newline);
134                                 last_loc = tb->get<1>().location;
135                                 last_posx = tb->get<1>().posx;
136                                 last_posy = tb->get<1>().posy;
137                                 break;
138                         }
139         }
140 }
141
142
143 void Toolbars::display(string const & name, bool show)
144 {
145         ToolbarBackend::Toolbars::iterator cit = toolbarbackend.begin();
146         ToolbarBackend::Toolbars::iterator end = toolbarbackend.end();
147
148         for (; cit != end; ++cit) {
149                 if (cit->name == name) {
150                         unsigned int flags = cit->flags;
151                         TurnOffFlag(ON);
152                         TurnOffFlag(OFF);
153                         TurnOffFlag(AUTO);
154                         if (show)
155                                 TurnOnFlag(ON);
156                         else
157                                 TurnOnFlag(OFF);
158                         cit->flags = static_cast<lyx::ToolbarInfo::Flags>(flags);
159                         displayToolbar(*cit, show);
160                 }
161         }
162
163         LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
164                 << name << endl;
165 }
166
167
168 ToolbarInfo * Toolbars::getToolbarInfo(string const & name)
169 {
170         return toolbarbackend.getUsedToolbarInfo(name);
171 }
172
173
174 void Toolbars::toggleToolbarState(string const & name, bool allowauto)
175 {
176         ToolbarInfo * tbi = toolbarbackend.getUsedToolbarInfo(name);
177
178         if (!tbi) {
179                 LYXERR(Debug::GUI) << "Toolbar::display: no toolbar named "
180                         << name << endl;
181                 return;
182         }
183
184         int flags = tbi->flags;
185         // off -> on
186         if (flags & ToolbarInfo::OFF) {
187                 TurnOffFlag(OFF);
188                 TurnOnFlag(ON);
189         // auto -> off
190         } else if (flags & ToolbarInfo::AUTO) {
191                 TurnOffFlag(AUTO);
192                 TurnOnFlag(OFF);
193         } else if (allowauto 
194                    && ((flags & ToolbarInfo::MATH) 
195                        || (flags & ToolbarInfo::TABLE)
196                        || (flags & ToolbarInfo::REVIEW))) {
197                 // for math etc, toggle from on -> auto
198                 TurnOffFlag(ON);
199                 TurnOnFlag(AUTO);
200         } else {
201                 // for others, toggle from on -> off
202                 TurnOffFlag(ON);
203                 TurnOnFlag(OFF);
204         }
205         tbi->flags = static_cast<ToolbarInfo::Flags>(flags);
206 }
207 #undef TurnOnFlag
208 #undef TurnOffFlag
209
210
211 void Toolbars::update(bool in_math, bool in_table, bool review)
212 {
213         updateIcons();
214
215         // extracts the toolbars from the backend
216         ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
217         ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
218
219         for (; cit != end; ++cit) {
220                 if (cit->flags & ToolbarInfo::ON)
221                         displayToolbar(*cit, true);
222                 else if (cit->flags & ToolbarInfo::OFF)
223                         displayToolbar(*cit, false);
224                 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::MATH))
225                         displayToolbar(*cit, in_math);
226                 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::TABLE))
227                         displayToolbar(*cit, in_table);
228                 else if ((cit->flags & ToolbarInfo::AUTO) && (cit->flags & ToolbarInfo::REVIEW))
229                         displayToolbar(*cit, review);
230         }
231 }
232
233
234 } // namespace frontend
235 } // namespace lyx