]> git.lyx.org Git - features.git/blob - src/toolbar.C
433ee6b9b5b78ca00225aed5c0096f18c5c758d7
[features.git] / src / toolbar.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-1999 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 "toolbar.h"
20 #endif
21
22 #include "lyx_main.h"
23 #include "lyx_gui_misc.h"
24 #include "lyx.h"
25 #include "toolbar.h"
26 #include "lyxfunc.h"
27 #include "lyxlex.h"
28 #include "debug.h"
29 #include "combox.h"
30 #include "lyx_cb.h"
31 #include "LyXView.h"
32 #include "LyXAction.h"
33 #include "support/lstrings.h"
34
35 #ifdef TWO_COLOR_ICONS
36 #include "cut_bw.xpm"
37 #include "emph_bw.xpm"
38 #include "fig_bw.xpm"
39 #include "foot_bw.xpm"
40 #include "math_bw.xpm"
41 #include "depth_bw.xpm"
42 #include "margin_bw.xpm"
43 #include "melt_bw.xpm"
44 #include "copy_bw.xpm"
45 #include "noun_bw.xpm"
46 #include "paste_bw.xpm"
47 #include "free_bw.xpm"
48 #include "tab_bw.xpm"
49 #include "tex_bw.xpm"
50 #include "open_bw.xpm"
51 #include "close_bw.xpm"
52 #include "save_bw.xpm"
53 #include "print1_bw.xpm"
54 #include "quit_bw.xpm"
55 #include "typeset_ps_bw.xpm"
56 #include "unknown_bw.xpm"
57 #else 
58 #include "cut.xpm"
59 #include "emph.xpm"
60 #include "fig.xpm"
61 #include "foot.xpm"
62 #include "math.xpm"
63 #include "depth.xpm"
64 #include "margin.xpm"
65 #include "melt.xpm"
66 #include "copy.xpm"
67 #include "noun.xpm"
68 #include "paste.xpm"
69 #include "free.xpm"
70 #include "tab.xpm"
71 #include "tex.xpm"
72 #include "open.xpm"
73 #include "close.xpm"
74 #include "save.xpm"
75 #include "print1.xpm"
76 #include "quit.xpm"
77 #include "typeset_ps.xpm"
78 #include "unknown.xpm"
79 #endif
80
81 // These pixmaps are the same regardless of color:
82 #include "bold_bw.xpm"
83 #include "make_ascii_bw.xpm"
84 #include "make_latex_bw.xpm"
85 #include "run_latex_bw.xpm"
86 #include "sans_bw.xpm"
87 #include "view_dvi_bw.xpm"
88 #include "view_ps_bw.xpm"
89 #include "layout_code.xpm"
90 #include "layout_latex.xpm"
91 #include "layout_scrap.xpm"
92 #include "layout_sec.xpm"
93 #include "layout_std.xpm"
94 #include "build.xpm"
95
96 // this one is not "C" because combox callbacks are really C++ %-|
97 extern void LayoutsCB(int, void*);
98 extern char const ** get_pixmap_from_symbol(char const * arg, int, int);
99 extern LyXAction lyxaction;
100
101
102 enum _tooltags {
103         TO_ADD = 1,
104         TO_ENDTOOLBAR,
105         TO_SEPARATOR,
106         TO_LAYOUTS,
107         TO_NEWLINE,
108         TO_LAST
109 };
110
111
112 struct keyword_item toolTags[TO_LAST-1] = {
113         { "\\add", TO_ADD },
114         { "\\end_toolbar", TO_ENDTOOLBAR },
115         { "\\layouts", TO_LAYOUTS },
116         { "\\newline", TO_NEWLINE },
117         { "\\separator", TO_SEPARATOR }
118 };
119
120
121 Toolbar::Toolbar(Toolbar const &rct, LyXView *o, int x, int y)
122         : owner(o), sxpos(x), sypos(y)
123 {
124         combox = 0;
125         bubble_timer = 0;
126         reset();
127
128         // extracts the toolbar struct form rct.
129         toolbarItem *tmplist = rct.toollist;
130         while (tmplist != 0) {
131                 add(tmplist->action);
132                 lyxerr[Debug::TOOLBAR] << "tool action: "
133                                        << tmplist->action << endl;
134                 tmplist= tmplist->next;
135         }
136 }
137
138
139 // timer-cb for bubble-help (Matthias)
140 void Toolbar::BubbleTimerCB(FL_OBJECT *, long data)
141 {
142         FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
143         char* help = static_cast<char*>(ob->u_vdata);
144         fl_show_oneliner(help, ob->form->x + ob->x,
145                          ob->form->y + ob->y + ob->h);
146 }
147
148
149 extern "C" void C_Toolbar_BubbleTimerCB(FL_OBJECT * ob, long data)
150 {
151         Toolbar::BubbleTimerCB(ob, data);
152 }
153
154
155 // post_handler for bubble-help (Matthias)
156 int Toolbar::BubblePost(FL_OBJECT *ob, int event,
157              FL_Coord /*mx*/, FL_Coord /*my*/, int /*key*/, void */*xev*/)
158 {
159         string help = static_cast<char *>(ob->u_vdata);
160         Toolbar * t = reinterpret_cast<Toolbar*>(ob->u_ldata);
161         
162         if(event == FL_ENTER && !help.empty()){
163                 fl_set_object_callback(t->bubble_timer,
164                                        C_Toolbar_BubbleTimerCB,
165                                        reinterpret_cast<long>(ob));
166                 fl_set_timer(t->bubble_timer, 1);
167         }
168         else if(event != FL_MOTION){
169                 fl_set_timer(t->bubble_timer, 0);
170                 fl_hide_oneliner();
171         }
172         return 0;
173 }
174
175
176 extern "C" int C_Toolbar_BubblePost(FL_OBJECT * ob, int event,
177                                    FL_Coord /*mx*/, FL_Coord /*my*/, 
178                                    int key, void * xev)
179 {
180         return Toolbar::BubblePost(ob, event, 0, 0, key, xev);
181 }
182
183
184 void Toolbar::activate()
185 {
186         toolbarItem * tmp= 0;
187         toolbarItem * item = toollist;
188         while(item){
189                 tmp = item->next;
190                 if (item->icon) {
191                         fl_activate_object(item->icon);
192                 }
193                 item = tmp;
194         }
195 }
196
197
198 void Toolbar::deactivate()
199 {
200         toolbarItem * tmp= 0;
201         toolbarItem * item = toollist;
202         while(item){
203                 tmp = item->next;
204                 if (item->icon) {
205                         fl_deactivate_object(item->icon);
206                 }
207                 item = tmp;
208         }
209 }
210
211
212 void Toolbar::ToolbarCB(FL_OBJECT * ob, long ac)
213 {
214         Toolbar * t = reinterpret_cast<Toolbar*>(ob->u_ldata);
215         
216         string res = t->owner->getLyXFunc()->Dispatch(int(ac));
217         if(!res.empty())
218                 lyxerr[Debug::TOOLBAR] << res << endl;
219 }
220
221
222 extern "C" void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
223 {
224         Toolbar::ToolbarCB(ob, data);
225 }
226
227
228 int Toolbar::get_toolbar_func(string const & func)
229 {
230         int action = lyxaction.LookupFunc(func.c_str());
231         if (action == -1) {
232                if (func == "separator"){
233                        action = TOOL_SEPARATOR;
234                } else if (func == "layouts"){
235                         action = TOOL_LAYOUTS;
236                 } else action = 0;
237         }
238         return action;
239 }
240
241
242 void Toolbar::init()
243 {
244         add(TOOL_LAYOUTS);
245         add(LFUN_MENUOPEN);
246         //add(LFUN_CLOSEBUFFER);
247         add(LFUN_MENUWRITE);
248         add(LFUN_MENUPRINT);
249         add(TOOL_SEPARATOR);
250
251         add(LFUN_CUT);
252         add(LFUN_COPY);
253         add(LFUN_PASTE);
254         add(TOOL_SEPARATOR);
255         
256         add(LFUN_EMPH);
257         add(LFUN_NOUN);
258         add(LFUN_FREE);
259         add(TOOL_SEPARATOR);
260         
261         add(LFUN_FOOTMELT);
262         add(LFUN_MARGINMELT);
263         add(LFUN_DEPTH);
264         add(TOOL_SEPARATOR);
265
266         add(LFUN_TEX);
267         add(LFUN_MATH_MODE);
268         add(TOOL_SEPARATOR);
269
270         add(LFUN_FIGURE);
271         add(LFUN_TABLE);
272         //add(LFUN_MELT);
273 }
274
275
276 void Toolbar::set(bool doingmain)
277 {
278         // we shouldn't set if we have not cleaned
279         if (!cleaned) return;
280         
281         FL_OBJECT * obj;
282         toolbarItem * item = toollist;
283         
284         if (!doingmain) {
285                 fl_freeze_form(owner->getForm());
286                 fl_addto_form(owner->getForm());
287         }
288
289 #if FL_REVISION <86
290         // Ensure borderwidth is 2 to get visual feedback
291         int bw = fl_get_border_width();
292         fl_set_border_width(-2);
293 #endif
294
295         // add the time if it don't exist
296         if (bubble_timer == 0)
297                 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
298                                             xpos, ypos, 0, 0, "Timer");
299         
300         while(item != 0) {
301                 switch(item->action){
302                   case TOOL_SEPARATOR:
303                           xpos += sepspace;
304                           item = item->next;
305                           break;
306                   case TOOL_LAYOUTS:
307                           xpos += standardspacing;
308                           if (!combox)
309                                   combox = new Combox(FL_COMBOX_DROPLIST);
310                           combox->add(xpos, ypos, 135, height, 300);
311                           combox->setcallback(LayoutsCB);
312                           combox->resize(FL_RESIZE_ALL);
313                           combox->gravity(NorthWestGravity, NorthWestGravity);
314                           item = item->next;
315                           xpos += 135;
316                           break;
317                   default:
318                           xpos += standardspacing;
319                           item->icon = obj = 
320                                   fl_add_pixmapbutton(FL_NORMAL_BUTTON,
321                                                       xpos, ypos,
322                                                       buttonwidth,
323                                                       height, "");
324                           fl_set_object_boxtype(obj, FL_UP_BOX);
325                           fl_set_object_color(obj, FL_MCOL, FL_BLUE);
326                           fl_set_object_resize(obj, FL_RESIZE_ALL);
327                           fl_set_object_gravity(obj,
328                                                 NorthWestGravity,
329                                                 NorthWestGravity);
330                           fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
331                                                  static_cast<long>(item->action));
332 #if FL_REVISION >85
333                           // Remove the blue feedback rectangle
334                           fl_set_pixmapbutton_focus_outline(obj, 0);
335 #endif
336
337                           // set the bubble-help (Matthias)
338 #warning This is dangerous!
339                           obj->u_vdata = const_cast<char*>(item->help.c_str());
340                           // we need to know what toolbar this item
341                           // belongs too. (Lgb)
342                           obj->u_ldata = reinterpret_cast<long>(this);
343                           
344                           fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
345
346                           fl_set_pixmapbutton_data(obj, const_cast<char**>(item->pixmap));
347                           item = item->next;
348                           // we must remember to update the positions
349                           xpos += buttonwidth;
350                           // ypos is constant
351                           /* Here will come a check to see if the new
352                            * pos is within the bounds of the main frame,
353                            * and perhaps wrap the toolbar if not.
354                            */
355                           break;
356                 }
357         }
358 #if FL_REVISION <86
359         // Reset borderwidth to its default value.
360         fl_set_border_width(bw);
361 #endif
362         if (!doingmain) {
363                 fl_end_form();
364                 fl_unfreeze_form(owner->getForm());
365                 // Should be safe to do this here.
366                 owner->updateLayoutChoice();
367         }
368         
369         cleaned = false;
370 }
371
372
373 char const **Toolbar::getPixmap(kb_action action, string const & arg)
374 {
375         char const ** pixmap = unknown_xpm; //0
376         switch(action){
377         case LFUN_MENUOPEN:    pixmap = open_xpm; break;
378         case LFUN_CLOSEBUFFER: pixmap = close_xpm; break;
379         case LFUN_MENUPRINT:   pixmap = print1_xpm; break;
380         case LFUN_MENUWRITE:   pixmap = save_xpm; break;
381         case LFUN_EMPH:  pixmap = emph_xpm; break;
382         case LFUN_NOUN:        pixmap = noun_xpm; break;
383         case LFUN_FREE:        pixmap = free_xpm; break;
384         case LFUN_FOOTMELT:    pixmap = foot_xpm; break;
385         case LFUN_DEPTH:       pixmap = depth_xpm; break;
386         case LFUN_COPY:        pixmap = copy_xpm; break;
387         case LFUN_CUT:         pixmap = cut_xpm; break;
388         case LFUN_PASTE:       pixmap = paste_xpm; break;
389         case LFUN_TEX:         pixmap = tex_xpm; break;
390         case LFUN_MATH_MODE:   pixmap = math_xpm; break;
391         case LFUN_MARGINMELT:  pixmap = margin_xpm; break;
392         case LFUN_FIGURE:      pixmap = fig_xpm; break;
393         case LFUN_TABLE:       pixmap = tab_xpm; break;
394         case LFUN_MELT:        pixmap = melt_xpm; break;
395         case LFUN_QUIT:        pixmap = quit_xpm; break;
396         case LFUN_RUNDVIPS:    pixmap = update_ps_xpm; break;
397         case LFUN_EXPORT:
398         {
399                 if (arg == "ascii")
400                         pixmap = make_ascii_xpm;
401                 else if (arg == "latex")
402                         pixmap = make_latex_xpm;
403         }
404         break; 
405         case LFUN_LAYOUT:
406         {
407                 if (arg == "Section")
408                         pixmap = layout_sec_xpm;
409                 else if (arg == "LaTeX")
410                         pixmap = layout_latex_xpm;
411                 else if (arg == "LyX-Code")
412                         pixmap = layout_code_xpm;
413                 else if (arg == "Scrap")
414                         pixmap = layout_scrap_xpm;
415                 else
416                         pixmap = layout_std_xpm;
417         }
418         break;
419
420         case LFUN_BOLD : pixmap = bold_xpm; break; 
421         case LFUN_SANS: pixmap = sans_xpm; break; 
422         case LFUN_RUNLATEX: pixmap = run_latex_xpm; break; 
423         case LFUN_BUILDPROG: pixmap = build_xpm; break; 
424         case LFUN_PREVIEWPS: pixmap = view_ps_xpm; break; 
425         case LFUN_PREVIEW: pixmap = view_dvi_xpm; break; 
426         case LFUN_INSERT_MATH:
427         {
428                 if (!arg.empty())
429                         pixmap = get_pixmap_from_symbol(arg.c_str(),
430                                                         buttonwidth,
431                                                         height);
432         }
433         break;
434         default:
435                 //pixmap = unknown_xpm;
436                 break;
437         }
438         return pixmap;
439 }
440
441
442 void Toolbar::add(int action, bool doclean)
443 {
444         if (doclean && !cleaned) clean();
445
446         // this is what we do if we want to add to an existing
447         // toolbar.
448         if (!doclean && owner) {
449                 // first «hide» the toolbar buttons. This is not a real hide
450                 // actually it deletes and frees the button altogether.
451                 lyxerr << "Toolbar::add: «hide» the toolbar buttons." << endl;
452                 toolbarItem * tmp= 0;
453                 toolbarItem * item = toollist;
454
455                 lightReset();
456                 
457                 fl_freeze_form(owner->getForm());
458                 while(item){
459                         tmp = item->next;
460                         if (item->icon) {
461                                 fl_delete_object(item->icon);
462                                 fl_free_object(item->icon);
463                         }
464                         item = tmp;
465                 }
466                 if (combox) {
467                         delete combox;
468                         combox = 0;
469                 }
470                 fl_unfreeze_form(owner->getForm());
471                 cleaned = true; // this is not completely true, but OK anyway
472         }
473         
474         // there exist some special actions not part of
475         // kb_action: SEPARATOR, LAYOUTS
476         char const ** pixmap = 0;
477         string help;
478
479         toolbarItem * newItem, * tmp;
480
481         if (lyxaction.isPseudoAction(action)) {
482                 string arg;
483                 kb_action act = static_cast<kb_action>(lyxaction.retrieveActionArg(action, arg));
484                 pixmap = getPixmap(act, arg);
485                 help = lyxaction.helpText(act);
486                 help += " ";
487                 help += arg;
488                 lyxerr.debug() << "Pseudo action " << action << endl;
489         } else {
490                 pixmap = getPixmap(static_cast<kb_action>(action));
491                 help = lyxaction.helpText(static_cast<kb_action>(action));
492         }
493         
494         // adds an item to the list
495         if (pixmap != 0
496             || action == TOOL_SEPARATOR
497             || action == TOOL_LAYOUTS)
498         {
499                 newItem = new toolbarItem;
500                 newItem->action = action;
501                 newItem->pixmap = pixmap;
502                 newItem->help = help;
503                 // the new item is placed at the end of the list
504                 tmp = toollist;
505                 if (tmp != 0){
506                         while(tmp->next != 0)
507                                 tmp = tmp->next;
508                         // here is tmp->next == 0
509                         tmp->next = newItem;
510                 } else
511                         toollist = newItem;
512         }
513         //if (action == TOOL_LAYOUTS) {
514         //      combox = new Combox(FL_COMBOX_DROPLIST);
515         //}
516 }
517
518
519 void Toolbar::add(string const & func, bool doclean)
520 {
521         int tf = lyxaction.LookupFunc(func.c_str());
522
523         if (tf == -1){
524                 lyxerr << "Toolbar::add: no LyX command called`"
525                        << func << "'exists!" << endl; 
526         } else {
527                 add(tf, doclean);
528         }
529 }
530
531
532 void Toolbar::clean()
533 {
534         toolbarItem * tmp = 0;
535         toolbarItem * item = toollist;
536
537         reset();
538
539         //now delete all the objects..
540         if (owner)
541                 fl_freeze_form(owner->getForm());
542         while (item) {
543                 tmp = item->next;
544                 delete item;
545                 item = tmp;
546         }
547         lyxerr[Debug::TOOLBAR] << "Combox: " << combox << endl;
548         if (combox) {
549                 delete combox;
550                 combox = 0;
551         }
552         if (owner)
553                 fl_unfreeze_form(owner->getForm());
554         lyxerr[Debug::TOOLBAR] << "toolbar cleaned" << endl;
555         cleaned = true;
556 }
557
558
559 void Toolbar::push(int nth)
560 {
561         lyxerr[Debug::TOOLBAR] << "Toolbar::push: trying to trigger no `"
562                                << nth << '\'' << endl;
563         
564         if (nth == 0) return;
565
566         int count = 0;
567         toolbarItem * tmp = toollist;
568         while (tmp) {
569                 count++;
570                 if (count == nth) {
571                         fl_trigger_object(tmp->icon);
572                         return;
573                 }
574                 tmp = tmp->next;
575         }
576         // item nth not found...
577         LyXBell();
578 }
579
580
581 void Toolbar::read(LyXLex & lex)
582 {
583         //consistency check
584         if (lex.GetString() != "\\begin_toolbar")
585                 lyxerr << "Toolbar::read: ERROR wrong token:`"
586                        << lex.GetString() << '\'' << endl;
587
588         clean();
589         string func;
590         bool quit = false;
591         
592         lex.pushTable(toolTags, TO_LAST - 1);
593
594         if (lyxerr.debugging(Debug::PARSER))
595                 lex.printTable();
596         
597         while (lex.IsOK() && !quit) {
598                 
599                 lyxerr[Debug::TOOLBAR] << "Toolbar::read: current lex text: `"
600                                        << lex.GetString() << '\'' << endl;
601
602                 switch(lex.lex()) {
603                   case TO_ADD:
604                           if (lex.EatLine()) {
605                                   func = lex.GetString();
606                                   lyxerr[Debug::TOOLBAR]
607                                           << "Toolbar::read TO_ADD func: `"
608                                           << func << "'" << endl;
609                                   add(func);
610                           }
611                           break;
612                    
613                   case TO_SEPARATOR:
614                           add(TOOL_SEPARATOR);
615                           break;
616                    
617                   case TO_LAYOUTS:
618                           add(TOOL_LAYOUTS);
619                           break;
620                    
621                   case TO_NEWLINE:
622                           add(TOOL_NEWLINE);
623                           break;
624                         
625                   case TO_ENDTOOLBAR:
626                           // should not set automatically
627                           //set();
628                           quit = true;
629                           break;
630                   default:
631                           lex.printError("Toolbar::read: "
632                                           "Unknown toolbar tag: `$$Token'");
633                           break;
634                 }
635         }
636         lex.popTable();
637 }