]> git.lyx.org Git - lyx.git/blobdiff - src/MenuBackend.C
More fixes to insettabular/text (and some missing features added).
[lyx.git] / src / MenuBackend.C
index 3d3360853b9e71e3bc22de9da208204bbb8f8fa4..1196dbaf2c23aeb7fc1b906dc3136d837d93cffd 100644 (file)
 #include "LyXAction.h"
 #include "debug.h"
 #include "gettext.h"
+#include "lastfiles.h"
+#include "bufferlist.h"
+#include "converter.h"
+#include "exporter.h"
+#include "importer.h"
+#include "support/filetools.h"
+#include "support/lyxfunctional.h"
 
 extern LyXAction lyxaction;
+extern LastFiles * lastfiles; 
+extern BufferList bufferlist;
 
 using std::endl;
+using std::vector;
+using std::pair;
+using std::find_if;
+using std::sort;
 
 // This is the global menu definition
 MenuBackend menubackend;
@@ -34,13 +47,16 @@ MenuItem::MenuItem(Kind kind, string const & label,
                   string const & command, bool optional) 
        : kind_(kind), label_(label), optional_(optional)
 {
-       switch(kind) {
+       switch (kind) {
        case Separator:
        case Documents:
        case Lastfiles:
+       case Toc:
+       case References:
        case ViewFormats:
        case UpdateFormats:
        case ExportFormats:
+       case ImportFormats:
                break;
        case Command:
                action_ = lyxaction.LookupFunc(command);
@@ -74,24 +90,30 @@ Menu & Menu::read(LyXLex & lex)
                md_documents,
                md_endmenu,
                md_exportformats,
+               md_importformats,
                md_lastfiles,
                md_optitem,
-               md_submenu,
+               md_references,
                md_separator,
+               md_submenu,
+               md_toc,
                md_updateformats,
                md_viewformats,
                md_last
        };
 
-       struct keyword_item menutags[md_last-1] = {
+       struct keyword_item menutags[md_last - 1] = {
                { "documents", md_documents },
                { "end", md_endmenu },
                { "exportformats", md_exportformats },
+               { "importformats", md_importformats },
                { "item", md_item },
                { "lastfiles", md_lastfiles },
                { "optitem", md_optitem }, 
+               { "references", md_references },
                { "separator", md_separator },
                { "submenu", md_submenu },
+               { "toc", md_toc },
                { "updateformats", md_updateformats },
                { "viewformats", md_viewformats }
        };
@@ -104,53 +126,70 @@ Menu & Menu::read(LyXLex & lex)
        bool optional = false;
 
        while (lex.IsOK() && !quit) {
-               switch(lex.lex()) {
+               switch (lex.lex()) {
                case md_optitem:
                        optional = true;
                        // fallback to md_item
                case md_item: {
                        lex.next();
-                       char * tmp = strdup(lex.GetString().c_str());
-                       string name = _(tmp);
-                       free(tmp);
+                       string name = _(lex.GetString());
                        lex.next();
-                       string command = lex.GetString();
+                       string const command = lex.GetString();
                        add(MenuItem(MenuItem::Command, name, 
                                     command, optional));
                        optional = false;
                        break;
                }
+
                case md_separator:
                        add(MenuItem(MenuItem::Separator));
                        break;
+
                case md_lastfiles:
                        add(MenuItem(MenuItem::Lastfiles));
                        break;
+
                case md_documents:
                        add(MenuItem(MenuItem::Documents));
                        break;
+
+               case md_toc:
+                       add(MenuItem(MenuItem::Toc));
+                       break;
+
+               case md_references:
+                       add(MenuItem(MenuItem::References));
+                       break;
+
                case md_viewformats:
                        add(MenuItem(MenuItem::ViewFormats));
                        break;
+
                case md_updateformats:
                        add(MenuItem(MenuItem::UpdateFormats));
                        break;
+
                case md_exportformats:
                        add(MenuItem(MenuItem::ExportFormats));
                        break;
+
+               case md_importformats:
+                       add(MenuItem(MenuItem::ImportFormats));
+                       break;
+
                case md_submenu: {
                        lex.next();
-                       char * tmp = strdup(lex.GetString().c_str());
-                       string mlabel = _(tmp);
-                       free(tmp);
+                       string mlabel = _(lex.GetString());
                        lex.next();
                        string mname = lex.GetString();
                        add(MenuItem(MenuItem::Submenu, mlabel, mname));
                        break;
                }
+
                case md_endmenu:
                        quit = true;
                        break;
+
                default:
                        lex.printError("menubar::read: "
                                       "Unknown menu tag: `$$Token'");
@@ -162,6 +201,136 @@ Menu & Menu::read(LyXLex & lex)
 }
 
 
+void Menu::checkShortcuts() const
+{
+       // This is a quadratic algorithm, but we do not care because
+       // it is used for debugging only.
+       for (const_iterator it1 = begin(); it1 != end(); ++it1) {
+               string shortcut = it1->shortcut();
+               if (shortcut.empty())
+                       continue;
+               if (!contains(it1->label(), shortcut))
+                       lyxerr << "Menu warning: menu entry \""
+                              << it1->label()
+                              << "\" does not contain shortcut `"
+                              << shortcut << '\'' << endl;
+               for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
+                       if (!compare_no_case(it2->shortcut(), shortcut)) {
+                               lyxerr << "Menu warning: menu entries "
+                                      << '"' << it1->fulllabel()
+                                      << "\" and \"" << it2->fulllabel()
+                                      << "\" share the same shortcut."
+                                      << endl;
+                       }
+               }
+       }
+}
+
+void Menu::expand(Menu & tomenu, Buffer * buf) const
+{
+       for (const_iterator cit = begin();
+            cit != end() ; ++cit) {
+               switch ((*cit).kind()) {
+               case MenuItem::Lastfiles: {
+                       int ii = 1;
+                       for (LastFiles::const_iterator lfit = lastfiles->begin();
+                            lfit != lastfiles->end() && ii < 10;
+                            ++lfit, ++ii) {
+                               string label = tostr(ii) + ". "
+                                       + MakeDisplayPath((*lfit), 30)
+                                       + '|' + tostr(ii);
+                               int action = lyxaction.
+                                       getPseudoAction(LFUN_FILE_OPEN,
+                                                       (*lfit));
+                               tomenu.add(MenuItem(MenuItem::Command,
+                                                   label, action));
+                       }
+               }
+               break;
+               
+               case MenuItem::Documents: {
+                       vector<string> names = bufferlist.getFileNames();
+                       
+                       if (names.empty()) {
+                               tomenu.add(MenuItem(MenuItem::Command,
+                                                   _("No Documents Open!"),
+                                                   LFUN_NOACTION));
+                               break;
+                       }
+
+                       for (vector<string>::const_iterator docit = names.begin();
+                            docit != names.end() ; ++docit) {
+                               int action =
+                                       lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
+                               string label = MakeDisplayPath(*docit, 30);
+                               tomenu.add(MenuItem(MenuItem::Command,
+                                                   label, action));
+                       }
+               }
+               break;
+
+               case MenuItem::ImportFormats:
+               case MenuItem::ViewFormats:
+               case MenuItem::UpdateFormats:
+               case MenuItem::ExportFormats: {
+                       vector<Format const *> formats;
+                       kb_action action;
+                       switch ((*cit).kind()) {
+                       case MenuItem::ImportFormats:
+                               formats = Importer::GetImportableFormats();
+                               action = LFUN_IMPORT;
+                               break;
+                       case MenuItem::ViewFormats:
+                               formats = Exporter::GetExportableFormats(buf, true); 
+                               action = LFUN_PREVIEW;
+                               break;
+                       case MenuItem::UpdateFormats:
+                               formats = Exporter::GetExportableFormats(buf, true); 
+                               action = LFUN_UPDATE;
+                               break;
+                       default:
+                               formats = Exporter::GetExportableFormats(buf, false); 
+                               action = LFUN_EXPORT;
+                       }
+                       sort(formats.begin(), formats.end());
+
+                       for (vector<Format const *>::const_iterator fit = formats.begin();
+                            fit != formats.end() ; ++fit) {
+                               if ((*fit)->dummy())
+                                       continue;
+                               string label = (*fit)->prettyname();
+                               if ((*cit).kind() == MenuItem::ImportFormats)
+                                       if ((*fit)->name() == "text")
+                                               label = _("Ascii text as lines");
+                                       else if ((*fit)->name() == "textparagraph")
+                                               label = _("Ascii text as paragraphs");
+                               if (!(*fit)->shortcut().empty())
+                                       label += "|" + (*fit)->shortcut();
+                               int action2 = lyxaction.
+                                       getPseudoAction(action, (*fit)->name());
+                               tomenu.add(MenuItem(MenuItem::Command,
+                                                   label, action2));
+                       }
+               }
+               break;
+                       
+               default:
+                       tomenu.add(*cit);
+               }
+       }
+
+       // Check whether the shortcuts are unique
+       if (lyxerr.debugging(Debug::GUI))
+               checkShortcuts();
+}
+
+bool Menu::hasSubmenu(string const & name) const
+{
+       return find_if(begin(), end(),
+                      compare_memfun(&MenuItem::submenu, name)) != end();
+}
+
+
 void MenuBackend::read(LyXLex & lex)
 {
        enum Menutags {
@@ -190,7 +359,7 @@ void MenuBackend::read(LyXLex & lex)
        bool menubar = false;
 
        while (lex.IsOK() && !quit) {
-               switch(lex.lex()) {
+               switch (lex.lex()) {
                case md_menubar: 
                        menubar = true;
                        // fallback to md_menu
@@ -275,7 +444,7 @@ void MenuBackend::defaults()
        add(main_nobuffer);
 
        if (lyxerr.debugging(Debug::GUI)) {
-               for(const_iterator cit = begin();
+               for (const_iterator cit = begin();
                    cit != end() ; ++cit)
                        lyxerr << "Menu name: " << cit->name() 
                               << ", Menubar: " << cit->menubar() 
@@ -292,36 +461,24 @@ void MenuBackend::add(Menu const & menu)
 
 bool MenuBackend::hasMenu(string const & name) const
 {
-       const_iterator mend = end();
-       for (const_iterator cit = begin(); cit != mend; ++cit) {
-               if ((*cit).name() == name)
-                       return true;
-       }
-       return false;
+       return find_if(begin(), end(),
+                      compare_memfun(&Menu::name, name)) != end();
 }
 
 
 Menu const & MenuBackend::getMenu(string const & name) const
 {
-       const_iterator mend = end();
-       for (const_iterator cit = begin(); cit != mend; ++cit) {
-               if ((*cit).name() == name)
-                       return (*cit);
-       }
-       Assert(false); // we actually require the name to exist.
-       return menulist_.front();
+       const_iterator cit = find_if(begin(), end(),
+                                    compare_memfun(&Menu::name, name));
+       Assert(cit != end());
+       return (*cit);
 }
 
 
 Menu & MenuBackend::getMenu(string const & name)
 {
-       MenuList::iterator end = menulist_.end();
-       for (MenuList::iterator cit = menulist_.begin(); 
-            cit != end; ++cit) {
-               if ((*cit).name() == name)
-                       return (*cit);
-       }
-       Assert(false); // we actually require the name to exist.
-       return menulist_.front();
+       MenuList::iterator it = find_if(menulist_.begin(), menulist_.end(),
+                                       compare_memfun(&Menu::name, name));
+       Assert(it != menulist_.end());
+       return (*it);
 }
-