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