]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/Toolbar_pimpl.C
add missing header
[lyx.git] / src / frontends / qt2 / Toolbar_pimpl.C
1 /**
2  * \file Toolbar_pimpl.C
3  * See the file COPYING.
4  *
5  * \author Lars Gullik Bjønnes, larsbj@lyx.org
6  * \uathor John Levon <moz@compsoc.man.ac.uk>
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "ToolbarDefaults.h"
16 #include "debug.h"
17 #include "lyxfunc.h"
18 #include "FuncStatus.h"
19 #include "BufferView.h"
20 #include "buffer.h"
21 #include "LyXAction.h"
22 #include "gettext.h"
23
24 #include "support/LAssert.h"
25 #include "support/filetools.h"
26 #include "support/lstrings.h" 
27
28 #include "QtView.h"
29  
30 #include "Toolbar_pimpl.h"
31  
32 #include <boost/tuple/tuple.hpp>
33  
34 #include <qtoolbar.h>
35 #include <qcombobox.h>
36 #include <qtooltip.h>
37 #include <qsizepolicy.h>
38  
39 using std::endl;
40
41 namespace {
42  
43 QPixmap getIconPixmap(int action)
44 {
45         kb_action act;
46         string arg;
47         boost::tie(act, arg) = lyxaction.retrieveActionArg(action);
48  
49         string const name = lyxaction.getActionName(act);
50         string xpm_name;
51
52         if (!arg.empty())
53                 xpm_name = subst(name + ' ' + arg, ' ','_');
54         else 
55                 xpm_name = name;
56
57         string fullname = LibFileSearch("images", xpm_name, "xpm");
58
59         if (!fullname.empty()) {
60                 lyxerr[Debug::GUI] << "Full icon name is `" 
61                                        << fullname << "'" << endl;
62                 return QPixmap(fullname.c_str());
63         }
64
65         if (act == LFUN_INSERT_MATH && !arg.empty()) {
66 #if 0 // FIXME: GUII
67                 char const ** pixmap =
68                         get_pixmap_from_symbol(arg.c_str(), 30, 30);
69                 if (pixmap) {
70                         lyxerr[Debug::GUI] << "Using mathed-provided icon"
71                                            << endl;
72                         return QPixmap(pixmap);
73                 }
74 #endif
75         }
76         
77         lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
78         fullname = LibFileSearch("images", "unknown", "xpm");
79         if (!fullname.empty()) {
80                 lyxerr[Debug::GUI] << "Using default `unknown' icon" 
81                                        << endl;
82         }
83         return QPixmap(fullname.c_str());
84 }
85
86 } // namespace anon
87
88  
89 class QLComboBox : public QComboBox {
90 public:
91         QLComboBox(QWidget * parent) : QComboBox(parent) {}
92         void popup() { QComboBox::popup(); }
93 };
94
95  
96 Toolbar::Pimpl::Pimpl(LyXView * o, Dialogs &, int, int)
97         : owner_(static_cast<QtView *>(o)), 
98         combo_(0)
99 {
100         proxy_.reset(new ToolbarProxy(*this));
101 }
102
103
104 Toolbar::Pimpl::~Pimpl()
105 {
106 }
107
108
109 void Toolbar::Pimpl::update()
110 {
111         ButtonMap::const_iterator p = map_.begin();
112         ButtonMap::const_iterator end = map_.end();
113  
114         for (; p != end; ++p) {
115                 QToolButton * button = p->first;
116                 int action = p->second;
117  
118                 FuncStatus const status = 
119                         owner_->getLyXFunc()->getStatus(action);
120  
121                 button->setToggleButton(true);
122                 button->setOn(status.onoff(true));
123                 button->setEnabled(!status.disabled());
124         }
125
126         if (combo_)
127                 combo_->setEnabled(!owner_->getLyXFunc()->getStatus(LFUN_LAYOUT).disabled());
128 }
129
130
131 void Toolbar::Pimpl::button_selected(QToolButton * button)
132 {
133         ButtonMap::const_iterator cit = map_.find(button);
134
135         if (cit == map_.end()) {
136                 lyxerr << "non existent tool button selected !" << endl;
137                 return;
138         }
139
140         owner_->getLyXFunc()->dispatch(cit->second, true);
141 }
142  
143
144 void Toolbar::Pimpl::changed_layout(string const & sel)
145 {
146         LyXTextClass const & tc =
147                 owner_->buffer()->params.getLyXTextClass();
148         
149         LyXTextClass::const_iterator end = tc.end();
150         for (LyXTextClass::const_iterator cit = tc.begin();
151              cit != end; ++cit) {
152                 if (_((*cit)->name()) == sel) {
153                         owner_->getLyXFunc()->dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()));
154                         return;
155                 }
156         }
157         lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
158                << endl;
159 }
160  
161
162 void Toolbar::Pimpl::setLayout(string const & layout)
163 {
164         LyXTextClass const & tc =
165                 owner_->buffer()->params.getLyXTextClass();
166  
167         string const & name = _(tc[layout]->name());
168  
169         int i;
170  
171         for (i = 0; i < combo_->count(); ++i) {
172                 if (name == combo_->text(i).latin1())
173                         break;
174         }
175
176         if (i == combo_->count()) {
177                 lyxerr << "Trying to select non existent layout type "
178                         << name << endl;
179                 return;
180         }
181
182         combo_->setCurrentItem(i);
183 }
184
185
186 void Toolbar::Pimpl::updateLayoutList(bool force)
187 {
188         // if we don't need an update, don't ... 
189         if (combo_->count() && !force)
190                 return;
191  
192         LyXTextClass const & tc =
193                 owner_->buffer()->params.getLyXTextClass();
194  
195         combo_->setUpdatesEnabled(false); 
196  
197         combo_->clear();
198  
199         LyXTextClass::const_iterator cit = tc.begin();
200         LyXTextClass::const_iterator end = tc.end();
201         for (; cit != end; ++cit) {
202                 // ignore obsolete entries
203                 if ((*cit)->obsoleted_by().empty())
204                         combo_->insertItem(_((*cit)->name()).c_str());
205         }
206
207         // needed to recalculate size hint
208         combo_->hide();
209         combo_->setMinimumWidth(combo_->sizeHint().width());
210         combo_->show();
211
212         combo_->setUpdatesEnabled(true);
213         combo_->update();
214 }
215
216
217 void Toolbar::Pimpl::clearLayoutList()
218 {
219         combo_->clear();
220 }
221
222
223 void Toolbar::Pimpl::openLayoutList()
224 {
225         combo_->popup();
226 }
227
228
229 void Toolbar::Pimpl::add(int action)
230 {
231         if (!toolbars_.size()) {
232                 toolbars_.push_back(new QToolBar(owner_));
233         }
234  
235         switch (action) {
236         case ToolbarDefaults::SEPARATOR:
237                 toolbars_.back()->addSeparator();
238                 break;
239         case ToolbarDefaults::NEWLINE:
240                 toolbars_.push_back(new QToolBar(owner_));
241                 break;
242         case ToolbarDefaults::LAYOUTS: {
243                 combo_ = new QLComboBox(toolbars_.back());
244                 QSizePolicy p(QSizePolicy::Minimum, QSizePolicy::Fixed);
245                 combo_->setSizePolicy(p);
246                 combo_->setFocusPolicy(QWidget::ClickFocus);
247                 combo_->setMinimumWidth(combo_->sizeHint().width());
248  
249                 QObject::connect(combo_, SIGNAL(activated(const QString &)),
250                         proxy_.get(), SLOT(layout_selected(const QString &)));
251                 break;
252         }
253         default: {
254                 QToolButton * tb = 
255                         new QToolButton(getIconPixmap(action),
256                         _(lyxaction.helpText(action)).c_str(), "",
257                         proxy_.get(), SLOT(button_selected()), toolbars_.back());
258
259                 map_[tb] = action;
260                 break;
261         }
262         }
263 }