]> git.lyx.org Git - lyx.git/blob - src/toolbar.C
make text private in LyXParagraph some changes to import ascii file
[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-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 "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 #include "lyxrc.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
97 // this one is not "C" because combox callbacks are really C++ %-|
98 extern void LayoutsCB(int, void *);
99 extern char const ** get_pixmap_from_symbol(char const * arg, int, int);
100 extern LyXAction lyxaction;
101
102
103 Toolbar::Toolbar(LyXView * o, int x, int y)
104         : owner(o), sxpos(x), sypos(y)
105 {
106         combox = 0;
107 #if FL_REVISION < 89
108         bubble_timer = 0;
109 #endif
110         reset();
111
112         // extracts the default toolbar actions from LyXRC
113         for (ToolbarDefaults::const_iterator cit =
114                      lyxrc.toolbardefaults.begin();
115              cit != lyxrc.toolbardefaults.end(); ++cit) {
116                 add((*cit));
117                 lyxerr[Debug::TOOLBAR] << "tool action: "
118                                        << (*cit) << endl;
119         }
120 }
121
122
123 #if FL_REVISION < 89
124 // timer-cb for bubble-help (Matthias)
125 void Toolbar::BubbleTimerCB(FL_OBJECT *, long data)
126 {
127         FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
128         char * help = static_cast<char*>(ob->u_vdata);
129         fl_show_oneliner(help, ob->form->x + ob->x,
130                          ob->form->y + ob->y + ob->h);
131 }
132
133
134 extern "C" void C_Toolbar_BubbleTimerCB(FL_OBJECT * ob, long data)
135 {
136         Toolbar::BubbleTimerCB(ob, data);
137 }
138
139
140 // post_handler for bubble-help (Matthias)
141 int Toolbar::BubblePost(FL_OBJECT *ob, int event,
142                         FL_Coord /*mx*/, FL_Coord /*my*/,
143                         int /*key*/, void */*xev*/)
144 {
145         string help = static_cast<char *>(ob->u_vdata);
146         Toolbar * t = reinterpret_cast<Toolbar*>(ob->u_ldata);
147         
148         if(event == FL_ENTER && !help.empty()){
149                 fl_set_object_callback(t->bubble_timer,
150                                        C_Toolbar_BubbleTimerCB,
151                                        reinterpret_cast<long>(ob));
152                 fl_set_timer(t->bubble_timer, 1);
153         }
154         else if(event != FL_MOTION){
155                 fl_set_timer(t->bubble_timer, 0);
156                 fl_hide_oneliner();
157         }
158         return 0;
159 }
160
161
162 extern "C" int C_Toolbar_BubblePost(FL_OBJECT * ob, int event,
163                                     FL_Coord /*mx*/, FL_Coord /*my*/, 
164                                     int key, void * xev)
165 {
166         return Toolbar::BubblePost(ob, event, 0, 0, key, xev);
167 }
168 #endif
169
170
171 void Toolbar::activate()
172 {
173         toolbarItem * tmp= 0;
174         toolbarItem * item = toollist;
175         while(item){
176                 tmp = item->next;
177                 if (item->icon) {
178                         fl_activate_object(item->icon);
179                 }
180                 item = tmp;
181         }
182 }
183
184
185 void Toolbar::deactivate()
186 {
187         toolbarItem * tmp= 0;
188         toolbarItem * item = toollist;
189         while(item){
190                 tmp = item->next;
191                 if (item->icon) {
192                         fl_deactivate_object(item->icon);
193                 }
194                 item = tmp;
195         }
196 }
197
198
199 void Toolbar::ToolbarCB(FL_OBJECT * ob, long ac)
200 {
201 #if FL_REVISION >= 89
202         Toolbar * t = static_cast<Toolbar*>(ob->u_vdata);
203 #else
204         Toolbar * t = reinterpret_cast<Toolbar*>(ob->u_ldata);
205 #endif
206         
207         string res = t->owner->getLyXFunc()->Dispatch(int(ac));
208         if(!res.empty())
209                 lyxerr[Debug::TOOLBAR] << res << endl;
210 }
211
212
213 extern "C" void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
214 {
215         Toolbar::ToolbarCB(ob, data);
216 }
217
218
219 int Toolbar::get_toolbar_func(string const & func)
220 {
221         int action = lyxaction.LookupFunc(func.c_str());
222         if (action == -1) {
223                 if (func == "separator"){
224                         action = TOOL_SEPARATOR;
225                 } else if (func == "layouts"){
226                         action = TOOL_LAYOUTS;
227                 } else action = 0;
228         }
229         return action;
230 }
231
232
233 void Toolbar::set(bool doingmain)
234 {
235         // we shouldn't set if we have not cleaned
236         if (!cleaned) return;
237         
238         FL_OBJECT * obj;
239         toolbarItem * item = toollist;
240         
241         if (!doingmain) {
242                 fl_freeze_form(owner->getForm());
243                 fl_addto_form(owner->getForm());
244         }
245
246 #if FL_REVISION < 89
247         // add the time if it don't exist
248         if (bubble_timer == 0)
249                 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
250                                             xpos, ypos, 0, 0, "Timer");
251 #endif
252         
253         while(item != 0) {
254                 switch(item->action){
255                 case TOOL_SEPARATOR:
256                         xpos += sepspace;
257                         item = item->next;
258                         break;
259                 case TOOL_LAYOUTS:
260                         xpos += standardspacing;
261                         if (!combox)
262                                 combox = new Combox(FL_COMBOX_DROPLIST);
263                         combox->add(xpos, ypos, 135, height, 400);
264                         combox->setcallback(LayoutsCB);
265                         combox->resize(FL_RESIZE_ALL);
266                         combox->gravity(NorthWestGravity, NorthWestGravity);
267                         item = item->next;
268                         xpos += 135;
269                         break;
270                 default:
271                         xpos += standardspacing;
272                         item->icon = obj = 
273                                 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
274                                                     xpos, ypos,
275                                                     buttonwidth,
276                                                     height, "");
277                         fl_set_object_boxtype(obj, FL_UP_BOX);
278                         fl_set_object_color(obj, FL_MCOL, FL_BLUE);
279                         fl_set_object_resize(obj, FL_RESIZE_ALL);
280                         fl_set_object_gravity(obj,
281                                               NorthWestGravity,
282                                               NorthWestGravity);
283                         fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
284                                                static_cast<long>(item->action));
285                         // Remove the blue feedback rectangle
286                         fl_set_pixmapbutton_focus_outline(obj, 0);
287
288                         // set the bubble-help (Matthias)
289 #if FL_REVISION >= 89
290                         // Set the tooltip
291                         fl_set_object_helper(obj, item->help.c_str());
292                         // The toolbar that this object belongs too.
293                         obj->u_vdata = this;
294                         
295                         
296 #else
297 #ifdef WITH_WARNINGS
298 #warning This is dangerous!
299 #endif
300                         obj->u_vdata = const_cast<char*>(item->help.c_str());
301                         // we need to know what toolbar this item
302                         // belongs too. (Lgb)
303                         obj->u_ldata = reinterpret_cast<long>(this);
304                           
305                         fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
306 #endif
307
308                         fl_set_pixmapbutton_data(obj, const_cast<char**>(item->pixmap));
309                         item = item->next;
310                         // we must remember to update the positions
311                         xpos += buttonwidth;
312                         // ypos is constant
313                         /* Here will come a check to see if the new
314                          * pos is within the bounds of the main frame,
315                          * and perhaps wrap the toolbar if not.
316                          */
317                         break;
318                 }
319         }
320
321         if (!doingmain) {
322                 fl_end_form();
323                 fl_unfreeze_form(owner->getForm());
324                 // Should be safe to do this here.
325                 owner->updateLayoutChoice();
326         }
327         
328         cleaned = false;
329 }
330
331
332 char const ** Toolbar::getPixmap(kb_action action, string const & arg)
333 {
334         char const ** pixmap = unknown_xpm; //0
335         switch(action){
336         case LFUN_MENUOPEN:    pixmap = open_xpm; break;
337         case LFUN_CLOSEBUFFER: pixmap = close_xpm; break;
338         case LFUN_MENUPRINT:   pixmap = print1_xpm; break;
339         case LFUN_MENUWRITE:   pixmap = save_xpm; break;
340         case LFUN_EMPH:  pixmap = emph_xpm; break;
341         case LFUN_NOUN:        pixmap = noun_xpm; break;
342         case LFUN_FREE:        pixmap = free_xpm; break;
343         case LFUN_FOOTMELT:    pixmap = foot_xpm; break;
344         case LFUN_DEPTH:       pixmap = depth_xpm; break;
345         case LFUN_COPY:        pixmap = copy_xpm; break;
346         case LFUN_CUT:         pixmap = cut_xpm; break;
347         case LFUN_PASTE:       pixmap = paste_xpm; break;
348         case LFUN_TEX:         pixmap = tex_xpm; break;
349         case LFUN_MATH_MODE:   pixmap = math_xpm; break;
350         case LFUN_MARGINMELT:  pixmap = margin_xpm; break;
351         case LFUN_FIGURE:      pixmap = fig_xpm; break;
352         case LFUN_TABLE:       pixmap = tab_xpm; break;
353         case LFUN_MELT:        pixmap = melt_xpm; break;
354         case LFUN_QUIT:        pixmap = quit_xpm; break;
355         case LFUN_RUNDVIPS:    pixmap = update_ps_xpm; break;
356         case LFUN_EXPORT:
357         {
358                 if (arg == "ascii")
359                         pixmap = make_ascii_xpm;
360                 else if (arg == "latex")
361                         pixmap = make_latex_xpm;
362         }
363         break; 
364         case LFUN_LAYOUT:
365         {
366                 if (arg == "Section")
367                         pixmap = layout_sec_xpm;
368                 else if (arg == "LaTeX")
369                         pixmap = layout_latex_xpm;
370                 else if (arg == "LyX-Code")
371                         pixmap = layout_code_xpm;
372                 else if (arg == "Scrap")
373                         pixmap = layout_scrap_xpm;
374                 else
375                         pixmap = layout_std_xpm;
376         }
377         break;
378
379         case LFUN_BOLD : pixmap = bold_xpm; break; 
380         case LFUN_SANS: pixmap = sans_xpm; break; 
381         case LFUN_RUNLATEX: pixmap = run_latex_xpm; break; 
382         case LFUN_BUILDPROG: pixmap = build_xpm; break; 
383         case LFUN_PREVIEWPS: pixmap = view_ps_xpm; break; 
384         case LFUN_PREVIEW: pixmap = view_dvi_xpm; break; 
385         case LFUN_INSERT_MATH:
386         {
387                 if (!arg.empty())
388                         pixmap = get_pixmap_from_symbol(arg.c_str(),
389                                                         buttonwidth,
390                                                         height);
391         }
392         break;
393         default:
394                 //pixmap = unknown_xpm;
395                 break;
396         }
397         return pixmap;
398 }
399
400
401 void Toolbar::add(int action, bool doclean)
402 {
403         if (doclean && !cleaned) clean();
404
405         // this is what we do if we want to add to an existing
406         // toolbar.
407         if (!doclean && owner) {
408                 // first «hide» the toolbar buttons. This is not a real hide
409                 // actually it deletes and frees the button altogether.
410                 lyxerr << "Toolbar::add: «hide» the toolbar buttons." << endl;
411                 toolbarItem * tmp= 0;
412                 toolbarItem * item = toollist;
413
414                 lightReset();
415                 
416                 fl_freeze_form(owner->getForm());
417                 while(item){
418                         tmp = item->next;
419                         if (item->icon) {
420                                 fl_delete_object(item->icon);
421                                 fl_free_object(item->icon);
422                         }
423                         item = tmp;
424                 }
425                 if (combox) {
426                         delete combox;
427                         combox = 0;
428                 }
429                 fl_unfreeze_form(owner->getForm());
430                 cleaned = true; // this is not completely true, but OK anyway
431         }
432         
433         // there exist some special actions not part of
434         // kb_action: SEPARATOR, LAYOUTS
435         char const ** pixmap = 0;
436         string help;
437
438         toolbarItem * newItem, * tmp;
439
440         if (lyxaction.isPseudoAction(action)) {
441                 string arg;
442                 kb_action act = static_cast<kb_action>
443                         (lyxaction.retrieveActionArg(action, arg));
444                 pixmap = getPixmap(act, arg);
445                 help = lyxaction.helpText(act);
446                 help += " ";
447                 help += arg;
448                 lyxerr.debug() << "Pseudo action " << action << endl;
449         } else {
450                 pixmap = getPixmap(static_cast<kb_action>(action));
451                 help = lyxaction.helpText(static_cast<kb_action>(action));
452         }
453         
454         // adds an item to the list
455         if (pixmap != 0
456             || action == TOOL_SEPARATOR
457             || action == TOOL_LAYOUTS) {
458                 newItem = new toolbarItem;
459                 newItem->action = action;
460                 newItem->pixmap = pixmap;
461                 newItem->help = help;
462                 // the new item is placed at the end of the list
463                 tmp = toollist;
464                 if (tmp != 0){
465                         while(tmp->next != 0)
466                                 tmp = tmp->next;
467                                 // here is tmp->next == 0
468                         tmp->next = newItem;
469                 } else
470                         toollist = newItem;
471         }
472 }
473
474
475 void Toolbar::clean()
476 {
477         toolbarItem * tmp = 0;
478         toolbarItem * item = toollist;
479
480         reset();
481
482         //now delete all the objects..
483         if (owner)
484                 fl_freeze_form(owner->getForm());
485         while (item) {
486                 tmp = item->next;
487                 delete item;
488                 item = tmp;
489         }
490         lyxerr[Debug::TOOLBAR] << "Combox: " << combox << endl;
491         if (combox) {
492                 delete combox;
493                 combox = 0;
494         }
495         if (owner)
496                 fl_unfreeze_form(owner->getForm());
497         lyxerr[Debug::TOOLBAR] << "toolbar cleaned" << endl;
498         cleaned = true;
499 }
500
501
502 void Toolbar::push(int nth)
503 {
504         lyxerr[Debug::TOOLBAR] << "Toolbar::push: trying to trigger no `"
505                                << nth << '\'' << endl;
506         
507         if (nth == 0) return;
508
509         int count = 0;
510         toolbarItem * tmp = toollist;
511         while (tmp) {
512                 ++count;
513                 if (count == nth) {
514                         fl_trigger_object(tmp->icon);
515                         return;
516                 }
517                 tmp = tmp->next;
518         }
519         // item nth not found...
520         LyXBell();
521 }
522
523
524 void Toolbar::add(string const & func, bool doclean)
525 {
526         int tf = lyxaction.LookupFunc(func);
527
528         if (tf == -1) {
529                 lyxerr << "Toolbar::add: no LyX command called`"
530                        << func << "'exists!" << endl; 
531         } else {
532                 add(tf, doclean);
533         }
534 }