You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

166 lines
4.7 KiB

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