]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Toolbar_pimpl.C
07406dbc98c41d55857b7d626ee87b93d48bcfd5
[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         ToolbarList::const_iterator end = toollist.end();
150         for (; p != end; ++p) {
151                 if (p->icon) {
152                         fl_activate_object(p->icon);
153                 }
154         }
155 }
156
157
158 void Toolbar::Pimpl::deactivate()
159 {
160         ToolbarList::const_iterator p = toollist.begin();
161         ToolbarList::const_iterator end = toollist.end();
162         for (; p != end; ++p) {
163                 if (p->icon) {
164                         fl_deactivate_object(p->icon);
165                 }
166         }
167 }
168
169 void Toolbar::Pimpl::update()
170 {
171         ToolbarList::const_iterator p = toollist.begin();
172         ToolbarList::const_iterator end = toollist.end();
173         for (; p != 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::Pimpl::setLayout(int layout) {
203         if (combox)
204                 combox->select(layout+1);
205 }
206
207
208 void Toolbar::Pimpl::updateLayoutList(bool force)
209 {
210         // Update the layout display
211         if (!combox) return;
212
213         // If textclass is different, we need to update the list
214         if (combox->empty() || force) {
215                 combox->clear();
216                 LyXTextClass const & tc =
217                         textclasslist.TextClass(owner->buffer()->
218                                                 params.textclass);
219                 LyXTextClass::const_iterator end = tc.end();
220                 for (LyXTextClass::const_iterator cit = tc.begin();
221                      cit != end; ++cit) {
222                         if ((*cit).obsoleted_by().empty())
223                                 combox->addline(_((*cit).name().c_str()));
224                         else
225                                 combox->addline(("@N" + string(_((*cit).name().c_str()))).c_str());
226                 }
227         }
228         // we need to do this.
229         combox->Redraw();
230 }
231
232 void Toolbar::Pimpl::clearLayoutList()
233 {
234         if (combox) {
235                 combox->clear();
236                 combox->Redraw();
237         }
238 }
239
240 void Toolbar::Pimpl::openLayoutList()
241 {
242         if (combox)
243                 combox->Show();
244 }
245
246 static
247 void ToolbarCB(FL_OBJECT * ob, long ac)
248 {
249         LyXView * owner = static_cast<LyXView *>(ob->u_vdata);
250         
251         string res = owner->getLyXFunc()->Dispatch(int(ac));
252         if(!res.empty())
253                 lyxerr[Debug::GUI] << "ToolbarCB: Function returned: " 
254                                    << res << endl;
255 }
256
257
258 extern "C" void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
259 {
260         ToolbarCB(ob, data);
261 }
262
263 #if 0
264 // What are we supposed to do with that??
265 int Toolbar::get_toolbar_func(string const & func)
266 {
267         int action = lyxaction.LookupFunc(func.c_str());
268         if (action == -1) {
269                 if (func == "separator"){
270                         action = TOOL_SEPARATOR;
271                 } else if (func == "layouts"){
272                         action = TOOL_LAYOUTS;
273                 } else action = 0;
274         }
275         return action;
276 }
277 #endif
278
279
280 static
281 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height) {
282         string name, arg, xpm_name;
283         kb_action act;
284
285         if (lyxaction.isPseudoAction(action)) {
286                 act = lyxaction.retrieveActionArg(action, arg);
287                 name = lyxaction.getActionName(act);
288                 xpm_name = subst(name + ' ' + arg, ' ','_');
289         } else {
290                 act = (kb_action)action;
291                 name = lyxaction.getActionName(action);
292                 xpm_name = name;
293         }
294
295         string fullname = LibFileSearch("images", xpm_name, "xpm");
296
297         if (!fullname.empty()) {
298                 lyxerr[Debug::GUI] << "Full icon name is `" 
299                                        << fullname << "'" << endl;
300                 fl_set_pixmapbutton_file(obj, fullname.c_str());
301                 return;
302         }
303
304         if (act == LFUN_INSERT_MATH && !arg.empty()) {
305                 lyxerr[Debug::GUI] << "Using mathed-provided icon" << endl;
306                 char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
307                                                         buttonwidth,
308                                                         height);
309                 fl_set_pixmapbutton_data(obj, const_cast<char **>(pixmap));
310                 return;
311         }
312         
313         lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
314         fullname = LibFileSearch("images", "unknown", "xpm");
315         if (!fullname.empty()) {
316                 lyxerr[Debug::GUI] << "Using default `unknown' icon" 
317                                        << endl;
318                 fl_set_pixmapbutton_file(obj, fullname.c_str());
319         }
320 }
321
322 void Toolbar::Pimpl::set(bool doingmain)
323 {
324         // we shouldn't set if we have not cleaned
325         if (!cleaned) return;
326         
327         FL_OBJECT * obj;
328         
329         if (!doingmain) {
330                 fl_freeze_form(owner->getForm());
331                 fl_addto_form(owner->getForm());
332         }
333
334 #if FL_REVISION < 89
335         // add the time if it don't exist
336         if (bubble_timer == 0)
337                 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
338                                             xpos, ypos, 0, 0, "Timer");
339 #endif
340         
341         ToolbarList::iterator item = toollist.begin();
342         ToolbarList::iterator end = toollist.end();
343         for (; item != end; ++item) {
344                 switch(item->action){
345                 case ToolbarDefaults::SEPARATOR:
346                         xpos += sepspace;
347                         break;
348                 case ToolbarDefaults::NEWLINE:
349                         // Not supported yet.
350                         break;
351                 case ToolbarDefaults::LAYOUTS:
352                         xpos += standardspacing;
353                         if (!combox)
354                                 combox = new Combox(FL_COMBOX_DROPLIST);
355                         combox->add(xpos, ypos, 135, height, 400);
356                         combox->setcallback(LayoutsCB);
357                         combox->resize(FL_RESIZE_ALL);
358                         combox->gravity(NorthWestGravity, NorthWestGravity);
359                         xpos += 135;
360                         break;
361                 default:
362                         xpos += standardspacing;
363                         item->icon = obj = 
364                                 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
365                                                     xpos, ypos,
366                                                     buttonwidth,
367                                                     height, "");
368                         fl_set_object_resize(obj, FL_RESIZE_ALL);
369                         fl_set_object_gravity(obj,
370                                               NorthWestGravity,
371                                               NorthWestGravity);
372                         fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
373                                                static_cast<long>(item->action));
374                         // Remove the blue feedback rectangle
375                         fl_set_pixmapbutton_focus_outline(obj, 0);
376
377                         // Set the tooltip
378 #if FL_REVISION >= 89
379                         string help = lyxaction.helpText(item->action);
380                         fl_set_object_helper(obj, help.c_str());        
381 #else
382                         fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
383                         obj->u_cdata = reinterpret_cast<char *>(bubble_timer);
384 #endif
385
386                         // The view that this object belongs to.
387                         obj->u_vdata = owner;
388
389                         setPixmap(obj, item->action, buttonwidth, height);
390                         // we must remember to update the positions
391                         xpos += buttonwidth;
392                         // ypos is constant
393                         /* Here will come a check to see if the new
394                          * pos is within the bounds of the main frame,
395                          * and perhaps wrap the toolbar if not.
396                          */
397                         break;
398                 }
399         }
400
401         if (!doingmain) {
402                 fl_end_form();
403                 fl_unfreeze_form(owner->getForm());
404                 // Should be safe to do this here.
405                 owner->updateLayoutChoice();
406         }
407
408         // set the state of the icons
409         //update();
410
411         cleaned = false;
412 }
413
414
415 void Toolbar::Pimpl::add(int action, bool doclean)
416 {
417         if (doclean && !cleaned) clean();
418
419         // this is what we do if we want to add to an existing
420         // toolbar.
421         if (!doclean && owner) {
422                 // first "hide" the toolbar buttons. This is not a real hide
423                 // actually it deletes and frees the button altogether.
424                 lyxerr << "Toolbar::add: \"hide\" the toolbar buttons." 
425                        << endl;
426
427                 lightReset();
428                 
429                 fl_freeze_form(owner->getForm());
430
431                 ToolbarList::iterator p = toollist.begin();
432                 ToolbarList::iterator end = toollist.end();
433                 for (; p != end; ++p) {
434                         p->clean();
435                 }
436
437                 if (combox) {
438                         delete combox;
439                         combox = 0;
440                 }
441                 fl_unfreeze_form(owner->getForm());
442                 cleaned = true; // this is not completely true, but OK anyway
443         }
444         
445         // there exist some special actions not part of
446         // kb_action: SEPARATOR, LAYOUTS
447
448         toolbarItem newItem;
449         newItem.action = action;
450         toollist.push_back(newItem);
451 }
452
453
454 void Toolbar::Pimpl::clean()
455 {
456         //reset(); // I do not understand what this reset() is, anyway
457
458         //now delete all the objects..
459         if (owner)
460                 fl_freeze_form(owner->getForm());
461
462         // G++ vector does not have clear defined
463         //toollist.clear();
464         toollist.erase(toollist.begin(), toollist.end());
465
466         lyxerr[Debug::GUI] << "Combox: " << combox << endl;
467         if (combox) {
468                 delete combox;
469                 combox = 0;
470         }
471
472         if (owner)
473                 fl_unfreeze_form(owner->getForm());
474         lyxerr[Debug::GUI] << "toolbar cleaned" << endl;
475         cleaned = true;
476 }
477
478
479 void Toolbar::Pimpl::push(int nth)
480 {
481         lyxerr[Debug::GUI] << "Toolbar::push: trying to trigger no `"
482                                << nth << '\'' << endl;
483         
484         if (nth <= 0 || nth >= int(toollist.size())) {
485                 // item nth not found...
486                 return;
487         }
488
489         fl_trigger_object(toollist[nth - 1].icon);
490 }
491
492
493 void Toolbar::Pimpl::reset() 
494 {
495         //toollist = 0; // what is this supposed to do?
496         cleaned = false;
497         lightReset();
498 }
499
500 void Toolbar::Pimpl::lightReset() {
501         xpos = sxpos - standardspacing;
502         ypos = sypos;
503 }