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