]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Menubar_pimpl.C
use the new sstream return non-pods as const, use string instead of char * in a lot...
[lyx.git] / src / frontends / xforms / Menubar_pimpl.C
1 /* This file is part of
2 * ======================================================
3
4 *           LyX, The Document Processor
5 *        
6 *           Copyright (C) 1999 The LyX Team.
7 *
8 *======================================================*/
9
10 #ifdef __GNUG__
11 #pragma implementation
12 #endif
13
14 #include <config.h>
15
16 #include <algorithm>
17 #include <cctype>
18 #include "support/lstrings.h"
19 #include "support/filetools.h"
20 #include "support/LAssert.h"
21 #include "debug.h"
22 #include "LyXAction.h"
23 #include "lyxfunc.h"
24 #include "kbmap.h"
25 #include "bufferlist.h"
26 #include "lastfiles.h"
27 #include "LyXView.h"
28 #include "MenuBackend.h"
29 #include "Menubar_pimpl.h"
30 #include "exporter.h"
31
32 using std::pair;
33 using std::endl;
34 using std::vector;
35 using std::max;
36 using std::min;
37
38 typedef vector<int>::size_type size_type;
39
40 extern kb_keymap * toplevel_keymap;
41 extern LyXAction lyxaction;
42 extern BufferList bufferlist;
43 extern LastFiles * lastfiles; 
44
45 // Some constants
46 const int MENU_LABEL_SIZE = FL_NORMAL_SIZE;
47 const int mheight = 30;
48 const int mbheight= 22;
49 // where to place the menubar?
50 const int yloc = (mheight - mbheight)/2; //air + bw;
51 const int mbadd = 20; // menu button add (to width)
52 // Some space between buttons on the menubar 
53 const int air = 2;
54 char const * menu_tabstop = "aa";
55 char const * default_tabstop = "aaaaaaaa";
56
57
58 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb) 
59         : frame_(0), owner_(view), menubackend_(&mb)
60 {
61         // Should we do something here?
62 }
63
64 Menubar::Pimpl::~Pimpl() 
65 {
66         // Should we do something here?
67 }
68
69 // This is used a few times below.
70 inline
71 int string_width(string const & str) 
72 {
73         return fl_get_string_widthTAB(FL_NORMAL_STYLE, MENU_LABEL_SIZE,
74                                       str.c_str(),      str.length());
75 }
76
77 //Defined later, used in set().
78 extern "C"
79 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
80
81 void Menubar::Pimpl::set(string const & menu_name) 
82 {
83         lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set " 
84                            << "for menu `" << menu_name << "'" << endl;
85
86         if (menu_name == current_menu) {
87                 lyxerr[Debug::GUI] << "Nothing to do." << endl;
88                 return;
89         }
90
91         // If the backend has not been initialized yet, we use a
92         // default instead.  
93         if (menubackend_->empty()) {
94                 lyxerr << "Menubar::Pimpl::set: menubackend is empty! "
95                         "using default values." << endl;
96                 MenuBackend * mb = new MenuBackend();
97                 mb->defaults();
98                 menubackend_ = mb;
99         }
100
101         if (!menubackend_->hasMenu(menu_name)){ 
102                 lyxerr << "ERROR:set: Unknown menu `" << menu_name
103                        << "'" << endl;
104                 return;
105         }
106
107         Menu menu = menubackend_->getMenu(menu_name);
108
109         if (!menu.menubar()) {
110                 lyxerr << "Only a menubar-type object can go in a "
111                         "toplevel menu" << endl;
112                 return;
113         }
114
115         current_menu = menu_name;
116         FL_FORM * form = owner_->getForm(); 
117         int moffset = 0;
118         bool form_was_open, form_was_frozen;
119
120         if (fl_current_form == form)
121                 form_was_open = true;
122         else if (fl_current_form == 0) {
123                 form_was_open = false;
124                 fl_addto_form(form);
125         } 
126         else {
127                 lyxerr << "Something is wrong: unknown form " 
128                        << fl_current_form << " is already open" 
129                        << "(main form is " << form << ")" << endl;
130                 return;
131         }
132         if (form->frozen)
133                 form_was_frozen = true;
134         else {
135                 form_was_frozen = false;
136                 fl_freeze_form(form);
137         }
138
139         // Delete old buttons if there are some.
140         for(ButtonList::const_iterator cit = buttonlist_.begin();
141             cit != buttonlist_.end(); ++cit) {
142                 if ((*cit)->obj_) {
143                         fl_delete_object((*cit)->obj_);
144                         fl_free_object((*cit)->obj_);
145                 }
146                 delete (*cit);
147         }
148         buttonlist_.clear();
149
150         // Create menu frame if there is non yet.
151         if (!frame_) {
152                 frame_ = fl_add_frame(FL_UP_FRAME, 0, 0, form->w, mheight, "");
153                 fl_set_object_resize(frame_, FL_RESIZE_ALL);
154                 fl_set_object_gravity(frame_, NorthWestGravity, 
155                                       NorthEastGravity);
156         } 
157
158         for (Menu::const_iterator i = menu.begin(); 
159              i != menu.end(); ++i) {
160                 FL_OBJECT * obj;
161                 if (i->kind() != MenuItem::Submenu) {
162                         lyxerr << "ERROR: Menubar::Pimpl::Pimpl:"
163                                 " only submenus can appear in a menubar";
164                         break;
165                 }
166                 string label = i->label();
167                 string shortcut = '#' + i->shortcut();
168                 int width = string_width(label);
169                 obj = fl_add_button(FL_TOUCH_BUTTON,
170                                     air + moffset, yloc,
171                                     width + mbadd,
172                                     mbheight, 
173                                     label.c_str());
174                 fl_set_object_boxtype(obj, FL_FLAT_BOX);
175                 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
176                 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
177                 fl_set_object_lstyle(obj, FL_NORMAL_STYLE);
178                 fl_set_object_resize(obj, FL_RESIZE_ALL);
179                 fl_set_object_gravity(obj, NorthWestGravity, 
180                                       NorthWestGravity);
181                 moffset += obj->w + air;
182                 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
183                 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
184
185                 ItemInfo * iteminfo = new ItemInfo(this, 
186                                                    new MenuItem(*i), obj);
187                 buttonlist_.push_back(iteminfo);
188                 obj->u_vdata = iteminfo;
189 //              lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
190 //                     << " Val=(pimpl_=" << iteminfo->pimpl_
191 //                     << ", item_=" << iteminfo->item_
192 //                     << ", obj_=" << iteminfo->obj_ << ")" <<endl;
193         }
194
195         if (!form_was_frozen) {
196                 fl_unfreeze_form(form);
197         }
198         if (!form_was_open) 
199                 fl_end_form();
200
201         // Force the redraw of the buttons (probably not the best
202         // method, but...) 
203         for(ButtonList::const_iterator cit = buttonlist_.begin();
204             cit != buttonlist_.end(); ++cit) {
205                 if ((*cit)->obj_) {
206                         fl_redraw_object((*cit)->obj_);
207                 }
208         }
209
210         lyxerr[Debug::GUI] << "Menubar set." << endl;
211
212
213 void Menubar::Pimpl::openByName(string const & name)
214 {
215         for(ButtonList::const_iterator cit = buttonlist_.begin();
216             cit != buttonlist_.end(); ++cit) {
217                 if ((*cit)->item_->submenu() == name) {
218                         MenuCallback((*cit)->obj_, 1);
219                         return;
220                 }
221         }
222         lyxerr << "Menubar::Pimpl::openByName: menu "
223                << name << " not found" << endl;
224 }
225
226
227 void Menubar::Pimpl::add_lastfiles(int menu, string const & extra_label) 
228 {
229         int ii = 1;
230         for (LastFiles::const_iterator cit = lastfiles->begin();
231              cit != lastfiles->end() && ii < 10; ++cit, ++ii) {
232
233                 int action =
234                         lyxaction.getPseudoAction(LFUN_FILE_OPEN, (*cit));
235                 string label = tostr(ii) + ". "
236                         + MakeDisplayPath((*cit),30)
237                         + "%x" + tostr(action) + "%h";
238                 if ((cit + 1) == lastfiles->end())
239                         label += extra_label;
240                 string shortcut = tostr(ii) + "#" + tostr(ii); 
241                 lyxerr[Debug::GUI] << "shortcut is " << shortcut <<
242                         endl;
243
244                 fl_addtopup(menu, label.c_str(), shortcut.c_str());
245         }
246
247 }
248
249 void Menubar::Pimpl::add_documents(int menu, string const & extra_label)
250 {
251         vector<string> names = bufferlist.getFileNames();
252
253         if (names.empty()) {
254                 fl_addtopup(menu,_("No Documents Open!%i"));
255                 return;
256         }
257
258         for (vector<string>::const_iterator cit = names.begin();
259              cit != names.end() ; ++cit) {
260                 int action =
261                         lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *cit);
262                 string label = MakeDisplayPath(*cit, 30)
263                         + "%x" + tostr(action);
264                 if ((cit + 1) == names.end())
265                         label += extra_label;
266                                 
267                 fl_addtopup(menu, label.c_str());
268         }
269
270 }
271
272
273 string limit_string_length(string const & str)
274 {
275         string::size_type const max_item_length = 45;
276
277         if (str.size() > max_item_length)
278                 return str.substr(0, max_item_length-3) + "...";
279         else
280                 return str;
281 }
282
283 size_type const max_number_of_menus = 32;
284 size_type const max_number_of_items = 25;
285
286 void add_toc2(int menu, string const & extra_label,
287               vector<int> & smn, Window win,
288               vector<Buffer::TocItem> const & toc_list,
289               size_type from, size_type to, int depth)
290 {
291         if (to - from <= max_number_of_items) {
292                 for (size_type i = from; i < to; ++i) {
293                         int action = lyxaction.
294                                 getPseudoAction(LFUN_GOTO_PARAGRAPH,
295                                                 tostr(toc_list[i].par->id()));
296                         string label(4 * max(0, toc_list[i].depth - depth),' ');
297                         label += toc_list[i].str;
298                         label = limit_string_length(label);
299                         label += "%x" + tostr(action);
300                         if (i == to - 1 && depth == 0)
301                                 label += extra_label;
302                         fl_addtopup(menu, label.c_str());
303                 }
304         } else {
305                 size_type pos = from;
306                 size_type count = 0;
307                 while (pos < to) {
308                         ++count;
309                         if (count > max_number_of_items) {
310                                 fl_addtopup(menu, ". . .%d");
311                                 break;
312                         }
313                         size_type new_pos = pos+1;
314                         while (new_pos < to &&
315                                toc_list[new_pos].depth > depth)
316                                 ++new_pos;
317
318                         int action = lyxaction.
319                                 getPseudoAction(LFUN_GOTO_PARAGRAPH,
320                                                 tostr(toc_list[pos].par->id()));
321                         string label(4 * max(0, toc_list[pos].depth - depth), ' ');
322                         label += toc_list[pos].str;
323                         label = limit_string_length(label);
324                         if (new_pos == to && depth == 0)
325                                 label += extra_label;
326
327                         if (new_pos == pos + 1) {
328                                 label += "%x" + tostr(action);
329                                 fl_addtopup(menu, label.c_str());
330                         } else if (smn.size() < max_number_of_menus) {
331                                 int menu2 = fl_newpup(win);
332                                 smn.push_back(menu2);
333                                 add_toc2(menu2, extra_label, smn, win,
334                                          toc_list, pos, new_pos, depth+1);
335                                 label += "%m";
336                                 fl_addtopup(menu, label.c_str(), menu2);
337                         } else {
338                                 label += "%d";
339                                 fl_addtopup(menu, label.c_str());
340                         }
341                         pos = new_pos;
342                 }
343         }
344 }
345
346 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
347                              vector<int> & smn, Window win)
348 {
349         //xgettext:no-c-format
350         static char const * MenuNames[3] = { N_("List of Figures%m"),
351         //xgettext:no-c-format
352                                              N_("List of Tables%m"),
353         //xgettext:no-c-format
354                                              N_("List of Algorithms%m") };
355
356         vector<vector<Buffer::TocItem> > toc_list =
357                 owner_->buffer()->getTocList();
358
359         // Handle LOF/LOT/LOA
360         int max_nonempty = 0;
361         for (int i = 1; i <= 3; ++i)
362                 if (!toc_list[i].empty())
363                         max_nonempty = i;
364
365         for (int j = 1; j <= 3; ++j)
366                 if (!toc_list[j].empty()) {
367                         int menu2 = fl_newpup(win);
368                         smn.push_back(menu2);
369                         for (size_type i = 0; i < toc_list[j].size(); ++i) {
370                                 if (i > max_number_of_items) {
371                                         fl_addtopup(menu2, ". . .%d");
372                                         break;
373                                 }
374                                 int action = lyxaction.
375                                         getPseudoAction(LFUN_GOTO_PARAGRAPH,
376                                                         tostr(toc_list[j][i].par->id()));
377                                 string label =
378                                         limit_string_length(toc_list[j][i].str);
379                                 label += "%x" + tostr(action);
380                                 fl_addtopup(menu2, label.c_str());
381                         }
382                         if (j == max_nonempty) {
383                                 string label = _(MenuNames[j-1]);
384                                 label += "%l";
385                                 fl_addtopup(menu, label.c_str(), menu2);
386                         } else
387                                 fl_addtopup(menu, _(MenuNames[j-1]), menu2);
388                 }
389
390         // Handle normal TOC
391         if (max_nonempty == 0 && toc_list[0].empty()) {
392                 fl_addtopup(menu,_("No Table of Contents%i"));
393                 return;
394         }
395
396         add_toc2(menu, extra_label, smn, win,
397                  toc_list[0], 0, toc_list[0].size(), 0);
398
399 }
400
401 void add_references2(int menu, vector<int> & smn, Window win,
402                      vector<string> const & label_list, string const & type)
403 {
404         size_type const max_number_of_items = 25;
405         size_type const max_number_of_items2 = 20;
406         string::size_type const max_item_length = 40;
407         string::size_type const max_item_length2 = 20;
408
409         if (label_list.size() <= max_number_of_items)
410                 for (size_type i = 0; i < label_list.size(); ++i) {
411                         int action = (type == "goto")
412                                 ? lyxaction.getPseudoAction(LFUN_REF_GOTO, 
413                                                             label_list[i])
414                                 : lyxaction.getPseudoAction(LFUN_REF_INSERT,
415                                                             type + "|++||++|"
416                                                             + label_list[i]);
417                         string label = label_list[i];
418                         if (label.size() > max_item_length)
419                                 label = label.substr(0, max_item_length-1) + "$";
420                         label += "%x" + tostr(action);
421                         fl_addtopup(menu, label.c_str());
422                 }
423         else {
424                 size_type count = 0;
425                 for (size_type i = 0; i < label_list.size();
426                      i += max_number_of_items2) {
427                         ++count;
428                         if (count > max_number_of_items) {
429                                 fl_addtopup(menu, ". . .%d");
430                                 break;
431                         }
432                         size_type j = min(label_list.size(),
433                                           i+max_number_of_items2);
434
435                         string label;
436                         label += (label_list[i].size() > max_item_length2)
437                                 ? label_list[i].substr(0, max_item_length2-1) + "$"
438                                 : label_list[i];
439                         label += "..";
440                         label += (label_list[j-1].size() > max_item_length2)
441                                 ? label_list[j-1].substr(0, max_item_length2-1) + "$"
442                                 : label += label_list[j-1];
443
444                         if (smn.size() < max_number_of_menus) {
445                                 int menu2 = fl_newpup(win);
446                                 smn.push_back(menu2);
447                                 for (size_type k = i;  k < j; ++k) {
448                                         int action = (type == "goto")
449                                                 ? lyxaction.getPseudoAction(LFUN_REF_GOTO, 
450                                                                             label_list[k])
451                                                 : lyxaction.getPseudoAction(LFUN_REF_INSERT,
452                                                                             type + "|++||++|"
453                                                                             + label_list[k]);
454                                         string label2 = label_list[k];
455                                         if (label2.size() > max_item_length)
456                                                 label2 = label2.substr(0, max_item_length-1) + "$";
457                                         label2 += "%x" + tostr(action);
458                                         fl_addtopup(menu2, label2.c_str());
459                                 }
460                                 label += "%m";
461                                 fl_addtopup(menu, label.c_str(), menu2);
462                         } else {
463                                 label += "%d";
464                                 fl_addtopup(menu, label.c_str());
465                         }
466                 }
467         }
468 }
469
470
471 void Menubar::Pimpl::add_references(int menu, string const & extra_label,
472                                     vector<int> & smn, Window win)
473 {
474         //xgettext:no-c-format
475         static char const * MenuNames[6] = { N_("Insert Reference%m"),
476         //xgettext:no-c-format
477                                              N_("Insert Page Number%m"),
478         //xgettext:no-c-format
479                                              N_("Insert vref%m"),
480         //xgettext:no-c-format
481                                              N_("Insert vpageref%m"),
482         //xgettext:no-c-format
483                                              N_("Insert Pretty Ref%m"),
484         //xgettext:no-c-format
485                                              N_("Goto Reference%m") };
486
487         int const EMPTY = 1;
488         int const SGML = 2;
489         int const READONLY = 4;
490
491         static int MenuFlags[6] = {
492                 EMPTY | READONLY,
493                 EMPTY | READONLY,
494                 EMPTY | READONLY | SGML,
495                 EMPTY | READONLY | SGML,
496                 EMPTY | READONLY | SGML,
497                 EMPTY };
498
499         static string const MenuTypes[6] = {
500                 "ref", "pageref", "vref", "vpageref", "prettyref", "goto" };
501
502         vector<string> label_list = owner_->buffer()->getLabelList();
503
504         int flag = 0;
505         if (label_list.empty())
506                 flag += EMPTY;
507         if (owner_->buffer()->isSGML())
508                 flag += SGML;
509         if (owner_->buffer()->isReadonly())
510                 flag += READONLY;
511
512         int max_nonempty = -1;
513         for (int i = 0; i < 6; ++i)
514                 if ((MenuFlags[i] & flag) == 0)
515                         max_nonempty = i;
516
517         for (int i = 0; i < 6; ++i) {
518                 if ((MenuFlags[i] & flag) == 0) {
519                         string label = _(MenuNames[i]);
520                         if (i == max_nonempty)
521                                 label += extra_label;
522                         if (smn.size() < max_number_of_menus) {
523                                 int menu2 = fl_newpup(win);
524                                 smn.push_back(menu2);
525                                 add_references2(menu2, smn, win, label_list,
526                                                 MenuTypes[i]);
527                                 fl_addtopup(menu, label.c_str(), menu2);
528                         } else {
529                                 label += "%d";
530                                 fl_addtopup(menu, label.c_str());       
531                         }
532                 }
533         }
534 }
535
536
537 void Menubar::Pimpl::add_formats(int menu, string const & extra_label,
538                                  kb_action action, bool viewable)
539 {
540         vector<pair<string,string> > names = 
541                 viewable
542                 ? Exporter::GetViewableFormats(owner_->buffer())
543                 : Exporter::GetExportableFormats(owner_->buffer());
544
545         for (vector<pair<string,string> >::const_iterator cit = names.begin();
546              cit != names.end() ; ++cit) {
547                 int action2 =
548                         lyxaction.getPseudoAction(action, (*cit).first);
549                 string label = (*cit).second
550                         + "%x" + tostr(action2);
551                 if ((cit + 1) == names.end())
552                         label += extra_label;
553                                 
554                 fl_addtopup(menu, label.c_str());
555         }
556 }
557
558
559 int Menubar::Pimpl::create_submenu(Window win, LyXView * view, 
560                                    string const & menu_name, 
561                                    vector<int> & smn) 
562 {
563         if (!menubackend_->hasMenu(menu_name)){ 
564                 lyxerr << "ERROR:create_submenu: Unknown menu `" 
565                        << menu_name << "'" << endl;
566                 return -1;
567         }
568         Menu md = menubackend_->getMenu(menu_name);
569
570         int menu = fl_newpup(win);
571         fl_setpup_softedge(menu, true);
572         fl_setpup_bw(menu, -1);
573         lyxerr[Debug::GUI] << "Adding menu " << menu 
574                            << " in deletion list" << endl;
575         smn.push_back(menu);
576
577         // Compute the size of the largest label (because xforms is
578         // not able to support shortcuts correctly...)
579         int max_width = 0;
580         string widest_label;
581         Menu::const_iterator end = md.end();
582         for (Menu::const_iterator i = md.begin(); i != end; ++i) {
583                 MenuItem const & item = (*i);
584                 if (item.kind() == MenuItem::Command) {
585                         string label = item.label() + '\t';
586                         int width = string_width(label);
587                         if (width > max_width) {
588                                 max_width = width;
589                                 widest_label = label;
590                         }
591                 }
592         }
593         lyxerr[Debug::GUI] << "max_width=" << max_width 
594                            << ", widest_label=`" << widest_label 
595                            << "'" << endl;
596
597         // Compute where to put separators
598         vector<string> extra_labels(md.size());
599         vector<string>::iterator it = extra_labels.begin();
600         vector<string>::iterator last = it;
601         for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
602                 if (i->kind() == MenuItem::Separator)
603                         *last = "%l";
604                 else if (!i->optional() ||
605                          !(view->getLyXFunc()->getStatus(i->action()) & LyXFunc::Disabled))
606                         last = it;
607
608         it = extra_labels.begin();
609         for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
610                 MenuItem const & item = (*i);
611                 string & extra_label = *it;
612
613                 switch(item.kind()) {
614                 case MenuItem::Command: {
615                         LyXFunc::func_status flag = 
616                                 view->getLyXFunc()->getStatus(item.action()); 
617
618                         // handle optional entries.
619                         if (item.optional() && (flag & LyXFunc::Disabled)) {
620                                 lyxerr[Debug::GUI] 
621                                         << "Skipping optional item " 
622                                         << item.label() << endl; 
623                                 break;
624                         }
625
626                         // Get the keys bound to this action, but keep only the
627                         // first one later
628                         string accel = toplevel_keymap->findbinding(item.action());
629                         // Build the menu label from all the info
630                         string label = item.label();
631
632                         if (!accel.empty()) {
633                                 // Try to be clever and add  just enough
634                                 // tabs to align shortcuts.
635                                 do 
636                                         label += '\t';
637                                 while (string_width(label) < max_width);
638                                 label += accel.substr(1,accel.find(']') - 1);
639                         }
640                         label += "%x" + tostr(item.action()) + extra_label;
641                         
642                         // Modify the entry using the function status
643                         string pupmode;
644                         if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
645                                 pupmode += "%i";
646                         if (flag & LyXFunc::ToggleOn)
647                                 pupmode += "%B";
648                         if (flag & LyXFunc::ToggleOff)
649                                 pupmode += "%b";
650                         label += pupmode;
651
652                         // Finally the menu shortcut
653                         string shortcut = item.shortcut();
654
655                         if (!shortcut.empty()) {
656                                 shortcut += lowercase(shortcut[0]);
657                                 label += "%h";
658                                 fl_addtopup(menu, label.c_str(), 
659                                             shortcut.c_str());
660                         } else
661                                 fl_addtopup(menu, label.c_str());
662                         
663                         lyxerr[Debug::GUI] << "Command: \""  
664                                            << lyxaction.getActionName(item.action())
665                                            << "\", Binding " << accel 
666                                            << ", shortcut " << shortcut 
667                                            << endl;
668
669
670                         break;
671                 }
672
673                 case MenuItem::Submenu: {
674                         int submenu = create_submenu(win, view, 
675                                                      item.submenu(), smn);
676                         if (submenu == -1)
677                                 return -1;
678                         string label = item.label();
679                         label += extra_label + "%m";
680                         string shortcut = item.shortcut();
681                         if (!shortcut.empty()) {
682                                 shortcut += lowercase(shortcut[0]);
683                                 fl_addtopup(menu, (label + "%h").c_str(),
684                                             submenu, shortcut.c_str());
685                         }
686                         else {
687                                 fl_addtopup(menu, label.c_str(), submenu);
688                         }
689                         break;
690                 }
691
692                 case MenuItem::Separator:
693                         // already done, and if it was the first one,
694                         // we just ignore it.
695                         break;
696
697                 case MenuItem::Documents: 
698                         add_documents(menu, extra_label);
699                         break;
700
701                 case MenuItem::Lastfiles: 
702                         add_lastfiles(menu, extra_label);
703                         break;
704
705                 case MenuItem::Toc:
706                         add_toc(menu, extra_label, smn, win);
707                         break;
708
709                 case MenuItem::References:
710                         add_references(menu, extra_label, smn, win);
711                         break;
712
713                 case MenuItem::ViewFormats:
714                         add_formats(menu, extra_label, LFUN_PREVIEW, true);
715                         break;
716
717                 case MenuItem::UpdateFormats:
718                         add_formats(menu, extra_label, LFUN_UPDATE, true);
719                         break;  
720
721                 case MenuItem::ExportFormats:
722                         add_formats(menu, extra_label, LFUN_EXPORT, false);
723                         break;
724
725                 }
726         }
727         return menu;
728 }
729
730 extern "C"
731 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
732 {
733         Menubar::Pimpl::MenuCallback(ob, button);
734 }
735
736
737 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
738 {
739         ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
740 //      lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
741 //             << "Val=(pimpl_=" << iteminfo->pimpl_
742 //             << ", item_=" << iteminfo->item_
743 //             << ", obj_=" << iteminfo->obj_ << ")" <<endl;
744
745         LyXView * view = iteminfo->pimpl_->owner_;
746         MenuItem const * item = iteminfo->item_;
747
748         if (button == 1) {
749                 // set the pseudo menu-button
750                 fl_set_object_boxtype(ob, FL_DOWN_BOX);
751                 fl_set_button(ob, 0);
752                 fl_redraw_object(ob);
753         }
754
755         // Paranoia check
756         Assert(item->kind() == MenuItem::Submenu);
757         
758         // set tabstop length
759         fl_set_tabstop(menu_tabstop);
760         vector<int> submenus;
761         int menu = iteminfo->pimpl_->
762                 create_submenu(FL_ObjWin(ob), view, 
763                                item->submenu(), submenus);
764         if (menu != -1) {
765                 // place popup
766                 fl_setpup_position(view->getForm()->x + ob->x,
767                                    view->getForm()->y + ob->y + ob->h + 10);   
768                 int choice = fl_dopup(menu);
769                 
770                 if (button == 1) {
771                                 // set the pseudo menu-button back
772                         fl_set_object_boxtype(ob, FL_FLAT_BOX);
773                         fl_redraw_object(ob);
774                 }
775                 
776                 if (choice >= 1) {
777                         view->getLyXFunc()->Dispatch(choice);
778                 }
779         }
780         else 
781                 lyxerr << "Error in MenuCallback" << endl;
782         
783         std::for_each(submenus.begin(), submenus.end(), fl_freepup);
784         // restore tabstop length
785         fl_set_tabstop(default_tabstop);
786
787 }