test/testime.c
author Jiang Jiang <gzjjgod@gmail.com>
Wed, 01 Jul 2009 07:33:58 +0000
branchgsoc2009_IME
changeset 3132 88861448961f
parent 3131 009bd8f81947
child 3133 84119fe89d26
permissions -rw-r--r--
Add SDL_TEXTEDTING event to inform application about marked text.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3131
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
     1
/* A simple program to test the Input Method support in the SDL library (1.3+) */
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
     2
 
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
     3
#include <stdlib.h>
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
     4
#include <stdio.h>
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
     5
#include <string.h>
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
     6
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
     7
#include "SDL.h"
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
     8
#include <SDL/SDL_ttf.h>
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
     9
 
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    10
#define DEFAULT_PTSIZE  30
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    11
#define DEFAULT_FONT    "DroidSansFallback.ttf"
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    12
#define MAX_TEXT_LENGTH 256
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    13
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    14
static void render_text(SDL_Surface *sur,
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    15
                        TTF_Font *font,
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    16
                        const char *text,
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    17
                        int x, int y,
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    18
                        SDL_Color color)
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    19
{
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    20
    SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, color);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    21
    SDL_Rect dest = { x, y, textSur->w, textSur->h };
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    22
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    23
    SDL_BlitSurface(textSur, NULL, sur, &dest);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    24
    SDL_FreeSurface(textSur);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    25
}
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    26
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    27
int main(int argc, char *argv[])
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    28
{
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    29
    int width, height;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    30
    SDL_Surface *screen;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    31
    TTF_Font *font;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    32
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    33
    width = 500, height = 250;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    34
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    35
    SDL_putenv("SDL_VIDEO_WINDOW_POS=center");
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    36
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    37
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    38
    {
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    39
        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    40
        return -1;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    41
    }
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    42
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    43
    /* Initialize fonts */
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    44
    TTF_Init();
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    45
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    46
    font = TTF_OpenFont(DEFAULT_FONT, DEFAULT_PTSIZE);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    47
    if (! font)
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    48
    {
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    49
        fprintf(stderr, "Failed to find font: %s\n", SDL_GetError());
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    50
        exit(-1);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    51
    }
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    52
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    53
    atexit(SDL_Quit);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    54
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    55
    /* Create window */
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    56
    screen = SDL_SetVideoMode(width, height, 32,
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    57
                              SDL_HWSURFACE | SDL_DOUBLEBUF);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    58
    if (screen == NULL)
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    59
    {
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    60
        fprintf(stderr, "Unable to set %dx%d video: %s\n",
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    61
                width, height, SDL_GetError());
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    62
        return -1;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    63
    }
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    64
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    65
    /* Prepare a rect for text input */
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    66
    SDL_Rect textRect = { 100, 80, 300, 50 }, markedRect;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    67
    Uint32 backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    68
    SDL_Color textColor = { 0, 0, 0 };
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    69
    SDL_FillRect(screen, &textRect, backColor);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    70
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    71
    markedRect = textRect;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    72
    SDL_StartTextInput(&markedRect);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    73
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    74
    SDL_Flip(screen);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    75
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    76
    SDL_Event event;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    77
    int done = 0, inputed = 0;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    78
    int w, h;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    79
    char text[MAX_TEXT_LENGTH];
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    80
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    81
    while (! done && SDL_WaitEvent(&event))
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    82
    {
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    83
        switch (event.type)
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    84
        {
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    85
        case SDL_KEYDOWN:
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    86
            fprintf(stderr,
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    87
                    "Keyboard %d: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    88
                    event.key.which, event.key.keysym.scancode,
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    89
                    SDL_GetScancodeName(event.key.keysym.scancode),
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    90
                    event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    91
            break;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    92
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    93
        case SDL_TEXTINPUT:
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    94
            fprintf(stderr, "Keyboard %d: text input \"%s\"\n",
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    95
                    event.text.which, event.text.text);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    96
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    97
            if (inputed < sizeof(text))
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    98
            {
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
    99
                strcpy(text + inputed, event.text.text);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   100
                inputed += strlen(event.text.text);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   101
            }
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   102
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   103
            fprintf(stderr, "text inputed: %s\n", text);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   104
            SDL_FillRect(screen, &textRect, backColor);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   105
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   106
            render_text(screen, font, text, textRect.x, textRect.y, textColor);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   107
            TTF_SizeUTF8(font, text, &w, &h);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   108
            markedRect.x = textRect.x + w;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   109
            SDL_Flip(screen);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   110
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   111
            SDL_StartTextInput(&markedRect);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   112
            break;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   113
3132
88861448961f Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents: 3131
diff changeset
   114
        case SDL_TEXTEDITING:
88861448961f Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents: 3131
diff changeset
   115
            fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n",
88861448961f Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents: 3131
diff changeset
   116
                    event.edit.text, event.edit.start, event.edit.length);
88861448961f Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents: 3131
diff changeset
   117
88861448961f Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents: 3131
diff changeset
   118
            SDL_FillRect(screen, &markedRect, backColor);
88861448961f Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents: 3131
diff changeset
   119
            render_text(screen, font, event.edit.text, markedRect.x, markedRect.y, textColor);
88861448961f Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents: 3131
diff changeset
   120
            SDL_Flip(screen);
88861448961f Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents: 3131
diff changeset
   121
            break;
88861448961f Add SDL_TEXTEDTING event to inform application about marked text.
Jiang Jiang <gzjjgod@gmail.com>
parents: 3131
diff changeset
   122
3131
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   123
        case SDL_QUIT:
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   124
            done = 1;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   125
            break;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   126
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   127
        default:
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   128
            break;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   129
        }
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   130
    }
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   131
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   132
    TTF_CloseFont(font);
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   133
    TTF_Quit();
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   134
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   135
    return 0;
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   136
}
009bd8f81947 Add IME test program
Jiang Jiang <gzjjgod@gmail.com>
parents:
diff changeset
   137