選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

174 行
5.2 KiB

  1. /*
  2. * input libcaca text input test program
  3. * Copyright (c) 2006-2012 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * This program is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What the Fuck You Want
  9. * to Public License, Version 2, as published by Sam Hocevar. See
  10. * http://www.wtfpl.net/ for more details.
  11. */
  12. #include "config.h"
  13. #if !defined(__KERNEL__)
  14. # include <string.h>
  15. # include <stdio.h>
  16. #endif
  17. #include "caca.h"
  18. #define BUFFER_SIZE 75
  19. #define TEXT_ENTRIES 5
  20. typedef struct textentry
  21. {
  22. uint32_t buffer[BUFFER_SIZE + 1];
  23. unsigned int size, cursor, changed;
  24. } textentry;
  25. int main(int argc, char *argv[])
  26. {
  27. textentry entries[TEXT_ENTRIES];
  28. caca_canvas_t *cv;
  29. caca_display_t *dp;
  30. unsigned int i, e = 0, running = 1;
  31. cv = caca_create_canvas(0, 0);
  32. if(cv == NULL)
  33. {
  34. printf("Can't create canvas\n");
  35. return -1;
  36. }
  37. dp = caca_create_display(cv);
  38. if(dp == NULL)
  39. {
  40. printf("Can't create display\n");
  41. return -1;
  42. }
  43. caca_set_cursor(dp, 1);
  44. caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
  45. caca_put_str(cv, 1, 1, "Text entries - press tab to cycle");
  46. for(i = 0; i < TEXT_ENTRIES; i++)
  47. {
  48. entries[i].buffer[0] = 0;
  49. entries[i].size = 0;
  50. entries[i].cursor = 0;
  51. entries[i].changed = 1;
  52. caca_printf(cv, 3, 3 * i + 4, "[entry %i]", i + 1);
  53. }
  54. /* Put Unicode crap in the last text entry */
  55. entries[TEXT_ENTRIES - 1].buffer[0] = 'A';
  56. entries[TEXT_ENTRIES - 1].buffer[1] = 'b';
  57. entries[TEXT_ENTRIES - 1].buffer[2] = caca_utf8_to_utf32("Ç", NULL);
  58. entries[TEXT_ENTRIES - 1].buffer[3] = caca_utf8_to_utf32("đ", NULL);
  59. entries[TEXT_ENTRIES - 1].buffer[4] = caca_utf8_to_utf32("ボ", NULL);
  60. entries[TEXT_ENTRIES - 1].buffer[5] = CACA_MAGIC_FULLWIDTH;
  61. entries[TEXT_ENTRIES - 1].buffer[6] = caca_utf8_to_utf32("♥", NULL);
  62. entries[TEXT_ENTRIES - 1].size = 7;
  63. while(running)
  64. {
  65. caca_event_t ev;
  66. for(i = 0; i < TEXT_ENTRIES; i++)
  67. {
  68. unsigned int j, start, size;
  69. if(!entries[i].changed)
  70. continue;
  71. caca_set_color_ansi(cv, CACA_BLACK, CACA_LIGHTGRAY);
  72. caca_fill_box(cv, 2, 3 * i + 5, BUFFER_SIZE + 1, 1, ' ');
  73. start = 0;
  74. size = entries[i].size;
  75. for(j = 0; j < size; j++)
  76. {
  77. caca_put_char(cv, 2 + j, 3 * i + 5,
  78. entries[i].buffer[start + j]);
  79. }
  80. entries[i].changed = 0;
  81. }
  82. /* Put the cursor on the active textentry */
  83. caca_gotoxy(cv, 2 + entries[e].cursor, 3 * e + 5);
  84. caca_refresh_display(dp);
  85. if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1) == 0)
  86. continue;
  87. switch(caca_get_event_key_ch(&ev))
  88. {
  89. case CACA_KEY_ESCAPE:
  90. running = 0;
  91. break;
  92. case CACA_KEY_TAB:
  93. case CACA_KEY_RETURN:
  94. e = (e + 1) % TEXT_ENTRIES;
  95. break;
  96. case CACA_KEY_HOME:
  97. entries[e].cursor = 0;
  98. break;
  99. case CACA_KEY_END:
  100. entries[e].cursor = entries[e].size;
  101. break;
  102. case CACA_KEY_LEFT:
  103. if(entries[e].cursor)
  104. entries[e].cursor--;
  105. break;
  106. case CACA_KEY_RIGHT:
  107. if(entries[e].cursor < entries[e].size)
  108. entries[e].cursor++;
  109. break;
  110. case CACA_KEY_DELETE:
  111. if(entries[e].cursor < entries[e].size)
  112. {
  113. memmove(entries[e].buffer + entries[e].cursor,
  114. entries[e].buffer + entries[e].cursor + 1,
  115. (entries[e].size - entries[e].cursor + 1) * 4);
  116. entries[e].size--;
  117. entries[e].changed = 1;
  118. }
  119. break;
  120. case CACA_KEY_BACKSPACE:
  121. if(entries[e].cursor)
  122. {
  123. memmove(entries[e].buffer + entries[e].cursor - 1,
  124. entries[e].buffer + entries[e].cursor,
  125. (entries[e].size - entries[e].cursor) * 4);
  126. entries[e].size--;
  127. entries[e].cursor--;
  128. entries[e].changed = 1;
  129. }
  130. break;
  131. default:
  132. if(entries[e].size < BUFFER_SIZE)
  133. {
  134. memmove(entries[e].buffer + entries[e].cursor + 1,
  135. entries[e].buffer + entries[e].cursor,
  136. (entries[e].size - entries[e].cursor) * 4);
  137. entries[e].buffer[entries[e].cursor] =
  138. caca_get_event_key_utf32(&ev);
  139. entries[e].size++;
  140. entries[e].cursor++;
  141. entries[e].changed = 1;
  142. }
  143. break;
  144. }
  145. }
  146. caca_free_display(dp);
  147. caca_free_canvas(cv);
  148. return 0;
  149. }