]> git.lyx.org Git - lyx.git/blob - src/frontends/Toolbars.C
* frontends/Toolbars.C:
[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, bool review)
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                 else if (cit->flags & ToolbarBackend::REVIEW)
82                         displayToolbar(*cit, review);
83         }
84 }
85
86
87 void Toolbars::setLayout(string const & layout)
88 {
89         if (layout_)
90                 layout_->set(layout);
91 }
92
93
94 bool Toolbars::updateLayoutList(int textclass)
95 {
96         // update the layout display
97         if (last_textclass_ != textclass) {
98                 if (layout_)
99                         layout_->update();
100                 last_textclass_ = textclass;
101                 return true;
102         } else
103                 return false;
104 }
105
106
107 void Toolbars::openLayoutList()
108 {
109         if (layout_)
110                 layout_->open();
111 }
112
113
114 void Toolbars::clearLayoutList()
115 {
116         last_textclass_ = -1;
117         if (layout_)
118                 layout_->clear();
119 }
120
121
122 void Toolbars::add(ToolbarBackend::Toolbar const & tbb)
123 {
124         ToolbarPtr tb_ptr = owner_.makeToolbar(tbb);
125         toolbars_[tbb.name] = tb_ptr;
126
127         if (tbb.flags & ToolbarBackend::ON)
128                 tb_ptr->show(false);
129         else
130                 tb_ptr->hide(false);
131
132         if (tb_ptr->layout())
133                 layout_ = tb_ptr->layout();
134 }
135
136
137 void Toolbars::displayToolbar(ToolbarBackend::Toolbar const & tbb,
138                               bool show_it)
139 {
140         ToolbarsMap::iterator it = toolbars_.find(tbb.name);
141         BOOST_ASSERT(it != toolbars_.end());
142
143         if (show_it)
144                 it->second->show(true);
145         else
146                 it->second->hide(true);
147 }
148
149
150 void Toolbars::update()
151 {
152         ToolbarsMap::const_iterator it = toolbars_.begin();
153         ToolbarsMap::const_iterator const end = toolbars_.end();
154         for (; it != end; ++it)
155                 it->second->update();
156
157         bool const enable =
158                 lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled();
159
160         if (layout_)
161                 layout_->setEnabled(enable);
162 }
163
164
165 void layoutSelected(LyXView & lv, string const & name)
166 {
167         LyXTextClass const & tc = lv.buffer()->params().getLyXTextClass();
168
169         LyXTextClass::const_iterator it  = tc.begin();
170         LyXTextClass::const_iterator const end = tc.end();
171         for (; it != end; ++it) {
172                 string const & itname = (*it)->name();
173                 // Yes, the lyx::to_utf8(_()) is correct
174                 if (lyx::to_utf8(_(itname)) == name) {
175                         FuncRequest const func(LFUN_LAYOUT, itname,
176                                                FuncRequest::UI);
177                         lv.dispatch(func);
178                         return;
179                 }
180         }
181         lyxerr << "ERROR (layoutSelected): layout not found!"
182                << endl;
183 }
184
185
186 } // namespace lyx