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