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