]> git.lyx.org Git - lyx.git/blob - src/frontends/Toolbars.C
remove warning, use shorter code
[lyx.git] / src / frontends / Toolbars.C
1 /**
2  * \file Toolbars.C
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 "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 "lyxfunc.h"
23 #include "lyxtextclass.h"
24 #include "LyXView.h"
25
26
27 namespace lyx {
28
29 using std::endl;
30 using std::string;
31
32
33 Toolbars::Toolbars(LyXView & owner)
34         : owner_(owner),
35           layout_(0),
36           last_textclass_(-1)
37 {}
38
39
40 void Toolbars::init()
41 {
42         // extracts the toolbars from the backend
43         ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
44         ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
45
46         for (; cit != end; ++cit)
47                 add(*cit);
48 }
49
50
51 void Toolbars::display(string const & name, bool show)
52 {
53         ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
54         ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
55
56         for (; cit != end; ++cit) {
57                 if (cit->name == name) {
58                         displayToolbar(*cit, show);
59                         return;
60                 }
61         }
62
63         lyxerr[Debug::GUI] << "Toolbar::display: no toolbar named "
64                 << name << endl;
65 }
66
67
68 void Toolbars::update(bool in_math, bool in_table)
69 {
70         update();
71
72         // extracts the toolbars from the backend
73         ToolbarBackend::Toolbars::const_iterator cit = toolbarbackend.begin();
74         ToolbarBackend::Toolbars::const_iterator end = toolbarbackend.end();
75
76         for (; cit != end; ++cit) {
77                 if (cit->flags & ToolbarBackend::MATH)
78                         displayToolbar(*cit, in_math);
79                 else if (cit->flags & ToolbarBackend::TABLE)
80                         displayToolbar(*cit, in_table);
81         }
82 }
83
84
85 void Toolbars::setLayout(string const & layout)
86 {
87         if (layout_)
88                 layout_->set(layout);
89 }
90
91
92 bool Toolbars::updateLayoutList(int textclass)
93 {
94         // update the layout display
95         if (last_textclass_ != textclass) {
96                 if (layout_)
97                         layout_->update();
98                 last_textclass_ = textclass;
99                 return true;
100         } else
101                 return false;
102 }
103
104
105 void Toolbars::openLayoutList()
106 {
107         if (layout_)
108                 layout_->open();
109 }
110
111
112 void Toolbars::clearLayoutList()
113 {
114         last_textclass_ = -1;
115         if (layout_)
116                 layout_->clear();
117 }
118
119
120 void Toolbars::add(ToolbarBackend::Toolbar const & tbb)
121 {
122         ToolbarPtr tb_ptr = owner_.makeToolbar(tbb);
123         toolbars_[tbb.name] = tb_ptr;
124
125         if (tbb.flags & ToolbarBackend::ON)
126                 tb_ptr->show(false);
127         else
128                 tb_ptr->hide(false);
129
130         if (tb_ptr->layout())
131                 layout_ = tb_ptr->layout();
132 }
133
134
135 void Toolbars::displayToolbar(ToolbarBackend::Toolbar const & tbb,
136                               bool show_it)
137 {
138         ToolbarsMap::iterator it = toolbars_.find(tbb.name);
139         BOOST_ASSERT(it != toolbars_.end());
140
141         if (show_it)
142                 it->second->show(true);
143         else
144                 it->second->hide(true);
145 }
146
147
148 void Toolbars::update()
149 {
150         ToolbarsMap::const_iterator it = toolbars_.begin();
151         ToolbarsMap::const_iterator const end = toolbars_.end();
152         for (; it != end; ++it)
153                 it->second->update();
154
155         bool const enable =
156                 lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled();
157
158         if (layout_)
159                 layout_->setEnabled(enable);
160 }
161
162
163 void layoutSelected(LyXView & lv, string const & name)
164 {
165         LyXTextClass const & tc = lv.buffer()->params().getLyXTextClass();
166
167         LyXTextClass::const_iterator it  = tc.begin();
168         LyXTextClass::const_iterator const end = tc.end();
169         for (; it != end; ++it) {
170                 string const & itname = (*it)->name();
171                 // Yes, the lyx::to_utf8(_()) is correct
172                 if (lyx::to_utf8(_(itname)) == name) {
173                         FuncRequest const func(LFUN_LAYOUT, itname,
174                                                FuncRequest::UI);
175                         lv.dispatch(func);
176                         return;
177                 }
178         }
179         lyxerr << "ERROR (layoutSelected): layout not found!"
180                << endl;
181 }
182
183
184 } // namespace lyx