]> git.lyx.org Git - lyx.git/blobdiff - src/MenuBackend.cpp
make frontend::Application a bit slimmer
[lyx.git] / src / MenuBackend.cpp
index 50c2d037cc9d4c7d2782c2762214d22f318faaa0..8368f8e5c31e594eff262f06e264ff972fcaa85f 100644 (file)
 #include "Buffer.h"
 #include "BufferList.h"
 #include "BufferParams.h"
+#include "Converter.h"
 #include "CutAndPaste.h"
 #include "debug.h"
-#include "Exporter.h"
 #include "Floating.h"
 #include "FloatList.h"
 #include "Format.h"
 #include "gettext.h"
-#include "Importer.h"
 #include "KeyMap.h"
 #include "Session.h"
 #include "LyXAction.h"
@@ -36,6 +35,7 @@
 #include "LyXFunc.h"
 #include "Lexer.h"
 #include "Paragraph.h"
+#include "TextClass.h"
 #include "TocBackend.h"
 #include "ToolbarBackend.h"
 
@@ -65,7 +65,7 @@ using std::max;
 using std::sort;
 using std::string;
 using std::vector;
-
+using std::stack;
 
 namespace {
 
@@ -73,8 +73,7 @@ class MenuNamesEqual : public std::unary_function<Menu, bool> {
 public:
        MenuNamesEqual(docstring const & name)
                : name_(name) {}
-       bool operator()(Menu const & menu) const
-       {
+       bool operator()(Menu const & menu) const {
                return menu.name() == name_;
        }
 private:
@@ -139,18 +138,15 @@ docstring const MenuItem::binding(bool forgui) const
 
        // Get the keys bound to this action, but keep only the
        // first one later
-       KeyMap::Bindings bindings = theTopLevelKeymap().findbindings(func_);
+       KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func_);
 
-       if (bindings.size()) {
-               return bindings.begin()->print(forgui);
-       } else {
-               LYXERR(Debug::KBMAP)
-                       << "No binding for "
-                       << lyxaction.getActionName(func_.action)
-                       << '(' << to_utf8(func_.argument()) << ')' << endl;
-               return docstring();
-       }
+       if (bindings.size())
+               return bindings.begin()->print(KeySequence::ForGui);
 
+       LYXERR(Debug::KBMAP, "No binding for "
+               << lyxaction.getActionName(func_.action)
+               << '(' << to_utf8(func_.argument()) << ')');
+       return docstring();
 }
 
 
@@ -218,6 +214,8 @@ Menu & Menu::read(Lexer & lex)
                md_documents,
                md_bookmarks,
                md_charstyles,
+               md_custom,
+               md_elements,
                md_endmenu,
                md_exportformats,
                md_importformats,
@@ -240,7 +238,9 @@ Menu & Menu::read(Lexer & lex)
                { "bookmarks", md_bookmarks },
                { "branches", md_branches },
                { "charstyles", md_charstyles },
+               { "custom", md_custom },
                { "documents", md_documents },
+               { "elements", md_elements },
                { "end", md_endmenu },
                { "exportformats", md_exportformats },
                { "floatinsert", md_floatinsert },
@@ -294,6 +294,14 @@ Menu & Menu::read(Lexer & lex)
                        add(MenuItem(MenuItem::CharStyles));
                        break;
 
+               case md_custom:
+                       add(MenuItem(MenuItem::Custom));
+                       break;
+
+               case md_elements:
+                       add(MenuItem(MenuItem::Elements));
+                       break;
+
                case md_documents:
                        add(MenuItem(MenuItem::Documents));
                        break;
@@ -411,6 +419,27 @@ void Menu::checkShortcuts() const
 }
 
 
+bool Menu::searchFunc(FuncRequest & func, stack<docstring> & names)
+{
+       const_iterator m = begin();
+       const_iterator m_end = end();
+       for (; m != m_end; ++m) {
+               if (m->kind() == MenuItem::Command && m->func() == func) {
+                       names.push(m->label());
+                       return true;
+               } else if (m->kind() == MenuItem::Submenu) {
+                       names.push(m->label());
+                       Menu submenu = menubackend.getMenu(m->submenuname());
+                       if (submenu.searchFunc(func, names))
+                               return true;
+                       else
+                               names.pop();
+               }
+       }
+       return false;
+}
+
+
 void MenuBackend::specialMenu(Menu const & menu)
 {
        specialmenu_ = menu;
@@ -456,23 +485,27 @@ void expandLastfiles(Menu & tomenu)
 
 void expandDocuments(Menu & tomenu)
 {
-       typedef vector<string> Strings;
-       Strings const names = theBufferList().getFileNames();
-
-       if (names.empty()) {
-               tomenu.add(MenuItem(MenuItem::Command, _("No Document Open!"),
-                                   FuncRequest(LFUN_NOACTION)));
-               return;
-       }
-
-       int ii = 1;
-       Strings::const_iterator docit = names.begin();
-       Strings::const_iterator end = names.end();
-       for (; docit != end; ++docit, ++ii) {
-               docstring label = makeDisplayPath(*docit, 20);
-               if (ii < 10)
-                       label = convert<docstring>(ii) + ". " + label + char_type('|') + convert<docstring>(ii);
-               tomenu.add(MenuItem(MenuItem::Command, label, FuncRequest(LFUN_BUFFER_SWITCH, *docit)));
+       Buffer * first = theBufferList().first();
+       if (first) {
+               Buffer * b = first;
+               int ii = 1;
+               
+               // We cannot use a for loop as the buffer list cycles.
+               do {
+                       docstring label = b->fileName().displayName(20);
+                       if (!b->isClean())
+                               label = label + "*";
+                       if (ii < 10)
+                               label = convert<docstring>(ii) + ". " + label + '|' + convert<docstring>(ii);
+                       tomenu.add(MenuItem(MenuItem::Command, label,
+                               FuncRequest(LFUN_BUFFER_SWITCH, b->absFileName())));
+                       
+                       b = theBufferList().next(b);
+                       ++ii;
+               } while (b != first); 
+       } else {
+               tomenu.add(MenuItem(MenuItem::Command, _("No Documents Open!"),
+                          FuncRequest(LFUN_NOACTION)));
        }
 }
 
@@ -508,19 +541,19 @@ void expandFormats(MenuItem::Kind kind, Menu & tomenu, Buffer const * buf)
 
        switch (kind) {
        case MenuItem::ImportFormats:
-               formats = Importer::GetImportableFormats();
+               formats = theConverters().importableFormats();
                action = LFUN_BUFFER_IMPORT;
                break;
        case MenuItem::ViewFormats:
-               formats = Exporter::getExportableFormats(*buf, true);
+               formats = buf->exportableFormats(true);
                action = LFUN_BUFFER_VIEW;
                break;
        case MenuItem::UpdateFormats:
-               formats = Exporter::getExportableFormats(*buf, true);
+               formats = buf->exportableFormats(true);
                action = LFUN_BUFFER_UPDATE;
                break;
        default:
-               formats = Exporter::getExportableFormats(*buf, false);
+               formats = buf->exportableFormats(false);
                action = LFUN_BUFFER_EXPORT;
        }
        sort(formats.begin(), formats.end(), compare_format());
@@ -610,7 +643,7 @@ void expandFloatInsert(Menu & tomenu, Buffer const * buf)
 }
 
 
-void expandCharStyleInsert(Menu & tomenu, Buffer const * buf)
+void expandFlexInsert(Menu & tomenu, Buffer const * buf, std::string s)
 {
        if (!buf) {
                tomenu.add(MenuItem(MenuItem::Command,
@@ -618,14 +651,15 @@ void expandCharStyleInsert(Menu & tomenu, Buffer const * buf)
                                    FuncRequest(LFUN_NOACTION)));
                return;
        }
-       CharStyles & charstyles =
-               buf->params().getTextClass().charstyles();
-       CharStyles::iterator cit = charstyles.begin();
-       CharStyles::iterator end = charstyles.end();
+       InsetLayouts const & insetlayouts =
+               buf->params().getTextClass().insetlayouts();
+       InsetLayouts::const_iterator cit = insetlayouts.begin();
+       InsetLayouts::const_iterator end = insetlayouts.end();
        for (; cit != end; ++cit) {
-               docstring const label = from_utf8(cit->name);
-               tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, label,
-                                   FuncRequest(LFUN_CHARSTYLE_INSERT,
+               docstring const label = cit->first;
+               if (cit->second.lyxtype == s)
+                       tomenu.addWithStatusCheck(MenuItem(MenuItem::Command, 
+                               label, FuncRequest(LFUN_FLEX_INSERT,
                                                label)));
        }
 }
@@ -705,8 +739,12 @@ void expandToc(Menu & tomenu, Buffer const * buf)
                return;
        }
 
+       Buffer* cbuf = const_cast<Buffer*>(buf);
+       cbuf->tocBackend().update();
+       cbuf->structureChanged();
+
        // Add an entry for the master doc if this is a child doc
-       Buffer const * const master = buf->getMasterBuffer();
+       Buffer const * const master = buf->masterBuffer();
        if (buf != master) {
                ParIterator const pit = par_iterator_begin(master->inset());
                string const arg = convert<string>(pit->id());
@@ -789,9 +827,10 @@ void expandToolbars(Menu & tomenu)
 
        for (; cit != end; ++cit) {
                docstring label = _(cit->gui_name);
-               // frontends are not supposed to turn on/off toolbars, if they cannot
-               // update ToolbarBackend::flags. That is to say, ToolbarsBackend::flags
-               // should reflect the true state of toolbars.
+               // frontends are not supposed to turn on/off toolbars,
+               // if they cannot update ToolbarBackend::flags. That
+               // is to say, ToolbarsBackend::flags should reflect
+               // the true state of toolbars.
                //
                // menu is displayed as
                //       on/off review
@@ -801,7 +840,7 @@ void expandToolbars(Menu & tomenu)
                if (cit->flags & ToolbarInfo::AUTO)
                        label += _(" (auto)");
                tomenu.add(MenuItem(MenuItem::Command, label,
-                                   FuncRequest(LFUN_TOOLBAR_TOGGLE_STATE, cit->name)));
+                                   FuncRequest(LFUN_TOOLBAR_TOGGLE, cit->name + " allowauto")));
        }
 }
 
@@ -815,7 +854,7 @@ void expandBranches(Menu & tomenu, Buffer const * buf)
                return;
        }
 
-       BufferParams const & params = buf->getMasterBuffer()->params();
+       BufferParams const & params = buf->masterBuffer()->params();
        if (params.branchlist().empty()) {
                tomenu.add(MenuItem(MenuItem::Command,
                                    _("No Branch in Document!"),
@@ -869,7 +908,15 @@ void MenuBackend::expand(Menu const & frommenu, Menu & tomenu,
                        break;
 
                case MenuItem::CharStyles:
-                       expandCharStyleInsert(tomenu, buf);
+                       expandFlexInsert(tomenu, buf, "charstyle");
+                       break;
+
+               case MenuItem::Custom:
+                       expandFlexInsert(tomenu, buf, "custom");
+                       break;
+
+               case MenuItem::Elements:
+                       expandFlexInsert(tomenu, buf, "element");
                        break;
 
                case MenuItem::FloatListInsert: