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