1 // Copyright (C) 2009-2015 Petr Pavlu <setup@dagobah.cz>
3 // This file is part of CenterIM.
5 // CenterIM is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // CenterIM is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with CenterIM. If not, see <http://www.gnu.org/licenses/>.
19 /// InputProcessor base class.
21 /// @ingroup cppconsui
23 #ifndef INPUTPROCESSOR_H
24 #define INPUTPROCESSOR_H
26 #include "CppConsUI.h"
28 #include <sigc++/sigc++.h>
29 #include <sigc++/signal.h>
38 /// Base class that takes care of input processing.
40 /// It allows to define:
41 /// - key-action bindings,
42 /// - a chain of input processors (top to bottom).
43 class InputProcessor
{
45 /// Defines when a key binding will be processed comparing with the child
46 /// input processor, @see ProcessInput.
48 /// Key bindings will be processed after the child input processor.
51 /// Key bindings will be processed before the child input processor.
56 virtual ~InputProcessor() {}
58 /// There are 4 steps when processing input:
61 /// <i>Overriding key combos</i><br>
62 /// Input is processed by checking for overriding key combinations. If a match
63 /// is found, the function for that combo is executed.
66 /// <i>Input child processing</i><br>
67 /// If an input child is assigned, processing is done recursively by this
71 /// <i>Other key combos</i><br>
72 /// Input is processed by checking for normal key combinations. If a match is
73 /// found, the signal for that combo is sent.
76 /// <i>Raw input processing</i><br>
77 /// Non key combo raw input processing by objects. Used for e.g. input
82 /// @return True if the input was successfully processed, false otherwise.
83 virtual bool processInput(const TermKeyKey
&key
);
86 /// Bindable struct holds a function and a bindable type that is associated to
87 /// some {context:action} pair.
89 Bindable() : type(BINDABLE_NORMAL
) {}
90 Bindable(const sigc::slot
<void> &function
, BindableType type
)
91 : function(function
), type(type
)
94 virtual ~Bindable() {}
95 // CONSUI_DISABLE_COPY(Bindable);
97 sigc::slot
<void> function
;
101 /// Holds all actions and Bindables for a context, {action: Bindable}.
102 typedef std::map
<std::string
, Bindable
> BindableContext
;
104 /// Holds all Key contexts for this class, {context: KeyContext}.
105 typedef std::map
<std::string
, BindableContext
> Bindables
;
107 /// The set of declared Bindables.
108 Bindables keybindings_
;
110 /// A child that will get to process the input.
111 InputProcessor
*input_child_
;
113 /// Set the child object that must process input before this object.
114 virtual void setInputChild(InputProcessor
&child
);
115 virtual void clearInputChild();
116 virtual InputProcessor
*getInputChild() { return input_child_
; }
118 /// Binds a (context, action) pair with a function.
120 /// The bind can be normal or override, depending on whether it needs to be
121 /// called after or before the @ref input_child_.
122 virtual void declareBindable(const char *context
, const char *action
,
123 const sigc::slot
<void> &function
, BindableType type
);
125 /// Tries to match an appropriate bound action to the input and process it.
126 /// @return True if a match was found and processed.
127 virtual bool process(BindableType type
, const TermKeyKey
&key
);
129 virtual bool processInputText(const TermKeyKey
&key
);
132 CONSUI_DISABLE_COPY(InputProcessor
);
135 } // namespace CppConsUI
137 #endif // INPUTPROCESSOR_H
139 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: