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