]> git.lyx.org Git - lyx.git/blobdiff - src/ToolbarBackend.C
fix bug 2089: Touching Navigate menu crashes Lyx when a TOC inset is in a section...
[lyx.git] / src / ToolbarBackend.C
index 1d379e0ab6004d5654edaa608ca2bdee02cc2820..baa2ddfbcdfaf57865410b8aaaf1f53a02978fb4 100644 (file)
@@ -3,18 +3,19 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author unknown
+ * \author Jean-Marc Lasgouttes
+ * \author John Levon
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
 #include "ToolbarBackend.h"
+#include "funcrequest.h"
 #include "LyXAction.h"
 #include "lyxlex.h"
 #include "debug.h"
-#include "lyxlex.h"
 #include "gettext.h"
 
 #include "support/lstrings.h"
 
 #include "frontends/controllers/ControlMath.h"
 
-#include <vector>
+using lyx::support::compare_ascii_no_case;
+using lyx::support::getVectorFromString;
+using lyx::support::LibFileSearch;
+using lyx::support::subst;
 
 using std::endl;
+using std::make_pair;
+using std::string;
 using std::vector;
 
+
 ToolbarBackend toolbarbackend;
 
 namespace {
@@ -69,38 +76,12 @@ void ToolbarBackend::read(LyXLex & lex)
        Toolbar tb;
        tb.name = lex.getString();
        lex.next(true);
-       
-       tb.flags = static_cast<Flags>(0);
-       string flagstr = lex.getString();
-       vector<string> flags = getVectorFromString(flagstr);
-
-       vector<string>::const_iterator cit = flags.begin();
-       vector<string>::const_iterator end = flags.end();
-
-       for (; cit != end; ++cit) {
-               int flag = 0;
-               if (!compare_ascii_no_case(*cit, "off"))
-                       flag = OFF;
-               else if (!compare_ascii_no_case(*cit, "on"))
-                       flag = ON;
-               else if (!compare_ascii_no_case(*cit, "math"))
-                       flag = MATH;
-               else if (!compare_ascii_no_case(*cit, "table"))
-                       flag = TABLE;
-               else if (!compare_ascii_no_case(*cit, "top"))
-                       flag = TOP;
-               else if (!compare_ascii_no_case(*cit, "bottom"))
-                       flag = BOTTOM;
-               else if (!compare_ascii_no_case(*cit, "left"))
-                       flag = LEFT;
-               else if (!compare_ascii_no_case(*cit, "right"))
-                       flag = RIGHT;
-               else {
-                       lyxerr << "ToolbarBackend::read: unrecognised token:`"
-                              << *cit << '\'' << endl;
-               }
-               tb.flags = static_cast<Flags>(tb.flags | flag);
+       if (!lex.isOK()) {
+               lyxerr << "ToolbarBackend::read: Malformed toolbar "
+                       "description " <<  lex.getString() << endl;
+               return;
        }
+       tb.gui_name = lex.getString();
 
        bool quit = false;
 
@@ -115,24 +96,27 @@ void ToolbarBackend::read(LyXLex & lex)
                        if (lex.next(true)) {
                                string const tooltip = _(lex.getString());
                                lex.next(true);
-                               string const func = lex.getString();
+                               string const func_arg = lex.getString();
                                lyxerr[Debug::PARSER]
                                        << "ToolbarBackend::read TO_ADD func: `"
-                                       << func << '\'' << endl;
+                                       << func_arg << '\'' << endl;
+
+                               FuncRequest func =
+                                       lyxaction.lookupFunc(func_arg);
                                add(tb, func, tooltip);
                        }
                        break;
 
                case TO_MINIBUFFER:
-                       add(tb, MINIBUFFER);
+                       add(tb, FuncRequest(kb_action(MINIBUFFER)));
                        break;
 
                case TO_SEPARATOR:
-                       add(tb, SEPARATOR);
+                       add(tb, FuncRequest(kb_action(SEPARATOR)));
                        break;
 
                case TO_LAYOUTS:
-                       add(tb, LAYOUTS);
+                       add(tb, FuncRequest(kb_action(LAYOUTS)));
                        break;
 
                case TO_ENDTOOLBAR:
@@ -151,29 +135,87 @@ void ToolbarBackend::read(LyXLex & lex)
 }
 
 
-void ToolbarBackend::add(Toolbar & tb, int action, string const & tooltip)
+void ToolbarBackend::readToolbars(LyXLex & lex)
 {
-       tb.items.push_back(make_pair(action, tooltip));
-}
+       //consistency check
+       if (compare_ascii_no_case(lex.getString(), "toolbars")) {
+               lyxerr << "ToolbarBackend::read: ERROR wrong token:`"
+                      << lex.getString() << '\'' << endl;
+       }
+
+       lex.next(true);
 
+       while (lex.isOK()) {
+               string name = lex.getString();
+               lex.next(true);
 
-void ToolbarBackend::add(Toolbar & tb, string const & func, string const & tooltip)
-{
-       int const tf = lyxaction.LookupFunc(func);
+               if (!compare_ascii_no_case(name, "end"))
+                       return;
 
-       if (tf == -1) {
-               lyxerr << "ToolbarBackend::add: no LyX command called `"
-                      << func << "' exists!" << endl;
-       } else {
-               add(tb, tf, tooltip);
+               Toolbars::iterator tcit = toolbars.begin();
+               Toolbars::iterator tend = toolbars.end();
+               for (; tcit != tend; ++tcit) {
+                       if (tcit->name == name)
+                               break;
+               }
+
+               if (tcit == tend) {
+                       lyxerr << "ToolbarBackend: undefined toolbar "
+                               << name << endl;
+                       return;
+               }
+
+               tcit->flags = static_cast<Flags>(0);
+               string flagstr = lex.getString();
+               lex.next(true);
+               vector<string> flags = getVectorFromString(flagstr);
+
+               vector<string>::const_iterator cit = flags.begin();
+               vector<string>::const_iterator end = flags.end();
+
+               for (; cit != end; ++cit) {
+                       int flag = 0;
+                       if (!compare_ascii_no_case(*cit, "off"))
+                               flag = OFF;
+                       else if (!compare_ascii_no_case(*cit, "on"))
+                               flag = ON;
+                       else if (!compare_ascii_no_case(*cit, "math"))
+                               flag = MATH;
+                       else if (!compare_ascii_no_case(*cit, "table"))
+                               flag = TABLE;
+                       else if (!compare_ascii_no_case(*cit, "top"))
+                               flag = TOP;
+                       else if (!compare_ascii_no_case(*cit, "bottom"))
+                               flag = BOTTOM;
+                       else if (!compare_ascii_no_case(*cit, "left"))
+                               flag = LEFT;
+                       else if (!compare_ascii_no_case(*cit, "right"))
+                               flag = RIGHT;
+                       else {
+                               lyxerr << "ToolbarBackend::read: unrecognised token:`"
+                                      << *cit << '\'' << endl;
+                       }
+                       tcit->flags = static_cast<Flags>(tcit->flags | flag);
+               }
+
+               usedtoolbars.push_back(*tcit);
        }
 }
 
 
-string const ToolbarBackend::getIcon(int action)
+void ToolbarBackend::add(Toolbar & tb,
+                        FuncRequest const & func, string const & tooltip)
+{
+       tb.items.push_back(make_pair(func, tooltip));
+       tb.items.back().first.origin = FuncRequest::UI;
+}
+
+
+string const ToolbarBackend::getIcon(FuncRequest const & f)
 {
+       using lyx::frontend::find_xpm;
+
        string fullname;
-       FuncRequest f = lyxaction.retrieveActionArg(action);
 
        if (f.action == LFUN_INSERT_MATH) {
                if (!f.argument.empty())
@@ -201,5 +243,8 @@ string const ToolbarBackend::getIcon(int action)
                return fullname;
        }
 
+       lyxerr[Debug::GUI] << "Cannot find icon for command \""
+                          << lyxaction.getActionName(f.action)
+                          << '(' << f.argument << ")\"" << endl;
        return LibFileSearch("images", "unknown", "xpm");
 }