diff --git a/src/gtk/editor.cpp b/src/gtk/editor.cpp index 6bcfa280..0c16d5db 100644 --- a/src/gtk/editor.cpp +++ b/src/gtk/editor.cpp @@ -46,6 +46,7 @@ int main(int argc, char **argv) new DebugFps(); glmapview->LoadMap("maps/testmap.tmx"); + glmapview->SetFocus(); gtk_main(); diff --git a/src/gtk/editor.xml b/src/gtk/editor.xml index 98287858..42784659 100644 --- a/src/gtk/editor.xml +++ b/src/gtk/editor.xml @@ -23,8 +23,8 @@ True - True action_new + True True True @@ -32,8 +32,8 @@ True - True action_open + True True True @@ -158,8 +158,8 @@ True - True action_new + True toolbutton True @@ -171,8 +171,8 @@ True - True action_open + True Open... True @@ -184,8 +184,8 @@ True - True action_save + True Save True @@ -345,9 +345,11 @@ gtk-new + True Save gtk-save + True diff --git a/src/gtk/glmapview.cpp b/src/gtk/glmapview.cpp index ebe6b3f9..b3c27867 100644 --- a/src/gtk/glmapview.cpp +++ b/src/gtk/glmapview.cpp @@ -9,6 +9,7 @@ #include #include +#include #include "core.h" #include "glmapview.h" @@ -39,6 +40,8 @@ GlMapView::GlMapView(GtkBuilder *builder) GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK); + gtk_widget_set_can_focus(glarea, TRUE); + GtkContainer *cont = GTK_CONTAINER(gtk_builder_get_object(builder, "gl_container")); gtk_container_add(cont, glarea); @@ -63,6 +66,9 @@ GlMapView::GlMapView(GtkBuilder *builder) GTK_SIGNAL_FUNC(MouseButtonSignal), this); gtk_signal_connect(GTK_OBJECT(glarea), "motion_notify_event", GTK_SIGNAL_FUNC(MouseMotionSignal), this); + + gtk_signal_connect(GTK_OBJECT(glarea), "key_press_event", + GTK_SIGNAL_FUNC(KeyPressSignal), this); } void GlMapView::LoadMap(char const *path) @@ -73,6 +79,11 @@ void GlMapView::LoadMap(char const *path) UpdateAdjustments(); } +void GlMapView::SetFocus() +{ + gtk_widget_grab_focus(glarea); +} + gboolean GlMapView::IdleTick() { // FIXME: do not do anything if the previous tick was too recent? @@ -162,6 +173,19 @@ gboolean GlMapView::UpdateAdjustments() return TRUE; } +gboolean GlMapView::MoveAdjustments(double dx, double dy) +{ + if (dx) + gtk_adjustment_set_value(hadj, gtk_adjustment_get_value(hadj) + dx); + + if (dy) + gtk_adjustment_set_value(vadj, gtk_adjustment_get_value(vadj) + dy); + + UpdateAdjustments(); + + return TRUE; +} + gboolean GlMapView::MouseButton(GdkEventButton *event) { if (event->type == GDK_BUTTON_PRESS && event->button == 2) @@ -216,6 +240,20 @@ gboolean GlMapView::MouseMotion(GdkEventMotion *event) return TRUE; } +gboolean GlMapView::KeyPress(GdkEventKey *event) +{ + switch (event->keyval) + { + case GDK_Up: MoveAdjustments( 0.0, -10.0); break; + case GDK_Down: MoveAdjustments( 0.0, 10.0); break; + case GDK_Left: MoveAdjustments(-10.0, 0.0); break; + case GDK_Right: MoveAdjustments( 10.0, 0.0); break; + default: return FALSE; + } + + return TRUE; +} + /* Private signal slots */ gboolean GlMapView::IdleTickSignal(GlMapView *that) { @@ -263,3 +301,10 @@ gboolean GlMapView::MouseMotionSignal(GtkWidget *w, GdkEventMotion *event, return that->MouseMotion(event); } +gboolean GlMapView::KeyPressSignal(GtkWidget *w, GdkEventKey *event, + GlMapView *that) +{ + (void)w; + return that->KeyPress(event); +} + diff --git a/src/gtk/glmapview.h b/src/gtk/glmapview.h index 6b7fa655..02d1fa50 100644 --- a/src/gtk/glmapview.h +++ b/src/gtk/glmapview.h @@ -13,6 +13,7 @@ class GlMapView public: GlMapView(GtkBuilder *builder); void LoadMap(char const *path); + void SetFocus(); private: /* Private methods */ @@ -21,8 +22,10 @@ private: gboolean Destroy(); gboolean Draw(GdkEventExpose *event); gboolean UpdateAdjustments(); + gboolean MoveAdjustments(double dx, double dy); gboolean MouseButton(GdkEventButton *event); gboolean MouseMotion(GdkEventMotion *event); + gboolean KeyPress(GdkEventKey *event); /* Private signal slots */ static gboolean IdleTickSignal(GlMapView *that); @@ -36,6 +39,8 @@ private: GlMapView *that); static gboolean MouseMotionSignal(GtkWidget *w, GdkEventMotion *event, GlMapView *that); + static gboolean KeyPressSignal(GtkWidget *w, GdkEventKey *event, + GlMapView *that); private: GtkAdjustment *hadj, *vadj;