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