test/testime.c
changeset 3478 f7541260e89c
parent 3338 9de326b3099c
child 3581 15eea7a1fa97
equal deleted inserted replaced
3477:2c07bb579922 3478:f7541260e89c
     8 #ifdef HAVE_SDL_TTF
     8 #ifdef HAVE_SDL_TTF
     9 #include "SDL_ttf.h"
     9 #include "SDL_ttf.h"
    10 #endif
    10 #endif
    11 
    11 
    12 #define DEFAULT_PTSIZE  30
    12 #define DEFAULT_PTSIZE  30
    13 #define DEFAULT_FONT    "/System/Library/Fonts/华文细黑.ttf"
    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
    14 #define MAX_TEXT_LENGTH 256
    18 #define MAX_TEXT_LENGTH 256
    15 
    19 
    16 SDL_Surface *screen;
    20 SDL_Surface *screen;
    17 
    21 
    18 #ifdef HAVE_SDL_TTF
    22 #ifdef HAVE_SDL_TTF
    29     exit(0);
    33     exit(0);
    30 }
    34 }
    31 
    35 
    32 void InitVideo(int argc, char *argv[])
    36 void InitVideo(int argc, char *argv[])
    33 {
    37 {
    34     int width = 500, height = 250;
    38     int width = 640, height = 480;
    35     int flags = SDL_HWSURFACE;
    39     int flags = SDL_HWSURFACE;
    36     const char *fontname = DEFAULT_FONT;
    40     const char *fontname = DEFAULT_FONT;
    37     int fullscreen = 0;
    41     int fullscreen = 0;
    38 
    42 
    39     for (argc--, argv++; argc > 0; argc--, argv++)
    43     for (argc--, argv++; argc > 0; argc--, argv++)
   161     {
   165     {
   162         SDL_Flip(screen);
   166         SDL_Flip(screen);
   163         // Stop text input because we cannot hold any more characters
   167         // Stop text input because we cannot hold any more characters
   164         SDL_StopTextInput();
   168         SDL_StopTextInput();
   165         return;
   169         return;
       
   170     }
       
   171     else
       
   172     {
       
   173         SDL_StartTextInput();
   166     }
   174     }
   167 
   175 
   168     cursorRect = markedRect;
   176     cursorRect = markedRect;
   169     cursorRect.w = 2;
   177     cursorRect.w = 2;
   170     cursorRect.h = h;
   178     cursorRect.h = h;
   220     while (! done && SDL_WaitEvent(&event))
   228     while (! done && SDL_WaitEvent(&event))
   221     {
   229     {
   222         switch (event.type)
   230         switch (event.type)
   223         {
   231         {
   224         case SDL_KEYDOWN:
   232         case SDL_KEYDOWN:
   225             if (event.key.keysym.sym == SDLK_ESCAPE) {
   233             switch (event.key.keysym.sym)
   226                 done = 1;
   234             {
       
   235                 case SDLK_ESCAPE:
       
   236                      done = 1;
       
   237                      break;
       
   238                 case SDLK_RETURN:
       
   239                      text[0]=0x00;
       
   240                      Redraw();
       
   241                      break;
       
   242                 case SDLK_BACKSPACE:
       
   243                      {
       
   244                          int textlen=SDL_strlen(text);
       
   245 
       
   246                          do {
       
   247                              if (textlen==0)
       
   248                              {
       
   249                                  break;
       
   250                              }
       
   251                              if ((text[textlen-1] & 0x80) == 0x00)
       
   252                              {
       
   253                                  /* One byte */
       
   254                                  text[textlen-1]=0x00;
       
   255                                  break;
       
   256                              }
       
   257                              if ((text[textlen-1] & 0xC0) == 0x80)
       
   258                              {
       
   259                                  /* Byte from the multibyte sequence */
       
   260                                  text[textlen-1]=0x00;
       
   261                                  textlen--;
       
   262                              }
       
   263                              if ((text[textlen-1] & 0xC0) == 0xC0)
       
   264                              {
       
   265                                  /* First byte of multibyte sequence */
       
   266                                  text[textlen-1]=0x00;
       
   267                                  break;
       
   268                              }
       
   269                          } while(1);
       
   270 
       
   271                          Redraw();
       
   272                      }
       
   273                      break;
       
   274             }
       
   275 
       
   276             if (done)
       
   277             {
   227                 break;
   278                 break;
   228             }
   279             }
   229 
   280 
   230             fprintf(stderr,
   281             fprintf(stderr,
   231                     "Keyboard %d: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
   282                     "Keyboard %d: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
   233                     SDL_GetScancodeName(event.key.keysym.scancode),
   284                     SDL_GetScancodeName(event.key.keysym.scancode),
   234                     event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
   285                     event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
   235             break;
   286             break;
   236 
   287 
   237         case SDL_TEXTINPUT:
   288         case SDL_TEXTINPUT:
   238             if (strlen(event.text.text) == 0 || event.text.text[0] == '\n' ||
   289             if (SDL_strlen(event.text.text) == 0 || event.text.text[0] == '\n' ||
   239                 markedRect.w < 0)
   290                 markedRect.w < 0)
   240                 break;
   291                 break;
   241 
   292 
   242             fprintf(stderr, "Keyboard %d: text input \"%s\"\n",
   293             fprintf(stderr, "Keyboard %d: text input \"%s\"\n",
   243                     event.text.which, event.text.text);
   294                     event.text.which, event.text.text);
   244 
   295 
   245             if (strlen(text) + strlen(event.text.text) < sizeof(text))
   296             if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
   246                 strcpy(text + strlen(text), event.text.text);
   297                 strcpy(text + SDL_strlen(text), event.text.text);
   247 
   298 
   248             fprintf(stderr, "text inputed: %s\n", text);
   299             fprintf(stderr, "text inputed: %s\n", text);
   249 
   300 
   250             // After text inputed, we can clear up markedText because it
   301             // After text inputed, we can clear up markedText because it
   251             // is committed
   302             // is committed