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