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