Add IME test program gsoc2009_IME
authorJiang Jiang <gzjjgod@gmail.com>
Wed, 01 Jul 2009 05:52:17 +0000
branchgsoc2009_IME
changeset 3131009bd8f81947
parent 3130 fef1a835af43
child 3132 88861448961f
Add IME test program
test/Makefile.in
test/testime.c
     1.1 --- a/test/Makefile.in	Fri Jun 26 10:37:57 2009 +0000
     1.2 +++ b/test/Makefile.in	Wed Jul 01 05:52:17 2009 +0000
     1.3 @@ -7,7 +7,7 @@
     1.4  CFLAGS  = @CFLAGS@
     1.5  LIBS	= @LIBS@
     1.6  
     1.7 -TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testresample$(EXE) testaudioinfo$(EXE) testmultiaudio$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testintersections$(EXE) testdraw2$(EXE) testdyngl$(EXE) testdyngles$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testgl2$(EXE) testgles$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE) testloadso$(EXE) testhaptic$(EXE) testmmousetablet$(EXE)
     1.8 +TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testresample$(EXE) testaudioinfo$(EXE) testmultiaudio$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testintersections$(EXE) testdraw2$(EXE) testdyngl$(EXE) testdyngles$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testgl2$(EXE) testgles$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE) testloadso$(EXE) testhaptic$(EXE) testmmousetablet$(EXE) testime$(EXE)
     1.9  
    1.10  all: Makefile $(TARGETS)
    1.11  
    1.12 @@ -146,6 +146,9 @@
    1.13  testmmousetablet$(EXE): $(srcdir)/testmmousetablet.c
    1.14  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.15  
    1.16 +testime$(EXE): $(srcdir)/testime.c
    1.17 +	$(CC) -o $@ $? $(CFLAGS) -L../build/.libs $(LIBS) -lSDL_ttf
    1.18 +
    1.19  clean:
    1.20  	rm -f $(TARGETS)
    1.21  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/testime.c	Wed Jul 01 05:52:17 2009 +0000
     2.3 @@ -0,0 +1,128 @@
     2.4 +/* A simple program to test the Input Method support in the SDL library (1.3+) */
     2.5 + 
     2.6 +#include <stdlib.h>
     2.7 +#include <stdio.h>
     2.8 +#include <string.h>
     2.9 +
    2.10 +#include "SDL.h"
    2.11 +#include <SDL/SDL_ttf.h>
    2.12 + 
    2.13 +#define DEFAULT_PTSIZE  30
    2.14 +#define DEFAULT_FONT    "DroidSansFallback.ttf"
    2.15 +#define MAX_TEXT_LENGTH 256
    2.16 +
    2.17 +static void render_text(SDL_Surface *sur,
    2.18 +                        TTF_Font *font,
    2.19 +                        const char *text,
    2.20 +                        int x, int y,
    2.21 +                        SDL_Color color)
    2.22 +{
    2.23 +    SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, color);
    2.24 +    SDL_Rect dest = { x, y, textSur->w, textSur->h };
    2.25 +
    2.26 +    SDL_BlitSurface(textSur, NULL, sur, &dest);
    2.27 +    SDL_FreeSurface(textSur);
    2.28 +}
    2.29 +
    2.30 +int main(int argc, char *argv[])
    2.31 +{
    2.32 +    int width, height;
    2.33 +    SDL_Surface *screen;
    2.34 +    TTF_Font *font;
    2.35 +
    2.36 +    width = 500, height = 250;
    2.37 +
    2.38 +    SDL_putenv("SDL_VIDEO_WINDOW_POS=center");
    2.39 +
    2.40 +    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    2.41 +    {
    2.42 +        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
    2.43 +        return -1;
    2.44 +    }
    2.45 +
    2.46 +    /* Initialize fonts */
    2.47 +    TTF_Init();
    2.48 +
    2.49 +    font = TTF_OpenFont(DEFAULT_FONT, DEFAULT_PTSIZE);
    2.50 +    if (! font)
    2.51 +    {
    2.52 +        fprintf(stderr, "Failed to find font: %s\n", SDL_GetError());
    2.53 +        exit(-1);
    2.54 +    }
    2.55 +
    2.56 +    atexit(SDL_Quit);
    2.57 +
    2.58 +    /* Create window */
    2.59 +    screen = SDL_SetVideoMode(width, height, 32,
    2.60 +                              SDL_HWSURFACE | SDL_DOUBLEBUF);
    2.61 +    if (screen == NULL)
    2.62 +    {
    2.63 +        fprintf(stderr, "Unable to set %dx%d video: %s\n",
    2.64 +                width, height, SDL_GetError());
    2.65 +        return -1;
    2.66 +    }
    2.67 +
    2.68 +    /* Prepare a rect for text input */
    2.69 +    SDL_Rect textRect = { 100, 80, 300, 50 }, markedRect;
    2.70 +    Uint32 backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
    2.71 +    SDL_Color textColor = { 0, 0, 0 };
    2.72 +    SDL_FillRect(screen, &textRect, backColor);
    2.73 +
    2.74 +    markedRect = textRect;
    2.75 +    SDL_StartTextInput(&markedRect);
    2.76 +
    2.77 +    SDL_Flip(screen);
    2.78 +
    2.79 +    SDL_Event event;
    2.80 +    int done = 0, inputed = 0;
    2.81 +    int w, h;
    2.82 +    char text[MAX_TEXT_LENGTH];
    2.83 +
    2.84 +    while (! done && SDL_WaitEvent(&event))
    2.85 +    {
    2.86 +        switch (event.type)
    2.87 +        {
    2.88 +        case SDL_KEYDOWN:
    2.89 +            fprintf(stderr,
    2.90 +                    "Keyboard %d: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
    2.91 +                    event.key.which, event.key.keysym.scancode,
    2.92 +                    SDL_GetScancodeName(event.key.keysym.scancode),
    2.93 +                    event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
    2.94 +            break;
    2.95 +
    2.96 +        case SDL_TEXTINPUT:
    2.97 +            fprintf(stderr, "Keyboard %d: text input \"%s\"\n",
    2.98 +                    event.text.which, event.text.text);
    2.99 +
   2.100 +            if (inputed < sizeof(text))
   2.101 +            {
   2.102 +                strcpy(text + inputed, event.text.text);
   2.103 +                inputed += strlen(event.text.text);
   2.104 +            }
   2.105 +
   2.106 +            fprintf(stderr, "text inputed: %s\n", text);
   2.107 +            SDL_FillRect(screen, &textRect, backColor);
   2.108 +
   2.109 +            render_text(screen, font, text, textRect.x, textRect.y, textColor);
   2.110 +            TTF_SizeUTF8(font, text, &w, &h);
   2.111 +            markedRect.x = textRect.x + w;
   2.112 +            SDL_Flip(screen);
   2.113 +
   2.114 +            SDL_StartTextInput(&markedRect);
   2.115 +            break;
   2.116 +
   2.117 +        case SDL_QUIT:
   2.118 +            done = 1;
   2.119 +            break;
   2.120 +
   2.121 +        default:
   2.122 +            break;
   2.123 +        }
   2.124 +    }
   2.125 +
   2.126 +    TTF_CloseFont(font);
   2.127 +    TTF_Quit();
   2.128 +
   2.129 +    return 0;
   2.130 +}
   2.131 +