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