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