2 * \file Toolbar_pimpl.C
3 * See the file COPYING.
5 * \author Lars Gullik Bjønnes, larsbj@lyx.org
6 * \uathor John Levon <moz@compsoc.man.ac.uk>
12 #pragma implementation
15 #include "ToolbarDefaults.h"
18 #include "FuncStatus.h"
19 #include "BufferView.h"
21 #include "LyXAction.h"
24 #include "support/LAssert.h"
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
30 #include "Toolbar_pimpl.h"
32 #include <boost/tuple/tuple.hpp>
35 #include <qcombobox.h>
37 #include <qsizepolicy.h>
43 QPixmap getIconPixmap(int action)
45 FuncRequest f = lyxaction.retrieveActionArg(action);
49 if (f.action == LFUN_INSERT_MATH && !f.argument.empty()) {
50 xpm_name = "math/" + subst(f.argument, ' ', '_');
52 string const name = lyxaction.getActionName(f.action);
53 if (!f.argument.empty())
54 xpm_name = subst(name + ' ' + f.argument, ' ','_');
59 string fullname = LibFileSearch("images", xpm_name, "xpm");
61 if (!fullname.empty()) {
62 lyxerr[Debug::GUI] << "Full icon name is `"
63 << fullname << "'" << endl;
64 return QPixmap(fullname.c_str());
67 lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
68 fullname = LibFileSearch("images", "unknown", "xpm");
69 if (!fullname.empty()) {
70 lyxerr[Debug::GUI] << "Using default `unknown' icon"
73 return QPixmap(fullname.c_str());
79 class QLComboBox : public QComboBox {
81 QLComboBox(QWidget * parent) : QComboBox(parent) {}
82 void popup() { QComboBox::popup(); }
86 Toolbar::Pimpl::Pimpl(LyXView * o, int, int)
87 : owner_(static_cast<QtView *>(o)),
90 proxy_.reset(new ToolbarProxy(*this));
94 Toolbar::Pimpl::~Pimpl()
99 void Toolbar::Pimpl::update()
101 ButtonMap::const_iterator p = map_.begin();
102 ButtonMap::const_iterator end = map_.end();
104 for (; p != end; ++p) {
105 QToolButton * button = p->first;
106 int action = p->second;
108 FuncStatus const status =
109 owner_->getLyXFunc().getStatus(action);
111 button->setToggleButton(true);
112 button->setOn(status.onoff(true));
113 button->setEnabled(!status.disabled());
117 combo_->setEnabled(!owner_->getLyXFunc().getStatus(LFUN_LAYOUT).disabled());
121 void Toolbar::Pimpl::button_selected(QToolButton * button)
123 ButtonMap::const_iterator cit = map_.find(button);
125 if (cit == map_.end()) {
126 lyxerr << "non existent tool button selected !" << endl;
130 owner_->getLyXFunc().dispatch(cit->second, true);
134 void Toolbar::Pimpl::changed_layout(string const & sel)
136 LyXTextClass const & tc =
137 owner_->buffer()->params.getLyXTextClass();
139 LyXTextClass::const_iterator end = tc.end();
140 for (LyXTextClass::const_iterator cit = tc.begin();
142 if (_((*cit)->name()) == sel) {
143 owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()));
147 lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
152 void Toolbar::Pimpl::setLayout(string const & layout)
154 LyXTextClass const & tc =
155 owner_->buffer()->params.getLyXTextClass();
157 string const & name = _(tc[layout]->name());
161 for (i = 0; i < combo_->count(); ++i) {
162 if (name == combo_->text(i).latin1())
166 if (i == combo_->count()) {
167 lyxerr << "Trying to select non existent layout type "
172 combo_->setCurrentItem(i);
176 void Toolbar::Pimpl::updateLayoutList(bool force)
178 // if we don't need an update, don't ...
179 if (combo_->count() && !force)
182 LyXTextClass const & tc =
183 owner_->buffer()->params.getLyXTextClass();
185 combo_->setUpdatesEnabled(false);
189 LyXTextClass::const_iterator cit = tc.begin();
190 LyXTextClass::const_iterator end = tc.end();
191 for (; cit != end; ++cit) {
192 // ignore obsolete entries
193 if ((*cit)->obsoleted_by().empty())
194 combo_->insertItem(_((*cit)->name()).c_str());
197 // needed to recalculate size hint
199 combo_->setMinimumWidth(combo_->sizeHint().width());
202 combo_->setUpdatesEnabled(true);
207 void Toolbar::Pimpl::clearLayoutList()
213 void Toolbar::Pimpl::openLayoutList()
219 void Toolbar::Pimpl::add(int action)
221 if (!toolbars_.size()) {
222 toolbars_.push_back(new QToolBar(owner_));
226 case ToolbarDefaults::SEPARATOR:
227 toolbars_.back()->addSeparator();
229 case ToolbarDefaults::NEWLINE:
230 toolbars_.push_back(new QToolBar(owner_));
232 case ToolbarDefaults::LAYOUTS: {
233 combo_ = new QLComboBox(toolbars_.back());
234 QSizePolicy p(QSizePolicy::Minimum, QSizePolicy::Fixed);
235 combo_->setSizePolicy(p);
236 combo_->setFocusPolicy(QWidget::ClickFocus);
237 combo_->setMinimumWidth(combo_->sizeHint().width());
239 QObject::connect(combo_, SIGNAL(activated(const QString &)),
240 proxy_.get(), SLOT(layout_selected(const QString &)));
245 new QToolButton(getIconPixmap(action),
246 _(lyxaction.helpText(action)).c_str(), "",
247 proxy_.get(), SLOT(button_selected()), toolbars_.back());