history.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2021
  4. Free Software Foundation, Inc.
  5. Authors:
  6. Radek Doulik, 1994, 1995
  7. Miguel de Icaza, 1994, 1995
  8. Jakub Jelinek, 1995
  9. Andrej Borsenkow, 1996
  10. Norbert Warmuth, 1997
  11. Andrew Borodin <aborodin@vmail.ru>, 2009-2019
  12. This file is part of the Midnight Commander.
  13. The Midnight Commander is free software: you can redistribute it
  14. and/or modify it under the terms of the GNU General Public License as
  15. published by the Free Software Foundation, either version 3 of the License,
  16. or (at your option) any later version.
  17. The Midnight Commander is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. /** \file history.c
  25. * \brief Source: show history
  26. */
  27. #include <config.h>
  28. #include <stdlib.h>
  29. #include <sys/types.h>
  30. #include "lib/global.h"
  31. #include "lib/tty/tty.h" /* LINES, COLS */
  32. #include "lib/strutil.h"
  33. #include "lib/widget.h"
  34. #include "lib/keybind.h" /* CK_* */
  35. /*** global variables ****************************************************************************/
  36. /*** file scope macro definitions ****************************************************************/
  37. #define B_VIEW (B_USER + 1)
  38. #define B_EDIT (B_USER + 2)
  39. /*** file scope type declarations ****************************************************************/
  40. typedef struct
  41. {
  42. int y;
  43. int x;
  44. size_t count;
  45. size_t max_width;
  46. } history_dlg_data;
  47. /*** file scope variables ************************************************************************/
  48. /*** file scope functions ************************************************************************/
  49. static cb_ret_t
  50. history_dlg_reposition (WDialog * dlg_head)
  51. {
  52. history_dlg_data *data;
  53. int x = 0, y, he, wi;
  54. WRect r;
  55. /* guard checks */
  56. if ((dlg_head == NULL) || (dlg_head->data == NULL))
  57. return MSG_NOT_HANDLED;
  58. data = (history_dlg_data *) dlg_head->data;
  59. y = data->y;
  60. he = data->count + 2;
  61. if (he <= y || y > (LINES - 6))
  62. {
  63. he = MIN (he, y - 1);
  64. y -= he;
  65. }
  66. else
  67. {
  68. y++;
  69. he = MIN (he, LINES - y);
  70. }
  71. if (data->x > 2)
  72. x = data->x - 2;
  73. wi = data->max_width + 4;
  74. if ((wi + x) > COLS)
  75. {
  76. wi = MIN (wi, COLS);
  77. x = COLS - wi;
  78. }
  79. rect_init (&r, y, x, he, wi);
  80. return dlg_default_callback (WIDGET (dlg_head), NULL, MSG_RESIZE, 0, &r);
  81. }
  82. /* --------------------------------------------------------------------------------------------- */
  83. static cb_ret_t
  84. history_dlg_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  85. {
  86. switch (msg)
  87. {
  88. case MSG_RESIZE:
  89. return history_dlg_reposition (DIALOG (w));
  90. case MSG_NOTIFY:
  91. {
  92. /* message from listbox */
  93. WDialog *d = DIALOG (w);
  94. switch (parm)
  95. {
  96. case CK_View:
  97. d->ret_value = B_VIEW;
  98. break;
  99. case CK_Edit:
  100. d->ret_value = B_EDIT;
  101. break;
  102. case CK_Enter:
  103. d->ret_value = B_ENTER;
  104. break;
  105. default:
  106. return MSG_NOT_HANDLED;
  107. }
  108. dlg_stop (d);
  109. return MSG_HANDLED;
  110. }
  111. default:
  112. return dlg_default_callback (w, sender, msg, parm, data);
  113. }
  114. }
  115. /* --------------------------------------------------------------------------------------------- */
  116. static void
  117. history_create_item (history_descriptor_t * hd, void *data)
  118. {
  119. char *text = (char *) data;
  120. size_t width;
  121. width = str_term_width1 (text);
  122. hd->max_width = MAX (width, hd->max_width);
  123. listbox_add_item (hd->listbox, LISTBOX_APPEND_AT_END, 0, text, NULL, TRUE);
  124. }
  125. /* --------------------------------------------------------------------------------------------- */
  126. static void *
  127. history_release_item (history_descriptor_t * hd, WLEntry * le)
  128. {
  129. void *text;
  130. (void) hd;
  131. text = le->text;
  132. le->text = NULL;
  133. return text;
  134. }
  135. /* --------------------------------------------------------------------------------------------- */
  136. /*** public functions ****************************************************************************/
  137. /* --------------------------------------------------------------------------------------------- */
  138. void
  139. history_descriptor_init (history_descriptor_t * hd, int y, int x, GList * history, int current)
  140. {
  141. hd->list = history;
  142. hd->y = y;
  143. hd->x = x;
  144. hd->current = current;
  145. hd->action = CK_IgnoreKey;
  146. hd->text = NULL;
  147. hd->max_width = 0;
  148. hd->listbox = listbox_new (1, 1, 2, 2, TRUE, NULL);
  149. /* in most cases history list contains string only and no any other data */
  150. hd->create = history_create_item;
  151. hd->release = history_release_item;
  152. hd->free = g_free;
  153. }
  154. /* --------------------------------------------------------------------------------------------- */
  155. void
  156. history_show (history_descriptor_t * hd)
  157. {
  158. GList *z, *hi;
  159. size_t count;
  160. WDialog *query_dlg;
  161. history_dlg_data hist_data;
  162. int dlg_ret;
  163. if (hd == NULL || hd->list == NULL)
  164. return;
  165. hd->max_width = str_term_width1 (_("History")) + 2;
  166. for (z = hd->list; z != NULL; z = g_list_previous (z))
  167. hd->create (hd, z->data);
  168. /* after this, the order of history items is following: recent at begin, oldest at end */
  169. count = listbox_get_length (hd->listbox);
  170. hist_data.y = hd->y;
  171. hist_data.x = hd->x;
  172. hist_data.count = count;
  173. hist_data.max_width = hd->max_width;
  174. query_dlg =
  175. dlg_create (TRUE, 0, 0, 4, 4, WPOS_KEEP_DEFAULT, TRUE, dialog_colors, history_dlg_callback,
  176. NULL, "[History-query]", _("History"));
  177. query_dlg->data = &hist_data;
  178. /* this call makes list stick to all sides of dialog, effectively make
  179. it be resized with dialog */
  180. group_add_widget_autopos (GROUP (query_dlg), hd->listbox, WPOS_KEEP_ALL, NULL);
  181. /* to avoid diplicating of (calculating sizes in two places)
  182. code, call history_dlg_callback function here, to set dialog and
  183. controls positions.
  184. The main idea - create 4x4 dialog and add 2x2 list in
  185. center of it, and let dialog function resize it to needed size. */
  186. send_message (query_dlg, NULL, MSG_RESIZE, 0, NULL);
  187. if (WIDGET (query_dlg)->y < hd->y)
  188. {
  189. /* history is above base widget -- revert order to place recent item at bottom */
  190. /* revert history direction */
  191. g_queue_reverse (hd->listbox->list);
  192. if (hd->current < 0 || (size_t) hd->current >= count)
  193. listbox_select_last (hd->listbox);
  194. else
  195. listbox_select_entry (hd->listbox, count - 1 - (size_t) hd->current);
  196. }
  197. else
  198. {
  199. /* history is below base widget -- keep order to place recent item on top */
  200. if (hd->current > 0)
  201. listbox_select_entry (hd->listbox, hd->current);
  202. }
  203. dlg_ret = dlg_run (query_dlg);
  204. if (dlg_ret != B_CANCEL)
  205. {
  206. char *q;
  207. switch (dlg_ret)
  208. {
  209. case B_EDIT:
  210. hd->action = CK_Edit;
  211. break;
  212. case B_VIEW:
  213. hd->action = CK_View;
  214. break;
  215. default:
  216. hd->action = CK_Enter;
  217. }
  218. listbox_get_current (hd->listbox, &q, NULL);
  219. hd->text = g_strdup (q);
  220. }
  221. /* get modified history from dialog */
  222. z = NULL;
  223. for (hi = listbox_get_first_link (hd->listbox); hi != NULL; hi = g_list_next (hi))
  224. /* history is being reverted here again */
  225. z = g_list_prepend (z, hd->release (hd, LENTRY (hi->data)));
  226. /* restore history direction */
  227. if (WIDGET (query_dlg)->y < hd->y)
  228. z = g_list_reverse (z);
  229. widget_destroy (WIDGET (query_dlg));
  230. hd->list = g_list_first (hd->list);
  231. g_list_free_full (hd->list, hd->free);
  232. hd->list = g_list_last (z);
  233. }
  234. /* --------------------------------------------------------------------------------------------- */