]> git.lyx.org Git - lyx.git/blob - src/toolbar.C
added a regex to be used when the system is missing one, use noinst instead of pkglib...
[lyx.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*/,
158                         int /*key*/, void */*xev*/)
159 {
160         string help = static_cast<char *>(ob->u_vdata);
161         Toolbar * t = reinterpret_cast<Toolbar*>(ob->u_ldata);
162         
163         if(event == FL_ENTER && !help.empty()){
164                 fl_set_object_callback(t->bubble_timer,
165                                        C_Toolbar_BubbleTimerCB,
166                                        reinterpret_cast<long>(ob));
167                 fl_set_timer(t->bubble_timer, 1);
168         }
169         else if(event != FL_MOTION){
170                 fl_set_timer(t->bubble_timer, 0);
171                 fl_hide_oneliner();
172         }
173         return 0;
174 }
175
176
177 extern "C" int C_Toolbar_BubblePost(FL_OBJECT * ob, int event,
178                                     FL_Coord /*mx*/, FL_Coord /*my*/, 
179                                     int key, void * xev)
180 {
181         return Toolbar::BubblePost(ob, event, 0, 0, key, xev);
182 }
183
184
185 void Toolbar::activate()
186 {
187         toolbarItem * tmp= 0;
188         toolbarItem * item = toollist;
189         while(item){
190                 tmp = item->next;
191                 if (item->icon) {
192                         fl_activate_object(item->icon);
193                 }
194                 item = tmp;
195         }
196 }
197
198
199 void Toolbar::deactivate()
200 {
201         toolbarItem * tmp= 0;
202         toolbarItem * item = toollist;
203         while(item){
204                 tmp = item->next;
205                 if (item->icon) {
206                         fl_deactivate_object(item->icon);
207                 }
208                 item = tmp;
209         }
210 }
211
212
213 void Toolbar::ToolbarCB(FL_OBJECT * ob, long ac)
214 {
215         Toolbar * t = reinterpret_cast<Toolbar*>(ob->u_ldata);
216         
217         string res = t->owner->getLyXFunc()->Dispatch(int(ac));
218         if(!res.empty())
219                 lyxerr[Debug::TOOLBAR] << res << endl;
220 }
221
222
223 extern "C" void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
224 {
225         Toolbar::ToolbarCB(ob, data);
226 }
227
228
229 int Toolbar::get_toolbar_func(string const & func)
230 {
231         int action = lyxaction.LookupFunc(func.c_str());
232         if (action == -1) {
233                 if (func == "separator"){
234                         action = TOOL_SEPARATOR;
235                 } else if (func == "layouts"){
236                         action = TOOL_LAYOUTS;
237                 } else action = 0;
238         }
239         return action;
240 }
241
242
243 void Toolbar::init()
244 {
245         add(TOOL_LAYOUTS);
246         add(LFUN_MENUOPEN);
247         //add(LFUN_CLOSEBUFFER);
248         add(LFUN_MENUWRITE);
249         add(LFUN_MENUPRINT);
250         add(TOOL_SEPARATOR);
251
252         add(LFUN_CUT);
253         add(LFUN_COPY);
254         add(LFUN_PASTE);
255         add(TOOL_SEPARATOR);
256         
257         add(LFUN_EMPH);
258         add(LFUN_NOUN);
259         add(LFUN_FREE);
260         add(TOOL_SEPARATOR);
261         
262         add(LFUN_FOOTMELT);
263         add(LFUN_MARGINMELT);
264         add(LFUN_DEPTH);
265         add(TOOL_SEPARATOR);
266
267         add(LFUN_TEX);
268         add(LFUN_MATH_MODE);
269         add(TOOL_SEPARATOR);
270
271         add(LFUN_FIGURE);
272         add(LFUN_TABLE);
273         //add(LFUN_MELT);
274 }
275
276
277 void Toolbar::set(bool doingmain)
278 {
279         // we shouldn't set if we have not cleaned
280         if (!cleaned) return;
281         
282         FL_OBJECT * obj;
283         toolbarItem * item = toollist;
284         
285         if (!doingmain) {
286                 fl_freeze_form(owner->getForm());
287                 fl_addto_form(owner->getForm());
288         }
289
290 #if FL_REVISION < 86
291         // Ensure borderwidth is 2 to get visual feedback
292         int bw = fl_get_border_width();
293         fl_set_border_width(-2);
294 #endif
295
296         // add the time if it don't exist
297         if (bubble_timer == 0)
298                 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
299                                             xpos, ypos, 0, 0, "Timer");
300         
301         while(item != 0) {
302                 switch(item->action){
303                 case TOOL_SEPARATOR:
304                         xpos += sepspace;
305                         item = item->next;
306                         break;
307                 case TOOL_LAYOUTS:
308                         xpos += standardspacing;
309                         if (!combox)
310                                 combox = new Combox(FL_COMBOX_DROPLIST);
311                         combox->add(xpos, ypos, 135, height, 300);
312                         combox->setcallback(LayoutsCB);
313                         combox->resize(FL_RESIZE_ALL);
314                         combox->gravity(NorthWestGravity, NorthWestGravity);
315                         item = item->next;
316                         xpos += 135;
317                         break;
318                 default:
319                         xpos += standardspacing;
320                         item->icon = obj = 
321                                 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
322                                                     xpos, ypos,
323                                                     buttonwidth,
324                                                     height, "");
325                         fl_set_object_boxtype(obj, FL_UP_BOX);
326                         fl_set_object_color(obj, FL_MCOL, FL_BLUE);
327                         fl_set_object_resize(obj, FL_RESIZE_ALL);
328                         fl_set_object_gravity(obj,
329                                               NorthWestGravity,
330                                               NorthWestGravity);
331                         fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
332                                                static_cast<long>(item->action));
333 #if FL_REVISION >85
334                         // Remove the blue feedback rectangle
335                         fl_set_pixmapbutton_focus_outline(obj, 0);
336 #endif
337
338                         // set the bubble-help (Matthias)
339 #ifdef WITH_WARNINGS
340 #warning This is dangerous!
341 #endif
342                         obj->u_vdata = const_cast<char*>(item->help.c_str());
343                         // we need to know what toolbar this item
344                         // belongs too. (Lgb)
345                         obj->u_ldata = reinterpret_cast<long>(this);
346                           
347                         fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
348
349                         fl_set_pixmapbutton_data(obj, const_cast<char**>(item->pixmap));
350                         item = item->next;
351                         // we must remember to update the positions
352                         xpos += buttonwidth;
353                         // ypos is constant
354                         /* Here will come a check to see if the new
355                          * pos is within the bounds of the main frame,
356                          * and perhaps wrap the toolbar if not.
357                          */
358                         break;
359                 }
360         }
361 #if FL_REVISION < 86
362         // Reset borderwidth to its default value.
363         fl_set_border_width(bw);
364 #endif
365         if (!doingmain) {
366                 fl_end_form();
367                 fl_unfreeze_form(owner->getForm());
368                 // Should be safe to do this here.
369                 owner->updateLayoutChoice();
370         }
371         
372         cleaned = false;
373 }
374
375
376 char const ** Toolbar::getPixmap(kb_action action, string const & arg)
377 {
378         char const ** pixmap = unknown_xpm; //0
379         switch(action){
380         case LFUN_MENUOPEN:    pixmap = open_xpm; break;
381         case LFUN_CLOSEBUFFER: pixmap = close_xpm; break;
382         case LFUN_MENUPRINT:   pixmap = print1_xpm; break;
383         case LFUN_MENUWRITE:   pixmap = save_xpm; break;
384         case LFUN_EMPH:  pixmap = emph_xpm; break;
385         case LFUN_NOUN:        pixmap = noun_xpm; break;
386         case LFUN_FREE:        pixmap = free_xpm; break;
387         case LFUN_FOOTMELT:    pixmap = foot_xpm; break;
388         case LFUN_DEPTH:       pixmap = depth_xpm; break;
389         case LFUN_COPY:        pixmap = copy_xpm; break;
390         case LFUN_CUT:         pixmap = cut_xpm; break;
391         case LFUN_PASTE:       pixmap = paste_xpm; break;
392         case LFUN_TEX:         pixmap = tex_xpm; break;
393         case LFUN_MATH_MODE:   pixmap = math_xpm; break;
394         case LFUN_MARGINMELT:  pixmap = margin_xpm; break;
395         case LFUN_FIGURE:      pixmap = fig_xpm; break;
396         case LFUN_TABLE:       pixmap = tab_xpm; break;
397         case LFUN_MELT:        pixmap = melt_xpm; break;
398         case LFUN_QUIT:        pixmap = quit_xpm; break;
399         case LFUN_RUNDVIPS:    pixmap = update_ps_xpm; break;
400         case LFUN_EXPORT:
401         {
402                 if (arg == "ascii")
403                         pixmap = make_ascii_xpm;
404                 else if (arg == "latex")
405                         pixmap = make_latex_xpm;
406         }
407         break; 
408         case LFUN_LAYOUT:
409         {
410                 if (arg == "Section")
411                         pixmap = layout_sec_xpm;
412                 else if (arg == "LaTeX")
413                         pixmap = layout_latex_xpm;
414                 else if (arg == "LyX-Code")
415                         pixmap = layout_code_xpm;
416                 else if (arg == "Scrap")
417                         pixmap = layout_scrap_xpm;
418                 else
419                         pixmap = layout_std_xpm;
420         }
421         break;
422
423         case LFUN_BOLD : pixmap = bold_xpm; break; 
424         case LFUN_SANS: pixmap = sans_xpm; break; 
425         case LFUN_RUNLATEX: pixmap = run_latex_xpm; break; 
426         case LFUN_BUILDPROG: pixmap = build_xpm; break; 
427         case LFUN_PREVIEWPS: pixmap = view_ps_xpm; break; 
428         case LFUN_PREVIEW: pixmap = view_dvi_xpm; break; 
429         case LFUN_INSERT_MATH:
430         {
431                 if (!arg.empty())
432                         pixmap = get_pixmap_from_symbol(arg.c_str(),
433                                                         buttonwidth,
434                                                         height);
435         }
436         break;
437         default:
438                 //pixmap = unknown_xpm;
439                 break;
440         }
441         return pixmap;
442 }
443
444
445 void Toolbar::add(int action, bool doclean)
446 {
447         if (doclean && !cleaned) clean();
448
449         // this is what we do if we want to add to an existing
450         // toolbar.
451         if (!doclean && owner) {
452                 // first «hide» the toolbar buttons. This is not a real hide
453                 // actually it deletes and frees the button altogether.
454                 lyxerr << "Toolbar::add: «hide» the toolbar buttons." << endl;
455                 toolbarItem * tmp= 0;
456                 toolbarItem * item = toollist;
457
458                 lightReset();
459                 
460                 fl_freeze_form(owner->getForm());
461                 while(item){
462                         tmp = item->next;
463                         if (item->icon) {
464                                 fl_delete_object(item->icon);
465                                 fl_free_object(item->icon);
466                         }
467                         item = tmp;
468                 }
469                 if (combox) {
470                         delete combox;
471                         combox = 0;
472                 }
473                 fl_unfreeze_form(owner->getForm());
474                 cleaned = true; // this is not completely true, but OK anyway
475         }
476         
477         // there exist some special actions not part of
478         // kb_action: SEPARATOR, LAYOUTS
479         char const ** pixmap = 0;
480         string help;
481
482         toolbarItem * newItem, * tmp;
483
484         if (lyxaction.isPseudoAction(action)) {
485                 string arg;
486                 kb_action act = static_cast<kb_action>
487                         (lyxaction.retrieveActionArg(action, arg));
488                 pixmap = getPixmap(act, arg);
489                 help = lyxaction.helpText(act);
490                 help += " ";
491                 help += arg;
492                 lyxerr.debug() << "Pseudo action " << action << endl;
493         } else {
494                 pixmap = getPixmap(static_cast<kb_action>(action));
495                 help = lyxaction.helpText(static_cast<kb_action>(action));
496         }
497         
498         // adds an item to the list
499         if (pixmap != 0
500             || action == TOOL_SEPARATOR
501             || action == TOOL_LAYOUTS)
502                 {
503                         newItem = new toolbarItem;
504                         newItem->action = action;
505                         newItem->pixmap = pixmap;
506                         newItem->help = help;
507                         // the new item is placed at the end of the list
508                         tmp = toollist;
509                         if (tmp != 0){
510                                 while(tmp->next != 0)
511                                         tmp = tmp->next;
512                                 // here is tmp->next == 0
513                                 tmp->next = newItem;
514                         } else
515                                 toollist = newItem;
516                 }
517         //if (action == TOOL_LAYOUTS) {
518         //      combox = new Combox(FL_COMBOX_DROPLIST);
519         //}
520 }
521
522
523 void Toolbar::add(string const & func, bool doclean)
524 {
525         int tf = lyxaction.LookupFunc(func.c_str());
526
527         if (tf == -1){
528                 lyxerr << "Toolbar::add: no LyX command called`"
529                        << func << "'exists!" << endl; 
530         } else {
531                 add(tf, doclean);
532         }
533 }
534
535
536 void Toolbar::clean()
537 {
538         toolbarItem * tmp = 0;
539         toolbarItem * item = toollist;
540
541         reset();
542
543         //now delete all the objects..
544         if (owner)
545                 fl_freeze_form(owner->getForm());
546         while (item) {
547                 tmp = item->next;
548                 delete item;
549                 item = tmp;
550         }
551         lyxerr[Debug::TOOLBAR] << "Combox: " << combox << endl;
552         if (combox) {
553                 delete combox;
554                 combox = 0;
555         }
556         if (owner)
557                 fl_unfreeze_form(owner->getForm());
558         lyxerr[Debug::TOOLBAR] << "toolbar cleaned" << endl;
559         cleaned = true;
560 }
561
562
563 void Toolbar::push(int nth)
564 {
565         lyxerr[Debug::TOOLBAR] << "Toolbar::push: trying to trigger no `"
566                                << nth << '\'' << endl;
567         
568         if (nth == 0) return;
569
570         int count = 0;
571         toolbarItem * tmp = toollist;
572         while (tmp) {
573                 ++count;
574                 if (count == nth) {
575                         fl_trigger_object(tmp->icon);
576                         return;
577                 }
578                 tmp = tmp->next;
579         }
580         // item nth not found...
581         LyXBell();
582 }
583
584
585 void Toolbar::read(LyXLex & lex)
586 {
587         //consistency check
588         if (lex.GetString() != "\\begin_toolbar")
589                 lyxerr << "Toolbar::read: ERROR wrong token:`"
590                        << lex.GetString() << '\'' << endl;
591
592         clean();
593         string func;
594         bool quit = false;
595         
596         lex.pushTable(toolTags, TO_LAST - 1);
597
598         if (lyxerr.debugging(Debug::PARSER))
599                 lex.printTable();
600         
601         while (lex.IsOK() && !quit) {
602                 
603                 lyxerr[Debug::TOOLBAR] << "Toolbar::read: current lex text: `"
604                                        << lex.GetString() << '\'' << endl;
605
606                 switch(lex.lex()) {
607                 case TO_ADD:
608                         if (lex.EatLine()) {
609                                 func = lex.GetString();
610                                 lyxerr[Debug::TOOLBAR]
611                                         << "Toolbar::read TO_ADD func: `"
612                                         << func << "'" << endl;
613                                 add(func);
614                         }
615                         break;
616                    
617                 case TO_SEPARATOR:
618                         add(TOOL_SEPARATOR);
619                         break;
620                    
621                 case TO_LAYOUTS:
622                         add(TOOL_LAYOUTS);
623                         break;
624                    
625                 case TO_NEWLINE:
626                         add(TOOL_NEWLINE);
627                         break;
628                         
629                 case TO_ENDTOOLBAR:
630                         // should not set automatically
631                         //set();
632                         quit = true;
633                         break;
634                 default:
635                         lex.printError("Toolbar::read: "
636                                        "Unknown toolbar tag: `$$Token'");
637                         break;
638                 }
639         }
640         lex.popTable();
641 }