Atlas - testime.c
Home / ext / SDL / test Lines: 7 | Size: 41681 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 3 4 This software is provided 'as-is', without any express or implied 5 warranty. In no event will the authors be held liable for any damages 6 arising from the use of this software. 7 8 Permission is granted to anyone to use this software for any purpose, 9 including commercial applications, and to alter it and redistribute it 10 freely. 11*/ 12 13/* A simple program to test the Input Method support in SDL. 14 * 15 * This uses the GNU Unifont to display non-ASCII characters, available at: 16 * http://unifoundry.com/unifont.html 17 * 18 * An example of IME support with TrueType fonts is available in the SDL_ttf example code: 19 * https://github.com/libsdl-org/SDL_ttf/blob/main/examples/editbox.h 20 */ 21#include <SDL3/SDL.h> 22#include <SDL3/SDL_main.h> 23#include <SDL3/SDL_test_font.h> 24 25#include <SDL3/SDL_test_common.h> 26#include "testutils.h" 27 28#define DEFAULT_FONT "unifont-15.1.05.hex" 29#define MAX_TEXT_LENGTH 256 30 31#define WINDOW_WIDTH 640 32#define WINDOW_HEIGHT 480 33 34#define MARGIN 32.0f 35#define LINE_HEIGHT (FONT_CHARACTER_SIZE + 4.0f) 36#define CURSOR_BLINK_INTERVAL_MS 500 37 38typedef struct 39{ 40 SDL_Window *window; 41 SDL_Renderer *renderer; 42 int rendererID; 43 bool settings_visible; 44 SDL_Texture *settings_icon; 45 SDL_FRect settings_rect; 46 SDL_PropertiesID text_settings; 47 SDL_FRect textRect; 48 SDL_FRect markedRect; 49 char text[MAX_TEXT_LENGTH]; 50 char markedText[MAX_TEXT_LENGTH]; 51 int cursor; 52 int cursor_length; 53 bool cursor_visible; 54 Uint64 last_cursor_change; 55 char **candidates; 56 int num_candidates; 57 int selected_candidate; 58 bool horizontal_candidates; 59} WindowState; 60 61static SDLTest_CommonState *state; 62static WindowState *windowstate; 63static const SDL_Color lineColor = { 0, 0, 0, 255 }; 64static const SDL_Color backColor = { 255, 255, 255, 255 }; 65static const SDL_Color textColor = { 0, 0, 0, 255 }; 66static SDL_BlendMode highlight_mode; 67 68static const struct 69{ 70 const char *label; 71 const char *setting; 72 int value; 73} settings[] = { 74 { "Text", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT }, 75 { "Name", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_NAME }, 76 { "E-mail", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_EMAIL }, 77 { "Username", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_USERNAME }, 78 { "Password (hidden)", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_PASSWORD_HIDDEN }, 79 { "Password (visible)", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT_PASSWORD_VISIBLE }, 80 { "Number", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_NUMBER }, 81 { "Numeric PIN (hidden)", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_NUMBER_PASSWORD_HIDDEN }, 82 { "Numeric PIN (visible)", SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_NUMBER_PASSWORD_VISIBLE }, 83 { "", NULL }, 84 { "No capitalization", SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_NONE }, 85 { "Capitalize sentences", SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_SENTENCES }, 86 { "Capitalize words", SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_WORDS }, 87 { "All caps", SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER, SDL_CAPITALIZE_LETTERS }, 88 { "", NULL }, 89 { "Auto-correct OFF", SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN, false }, 90 { "Auto-correct ON", SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN, true }, 91 { "Multiline OFF", SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN, false }, 92 { "Multiline ON", SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN, true } 93}; 94 95#define UNIFONT_MAX_CODEPOINT 0x1ffff 96#define UNIFONT_NUM_GLYPHS 0x20000 97#define UNIFONT_REPLACEMENT 0xFFFD 98/* Using 512x512 textures that are supported everywhere. */ 99#define UNIFONT_TEXTURE_WIDTH 512 100#define UNIFONT_GLYPH_SIZE 16 101#define UNIFONT_GLYPH_BORDER 1 102#define UNIFONT_GLYPH_AREA (UNIFONT_GLYPH_BORDER + UNIFONT_GLYPH_SIZE + UNIFONT_GLYPH_BORDER) 103#define UNIFONT_GLYPHS_IN_ROW (UNIFONT_TEXTURE_WIDTH / UNIFONT_GLYPH_AREA) 104#define UNIFONT_GLYPHS_IN_TEXTURE (UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPHS_IN_ROW) 105#define UNIFONT_NUM_TEXTURES ((UNIFONT_NUM_GLYPHS + UNIFONT_GLYPHS_IN_TEXTURE - 1) / UNIFONT_GLYPHS_IN_TEXTURE) 106#define UNIFONT_TEXTURE_SIZE (UNIFONT_TEXTURE_WIDTH * UNIFONT_TEXTURE_WIDTH * 4) 107#define UNIFONT_TEXTURE_PITCH (UNIFONT_TEXTURE_WIDTH * 4) 108#define UNIFONT_DRAW_SCALE 2.0f 109static struct UnifontGlyph 110{ 111 Uint8 width; 112 Uint8 data[32]; 113} * unifontGlyph; 114static SDL_Texture **unifontTexture; 115static Uint8 unifontTextureLoaded[UNIFONT_NUM_TEXTURES] = { 0 }; 116 117/* Unifont loading code start */ 118 119static Uint8 dehex(char c) 120{ 121 if (c >= '0' && c <= '9') { 122 return c - '0'; 123 } else if (c >= 'a' && c <= 'f') { 124 return c - 'a' + 10; 125 } else if (c >= 'A' && c <= 'F') { 126 return c - 'A' + 10; 127 } 128 return 255; 129} 130 131static Uint8 dehex2(char c1, char c2) 132{ 133 return (dehex(c1) << 4) | dehex(c2); 134} 135 136static Uint8 validate_hex(const char *cp, size_t len, Uint32 *np) 137{ 138 Uint32 n = 0; 139 for (; len > 0; cp++, len--) { 140 Uint8 c = dehex(*cp); 141 if (c == 255) { 142 return 0; 143 } 144 n = (n << 4) | c; 145 } 146 if (np) { 147 *np = n; 148 } 149 return 1; 150} 151 152static int unifont_init(const char *fontname) 153{ 154 Uint8 hexBuffer[65]; 155 Uint32 numGlyphs = 0; 156 int lineNumber = 1; 157 size_t bytesRead; 158 SDL_IOStream *hexFile; 159 const size_t unifontGlyphSize = UNIFONT_NUM_GLYPHS * sizeof(struct UnifontGlyph); 160 const size_t unifontTextureSize = UNIFONT_NUM_TEXTURES * state->num_windows * sizeof(void *); 161 char *filename; 162 163 /* Allocate memory for the glyph data so the file can be closed after initialization. */ 164 unifontGlyph = (struct UnifontGlyph *)SDL_malloc(unifontGlyphSize); 165 if (!unifontGlyph) { 166 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for glyph data.", (int)(unifontGlyphSize + 1023) / 1024); 167 return -1; 168 } 169 SDL_memset(unifontGlyph, 0, unifontGlyphSize); 170 171 /* Allocate memory for texture pointers for all renderers. */ 172 unifontTexture = (SDL_Texture **)SDL_malloc(unifontTextureSize); 173 if (!unifontTexture) { 174 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for texture pointer data.", (int)(unifontTextureSize + 1023) / 1024); 175 return -1; 176 } 177 SDL_memset(unifontTexture, 0, unifontTextureSize); 178 179 filename = GetResourceFilename(NULL, fontname); 180 if (!filename) { 181 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory"); 182 return -1; 183 } 184 hexFile = SDL_IOFromFile(filename, "rb"); 185 SDL_free(filename); 186 if (!hexFile) { 187 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to open font file: %s", fontname); 188 return -1; 189 } 190 191 /* Read all the glyph data into memory to make it accessible later when textures are created. */ 192 do { 193 int i, codepointHexSize; 194 size_t bytesOverread; 195 Uint8 glyphWidth; 196 Uint32 codepoint; 197 198 bytesRead = SDL_ReadIO(hexFile, hexBuffer, 9); 199 if (numGlyphs > 0 && bytesRead == 0) { 200 break; /* EOF */ 201 } 202 if ((numGlyphs == 0 && bytesRead == 0) || (numGlyphs > 0 && bytesRead < 9)) { 203 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file."); 204 return -1; 205 } 206 207 /* Looking for the colon that separates the codepoint and glyph data at position 2, 4, 6 and 8. */ 208 if (hexBuffer[2] == ':') { 209 codepointHexSize = 2; 210 } else if (hexBuffer[4] == ':') { 211 codepointHexSize = 4; 212 } else if (hexBuffer[6] == ':') { 213 codepointHexSize = 6; 214 } else if (hexBuffer[8] == ':') { 215 codepointHexSize = 8; 216 } else { 217 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Could not find codepoint and glyph data separator symbol in hex file on line %d.", lineNumber); 218 return -1; 219 } 220 221 if (!validate_hex((const char *)hexBuffer, codepointHexSize, &codepoint)) { 222 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal number in hex file on line %d.", lineNumber); 223 return -1; 224 } 225 if (codepoint > UNIFONT_MAX_CODEPOINT) { 226 SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Codepoint on line %d exceeded limit of 0x%x.", lineNumber, UNIFONT_MAX_CODEPOINT); 227 } 228 229 /* If there was glyph data read in the last file read, move it to the front of the buffer. */ 230 bytesOverread = 8 - codepointHexSize; 231 if (codepointHexSize < 8) { 232 SDL_memmove(hexBuffer, hexBuffer + codepointHexSize + 1, bytesOverread); 233 } 234 bytesRead = SDL_ReadIO(hexFile, hexBuffer + bytesOverread, 33 - bytesOverread); 235 236 if (bytesRead < (33 - bytesOverread)) { 237 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file."); 238 return -1; 239 } 240 if (hexBuffer[32] == '\n') { 241 glyphWidth = 8; 242 } else { 243 glyphWidth = 16; 244 bytesRead = SDL_ReadIO(hexFile, hexBuffer + 33, 32); 245 if (bytesRead < 32) { 246 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file."); 247 return -1; 248 } 249 } 250 251 if (!validate_hex((const char *)hexBuffer, glyphWidth * 4, NULL)) { 252 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal glyph data in hex file on line %d.", lineNumber); 253 return -1; 254 } 255 256 if (codepoint <= UNIFONT_MAX_CODEPOINT) { 257 if (unifontGlyph[codepoint].width > 0) { 258 SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Ignoring duplicate codepoint 0x%08" SDL_PRIx32 " in hex file on line %d.", codepoint, lineNumber); 259 } else { 260 unifontGlyph[codepoint].width = glyphWidth; 261 /* Pack the hex data into a more compact form. */ 262 for (i = 0; i < glyphWidth * 2; i++) { 263 unifontGlyph[codepoint].data[i] = dehex2(hexBuffer[i * 2], hexBuffer[i * 2 + 1]); 264 } 265 numGlyphs++; 266 } 267 } 268 269 lineNumber++; 270 } while (bytesRead > 0); 271 272 SDL_CloseIO(hexFile); 273 SDL_Log("unifont: Loaded %" SDL_PRIu32 " glyphs.", numGlyphs); 274 return 0; 275} 276 277static void unifont_make_rgba(const Uint8 *src, Uint8 *dst, Uint8 width) 278{ 279 int i, j; 280 Uint8 *row = dst; 281 282 for (i = 0; i < width * 2; i++) { 283 Uint8 data = src[i]; 284 for (j = 0; j < 8; j++) { 285 if (data & 0x80) { 286 row[0] = textColor.r; 287 row[1] = textColor.g; 288 row[2] = textColor.b; 289 row[3] = textColor.a; 290 } else { 291 row[0] = 0; 292 row[1] = 0; 293 row[2] = 0; 294 row[3] = 0; 295 } 296 data <<= 1; 297 row += 4; 298 } 299 300 if (width == 8 || (width == 16 && i % 2 == 1)) { 301 dst += UNIFONT_TEXTURE_PITCH; 302 row = dst; 303 } 304 } 305} 306 307static int unifont_load_texture(Uint32 textureID) 308{ 309 int i; 310 Uint8 *textureRGBA; 311 312 if (textureID >= UNIFONT_NUM_TEXTURES) { 313 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Tried to load out of range texture %" SDL_PRIu32, textureID); 314 return -1; 315 } 316 317 textureRGBA = (Uint8 *)SDL_malloc(UNIFONT_TEXTURE_SIZE); 318 if (!textureRGBA) { 319 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d MiB for a texture.", UNIFONT_TEXTURE_SIZE / 1024 / 1024); 320 return -1; 321 } 322 SDL_memset(textureRGBA, 0, UNIFONT_TEXTURE_SIZE); 323 324 /* Copy the glyphs into memory in RGBA format. */ 325 for (i = 0; i < UNIFONT_GLYPHS_IN_TEXTURE; i++) { 326 Uint32 codepoint = UNIFONT_GLYPHS_IN_TEXTURE * textureID + i; 327 if (unifontGlyph[codepoint].width > 0) { 328 const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE; 329 const size_t offset = ((size_t)cInTex / UNIFONT_GLYPHS_IN_ROW) * UNIFONT_TEXTURE_PITCH * UNIFONT_GLYPH_AREA + (cInTex % UNIFONT_GLYPHS_IN_ROW) * UNIFONT_GLYPH_AREA * 4; 330 unifont_make_rgba(unifontGlyph[codepoint].data, textureRGBA + offset, unifontGlyph[codepoint].width); 331 } 332 } 333 334 /* Create textures and upload the RGBA data from above. */ 335 for (i = 0; i < state->num_windows; ++i) { 336 SDL_Renderer *renderer = state->renderers[i]; 337 SDL_Texture *tex = unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID]; 338 if (state->windows[i] == NULL || renderer == NULL || tex != NULL) { 339 continue; 340 } 341 tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, UNIFONT_TEXTURE_WIDTH, UNIFONT_TEXTURE_WIDTH); 342 if (tex == NULL) { 343 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to create texture %" SDL_PRIu32 " for renderer %d.", textureID, i); 344 return -1; 345 } 346 unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID] = tex; 347 SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND); 348 if (!SDL_UpdateTexture(tex, NULL, textureRGBA, UNIFONT_TEXTURE_PITCH)) { 349 SDL_Log("unifont error: Failed to update texture %" SDL_PRIu32 " data for renderer %d.", textureID, i); 350 } 351 } 352 353 SDL_free(textureRGBA); 354 unifontTextureLoaded[textureID] = 1; 355 return -1; 356} 357 358static int unifont_glyph_width(Uint32 codepoint) 359{ 360 if (codepoint > UNIFONT_MAX_CODEPOINT || 361 unifontGlyph[codepoint].width == 0) { 362 codepoint = UNIFONT_REPLACEMENT; 363 } 364 return unifontGlyph[codepoint].width; 365} 366 367static int unifont_draw_glyph(Uint32 codepoint, int rendererID, SDL_FRect *dst) 368{ 369 SDL_Texture *texture; 370 Uint32 textureID; 371 SDL_FRect srcrect; 372 srcrect.w = srcrect.h = (float)UNIFONT_GLYPH_SIZE; 373 374 if (codepoint > UNIFONT_MAX_CODEPOINT || 375 unifontGlyph[codepoint].width == 0) { 376 codepoint = UNIFONT_REPLACEMENT; 377 } 378 379 textureID = codepoint / UNIFONT_GLYPHS_IN_TEXTURE; 380 if (!unifontTextureLoaded[textureID]) { 381 if (unifont_load_texture(textureID) < 0) { 382 return 0; 383 } 384 } 385 texture = unifontTexture[UNIFONT_NUM_TEXTURES * rendererID + textureID]; 386 if (texture) { 387 const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE; 388 srcrect.x = (float)(cInTex % UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPH_AREA); 389 srcrect.y = (float)(cInTex / UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPH_AREA); 390 SDL_RenderTexture(state->renderers[rendererID], texture, &srcrect, dst); 391 } 392 return unifontGlyph[codepoint].width; 393} 394 395static void unifont_cleanup(void) 396{ 397 int i, j; 398 for (i = 0; i < state->num_windows; ++i) { 399 SDL_Renderer *renderer = state->renderers[i]; 400 if (state->windows[i] == NULL || !renderer) { 401 continue; 402 } 403 for (j = 0; j < UNIFONT_NUM_TEXTURES; j++) { 404 SDL_Texture *tex = unifontTexture[UNIFONT_NUM_TEXTURES * i + j]; 405 if (tex) { 406 SDL_DestroyTexture(tex); 407 } 408 } 409 } 410 411 for (j = 0; j < UNIFONT_NUM_TEXTURES; j++) { 412 unifontTextureLoaded[j] = 0; 413 } 414 415 SDL_free(unifontTexture); 416 SDL_free(unifontGlyph); 417} 418 419/* Unifont code end */ 420 421static size_t utf8_length(unsigned char c) 422{ 423 c = (unsigned char)(0xff & c); 424 if (c < 0x80) { 425 return 1; 426 } else if ((c >> 5) == 0x6) { 427 return 2; 428 } else if ((c >> 4) == 0xe) { 429 return 3; 430 } else if ((c >> 3) == 0x1e) { 431 return 4; 432 } 433 return 0; 434} 435 436static Uint32 utf8_decode(const char *p, size_t len) 437{ 438 Uint32 codepoint = 0; 439 size_t i = 0; 440 if (!len) { 441 return 0; 442 } 443 444 for (; i < len; ++i) { 445 if (i == 0) { 446 codepoint = (0xff >> len) & *p; 447 } else { 448 codepoint <<= 6; 449 codepoint |= 0x3f & *p; 450 } 451 if (!*p) { 452 return 0; 453 } 454 p++; 455 } 456 457 return codepoint; 458} 459 460static WindowState *GetWindowStateForWindowID(SDL_WindowID windowID) 461{ 462 int i; 463 SDL_Window *window = SDL_GetWindowFromID(windowID); 464 465 for (i = 0; i < state->num_windows; ++i) { 466 if (windowstate[i].window == window) { 467 return &windowstate[i]; 468 } 469 } 470 return NULL; 471} 472 473static void InitInput(WindowState *ctx) 474{ 475 /* Prepare a rect for text input */ 476 ctx->textRect.x = ctx->textRect.y = 100.0f; 477 ctx->textRect.w = DEFAULT_WINDOW_WIDTH - 2 * ctx->textRect.x; 478 ctx->textRect.h = 50.0f; 479 ctx->markedRect = ctx->textRect; 480 481 ctx->text_settings = SDL_CreateProperties(); 482 483 SDL_StartTextInputWithProperties(ctx->window, ctx->text_settings); 484} 485 486 487static void ClearCandidates(WindowState *ctx) 488{ 489 int i; 490 491 for (i = 0; i < ctx->num_candidates; ++i) { 492 SDL_free(ctx->candidates[i]); 493 } 494 SDL_free(ctx->candidates); 495 ctx->candidates = NULL; 496 ctx->num_candidates = 0; 497} 498 499static void SaveCandidates(WindowState *ctx, SDL_Event *event) 500{ 501 int i; 502 503 ClearCandidates(ctx); 504 505 ctx->num_candidates = event->edit_candidates.num_candidates; 506 if (ctx->num_candidates > 0) { 507 ctx->candidates = (char **)SDL_malloc(ctx->num_candidates * sizeof(*ctx->candidates)); 508 if (!ctx->candidates) { 509 ctx->num_candidates = 0; 510 return; 511 } 512 for (i = 0; i < ctx->num_candidates; ++i) { 513 ctx->candidates[i] = SDL_strdup(event->edit_candidates.candidates[i]); 514 } 515 ctx->selected_candidate = event->edit_candidates.selected_candidate; 516 ctx->horizontal_candidates = event->edit_candidates.horizontal; 517 } 518} 519 520static void DrawCandidates(WindowState *ctx, SDL_FRect *cursorRect) 521{ 522 SDL_Renderer *renderer = ctx->renderer; 523 int rendererID = ctx->rendererID; 524 int i; 525 int output_w = 0, output_h = 0; 526 float w = 0.0f, h = 0.0f; 527 SDL_FRect candidatesRect, dstRect, underlineRect; 528 529 if (ctx->num_candidates == 0) { 530 return; 531 } 532 533 /* Calculate the size of the candidate list */ 534 for (i = 0; i < ctx->num_candidates; ++i) { 535 if (!ctx->candidates[i]) { 536 continue; 537 } 538 539 if (ctx->horizontal_candidates) { 540 const char *utext = ctx->candidates[i]; 541 Uint32 codepoint; 542 size_t len; 543 float advance = 0.0f; 544 545 if (i > 0) { 546 advance += unifont_glyph_width(' ') * UNIFONT_DRAW_SCALE; 547 } 548 while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) { 549 advance += unifont_glyph_width(codepoint) * UNIFONT_DRAW_SCALE; 550 utext += len; 551 } 552 w += advance; 553 h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 554 } else { 555 const char *utext = ctx->candidates[i]; 556 Uint32 codepoint; 557 size_t len; 558 float advance = 0.0f; 559 560 while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) { 561 advance += unifont_glyph_width(codepoint) * UNIFONT_DRAW_SCALE; 562 utext += len; 563 } 564 w = SDL_max(w, advance); 565 if (i > 0) { 566 h += 2.0f; 567 } 568 h += UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 569 } 570 } 571 572 /* Position the candidate window */ 573 SDL_GetCurrentRenderOutputSize(renderer, &output_w, &output_h); 574 candidatesRect.x = cursorRect->x; 575 candidatesRect.y = cursorRect->y + cursorRect->h + 2.0f; 576 candidatesRect.w = 1.0f + 2.0f + w + 2.0f + 1.0f; 577 candidatesRect.h = 1.0f + 2.0f + h + 2.0f + 1.0f; 578 if ((candidatesRect.x + candidatesRect.w) > output_w) { 579 candidatesRect.x = (output_w - candidatesRect.w); 580 if (candidatesRect.x < 0.0f) { 581 candidatesRect.x = 0.0f; 582 } 583 } 584 585 /* Draw the candidate background */ 586 SDL_SetRenderDrawColor(renderer, 0xAA, 0xAA, 0xAA, 0xFF); 587 SDL_RenderFillRect(renderer, &candidatesRect); 588 SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); 589 SDL_RenderRect(renderer, &candidatesRect); 590 591 /* Draw the candidates */ 592 dstRect.x = candidatesRect.x + 3.0f; 593 dstRect.y = candidatesRect.y + 3.0f; 594 for (i = 0; i < ctx->num_candidates; ++i) { 595 if (!ctx->candidates[i]) { 596 continue; 597 } 598 599 dstRect.w = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 600 dstRect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 601 602 if (ctx->horizontal_candidates) { 603 const char *utext = ctx->candidates[i]; 604 Uint32 codepoint; 605 size_t len; 606 float start; 607 608 if (i > 0) { 609 dstRect.x += unifont_draw_glyph(' ', rendererID, &dstRect) * UNIFONT_DRAW_SCALE; 610 } 611 612 start = dstRect.x + 2 * unifont_glyph_width(' ') * UNIFONT_DRAW_SCALE; 613 while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) { 614 dstRect.x += unifont_draw_glyph(codepoint, rendererID, &dstRect) * UNIFONT_DRAW_SCALE; 615 utext += len; 616 } 617 618 if (i == ctx->selected_candidate) { 619 underlineRect.x = start; 620 underlineRect.y = dstRect.y + dstRect.h - 2; 621 underlineRect.h = 2; 622 underlineRect.w = dstRect.x - start; 623 624 SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a); 625 SDL_RenderFillRect(renderer, &underlineRect); 626 } 627 } else { 628 const char *utext = ctx->candidates[i]; 629 Uint32 codepoint; 630 size_t len; 631 float start; 632 633 dstRect.x = candidatesRect.x + 3.0f; 634 635 start = dstRect.x + 2 * unifont_glyph_width(' ') * UNIFONT_DRAW_SCALE; 636 while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) { 637 dstRect.x += unifont_draw_glyph(codepoint, rendererID, &dstRect) * UNIFONT_DRAW_SCALE; 638 utext += len; 639 } 640 641 if (i == ctx->selected_candidate) { 642 underlineRect.x = start; 643 underlineRect.y = dstRect.y + dstRect.h - 2; 644 underlineRect.h = 2; 645 underlineRect.w = dstRect.x - start; 646 647 SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a); 648 SDL_RenderFillRect(renderer, &underlineRect); 649 } 650 651 if (i > 0) { 652 dstRect.y += 2.0f; 653 } 654 dstRect.y += dstRect.h; 655 } 656 } 657} 658 659static void UpdateTextInputArea(WindowState *ctx, const SDL_FRect *cursorRect) 660{ 661 SDL_Rect rect; 662 int cursor_offset = (int)(cursorRect->x - ctx->textRect.x); 663 664 rect.x = (int)ctx->textRect.x; 665 rect.y = (int)ctx->textRect.y; 666 rect.w = (int)ctx->textRect.w; 667 rect.h = (int)ctx->textRect.h; 668 SDL_SetTextInputArea(ctx->window, &rect, cursor_offset); 669} 670 671static void CleanupVideo(void) 672{ 673 int i; 674 675 for (i = 0; i < state->num_windows; ++i) { 676 WindowState *ctx = &windowstate[i]; 677 678 SDL_StopTextInput(ctx->window); 679 ClearCandidates(ctx); 680 SDL_DestroyProperties(ctx->text_settings); 681 } 682 unifont_cleanup(); 683} 684 685static void DrawSettingsButton(WindowState *ctx) 686{ 687 SDL_Renderer *renderer = ctx->renderer; 688 689 SDL_RenderTexture(renderer, ctx->settings_icon, NULL, &ctx->settings_rect); 690} 691 692static void ToggleSettings(WindowState *ctx) 693{ 694 if (ctx->settings_visible) { 695 ctx->settings_visible = false; 696 SDL_StartTextInputWithProperties(ctx->window, ctx->text_settings); 697 } else { 698 SDL_StopTextInput(ctx->window); 699 ctx->settings_visible = true; 700 } 701} 702 703static int GetDefaultSetting(SDL_PropertiesID props, const char *setting) 704{ 705 if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_TYPE_NUMBER) == 0) { 706 return SDL_TEXTINPUT_TYPE_TEXT; 707 } 708 709 if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER) == 0) { 710 switch (SDL_GetNumberProperty(props, SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT)) { 711 case SDL_TEXTINPUT_TYPE_TEXT: 712 return SDL_CAPITALIZE_SENTENCES; 713 case SDL_TEXTINPUT_TYPE_TEXT_NAME: 714 return SDL_CAPITALIZE_WORDS; 715 default: 716 return SDL_CAPITALIZE_NONE; 717 } 718 } 719 720 if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN) == 0) { 721 return true; 722 } 723 724 if (SDL_strcmp(setting, SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN) == 0) { 725 return true; 726 } 727 728 SDL_assert(!"Unknown setting"); 729 return 0; 730} 731 732static void DrawSettings(WindowState *ctx) 733{ 734 SDL_Renderer *renderer = ctx->renderer; 735 SDL_FRect checkbox; 736 int i; 737 738 checkbox.x = MARGIN; 739 checkbox.y = MARGIN; 740 checkbox.w = (float)FONT_CHARACTER_SIZE; 741 checkbox.h = (float)FONT_CHARACTER_SIZE; 742 743 for (i = 0; i < SDL_arraysize(settings); ++i) { 744 if (settings[i].setting) { 745 int value = (int)SDL_GetNumberProperty(ctx->text_settings, settings[i].setting, GetDefaultSetting(ctx->text_settings, settings[i].setting)); 746 if (value == settings[i].value) { 747 SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); 748 SDL_RenderFillRect(renderer, &checkbox); 749 } 750 SDL_SetRenderDrawColor(renderer, backColor.r, backColor.g, backColor.b, backColor.a); 751 SDL_RenderRect(renderer, &checkbox); 752 SDLTest_DrawString(renderer, checkbox.x + checkbox.w + 8.0f, checkbox.y, settings[i].label); 753 } 754 checkbox.y += LINE_HEIGHT; 755 } 756} 757 758static void ClickSettings(WindowState *ctx, float x, float y) 759{ 760 int setting = (int)SDL_floorf((y - MARGIN) / LINE_HEIGHT); 761 if (setting >= 0 && setting < SDL_arraysize(settings)) { 762 SDL_SetNumberProperty(ctx->text_settings, settings[setting].setting, settings[setting].value); 763 } 764} 765 766static void RedrawWindow(WindowState *ctx) 767{ 768 SDL_Renderer *renderer = ctx->renderer; 769 int rendererID = ctx->rendererID; 770 SDL_FRect drawnTextRect, cursorRect, underlineRect; 771 char text[MAX_TEXT_LENGTH]; 772 773 DrawSettingsButton(ctx); 774 775 if (ctx->settings_visible) { 776 DrawSettings(ctx); 777 return; 778 } 779 780 /* Hide the text if it's a password */ 781 switch ((SDL_TextInputType)SDL_GetNumberProperty(ctx->text_settings, SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_TEXT)) { 782 case SDL_TEXTINPUT_TYPE_TEXT_PASSWORD_HIDDEN: 783 case SDL_TEXTINPUT_TYPE_NUMBER_PASSWORD_HIDDEN: { 784 size_t len = SDL_utf8strlen(ctx->text); 785 SDL_memset(text, '*', len); 786 text[len] = '\0'; 787 break; 788 } 789 default: 790 SDL_strlcpy(text, ctx->text, sizeof(text)); 791 break; 792 } 793 794 if (SDL_TextInputActive(ctx->window)) { 795 SDL_SetRenderDrawColor(renderer, backColor.r, backColor.g, backColor.b, backColor.a); 796 } else { 797 SDL_SetRenderDrawColor(renderer, 0x80, 0x80, 0x80, 0xFF); 798 } 799 SDL_RenderFillRect(renderer, &ctx->textRect); 800 801 /* Initialize the drawn text rectangle for the cursor */ 802 drawnTextRect.x = ctx->textRect.x; 803 drawnTextRect.y = ctx->textRect.y + (ctx->textRect.h - UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE) / 2; 804 drawnTextRect.w = 0.0f; 805 drawnTextRect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 806 807 if (text[0]) { 808 char *utext = text; 809 Uint32 codepoint; 810 size_t len; 811 SDL_FRect dstrect; 812 813 dstrect.x = ctx->textRect.x; 814 dstrect.y = ctx->textRect.y + (ctx->textRect.h - UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE) / 2; 815 dstrect.w = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 816 dstrect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 817 drawnTextRect.y = dstrect.y; 818 drawnTextRect.h = dstrect.h; 819 820 while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) { 821 float advance = unifont_draw_glyph(codepoint, rendererID, &dstrect) * UNIFONT_DRAW_SCALE; 822 dstrect.x += advance; 823 drawnTextRect.w += advance; 824 utext += len; 825 } 826 } 827 828 /* The marked text rectangle is the text area that hasn't been filled by committed text */ 829 ctx->markedRect.x = ctx->textRect.x + drawnTextRect.w; 830 ctx->markedRect.w = ctx->textRect.w - drawnTextRect.w; 831 832 /* Update the drawn text rectangle for composition text, after the committed text */ 833 drawnTextRect.x += drawnTextRect.w; 834 drawnTextRect.w = 0; 835 836 /* Set the cursor to the new location, we'll update it as we go, below */ 837 cursorRect = drawnTextRect; 838 cursorRect.w = 2; 839 cursorRect.h = drawnTextRect.h; 840 841 if (ctx->markedText[0]) { 842 int i = 0; 843 char *utext = ctx->markedText; 844 Uint32 codepoint; 845 size_t len; 846 SDL_FRect dstrect; 847 848 dstrect.x = drawnTextRect.x; 849 dstrect.y = ctx->textRect.y + (ctx->textRect.h - UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE) / 2; 850 dstrect.w = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 851 dstrect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 852 drawnTextRect.y = dstrect.y; 853 drawnTextRect.h = dstrect.h; 854 855 while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) { 856 float advance = unifont_draw_glyph(codepoint, rendererID, &dstrect) * UNIFONT_DRAW_SCALE; 857 dstrect.x += advance; 858 drawnTextRect.w += advance; 859 if (i < ctx->cursor) { 860 cursorRect.x += advance; 861 } 862 i++; 863 utext += len; 864 } 865 866 if (ctx->cursor_length > 0) { 867 cursorRect.w = ctx->cursor_length * UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE; 868 } 869 870 cursorRect.y = drawnTextRect.y; 871 cursorRect.h = drawnTextRect.h; 872 873 underlineRect = ctx->markedRect; 874 underlineRect.y = drawnTextRect.y + drawnTextRect.h - 2; 875 underlineRect.h = 2; 876 underlineRect.w = drawnTextRect.w; 877 878 SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a); 879 SDL_RenderFillRect(renderer, &underlineRect); 880 } 881 882 /* Draw the cursor */ 883 if (SDL_TextInputActive(ctx->window)) { 884 Uint64 now = SDL_GetTicks(); 885 if ((now - ctx->last_cursor_change) >= CURSOR_BLINK_INTERVAL_MS) { 886 ctx->cursor_visible = !ctx->cursor_visible; 887 ctx->last_cursor_change = now; 888 } 889 if (ctx->cursor_length > 0) { 890 /* We'll show a highlight */ 891 SDL_SetRenderDrawBlendMode(renderer, highlight_mode); 892 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 893 SDL_RenderFillRect(renderer, &cursorRect); 894 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE); 895 } else if (ctx->cursor_visible) { 896 SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a); 897 SDL_RenderFillRect(renderer, &cursorRect); 898 } 899 } 900 901 /* Draw the candidates */ 902 DrawCandidates(ctx, &cursorRect); 903 904 /* Update the area used to draw composition UI */ 905 UpdateTextInputArea(ctx, &cursorRect); 906} 907 908static void Redraw(void) 909{ 910 int i; 911 for (i = 0; i < state->num_windows; ++i) { 912 SDL_Renderer *renderer = state->renderers[i]; 913 if (state->windows[i] == NULL) { 914 continue; 915 } 916 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); 917 SDL_RenderClear(renderer); 918 919 RedrawWindow(&windowstate[i]); 920 921 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 922 SDL_RenderDebugTextFormat(renderer, 4, 4, "Window %d", 1 + i); 923 924 SDL_RenderPresent(renderer); 925 } 926} 927 928int main(int argc, char *argv[]) 929{ 930 bool render_composition = false; 931 bool render_candidates = false; 932 int i, done; 933 SDL_Event event; 934 char *fontname = NULL; 935 936 /* Initialize test framework */ 937 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); 938 if (!state) { 939 return 1; 940 } 941 942 /* Parse commandline */ 943 for (i = 1; i < argc;) { 944 int consumed; 945 946 consumed = SDLTest_CommonArg(state, i); 947 if (SDL_strcmp(argv[i], "--font") == 0) { 948 if (*argv[i + 1]) { 949 fontname = argv[i + 1]; 950 consumed = 2; 951 } 952 } else if (SDL_strcmp(argv[i], "--render-composition") == 0) { 953 render_composition = true; 954 consumed = 1; 955 } else if (SDL_strcmp(argv[i], "--render-candidates") == 0) { 956 render_candidates = true; 957 consumed = 1; 958 } 959 if (consumed <= 0) { 960 static const char *options[] = { "[--font fontfile] [--render-composition] [--render-candidates]", NULL }; 961 SDLTest_CommonLogUsage(state, argv[0], options); 962 return 1; 963 } 964 965 i += consumed; 966 } 967 968 if (render_composition && render_candidates) { 969 SDL_SetHint(SDL_HINT_IME_IMPLEMENTED_UI, "composition,candidates"); 970 } else if (render_composition) { 971 SDL_SetHint(SDL_HINT_IME_IMPLEMENTED_UI, "composition"); 972 } else if (render_candidates) { 973 SDL_SetHint(SDL_HINT_IME_IMPLEMENTED_UI, "candidates"); 974 } 975 976 if (!SDLTest_CommonInit(state)) { 977 return 2; 978 } 979 980 windowstate = (WindowState *)SDL_calloc(state->num_windows, sizeof(*windowstate)); 981 if (!windowstate) { 982 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't allocate window state: %s", SDL_GetError()); 983 return -1; 984 } 985 986 fontname = GetResourceFilename(fontname, DEFAULT_FONT); 987 988 if (unifont_init(fontname) < 0) { 989 return -1; 990 } 991 992 SDL_Log("Using font: %s", fontname); 993 994 /* Initialize window state */ 995 for (i = 0; i < state->num_windows; ++i) { 996 WindowState *ctx = &windowstate[i]; 997 SDL_Window *window = state->windows[i]; 998 SDL_Renderer *renderer = state->renderers[i]; 999 1000 SDL_SetRenderLogicalPresentation(renderer, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX); 1001 1002 ctx->window = window; 1003 ctx->renderer = renderer; 1004 ctx->rendererID = i; 1005 ctx->settings_icon = LoadTexture(renderer, "icon.png", true); 1006 if (ctx->settings_icon) { 1007 ctx->settings_rect.w = (float)ctx->settings_icon->w; 1008 ctx->settings_rect.h = (float)ctx->settings_icon->h; 1009 ctx->settings_rect.x = (float)WINDOW_WIDTH - ctx->settings_rect.w - MARGIN; 1010 ctx->settings_rect.y = MARGIN; 1011 } 1012 1013 InitInput(ctx); 1014 1015 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE); 1016 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); 1017 SDL_RenderClear(renderer); 1018 } 1019 highlight_mode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR, 1020 SDL_BLENDFACTOR_ZERO, 1021 SDL_BLENDOPERATION_ADD, 1022 SDL_BLENDFACTOR_ZERO, 1023 SDL_BLENDFACTOR_ONE, 1024 SDL_BLENDOPERATION_ADD); 1025 1026 /* Main render loop */ 1027 done = 0; 1028 while (!done) { 1029 /* Check for events */ 1030 while (SDL_PollEvent(&event)) { 1031 SDLTest_CommonEvent(state, &event, &done); 1032 switch (event.type) { 1033 case SDL_EVENT_MOUSE_BUTTON_UP: { 1034 SDL_FPoint point; 1035 WindowState *ctx = GetWindowStateForWindowID(event.button.windowID); 1036 if (!ctx) { 1037 break; 1038 } 1039 1040 SDL_ConvertEventToRenderCoordinates(ctx->renderer, &event); 1041 point.x = event.button.x; 1042 point.y = event.button.y; 1043 if (SDL_PointInRectFloat(&point, &ctx->settings_rect)) { 1044 ToggleSettings(ctx); 1045 } else if (ctx->settings_visible) { 1046 ClickSettings(ctx, point.x, point.y); 1047 } else { 1048 if (SDL_TextInputActive(ctx->window)) { 1049 SDL_Log("Disabling text input\n"); 1050 SDL_StopTextInput(ctx->window); 1051 } else { 1052 SDL_Log("Enabling text input\n"); 1053 SDL_StartTextInput(ctx->window); 1054 } 1055 } 1056 break; 1057 } 1058 case SDL_EVENT_KEY_DOWN: { 1059 WindowState *ctx = GetWindowStateForWindowID(event.key.windowID); 1060 if (!ctx) { 1061 break; 1062 } 1063 1064 switch (event.key.key) { 1065 case SDLK_RETURN: 1066 ctx->text[0] = 0x00; 1067 break; 1068 case SDLK_BACKSPACE: 1069 /* Only delete text if not in editing mode. */ 1070 if (!ctx->markedText[0]) { 1071 size_t textlen = SDL_strlen(ctx->text); 1072 1073 do { 1074 if (textlen == 0) { 1075 break; 1076 } 1077 if (!(ctx->text[textlen - 1] & 0x80)) { 1078 /* One byte */ 1079 ctx->text[textlen - 1] = 0x00; 1080 break; 1081 } 1082 if ((ctx->text[textlen - 1] & 0xC0) == 0x80) { 1083 /* Byte from the multibyte sequence */ 1084 ctx->text[textlen - 1] = 0x00; 1085 textlen--; 1086 } 1087 if ((ctx->text[textlen - 1] & 0xC0) == 0xC0) { 1088 /* First byte of multibyte sequence */ 1089 ctx->text[textlen - 1] = 0x00; 1090 break; 1091 } 1092 } while (1); 1093 } 1094 break; 1095 default: 1096 if ((event.key.mod & SDL_KMOD_CTRL) && (event.key.key >= SDLK_KP_1 && event.key.key <= SDLK_KP_9)) { 1097 int index = (event.key.key - SDLK_KP_1); 1098 if (index < state->num_windows) { 1099 SDL_Window *window = state->windows[index]; 1100 if (SDL_TextInputActive(window)) { 1101 SDL_Log("Disabling text input for window %d\n", 1 + index); 1102 SDL_StopTextInput(window); 1103 } else { 1104 SDL_Log("Enabling text input for window %d\n", 1 + index); 1105 SDL_StartTextInput(window); 1106 } 1107 } 1108 } 1109 break; 1110 } 1111 1112 if (done) { 1113 break; 1114 } 1115 1116 SDL_Log("Keyboard: scancode 0x%08X = %s, keycode 0x%08" SDL_PRIX32 " = %s", 1117 event.key.scancode, 1118 SDL_GetScancodeName(event.key.scancode), 1119 SDL_static_cast(Uint32, event.key.key), 1120 SDL_GetKeyName(event.key.key)); 1121 break; 1122 } 1123 case SDL_EVENT_TEXT_INPUT: { 1124 WindowState *ctx = GetWindowStateForWindowID(event.text.windowID); 1125 if (!ctx) { 1126 break; 1127 } 1128 1129 if (event.text.text[0] == '\0' || event.text.text[0] == '\n' || ctx->markedRect.w < 0) { 1130 break; 1131 } 1132 1133 SDL_Log("Keyboard: text input \"%s\"", event.text.text); 1134 1135 if (SDL_strlen(ctx->text) + SDL_strlen(event.text.text) < sizeof(ctx->text)) { 1136 SDL_strlcat(ctx->text, event.text.text, sizeof(ctx->text)); 1137 } 1138 1139 SDL_Log("text inputted: %s", ctx->text); 1140 1141 /* After text inputted, we can clear up markedText because it */ 1142 /* is committed */ 1143 ctx->markedText[0] = 0; 1144 break; 1145 } 1146 case SDL_EVENT_TEXT_EDITING: { 1147 WindowState *ctx = GetWindowStateForWindowID(event.edit.windowID); 1148 if (!ctx) { 1149 break; 1150 } 1151 1152 SDL_Log("text editing \"%s\", selected range (%" SDL_PRIs32 ", %" SDL_PRIs32 ")", 1153 event.edit.text, event.edit.start, event.edit.length); 1154 1155 SDL_strlcpy(ctx->markedText, event.edit.text, sizeof(ctx->markedText)); 1156 ctx->cursor = event.edit.start; 1157 ctx->cursor_length = event.edit.length; 1158 break; 1159 } 1160 case SDL_EVENT_TEXT_EDITING_CANDIDATES: { 1161 WindowState *ctx = GetWindowStateForWindowID(event.edit.windowID); 1162 if (!ctx) { 1163 break; 1164 } 1165 1166 SDL_Log("text candidates:"); 1167 for (i = 0; i < event.edit_candidates.num_candidates; ++i) { 1168 SDL_Log("%c%s", i == event.edit_candidates.selected_candidate ? '>' : ' ', event.edit_candidates.candidates[i]); 1169 } 1170 1171 ClearCandidates(ctx); 1172 SaveCandidates(ctx, &event); 1173 break; 1174 } 1175 default: 1176 break; 1177 } 1178 } 1179 1180 Redraw(); 1181 } 1182 SDL_free(fontname); 1183 CleanupVideo(); 1184 SDLTest_CommonQuit(state); 1185 return 0; 1186} 1187[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.