3 #include "gtkmm2ext/bindings.h"
4 #include "gtkmm2ext/keyboard.h"
11 using namespace Gtkmm2ext
;
14 KeyboardKey::name () const
20 if (s
& Keyboard::PrimaryModifier
) {
23 if (s
& Keyboard::SecondaryModifier
) {
29 if (s
& Keyboard::TertiaryModifier
) {
35 if (s
& Keyboard::Level4Modifier
) {
46 str
+= gdk_keyval_name (key());
52 KeyboardKey::make_key (const string
& str
, KeyboardKey
& k
)
56 if (str
.find ("Primary") != string::npos
) {
57 s
|= Keyboard::PrimaryModifier
;
60 if (str
.find ("Secondary") != string::npos
) {
61 s
|= Keyboard::SecondaryModifier
;
64 if (str
.find ("Tertiary") != string::npos
) {
65 s
|= Keyboard::TertiaryModifier
;
68 if (str
.find ("Level4") != string::npos
) {
69 s
|= Keyboard::Level4Modifier
;
72 string::size_type lastmod
= str
.find_last_of ('-');
75 if (lastmod
== string::npos
) {
76 keyval
= gdk_keyval_from_name (str
.c_str());
78 keyval
= gdk_keyval_from_name (str
.substr (lastmod
+1).c_str());
81 if (keyval
== GDK_VoidSymbol
) {
85 k
= KeyboardKey (s
, keyval
);
99 Bindings::set_action_map (ActionMap
& am
)
102 press_bindings
.clear ();
103 release_bindings
.clear ();
107 Bindings::activate (KeyboardKey kb
, KeyboardKey::Operation op
)
112 case KeyboardKey::Press
:
113 kbm
= &press_bindings
;
115 case KeyboardKey::Release
:
116 kbm
= &release_bindings
;
120 KeybindingMap::iterator k
= kbm
->find (kb
);
122 if (k
== kbm
->end()) {
123 /* no entry for this key in the state map */
129 cerr
<< "Firing up " << k
->second
->get_name() << endl
;
130 k
->second
->activate ();
135 Bindings::add (KeyboardKey kb
, KeyboardKey::Operation op
, RefPtr
<Action
> what
)
140 case KeyboardKey::Press
:
141 kbm
= &press_bindings
;
143 case KeyboardKey::Release
:
144 kbm
= &release_bindings
;
148 KeybindingMap::iterator k
= kbm
->find (kb
);
150 if (k
== kbm
->end()) {
151 pair
<KeyboardKey
,RefPtr
<Action
> > newpair (kb
, what
);
152 kbm
->insert (newpair
);
159 Bindings::remove (KeyboardKey kb
, KeyboardKey::Operation op
)
164 case KeyboardKey::Press
:
165 kbm
= &press_bindings
;
167 case KeyboardKey::Release
:
168 kbm
= &release_bindings
;
172 KeybindingMap::iterator k
= kbm
->find (kb
);
174 if (k
!= kbm
->end()) {
180 Bindings::save (const string
& path
)
183 XMLNode
* root
= new XMLNode (X_("Bindings"));
184 tree
.set_root (root
);
186 XMLNode
* presses
= new XMLNode (X_("Press"));
187 root
->add_child_nocopy (*presses
);
189 for (KeybindingMap::iterator k
= press_bindings
.begin(); k
!= press_bindings
.end(); ++k
) {
191 child
= new XMLNode (X_("Binding"));
192 child
->add_property (X_("key"), k
->first
.name());
193 child
->add_property (X_("action"), k
->second
->get_name());
194 presses
->add_child_nocopy (*child
);
197 if (!tree
.write (path
)) {
198 ::unlink (path
.c_str());
206 Bindings::load (const string
& path
)
214 if (!tree
.read (path
)) {
218 press_bindings
.clear ();
219 release_bindings
.clear ();
221 XMLNode
& root (*tree
.root());
222 const XMLNodeList
& children (root
.children());
224 for (XMLNodeList::const_iterator i
= children
.begin(); i
!= children
.end(); ++i
) {
226 if ((*i
)->name() == X_("Press") || (*i
)->name() == X_("Release")) {
228 KeyboardKey::Operation op
;
230 if ((*i
)->name() == X_("Press")) {
231 op
= KeyboardKey::Press
;
233 op
= KeyboardKey::Release
;
236 const XMLNodeList
& gchildren ((*i
)->children());
238 for (XMLNodeList::const_iterator p
= gchildren
.begin(); p
!= gchildren
.end(); ++p
) {
243 ap
= (*p
)->property ("action");
244 kp
= (*p
)->property ("key");
250 RefPtr
<Action
> act
= action_map
->find_action (ap
->value());
258 if (!KeyboardKey::make_key (kp
->value(), k
)) {
271 ActionMap::find_action (const string
& name
)
273 _ActionMap::iterator a
= actions
.find (name
);
275 if (a
!= actions
.end()) {
279 return RefPtr
<Action
>();
283 ActionMap::register_action (const char* path
,
284 const char* name
, const char* label
, sigc::slot
<void> sl
)
288 RefPtr
<Action
> act
= Action::create (name
, label
);
290 act
->signal_activate().connect (sl
);
296 actions
.insert (_ActionMap::value_type (fullpath
, act
));
301 ActionMap::register_radio_action (const char* path
, Gtk::RadioAction::Group
& rgroup
,
302 const char* name
, const char* label
,
303 sigc::slot
<void,GtkAction
*> sl
,
308 RefPtr
<Action
> act
= RadioAction::create (rgroup
, name
, label
);
309 RefPtr
<RadioAction
> ract
= RefPtr
<RadioAction
>::cast_dynamic(act
);
310 ract
->property_value() = value
;
312 act
->signal_activate().connect (sigc::bind (sl
, act
->gobj()));
318 actions
.insert (_ActionMap::value_type (fullpath
, act
));
323 ActionMap::register_toggle_action (const char* path
,
324 const char* name
, const char* label
, sigc::slot
<void> sl
)
328 RefPtr
<Action
> act
= ToggleAction::create (name
, label
);
330 act
->signal_activate().connect (sl
);
336 actions
.insert (_ActionMap::value_type (fullpath
, act
));