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