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