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.
 
 
 
 
 
 

157 line
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 "cucul.h"
  20. #include "caca.h"
  21. #define BUFFER_SIZE 75
  22. #define TEXT_ENTRIES 5
  23. typedef struct textentry
  24. {
  25. uint32_t buffer[BUFFER_SIZE + 1];
  26. unsigned int size, cursor;
  27. } textentry;
  28. int main(int argc, char *argv[])
  29. {
  30. textentry entries[TEXT_ENTRIES];
  31. cucul_canvas_t *cv;
  32. caca_display_t *dp;
  33. unsigned int i, e = 0, running = 1;
  34. cv = cucul_create_canvas(0, 0);
  35. if(cv == NULL)
  36. {
  37. printf("Can't create canvas\n");
  38. return -1;
  39. }
  40. dp = caca_create_display(cv);
  41. if(dp == NULL)
  42. {
  43. printf("Can't create display\n");
  44. return -1;
  45. }
  46. caca_set_cursor(dp, 1);
  47. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
  48. cucul_put_str(cv, 1, 1, "Text entries - press tab to cycle");
  49. for(i = 0; i < TEXT_ENTRIES; i++)
  50. {
  51. entries[i].buffer[0] = 0;
  52. entries[i].size = 0;
  53. entries[i].cursor = 0;
  54. }
  55. while(running)
  56. {
  57. caca_event_t ev;
  58. for(i = 0; i < TEXT_ENTRIES; i++)
  59. {
  60. unsigned int j, start, size;
  61. cucul_set_color_ansi(cv, CUCUL_BLACK, CUCUL_LIGHTGRAY);
  62. cucul_fill_box(cv, 2, 3 * i + 4, 2 + BUFFER_SIZE, 3 * i + 4, ' ');
  63. start = 0;
  64. size = entries[i].size;
  65. for(j = 0; j < size; j++)
  66. {
  67. cucul_put_char(cv, 2 + j, 3 * i + 4,
  68. entries[i].buffer[start + j]);
  69. }
  70. }
  71. /* Put the cursor on the active textentry */
  72. cucul_gotoxy(cv, 2 + entries[e].cursor, 3 * e + 4);
  73. caca_refresh_display(dp);
  74. if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1) == 0)
  75. continue;
  76. switch(caca_get_event_key_ch(&ev))
  77. {
  78. case CACA_KEY_ESCAPE:
  79. running = 0;
  80. break;
  81. case CACA_KEY_TAB:
  82. case CACA_KEY_RETURN:
  83. e = (e + 1) % TEXT_ENTRIES;
  84. break;
  85. case CACA_KEY_HOME:
  86. entries[e].cursor = 0;
  87. break;
  88. case CACA_KEY_END:
  89. entries[e].cursor = entries[e].size;
  90. break;
  91. case CACA_KEY_LEFT:
  92. if(entries[e].cursor)
  93. entries[e].cursor--;
  94. break;
  95. case CACA_KEY_RIGHT:
  96. if(entries[e].cursor < entries[e].size)
  97. entries[e].cursor++;
  98. break;
  99. case CACA_KEY_DELETE:
  100. if(entries[e].cursor < entries[e].size)
  101. {
  102. memmove(entries[e].buffer + entries[e].cursor,
  103. entries[e].buffer + entries[e].cursor + 1,
  104. (entries[e].size - entries[e].cursor + 1) * 4);
  105. entries[e].size--;
  106. }
  107. break;
  108. case CACA_KEY_BACKSPACE:
  109. if(entries[e].cursor)
  110. {
  111. memmove(entries[e].buffer + entries[e].cursor - 1,
  112. entries[e].buffer + entries[e].cursor,
  113. (entries[e].size - entries[e].cursor) * 4);
  114. entries[e].size--;
  115. entries[e].cursor--;
  116. }
  117. break;
  118. default:
  119. if(entries[e].size < BUFFER_SIZE)
  120. {
  121. memmove(entries[e].buffer + entries[e].cursor + 1,
  122. entries[e].buffer + entries[e].cursor,
  123. (entries[e].size - entries[e].cursor) * 4);
  124. entries[e].buffer[entries[e].cursor] =
  125. caca_get_event_key_utf32(&ev);
  126. entries[e].size++;
  127. entries[e].cursor++;
  128. }
  129. break;
  130. }
  131. }
  132. caca_free_display(dp);
  133. cucul_free_canvas(cv);
  134. return 0;
  135. }