1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1999 The LyX Team.
8 *======================================================*/
11 #pragma implementation
18 #include "support/lstrings.h"
19 #include "support/filetools.h"
20 #include "support/LAssert.h"
22 #include "LyXAction.h"
25 #include "bufferlist.h"
26 #include "lastfiles.h"
28 #include "MenuBackend.h"
29 #include "Menubar_pimpl.h"
38 typedef vector<int>::size_type size_type;
40 extern kb_keymap * toplevel_keymap;
41 extern LyXAction lyxaction;
42 extern BufferList bufferlist;
43 extern LastFiles * lastfiles;
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
54 char const * menu_tabstop = "aa";
55 char const * default_tabstop = "aaaaaaaa";
58 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
59 : frame_(0), owner_(view), menubackend_(&mb)
61 // Should we do something here?
64 Menubar::Pimpl::~Pimpl()
66 // Should we do something here?
69 // This is used a few times below.
71 int string_width(string const & str)
73 return fl_get_string_widthTAB(FL_NORMAL_STYLE, MENU_LABEL_SIZE,
74 str.c_str(), str.length());
77 //Defined later, used in set().
79 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
81 void Menubar::Pimpl::set(string const & menu_name)
83 lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set "
84 << "for menu `" << menu_name << "'" << endl;
86 if (menu_name == current_menu) {
87 lyxerr[Debug::GUI] << "Nothing to do." << endl;
91 // If the backend has not been initialized yet, we use a
93 if (menubackend_->empty()) {
94 lyxerr << "Menubar::Pimpl::set: menubackend is empty! "
95 "using default values." << endl;
96 MenuBackend * mb = new MenuBackend();
101 if (!menubackend_->hasMenu(menu_name)){
102 lyxerr << "ERROR:set: Unknown menu `" << menu_name
107 Menu menu = menubackend_->getMenu(menu_name);
109 if (!menu.menubar()) {
110 lyxerr << "Only a menubar-type object can go in a "
111 "toplevel menu" << endl;
115 current_menu = menu_name;
116 FL_FORM * form = owner_->getForm();
118 bool form_was_open, form_was_frozen;
120 if (fl_current_form == form)
121 form_was_open = true;
122 else if (fl_current_form == 0) {
123 form_was_open = false;
127 lyxerr << "Something is wrong: unknown form "
128 << fl_current_form << " is already open"
129 << "(main form is " << form << ")" << endl;
133 form_was_frozen = true;
135 form_was_frozen = false;
136 fl_freeze_form(form);
139 // Delete old buttons if there are some.
140 for(ButtonList::const_iterator cit = buttonlist_.begin();
141 cit != buttonlist_.end(); ++cit) {
143 fl_delete_object((*cit)->obj_);
144 fl_free_object((*cit)->obj_);
150 // Create menu frame if there is non yet.
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,
158 for (Menu::const_iterator i = menu.begin();
159 i != menu.end(); ++i) {
161 if (i->kind() != MenuItem::Submenu) {
162 lyxerr << "ERROR: Menubar::Pimpl::Pimpl:"
163 " only submenus can appear in a menubar";
166 string label = i->label();
167 string shortcut = '#' + i->shortcut();
168 int width = string_width(label);
169 obj = fl_add_button(FL_TOUCH_BUTTON,
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,
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);
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;
195 if (!form_was_frozen) {
196 fl_unfreeze_form(form);
201 // Force the redraw of the buttons (probably not the best
203 for(ButtonList::const_iterator cit = buttonlist_.begin();
204 cit != buttonlist_.end(); ++cit) {
206 fl_redraw_object((*cit)->obj_);
210 lyxerr[Debug::GUI] << "Menubar set." << endl;
213 void Menubar::Pimpl::openByName(string const & name)
215 for(ButtonList::const_iterator cit = buttonlist_.begin();
216 cit != buttonlist_.end(); ++cit) {
217 if ((*cit)->item_->submenu() == name) {
218 MenuCallback((*cit)->obj_, 1);
222 lyxerr << "Menubar::Pimpl::openByName: menu "
223 << name << " not found" << endl;
227 void Menubar::Pimpl::add_lastfiles(int menu, string const & extra_label)
230 for (LastFiles::const_iterator cit = lastfiles->begin();
231 cit != lastfiles->end() && ii < 10; ++cit, ++ii) {
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 <<
244 fl_addtopup(menu, label.c_str(), shortcut.c_str());
249 void Menubar::Pimpl::add_documents(int menu, string const & extra_label)
251 vector<string> names = bufferlist.getFileNames();
254 fl_addtopup(menu,_("No Documents Open!%i"));
258 for (vector<string>::const_iterator cit = names.begin();
259 cit != names.end() ; ++cit) {
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;
267 fl_addtopup(menu, label.c_str());
273 string limit_string_length(string const & str)
275 string::size_type const max_item_length = 45;
277 if (str.size() > max_item_length)
278 return str.substr(0, max_item_length-3) + "...";
283 size_type const max_number_of_menus = 32;
284 size_type const max_number_of_items = 25;
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)
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());
305 size_type pos = from;
309 if (count > max_number_of_items) {
310 fl_addtopup(menu, ". . .%d");
313 size_type new_pos = pos+1;
314 while (new_pos < to &&
315 toc_list[new_pos].depth > depth)
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;
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);
336 fl_addtopup(menu, label.c_str(), menu2);
339 fl_addtopup(menu, label.c_str());
346 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
347 vector<int> & smn, Window win)
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") };
356 vector<vector<Buffer::TocItem> > toc_list =
357 owner_->buffer()->getTocList();
359 // Handle LOF/LOT/LOA
360 int max_nonempty = 0;
361 for (int i = 1; i <= 3; ++i)
362 if (!toc_list[i].empty())
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");
374 int action = lyxaction.
375 getPseudoAction(LFUN_GOTO_PARAGRAPH,
376 tostr(toc_list[j][i].par->id()));
378 limit_string_length(toc_list[j][i].str);
379 label += "%x" + tostr(action);
380 fl_addtopup(menu2, label.c_str());
382 if (j == max_nonempty) {
383 string label = _(MenuNames[j-1]);
385 fl_addtopup(menu, label.c_str(), menu2);
387 fl_addtopup(menu, _(MenuNames[j-1]), menu2);
391 if (max_nonempty == 0 && toc_list[0].empty()) {
392 fl_addtopup(menu,_("No Table of Contents%i"));
396 add_toc2(menu, extra_label, smn, win,
397 toc_list[0], 0, toc_list[0].size(), 0);
401 void add_references2(int menu, vector<int> & smn, Window win,
402 vector<string> const & label_list, string const & type)
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;
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,
414 : lyxaction.getPseudoAction(LFUN_REF_INSERT,
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());
425 for (size_type i = 0; i < label_list.size();
426 i += max_number_of_items2) {
428 if (count > max_number_of_items) {
429 fl_addtopup(menu, ". . .%d");
432 size_type j = min(label_list.size(),
433 i+max_number_of_items2);
436 label += (label_list[i].size() > max_item_length2)
437 ? label_list[i].substr(0, max_item_length2-1) + "$"
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];
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,
451 : lyxaction.getPseudoAction(LFUN_REF_INSERT,
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());
461 fl_addtopup(menu, label.c_str(), menu2);
464 fl_addtopup(menu, label.c_str());
471 void Menubar::Pimpl::add_references(int menu, string const & extra_label,
472 vector<int> & smn, Window win)
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
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") };
489 int const READONLY = 4;
491 static int MenuFlags[6] = {
494 EMPTY | READONLY | SGML,
495 EMPTY | READONLY | SGML,
496 EMPTY | READONLY | SGML,
499 static string const MenuTypes[6] = {
500 "ref", "pageref", "vref", "vpageref", "prettyref", "goto" };
502 vector<string> label_list = owner_->buffer()->getLabelList();
505 if (label_list.empty())
507 if (owner_->buffer()->isSGML())
509 if (owner_->buffer()->isReadonly())
512 int max_nonempty = -1;
513 for (int i = 0; i < 6; ++i)
514 if ((MenuFlags[i] & flag) == 0)
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,
527 fl_addtopup(menu, label.c_str(), menu2);
530 fl_addtopup(menu, label.c_str());
537 void Menubar::Pimpl::add_formats(int menu, string const & extra_label,
538 kb_action action, bool viewable)
540 vector<pair<string,string> > names =
542 ? Exporter::GetViewableFormats(owner_->buffer())
543 : Exporter::GetExportableFormats(owner_->buffer());
545 for (vector<pair<string,string> >::const_iterator cit = names.begin();
546 cit != names.end() ; ++cit) {
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;
554 fl_addtopup(menu, label.c_str());
559 int Menubar::Pimpl::create_submenu(Window win, LyXView * view,
560 string const & menu_name,
563 if (!menubackend_->hasMenu(menu_name)){
564 lyxerr << "ERROR:create_submenu: Unknown menu `"
565 << menu_name << "'" << endl;
568 Menu md = menubackend_->getMenu(menu_name);
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;
577 // Compute the size of the largest label (because xforms is
578 // not able to support shortcuts correctly...)
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) {
589 widest_label = label;
593 lyxerr[Debug::GUI] << "max_width=" << max_width
594 << ", widest_label=`" << widest_label
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)
604 else if (!i->optional() ||
605 !(view->getLyXFunc()->getStatus(i->action()) & LyXFunc::Disabled))
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;
613 switch(item.kind()) {
614 case MenuItem::Command: {
615 LyXFunc::func_status flag =
616 view->getLyXFunc()->getStatus(item.action());
618 // handle optional entries.
619 if (item.optional() && (flag & LyXFunc::Disabled)) {
621 << "Skipping optional item "
622 << item.label() << endl;
626 // Get the keys bound to this action, but keep only the
628 string accel = toplevel_keymap->findbinding(item.action());
629 // Build the menu label from all the info
630 string label = item.label();
632 if (!accel.empty()) {
633 // Try to be clever and add just enough
634 // tabs to align shortcuts.
637 while (string_width(label) < max_width);
638 label += accel.substr(1,accel.find(']') - 1);
640 label += "%x" + tostr(item.action()) + extra_label;
642 // Modify the entry using the function status
644 if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
646 if (flag & LyXFunc::ToggleOn)
648 if (flag & LyXFunc::ToggleOff)
652 // Finally the menu shortcut
653 string shortcut = item.shortcut();
655 if (!shortcut.empty()) {
656 shortcut += lowercase(shortcut[0]);
658 fl_addtopup(menu, label.c_str(),
661 fl_addtopup(menu, label.c_str());
663 lyxerr[Debug::GUI] << "Command: \""
664 << lyxaction.getActionName(item.action())
665 << "\", Binding " << accel
666 << ", shortcut " << shortcut
673 case MenuItem::Submenu: {
674 int submenu = create_submenu(win, view,
675 item.submenu(), smn);
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());
687 fl_addtopup(menu, label.c_str(), submenu);
692 case MenuItem::Separator:
693 // already done, and if it was the first one,
694 // we just ignore it.
697 case MenuItem::Documents:
698 add_documents(menu, extra_label);
701 case MenuItem::Lastfiles:
702 add_lastfiles(menu, extra_label);
706 add_toc(menu, extra_label, smn, win);
709 case MenuItem::References:
710 add_references(menu, extra_label, smn, win);
713 case MenuItem::ViewFormats:
714 add_formats(menu, extra_label, LFUN_PREVIEW, true);
717 case MenuItem::UpdateFormats:
718 add_formats(menu, extra_label, LFUN_UPDATE, true);
721 case MenuItem::ExportFormats:
722 add_formats(menu, extra_label, LFUN_EXPORT, false);
731 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
733 Menubar::Pimpl::MenuCallback(ob, button);
737 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
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;
745 LyXView * view = iteminfo->pimpl_->owner_;
746 MenuItem const * item = iteminfo->item_;
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);
756 Assert(item->kind() == MenuItem::Submenu);
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);
766 fl_setpup_position(view->getForm()->x + ob->x,
767 view->getForm()->y + ob->y + ob->h + 10);
768 int choice = fl_dopup(menu);
771 // set the pseudo menu-button back
772 fl_set_object_boxtype(ob, FL_FLAT_BOX);
773 fl_redraw_object(ob);
777 view->getLyXFunc()->Dispatch(choice);
781 lyxerr << "Error in MenuCallback" << endl;
783 std::for_each(submenus.begin(), submenus.end(), fl_freepup);
784 // restore tabstop length
785 fl_set_tabstop(default_tabstop);