test/testime.c
changeset 3280 00cace2d9080
child 3281 2d3a9f229ba1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testime.c	Sat Sep 19 13:29:40 2009 +0000
     1.3 @@ -0,0 +1,275 @@
     1.4 +/* A simple program to test the Input Method support in the SDL library (1.3+) */
     1.5 + 
     1.6 +#include <stdlib.h>
     1.7 +#include <stdio.h>
     1.8 +#include <string.h>
     1.9 +
    1.10 +#include "SDL.h"
    1.11 +#ifdef HAVE_SDL_TTF
    1.12 +#include "SDL_ttf.h"
    1.13 +#endif
    1.14 + 
    1.15 +#define DEFAULT_PTSIZE  30
    1.16 +#define DEFAULT_FONT    "/System/Library/Fonts/华文细黑.ttf"
    1.17 +#define MAX_TEXT_LENGTH 256
    1.18 +
    1.19 +SDL_Surface *screen;
    1.20 +
    1.21 +#ifdef HAVE_SDL_TTF
    1.22 +TTF_Font *font;
    1.23 +#endif
    1.24 +SDL_Rect textRect, markedRect;
    1.25 +Uint32 lineColor, backColor;
    1.26 +SDL_Color textColor = { 0, 0, 0 };
    1.27 +char text[MAX_TEXT_LENGTH], *markedText;
    1.28 +
    1.29 +void usage()
    1.30 +{
    1.31 +    printf("usage: testime [--font fontfile] [--fullscreen]\n");
    1.32 +    exit(0);
    1.33 +}
    1.34 +
    1.35 +void InitVideo(int argc, char *argv[])
    1.36 +{
    1.37 +    int width = 500, height = 250;
    1.38 +    int flags = SDL_HWSURFACE;
    1.39 +    const char *fontname = DEFAULT_FONT;
    1.40 +    int fullscreen = 0;
    1.41 +
    1.42 +    for (argc--, argv++; argc > 0; argc--, argv++)
    1.43 +    {
    1.44 +        if (strcmp(argv[0], "--help") == 0)
    1.45 +            usage();
    1.46 +
    1.47 +        else if (strcmp(argv[0], "--fullscreen") == 0)
    1.48 +            fullscreen = 1;
    1.49 +
    1.50 +        else if (strcmp(argv[0], "--font") == 0)
    1.51 +        {
    1.52 +            argc--;
    1.53 +            argv++;
    1.54 +
    1.55 +            if (argc > 0)
    1.56 +                fontname = argv[0];
    1.57 +            else
    1.58 +                usage();
    1.59 +        }
    1.60 +    }
    1.61 +
    1.62 +    SDL_putenv("SDL_VIDEO_WINDOW_POS=center");
    1.63 +    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    1.64 +    {
    1.65 +        fprintf(stderr, "Unable to init SDL: %s\n", TTF_GetError());
    1.66 +        exit(-1);
    1.67 +    }
    1.68 +
    1.69 +#ifdef HAVE_SDL_TTF
    1.70 +    /* Initialize fonts */
    1.71 +    TTF_Init();
    1.72 +
    1.73 +    font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
    1.74 +    if (! font)
    1.75 +    {
    1.76 +        fprintf(stderr, "Failed to find font: %s\n", SDL_GetError());
    1.77 +        exit(-1);
    1.78 +    }
    1.79 +#endif
    1.80 +
    1.81 +    printf("Using font: %s\n", fontname);
    1.82 +    atexit(SDL_Quit);
    1.83 +
    1.84 +    if (fullscreen)
    1.85 +    {
    1.86 +        SDL_DisplayMode mode;
    1.87 +        SDL_GetDesktopDisplayMode(&mode);
    1.88 +
    1.89 +        width = mode.w;
    1.90 +        height = mode.h;
    1.91 +        fprintf(stderr, "%dx%d\n", width, height);
    1.92 +        flags |= SDL_FULLSCREEN;
    1.93 +    }
    1.94 +
    1.95 +    /* Create window */
    1.96 +    screen = SDL_SetVideoMode(width, height, 32, flags);
    1.97 +    if (screen == NULL)
    1.98 +    {
    1.99 +        fprintf(stderr, "Unable to set %dx%d video: %s\n",
   1.100 +                width, height, SDL_GetError());
   1.101 +        exit(-1);
   1.102 +    }
   1.103 +}
   1.104 +
   1.105 +void CleanupVideo()
   1.106 +{
   1.107 +    SDL_StopTextInput();
   1.108 +#ifdef HAVE_SDL_TTF
   1.109 +    TTF_CloseFont(font);
   1.110 +    TTF_Quit();
   1.111 +#endif
   1.112 +}
   1.113 +
   1.114 +void InitInput()
   1.115 +{
   1.116 +    backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
   1.117 +    lineColor = SDL_MapRGB(screen->format, 0x0, 0x0, 0x0);
   1.118 +
   1.119 +    /* Prepare a rect for text input */
   1.120 +    textRect.x = textRect.y = 100;
   1.121 +    textRect.w = screen->w - 2 * textRect.x;
   1.122 +    textRect.h = 50;
   1.123 +
   1.124 +    text[0] = 0;
   1.125 +    markedRect = textRect;
   1.126 +    markedText = NULL;
   1.127 +
   1.128 +    SDL_StartTextInput();
   1.129 +}
   1.130 +
   1.131 +#ifdef HAVE_SDL_TTF
   1.132 +static void RenderText(SDL_Surface *sur,
   1.133 +                        TTF_Font *font,
   1.134 +                        const char *text,
   1.135 +                        int x, int y,
   1.136 +                        SDL_Color color)
   1.137 +{
   1.138 +    SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, color);
   1.139 +    SDL_Rect dest = { x, y, textSur->w, textSur->h };
   1.140 +
   1.141 +    SDL_BlitSurface(textSur, NULL, sur, &dest);
   1.142 +    SDL_FreeSurface(textSur);
   1.143 +}
   1.144 +#endif
   1.145 +
   1.146 +void Redraw()
   1.147 +{
   1.148 +    int w = 0, h = textRect.h;
   1.149 +    SDL_Rect cursorRect, underlineRect;
   1.150 +
   1.151 +    SDL_FillRect(screen, &textRect, backColor);
   1.152 +
   1.153 +#ifdef HAVE_SDL_TTF
   1.154 +    if (strlen(text))
   1.155 +    {
   1.156 +        RenderText(screen, font, text, textRect.x, textRect.y, textColor);
   1.157 +        TTF_SizeUTF8(font, text, &w, &h);
   1.158 +    }
   1.159 +#endif
   1.160 +
   1.161 +    markedRect.x = textRect.x + w;
   1.162 +    markedRect.w = textRect.w - w;
   1.163 +    if (markedRect.w < 0)
   1.164 +    {
   1.165 +        SDL_Flip(screen);
   1.166 +        // Stop text input because we cannot hold any more characters
   1.167 +        SDL_StopTextInput();
   1.168 +        return;
   1.169 +    }
   1.170 +
   1.171 +    cursorRect = markedRect;
   1.172 +    cursorRect.w = 2;
   1.173 +    cursorRect.h = h;
   1.174 +
   1.175 +    SDL_FillRect(screen, &markedRect, backColor);
   1.176 +    if (markedText)
   1.177 +    {
   1.178 +#ifdef HAVE_SDL_TTF
   1.179 +        RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor);
   1.180 +        TTF_SizeUTF8(font, markedText, &w, &h);
   1.181 +#endif
   1.182 +
   1.183 +        underlineRect = markedRect;
   1.184 +        underlineRect.y += (h - 2);
   1.185 +        underlineRect.h = 2;
   1.186 +        underlineRect.w = w;
   1.187 +
   1.188 +        cursorRect.x += w + 1;
   1.189 +
   1.190 +        SDL_FillRect(screen, &underlineRect, lineColor);
   1.191 +    }
   1.192 +
   1.193 +    SDL_FillRect(screen, &cursorRect, lineColor);
   1.194 +
   1.195 +    SDL_Flip(screen);
   1.196 +
   1.197 +    SDL_SetTextInputRect(&markedRect);
   1.198 +}
   1.199 +
   1.200 +void
   1.201 +HotKey_ToggleFullScreen(void)
   1.202 +{
   1.203 +    SDL_Surface *screen;
   1.204 +
   1.205 +    screen = SDL_GetVideoSurface();
   1.206 +    if (SDL_WM_ToggleFullScreen(screen)) {
   1.207 +        printf("Toggled fullscreen mode - now %s\n",
   1.208 +               (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
   1.209 +    } else {
   1.210 +        printf("Unable to toggle fullscreen mode\n");
   1.211 +    }
   1.212 +}
   1.213 +
   1.214 +int main(int argc, char *argv[])
   1.215 +{
   1.216 +    InitVideo(argc, argv);
   1.217 +    InitInput();
   1.218 +    Redraw();
   1.219 +
   1.220 +    SDL_Event event;
   1.221 +    int done = 0;
   1.222 +
   1.223 +    while (! done && SDL_WaitEvent(&event))
   1.224 +    {
   1.225 +        switch (event.type)
   1.226 +        {
   1.227 +        case SDL_KEYDOWN:
   1.228 +            if (event.key.keysym.sym == SDLK_ESCAPE) {
   1.229 +                done = 1;
   1.230 +                break;
   1.231 +            }
   1.232 +
   1.233 +            fprintf(stderr,
   1.234 +                    "Keyboard %d: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
   1.235 +                    event.key.which, event.key.keysym.scancode,
   1.236 +                    SDL_GetScancodeName(event.key.keysym.scancode),
   1.237 +                    event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
   1.238 +            break;
   1.239 +
   1.240 +        case SDL_TEXTINPUT:
   1.241 +            if (strlen(event.text.text) == 0 || event.text.text[0] == '\n' ||
   1.242 +                markedRect.w < 0)
   1.243 +                break;
   1.244 +
   1.245 +            fprintf(stderr, "Keyboard %d: text input \"%s\"\n",
   1.246 +                    event.text.which, event.text.text);
   1.247 +
   1.248 +            if (strlen(text) + strlen(event.text.text) < sizeof(text))
   1.249 +                strcpy(text + strlen(text), event.text.text);
   1.250 +
   1.251 +            fprintf(stderr, "text inputed: %s\n", text);
   1.252 +
   1.253 +            // After text inputed, we can clear up markedText because it
   1.254 +            // is committed
   1.255 +            markedText = NULL;
   1.256 +            Redraw();
   1.257 +            break;
   1.258 +
   1.259 +        case SDL_TEXTEDITING:
   1.260 +            fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n",
   1.261 +                    event.edit.text, event.edit.start, event.edit.length);
   1.262 +
   1.263 +            markedText = event.edit.text;
   1.264 +            Redraw();
   1.265 +            break;
   1.266 +
   1.267 +        case SDL_QUIT:
   1.268 +            done = 1;
   1.269 +            break;
   1.270 +
   1.271 +        default:
   1.272 +            break;
   1.273 +        }
   1.274 +    }
   1.275 +
   1.276 +    CleanupVideo();
   1.277 +    return 0;
   1.278 +}