shithub: choc

Download patch

ref: 62b5f953677addeebac38ab6774599bbbab130f0
parent: bb630087923d51ce190461e8aba044bff04249eb
author: Simon Howard <[email protected]>
date: Sat May 20 12:34:34 EDT 2006

Add main loop function and forward key presses to widgets.

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 490

--- 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
--- a/textscreen/guitest.c
+++ b/textscreen/guitest.c
@@ -66,12 +66,7 @@
     Window2();
     SetupWindow();
 
-    for (;;)
-    {
-        firstwin->selected = (firstwin->selected + 1) % firstwin->num_widgets;
-
-        TXT_DrawDesktop();
-    }
+    TXT_GUIMainLoop();
 }
 
 
--- a/textscreen/txt_button.c
+++ b/textscreen/txt_button.c
@@ -47,6 +47,7 @@
 
 static int TXT_ButtonKeyPress(txt_widget_t *widget, int key)
 {
+    return 0;
 }
 
 txt_widget_class_t txt_button_class =
--- a/textscreen/txt_desktop.c
+++ b/textscreen/txt_desktop.c
@@ -131,3 +131,30 @@
     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();
+    }
+}
+
--- 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 @@
     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 @@
                 break;
 
             case SDL_KEYDOWN:
-                return ev.key.keysym.unicode;
+                return TranslateKey(&ev.key.keysym);
 
             case SDL_QUIT:
                 // Quit = escape
--- a/textscreen/txt_widget.c
+++ b/textscreen/txt_widget.c
@@ -17,12 +17,14 @@
     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;
 }
 
 
--- a/textscreen/txt_widget.h
+++ b/textscreen/txt_widget.h
@@ -52,7 +52,7 @@
 
 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 */
--- 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"
@@ -215,5 +217,56 @@
     window->horiz_align = horiz_align;
     window->x = x;
     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;
+            }
+        } 
+    }
 }