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