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