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