foxygit / doom Log in
commit 62b5f953677addeebac38ab6774599bbbab130f0
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Sat May 20 16:34:34 2006 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Sat May 20 16:34:34 2006 +0000

    Add main loop function and forward key presses to widgets.

    Subversion-branch: /trunk/chocolate-doom
    Subversion-revision: 490
---
 textscreen/Makefile.am   |  2 +-
 textscreen/guitest.c     |  7 +---
 textscreen/txt_button.c  |  1 +
 textscreen/txt_desktop.c | 27 +++++++++++++++
 textscreen/txt_main.c    | 87 ++++++++++++++++++++++++++++++++++++++++++++++--
 textscreen/txt_widget.c  |  6 ++--
 textscreen/txt_widget.h  |  2 +-
 textscreen/txt_window.c  | 53 +++++++++++++++++++++++++++++
 8 files changed, 173 insertions(+), 12 deletions(-)

diff --git a/textscreen/Makefile.am b/textscreen/Makefile.am
index c00f6179..17f9f39d 100644
--- a/textscreen/Makefile.am
+++ b/textscreen/Makefile.am
@@ -1,5 +1,5 @@

-AM_CFLAGS = @SDL_CFLAGS@
+AM_CFLAGS = @SDL_CFLAGS@ -I../src

 noinst_LIBRARIES=libtextscreen.a
 bin_PROGRAMS=guitest
diff --git a/textscreen/guitest.c b/textscreen/guitest.c
index 44da349c..66f2a363 100644
--- a/textscreen/guitest.c
+++ b/textscreen/guitest.c
@@ -66,12 +66,7 @@ int main()
     Window2();
     SetupWindow();

-    for (;;)
-    {
-        firstwin->selected = (firstwin->selected + 1) % firstwin->num_widgets;
-
-        TXT_DrawDesktop();
-    }
+    TXT_GUIMainLoop();
 }


diff --git a/textscreen/txt_button.c b/textscreen/txt_button.c
index 4bf5b9c0..35457d69 100644
--- a/textscreen/txt_button.c
+++ b/textscreen/txt_button.c
@@ -47,6 +47,7 @@ static void TXT_ButtonDestructor(txt_widget_t *widget)

 static int TXT_ButtonKeyPress(txt_widget_t *widget, int key)
 {
+    return 0;
 }

 txt_widget_class_t txt_button_class =
diff --git a/textscreen/txt_desktop.c b/textscreen/txt_desktop.c
index a2ec5c56..4cfe30a0 100644
--- a/textscreen/txt_desktop.c
+++ b/textscreen/txt_desktop.c
@@ -131,3 +131,30 @@ void TXT_DrawDesktop(void)
     TXT_UpdateScreen();
 }

+void TXT_DispatchEvents(void)
+{
+    int c;
+
+    while ((c = TXT_GetChar()) > 0)
+    {
+        if (c == 27)
+            exit(0);
+
+        if (num_windows > 0)
+        {
+            // Send the keypress to the top window
+
+            TXT_WindowKeyPress(all_windows[num_windows - 1], c);
+        }
+    }
+}
+
+void TXT_GUIMainLoop(void)
+{
+    for (;;)
+    {
+        TXT_DispatchEvents();
+        TXT_DrawDesktop();
+    }
+}
+
diff --git a/textscreen/txt_main.c b/textscreen/txt_main.c
index 256775a8..5682c023 100644
--- a/textscreen/txt_main.c
+++ b/textscreen/txt_main.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*-
 //-----------------------------------------------------------------------------
 //
-// $Id: txt_main.c 291 2006-01-13 23:56:00Z fraggle $
+// $Id: txt_main.c 490 2006-05-20 16:34:34Z fraggle $
 //
 // Copyright(C) 1993-1996 Id Software, Inc.
 // Copyright(C) 2005 Simon Howard
@@ -49,6 +49,8 @@
 #include <stdlib.h>
 #include <string.h>

+#include "doomkeys.h"
+
 #include "txt_main.h"
 #include "txt_font.h"

@@ -183,6 +185,87 @@ void TXT_UpdateScreen(void)
     TXT_UpdateScreenArea(0, 0, TXT_SCREEN_W, TXT_SCREEN_H);
 }

+//
+// Translates the SDL key
+//
+
+static int TranslateKey(SDL_keysym *sym)
+{
+    switch(sym->sym)
+    {
+      case SDLK_LEFT:        return KEY_LEFTARROW;
+      case SDLK_RIGHT:       return KEY_RIGHTARROW;
+      case SDLK_DOWN:        return KEY_DOWNARROW;
+      case SDLK_UP:          return KEY_UPARROW;
+      case SDLK_ESCAPE:      return KEY_ESCAPE;
+      case SDLK_RETURN:      return KEY_ENTER;
+      case SDLK_TAB:         return KEY_TAB;
+      case SDLK_F1:          return KEY_F1;
+      case SDLK_F2:          return KEY_F2;
+      case SDLK_F3:          return KEY_F3;
+      case SDLK_F4:          return KEY_F4;
+      case SDLK_F5:          return KEY_F5;
+      case SDLK_F6:          return KEY_F6;
+      case SDLK_F7:          return KEY_F7;
+      case SDLK_F8:          return KEY_F8;
+      case SDLK_F9:          return KEY_F9;
+      case SDLK_F10:         return KEY_F10;
+      case SDLK_F11:         return KEY_F11;
+      case SDLK_F12:         return KEY_F12;
+
+      case SDLK_BACKSPACE:   return KEY_BACKSPACE;
+      case SDLK_DELETE:      return KEY_DEL;
+
+      case SDLK_PAUSE:       return KEY_PAUSE;
+
+      case SDLK_EQUALS:
+      case SDLK_KP_EQUALS:   return KEY_EQUALS;
+
+      case SDLK_MINUS:       return KEY_MINUS;
+
+      case SDLK_LSHIFT:
+      case SDLK_RSHIFT:
+                             return KEY_RSHIFT;
+
+      case SDLK_LCTRL:
+      case SDLK_RCTRL:
+                             return KEY_RCTRL;
+
+      case SDLK_LALT:
+      case SDLK_LMETA:
+      case SDLK_RALT:
+      case SDLK_RMETA:
+                             return KEY_RALT;
+
+      case SDLK_CAPSLOCK:    return KEY_CAPSLOCK;
+      case SDLK_SCROLLOCK:   return KEY_SCRLCK;
+
+      case SDLK_KP0:         return KEYP_0;
+      case SDLK_KP1:         return KEYP_1;
+      case SDLK_KP2:         return KEYP_2;
+      case SDLK_KP3:         return KEYP_3;
+      case SDLK_KP4:         return KEYP_4;
+      case SDLK_KP5:         return KEYP_5;
+      case SDLK_KP6:         return KEYP_6;
+      case SDLK_KP7:         return KEYP_7;
+      case SDLK_KP8:         return KEYP_8;
+      case SDLK_KP9:         return KEYP_9;
+
+      case SDLK_HOME:        return KEY_HOME;
+      case SDLK_INSERT:      return KEY_INS;
+      case SDLK_END:         return KEY_END;
+      case SDLK_PAGEUP:      return KEY_PGUP;
+      case SDLK_PAGEDOWN:    return KEY_PGDN;
+      case SDLK_KP_MULTIPLY: return KEYP_MULTIPLY;
+      case SDLK_KP_PLUS:     return KEYP_PLUS;
+      case SDLK_KP_MINUS:    return KEYP_MINUS;
+      case SDLK_KP_DIVIDE:   return KEYP_DIVIDE;
+
+      default:               return tolower(sym->sym);
+    }
+}
+
+
 signed int TXT_GetChar(void)
 {
     SDL_Event ev;
@@ -201,7 +284,7 @@ signed int TXT_GetChar(void)
                 break;

             case SDL_KEYDOWN:
-                return ev.key.keysym.unicode;
+                return TranslateKey(&ev.key.keysym);

             case SDL_QUIT:
                 // Quit = escape
diff --git a/textscreen/txt_widget.c b/textscreen/txt_widget.c
index 68e3d9b5..de79beb7 100644
--- a/textscreen/txt_widget.c
+++ b/textscreen/txt_widget.c
@@ -17,12 +17,14 @@ void TXT_DestroyWidget(txt_widget_t *widget)
     widget->widget_class->destructor(widget);
 }

-void TXT_WidgetKeyPress(txt_widget_t *widget, int key)
+int TXT_WidgetKeyPress(txt_widget_t *widget, int key)
 {
     if (widget->widget_class->key_press != NULL)
     {
-        widget->widget_class->key_press(widget, key);
+        return widget->widget_class->key_press(widget, key);
     }
+
+    return 0;
 }


diff --git a/textscreen/txt_widget.h b/textscreen/txt_widget.h
index e0a4350e..9852f732 100644
--- a/textscreen/txt_widget.h
+++ b/textscreen/txt_widget.h
@@ -52,7 +52,7 @@ struct txt_widget_s

 int TXT_WidgetWidth(txt_widget_t *widget);
 void TXT_DrawWidget(txt_widget_t *widget, int w, int selected);
-void TXT_WidgetKeyPress(txt_widget_t *widget, int key);
+int TXT_WidgetKeyPress(txt_widget_t *widget, int key);
 void TXT_DestroyWidget(txt_widget_t *widget);

 #endif /* #ifndef TXT_WIDGET_H */
diff --git a/textscreen/txt_window.c b/textscreen/txt_window.c
index 1dcb2d88..85c6c5d1 100644
--- a/textscreen/txt_window.c
+++ b/textscreen/txt_window.c
@@ -25,6 +25,8 @@
 #include <stdlib.h>
 #include <string.h>

+#include "doomkeys.h"
+
 #include "txt_desktop.h"
 #include "txt_gui.h"
 #include "txt_main.h"
@@ -217,3 +219,54 @@ void TXT_SetWindowPosition(txt_window_t *window,
     window->y = y;
 }

+void TXT_WindowKeyPress(txt_window_t *window, int c)
+{
+    // Send to the currently selected widget first
+
+    if (window->selected > 0 && window->selected <= window->num_widgets)
+    {
+        if (TXT_WidgetKeyPress(window->widgets[window->selected], c))
+        {
+            return;
+        }
+    }
+
+    if (c == KEY_DOWNARROW)
+    {
+        int newsel;
+
+        // Move cursor down to the next selectable widget
+
+        for (newsel = window->selected + 1;
+             newsel < window->num_widgets;
+             ++newsel)
+        {
+            if (window->widgets[newsel]->visible
+             && window->widgets[newsel]->selectable)
+            {
+                window->selected = newsel;
+                break;
+            }
+        }
+    }
+
+    if (c == KEY_UPARROW)
+    {
+        int newsel;
+
+        // Move cursor down to the next selectable widget
+
+        for (newsel = window->selected - 1;
+             newsel >= 0;
+             --newsel)
+        {
+            if (window->widgets[newsel]->visible
+             && window->widgets[newsel]->selectable)
+            {
+                window->selected = newsel;
+                break;
+            }
+        }
+    }
+}
+