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.
 
 
 
 
 
 

156 lines
4.4 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;
  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. }
  54. while(running)
  55. {
  56. caca_event_t ev;
  57. for(i = 0; i < TEXT_ENTRIES; i++)
  58. {
  59. unsigned int j, start, size;
  60. caca_set_color_ansi(cv, CACA_BLACK, CACA_LIGHTGRAY);
  61. caca_fill_box(cv, 2, 3 * i + 4, 2 + BUFFER_SIZE, 3 * i + 4, ' ');
  62. start = 0;
  63. size = entries[i].size;
  64. for(j = 0; j < size; j++)
  65. {
  66. caca_put_char(cv, 2 + j, 3 * i + 4,
  67. entries[i].buffer[start + j]);
  68. }
  69. }
  70. /* Put the cursor on the active textentry */
  71. caca_gotoxy(cv, 2 + entries[e].cursor, 3 * e + 4);
  72. caca_refresh_display(dp);
  73. if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1) == 0)
  74. continue;
  75. switch(caca_get_event_key_ch(&ev))
  76. {
  77. case CACA_KEY_ESCAPE:
  78. running = 0;
  79. break;
  80. case CACA_KEY_TAB:
  81. case CACA_KEY_RETURN:
  82. e = (e + 1) % TEXT_ENTRIES;
  83. break;
  84. case CACA_KEY_HOME:
  85. entries[e].cursor = 0;
  86. break;
  87. case CACA_KEY_END:
  88. entries[e].cursor = entries[e].size;
  89. break;
  90. case CACA_KEY_LEFT:
  91. if(entries[e].cursor)
  92. entries[e].cursor--;
  93. break;
  94. case CACA_KEY_RIGHT:
  95. if(entries[e].cursor < entries[e].size)
  96. entries[e].cursor++;
  97. break;
  98. case CACA_KEY_DELETE:
  99. if(entries[e].cursor < entries[e].size)
  100. {
  101. memmove(entries[e].buffer + entries[e].cursor,
  102. entries[e].buffer + entries[e].cursor + 1,
  103. (entries[e].size - entries[e].cursor + 1) * 4);
  104. entries[e].size--;
  105. }
  106. break;
  107. case CACA_KEY_BACKSPACE:
  108. if(entries[e].cursor)
  109. {
  110. memmove(entries[e].buffer + entries[e].cursor - 1,
  111. entries[e].buffer + entries[e].cursor,
  112. (entries[e].size - entries[e].cursor) * 4);
  113. entries[e].size--;
  114. entries[e].cursor--;
  115. }
  116. break;
  117. default:
  118. if(entries[e].size < BUFFER_SIZE)
  119. {
  120. memmove(entries[e].buffer + entries[e].cursor + 1,
  121. entries[e].buffer + entries[e].cursor,
  122. (entries[e].size - entries[e].cursor) * 4);
  123. entries[e].buffer[entries[e].cursor] =
  124. caca_get_event_key_utf32(&ev);
  125. entries[e].size++;
  126. entries[e].cursor++;
  127. }
  128. break;
  129. }
  130. }
  131. caca_free_display(dp);
  132. caca_free_canvas(cv);
  133. return 0;
  134. }