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.
 
 
 
 
 
 

147 lines
4.1 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; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. #include "config.h"
  14. #include "common.h"
  15. #if defined(HAVE_INTTYPES_H)
  16. # include <inttypes.h>
  17. #endif
  18. #include <string.h>
  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(void)
  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. dp = caca_create_display(cv);
  36. cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
  37. cucul_putstr(cv, 1, 1, "Text entries - press tab to cycle");
  38. for(i = 0; i < TEXT_ENTRIES; i++)
  39. {
  40. entries[i].buffer[0] = 0;
  41. entries[i].size = 0;
  42. entries[i].cursor = 0;
  43. }
  44. while(running)
  45. {
  46. caca_event_t ev;
  47. for(i = 0; i < TEXT_ENTRIES; i++)
  48. {
  49. unsigned int j, start, size;
  50. cucul_set_color(cv, CUCUL_COLOR_BLACK, CUCUL_COLOR_LIGHTGRAY);
  51. cucul_fill_box(cv, 2, 3 * i + 4, 2 + BUFFER_SIZE, 3 * i + 4, " ");
  52. start = 0;
  53. size = entries[i].size;
  54. for(j = 0; j < size; j++)
  55. {
  56. cucul_putchar(cv, 2 + j, 3 * i + 4,
  57. entries[i].buffer[start + j]);
  58. }
  59. }
  60. /* Put the cursor on the active textentry */
  61. cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_WHITE);
  62. cucul_putchar(cv, 2 + entries[e].cursor, 3 * e + 4, ' ');
  63. caca_refresh_display(dp);
  64. if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1) == 0)
  65. continue;
  66. switch(ev.data.key.ch)
  67. {
  68. case CACA_KEY_ESCAPE:
  69. running = 0;
  70. break;
  71. case CACA_KEY_TAB:
  72. case CACA_KEY_RETURN:
  73. e = (e + 1) % TEXT_ENTRIES;
  74. break;
  75. case CACA_KEY_HOME:
  76. entries[e].cursor = 0;
  77. break;
  78. case CACA_KEY_END:
  79. entries[e].cursor = entries[e].size;
  80. break;
  81. case CACA_KEY_LEFT:
  82. if(entries[e].cursor)
  83. entries[e].cursor--;
  84. break;
  85. case CACA_KEY_RIGHT:
  86. if(entries[e].cursor < entries[e].size)
  87. entries[e].cursor++;
  88. break;
  89. case CACA_KEY_DELETE:
  90. if(entries[e].cursor < entries[e].size)
  91. {
  92. memmove(entries[e].buffer + entries[e].cursor,
  93. entries[e].buffer + entries[e].cursor + 1,
  94. (entries[e].size - entries[e].cursor + 1) * 4);
  95. entries[e].size--;
  96. }
  97. break;
  98. case CACA_KEY_BACKSPACE:
  99. if(entries[e].cursor)
  100. {
  101. memmove(entries[e].buffer + entries[e].cursor - 1,
  102. entries[e].buffer + entries[e].cursor,
  103. (entries[e].size - entries[e].cursor) * 4);
  104. entries[e].size--;
  105. entries[e].cursor--;
  106. }
  107. break;
  108. default:
  109. if(entries[e].size < BUFFER_SIZE)
  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].buffer[entries[e].cursor] = ev.data.key.utf32;
  115. entries[e].size++;
  116. entries[e].cursor++;
  117. }
  118. break;
  119. }
  120. }
  121. caca_free_display(dp);
  122. cucul_free_canvas(cv);
  123. return 0;
  124. }