]> git.lyx.org Git - lyx.git/blob - src/LyXAction.h
Fix nasty memory bug signalled by valgrind while looking at another bug.
[lyx.git] / src / LyXAction.h
1 // -*- C++ -*-
2 /**
3  * \file LyXAction.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author John Levon
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef LYXACTION_H
14 #define LYXACTION_H
15
16 #include "FuncCode.h"
17
18 #include <map>
19 #include <string>
20
21
22 namespace lyx {
23
24 class FuncRequest;
25 class LyXErr;
26
27 /**
28  * This class is a container for LyX actions. It associates a name to
29  * most of them and describes some of their properties.
30  */
31 class LyXAction {
32 public:
33         /// category of an action, used in the Shortcuts dialog
34         enum FuncType {
35                 Hidden,  //< Not listed for configuration
36                 Edit,    //< Cursor and mouse movement, copy/paste etc
37                 Math,    //< Mathematics
38                 Buffer,  //< Buffer and window related
39                 Layout,  //< Font, Layout and textclass related
40                 System,  //< Lyx preference, server etc
41         };
42
43 private:
44         /// information for an action
45         struct FuncInfo {
46                 /// the action name
47                 std::string name;
48                 /// the FuncAttribs values set
49                 unsigned int attrib;
50                 /// the category of this func
51                 FuncType type;
52         };
53         /// type for map between a function name and its action
54         typedef std::map<std::string, FuncCode> FuncMap;
55         /// type for map between an action and its info
56         typedef std::map<FuncCode, FuncInfo> InfoMap;
57
58 public:
59         /// possible "permissions" for an action
60         enum FuncAttribs {
61                 Noop = 0, //< Nothing special about this func
62                 ReadOnly = 1, //< Can be used in RO mode (perhaps this should change); no automatic markDirty
63                 NoBuffer = 2, //< Can be used when there is no document open
64                 Argument = 4, //< Requires argument
65                 NoUpdate = 8, //< Does not (usually) require update
66                 SingleParUpdate = 16, //< Usually only requires this par updated
67                 AtPoint = 32, //< dispatch first to inset at cursor if there is one
68                 NoInternal = 64, //< Cannot be used for internal, non-document Buffers
69         };
70
71         LyXAction();
72
73         /**
74          * Creates a FuncRequest from a string of the form:
75          *   lyx-function [argument]
76          * where the argument is optional and "lyx-function" is in the form you'd
77          * enter it in the mini-buffer. 
78          */
79         FuncRequest lookupFunc(std::string const & func_name) const;
80
81         /// Return the command name associated with the given action
82         /// Thus: getActionName(LFUN_ERT_INSERT) --> "ert-insert".
83         std::string const getActionName(FuncCode action) const;
84         ///
85         FuncType getActionType(FuncCode action) const;
86
87         /// True if the command has `flag' set
88         bool funcHasFlag(FuncCode action, FuncAttribs flag) const;
89
90         /// iterator across all LFUNs
91         typedef FuncMap::const_iterator const_iterator;
92
93         /// return an iterator to the start of the list of LFUNs
94         const_iterator func_begin() const;
95
96         /// return an iterator to one past the end of the list of LFUNs
97         const_iterator func_end() const;
98
99 private:
100         /// noncopyable
101         LyXAction(LyXAction const &);
102         void operator=(LyXAction const &);
103
104         /// populate the action container with our actions
105         void init();
106         /// add the given action
107         void newFunc(FuncCode, std::string const & name, unsigned int attrib, FuncType type);
108
109         /**
110          * This maps LyX function names to function codes, e.g.:
111          *   lyx_func_map["ert-insert"] == LFUN_ERT_INSERT
112          */
113         FuncMap lyx_func_map;
114
115         /**
116          * This maps function codes to objects holding info about the corresponding
117          * action. E.g., if
118          *   FuncInfo const & ert = lyx_info_map[LFUN_ERT_INSERT];
119          * then:
120          *   ert.name   == "ert-insert"'
121          *   ert.attrib == Noop
122          *   ert.type   == Edit
123          */
124         InfoMap lyx_info_map;
125 };
126
127 LyXErr & operator<<(LyXErr &, FuncCode);
128
129 /// singleton instance
130 extern LyXAction lyxaction;
131
132
133 } // namespace lyx
134
135 #endif // LYXACTION_H