OSDN Git Service

separate intermediate directories for each projects to avoid confliction of dependenc...
[yamy/yamy.git] / keymap.h
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 // keymap.h
3
4
5 #ifndef _KEYMAP_H
6 #  define _KEYMAP_H
7
8 #  include "keyboard.h"
9 #  include "function.h"
10 #  include <vector>
11
12
13 ///
14 class Action
15 {
16 public:
17   ///
18   enum Type
19   {
20     Type_key,                                   ///
21     Type_keySeq,                                ///
22     Type_function,                              ///
23   };
24
25 private:
26   Action(const Action &i_action);
27   
28 public:
29   Action() { }
30   ///
31   virtual ~Action() { }
32   ///
33   virtual Type getType() const = 0;
34   /// create clone
35   virtual Action *clone() const = 0;
36   /// stream output
37   virtual tostream &output(tostream &i_ost) const = 0;
38 };
39
40 ///
41 tostream &operator<<(tostream &i_ost, const Action &i_action);
42
43 ///
44 class ActionKey : public Action
45 {
46 public:
47   ///
48   const ModifiedKey m_modifiedKey;
49
50 private:
51   ActionKey(const ActionKey &i_actionKey);
52   
53 public:
54   ///
55   ActionKey(const ModifiedKey &i_mk);
56   ///
57   virtual Type getType() const;
58   /// create clone
59   virtual Action *clone() const;
60   /// stream output
61   virtual tostream &output(tostream &i_ost) const;
62 };
63
64
65 class KeySeq;
66 ///
67 class ActionKeySeq : public Action
68 {
69 public:
70   KeySeq * const m_keySeq;                      ///
71
72 private:
73   ActionKeySeq(const ActionKeySeq &i_actionKeySeq);
74   
75 public:
76   ///
77   ActionKeySeq(KeySeq *i_keySeq);
78   ///
79   virtual Type getType() const;
80   /// create clone
81   virtual Action *clone() const;
82   /// stream output
83   virtual tostream &output(tostream &i_ost) const;
84 };
85
86
87 ///
88 class ActionFunction : public Action
89 {
90 public:
91   FunctionData * const m_functionData;          /// function data
92   const Modifier m_modifier;                    /// modifier for &Sync
93
94 private:
95   ActionFunction(const ActionFunction &i_actionFunction);
96   
97 public:
98   ///
99   ActionFunction(FunctionData *i_functionData,
100                  Modifier i_modifier = Modifier());
101   ///
102   virtual ~ActionFunction();
103   ///
104   virtual Type getType() const;
105   /// create clone
106   virtual Action *clone() const;
107   /// stream output
108   virtual tostream &output(tostream &i_ost) const;
109 };
110
111
112 ///
113 class KeySeq
114 {
115 public:
116   typedef std::vector<Action *> Actions;        /// 
117
118 private:
119   Actions m_actions;                            ///
120   tstringi m_name;                              ///
121   Modifier::Type m_mode;                        /** Either Modifier::Type_KEYSEQ
122                                                     or Modifier::Type_ASSIGN */
123
124 private:
125   ///
126   void copy();
127   ///
128   void clear();
129   
130 public:
131   ///
132   KeySeq(const tstringi &i_name);
133   ///
134   KeySeq(const KeySeq &i_ks);
135   ///
136   ~KeySeq();
137   
138   ///
139   const Actions &getActions() const { return m_actions; }
140   
141   ///
142   KeySeq &operator=(const KeySeq &i_ks);
143   
144   /// add
145   KeySeq &add(const Action &i_action);
146
147   /// get the first modified key of this key sequence
148   ModifiedKey getFirstModifiedKey() const;
149   
150   ///
151   const tstringi &getName() const { return m_name; }
152   
153   /// stream output
154   friend tostream &operator<<(tostream &i_ost, const KeySeq &i_ks);
155
156   ///
157   bool isCorrectMode(Modifier::Type i_mode) { return m_mode <= i_mode; }
158
159   ///
160   void setMode(Modifier::Type i_mode)
161   {
162     if (m_mode < i_mode)
163       m_mode = i_mode;
164     ASSERT( m_mode == Modifier::Type_KEYSEQ ||
165             m_mode == Modifier::Type_ASSIGN);
166   }
167
168   ///
169   Modifier::Type getMode() const { return m_mode; }
170 };
171
172
173 ///
174 class Keymap
175 {
176 public:
177   ///
178   enum Type
179   {
180     Type_keymap,                                /// this is keymap
181     Type_windowAnd,                             /// this is window &amp;&amp;
182     Type_windowOr,                              /// this is window ||
183   };
184   ///
185   enum AssignOperator
186   {
187     AO_new,                                     /// =
188     AO_add,                                     /// +=
189     AO_sub,                                     /// -=
190     AO_overwrite,                               /// !, !!
191   };
192   ///
193   enum AssignMode
194   {
195     AM_notModifier,                             ///    not modifier
196     AM_normal,                                  ///    normal modifier
197     AM_true,                                    /** !  true modifier(doesn't
198                                                     generate scan code) */
199     AM_oneShot,                                 /// !! one shot modifier
200     AM_oneShotRepeatable,                       /// !!! one shot modifier
201   };
202   
203   /// key assignment
204   class KeyAssignment
205   {
206   public:
207     ModifiedKey m_modifiedKey;  ///
208     KeySeq *m_keySeq;           ///
209
210   public:
211     ///
212     KeyAssignment(const ModifiedKey &i_modifiedKey, KeySeq *i_keySeq)
213       : m_modifiedKey(i_modifiedKey), m_keySeq(i_keySeq) { }
214     ///
215     KeyAssignment(const KeyAssignment &i_o)
216       : m_modifiedKey(i_o.m_modifiedKey), m_keySeq(i_o.m_keySeq) { }
217     ///
218     bool operator<(const KeyAssignment &i_o) const
219     { return m_modifiedKey < i_o.m_modifiedKey; }
220   };
221
222   /// modifier assignments
223   class ModAssignment
224   {
225   public:
226     AssignOperator m_assignOperator;    ///
227     AssignMode m_assignMode;            ///
228     Key *m_key;                         ///
229   };
230   typedef std::list<ModAssignment> ModAssignments; ///
231
232   /// parameter for describe();
233   class DescribeParam
234   {
235   private:
236     typedef std::list<ModifiedKey> DescribedKeys;
237     typedef std::list<const Keymap *> DescribedKeymap;
238     friend class Keymap;
239
240   private:
241     DescribedKeys m_dk;
242     DescribedKeymap m_dkeymap;
243     bool m_doesDescribeModifiers;
244
245   public:
246     DescribeParam() : m_doesDescribeModifiers(true) { }
247   };
248   
249 private:
250   /// key assignments (hashed by first scan code)
251   typedef std::list<KeyAssignment> KeyAssignments;
252   enum {
253     HASHED_KEY_ASSIGNMENT_SIZE = 32,    ///
254   };
255
256 private:
257   KeyAssignments m_hashedKeyAssignments[HASHED_KEY_ASSIGNMENT_SIZE];    ///
258   
259   /// modifier assignments
260   ModAssignments m_modAssignments[Modifier::Type_ASSIGN];
261
262   Type m_type;                                  /// type
263   tstringi m_name;                              /// keymap name
264   tregex m_windowClass;                         /// window class name regexp
265   tregex m_windowTitle;                         /// window title name regexp
266
267   KeySeq *m_defaultKeySeq;                      /// default keySeq
268   Keymap *m_parentKeymap;                       /// parent keymap
269   
270 private:
271   ///
272   KeyAssignments &getKeyAssignments(const ModifiedKey &i_mk);
273   ///
274   const KeyAssignments &getKeyAssignments(const ModifiedKey &i_mk) const;
275
276 public:
277   ///
278   Keymap(Type i_type,
279          const tstringi &i_name,
280          const tstringi &i_windowClass,
281          const tstringi &i_windowTitle,
282          KeySeq *i_defaultKeySeq,
283          Keymap *i_parentKeymap);
284   
285   /// add a key assignment;
286   void addAssignment(const ModifiedKey &i_mk, KeySeq *i_keySeq);
287
288   /// add modifier
289   void addModifier(Modifier::Type i_mt, AssignOperator i_ao,
290                    AssignMode i_am, Key *i_key);
291   
292   /// search
293   const KeyAssignment *searchAssignment(const ModifiedKey &i_mk) const;
294
295   /// get
296   const KeySeq *getDefaultKeySeq() const { return m_defaultKeySeq; }
297   ///
298   Keymap *getParentKeymap() const { return m_parentKeymap; }
299   ///
300   const tstringi &getName() const { return m_name; }
301
302   /// does same window
303   bool doesSameWindow(const tstringi i_className,
304                       const tstringi &i_titleName);
305   
306   /// adjust modifier
307   void adjustModifier(Keyboard &i_keyboard);
308
309   /// get modAssignments
310   const ModAssignments &getModAssignments(Modifier::Type i_mt) const
311   { return m_modAssignments[i_mt]; }
312
313   /// describe
314   void describe(tostream &i_ost, DescribeParam *i_dp) const;
315   
316   /// set default keySeq and parent keymap if default keySeq has not been set
317   bool setIfNotYet(KeySeq *i_keySeq, Keymap *i_parentKeymap);
318 };
319
320
321 /// stream output
322 extern tostream &operator<<(tostream &i_ost, const Keymap *i_keymap);
323
324
325 ///
326 class Keymaps
327 {
328 public:
329   typedef std::list<Keymap *> KeymapPtrList;    /// 
330   
331 private:
332   typedef std::list<Keymap> KeymapList;         /// 
333
334 private:
335   KeymapList m_keymapList;                      /** pointer into keymaps may
336                                                     exist */
337   
338 public:
339   ///
340   Keymaps();
341   
342   /// search by name
343   Keymap *searchByName(const tstringi &i_name);
344
345   /// search window
346   void searchWindow(KeymapPtrList *o_keymapPtrList,
347                     const tstringi &i_className,
348                     const tstringi &i_titleName);
349   
350   /// add keymap
351   Keymap *add(const Keymap &i_keymap);
352   
353   /// adjust modifier
354   void adjustModifier(Keyboard &i_keyboard);
355 };
356
357
358 ///
359 class KeySeqs
360 {
361 private:
362   typedef std::list<KeySeq> KeySeqList;         ///
363
364 private:
365   KeySeqList m_keySeqList;                      ///
366   
367 public:
368   /// add a named keyseq (name can be empty)
369   KeySeq *add(const KeySeq &i_keySeq);
370   
371   /// search by name
372   KeySeq *searchByName(const tstringi &i_name);
373 };
374
375
376 #endif // !_KEYMAP_H