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