test/testime.c
author dewyatt
Sun, 25 Jul 2010 13:17:31 -0400
changeset 4757 140be6839185
parent 4756 b99b1024607a
child 4763 518d1679d2d0
permissions -rw-r--r--
Correctly draw cursor based on position.
Had to add UTF-8 functions utf8_length, utf8_next, utf8_advance.
     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 #ifdef HAVE_SDL_TTF
     9 #include "SDL_ttf.h"
    10 #endif
    11 
    12 #define DEFAULT_PTSIZE  30
    13 #ifdef __QNXNTO__
    14     #define DEFAULT_FONT    "/usr/photon/font_repository/tt0003m_.ttf"
    15 #else
    16     #define DEFAULT_FONT    "/System/Library/Fonts/华文细黑.ttf"
    17 #endif
    18 #define MAX_TEXT_LENGTH 256
    19 
    20 SDL_Surface *screen;
    21 
    22 #ifdef HAVE_SDL_TTF
    23 TTF_Font *font;
    24 #endif
    25 SDL_Rect textRect, markedRect;
    26 Uint32 lineColor, backColor;
    27 SDL_Color textColor = { 0, 0, 0 };
    28 char text[MAX_TEXT_LENGTH], markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
    29 int cursor = 0;
    30 
    31 size_t utf8_length(unsigned char c)
    32 {
    33     c = (unsigned char)(0xff & c);
    34     if (c < 0x80)
    35         return 1;
    36     else if ((c >> 5) ==0x6)
    37         return 2;
    38     else if ((c >> 4) == 0xe)
    39         return 3;
    40     else if ((c >> 3) == 0x1e)
    41         return 4;
    42     else
    43         return 0;
    44 }
    45 
    46 char *utf8_next(char *p)
    47 {
    48     size_t len = utf8_length(*p);
    49     size_t i = 0;
    50     if (!len)
    51         return 0;
    52 
    53     for (; i < len; ++i)
    54     {
    55         ++p;
    56         if (!*p)
    57             return 0;
    58     }
    59     return p;
    60 }
    61 
    62 char *utf8_advance(char *p, size_t distance)
    63 {
    64     size_t i = 0;
    65     for (; i < distance && p; ++i)
    66     {
    67         p = utf8_next(p);
    68     }
    69     return p;
    70 }
    71 
    72 void usage()
    73 {
    74     printf("usage: testime [--font fontfile] [--fullscreen]\n");
    75     exit(0);
    76 }
    77 
    78 void InitVideo(int argc, char *argv[])
    79 {
    80     int width = 640, height = 480;
    81     int flags = SDL_HWSURFACE;
    82     const char *fontname = DEFAULT_FONT;
    83     int fullscreen = 0;
    84 
    85     for (argc--, argv++; argc > 0; argc--, argv++)
    86     {
    87         if (strcmp(argv[0], "--help") == 0)
    88             usage();
    89 
    90         else if (strcmp(argv[0], "--fullscreen") == 0)
    91             fullscreen = 1;
    92 
    93         else if (strcmp(argv[0], "--font") == 0)
    94         {
    95             argc--;
    96             argv++;
    97 
    98             if (argc > 0)
    99                 fontname = argv[0];
   100             else
   101                 usage();
   102         }
   103     }
   104 
   105     SDL_setenv("SDL_VIDEO_WINDOW_POS", "center", 1);
   106     if (SDL_Init(SDL_INIT_VIDEO) < 0)
   107     {
   108         fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
   109         exit(-1);
   110     }
   111 
   112 #ifdef HAVE_SDL_TTF
   113     /* Initialize fonts */
   114     TTF_Init();
   115 
   116     font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
   117     if (! font)
   118     {
   119         fprintf(stderr, "Failed to find font: %s\n", TTF_GetError());
   120         exit(-1);
   121     }
   122 #endif
   123 
   124     printf("Using font: %s\n", fontname);
   125     atexit(SDL_Quit);
   126 
   127     if (fullscreen)
   128     {
   129         SDL_DisplayMode mode;
   130         SDL_GetDesktopDisplayMode(&mode);
   131 
   132         width = mode.w;
   133         height = mode.h;
   134         fprintf(stderr, "%dx%d\n", width, height);
   135         flags |= SDL_FULLSCREEN;
   136     }
   137 
   138     /* Create window */
   139     screen = SDL_SetVideoMode(width, height, 32, flags);
   140     if (screen == NULL)
   141     {
   142         fprintf(stderr, "Unable to set %dx%d video: %s\n",
   143                 width, height, SDL_GetError());
   144         exit(-1);
   145     }
   146 }
   147 
   148 void CleanupVideo()
   149 {
   150     SDL_StopTextInput();
   151 #ifdef HAVE_SDL_TTF
   152     TTF_CloseFont(font);
   153     TTF_Quit();
   154 #endif
   155 }
   156 
   157 void InitInput()
   158 {
   159     backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
   160     lineColor = SDL_MapRGB(screen->format, 0x0, 0x0, 0x0);
   161 
   162     /* Prepare a rect for text input */
   163     textRect.x = textRect.y = 100;
   164     textRect.w = screen->w - 2 * textRect.x;
   165     textRect.h = 50;
   166 
   167     text[0] = 0;
   168     markedRect = textRect;
   169     markedText[0] = 0;
   170 
   171     SDL_StartTextInput();
   172 }
   173 
   174 #ifdef HAVE_SDL_TTF
   175 static void RenderText(SDL_Surface *sur,
   176                         TTF_Font *font,
   177                         const char *text,
   178                         int x, int y,
   179                         SDL_Color color)
   180 {
   181     SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, color);
   182     SDL_Rect dest = { x, y, textSur->w, textSur->h };
   183 
   184     SDL_BlitSurface(textSur, NULL, sur, &dest);
   185     SDL_FreeSurface(textSur);
   186 }
   187 #endif
   188 
   189 void Redraw()
   190 {
   191     int w = 0, h = textRect.h;
   192     SDL_Rect cursorRect, underlineRect;
   193 
   194     SDL_FillRect(screen, &textRect, backColor);
   195 
   196 #ifdef HAVE_SDL_TTF
   197     if (strlen(text))
   198     {
   199         RenderText(screen, font, text, textRect.x, textRect.y, textColor);
   200         TTF_SizeUTF8(font, text, &w, &h);
   201     }
   202 #endif
   203 
   204     markedRect.x = textRect.x + w;
   205     markedRect.w = textRect.w - w;
   206     if (markedRect.w < 0)
   207     {
   208         SDL_Flip(screen);
   209         // Stop text input because we cannot hold any more characters
   210         SDL_StopTextInput();
   211         return;
   212     }
   213     else
   214     {
   215         SDL_StartTextInput();
   216     }
   217 
   218     cursorRect = markedRect;
   219     cursorRect.w = 2;
   220     cursorRect.h = h;
   221 
   222     SDL_FillRect(screen, &markedRect, backColor);
   223     if (markedText[0])
   224     {
   225 #ifdef HAVE_SDL_TTF
   226         if (cursor)
   227         {
   228             char *p = utf8_advance(markedText, cursor);
   229             char c = 0;
   230             if (!p)
   231                 p = &markedText[strlen(markedText)];
   232 
   233             c = *p;
   234             *p = 0;
   235             TTF_SizeUTF8(font, markedText, &w, 0);
   236             cursorRect.x += w;
   237             *p = c;
   238         }
   239         RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor);
   240         TTF_SizeUTF8(font, markedText, &w, &h);
   241 #endif
   242 
   243         underlineRect = markedRect;
   244         underlineRect.y += (h - 2);
   245         underlineRect.h = 2;
   246         underlineRect.w = w;
   247 
   248         SDL_FillRect(screen, &underlineRect, lineColor);
   249     }
   250 
   251     SDL_FillRect(screen, &cursorRect, lineColor);
   252 
   253     SDL_Flip(screen);
   254 
   255     SDL_SetTextInputRect(&markedRect);
   256 }
   257 
   258 void
   259 HotKey_ToggleFullScreen(void)
   260 {
   261     SDL_Surface *screen;
   262 
   263     screen = SDL_GetVideoSurface();
   264     if (SDL_WM_ToggleFullScreen(screen)) {
   265         printf("Toggled fullscreen mode - now %s\n",
   266                (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
   267     } else {
   268         printf("Unable to toggle fullscreen mode\n");
   269     }
   270 }
   271 
   272 int main(int argc, char *argv[])
   273 {
   274     SDL_Event event;
   275     int done = 0;
   276 
   277     InitVideo(argc, argv);
   278     InitInput();
   279     Redraw();
   280 
   281     while (! done && SDL_WaitEvent(&event))
   282     {
   283         switch (event.type)
   284         {
   285         case SDL_KEYDOWN:
   286             switch (event.key.keysym.sym)
   287             {
   288                 case SDLK_ESCAPE:
   289                      done = 1;
   290                      break;
   291                 case SDLK_RETURN:
   292                      text[0]=0x00;
   293                      Redraw();
   294                      break;
   295                 case SDLK_BACKSPACE:
   296                      {
   297                          int textlen=SDL_strlen(text);
   298 
   299                          do {
   300                              if (textlen==0)
   301                              {
   302                                  break;
   303                              }
   304                              if ((text[textlen-1] & 0x80) == 0x00)
   305                              {
   306                                  /* One byte */
   307                                  text[textlen-1]=0x00;
   308                                  break;
   309                              }
   310                              if ((text[textlen-1] & 0xC0) == 0x80)
   311                              {
   312                                  /* Byte from the multibyte sequence */
   313                                  text[textlen-1]=0x00;
   314                                  textlen--;
   315                              }
   316                              if ((text[textlen-1] & 0xC0) == 0xC0)
   317                              {
   318                                  /* First byte of multibyte sequence */
   319                                  text[textlen-1]=0x00;
   320                                  break;
   321                              }
   322                          } while(1);
   323 
   324                          Redraw();
   325                      }
   326                      break;
   327             }
   328 
   329             if (done)
   330             {
   331                 break;
   332             }
   333 
   334             fprintf(stderr,
   335                     "Keyboard: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
   336                     event.key.keysym.scancode,
   337                     SDL_GetScancodeName(event.key.keysym.scancode),
   338                     event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
   339             break;
   340 
   341         case SDL_TEXTINPUT:
   342             if (SDL_strlen(event.text.text) == 0 || event.text.text[0] == '\n' ||
   343                 markedRect.w < 0)
   344                 break;
   345 
   346             fprintf(stderr, "Keyboard: text input \"%s\"\n", event.text.text);
   347 
   348             if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
   349                 strcat(text, event.text.text);
   350 
   351             fprintf(stderr, "text inputed: %s\n", text);
   352 
   353             // After text inputed, we can clear up markedText because it
   354             // is committed
   355             markedText[0] = 0;
   356             Redraw();
   357             break;
   358 
   359         case SDL_TEXTEDITING:
   360             fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n",
   361                     event.edit.text, event.edit.start, event.edit.length);
   362 
   363             strcpy(markedText, event.edit.text);
   364             cursor = event.edit.start;
   365             Redraw();
   366             break;
   367 
   368         case SDL_QUIT:
   369             done = 1;
   370             break;
   371 
   372         default:
   373             break;
   374         }
   375     }
   376 
   377     CleanupVideo();
   378     return 0;
   379 }