]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Toolbar_pimpl.C
remove unused toolbar (de)activate()
[lyx.git] / src / frontends / xforms / Toolbar_pimpl.C
1 /**
2  * \file Toolbar_pimpl.C
3  * Copyright 1995 Matthias Ettrich
4  * Copyright 1995-2001 The LyX Team.
5  * Copyright 1996-1998 Lars Gullik Bjønnes
6  * See the file COPYING.
7  *
8  * \author Lars Gullik Bjønnes, larsbj@lyx.org
9  */
10
11 //  Added pseudo-action handling, asierra 180296
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "Toolbar_pimpl.h"
20 #include "debug.h"
21 #include "XFormsView.h"
22 #include "lyxfunc.h"
23 #include "FuncStatus.h"
24 #include "BufferView.h"
25 #include "buffer.h"
26 #include "lyxtextclasslist.h"
27 #include "LyXAction.h"
28 #include "MathsSymbols.h"
29 #include "gettext.h"
30 #include "Tooltips.h"
31 #include FORMS_H_LOCATION
32
33 #include "support/LAssert.h"
34 #include "support/filetools.h"
35 #include "support/lstrings.h"
36
37 using std::endl;
38
39 extern LyXAction lyxaction;
40
41 // some constants
42 const int standardspacing = 2; // the usual space between items
43 const int sepspace = 6; // extra space
44 const int buttonwidth = 30; // the standard button width
45 const int height = 30; // the height of all items in the toolbar
46
47 Toolbar::Pimpl::toolbarItem::toolbarItem()
48 {
49         action = LFUN_NOACTION;
50         icon = 0;
51 }
52
53
54 Toolbar::Pimpl::toolbarItem::~toolbarItem()
55 {
56         // It seems that now this is taken care of
57         // in the XFormsView destructor. (Lgb)
58         // clean();
59 }
60
61
62 void Toolbar::Pimpl::toolbarItem::clean()
63 {
64         if (icon) {
65                 fl_delete_object(icon);
66                 fl_free_object(icon);
67                 icon = 0;
68         }
69 }
70
71
72 Toolbar::Pimpl::toolbarItem &
73 Toolbar::Pimpl::toolbarItem::operator=(toolbarItem const & ti)
74 {
75         // Are we assigning the object onto itself?
76         if (this == &ti)
77                 return *this;
78
79         // If we already have an icon, release it.
80         clean();
81
82         // do we have to check icon too?
83         action = ti.action;
84         icon = 0; // locally we need to get the icon anew
85
86         return *this;
87 }
88
89
90
91 Toolbar::Pimpl::Pimpl(LyXView * o, Dialogs & d, int x, int y)
92         : owner(static_cast<XFormsView *>(o)), sxpos(x), sypos(y)
93 {
94         combox = 0;
95         tooltip_ = new Tooltips(d);
96 }
97
98
99 Toolbar::Pimpl::~Pimpl()
100 {
101         clean();
102         delete tooltip_;
103 }
104
105
106 void Toolbar::Pimpl::update()
107 {
108         ToolbarList::const_iterator p = toollist.begin();
109         ToolbarList::const_iterator end = toollist.end();
110         for (; p != end; ++p) {
111                 if (p->icon) {
112                         FuncStatus status = owner->getLyXFunc()->getStatus(p->action);
113                         if (status.onoff(true)) {
114                                 // I'd like to use a different color
115                                 // here, but then the problem is to
116                                 // know how to use transparency with
117                                 // Xpm library. It seems pretty
118                                 // complicated to me (JMarc)
119                                 fl_set_object_color(p->icon, FL_LEFT_BCOL, FL_BLUE);
120                                 fl_set_object_boxtype(p->icon, FL_DOWN_BOX);
121                         } else {
122                                 fl_set_object_color(p->icon, FL_MCOL, FL_BLUE);
123                                 fl_set_object_boxtype(p->icon, FL_UP_BOX);
124                         }
125                         if (status.disabled()) {
126                                 // Is there a way here to specify a
127                                 // mask in order to show that the
128                                 // button is disabled? (JMarc)
129                                 fl_deactivate_object(p->icon);
130                         }
131                         else
132                                 fl_activate_object(p->icon);
133                 } else if (p->action == ToolbarDefaults::LAYOUTS && combox) {
134                         if (owner->getLyXFunc()->getStatus(LFUN_LAYOUT).disabled())
135                                 combox->deactivate();
136                         else
137                                 combox->activate();
138                 }
139         }
140 }
141
142
143 // this one is not "C" because combox callbacks are really C++ %-|
144 void Toolbar::Pimpl::layoutSelectedCB(int, void * arg, Combox *)
145 {
146         Toolbar::Pimpl * tb = reinterpret_cast<Toolbar::Pimpl *>(arg);
147
148         tb->layoutSelected();
149 }
150
151
152 void Toolbar::Pimpl::layoutSelected()
153 {
154         string const & layoutguiname = combox->getline();
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()) == layoutguiname) {
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         if (combox) {
174                 LyXTextClass const & tc =
175                         textclasslist[owner->buffer()->params.textclass];
176                 combox->select(_(tc[layout]->name()));
177         }
178 }
179
180
181 void Toolbar::Pimpl::updateLayoutList(bool force)
182 {
183         // Update the layout display
184         if (!combox) return;
185
186         // If textclass is different, we need to update the list
187         if (combox->empty() || force) {
188                 combox->clear();
189                 LyXTextClass const & tc =
190                         textclasslist[owner->buffer()->params.textclass];
191                 LyXTextClass::const_iterator end = tc.end();
192                 for (LyXTextClass::const_iterator cit = tc.begin();
193                      cit != end; ++cit) {
194                         // ignore obsolete entries
195                         if ((*cit)->obsoleted_by().empty())
196                                 combox->addline(_((*cit)->name()));
197                 }
198         }
199         // we need to do this.
200         combox->redraw();
201 }
202
203
204 void Toolbar::Pimpl::clearLayoutList()
205 {
206         if (combox) {
207                 combox->clear();
208                 combox->redraw();
209         }
210 }
211
212
213 void Toolbar::Pimpl::openLayoutList()
214 {
215         if (combox)
216                 combox->show();
217 }
218
219
220 namespace {
221
222 void ToolbarCB(FL_OBJECT * ob, long ac)
223 {
224         XFormsView * owner = static_cast<XFormsView *>(ob->u_vdata);
225
226         owner->getLyXFunc()->verboseDispatch(int(ac), true);
227 }
228
229
230 extern "C" {
231
232         static
233         void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
234         {
235                 ToolbarCB(ob, data);
236         }
237
238 }
239
240
241 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height)
242 {
243         string arg;
244         string xpm_name;
245
246         const kb_action act = lyxaction.retrieveActionArg(action, arg);
247         string const name = lyxaction.getActionName(act);
248         if (!arg.empty())
249                 xpm_name = subst(name + ' ' + arg, ' ','_');
250         else
251                 xpm_name = name;
252
253         string fullname = LibFileSearch("images", xpm_name, "xpm");
254
255         if (!fullname.empty()) {
256                 lyxerr[Debug::GUI] << "Full icon name is `"
257                                    << fullname << "'" << endl;
258                 fl_set_pixmapbutton_file(obj, fullname.c_str());
259                 return;
260         }
261
262         if (act == LFUN_INSERT_MATH && !arg.empty()) {
263                 char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
264                                                               buttonwidth,
265                                                               height);
266                 if (pixmap) {
267                         lyxerr[Debug::GUI] << "Using mathed-provided icon"
268                                            << endl;
269                         fl_set_pixmapbutton_data(obj,
270                                                  const_cast<char **>(pixmap));
271                         return;
272                 }
273         }
274
275         lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
276         fullname = LibFileSearch("images", "unknown", "xpm");
277         if (!fullname.empty()) {
278                 lyxerr[Debug::GUI] << "Using default `unknown' icon"
279                                    << endl;
280                 fl_set_pixmapbutton_file(obj, fullname.c_str());
281         }
282 }
283
284 } // namespace anon
285
286
287 void Toolbar::Pimpl::set(bool doingmain)
288 {
289         // we shouldn't set if we have not cleaned
290         if (!cleaned) return;
291
292         FL_OBJECT * obj;
293
294         if (!doingmain) {
295                 fl_freeze_form(owner->getForm());
296                 fl_addto_form(owner->getForm());
297         }
298
299         ToolbarList::iterator item = toollist.begin();
300         ToolbarList::iterator end = toollist.end();
301         for (; item != end; ++item) {
302                 switch (item->action) {
303                 case ToolbarDefaults::SEPARATOR:
304                         xpos += sepspace;
305                         break;
306                 case ToolbarDefaults::NEWLINE:
307                         // Not supported yet.
308                         break;
309                 case ToolbarDefaults::LAYOUTS:
310                         xpos += standardspacing;
311                         if (!combox)
312                                 combox = new Combox(FL_COMBOX_DROPLIST);
313                         combox->add(xpos, ypos, 135, height, 400);
314                         combox->setcallback(layoutSelectedCB, this);
315                         combox->resize(FL_RESIZE_ALL);
316                         combox->gravity(NorthWestGravity, NorthWestGravity);
317                         xpos += 135;
318                         break;
319                 default:
320                         xpos += standardspacing;
321                         item->icon = obj =
322                                 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
323                                                     xpos, ypos,
324                                                     buttonwidth,
325                                                     height, "");
326                         fl_set_object_resize(obj, FL_RESIZE_ALL);
327                         fl_set_object_gravity(obj,
328                                               NorthWestGravity,
329                                               NorthWestGravity);
330                         fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
331                                                static_cast<long>(item->action));
332                         // Remove the blue feedback rectangle
333                         fl_set_pixmapbutton_focus_outline(obj, 0);
334
335                         // initialise the tooltip
336                         string const tip = _(lyxaction.helpText(obj->argument));
337                         tooltip_->init(obj, tip);
338
339                         // The view that this object belongs to.
340                         obj->u_vdata = owner;
341
342                         setPixmap(obj, item->action, buttonwidth, height);
343                         // we must remember to update the positions
344                         xpos += buttonwidth;
345                         // ypos is constant
346                         /* Here will come a check to see if the new
347                          * pos is within the bounds of the main frame,
348                          * and perhaps wrap the toolbar if not.
349                          */
350                         break;
351                 }
352         }
353
354         if (!doingmain) {
355                 fl_end_form();
356                 fl_unfreeze_form(owner->getForm());
357                 // Should be safe to do this here.
358                 owner->updateLayoutChoice();
359         }
360
361         // set the state of the icons
362         //update();
363
364         cleaned = false;
365 }
366
367
368 void Toolbar::Pimpl::add(int action, bool doclean)
369 {
370         if (doclean && !cleaned) clean();
371
372         // this is what we do if we want to add to an existing
373         // toolbar.
374         if (!doclean && owner) {
375                 // first "hide" the toolbar buttons. This is not a real hide
376                 // actually it deletes and frees the button altogether.
377                 lyxerr << "Toolbar::add: \"hide\" the toolbar buttons."
378                        << endl;
379
380                 lightReset();
381
382                 fl_freeze_form(owner->getForm());
383
384                 ToolbarList::iterator p = toollist.begin();
385                 ToolbarList::iterator end = toollist.end();
386                 for (; p != end; ++p) {
387                         p->clean();
388                 }
389
390                 if (combox) {
391                         delete combox;
392                         combox = 0;
393                 }
394                 fl_unfreeze_form(owner->getForm());
395                 cleaned = true; // this is not completely true, but OK anyway
396         }
397
398         // there exist some special actions not part of
399         // kb_action: SEPARATOR, LAYOUTS
400
401         toolbarItem newItem;
402         newItem.action = action;
403         toollist.push_back(newItem);
404 }
405
406
407 void Toolbar::Pimpl::clean()
408 {
409         //reset(); // I do not understand what this reset() is, anyway
410
411         //now delete all the objects..
412         if (owner)
413                 fl_freeze_form(owner->getForm());
414
415         // G++ vector does not have clear defined
416         //toollist.clear();
417         toollist.erase(toollist.begin(), toollist.end());
418
419         lyxerr[Debug::GUI] << "Combox: " << combox << endl;
420         if (combox) {
421                 delete combox;
422                 combox = 0;
423         }
424
425         if (owner)
426                 fl_unfreeze_form(owner->getForm());
427         lyxerr[Debug::GUI] << "toolbar cleaned" << endl;
428         cleaned = true;
429 }
430
431
432 void Toolbar::Pimpl::push(int nth)
433 {
434         lyxerr[Debug::GUI] << "Toolbar::push: trying to trigger no `"
435                            << nth << '\'' << endl;
436
437         if (nth <= 0 || nth >= int(toollist.size())) {
438                 // item nth not found...
439                 return;
440         }
441
442         fl_trigger_object(toollist[nth - 1].icon);
443 }
444
445
446 void Toolbar::Pimpl::reset()
447 {
448         //toollist = 0; // what is this supposed to do?
449         cleaned = false;
450         lightReset();
451 }
452
453
454 void Toolbar::Pimpl::lightReset() {
455         xpos = sxpos - standardspacing;
456         ypos = sypos;
457 }