]> git.lyx.org Git - features.git/blob - src/toolbar.C
29f9ceca1b0c53942403055822ee72e86a0cb6bf
[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-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 "support/filetools.h"
34 #include "lyxrc.h"
35
36 using std::endl;
37
38 // this one is not "C" because combox callbacks are really C++ %-|
39 extern void LayoutsCB(int, void *);
40 extern char const ** get_pixmap_from_symbol(char const * arg, int, int);
41 extern LyXAction lyxaction;
42
43
44 Toolbar::toolbarItem::toolbarItem()
45 {
46         action = LFUN_NOACTION;
47         icon = 0;
48 }
49
50
51 void Toolbar::toolbarItem::clean()
52 {
53         if (icon) {
54                 fl_delete_object(icon);
55                 fl_free_object(icon);
56                 icon = 0;
57         }
58 }
59
60
61 Toolbar::toolbarItem::~toolbarItem()
62 {
63         clean();
64 }
65
66
67 Toolbar::toolbarItem &
68 Toolbar::toolbarItem::operator=(Toolbar::toolbarItem const & ti)
69 {
70         // do we have to check icon and IsBitmap too?
71         action = ti.action;
72         icon = 0; // locally we need to get the icon anew
73         
74         return *this;
75 }
76
77
78 Toolbar::Toolbar(LyXView * o, int x, int y)
79         : owner(o), sxpos(x), sypos(y)
80 {
81         combox = 0;
82 #if FL_REVISION < 89
83         bubble_timer = 0;
84 #endif
85         reset();
86
87         // extracts the default toolbar actions from LyXRC
88         for (ToolbarDefaults::const_iterator cit =
89                      lyxrc.toolbardefaults.begin();
90              cit != lyxrc.toolbardefaults.end(); ++cit) {
91                 add((*cit));
92                 lyxerr[Debug::TOOLBAR] << "tool action: "
93                                        << (*cit) << endl;
94         }
95 }
96
97
98 #if FL_REVISION < 89
99 // timer-cb for bubble-help (Matthias)
100 void Toolbar::BubbleTimerCB(FL_OBJECT *, long data)
101 {
102         FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
103         // The trick we use to get the help text is to read the
104         // argument of the callback that has been registered for
105         // ToolBarCB.  (JMarc)
106         string help = lyxaction.helpText(ob->argument);
107         fl_show_oneliner(help.c_str(), ob->form->x + ob->x,
108                          ob->form->y + ob->y + ob->h);
109 }
110
111
112 extern "C" void C_Toolbar_BubbleTimerCB(FL_OBJECT * ob, long data)
113 {
114         Toolbar::BubbleTimerCB(ob, data);
115 }
116
117
118 // post_handler for bubble-help (Matthias)
119 int Toolbar::BubblePost(FL_OBJECT *ob, int event,
120                         FL_Coord /*mx*/, FL_Coord /*my*/,
121                         int /*key*/, void */*xev*/)
122 {
123         Toolbar * t = reinterpret_cast<Toolbar*>(ob->u_vdata);
124         
125         // We do not test for empty help here, since this can never happen
126         if(event == FL_ENTER){
127                 fl_set_object_callback(t->bubble_timer,
128                                        C_Toolbar_BubbleTimerCB,
129                                        reinterpret_cast<long>(ob));
130                 fl_set_timer(t->bubble_timer, 1);
131         }
132         else if(event != FL_MOTION){
133                 fl_set_timer(t->bubble_timer, 0);
134                 fl_hide_oneliner();
135         }
136         return 0;
137 }
138
139
140 extern "C" int C_Toolbar_BubblePost(FL_OBJECT * ob, int event,
141                                     FL_Coord /*mx*/, FL_Coord /*my*/, 
142                                     int key, void * xev)
143 {
144         return Toolbar::BubblePost(ob, event, 0, 0, key, xev);
145 }
146 #endif
147
148
149 void Toolbar::activate()
150 {
151         ToolbarList::const_iterator p = toollist.begin();
152         for (; p != toollist.end(); ++p) {
153                 if (p->icon) {
154                         fl_activate_object(p->icon);
155                 }
156         }
157 }
158
159
160 void Toolbar::deactivate()
161 {
162         ToolbarList::const_iterator p = toollist.begin();
163         for (; p != toollist.end(); ++p) {
164                 if (p->icon) {
165                         fl_deactivate_object(p->icon);
166                 }
167         }
168 }
169
170 void Toolbar::update()
171 {
172         ToolbarList::const_iterator p = toollist.begin();
173         for (; p != toollist.end(); ++p) {
174                 if (p->icon) {
175                         int status = owner->getLyXFunc()->getStatus(p->action);
176                         if (status & LyXFunc::ToggleOn) {
177                                 // I'd like to use a different color
178                                 // here, but then the problem is to
179                                 // know how to use transparency with
180                                 // Xpm library. It seems pretty
181                                 // complicated to me (JMarc)
182                                 fl_set_object_color(p->icon, FL_LEFT_BCOL, FL_BLUE);
183                                 fl_set_object_boxtype(p->icon, FL_DOWN_BOX);
184                         } else {
185                                 fl_set_object_color(p->icon, FL_MCOL, FL_BLUE);
186                                 fl_set_object_boxtype(p->icon, FL_UP_BOX);
187                         }
188
189                         if (status & LyXFunc::Disabled) {
190                                 // Is there a way here to specify a
191                                 // mask in order to show that the
192                                 // button is disabled? (JMarc)
193                                 fl_deactivate_object(p->icon);
194                         }
195                         else
196                                 fl_activate_object(p->icon);
197                 }
198         }
199 }
200
201
202 void Toolbar::ToolbarCB(FL_OBJECT * ob, long ac)
203 {
204         Toolbar * t = static_cast<Toolbar*>(ob->u_vdata);
205         
206         string res = t->owner->getLyXFunc()->Dispatch(int(ac));
207         if(!res.empty())
208                 lyxerr[Debug::TOOLBAR] << res << endl;
209 }
210
211
212 extern "C" void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
213 {
214         Toolbar::ToolbarCB(ob, data);
215 }
216
217
218 int Toolbar::get_toolbar_func(string const & func)
219 {
220         int action = lyxaction.LookupFunc(func.c_str());
221         if (action == -1) {
222                 if (func == "separator"){
223                         action = TOOL_SEPARATOR;
224                 } else if (func == "layouts"){
225                         action = TOOL_LAYOUTS;
226                 } else action = 0;
227         }
228         return action;
229 }
230
231 static
232 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height) {
233         string name, arg, xpm_name;
234         kb_action act;
235
236         if (lyxaction.isPseudoAction(action)) {
237                 lyxerr[Debug::TOOLBAR] << "Pseudo action " << action << endl;
238
239                 act = lyxaction.retrieveActionArg(action, arg);
240                 name = lyxaction.getActionName(act);
241                 xpm_name = subst(name + ' ' + arg, ' ','_');
242         } else {
243                 act = (kb_action)action;
244                 name = lyxaction.getActionName(action);
245                 xpm_name = name;
246         }
247
248         lyxerr[Debug::TOOLBAR] << "Icon name for action " << action
249                                << " is `" << xpm_name << "'" << endl;
250         
251         string fullname = LibFileSearch("images", xpm_name, "xpm");
252
253         if (!fullname.empty()) {
254                 lyxerr[Debug::TOOLBAR] << "Full icon name is `" 
255                                        << fullname << "'" << endl;
256                 fl_set_pixmapbutton_file(obj, fullname.c_str());
257                 return;
258         }
259
260         if (act == LFUN_INSERT_MATH && !arg.empty()) {
261                 lyxerr[Debug::TOOLBAR] << "Using mathed-provided icon" << endl;
262                 char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
263                                                         buttonwidth,
264                                                         height);
265                 fl_set_pixmapbutton_data(obj, const_cast<char **>(pixmap));
266                 return;
267         }
268         
269         lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
270         fullname = LibFileSearch("images", "unknown", "xpm");
271         if (!fullname.empty()) {
272                 lyxerr[Debug::TOOLBAR] << "Using default `unknown' icon" 
273                                        << endl;
274                 fl_set_pixmapbutton_file(obj, fullname.c_str());
275         }
276 }
277
278 void Toolbar::set(bool doingmain)
279 {
280         // we shouldn't set if we have not cleaned
281         if (!cleaned) return;
282         
283         FL_OBJECT * obj;
284         
285         if (!doingmain) {
286                 fl_freeze_form(owner->getForm());
287                 fl_addto_form(owner->getForm());
288         }
289
290 #if FL_REVISION < 89
291         // add the time if it don't exist
292         if (bubble_timer == 0)
293                 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
294                                             xpos, ypos, 0, 0, "Timer");
295 #endif
296         
297         ToolbarList::iterator item = toollist.begin();
298         for (; item != toollist.end(); ++item) {
299                 switch(item->action){
300                 case TOOL_SEPARATOR:
301                         xpos += sepspace;
302                         break;
303                 case TOOL_LAYOUTS:
304                         xpos += standardspacing;
305                         if (!combox)
306                                 combox = new Combox(FL_COMBOX_DROPLIST);
307                         combox->add(xpos, ypos, 135, height, 400);
308                         combox->setcallback(LayoutsCB);
309                         combox->resize(FL_RESIZE_ALL);
310                         combox->gravity(NorthWestGravity, NorthWestGravity);
311                         xpos += 135;
312                         break;
313                 default:
314                         xpos += standardspacing;
315                         item->icon = obj = 
316                                 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
317                                                     xpos, ypos,
318                                                     buttonwidth,
319                                                     height, "");
320                         fl_set_object_resize(obj, FL_RESIZE_ALL);
321                         fl_set_object_gravity(obj,
322                                               NorthWestGravity,
323                                               NorthWestGravity);
324                         fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
325                                                static_cast<long>(item->action));
326                         // Remove the blue feedback rectangle
327                         fl_set_pixmapbutton_focus_outline(obj, 0);
328
329                         // Set the tooltip
330 #if FL_REVISION >= 89
331                         string help = lyxaction.helpText(item->action);
332                         fl_set_object_helper(obj, help.c_str());        
333 #else
334                         fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
335 #endif
336
337                         // The toolbar that this object belongs too.
338                         obj->u_vdata = this;
339
340                         setPixmap(obj, item->action, buttonwidth, height);
341                         // we must remember to update the positions
342                         xpos += buttonwidth;
343                         // ypos is constant
344                         /* Here will come a check to see if the new
345                          * pos is within the bounds of the main frame,
346                          * and perhaps wrap the toolbar if not.
347                          */
348                         break;
349                 }
350         }
351
352         if (!doingmain) {
353                 fl_end_form();
354                 fl_unfreeze_form(owner->getForm());
355                 // Should be safe to do this here.
356                 owner->updateLayoutChoice();
357         }
358
359         // set the state of the icons
360         //update();
361
362         cleaned = false;
363 }
364
365
366 void Toolbar::add(int action, bool doclean)
367 {
368         if (doclean && !cleaned) clean();
369
370         // this is what we do if we want to add to an existing
371         // toolbar.
372         if (!doclean && owner) {
373                 // first "hide" the toolbar buttons. This is not a real hide
374                 // actually it deletes and frees the button altogether.
375                 lyxerr << "Toolbar::add: \"hide\" the toolbar buttons." 
376                        << endl;
377
378                 lightReset();
379                 
380                 fl_freeze_form(owner->getForm());
381
382                 ToolbarList::iterator p = toollist.begin();
383                 for (; p != toollist.end(); ++p) {
384                         p->clean();
385                 }
386
387                 if (combox) {
388                         delete combox;
389                         combox = 0;
390                 }
391                 fl_unfreeze_form(owner->getForm());
392                 cleaned = true; // this is not completely true, but OK anyway
393         }
394         
395         // there exist some special actions not part of
396         // kb_action: SEPARATOR, LAYOUTS
397
398         toolbarItem newItem;
399         newItem.action = action;
400         toollist.push_back(newItem);
401 }
402
403
404 void Toolbar::clean()
405 {
406         //reset(); // I do not understand what this reset() is, anyway
407
408         //now delete all the objects..
409         if (owner)
410                 fl_freeze_form(owner->getForm());
411
412         // G++ vector does not have clear defined
413         //toollist.clear();
414         toollist.erase(toollist.begin(), toollist.end());
415
416         lyxerr[Debug::TOOLBAR] << "Combox: " << combox << endl;
417         if (combox) {
418                 delete combox;
419                 combox = 0;
420         }
421
422         if (owner)
423                 fl_unfreeze_form(owner->getForm());
424         lyxerr[Debug::TOOLBAR] << "toolbar cleaned" << endl;
425         cleaned = true;
426 }
427
428
429 void Toolbar::push(int nth)
430 {
431         lyxerr[Debug::TOOLBAR] << "Toolbar::push: trying to trigger no `"
432                                << nth << '\'' << endl;
433         
434         if (nth <= 0 || nth >= int(toollist.size())) {
435                 // item nth not found...
436                 LyXBell();
437                 return;
438         }
439
440         fl_trigger_object(toollist[nth - 1].icon);
441 }
442
443
444 void Toolbar::add(string const & func, bool doclean)
445 {
446         int tf = lyxaction.LookupFunc(func);
447
448         if (tf == -1) {
449                 lyxerr << "Toolbar::add: no LyX command called`"
450                        << func << "'exists!" << endl; 
451         } else {
452                 add(tf, doclean);
453         }
454 }
455
456
457 void Toolbar::reset() 
458 {
459         //toollist = 0; // what is this supposed to do?
460         cleaned = false;
461         lightReset();
462 }
463
464 //  void Toolbar::lightReset()
465 //  {
466 //      standardspacing = 2; // the usual space between items
467 //      sepspace = 6; // extra space
468 //      xpos = sxpos - standardspacing;
469 //      ypos = sypos;
470 //      buttonwidth = 30; // the standard button width
471 //      height = 30; // the height of all items in the toolbar
472 //  }