]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Toolbar_pimpl.C
Bugfixes: checkboxes to radiobuttons (from J�rgen S) and remove a little
[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.TextClass(owner->buffer()->
238                                         params.textclass);
239         
240         LyXTextClass::const_iterator end = tc.end();
241         for (LyXTextClass::const_iterator cit = tc.begin();
242              cit != end; ++cit) {
243                 if (_(cit->name()) == layoutguiname) {
244                         owner->getLyXFunc()->dispatch(LFUN_LAYOUT, cit->name());
245                         return;
246                 }
247         }
248         lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
249                << endl;
250 }
251  
252
253 void Toolbar::Pimpl::setLayout(int layout)
254 {
255         if (combox) {
256                 LyXTextClass const & tc =
257                         textclasslist.TextClass(owner->buffer()->
258                                                 params.textclass);
259                 combox->select(_(tc[layout].name()));
260         }
261 }
262
263
264 void Toolbar::Pimpl::updateLayoutList(bool force)
265 {
266         // Update the layout display
267         if (!combox) return;
268
269         // If textclass is different, we need to update the list
270         if (combox->empty() || force) {
271                 combox->clear();
272                 LyXTextClass const & tc =
273                         textclasslist.TextClass(owner->buffer()->
274                                                 params.textclass);
275                 LyXTextClass::const_iterator end = tc.end();
276                 for (LyXTextClass::const_iterator cit = tc.begin();
277                      cit != end; ++cit) {
278                         // ignore obsolete entries
279                         if (cit->obsoleted_by().empty())
280                                 combox->addline(_(cit->name()));
281                 }
282         }
283         // we need to do this.
284         combox->redraw();
285 }
286
287
288 void Toolbar::Pimpl::clearLayoutList()
289 {
290         if (combox) {
291                 combox->clear();
292                 combox->redraw();
293         }
294 }
295
296
297 void Toolbar::Pimpl::openLayoutList()
298 {
299         if (combox)
300                 combox->show();
301 }
302
303
304 namespace {
305
306 void ToolbarCB(FL_OBJECT * ob, long ac)
307 {
308         XFormsView * owner = static_cast<XFormsView *>(ob->u_vdata);
309         
310         owner->getLyXFunc()->verboseDispatch(int(ac), true);
311 }
312
313
314 extern "C" {
315         
316         static
317         void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
318         {
319                 ToolbarCB(ob, data);
320         }
321         
322 }
323
324
325 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height)
326 {
327         string arg;
328         string xpm_name;
329
330         const kb_action act = lyxaction.retrieveActionArg(action, arg);
331         string const name = lyxaction.getActionName(act);
332         if (!arg.empty())
333                 xpm_name = subst(name + ' ' + arg, ' ','_');
334         else 
335                 xpm_name = name;
336
337         string fullname = LibFileSearch("images", xpm_name, "xpm");
338
339         if (!fullname.empty()) {
340                 lyxerr[Debug::GUI] << "Full icon name is `" 
341                                        << fullname << "'" << endl;
342                 fl_set_pixmapbutton_file(obj, fullname.c_str());
343                 return;
344         }
345
346         if (act == LFUN_INSERT_MATH && !arg.empty()) {
347                 char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
348                                                         buttonwidth,
349                                                         height);
350                 if (pixmap) {
351                         lyxerr[Debug::GUI] << "Using mathed-provided icon"
352                                            << endl;
353                         fl_set_pixmapbutton_data(obj,
354                                                  const_cast<char **>(pixmap));
355                         return;
356                 }
357         }
358         
359         lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
360         fullname = LibFileSearch("images", "unknown", "xpm");
361         if (!fullname.empty()) {
362                 lyxerr[Debug::GUI] << "Using default `unknown' icon" 
363                                        << endl;
364                 fl_set_pixmapbutton_file(obj, fullname.c_str());
365         }
366 }
367
368 } // namespace anon
369
370
371 void Toolbar::Pimpl::set(bool doingmain)
372 {
373         // we shouldn't set if we have not cleaned
374         if (!cleaned) return;
375         
376         FL_OBJECT * obj;
377         
378         if (!doingmain) {
379                 fl_freeze_form(owner->getForm());
380                 fl_addto_form(owner->getForm());
381         }
382
383 #if FL_REVISION < 89
384         // add the time if it don't exist
385         if (bubble_timer == 0)
386                 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
387                                             xpos, ypos, 0, 0, "Timer");
388 #endif
389         
390         ToolbarList::iterator item = toollist.begin();
391         ToolbarList::iterator end = toollist.end();
392         for (; item != end; ++item) {
393                 switch (item->action){
394                 case ToolbarDefaults::SEPARATOR:
395                         xpos += sepspace;
396                         break;
397                 case ToolbarDefaults::NEWLINE:
398                         // Not supported yet.
399                         break;
400                 case ToolbarDefaults::LAYOUTS:
401                         xpos += standardspacing;
402                         if (!combox)
403                                 combox = new Combox(FL_COMBOX_DROPLIST);
404                         combox->add(xpos, ypos, 135, height, 400);
405                         combox->setcallback(layoutSelectedCB, this);
406                         combox->resize(FL_RESIZE_ALL);
407                         combox->gravity(NorthWestGravity, NorthWestGravity);
408                         xpos += 135;
409                         break;
410                 default:
411                         xpos += standardspacing;
412                         item->icon = obj = 
413                                 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
414                                                     xpos, ypos,
415                                                     buttonwidth,
416                                                     height, "");
417                         fl_set_object_resize(obj, FL_RESIZE_ALL);
418                         fl_set_object_gravity(obj,
419                                               NorthWestGravity,
420                                               NorthWestGravity);
421                         fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
422                                                static_cast<long>(item->action));
423                         // Remove the blue feedback rectangle
424                         fl_set_pixmapbutton_focus_outline(obj, 0);
425
426                         // Set the tooltip
427 #if FL_REVISION >= 89
428                         string const help(_(lyxaction.helpText(item->action)));
429                         fl_set_object_helper(obj, help.c_str());        
430 #else
431                         fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
432                         obj->u_cdata = reinterpret_cast<char *>(bubble_timer);
433 #endif
434
435                         // The view that this object belongs to.
436                         obj->u_vdata = owner;
437
438                         setPixmap(obj, item->action, buttonwidth, height);
439                         // we must remember to update the positions
440                         xpos += buttonwidth;
441                         // ypos is constant
442                         /* Here will come a check to see if the new
443                          * pos is within the bounds of the main frame,
444                          * and perhaps wrap the toolbar if not.
445                          */
446                         break;
447                 }
448         }
449
450         if (!doingmain) {
451                 fl_end_form();
452                 fl_unfreeze_form(owner->getForm());
453                 // Should be safe to do this here.
454                 owner->updateLayoutChoice();
455         }
456
457         // set the state of the icons
458         //update();
459
460         cleaned = false;
461 }
462
463
464 void Toolbar::Pimpl::add(int action, bool doclean)
465 {
466         if (doclean && !cleaned) clean();
467
468         // this is what we do if we want to add to an existing
469         // toolbar.
470         if (!doclean && owner) {
471                 // first "hide" the toolbar buttons. This is not a real hide
472                 // actually it deletes and frees the button altogether.
473                 lyxerr << "Toolbar::add: \"hide\" the toolbar buttons." 
474                        << endl;
475
476                 lightReset();
477                 
478                 fl_freeze_form(owner->getForm());
479
480                 ToolbarList::iterator p = toollist.begin();
481                 ToolbarList::iterator end = toollist.end();
482                 for (; p != end; ++p) {
483                         p->clean();
484                 }
485
486                 if (combox) {
487                         delete combox;
488                         combox = 0;
489                 }
490                 fl_unfreeze_form(owner->getForm());
491                 cleaned = true; // this is not completely true, but OK anyway
492         }
493         
494         // there exist some special actions not part of
495         // kb_action: SEPARATOR, LAYOUTS
496
497         toolbarItem newItem;
498         newItem.action = action;
499         toollist.push_back(newItem);
500 }
501
502
503 void Toolbar::Pimpl::clean()
504 {
505         //reset(); // I do not understand what this reset() is, anyway
506
507         //now delete all the objects..
508         if (owner)
509                 fl_freeze_form(owner->getForm());
510
511         // G++ vector does not have clear defined
512         //toollist.clear();
513         toollist.erase(toollist.begin(), toollist.end());
514
515         lyxerr[Debug::GUI] << "Combox: " << combox << endl;
516         if (combox) {
517                 delete combox;
518                 combox = 0;
519         }
520
521         if (owner)
522                 fl_unfreeze_form(owner->getForm());
523         lyxerr[Debug::GUI] << "toolbar cleaned" << endl;
524         cleaned = true;
525 }
526
527
528 void Toolbar::Pimpl::push(int nth)
529 {
530         lyxerr[Debug::GUI] << "Toolbar::push: trying to trigger no `"
531                                << nth << '\'' << endl;
532         
533         if (nth <= 0 || nth >= int(toollist.size())) {
534                 // item nth not found...
535                 return;
536         }
537
538         fl_trigger_object(toollist[nth - 1].icon);
539 }
540
541
542 void Toolbar::Pimpl::reset() 
543 {
544         //toollist = 0; // what is this supposed to do?
545         cleaned = false;
546         lightReset();
547 }
548
549
550 void Toolbar::Pimpl::lightReset() {
551         xpos = sxpos - standardspacing;
552         ypos = sypos;
553 }