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