ocarina/ocarina/shortcut.cpp
2011-12-18 15:07:44 -05:00

46 lines
929 B
C++

#include <gdk/gdk.h>
#include <ocarina/shortcut.h>
#include <ocarina/window.h>
#include <ocarina/header.h>
#include <libsaria/print.h>
#include <map>
#include <string>
using namespace std;
static map<string, void (*)()> shortcut_map;
void register_shortcut(string key, void (*func)())
{
shortcut_map[key] = func;
}
/*
* Returns FALSE to allow other functions to run
* Returns TRUE if other handlers should not be run
*/
gboolean handle_shortcut(GtkWidget *widget, GdkEvent *event, gpointer data)
{
string key = gdk_keyval_name(event->key.keyval);
map<string, void (*)()>::iterator it;
if (key == "Escape") {
window_focus();
return TRUE;
}
/* Don't run shortcuts if user is filtering */
if (entry_focused() == true)
return FALSE;
it = shortcut_map.find(key);
if (it == shortcut_map.end()) {
println("No function registered for key: %s", key.c_str());
return FALSE;
}
it->second();
return TRUE;
}