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