]> 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 acd8603743b7443c3283e19b84bdbc11cb893171..1196dbaf2c23aeb7fc1b906dc3136d837d93cffd 100644 (file)
@@ -23,7 +23,9 @@
 #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"
 
@@ -35,6 +37,7 @@ using std::endl;
 using std::vector;
 using std::pair;
 using std::find_if;
+using std::sort;
 
 // This is the global menu definition
 MenuBackend menubackend;
@@ -44,7 +47,7 @@ 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:
@@ -53,6 +56,7 @@ MenuItem::MenuItem(Kind kind, string const & label,
        case ViewFormats:
        case UpdateFormats:
        case ExportFormats:
+       case ImportFormats:
                break;
        case Command:
                action_ = lyxaction.LookupFunc(command);
@@ -86,6 +90,7 @@ Menu & Menu::read(LyXLex & lex)
                md_documents,
                md_endmenu,
                md_exportformats,
+               md_importformats,
                md_lastfiles,
                md_optitem,
                md_references,
@@ -101,6 +106,7 @@ Menu & Menu::read(LyXLex & lex)
                { "documents", md_documents },
                { "end", md_endmenu },
                { "exportformats", md_exportformats },
+               { "importformats", md_importformats },
                { "item", md_item },
                { "lastfiles", md_lastfiles },
                { "optitem", md_optitem }, 
@@ -120,7 +126,7 @@ 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
@@ -167,6 +173,10 @@ Menu & Menu::read(LyXLex & lex)
                        add(MenuItem(MenuItem::ExportFormats));
                        break;
 
+               case md_importformats:
+                       add(MenuItem(MenuItem::ImportFormats));
+                       break;
+
                case md_submenu: {
                        lex.next();
                        string mlabel = _(lex.GetString());
@@ -191,6 +201,31 @@ 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();
@@ -234,39 +269,65 @@ void Menu::expand(Menu & tomenu, Buffer * buf) const
                }
                break;
 
+               case MenuItem::ImportFormats:
                case MenuItem::ViewFormats:
                case MenuItem::UpdateFormats:
                case MenuItem::ExportFormats: {
-                       vector<pair<string,string> > names;
+                       vector<Format const *> formats;
                        kb_action action;
-                       if ((*cit).kind() == MenuItem::ViewFormats) {
-                               names = Exporter::GetViewableFormats(buf);
+                       switch ((*cit).kind()) {
+                       case MenuItem::ImportFormats:
+                               formats = Importer::GetImportableFormats();
+                               action = LFUN_IMPORT;
+                               break;
+                       case MenuItem::ViewFormats:
+                               formats = Exporter::GetExportableFormats(buf, true); 
                                action = LFUN_PREVIEW;
-                       } else if ((*cit).kind() == MenuItem::UpdateFormats) {
-                               names = Exporter::GetViewableFormats(buf);
+                               break;
+                       case MenuItem::UpdateFormats:
+                               formats = Exporter::GetExportableFormats(buf, true); 
                                action = LFUN_UPDATE;
-                       } else {
-                               names = Exporter::GetExportableFormats(buf);
+                               break;
+                       default:
+                               formats = Exporter::GetExportableFormats(buf, false); 
                                action = LFUN_EXPORT;
                        }
-
-                       for (vector<pair<string,string> >::const_iterator fit = names.begin();
-                            fit != names.end() ; ++fit) {
-                               int action2 =
-                                       lyxaction.getPseudoAction(action,
-                                                                 (*fit).first);
-                               string label = (*fit).second;
+                       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();
 }
 
 
@@ -298,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
@@ -383,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()