]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/Toolbar_pimpl.C
time to recompile everything: I removed #include directives from headers here and...
[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 Toolbar::Pimpl::Pimpl(LyXView * o, Dialogs &, int, int)
88         : owner_(static_cast<QtView *>(o)), 
89         combo_(0)
90 {
91         proxy_.reset(new ToolbarProxy(*this));
92 }
93
94
95 Toolbar::Pimpl::~Pimpl()
96 {
97 }
98
99
100 void Toolbar::Pimpl::update()
101 {
102         ButtonMap::const_iterator p = map_.begin();
103         ButtonMap::const_iterator end = map_.end();
104  
105         for (; p != end; ++p) {
106                 QToolButton * button = p->first;
107                 int action = p->second;
108  
109                 FuncStatus const status = 
110                         owner_->getLyXFunc()->getStatus(action);
111  
112                 button->setToggleButton(true);
113                 button->setOn(status.onoff(true));
114                 button->setEnabled(!status.disabled());
115         }
116
117         if (combo_)
118                 combo_->setEnabled(!owner_->getLyXFunc()->getStatus(LFUN_LAYOUT).disabled());
119 }
120
121
122 void Toolbar::Pimpl::button_selected(QToolButton * button)
123 {
124         ButtonMap::const_iterator cit = map_.find(button);
125
126         if (cit == map_.end()) {
127                 lyxerr << "non existent tool button selected !" << endl;
128                 return;
129         }
130
131         owner_->getLyXFunc()->dispatch(cit->second, true);
132 }
133  
134
135 void Toolbar::Pimpl::changed_layout(string const & sel)
136 {
137         LyXTextClass const & tc =
138                 owner_->buffer()->params.getLyXTextClass();
139         
140         LyXTextClass::const_iterator end = tc.end();
141         for (LyXTextClass::const_iterator cit = tc.begin();
142              cit != end; ++cit) {
143                 if (_((*cit)->name()) == sel) {
144                         owner_->getLyXFunc()->dispatch(LFUN_LAYOUT, (*cit)->name());
145                         return;
146                 }
147         }
148         lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
149                << endl;
150 }
151  
152
153 void Toolbar::Pimpl::setLayout(string const & layout)
154 {
155         LyXTextClass const & tc =
156                 owner_->buffer()->params.getLyXTextClass();
157  
158         string const & name = _(tc[layout]->name());
159  
160         int i;
161  
162         for (i = 0; i < combo_->count(); ++i) {
163                 if (name == combo_->text(i).latin1())
164                         break;
165         }
166
167         if (i == combo_->count()) {
168                 lyxerr << "Trying to select non existent layout type "
169                         << name << endl;
170                 return;
171         }
172
173         combo_->setCurrentItem(i);
174 }
175
176
177 void Toolbar::Pimpl::updateLayoutList(bool force)
178 {
179         // if we don't need an update, don't ... 
180         if (combo_->count() && !force)
181                 return;
182  
183         LyXTextClass const & tc =
184                 owner_->buffer()->params.getLyXTextClass();
185  
186         combo_->setUpdatesEnabled(false); 
187  
188         combo_->clear();
189  
190         LyXTextClass::const_iterator cit = tc.begin();
191         LyXTextClass::const_iterator end = tc.end();
192         for (; cit != end; ++cit) {
193                 // ignore obsolete entries
194                 if ((*cit)->obsoleted_by().empty())
195                         combo_->insertItem(_((*cit)->name()).c_str());
196         }
197
198         // needed to recalculate size hint
199         combo_->hide();
200         combo_->setMinimumWidth(combo_->sizeHint().width());
201         combo_->show();
202
203         combo_->setUpdatesEnabled(true);
204         combo_->update();
205 }
206
207
208 void Toolbar::Pimpl::clearLayoutList()
209 {
210         combo_->clear();
211 }
212
213
214 void Toolbar::Pimpl::openLayoutList()
215 {
216 #if 0 // popup() is protected
217         combo_->popup();
218 #endif 
219 }
220
221
222 void Toolbar::Pimpl::set(bool)
223 {
224 // FIXME: ???
225 }
226
227
228 void Toolbar::Pimpl::add(int action, bool)
229 {
230         if (!toolbars_.size()) {
231                 toolbars_.push_back(new QToolBar(owner_));
232         }
233  
234         switch (action) {
235         case ToolbarDefaults::SEPARATOR:
236                 toolbars_.back()->addSeparator();
237                 break;
238         case ToolbarDefaults::NEWLINE:
239                 toolbars_.push_back(new QToolBar(owner_));
240                 break;
241         case ToolbarDefaults::LAYOUTS: {
242                 combo_ = new QComboBox(toolbars_.back());
243                 QSizePolicy p(QSizePolicy::Minimum, QSizePolicy::Fixed);
244                 combo_->setSizePolicy(p);
245                 combo_->setFocusPolicy(QWidget::TabFocus);
246                 combo_->setMinimumWidth(combo_->sizeHint().width());
247  
248                 QObject::connect(combo_, SIGNAL(activated(const QString &)),
249                         proxy_.get(), SLOT(layout_selected(const QString &)));
250                 break;
251         }
252         default: {
253                 char const * tooltip = _(lyxaction.helpText(action)).c_str();
254  
255                 QToolButton * tb = 
256                         new QToolButton(getIconPixmap(action),
257                         tooltip, tooltip,
258                         proxy_.get(), SLOT(button_selected()), toolbars_.back());
259
260                 map_[tb] = action;
261  
262                 QToolTip::add(tb, tooltip);
263                 break;
264         }
265         }
266 }
267
268
269 void Toolbar::Pimpl::push(int)
270 {
271 #if 0
272         lyxerr[Debug::GUI] << "Toolbar::push: trying to trigger no `"
273                                << nth << '\'' << endl;
274         
275         if (nth <= 0 || nth >= int(toollist.size())) {
276                 // item nth not found...
277                 return;
278         }
279
280         fl_trigger_object(toollist[nth - 1].icon);
281 #endif
282 }