decoupling, moved private datastructures to Keys.cc

This commit is contained in:
Mathias Gumz 2008-01-02 09:28:00 +01:00
parent 7588fc10a6
commit ec13263a18
2 changed files with 89 additions and 80 deletions

View file

@ -31,7 +31,8 @@
#include "FbTk/StringUtil.hh" #include "FbTk/StringUtil.hh"
#include "FbTk/App.hh" #include "FbTk/App.hh"
#include "FbTk/Command.hh" #include "FbTk/Command.hh"
#include "FbTk/RefCount.hh"
#include "FbTk/KeyUtil.hh"
#include "FbTk/ObjectRegistry.hh" #include "FbTk/ObjectRegistry.hh"
#include "FbTk/I18n.hh" #include "FbTk/I18n.hh"
@ -90,6 +91,7 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <list>
#include <vector> #include <vector>
#include <map> #include <map>
#include <memory> #include <memory>
@ -101,7 +103,75 @@ using std::vector;
using std::ifstream; using std::ifstream;
using std::pair; using std::pair;
Keys::Keys(): m_display(FbTk::App::instance()->display()) { } // helper class 'keytree'
class Keys::t_key {
public:
// typedefs
typedef std::list<t_key*> keylist_t;
// constructor / destructor
t_key(int type, unsigned int mod, unsigned int key, int context,
bool isdouble);
t_key(t_key *k);
~t_key();
t_key *find(int type_, unsigned int mod_, unsigned int key_,
int context_, bool isdouble_) {
// t_key ctor sets context_ of 0 to GLOBAL, so we must here too
context_ = context_ ? context_ : GLOBAL;
keylist_t::iterator it = keylist.begin(), it_end = keylist.end();
for (; it != it_end; it++) {
if ((*it)->type == type_ && (*it)->key == key_ &&
((*it)->context & context_) > 0 &&
isdouble_ == (*it)->isdouble && (*it)->mod ==
FbTk::KeyUtil::instance().isolateModifierMask(mod_))
return *it;
}
return 0;
}
// member variables
int type; // KeyPress or ButtonPress
unsigned int mod;
unsigned int key; // key code or button number
int context; // ON_TITLEBAR, etc.: bitwise-or of all desired contexts
bool isdouble;
FbTk::RefCount<FbTk::Command> m_command;
keylist_t keylist;
};
Keys::t_key::t_key(int type_, unsigned int mod_, unsigned int key_,
int context_, bool isdouble_) :
type(type_),
mod(mod_),
key(key_),
context(context_),
isdouble(isdouble_),
m_command(0) {
context = context_ ? context_ : GLOBAL;
}
Keys::t_key::t_key(t_key *k) {
key = k->key;
mod = k->mod;
type = k->type;
context = k->context;
isdouble = k->isdouble;
m_command = k->m_command;
}
Keys::t_key::~t_key() {
for (keylist_t::iterator list_it = keylist.begin(); list_it != keylist.end(); ++list_it)
delete *list_it;
keylist.clear();
}
Keys::Keys() { }
Keys::~Keys() { Keys::~Keys() {
ungrabKeys(); ungrabKeys();
@ -168,8 +238,8 @@ void Keys::grabWindow(Window win) {
return; return;
m_handler_map[win]->grabButtons(); m_handler_map[win]->grabButtons();
keylist_t::iterator it = m_keylist->keylist.begin(); t_key::keylist_t::iterator it = m_keylist->keylist.begin();
keylist_t::iterator it_end = m_keylist->keylist.end(); t_key::keylist_t::iterator it_end = m_keylist->keylist.end();
for (; it != it_end; ++it) { for (; it != it_end; ++it) {
// keys are only grabbed in global context // keys are only grabbed in global context
if ((win_it->second & Keys::GLOBAL) > 0 && (*it)->type == KeyPress) if ((win_it->second & Keys::GLOBAL) > 0 && (*it)->type == KeyPress)
@ -271,7 +341,7 @@ bool Keys::addBinding(const string &linebuffer) {
FbTk::StringUtil::stringtok(val, linebuffer.c_str()); FbTk::StringUtil::stringtok(val, linebuffer.c_str());
// must have at least 1 argument // must have at least 1 argument
if (val.size() <= 0) if (val.empty())
return true; // empty lines are valid. return true; // empty lines are valid.
if (val[0][0] == '#' || val[0][0] == '!' ) //the line is commented if (val[0][0] == '#' || val[0][0] == '!' ) //the line is commented
@ -345,7 +415,7 @@ bool Keys::addBinding(const string &linebuffer) {
// +[1-9] - number between +1 and +9 // +[1-9] - number between +1 and +9
// numbers 10 and above // numbers 10 and above
// //
} else if (val[argc].size() > 1 && (isdigit(val[argc][0]) && } else if (!val[argc].empty() && (isdigit(val[argc][0]) &&
(isdigit(val[argc][1]) || val[argc][1] == 'x') || (isdigit(val[argc][1]) || val[argc][1] == 'x') ||
val[argc][0] == '+' && isdigit(val[argc][1])) ) { val[argc][0] == '+' && isdigit(val[argc][1])) ) {
@ -512,7 +582,7 @@ bool Keys::reconfigure(const char *filename) {
return load(filename); return load(filename);
} }
void Keys::keyMode(string keyMode) { void Keys::keyMode(const string& keyMode) {
keyspace_t::iterator it = m_map.find(keyMode + ":"); keyspace_t::iterator it = m_map.find(keyMode + ":");
if (it == m_map.end()) if (it == m_map.end())
setKeyMode(m_map["default:"]); setKeyMode(m_map["default:"]);
@ -530,8 +600,8 @@ void Keys::setKeyMode(t_key *keyMode) {
for (; h_it != h_it_end; ++h_it) for (; h_it != h_it_end; ++h_it)
h_it->second->grabButtons(); h_it->second->grabButtons();
keylist_t::iterator it = keyMode->keylist.begin(); t_key::keylist_t::iterator it = keyMode->keylist.begin();
keylist_t::iterator it_end = keyMode->keylist.end(); t_key::keylist_t::iterator it_end = keyMode->keylist.end();
for (; it != it_end; ++it) { for (; it != it_end; ++it) {
if ((*it)->type == KeyPress) if ((*it)->type == KeyPress)
grabKey((*it)->key, (*it)->mod); grabKey((*it)->key, (*it)->mod);
@ -540,28 +610,5 @@ void Keys::setKeyMode(t_key *keyMode) {
} }
m_keylist = keyMode; m_keylist = keyMode;
} }
Keys::t_key::t_key(int type_, unsigned int mod_, unsigned int key_,
int context_, bool isdouble_) {
key = key_;
mod = mod_;
type = type_;
context = context_ ? context_ : GLOBAL;
isdouble = isdouble_;
m_command = 0;
}
Keys::t_key::t_key(t_key *k) {
key = k->key;
mod = k->mod;
type = k->type;
context = k->context;
isdouble = k->isdouble;
m_command = k->m_command;
}
Keys::t_key::~t_key() {
for (keylist_t::iterator list_it = keylist.begin(); list_it != keylist.end(); ++list_it)
delete *list_it;
keylist.clear();
}

View file

@ -25,12 +25,9 @@
#define KEYS_HH #define KEYS_HH
#include "FbTk/NotCopyable.hh" #include "FbTk/NotCopyable.hh"
#include "FbTk/RefCount.hh"
#include "FbTk/Command.hh"
#include "FbTk/KeyUtil.hh" #include "FbTk/KeyUtil.hh"
#include <string> #include <string>
#include <list>
#include <map> #include <map>
@ -92,9 +89,14 @@ public:
@return true on success, else false @return true on success, else false
*/ */
bool reconfigure(const char *filename); bool reconfigure(const char *filename);
const std::string filename() const { return m_filename; } const std::string& filename() const { return m_filename; }
void keyMode(std::string keyMode); void keyMode(const std::string& keyMode);
private: private:
class t_key; // helper class to build a 'keytree'
typedef std::map<std::string, t_key *> keyspace_t;
typedef std::map<Window, int> WindowMap;
typedef std::map<Window, FbTk::EventHandler*> HandlerMap;
void deleteTree(); void deleteTree();
void grabKey(unsigned int key, unsigned int mod); void grabKey(unsigned int key, unsigned int mod);
@ -105,54 +107,14 @@ private:
// Load default keybindings for when there are errors loading the initial one // Load default keybindings for when there are errors loading the initial one
void loadDefaults(); void loadDefaults();
std::string m_filename;
class t_key;
typedef std::list<t_key*> keylist_t;
class t_key {
public:
t_key(int type, unsigned int mod, unsigned int key, int context,
bool isdouble);
t_key(t_key *k);
~t_key();
t_key *find(int type_, unsigned int mod_, unsigned int key_,
int context_, bool isdouble_) {
// t_key ctor sets context_ of 0 to GLOBAL, so we must here too
context_ = context_ ? context_ : GLOBAL;
keylist_t::iterator it = keylist.begin(), it_end = keylist.end();
for (; it != it_end; it++) {
if ((*it)->type == type_ && (*it)->key == key_ &&
((*it)->context & context_) > 0 &&
isdouble_ == (*it)->isdouble && (*it)->mod ==
FbTk::KeyUtil::instance().isolateModifierMask(mod_))
return *it;
}
return 0;
}
FbTk::RefCount<FbTk::Command> m_command;
int context; // ON_TITLEBAR, etc.: bitwise-or of all desired contexts
int type; // KeyPress or ButtonPress
unsigned int key; // key code or button number
unsigned int mod;
bool isdouble;
keylist_t keylist;
};
void setKeyMode(t_key *keyMode); void setKeyMode(t_key *keyMode);
typedef std::map<std::string, t_key *> keyspace_t;
// member variables
std::string m_filename;
t_key *m_keylist; t_key *m_keylist;
keyspace_t m_map; keyspace_t m_map;
Display *m_display; ///< display connection
typedef std::map<Window, int> WindowMap;
typedef std::map<Window, FbTk::EventHandler*> HandlerMap;
WindowMap m_window_map; WindowMap m_window_map;
HandlerMap m_handler_map; HandlerMap m_handler_map;
}; };