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)
47 boost::tie(act, arg) = lyxaction.retrieveActionArg(action);
49 string const name = lyxaction.getActionName(act);
53 xpm_name = subst(name + ' ' + arg, ' ','_');
57 string fullname = LibFileSearch("images", xpm_name, "xpm");
59 if (!fullname.empty()) {
60 lyxerr[Debug::GUI] << "Full icon name is `"
61 << fullname << "'" << endl;
62 return QPixmap(fullname.c_str());
65 if (act == LFUN_INSERT_MATH && !arg.empty()) {
67 char const ** pixmap =
68 get_pixmap_from_symbol(arg.c_str(), 30, 30);
70 lyxerr[Debug::GUI] << "Using mathed-provided icon"
72 return QPixmap(pixmap);
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"
83 return QPixmap(fullname.c_str());
89 class QLComboBox : public QComboBox {
91 QLComboBox(QWidget * parent) : QComboBox(parent) {}
92 void popup() { QComboBox::popup(); }
96 Toolbar::Pimpl::Pimpl(LyXView * o, Dialogs &, int, int)
97 : owner_(static_cast<QtView *>(o)),
100 proxy_.reset(new ToolbarProxy(*this));
104 Toolbar::Pimpl::~Pimpl()
109 void Toolbar::Pimpl::update()
111 ButtonMap::const_iterator p = map_.begin();
112 ButtonMap::const_iterator end = map_.end();
114 for (; p != end; ++p) {
115 QToolButton * button = p->first;
116 int action = p->second;
118 FuncStatus const status =
119 owner_->getLyXFunc()->getStatus(action);
121 button->setToggleButton(true);
122 button->setOn(status.onoff(true));
123 button->setEnabled(!status.disabled());
127 combo_->setEnabled(!owner_->getLyXFunc()->getStatus(LFUN_LAYOUT).disabled());
131 void Toolbar::Pimpl::button_selected(QToolButton * button)
133 ButtonMap::const_iterator cit = map_.find(button);
135 if (cit == map_.end()) {
136 lyxerr << "non existent tool button selected !" << endl;
140 owner_->getLyXFunc()->dispatch(cit->second, true);
144 void Toolbar::Pimpl::changed_layout(string const & sel)
146 LyXTextClass const & tc =
147 owner_->buffer()->params.getLyXTextClass();
149 LyXTextClass::const_iterator end = tc.end();
150 for (LyXTextClass::const_iterator cit = tc.begin();
152 if (_((*cit)->name()) == sel) {
153 owner_->getLyXFunc()->dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()));
157 lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
162 void Toolbar::Pimpl::setLayout(string const & layout)
164 LyXTextClass const & tc =
165 owner_->buffer()->params.getLyXTextClass();
167 string const & name = _(tc[layout]->name());
171 for (i = 0; i < combo_->count(); ++i) {
172 if (name == combo_->text(i).latin1())
176 if (i == combo_->count()) {
177 lyxerr << "Trying to select non existent layout type "
182 combo_->setCurrentItem(i);
186 void Toolbar::Pimpl::updateLayoutList(bool force)
188 // if we don't need an update, don't ...
189 if (combo_->count() && !force)
192 LyXTextClass const & tc =
193 owner_->buffer()->params.getLyXTextClass();
195 combo_->setUpdatesEnabled(false);
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());
207 // needed to recalculate size hint
209 combo_->setMinimumWidth(combo_->sizeHint().width());
212 combo_->setUpdatesEnabled(true);
217 void Toolbar::Pimpl::clearLayoutList()
223 void Toolbar::Pimpl::openLayoutList()
229 void Toolbar::Pimpl::add(int action)
231 if (!toolbars_.size()) {
232 toolbars_.push_back(new QToolBar(owner_));
236 case ToolbarDefaults::SEPARATOR:
237 toolbars_.back()->addSeparator();
239 case ToolbarDefaults::NEWLINE:
240 toolbars_.push_back(new QToolBar(owner_));
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());
249 QObject::connect(combo_, SIGNAL(activated(const QString &)),
250 proxy_.get(), SLOT(layout_selected(const QString &)));
255 new QToolButton(getIconPixmap(action),
256 _(lyxaction.helpText(action)).c_str(), "",
257 proxy_.get(), SLOT(button_selected()), toolbars_.back());