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.
 
 
 
 
 

177 lines
4.1 KiB

  1. /*
  2. * neercs console-based window manager
  3. * Copyright (c) 2009-2010 Jean-Yves Lamoureux <jylam@lnxscene.org>
  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 "widgets.h"
  13. static void widget_ibox_add_char(struct input_box *box, unsigned int c);
  14. static void widget_ibox_del_char(struct input_box *box);
  15. struct input_box *widget_ibox_init(caca_canvas_t *cv, int w, int h)
  16. {
  17. struct input_box *box = (struct input_box *)malloc(sizeof(struct input_box));
  18. if (!box)
  19. return NULL;
  20. box->cv = cv;
  21. box->w = w;
  22. box->h = h;
  23. box->command = NULL;
  24. box->output_err = NULL;
  25. box->output_res = NULL;
  26. return box;
  27. }
  28. int widget_ibox_draw(struct input_box *box)
  29. {
  30. int x = (caca_get_canvas_width(box->cv) - box->w) / 2;
  31. int y = (caca_get_canvas_height(box->cv) - box->h) / 2;
  32. caca_set_color_ansi(box->cv, CACA_BLUE, CACA_BLUE);
  33. caca_fill_box(box->cv, x, y, box->w, box->h, '#');
  34. caca_set_color_ansi(box->cv, CACA_DEFAULT, CACA_BLUE);
  35. caca_draw_cp437_box(box->cv, x, y, box->w, box->h);
  36. caca_printf(box->cv, x, y, "Mini-command");
  37. caca_printf(box->cv, x + 2, y + 2,
  38. "[___________________________________________________________]");
  39. if (box->command)
  40. {
  41. caca_printf(box->cv, x + 3, y + 2, "%s", box->command);
  42. caca_gotoxy(box->cv, x + 3 + box->x, y + 2);
  43. }
  44. else
  45. {
  46. caca_gotoxy(box->cv, x + 3, y + 2);
  47. }
  48. if (box->output_err)
  49. {
  50. caca_set_color_ansi(box->cv, CACA_RED, CACA_BLUE);
  51. caca_printf(box->cv, x + 2, y + 4, box->output_err);
  52. }
  53. if (box->output_res)
  54. {
  55. caca_set_color_ansi(box->cv, CACA_LIGHTGREEN, CACA_BLUE);
  56. caca_printf(box->cv, x + 2, y + 4, box->output_res);
  57. }
  58. return 0;
  59. }
  60. char *widget_ibox_get_text(struct input_box *box)
  61. {
  62. return box->command;
  63. }
  64. void widget_ibox_destroy(struct input_box *box)
  65. {
  66. if(!box) return;
  67. if (box->command)
  68. free(box->command);
  69. if (box->output_err)
  70. free(box->output_err);
  71. if (box->output_res)
  72. free(box->output_res);
  73. }
  74. void widget_ibox_set_error(struct input_box *box, char *err)
  75. {
  76. box->output_err = err;
  77. }
  78. void widget_ibox_set_msg(struct input_box *box, char *msg)
  79. {
  80. box->output_res = msg;
  81. }
  82. int widget_ibox_handle_key(struct input_box *box, unsigned int c)
  83. {
  84. if (c == CACA_KEY_ESCAPE)
  85. {
  86. if (box->command)
  87. {
  88. free(box->command);
  89. box->command = NULL;
  90. }
  91. return INPUT_BOX_ESC;
  92. }
  93. else if (c == CACA_KEY_LEFT)
  94. {
  95. if (box->x)
  96. box->x--;
  97. }
  98. else if (c == CACA_KEY_RIGHT)
  99. {
  100. if (box->x < box->size - 1)
  101. box->x++;
  102. }
  103. else if (c == CACA_KEY_RETURN)
  104. {
  105. return INPUT_BOX_RET;
  106. }
  107. else
  108. {
  109. if (c >= ' ' && c < 127)
  110. widget_ibox_add_char(box, c);
  111. else if (c == 8)
  112. {
  113. widget_ibox_del_char(box);
  114. }
  115. }
  116. return INPUT_BOX_NOTHING;
  117. }
  118. static void widget_ibox_add_char(struct input_box *box, unsigned int c)
  119. {
  120. /* FIXME handle return values */
  121. if (!box->command)
  122. {
  123. box->size = 1;
  124. box->x = 0;
  125. box->command = (char *)malloc(2);
  126. box->command[0] = 0;
  127. }
  128. else
  129. {
  130. box->command = (char *)realloc(box->command, box->size + 1);
  131. }
  132. memmove(&box->command[box->x + 1],
  133. &box->command[box->x], (box->size - box->x));
  134. box->command[box->x] = c;
  135. box->x++;
  136. box->size++;
  137. }
  138. static void widget_ibox_del_char(struct input_box *box)
  139. {
  140. if (box->x < 1)
  141. return;
  142. if (box->size > 1)
  143. box->size--;
  144. else
  145. return;
  146. memcpy(&box->command[box->x - 1], &box->command[box->x], box->size - box->x);
  147. box->command = (char *)realloc(box->command, box->size);
  148. if (box->x)
  149. box->x--;
  150. box->command[box->size - 1] = 0;
  151. }